]> git.sesse.net Git - mlt/blob - src/modules/decklink/producer_decklink.cpp
enumerate DeckLink devices when list_devices property is set
[mlt] / src / modules / decklink / producer_decklink.cpp
1 /*
2  * producer_decklink.c -- input from Blackmagic Design DeckLink
3  * Copyright (C) 2011 Dan Dennedy <dan@dennedy.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with consumer library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <framework/mlt.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <sys/time.h>
27 #ifdef WIN32
28 #include <objbase.h>
29 #include "DeckLinkAPI_h.h"
30 #else
31 #include "DeckLinkAPI.h"
32 #endif
33
34 #define SAFE_RELEASE(V) if (V) { V->Release(); V = NULL; }
35
36 class DeckLinkProducer
37         : public IDeckLinkInputCallback
38 {
39 private:
40         mlt_producer     m_producer;
41         IDeckLink*       m_decklink;
42         IDeckLinkInput*  m_decklinkInput;
43         mlt_deque        m_queue;
44         pthread_mutex_t  m_mutex;
45         pthread_cond_t   m_condition;
46         bool             m_started;
47         int              m_dropped;
48         bool             m_isBuffering;
49         int              m_topFieldFirst;
50         int              m_colorspace;
51         int              m_vancLines;
52         mlt_cache        m_cache;
53
54         BMDDisplayMode getDisplayMode( mlt_profile profile, int vancLines )
55         {
56                 IDeckLinkDisplayModeIterator* iter = NULL;
57                 IDeckLinkDisplayMode* mode = NULL;
58                 BMDDisplayMode result = (BMDDisplayMode) bmdDisplayModeNotSupported;
59
60                 if ( m_decklinkInput->GetDisplayModeIterator( &iter ) == S_OK )
61                 {
62                         while ( !result && iter->Next( &mode ) == S_OK )
63                         {
64                                 int width = mode->GetWidth();
65                                 int height = mode->GetHeight();
66                                 BMDTimeValue duration;
67                                 BMDTimeScale timescale;
68                                 mode->GetFrameRate( &duration, &timescale );
69                                 double fps = (double) timescale / duration;
70                                 int p = mode->GetFieldDominance() == bmdProgressiveFrame;
71                                 m_topFieldFirst = mode->GetFieldDominance() == bmdUpperFieldFirst;
72                                 m_colorspace = ( mode->GetFlags() & bmdDisplayModeColorspaceRec709 ) ? 709 : 601;
73                                 mlt_log_verbose( getProducer(), "BMD mode %dx%d %.3f fps prog %d tff %d\n", width, height, fps, p, m_topFieldFirst );
74
75                                 if ( width == profile->width && p == profile->progressive
76                                          && ( height + vancLines == profile->height || ( height == 486 && profile->height == 480 + vancLines ) )
77                                          && fps == mlt_profile_fps( profile ) )
78                                         result = mode->GetDisplayMode();
79                                 SAFE_RELEASE( mode );
80                         }
81                         SAFE_RELEASE( iter );
82                 }
83
84                 return result;
85         }
86
87 public:
88
89         void setProducer( mlt_producer producer )
90                 { m_producer = producer; }
91
92         mlt_producer getProducer() const
93                 { return m_producer; }
94
95         DeckLinkProducer()
96         {
97                 m_decklink = NULL;
98                 m_decklinkInput = NULL;
99         }
100
101         ~DeckLinkProducer()
102         {
103                 if ( m_queue )
104                 {
105                         stop();
106                         mlt_deque_close( m_queue );
107                         pthread_mutex_destroy( &m_mutex );
108                         pthread_cond_destroy( &m_condition );
109                         mlt_cache_close( m_cache );
110                 }
111                 SAFE_RELEASE( m_decklinkInput );
112                 SAFE_RELEASE( m_decklink );
113         }
114
115         bool open( unsigned card =  0 )
116         {
117                 IDeckLinkIterator* decklinkIterator = NULL;
118                 try
119                 {
120 #ifdef WIN32
121                         HRESULT result =  CoInitialize( NULL );
122                         if ( FAILED( result ) )
123                                 throw "COM initialization failed";
124                         result = CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &decklinkIterator );
125                         if ( FAILED( result ) )
126                                 throw "The DeckLink drivers are not installed.";
127 #else
128                         decklinkIterator = CreateDeckLinkIteratorInstance();
129                         if ( !decklinkIterator )
130                                 throw "The DeckLink drivers are not installed.";
131 #endif
132                         // Connect to the Nth DeckLink instance
133                         for ( unsigned i = 0; decklinkIterator->Next( &m_decklink ) == S_OK ; i++)
134                         {
135                                 if ( i == card )
136                                         break;
137                                 else
138                                         SAFE_RELEASE( m_decklink );
139                         }
140                         SAFE_RELEASE( decklinkIterator );
141                         if ( !m_decklink )
142                                 throw "DeckLink card not found.";
143
144                         // Get the input interface
145                         if ( m_decklink->QueryInterface( IID_IDeckLinkInput, (void**) &m_decklinkInput ) != S_OK )
146                                 throw "No DeckLink cards support input.";
147
148                         // Provide this class as a delegate to the input callback
149                         m_decklinkInput->SetCallback( this );
150
151                         // Initialize other members
152                         pthread_mutex_init( &m_mutex, NULL );
153                         pthread_cond_init( &m_condition, NULL );
154                         m_queue = mlt_deque_init();
155                         m_started = false;
156                         m_dropped = 0;
157                         m_isBuffering = true;
158                         m_cache = mlt_cache_init();
159
160                         // 3 covers YADIF and increasing framerate use cases
161                         mlt_cache_set_size( m_cache, 3 );
162                 }
163                 catch ( const char *error )
164                 {
165                         SAFE_RELEASE( m_decklinkInput );
166                         SAFE_RELEASE( m_decklink );
167                         mlt_log_error( getProducer(), "%s\n", error );
168                         return false;
169                 }
170                 return true;
171         }
172
173         bool start( mlt_profile profile = 0 )
174         {
175                 if ( m_started )
176                         return false;
177                 try
178                 {
179                         // Initialize some members
180                         m_vancLines = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "vanc" );
181                         if ( m_vancLines == -1 )
182                                 m_vancLines = profile->height <= 512 ? 26 : 32;
183
184                         if ( !profile )
185                                 profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
186
187                         // Get the display mode
188                         BMDDisplayMode displayMode = getDisplayMode( profile, m_vancLines );
189                         if ( displayMode == (BMDDisplayMode) bmdDisplayModeNotSupported )
190                         {
191                                 mlt_log_info( getProducer(), "profile = %dx%d %f fps %s\n", profile->width, profile->height,
192                                                           mlt_profile_fps( profile ), profile->progressive? "progressive" : "interlace" );
193                                 throw "Profile is not compatible with decklink.";
194                         }
195
196                         // Determine if supports input format detection
197 #ifdef WIN32
198                         BOOL doesDetectFormat = FALSE;
199 #else
200                         bool doesDetectFormat = false;
201 #endif
202                         IDeckLinkAttributes *decklinkAttributes = 0;
203                         if ( m_decklink->QueryInterface( IID_IDeckLinkAttributes, (void**) &decklinkAttributes ) == S_OK )
204                         {
205                                 if ( decklinkAttributes->GetFlag( BMDDeckLinkSupportsInputFormatDetection, &doesDetectFormat ) != S_OK )
206                                         doesDetectFormat = false;
207                                 SAFE_RELEASE( decklinkAttributes );
208                         }
209                         mlt_log_verbose( getProducer(), "%s format detection\n", doesDetectFormat ? "supports" : "does not support" );
210
211                         // Enable video capture
212                         BMDPixelFormat pixelFormat = bmdFormat8BitYUV;
213                         BMDVideoInputFlags flags = doesDetectFormat ? bmdVideoInputEnableFormatDetection : bmdVideoInputFlagDefault;
214                         if ( S_OK != m_decklinkInput->EnableVideoInput( displayMode, pixelFormat, flags ) )
215                                 throw "Failed to enable video capture.";
216
217                         // Enable audio capture
218                         BMDAudioSampleRate sampleRate = bmdAudioSampleRate48kHz;
219                         BMDAudioSampleType sampleType = bmdAudioSampleType16bitInteger;
220                         int channels = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" );
221                         if ( S_OK != m_decklinkInput->EnableAudioInput( sampleRate, sampleType, channels ) )
222                                 throw "Failed to enable audio capture.";
223
224                         // Start capture
225                         m_dropped = 0;
226                         mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "dropped", m_dropped );
227                         m_started = m_decklinkInput->StartStreams() == S_OK;
228                         if ( !m_started )
229                                 throw "Failed to start capture.";
230                 }
231                 catch ( const char *error )
232                 {
233                         m_decklinkInput->DisableVideoInput();
234                         mlt_log_error( getProducer(), "%s\n", error );
235                         return false;
236                 }
237                 return true;
238         }
239
240         void stop()
241         {
242                 if ( !m_started )
243                         return;
244                 m_started = false;
245
246                 // Release the wait in getFrame
247                 pthread_mutex_lock( &m_mutex );
248                 pthread_cond_broadcast( &m_condition );
249                 pthread_mutex_unlock( &m_mutex );
250
251                 m_decklinkInput->StopStreams();
252
253                 // Cleanup queue
254                 pthread_mutex_lock( &m_mutex );
255                 while ( mlt_frame frame = (mlt_frame) mlt_deque_pop_back( m_queue ) )
256                         mlt_frame_close( frame );
257                 pthread_mutex_unlock( &m_mutex );
258         }
259
260         mlt_frame getFrame()
261         {
262                 mlt_frame frame = NULL;
263                 struct timeval now;
264                 struct timespec tm;
265                 double fps = mlt_producer_get_fps( getProducer() );
266                 mlt_position position = mlt_producer_position( getProducer() );
267                 mlt_cache_item cached = mlt_cache_get( m_cache, (void*) position );
268
269                 // Allow the buffer to fill to the requested initial buffer level.
270                 if ( m_isBuffering )
271                 {
272                         int prefill = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "prefill" );
273                         int buffer = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "buffer" );
274
275                         m_isBuffering = false;
276                         prefill = prefill > buffer ? buffer : prefill;
277                         pthread_mutex_lock( &m_mutex );
278                         while ( mlt_deque_count( m_queue ) < prefill )
279                         {
280                                 // Wait up to buffer/fps seconds
281                                 gettimeofday( &now, NULL );
282                                 long usec = now.tv_sec * 1000000 + now.tv_usec;
283                                 usec += 1000000 * buffer / fps;
284                                 tm.tv_sec = usec / 1000000;
285                                 tm.tv_nsec = (usec % 1000000) * 1000;
286                                 if ( pthread_cond_timedwait( &m_condition, &m_mutex, &tm ) )
287                                         break;
288                         }
289                         pthread_mutex_unlock( &m_mutex );
290                 }
291
292                 if ( cached )
293                 {
294                         // Copy cached frame instead of pulling from queue
295                         frame = mlt_frame_clone( (mlt_frame) mlt_cache_item_data( cached, NULL ), 0 );
296                         mlt_cache_item_close( cached );
297                 }
298                 else
299                 {
300                         // Wait if queue is empty
301                         pthread_mutex_lock( &m_mutex );
302                         while ( mlt_deque_count( m_queue ) < 1 )
303                         {
304                                 // Wait up to twice frame duration
305                                 gettimeofday( &now, NULL );
306                                 long usec = now.tv_sec * 1000000 + now.tv_usec;
307                                 usec += 2000000 / fps;
308                                 tm.tv_sec = usec / 1000000;
309                                 tm.tv_nsec = (usec % 1000000) * 1000;
310                                 if ( pthread_cond_timedwait( &m_condition, &m_mutex, &tm ) )
311                                         // Stop waiting if error (timed out)
312                                         break;
313                         }
314                         frame = ( mlt_frame ) mlt_deque_pop_front( m_queue );
315                         pthread_mutex_unlock( &m_mutex );
316
317                         // add to cache
318                         if ( frame )
319                                 mlt_cache_put( m_cache, (void*) position, mlt_frame_clone( frame, 1 ), 0,
320                                         (mlt_destructor) mlt_frame_close );
321                 }
322
323                 // Set frame timestamp and properties
324                 if ( frame )
325                 {
326                         mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
327                         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
328                         mlt_properties_set_int( properties, "progressive", profile->progressive );
329                         mlt_properties_set_int( properties, "meta.media.progressive", profile->progressive );
330                         mlt_properties_set_int( properties, "top_field_first", m_topFieldFirst );
331                         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
332                         mlt_properties_set_int( properties, "meta.media.sample_aspect_num", profile->sample_aspect_num );
333                         mlt_properties_set_int( properties, "meta.media.sample_aspect_den", profile->sample_aspect_den );
334                         mlt_properties_set_int( properties, "meta.media.frame_rate_num", profile->frame_rate_num );
335                         mlt_properties_set_int( properties, "meta.media.frame_rate_den", profile->frame_rate_den );
336                         mlt_properties_set_int( properties, "width", profile->width );
337                         mlt_properties_set_int( properties, "meta.media.width", profile->width );
338                         mlt_properties_set_int( properties, "height", profile->height );
339                         mlt_properties_set_int( properties, "meta.media.height", profile->height );
340                         mlt_properties_set_int( properties, "format", mlt_image_yuv422 );
341                         mlt_properties_set_int( properties, "colorspace", m_colorspace );
342                         mlt_properties_set_int( properties, "meta.media.colorspace", m_colorspace );
343                         mlt_properties_set_int( properties, "audio_frequency", 48000 );
344                         mlt_properties_set_int( properties, "audio_channels",
345                                 mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" ) );
346                 }
347                 else
348                         mlt_log_warning( getProducer(), "buffer underrun\n" );
349
350                 return frame;
351         }
352
353         // *** DeckLink API implementation of IDeckLinkInputCallback *** //
354
355         // IUnknown needs only a dummy implementation
356         virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppv )
357                 { return E_NOINTERFACE; }
358         virtual ULONG STDMETHODCALLTYPE AddRef()
359                 { return 1; }
360         virtual ULONG STDMETHODCALLTYPE Release()
361                 { return 1; }
362
363         /************************* DeckLink API Delegate Methods *****************************/
364
365         virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(
366                         IDeckLinkVideoInputFrame* video,
367                         IDeckLinkAudioInputPacket* audio )
368         {
369                 if ( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "preview" ) &&
370                         mlt_producer_get_speed( getProducer() ) == 0.0 && !mlt_deque_count( m_queue ))
371                 {
372                         pthread_cond_broadcast( &m_condition );
373                         return S_OK;
374                 }
375
376                 // Create mlt_frame
377                 mlt_frame frame = mlt_frame_init( MLT_PRODUCER_SERVICE( getProducer() ) );
378
379                 // Copy video
380                 if ( video )
381                 {
382                         if ( !( video->GetFlags() & bmdFrameHasNoInputSource ) )
383                         {
384                                 int size = video->GetRowBytes() * ( video->GetHeight() + m_vancLines );
385                                 void* image = mlt_pool_alloc( size );
386                                 void* buffer = 0;
387                                 unsigned char* p = (unsigned char*) image;
388                                 int n = size / 2;
389 \
390                                 // Initialize VANC lines to nominal black
391                                 while ( --n )
392                                 {
393                                         *p ++ = 16;
394                                         *p ++ = 128;
395                                 }
396
397                                 // Capture VANC
398                                 if ( m_vancLines > 0 )
399                                 {
400                                         IDeckLinkVideoFrameAncillary* vanc = 0;
401                                         if ( video->GetAncillaryData( &vanc ) == S_OK && vanc )
402                                         {
403                                                 for ( int i = 1; i < m_vancLines + 1; i++ )
404                                                 {
405                                                         if ( vanc->GetBufferForVerticalBlankingLine( i, &buffer ) == S_OK )
406                                                                 swab( (char*) buffer, (char*) image + ( i - 1 ) * video->GetRowBytes(), video->GetRowBytes() );
407                                                         else
408                                                                 mlt_log_debug( getProducer(), "failed capture vanc line %d\n", i );
409                                                 }
410                                                 SAFE_RELEASE(vanc);
411                                         }
412                                 }
413
414                                 // Capture image
415                                 video->GetBytes( &buffer );
416                                 if ( image && buffer )
417                                 {
418                                         size =  video->GetRowBytes() * video->GetHeight();
419                                         swab( (char*) buffer, (char*) image + m_vancLines * video->GetRowBytes(), size );
420                                         mlt_frame_set_image( frame, (uint8_t*) image, size, mlt_pool_release );
421                                 }
422                                 else if ( image )
423                                 {
424                                         mlt_log_verbose( getProducer(), "no video\n" );
425                                         mlt_pool_release( image );
426                                 }
427                         }
428                         else
429                         {
430                                 mlt_log_verbose( getProducer(), "no signal\n" );
431                                 mlt_frame_close( frame );
432                                 frame = 0;
433                         }
434
435                         // Get timecode
436                         IDeckLinkTimecode* timecode = 0;
437                         if ( video->GetTimecode( bmdTimecodeVITC, &timecode ) == S_OK && timecode )
438                         {
439                                 const char* timecodeString = 0;
440
441 #ifdef WIN32
442                                 if ( timecode->GetString( (BSTR*) &timecodeString ) == S_OK )
443 #else
444                                 if ( timecode->GetString( &timecodeString ) == S_OK )
445 #endif
446                                 {
447                                         mlt_properties_set( MLT_FRAME_PROPERTIES( frame ), "meta.attr.vitc.markup", timecodeString );
448                                         mlt_log_debug( getProducer(), "timecode %s\n", timecodeString );
449                                 }
450                                 if ( timecodeString )
451                                         free( (void*) timecodeString );
452                                 SAFE_RELEASE( timecode );
453                         }
454                 }
455                 else
456                 {
457                         mlt_log_verbose( getProducer(), "no video\n" );
458                         mlt_frame_close( frame );
459                         frame = 0;
460                 }
461
462                 // Copy audio
463                 if ( frame && audio )
464                 {
465                         int channels = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "channels" );
466                         int size = audio->GetSampleFrameCount() * channels * sizeof(int16_t);
467                         mlt_audio_format format = mlt_audio_s16;
468                         void* pcm = mlt_pool_alloc( size );
469                         void* buffer = 0;
470
471                         audio->GetBytes( &buffer );
472                         if ( buffer )
473                         {
474                                 memcpy( pcm, buffer, size );
475                                 mlt_frame_set_audio( frame, pcm, format, size, mlt_pool_release );
476                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(frame), "audio_samples", audio->GetSampleFrameCount() );
477                         }
478                         else
479                         {
480                                 mlt_log_verbose( getProducer(), "no audio\n" );
481                                 mlt_pool_release( pcm );
482                         }
483                 }
484                 else
485                 {
486                         mlt_log_verbose( getProducer(), "no audio\n" );
487                 }
488
489                 // Put frame in queue
490                 if ( frame )
491                 {
492                         int queueMax = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "buffer" );
493                         pthread_mutex_lock( &m_mutex );
494                         if ( mlt_deque_count( m_queue ) < queueMax )
495                         {
496                                 mlt_deque_push_back( m_queue, frame );
497                                 pthread_cond_broadcast( &m_condition );
498                         }
499                         else
500                         {
501                                 mlt_frame_close( frame );
502                                 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( getProducer() ), "dropped", ++m_dropped );
503                                 mlt_log_warning( getProducer(), "frame dropped %d\n", m_dropped );
504                         }
505                         pthread_mutex_unlock( &m_mutex );
506                 }
507
508                 return S_OK;
509         }
510
511         virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(
512                         BMDVideoInputFormatChangedEvents events,
513                         IDeckLinkDisplayMode* mode,
514                         BMDDetectedVideoInputFormatFlags flags )
515         {
516                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( getProducer() ) );
517                 if ( events & bmdVideoInputDisplayModeChanged )
518                 {
519                         BMDTimeValue duration;
520                         BMDTimeScale timescale;
521                         mode->GetFrameRate( &duration, &timescale );
522                         profile->width = mode->GetWidth();
523                         profile->height = mode->GetHeight() + m_vancLines;
524                         profile->frame_rate_num = timescale;
525                         profile->frame_rate_den = duration;
526                         if ( profile->width == 720 )
527                         {
528                                 if ( profile->height == 576 )
529                                 {
530                                         profile->sample_aspect_num = 16;
531                                         profile->sample_aspect_den = 15;
532                                 }
533                                 else
534                                 {
535                                         profile->sample_aspect_num = 8;
536                                         profile->sample_aspect_den = 9;
537                                 }
538                                 profile->display_aspect_num = 4;
539                                 profile->display_aspect_den = 3;
540                         }
541                         else
542                         {
543                                 profile->sample_aspect_num = 1;
544                                 profile->sample_aspect_den = 1;
545                                 profile->display_aspect_num = 16;
546                                 profile->display_aspect_den = 9;
547                         }
548                         free( profile->description );
549                         profile->description = strdup( "decklink" );
550                         mlt_log_verbose( getProducer(), "format changed %dx%d %.3f fps\n",
551                                 profile->width, profile->height, (double) profile->frame_rate_num / profile->frame_rate_den );
552                 }
553                 if ( events & bmdVideoInputFieldDominanceChanged )
554                 {
555                         profile->progressive = mode->GetFieldDominance() == bmdProgressiveFrame;
556                         m_topFieldFirst = mode->GetFieldDominance() == bmdUpperFieldFirst;
557                         mlt_log_verbose( getProducer(), "field dominance changed prog %d tff %d\n",
558                                 profile->progressive, m_topFieldFirst );
559                 }
560                 if ( events & bmdVideoInputColorspaceChanged )
561                 {
562                         profile->colorspace = m_colorspace =
563                                 ( mode->GetFlags() & bmdDisplayModeColorspaceRec709 ) ? 709 : 601;
564                         mlt_log_verbose( getProducer(), "colorspace changed %d\n", profile->colorspace );
565                 }
566                 return S_OK;
567         }
568 };
569
570 static int get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
571 {
572         return mlt_frame_get_audio( frame, (void**) buffer, format, frequency, channels, samples );
573 }
574
575 static int get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
576 {
577         return mlt_frame_get_image( frame, buffer, format, width, height, writable );
578 }
579
580 static int get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
581 {
582         DeckLinkProducer* decklink = (DeckLinkProducer*) producer->child;
583         mlt_position pos = mlt_producer_position( producer );
584         mlt_position end = mlt_producer_get_playtime( producer );
585         end = ( mlt_producer_get_length( producer ) < end ? mlt_producer_get_length( producer ) : end ) - 1;
586
587         // Re-open if needed
588         if ( !decklink && pos < end )
589         {
590                 producer->child = decklink = new DeckLinkProducer();
591                 decklink->setProducer( producer );
592                 decklink->open( mlt_properties_get_int( MLT_PRODUCER_PROPERTIES(producer), "resource" ) );
593         }
594
595         // Start if needed
596         if ( decklink )
597         {
598                 decklink->start( mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) ) );
599
600                 // Get the next frame from the decklink object
601                 if ( ( *frame = decklink->getFrame() ))
602                 {
603                         // Add audio and video getters
604                         mlt_frame_push_audio( *frame, (void*) get_audio );
605                         mlt_frame_push_get_image( *frame, get_image );
606                 }
607         }
608         if ( !*frame )
609                 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE(producer) );
610
611         // Calculate the next timecode
612         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
613         mlt_producer_prepare_next( producer );
614
615         // Close DeckLink if at end
616         if ( pos >= end && decklink )
617         {
618                 decklink->stop();
619                 delete decklink;
620                 producer->child = NULL;
621         }
622
623         return 0;
624 }
625
626 static void producer_close( mlt_producer producer )
627 {
628         delete (DeckLinkProducer*) producer->child;
629         producer->close = NULL;
630         mlt_producer_close( producer );
631 }
632
633 extern "C" {
634
635 // Listen for the list_devices property to be set
636 static void on_property_changed( void*, mlt_properties properties, const char *name )
637 {
638         IDeckLinkIterator* decklinkIterator = NULL;
639         IDeckLink* decklink = NULL;
640         IDeckLinkInput* decklinkInput = NULL;
641         int i = 0;
642
643         if ( name && !strcmp( name, "list_devices" ) )
644                 mlt_event_block( (mlt_event) mlt_properties_get_data( properties, "list-devices-event", NULL ) );
645         else
646                 return;
647
648 #ifdef WIN32
649         if ( FAILED( CoInitialize( NULL ) ) )
650                 return;
651         if ( FAILED( CoCreateInstance( CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**) &decklinkIterator ) ) )
652                 return;
653 #else
654         if ( !( decklinkIterator = CreateDeckLinkIteratorInstance() ) )
655                 return;
656 #endif
657         for ( ; decklinkIterator->Next( &decklink ) == S_OK; i++ )
658         {
659                 if ( decklink->QueryInterface( IID_IDeckLinkInput, (void**) &decklinkInput ) == S_OK )
660                 {
661                         char *name = NULL;
662                         if ( decklink->GetModelName( (const char**) &name ) == S_OK )
663                         {
664                                 const char *format = "device.%d";
665                                 char *key = (char*) calloc( 1, strlen( format ) + 1 );
666
667                                 sprintf( key, format, i );
668                                 mlt_properties_set( properties, key, name );
669                                 free( key );
670                                 free( name );
671                         }
672                         SAFE_RELEASE( decklinkInput );
673                 }
674                 SAFE_RELEASE( decklink );
675         }
676         SAFE_RELEASE( decklinkIterator );
677         mlt_properties_set_int( properties, "devices", i );
678 }
679
680 /** Initialise the producer.
681  */
682
683 mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
684 {
685         // Allocate the producer
686         DeckLinkProducer* decklink = new DeckLinkProducer();
687         mlt_producer producer = (mlt_producer) calloc( 1, sizeof( *producer ) );
688
689         // If allocated and initializes
690         if ( decklink && !mlt_producer_init( producer, decklink ) )
691         {
692                 if ( decklink->open( arg? atoi( arg ) : 0 ) )
693                 {
694                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
695
696                         // Close DeckLink and defer re-open to get_frame
697                         delete decklink;
698                         producer->child = NULL;
699
700                         // Set callbacks
701                         producer->close = (mlt_destructor) producer_close;
702                         producer->get_frame = get_frame;
703
704                         // Set properties
705                         mlt_properties_set( properties, "resource", (arg && strcmp( arg, ""))? arg : "0" );
706                         mlt_properties_set_int( properties, "channels", 2 );
707                         mlt_properties_set_int( properties, "buffer", 25 );
708                         mlt_properties_set_int( properties, "prefill", 25 );
709
710                         // These properties effectively make it infinite.
711                         mlt_properties_set_int( properties, "length", INT_MAX );
712                         mlt_properties_set_int( properties, "out", INT_MAX - 1 );
713                         mlt_properties_set( properties, "eof", "loop" );
714
715                         mlt_event event = mlt_events_listen( properties, properties, "property-changed", (mlt_listener) on_property_changed );
716                         mlt_properties_set_data( properties, "list-devices-event", event, 0, NULL, NULL );
717                 }
718         }
719
720         return producer;
721 }
722
723 }