]> git.sesse.net Git - vlc/blob - modules/access/decklink.cpp
XCB/screen: fix config item descriptions
[vlc] / modules / access / decklink.cpp
1 /*****************************************************************************
2  * decklink.cpp: BlackMagic DeckLink SDI input module
3  *****************************************************************************
4  * Copyright (C) 2010 Steinar H. Gunderson
5  *
6  * Authors: Steinar H. Gunderson <steinar+vlc@gunderson.no>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  *****************************************************************************/
23
24 #define __STDC_CONSTANT_MACROS 1
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_demux.h>
33 #include <vlc_atomic.h>
34
35 #include <arpa/inet.h>
36
37 #include <DeckLinkAPI.h>
38 #include <DeckLinkAPIDispatch.cpp>
39
40 static int  Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 #define CARD_INDEX_TEXT N_("Input card to use")
44 #define CARD_INDEX_LONGTEXT N_( \
45     "DeckLink capture card to use, if multiple exist. " \
46     "The cards are numbered from 0." )
47
48 #define MODE_TEXT N_("Desired input video mode")
49 #define MODE_LONGTEXT N_( \
50     "Desired input video mode for DeckLink captures. " \
51     "This value should be a FOURCC code in textual " \
52     "form, e.g. \"ntsc\"." )
53
54 #define CACHING_TEXT N_("Caching value in ms")
55 #define CACHING_LONGTEXT N_( \
56     "Caching value for DeckLink captures. This " \
57     "value should be set in milliseconds." )
58
59 #define AUDIO_CONNECTION_TEXT N_("Audio connection")
60 #define AUDIO_CONNECTION_LONGTEXT N_( \
61     "Audio connection to use for DeckLink captures. " \
62     "Valid choices: embedded, aesebu, analog. " \
63     "Leave blank for card default." )
64
65 #define RATE_TEXT N_("Audio sampling rate in Hz")
66 #define RATE_LONGTEXT N_( \
67     "Audio sampling rate (in hertz) for DeckLink captures. " \
68     "0 disables audio input." )
69
70 #define CHANNELS_TEXT N_("Number of audio channels")
71 #define CHANNELS_LONGTEXT N_( \
72     "Number of input audio channels for DeckLink captures. " \
73     "Must be 2, 8 or 16. 0 disables audio input." )
74
75 #define VIDEO_CONNECTION_TEXT N_("Video connection")
76 #define VIDEO_CONNECTION_LONGTEXT N_( \
77     "Video connection to use for DeckLink captures. " \
78     "Valid choices: sdi, hdmi, opticalsdi, component, " \
79     "composite, svideo. " \
80     "Leave blank for card default." )
81
82 static const char *const ppsz_videoconns[] = {
83     "sdi", "hdmi", "opticalsdi", "component", "composite", "svideo"
84 };
85 static const char *const ppsz_videoconns_text[] = {
86     N_("SDI"), N_("HDMI"), N_("Optical SDI"), N_("Component"), N_("Composite"), N_("S-video")
87 };
88
89 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
90 #define ASPECT_RATIO_LONGTEXT N_( \
91     "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
92
93 vlc_module_begin ()
94     set_shortname( N_("DeckLink") )
95     set_description( N_("Blackmagic DeckLink SDI input") )
96     set_category( CAT_INPUT )
97     set_subcategory( SUBCAT_INPUT_ACCESS )
98
99     add_integer( "decklink-card-index", 0,
100                  CARD_INDEX_TEXT, CARD_INDEX_LONGTEXT, true )
101     add_string( "decklink-mode", "pal ",
102                  MODE_TEXT, MODE_LONGTEXT, true )
103     add_integer( "decklink-caching", DEFAULT_PTS_DELAY / 1000,
104                  CACHING_TEXT, CACHING_LONGTEXT, true )
105     add_string( "decklink-audio-connection", 0,
106                  AUDIO_CONNECTION_TEXT, AUDIO_CONNECTION_LONGTEXT, true )
107     add_integer( "decklink-audio-rate", 48000,
108                  RATE_TEXT, RATE_LONGTEXT, true )
109     add_integer( "decklink-audio-channels", 2,
110                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
111     add_string( "decklink-video-connection", 0,
112                  VIDEO_CONNECTION_TEXT, VIDEO_CONNECTION_LONGTEXT, true )
113         change_string_list( ppsz_videoconns, ppsz_videoconns_text, 0 )
114     add_string( "decklink-aspect-ratio", NULL,
115                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
116
117     add_shortcut( "decklink" )
118     set_capability( "access_demux", 10 )
119     set_callbacks( Open, Close )
120 vlc_module_end ()
121
122 static int Control( demux_t *, int, va_list );
123
124 class DeckLinkCaptureDelegate;
125
126 struct demux_sys_t
127 {
128     IDeckLink *p_card;
129     IDeckLinkInput *p_input;
130     DeckLinkCaptureDelegate *p_delegate;
131
132     es_out_id_t *p_video_es;
133     es_out_id_t *p_audio_es;
134
135     vlc_mutex_t pts_lock;
136     int i_last_pts;  /* protected by <pts_lock> */
137
138     uint32_t i_dominance_flags;
139     int i_channels;
140 };
141
142 class DeckLinkCaptureDelegate : public IDeckLinkInputCallback
143 {
144 public:
145     DeckLinkCaptureDelegate( demux_t *p_demux ) : p_demux_(p_demux)
146     {
147         vlc_atomic_set( &m_ref_, 1 );
148     }
149
150     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
151
152     virtual ULONG STDMETHODCALLTYPE AddRef(void)
153     {
154         return vlc_atomic_inc( &m_ref_ );
155     }
156
157     virtual ULONG STDMETHODCALLTYPE Release(void)
158     {
159         uintptr_t new_ref = vlc_atomic_dec( &m_ref_ );
160         if ( new_ref == 0 )
161             delete this;
162         return new_ref;
163     }
164
165     virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents, IDeckLinkDisplayMode*, BMDDetectedVideoInputFormatFlags)
166     {
167         msg_Dbg( p_demux_, "Video input format changed" );
168         return S_OK;
169     }
170
171     virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*);
172
173 private:
174     vlc_atomic_t m_ref_;
175     demux_t *p_demux_;
176 };
177
178 HRESULT DeckLinkCaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, IDeckLinkAudioInputPacket* audioFrame)
179 {
180     demux_sys_t *p_sys = p_demux_->p_sys;
181     block_t *p_video_frame = NULL;
182     block_t *p_audio_frame = NULL;
183
184     if( videoFrame )
185     {
186         if( videoFrame->GetFlags() & bmdFrameHasNoInputSource )
187         {
188             msg_Warn( p_demux_, "No input signal detected" );
189             return S_OK;
190         }
191
192         const int i_width = videoFrame->GetWidth();
193         const int i_height = videoFrame->GetHeight();
194         const int i_stride = videoFrame->GetRowBytes();
195         const int i_bpp = 2;
196
197         p_video_frame = block_New( p_demux_, i_width * i_height * i_bpp );
198         if( !p_video_frame )
199         {
200             msg_Err( p_demux_, "Could not allocate memory for video frame" );
201             return S_OK;
202         }
203
204         void *frame_bytes;
205         videoFrame->GetBytes( &frame_bytes );
206         for( int y = 0; y < i_height; ++y )
207         {
208             const uint8_t *src = (const uint8_t *)frame_bytes + i_stride * y;
209             uint8_t *dst = p_video_frame->p_buffer + i_width * i_bpp * y;
210             memcpy( dst, src, i_width * i_bpp );
211         }
212
213         BMDTimeValue stream_time, frame_duration;
214         videoFrame->GetStreamTime( &stream_time, &frame_duration, CLOCK_FREQ );
215         p_video_frame->i_flags = BLOCK_FLAG_TYPE_I | p_sys->i_dominance_flags;
216         p_video_frame->i_pts = p_video_frame->i_dts = VLC_TS_0 + stream_time;
217
218         vlc_mutex_lock( &p_sys->pts_lock );
219         if( p_video_frame->i_pts > p_sys->i_last_pts )
220             p_sys->i_last_pts = p_video_frame->i_pts;
221         vlc_mutex_unlock( &p_sys->pts_lock );
222
223         es_out_Control( p_demux_->out, ES_OUT_SET_PCR, p_video_frame->i_pts );
224         es_out_Send( p_demux_->out, p_sys->p_video_es, p_video_frame );
225     }
226
227     if( audioFrame )
228     {
229         const int i_bytes = audioFrame->GetSampleFrameCount() * sizeof(int16_t) * p_sys->i_channels;
230
231         p_audio_frame = block_New( p_demux_, i_bytes );
232         if( !p_audio_frame )
233         {
234             msg_Err( p_demux_, "Could not allocate memory for audio frame" );
235             if( p_video_frame )
236                 block_Release( p_video_frame );
237             return S_OK;
238         }
239
240         void *frame_bytes;
241         audioFrame->GetBytes( &frame_bytes );
242         memcpy( p_audio_frame->p_buffer, frame_bytes, i_bytes );
243
244         BMDTimeValue packet_time;
245         audioFrame->GetPacketTime( &packet_time, CLOCK_FREQ );
246         p_audio_frame->i_pts = p_audio_frame->i_dts = VLC_TS_0 + packet_time;
247
248         vlc_mutex_lock( &p_sys->pts_lock );
249         if( p_audio_frame->i_pts > p_sys->i_last_pts )
250             p_sys->i_last_pts = p_audio_frame->i_pts;
251         vlc_mutex_unlock( &p_sys->pts_lock );
252         if( p_audio_frame->i_pts > p_sys->i_last_pts )
253
254         es_out_Control( p_demux_->out, ES_OUT_SET_PCR, p_audio_frame->i_pts );
255         es_out_Send( p_demux_->out, p_sys->p_audio_es, p_audio_frame );
256     }
257
258     return S_OK;
259 }
260
261 static int Open( vlc_object_t *p_this )
262 {
263     demux_t     *p_demux = (demux_t*)p_this;
264     demux_sys_t *p_sys;
265     int         ret = VLC_EGENERIC;
266     char        *psz_aspect;
267     char        *psz_display_mode = NULL;
268     char        *psz_video_connection = NULL;
269     char        *psz_audio_connection = NULL;
270     bool        b_found_mode;
271     int         i_card_index;
272     int         i_width, i_height, i_fps_num, i_fps_den;
273     int         i_rate;
274     unsigned    u_aspect_num, u_aspect_den;
275
276     /* Only when selected */
277     if( *p_demux->psz_access == '\0' )
278         return VLC_EGENERIC;
279
280     /* Set up p_demux */
281     p_demux->pf_demux = NULL;
282     p_demux->pf_control = Control;
283     p_demux->info.i_update = 0;
284     p_demux->info.i_title = 0;
285     p_demux->info.i_seekpoint = 0;
286     p_demux->p_sys = p_sys = (demux_sys_t*)calloc( 1, sizeof( demux_sys_t ) );
287     if( !p_sys )
288         return VLC_ENOMEM;
289
290     vlc_mutex_init( &p_sys->pts_lock );
291
292     IDeckLinkDisplayModeIterator *p_display_iterator = NULL;
293
294     IDeckLinkIterator *decklink_iterator = CreateDeckLinkIteratorInstance();
295     if( !decklink_iterator )
296     {
297         msg_Err( p_demux, "DeckLink drivers not found." );
298         goto finish;
299     }
300
301     HRESULT result;
302
303     i_card_index = var_InheritInteger( p_demux, "decklink-card-index" );
304     if( i_card_index < 0 )
305     {
306         msg_Err( p_demux, "Invalid card index %d", i_card_index );
307         goto finish;
308     }
309
310     for( int i = 0; i <= i_card_index; ++i )
311     {
312         if( p_sys->p_card )
313             p_sys->p_card->Release();
314         result = decklink_iterator->Next( &p_sys->p_card );
315         if( result != S_OK )
316             break;
317     }
318
319     if( result != S_OK )
320     {
321         msg_Err( p_demux, "DeckLink PCI card %d not found", i_card_index );
322         goto finish;
323     }
324
325     const char *psz_model_name;
326     result = p_sys->p_card->GetModelName( &psz_model_name );
327
328     if( result != S_OK )
329     {
330         msg_Err( p_demux, "Could not get model name" );
331         goto finish;
332     }
333
334     msg_Dbg( p_demux, "Opened DeckLink PCI card %d (%s)", i_card_index, psz_model_name );
335
336     if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK )
337     {
338         msg_Err( p_demux, "Card has no inputs" );
339         goto finish;
340     }
341
342     /* Set up the video and audio sources. */
343     IDeckLinkConfiguration *p_config;
344     if( p_sys->p_card->QueryInterface( IID_IDeckLinkConfiguration, (void**)&p_config) != S_OK )
345     {
346         msg_Err( p_demux, "Failed to get configuration interface" );
347         goto finish;
348     }
349
350     psz_video_connection = var_InheritString( p_demux, "decklink-video-connection" );
351     if( psz_video_connection )
352     {
353         BMDVideoConnection conn;
354         if ( !strcmp( psz_video_connection, "sdi" ) )
355             conn = bmdVideoConnectionSDI;
356         else if ( !strcmp( psz_video_connection, "hdmi" ) )
357             conn = bmdVideoConnectionHDMI;
358         else if ( !strcmp( psz_video_connection, "opticalsdi" ) )
359             conn = bmdVideoConnectionOpticalSDI;
360         else if ( !strcmp( psz_video_connection, "component" ) )
361             conn = bmdVideoConnectionComponent;
362         else if ( !strcmp( psz_video_connection, "composite" ) )
363             conn = bmdVideoConnectionComposite;
364         else if ( !strcmp( psz_video_connection, "svideo" ) )
365             conn = bmdVideoConnectionSVideo;
366         else
367         {
368             msg_Err( p_demux, "Invalid --decklink-video-connection specified; choose one of " \
369                               "sdi, hdmi, opticalsdi, component, composite, or svideo." );
370             goto finish;
371         }
372
373         msg_Dbg( p_demux, "Setting video input format to 0x%x", conn);
374         result = p_config->SetInt( bmdDeckLinkConfigVideoInputConnection, conn );
375         if( result != S_OK )
376         {
377             msg_Err( p_demux, "Failed to set video input connection" );
378             goto finish;
379         }
380     }
381
382     psz_audio_connection = var_CreateGetNonEmptyString( p_demux, "decklink-audio-connection" );
383     if( psz_audio_connection )
384     {
385         BMDAudioConnection conn;
386         if ( !strcmp( psz_audio_connection, "embedded" ) )
387             conn = bmdAudioConnectionEmbedded;
388         else if ( !strcmp( psz_audio_connection, "aesebu" ) )
389             conn = bmdAudioConnectionAESEBU;
390         else if ( !strcmp( psz_audio_connection, "analog" ) )
391             conn = bmdAudioConnectionAnalog;
392         else
393         {
394             msg_Err( p_demux, "Invalid --decklink-audio-connection specified; choose one of " \
395                               "embedded, aesebu, or analog." );
396             goto finish;
397         }
398
399         msg_Dbg( p_demux, "Setting audio input format to 0x%x", conn);
400         result = p_config->SetInt( bmdDeckLinkConfigAudioInputConnection, conn );
401         if( result != S_OK )
402         {
403             msg_Err( p_demux, "Failed to set audio input connection" );
404             goto finish;
405         }
406     }
407
408     /* Get the list of display modes. */
409     result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator );
410     if( result != S_OK )
411     {
412         msg_Err( p_demux, "Failed to enumerate display modes" );
413         goto finish;
414     }
415
416     psz_display_mode = var_CreateGetNonEmptyString( p_demux, "decklink-mode" );
417     if( !psz_display_mode || strlen( psz_display_mode ) > 4 ) {
418         msg_Err( p_demux, "Missing or invalid --decklink-mode string" );
419         goto finish;
420     }
421
422     /*
423      * Pad the --decklink-mode string to four characters, so the user can specify e.g. "pal"
424      * without having to add the trailing space.
425      */
426     char sz_display_mode_padded[5];
427     strcpy(sz_display_mode_padded, "    ");
428     for( int i = 0; i < strlen( psz_display_mode ); ++i )
429         sz_display_mode_padded[i] = psz_display_mode[i];
430
431     BMDDisplayMode wanted_mode_id;
432     memcpy( &wanted_mode_id, &sz_display_mode_padded, sizeof(wanted_mode_id) );
433
434     b_found_mode = false;
435
436     for (;;)
437     {
438         IDeckLinkDisplayMode *p_display_mode;
439         result = p_display_iterator->Next( &p_display_mode );
440         if( result != S_OK || !p_display_mode )
441             break;
442
443         char sz_mode_id_text[5] = {0};
444         BMDDisplayMode mode_id = ntohl( p_display_mode->GetDisplayMode() );
445         memcpy( sz_mode_id_text, &mode_id, sizeof(mode_id) );
446
447         const char *psz_mode_name;
448         result = p_display_mode->GetName( &psz_mode_name );
449         if( result != S_OK )
450         {
451             msg_Err( p_demux, "Failed to get display mode name" );
452             p_display_mode->Release();
453             goto finish;
454         }
455
456         BMDTimeValue frame_duration, time_scale;
457         result = p_display_mode->GetFrameRate( &frame_duration, &time_scale );
458         if( result != S_OK )
459         {
460             msg_Err( p_demux, "Failed to get frame rate" );
461             p_display_mode->Release();
462             goto finish;
463         }
464
465         const char *psz_field_dominance;
466         uint32_t i_dominance_flags = 0;
467         switch( p_display_mode->GetFieldDominance() )
468         {
469         case bmdProgressiveFrame:
470             psz_field_dominance = "";
471             break;
472         case bmdProgressiveSegmentedFrame:
473             psz_field_dominance = ", segmented";
474             break;
475         case bmdLowerFieldFirst:
476             psz_field_dominance = ", interlaced [BFF]";
477             i_dominance_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
478             break;
479         case bmdUpperFieldFirst:
480             psz_field_dominance = ", interlaced [TFF]";
481             i_dominance_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
482             break;
483         case bmdUnknownFieldDominance:
484         default:
485             psz_field_dominance = ", unknown field dominance";
486             break;
487         }
488
489         msg_Dbg( p_demux, "Found mode '%s': %s (%dx%d, %.3f fps%s)",
490                  sz_mode_id_text, psz_mode_name,
491                  p_display_mode->GetWidth(), p_display_mode->GetHeight(),
492                  double(time_scale) / frame_duration, psz_field_dominance );
493
494         if( wanted_mode_id == mode_id )
495         {
496             b_found_mode = true;
497             i_width = p_display_mode->GetWidth();
498             i_height = p_display_mode->GetHeight();
499             i_fps_num = time_scale;
500             i_fps_den = frame_duration;
501             p_sys->i_dominance_flags = i_dominance_flags;
502         }
503
504         p_display_mode->Release();
505     }
506
507     if( !b_found_mode )
508     {
509         msg_Err( p_demux, "Unknown video mode specified. " \
510                           "Run VLC with -v --verbose-objects=-all,+decklink " \
511                           "to get a list of supported modes." );
512         goto finish;
513     }
514
515     result = p_sys->p_input->EnableVideoInput( htonl( wanted_mode_id ), bmdFormat8BitYUV, 0 );
516     if( result != S_OK )
517     {
518         msg_Err( p_demux, "Failed to enable video input" );
519         goto finish;
520     }
521
522     /* Set up audio. */
523     p_sys->i_channels = var_InheritInteger( p_demux, "decklink-audio-channels" );
524     i_rate = var_InheritInteger( p_demux, "decklink-audio-rate" );
525     if( i_rate > 0 && p_sys->i_channels > 0 )
526     {
527         result = p_sys->p_input->EnableAudioInput( i_rate, bmdAudioSampleType16bitInteger, p_sys->i_channels );
528         if( result != S_OK )
529         {
530             msg_Err( p_demux, "Failed to enable audio input" );
531             goto finish;
532         }
533     }
534
535     p_sys->p_delegate = new DeckLinkCaptureDelegate( p_demux );
536     p_sys->p_input->SetCallback( p_sys->p_delegate );
537
538     result = p_sys->p_input->StartStreams();
539     if( result != S_OK )
540     {
541         msg_Err( p_demux, "Could not start streaming from SDI card. This could be caused "
542                           "by invalid video mode or flags, access denied, or card already in use." );
543         goto finish;
544     }
545
546     /* Declare elementary streams */
547     es_format_t video_fmt;
548     es_format_Init( &video_fmt, VIDEO_ES, VLC_CODEC_UYVY );
549     video_fmt.video.i_width = i_width;
550     video_fmt.video.i_height = i_height;
551     video_fmt.video.i_sar_num = 1;
552     video_fmt.video.i_sar_den = 1;
553     video_fmt.video.i_frame_rate = i_fps_num;
554     video_fmt.video.i_frame_rate_base = i_fps_den;
555     video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8;
556
557     if ( !var_InheritURational( p_demux, &u_aspect_num, &u_aspect_den, "decklink-aspect-ratio" ) &&
558          u_aspect_num > 0 && u_aspect_den > 0 ) {
559         video_fmt.video.i_sar_num = u_aspect_num * video_fmt.video.i_height;
560         video_fmt.video.i_sar_den = u_aspect_den * video_fmt.video.i_width;
561     }
562
563     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
564              (char*)&video_fmt.i_codec, video_fmt.video.i_width, video_fmt.video.i_height );
565     p_sys->p_video_es = es_out_Add( p_demux->out, &video_fmt );
566
567     es_format_t audio_fmt;
568     es_format_Init( &audio_fmt, AUDIO_ES, VLC_CODEC_S16N );
569     audio_fmt.audio.i_channels = p_sys->i_channels;
570     audio_fmt.audio.i_rate = i_rate;
571     audio_fmt.audio.i_bitspersample = 16;
572     audio_fmt.audio.i_blockalign = audio_fmt.audio.i_channels * audio_fmt.audio.i_bitspersample / 8;
573     audio_fmt.i_bitrate = audio_fmt.audio.i_channels * audio_fmt.audio.i_rate * audio_fmt.audio.i_bitspersample;
574
575     msg_Dbg( p_demux, "added new audio es %4.4s %dHz %dbpp %dch",
576              (char*)&audio_fmt.i_codec, audio_fmt.audio.i_rate, audio_fmt.audio.i_bitspersample, audio_fmt.audio.i_channels);
577     p_sys->p_audio_es = es_out_Add( p_demux->out, &audio_fmt );
578
579     ret = VLC_SUCCESS;
580
581 finish:
582     if( decklink_iterator )
583         decklink_iterator->Release();
584
585     if( p_config )
586         p_config->Release();
587
588     free( psz_video_connection );
589     free( psz_audio_connection );
590     free( psz_display_mode );
591
592     if( p_display_iterator )
593         p_display_iterator->Release();
594
595     if( ret != VLC_SUCCESS )
596         Close( p_this );
597
598     return ret;
599 }
600
601 static void Close( vlc_object_t *p_this )
602 {
603     demux_t     *p_demux = (demux_t *)p_this;
604     demux_sys_t *p_sys   = p_demux->p_sys;
605
606     if( p_sys->p_input )
607     {
608         p_sys->p_input->StopStreams();
609         p_sys->p_input->Release();
610     }
611
612     if( p_sys->p_card )
613         p_sys->p_card->Release();
614
615     if( p_sys->p_delegate )
616         p_sys->p_delegate->Release();
617
618     vlc_mutex_destroy( &p_sys->pts_lock );
619     free( p_sys );
620 }
621
622 static int Control( demux_t *p_demux, int i_query, va_list args )
623 {
624     demux_sys_t *p_sys = p_demux->p_sys;
625     bool *pb;
626     int64_t    *pi64;
627
628     switch( i_query )
629     {
630         /* Special for access_demux */
631         case DEMUX_CAN_PAUSE:
632         case DEMUX_CAN_SEEK:
633         case DEMUX_CAN_CONTROL_PACE:
634             pb = (bool*)va_arg( args, bool * );
635             *pb = false;
636             return VLC_SUCCESS;
637
638         case DEMUX_GET_PTS_DELAY:
639             pi64 = (int64_t*)va_arg( args, int64_t * );
640             *pi64 = var_InheritInteger( p_demux, "decklink-caching" ) * 1000;
641             return VLC_SUCCESS;
642
643         case DEMUX_GET_TIME:
644             pi64 = (int64_t*)va_arg( args, int64_t * );
645             vlc_mutex_lock( &p_sys->pts_lock );
646             *pi64 = p_sys->i_last_pts;
647             vlc_mutex_unlock( &p_sys->pts_lock );
648             return VLC_SUCCESS;
649
650         default:
651             return VLC_EGENERIC;
652     }
653
654     return VLC_EGENERIC;
655 }