input/clock: (docs) fix old function name
[vlc.git] / modules / access / decklink.cpp
blob5d54b15d874e44e05c57ee8884b35767f9e1e718
1 /*****************************************************************************
2 * decklink.cpp: BlackMagic DeckLink SDI input module
3 *****************************************************************************
4 * Copyright (C) 2010 Steinar H. Gunderson
5 * Copyright (C) 2012-2014 Rafaël Carré
7 * Authors: Steinar H. Gunderson <steinar+vlc@gunderson.no>
8 Rafaël Carré <funman@videolanorg>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_demux.h>
33 #include <arpa/inet.h>
35 #include <DeckLinkAPI.h>
36 #include <DeckLinkAPIDispatch.cpp>
38 #include "sdi.h"
40 #include <atomic>
42 static int Open (vlc_object_t *);
43 static void Close(vlc_object_t *);
45 #define CARD_INDEX_TEXT N_("Input card to use")
46 #define CARD_INDEX_LONGTEXT N_( \
47 "DeckLink capture card to use, if multiple exist. " \
48 "The cards are numbered from 0.")
50 #define MODE_TEXT N_("Desired input video mode. Leave empty for autodetection.")
51 #define MODE_LONGTEXT N_( \
52 "Desired input video mode for DeckLink captures. " \
53 "This value should be a FOURCC code in textual " \
54 "form, e.g. \"ntsc\".")
56 #define AUDIO_CONNECTION_TEXT N_("Audio connection")
57 #define AUDIO_CONNECTION_LONGTEXT N_( \
58 "Audio connection to use for DeckLink captures. " \
59 "Valid choices: embedded, aesebu, analog. " \
60 "Leave blank for card default.")
62 #define RATE_TEXT N_("Audio samplerate (Hz)")
63 #define RATE_LONGTEXT N_( \
64 "Audio sampling rate (in hertz) for DeckLink captures. " \
65 "0 disables audio input.")
67 #define CHANNELS_TEXT N_("Number of audio channels")
68 #define CHANNELS_LONGTEXT N_( \
69 "Number of input audio channels for DeckLink captures. " \
70 "Must be 2, 8 or 16. 0 disables audio input.")
72 #define VIDEO_CONNECTION_TEXT N_("Video connection")
73 #define VIDEO_CONNECTION_LONGTEXT N_( \
74 "Video connection to use for DeckLink captures. " \
75 "Valid choices: sdi, hdmi, opticalsdi, component, " \
76 "composite, svideo. " \
77 "Leave blank for card default.")
79 static const char *const ppsz_videoconns[] = {
80 "sdi", "hdmi", "opticalsdi", "component", "composite", "svideo"
82 static const char *const ppsz_videoconns_text[] = {
83 N_("SDI"), N_("HDMI"), N_("Optical SDI"), N_("Component"), N_("Composite"), N_("S-Video")
86 static const char *const ppsz_audioconns[] = {
87 "embedded", "aesebu", "analog"
89 static const char *const ppsz_audioconns_text[] = {
90 N_("Embedded"), N_("AES/EBU"), N_("Analog")
93 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
94 #define ASPECT_RATIO_LONGTEXT N_(\
95 "Aspect ratio (4:3, 16:9). Default assumes square pixels.")
97 vlc_module_begin ()
98 set_shortname(N_("DeckLink"))
99 set_description(N_("Blackmagic DeckLink SDI input"))
100 set_category(CAT_INPUT)
101 set_subcategory(SUBCAT_INPUT_ACCESS)
103 add_integer("decklink-card-index", 0,
104 CARD_INDEX_TEXT, CARD_INDEX_LONGTEXT, true)
105 add_string("decklink-mode", NULL,
106 MODE_TEXT, MODE_LONGTEXT, true)
107 add_string("decklink-audio-connection", 0,
108 AUDIO_CONNECTION_TEXT, AUDIO_CONNECTION_LONGTEXT, true)
109 change_string_list(ppsz_audioconns, ppsz_audioconns_text)
110 add_integer("decklink-audio-rate", 48000,
111 RATE_TEXT, RATE_LONGTEXT, true)
112 add_integer("decklink-audio-channels", 2,
113 CHANNELS_TEXT, CHANNELS_LONGTEXT, true)
114 add_string("decklink-video-connection", 0,
115 VIDEO_CONNECTION_TEXT, VIDEO_CONNECTION_LONGTEXT, true)
116 change_string_list(ppsz_videoconns, ppsz_videoconns_text)
117 add_string("decklink-aspect-ratio", NULL,
118 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true)
119 add_bool("decklink-tenbits", false, N_("10 bits"), N_("10 bits"), true)
121 add_shortcut("decklink")
122 set_capability("access_demux", 0)
123 set_callbacks(Open, Close)
124 vlc_module_end ()
126 static int Control(demux_t *, int, va_list);
128 class DeckLinkCaptureDelegate;
130 struct demux_sys_t
132 IDeckLink *card;
133 IDeckLinkInput *input;
134 DeckLinkCaptureDelegate *delegate;
136 /* We need to hold onto the IDeckLinkConfiguration object, or our settings will not apply.
137 See section 2.4.15 of the Blackmagic DeckLink SDK documentation. */
138 IDeckLinkConfiguration *config;
139 IDeckLinkAttributes *attributes;
141 bool autodetect;
143 es_out_id_t *video_es;
144 es_format_t video_fmt;
145 es_out_id_t *audio_es;
146 es_out_id_t *cc_es;
148 vlc_mutex_t pts_lock;
149 int last_pts; /* protected by <pts_lock> */
151 uint32_t dominance_flags;
152 int channels;
154 bool tenbits;
157 static const char *GetFieldDominance(BMDFieldDominance dom, uint32_t *flags)
159 switch(dom)
161 case bmdProgressiveFrame:
162 return "";
163 case bmdProgressiveSegmentedFrame:
164 return ", segmented";
165 case bmdLowerFieldFirst:
166 *flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
167 return ", interlaced [BFF]";
168 case bmdUpperFieldFirst:
169 *flags = BLOCK_FLAG_TOP_FIELD_FIRST;
170 return ", interlaced [TFF]";
171 case bmdUnknownFieldDominance:
172 default:
173 return ", unknown field dominance";
177 static es_format_t GetModeSettings(demux_t *demux, IDeckLinkDisplayMode *m,
178 BMDDetectedVideoInputFormatFlags fmt_flags)
180 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
181 uint32_t flags = 0;
182 (void)GetFieldDominance(m->GetFieldDominance(), &flags);
184 BMDTimeValue frame_duration, time_scale;
185 if (m->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
186 time_scale = 0;
187 frame_duration = 1;
190 es_format_t video_fmt;
191 vlc_fourcc_t chroma = 0;
192 switch (fmt_flags) {
193 case bmdDetectedVideoInputYCbCr422:
194 chroma = sys->tenbits ? VLC_CODEC_I422_10L : VLC_CODEC_UYVY;
195 break;
196 case bmdDetectedVideoInputRGB444:
197 chroma = VLC_CODEC_ARGB;
198 break;
199 default:
200 msg_Err(demux, "Unsupported input format");
201 break;
203 es_format_Init(&video_fmt, VIDEO_ES, chroma);
205 video_fmt.video.i_chroma = chroma;
206 video_fmt.video.i_width = m->GetWidth();
207 video_fmt.video.i_height = m->GetHeight();
208 video_fmt.video.i_sar_num = 1;
209 video_fmt.video.i_sar_den = 1;
210 video_fmt.video.i_frame_rate = time_scale;
211 video_fmt.video.i_frame_rate_base = frame_duration;
212 video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8;
214 unsigned aspect_num, aspect_den;
215 if (!var_InheritURational(demux, &aspect_num, &aspect_den, "decklink-aspect-ratio") &&
216 aspect_num > 0 && aspect_den > 0) {
217 video_fmt.video.i_sar_num = aspect_num * video_fmt.video.i_height;
218 video_fmt.video.i_sar_den = aspect_den * video_fmt.video.i_width;
221 sys->dominance_flags = flags;
223 return video_fmt;
226 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
228 public:
229 DeckLinkCaptureDelegate(demux_t *demux) : demux_(demux)
231 m_ref_.store(1);
234 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, LPVOID *) { return E_NOINTERFACE; }
236 virtual ULONG STDMETHODCALLTYPE AddRef(void)
238 return m_ref_.fetch_add(1);
241 virtual ULONG STDMETHODCALLTYPE Release(void)
243 uintptr_t new_ref = m_ref_.fetch_sub(1);
244 if (new_ref == 0)
245 delete this;
246 return new_ref;
249 virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags flags)
251 demux_sys_t *sys = static_cast<demux_sys_t *>(demux_->p_sys);
253 if( !(events & bmdVideoInputDisplayModeChanged ))
254 return S_OK;
256 const char *mode_name;
257 if (mode->GetName(&mode_name) != S_OK)
258 mode_name = "unknown";
260 msg_Dbg(demux_, "Video input format changed to %s", mode_name);
261 if (!sys->autodetect) {
262 msg_Err(demux_, "Video format detection disabled");
263 return S_OK;
266 BMDPixelFormat fmt = 0;
267 switch (flags) {
268 case bmdDetectedVideoInputYCbCr422:
269 fmt = sys->tenbits ? bmdFormat10BitYUV : bmdFormat8BitYUV;
270 break;
271 case bmdDetectedVideoInputRGB444:
272 fmt = bmdFormat8BitARGB;
273 break;
274 default:
275 msg_Err(demux_, "Unsupported input format");
276 return S_OK;
279 es_out_Del(demux_->out, sys->video_es);
280 sys->video_fmt = GetModeSettings(demux_, mode, flags);
281 sys->video_es = es_out_Add(demux_->out, &sys->video_fmt);
283 sys->input->PauseStreams();
284 sys->input->EnableVideoInput( mode->GetDisplayMode(), fmt, bmdVideoInputEnableFormatDetection );
285 sys->input->FlushStreams();
286 sys->input->StartStreams();
288 return S_OK;
291 virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
293 private:
294 std::atomic_uint m_ref_;
295 demux_t *demux_;
298 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
300 demux_sys_t *sys = static_cast<demux_sys_t *>(demux_->p_sys);
302 if (videoFrame) {
303 if (videoFrame->GetFlags() & bmdFrameHasNoInputSource) {
304 msg_Warn(demux_, "No input signal detected (%dx%d)",
305 videoFrame->GetWidth(), videoFrame->GetHeight());
306 return S_OK;
309 const int width = videoFrame->GetWidth();
310 const int height = videoFrame->GetHeight();
311 const int stride = videoFrame->GetRowBytes();
313 int bpp = 0;
314 switch (sys->video_fmt.i_codec) {
315 case VLC_CODEC_I422_10L:
316 case VLC_CODEC_ARGB:
317 bpp = 4;
318 break;
319 case VLC_CODEC_UYVY:
320 bpp = 2;
321 break;
323 block_t *video_frame = block_Alloc(width * height * bpp);
324 if (!video_frame)
325 return S_OK;
327 const uint32_t *frame_bytes;
328 videoFrame->GetBytes((void**)&frame_bytes);
330 BMDTimeValue stream_time, frame_duration;
331 videoFrame->GetStreamTime(&stream_time, &frame_duration, CLOCK_FREQ);
332 video_frame->i_flags = BLOCK_FLAG_TYPE_I | sys->dominance_flags;
333 video_frame->i_pts = video_frame->i_dts = VLC_TS_0 + stream_time;
335 if (sys->video_fmt.i_codec == VLC_CODEC_I422_10L) {
336 v210_convert((uint16_t*)video_frame->p_buffer, frame_bytes, width, height);
337 IDeckLinkVideoFrameAncillary *vanc;
338 if (videoFrame->GetAncillaryData(&vanc) == S_OK) {
339 for (int i = 1; i < 21; i++) {
340 uint32_t *buf;
341 if (vanc->GetBufferForVerticalBlankingLine(i, (void**)&buf) != S_OK)
342 break;
343 uint16_t dec[width * 2];
344 v210_convert(&dec[0], buf, width, 1);
345 block_t *cc = vanc_to_cc(demux_, dec, width * 2);
346 if (!cc)
347 continue;
348 cc->i_pts = cc->i_dts = VLC_TS_0 + stream_time;
350 if (!sys->cc_es) {
351 es_format_t fmt;
353 es_format_Init( &fmt, SPU_ES, VLC_CODEC_CEA608 );
354 fmt.psz_description = strdup(N_("Closed captions 1"));
355 if (fmt.psz_description) {
356 sys->cc_es = es_out_Add(demux_->out, &fmt);
357 msg_Dbg(demux_, "Adding Closed captions stream");
360 if (sys->cc_es)
361 es_out_Send(demux_->out, sys->cc_es, cc);
362 else
363 block_Release(cc);
364 break; // we found the line with Closed Caption data
366 vanc->Release();
368 } else if (sys->video_fmt.i_codec == VLC_CODEC_UYVY) {
369 for (int y = 0; y < height; ++y) {
370 const uint8_t *src = (const uint8_t *)frame_bytes + stride * y;
371 uint8_t *dst = video_frame->p_buffer + width * 2 * y;
372 memcpy(dst, src, width * 2);
374 } else {
375 memcpy(video_frame->p_buffer, frame_bytes, width * height * bpp);
378 vlc_mutex_lock(&sys->pts_lock);
379 if (video_frame->i_pts > sys->last_pts)
380 sys->last_pts = video_frame->i_pts;
381 vlc_mutex_unlock(&sys->pts_lock);
383 es_out_SetPCR(demux_->out, video_frame->i_pts);
384 es_out_Send(demux_->out, sys->video_es, video_frame);
387 if (audioFrame) {
388 const int bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * sys->channels;
390 block_t *audio_frame = block_Alloc(bytes);
391 if (!audio_frame)
392 return S_OK;
394 void *frame_bytes;
395 audioFrame->GetBytes(&frame_bytes);
396 memcpy(audio_frame->p_buffer, frame_bytes, bytes);
398 BMDTimeValue packet_time;
399 audioFrame->GetPacketTime(&packet_time, CLOCK_FREQ);
400 audio_frame->i_pts = audio_frame->i_dts = VLC_TS_0 + packet_time;
402 vlc_mutex_lock(&sys->pts_lock);
403 if (audio_frame->i_pts > sys->last_pts)
404 sys->last_pts = audio_frame->i_pts;
405 vlc_mutex_unlock(&sys->pts_lock);
407 es_out_SetPCR(demux_->out, audio_frame->i_pts);
408 es_out_Send(demux_->out, sys->audio_es, audio_frame);
411 return S_OK;
415 static int GetAudioConn(demux_t *demux)
417 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
419 char *opt = var_CreateGetNonEmptyString(demux, "decklink-audio-connection");
420 if (!opt)
421 return VLC_SUCCESS;
423 BMDAudioConnection c;
424 if (!strcmp(opt, "embedded"))
425 c = bmdAudioConnectionEmbedded;
426 else if (!strcmp(opt, "aesebu"))
427 c = bmdAudioConnectionAESEBU;
428 else if (!strcmp(opt, "analog"))
429 c = bmdAudioConnectionAnalog;
430 else {
431 msg_Err(demux, "Invalid audio-connection: `%s\' specified", opt);
432 free(opt);
433 return VLC_EGENERIC;
436 if (sys->config->SetInt(bmdDeckLinkConfigAudioInputConnection, c) != S_OK) {
437 msg_Err(demux, "Failed to set audio input connection");
438 return VLC_EGENERIC;
441 return VLC_SUCCESS;
444 static int GetVideoConn(demux_t *demux)
446 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
448 char *opt = var_InheritString(demux, "decklink-video-connection");
449 if (!opt)
450 return VLC_SUCCESS;
452 BMDVideoConnection c;
453 if (!strcmp(opt, "sdi"))
454 c = bmdVideoConnectionSDI;
455 else if (!strcmp(opt, "hdmi"))
456 c = bmdVideoConnectionHDMI;
457 else if (!strcmp(opt, "opticalsdi"))
458 c = bmdVideoConnectionOpticalSDI;
459 else if (!strcmp(opt, "component"))
460 c = bmdVideoConnectionComponent;
461 else if (!strcmp(opt, "composite"))
462 c = bmdVideoConnectionComposite;
463 else if (!strcmp(opt, "svideo"))
464 c = bmdVideoConnectionSVideo;
465 else {
466 msg_Err(demux, "Invalid video-connection: `%s\' specified", opt);
467 free(opt);
468 return VLC_EGENERIC;
471 free(opt);
472 if (sys->config->SetInt(bmdDeckLinkConfigVideoInputConnection, c) != S_OK) {
473 msg_Err(demux, "Failed to set video input connection");
474 return VLC_EGENERIC;
477 return VLC_SUCCESS;
480 static int Open(vlc_object_t *p_this)
482 demux_t *demux = (demux_t*)p_this;
483 demux_sys_t *sys;
484 int ret = VLC_EGENERIC;
485 int card_index;
486 int physical_channels = 0;
487 int rate;
488 BMDVideoInputFlags flags = bmdVideoInputFlagDefault;
490 /* Set up demux */
491 demux->pf_demux = NULL;
492 demux->pf_control = Control;
493 demux->p_sys = sys = (demux_sys_t*)calloc(1, sizeof(demux_sys_t));
494 if (!sys)
495 return VLC_ENOMEM;
497 vlc_mutex_init(&sys->pts_lock);
499 sys->tenbits = var_InheritBool(p_this, "decklink-tenbits");
501 IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
502 if (!decklink_iterator) {
503 msg_Err(demux, "DeckLink drivers not found.");
504 goto finish;
507 card_index = var_InheritInteger(demux, "decklink-card-index");
508 if (card_index < 0) {
509 msg_Err(demux, "Invalid card index %d", card_index);
510 goto finish;
513 for (int i = 0; i <= card_index; i++) {
514 if (sys->card)
515 sys->card->Release();
516 if (decklink_iterator->Next(&sys->card) != S_OK) {
517 msg_Err(demux, "DeckLink PCI card %d not found", card_index);
518 goto finish;
522 const char *model_name;
523 if (sys->card->GetModelName(&model_name) != S_OK)
524 model_name = "unknown";
526 msg_Dbg(demux, "Opened DeckLink PCI card %d (%s)", card_index, model_name);
528 if (sys->card->QueryInterface(IID_IDeckLinkInput, (void**)&sys->input) != S_OK) {
529 msg_Err(demux, "Card has no inputs");
530 goto finish;
533 /* Set up the video and audio sources. */
534 if (sys->card->QueryInterface(IID_IDeckLinkConfiguration, (void**)&sys->config) != S_OK) {
535 msg_Err(demux, "Failed to get configuration interface");
536 goto finish;
539 if (sys->card->QueryInterface(IID_IDeckLinkAttributes, (void**)&sys->attributes) != S_OK) {
540 msg_Err(demux, "Failed to get attributes interface");
541 goto finish;
544 if (GetVideoConn(demux) || GetAudioConn(demux))
545 goto finish;
547 BMDPixelFormat fmt;
548 fmt = sys->tenbits ? bmdFormat10BitYUV : bmdFormat8BitYUV;
549 if (sys->attributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &sys->autodetect) != S_OK) {
550 msg_Err(demux, "Failed to query card attribute");
551 goto finish;
554 /* Get the list of display modes. */
555 IDeckLinkDisplayModeIterator *mode_it;
556 if (sys->input->GetDisplayModeIterator(&mode_it) != S_OK) {
557 msg_Err(demux, "Failed to enumerate display modes");
558 goto finish;
561 union {
562 BMDDisplayMode id;
563 char str[4];
564 } u;
566 u.id = 0;
568 char *mode;
569 mode = var_CreateGetNonEmptyString(demux, "decklink-mode");
570 if (mode)
571 sys->autodetect = false; // disable autodetection if mode was set
573 if (sys->autodetect) {
574 msg_Dbg(demux, "Card supports input format detection");
575 flags |= bmdVideoInputEnableFormatDetection;
576 /* Enable a random format, we will reconfigure on format detection */
577 u.id = htonl(bmdModeHD1080p2997);
578 } else {
579 if (!mode || strlen(mode) < 3 || strlen(mode) > 4) {
580 msg_Err(demux, "Invalid mode: \'%s\'", mode ? mode : "");
581 free(mode);
582 goto finish;
585 msg_Dbg(demux, "Looking for mode \'%s\'", mode);
586 memcpy(u.str, mode, 4);
587 if (u.str[3] == '\0')
588 u.str[3] = ' '; /* 'pal'\0 -> 'pal ' */
589 free(mode);
592 sys->video_fmt.video.i_width = 0;
594 for (IDeckLinkDisplayMode *m;; m->Release()) {
595 if ((mode_it->Next(&m) != S_OK) || !m)
596 break;
598 const char *mode_name;
599 BMDTimeValue frame_duration, time_scale;
600 uint32_t field_flags;
601 const char *field = GetFieldDominance(m->GetFieldDominance(), &field_flags);
602 BMDDisplayMode id = ntohl(m->GetDisplayMode());
604 if (m->GetName(&mode_name) != S_OK)
605 mode_name = "unknown";
606 if (m->GetFrameRate(&frame_duration, &time_scale) != S_OK) {
607 time_scale = 0;
608 frame_duration = 1;
611 msg_Dbg(demux, "Found mode '%4.4s': %s (%dx%d, %.3f fps%s)",
612 (char*)&id, mode_name,
613 (int)m->GetWidth(), (int)m->GetHeight(),
614 double(time_scale) / frame_duration, field);
616 if (u.id == id) {
617 sys->video_fmt = GetModeSettings(demux, m, bmdDetectedVideoInputYCbCr422);
618 msg_Dbg(demux, "Using that mode");
622 mode_it->Release();
624 if (sys->video_fmt.video.i_width == 0) {
625 msg_Err(demux, "Unknown video mode `%4.4s\' specified.", (char*)&u.id);
626 goto finish;
629 if (sys->input->EnableVideoInput(htonl(u.id), fmt, flags) != S_OK) {
630 msg_Err(demux, "Failed to enable video input");
631 goto finish;
634 /* Set up audio. */
635 sys->channels = var_InheritInteger(demux, "decklink-audio-channels");
636 switch (sys->channels) {
637 case 0:
638 break;
639 case 2:
640 physical_channels = AOUT_CHANS_STEREO;
641 break;
642 case 8:
643 physical_channels = AOUT_CHANS_7_1;
644 break;
645 //case 16:
646 default:
647 msg_Err(demux, "Invalid number of channels (%d), disabling audio", sys->channels);
648 sys->channels = 0;
650 rate = var_InheritInteger(demux, "decklink-audio-rate");
651 if (rate > 0 && sys->channels > 0) {
652 if (sys->input->EnableAudioInput(rate, bmdAudioSampleType16bitInteger, sys->channels) != S_OK) {
653 msg_Err(demux, "Failed to enable audio input");
654 goto finish;
658 sys->delegate = new DeckLinkCaptureDelegate(demux);
659 sys->input->SetCallback(sys->delegate);
661 if (sys->input->StartStreams() != S_OK) {
662 msg_Err(demux, "Could not start streaming from SDI card. This could be caused "
663 "by invalid video mode or flags, access denied, or card already in use.");
664 goto finish;
667 msg_Dbg(demux, "added new video es %4.4s %dx%d",
668 (char*)&sys->video_fmt.i_codec, sys->video_fmt.video.i_width, sys->video_fmt.video.i_height);
669 sys->video_es = es_out_Add(demux->out, &sys->video_fmt);
671 es_format_t audio_fmt;
672 es_format_Init(&audio_fmt, AUDIO_ES, VLC_CODEC_S16N);
673 audio_fmt.audio.i_channels = sys->channels;
674 audio_fmt.audio.i_physical_channels = physical_channels;
675 audio_fmt.audio.i_rate = rate;
676 audio_fmt.audio.i_bitspersample = 16;
677 audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
678 audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
680 msg_Dbg(demux, "added new audio es %4.4s %dHz %dbpp %dch",
681 (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
682 sys->audio_es = es_out_Add(demux->out, &audio_fmt);
684 ret = VLC_SUCCESS;
686 finish:
687 if (decklink_iterator)
688 decklink_iterator->Release();
690 if (ret != VLC_SUCCESS)
691 Close(p_this);
693 return ret;
696 static void Close(vlc_object_t *p_this)
698 demux_t *demux = (demux_t *)p_this;
699 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
701 if (sys->attributes)
702 sys->attributes->Release();
704 if (sys->config)
705 sys->config->Release();
707 if (sys->input) {
708 sys->input->StopStreams();
709 sys->input->Release();
712 if (sys->card)
713 sys->card->Release();
715 if (sys->delegate)
716 sys->delegate->Release();
718 vlc_mutex_destroy(&sys->pts_lock);
719 free(sys);
722 static int Control(demux_t *demux, int query, va_list args)
724 demux_sys_t *sys = (demux_sys_t *)demux->p_sys;
725 bool *pb;
726 int64_t *pi64;
728 switch(query)
730 /* Special for access_demux */
731 case DEMUX_CAN_PAUSE:
732 case DEMUX_CAN_SEEK:
733 case DEMUX_CAN_CONTROL_PACE:
734 pb = va_arg(args, bool *);
735 *pb = false;
736 return VLC_SUCCESS;
738 case DEMUX_GET_PTS_DELAY:
739 pi64 = va_arg(args, int64_t *);
740 *pi64 = INT64_C(1000) * var_InheritInteger(demux, "live-caching");
741 return VLC_SUCCESS;
743 case DEMUX_GET_TIME:
744 pi64 = va_arg(args, int64_t *);
745 vlc_mutex_lock(&sys->pts_lock);
746 *pi64 = sys->last_pts;
747 vlc_mutex_unlock(&sys->pts_lock);
748 return VLC_SUCCESS;
750 default:
751 return VLC_EGENERIC;