]> git.sesse.net Git - mlt/blob - src/modules/decklink/consumer_decklink.cpp
Add keyer support to decklink consumer.
[mlt] / src / modules / decklink / consumer_decklink.cpp
1 /*
2  * consumer_decklink.c -- output through Blackmagic Design DeckLink
3  * Copyright (C) 2010 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 <string.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <sys/time.h>
26 #include <limits.h>
27 #include "DeckLinkAPI.h"
28
29 static const unsigned PREROLL_MINIMUM = 3;
30
31 typedef struct
32 {
33         int16_t *buffer;
34         int size;
35         int used;
36         pthread_mutex_t mutex;
37 } *sample_fifo;
38
39 static sample_fifo sample_fifo_init()
40 {
41         sample_fifo fifo = (sample_fifo) calloc( 1, sizeof( *fifo ) );
42         pthread_mutex_init( &fifo->mutex, NULL );
43         return fifo;
44 }
45
46 static void sample_fifo_append( sample_fifo fifo, int16_t *samples, int count )
47 {
48         pthread_mutex_lock( &fifo->mutex );
49         if ( ( fifo->size - fifo->used ) < count )
50         {
51                 fifo->size += count * 5;
52                 fifo->buffer = (int16_t*) realloc( fifo->buffer, fifo->size * sizeof( int16_t ) );
53         }
54         memcpy( fifo->buffer + fifo->used, samples, count * sizeof( int16_t ) );
55         fifo->used += count;
56         pthread_mutex_unlock( &fifo->mutex );
57 }
58
59 static void sample_fifo_remove( sample_fifo fifo, int count )
60 {
61         pthread_mutex_lock( &fifo->mutex );
62         if ( count > fifo->used )
63                 count = fifo->used;
64         fifo->used -= count;
65         memmove( fifo->buffer, fifo->buffer + count, fifo->used * sizeof( int16_t ) );
66         pthread_mutex_unlock( &fifo->mutex );
67 }
68
69 static void sample_fifo_close( sample_fifo fifo )
70 {
71         free( fifo->buffer );
72         pthread_mutex_destroy( &fifo->mutex );
73         free( fifo );
74 }
75
76
77 class DeckLinkConsumer
78         : public IDeckLinkVideoOutputCallback
79         , public IDeckLinkAudioOutputCallback
80 {
81 private:
82         mlt_consumer_s              m_consumer;
83         IDeckLink*                  m_deckLink;
84         IDeckLinkOutput*            m_deckLinkOutput;
85         IDeckLinkDisplayMode*       m_displayMode;
86         pthread_mutex_t             m_mutex;
87         pthread_cond_t              m_condition;
88         int                         m_width;
89         int                         m_height;
90         BMDTimeValue                m_duration;
91         BMDTimeScale                m_timescale;
92         double                      m_fps;
93         uint64_t                    m_count;
94         sample_fifo                 m_fifo;
95         unsigned                    m_preroll;
96         bool                        m_isPrerolling;
97         unsigned                    m_prerollCounter;
98         int                         m_channels;
99         uint32_t                    m_maxAudioBuffer;
100         mlt_deque                   m_videoFrameQ;
101         mlt_frame                   m_frame;
102         unsigned                    m_dropped;
103         bool                        m_isAudio;
104         bool                        m_isKeyer;
105         IDeckLinkKeyer*             m_deckLinkKeyer;
106
107         IDeckLinkDisplayMode* getDisplayMode()
108         {
109                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( getConsumer() ) );
110                 IDeckLinkDisplayModeIterator* iter;
111                 IDeckLinkDisplayMode* mode;
112                 IDeckLinkDisplayMode* result = 0;
113                 
114                 if ( m_deckLinkOutput->GetDisplayModeIterator( &iter ) == S_OK )
115                 {
116                         while ( !result && iter->Next( &mode ) == S_OK )
117                         {
118                                 m_width = mode->GetWidth();
119                                 m_height = mode->GetHeight();
120                                 mode->GetFrameRate( &m_duration, &m_timescale );
121                                 m_fps = (double) m_timescale / m_duration;
122                                 int p = mode->GetFieldDominance() == bmdProgressiveFrame;
123                                 mlt_log_verbose( getConsumer(), "BMD mode %dx%d %.3f fps prog %d\n", m_width, m_height, m_fps, p );
124                                 
125                                 if ( m_width == profile->width && m_height == profile->height && p == profile->progressive
126                                          && m_fps == mlt_profile_fps( profile ) )
127                                         result = mode;
128                         }
129                 }
130                 
131                 return result;
132         }
133         
134 public:
135         mlt_consumer getConsumer()
136                 { return &m_consumer; }
137         uint64_t isBuffering() const
138                 { return m_prerollCounter < m_preroll; }
139         
140         ~DeckLinkConsumer()
141         {
142                 if ( m_deckLinkKeyer )
143                         m_deckLinkKeyer->Release();
144                 if ( m_deckLinkOutput )
145                         m_deckLinkOutput->Release();
146                 if ( m_deckLink )
147                         m_deckLink->Release();
148                 if ( m_videoFrameQ )
149                 {
150                         mlt_deque_close( m_videoFrameQ );
151                         pthread_mutex_destroy( &m_mutex );
152                         pthread_cond_destroy( &m_condition );
153                 }
154         }
155         
156         bool open( unsigned card = 0 )
157         {
158                 IDeckLinkIterator* deckLinkIterator = CreateDeckLinkIteratorInstance();
159                 unsigned i = 0;
160                 
161                 if ( !deckLinkIterator )
162                 {
163                         mlt_log_error( getConsumer(), "The DeckLink drivers not installed.\n" );
164                         return false;
165                 }
166                 
167                 // Connect to the Nth DeckLink instance
168                 do {
169                         if ( deckLinkIterator->Next( &m_deckLink ) != S_OK )
170                         {
171                                 mlt_log_error( getConsumer(), "DeckLink card not found\n" );
172                                 deckLinkIterator->Release();
173                                 return false;
174                         }
175                 } while ( ++i <= card );
176                 deckLinkIterator->Release();
177                 
178                 // Obtain the audio/video output interface (IDeckLinkOutput)
179                 if ( m_deckLink->QueryInterface( IID_IDeckLinkOutput, (void**)&m_deckLinkOutput ) != S_OK )
180                 {
181                         mlt_log_error( getConsumer(), "No DeckLink cards support output\n" );
182                         m_deckLink->Release();
183                         m_deckLink = 0;
184                         return false;
185                 }
186                 
187                 // Get the keyer interface
188                 if ( m_deckLink->QueryInterface( IID_IDeckLinkKeyer, (void**) &m_deckLinkKeyer ) != S_OK )
189                 {
190                         mlt_log_error( getConsumer(), "Failed to get keyer\n" );
191                         m_deckLinkOutput->Release();
192                         m_deckLinkOutput = 0;
193                         m_deckLink->Release();
194                         m_deckLink = 0;
195                         return false;
196                 }
197
198                 // Provide this class as a delegate to the audio and video output interfaces
199                 m_deckLinkOutput->SetScheduledFrameCompletionCallback( this );
200                 m_deckLinkOutput->SetAudioCallback( this );
201                 
202                 pthread_mutex_init( &m_mutex, NULL );
203                 pthread_cond_init( &m_condition, NULL );
204                 m_maxAudioBuffer = bmdAudioSampleRate48kHz;
205                 m_videoFrameQ = mlt_deque_init();
206                 
207                 return true;
208         }
209         
210         bool start( unsigned preroll )
211         {
212                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( getConsumer() );
213
214                 // Initialize members
215                 m_count = 0;
216                 m_frame = 0;
217                 m_dropped = 0;
218                 m_isPrerolling = true;
219                 m_prerollCounter = 0;
220                 m_preroll = preroll < PREROLL_MINIMUM ? PREROLL_MINIMUM : preroll;
221                 m_channels = mlt_properties_get_int( properties, "channels" );
222                 m_isAudio = !mlt_properties_get_int( properties, "audio_off" );
223
224                 m_displayMode = getDisplayMode();
225                 if ( !m_displayMode )
226                 {
227                         mlt_log_error( getConsumer(), "Profile is not compatible with decklink.\n" );
228                         return false;
229                 }
230                 
231                 // Set the keyer
232                 if ( ( m_isKeyer = mlt_properties_get_int( properties, "keyer" ) ) )
233                 {
234                         bool external = false;
235                         double level = mlt_properties_get_double( properties, "keyer_level" );
236
237                         if ( m_deckLinkKeyer->Enable( external ) != S_OK )
238                                 mlt_log_error( getConsumer(), "Failed to enable keyer\n" );
239                         m_deckLinkKeyer->SetLevel( level <= 1 ? ( level > 0 ? 255 * level : 255 ) : 255 );
240                         m_preroll = 0;
241                         m_isAudio = false;
242                 }
243                 else
244                 {
245                         m_deckLinkKeyer->Disable();
246                 }
247
248                 // Set the video output mode
249                 if ( S_OK != m_deckLinkOutput->EnableVideoOutput( m_displayMode->GetDisplayMode(), bmdVideoOutputFlagDefault) )
250                 {
251                         mlt_log_error( getConsumer(), "Failed to enable video output\n" );
252                         return false;
253                 }
254
255                 // Set the audio output mode
256                 if ( !m_isAudio )
257                 {
258                         m_deckLinkOutput->DisableAudioOutput();
259                         return true;
260                 }
261                 if ( S_OK != m_deckLinkOutput->EnableAudioOutput( bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
262                         m_channels, bmdAudioOutputStreamContinuous ) )
263                 {
264                         mlt_log_error( getConsumer(), "Failed to enable audio output\n" );
265                         stop();
266                         return false;
267                 }
268                 m_fifo = sample_fifo_init();
269                 m_deckLinkOutput->BeginAudioPreroll();
270                 
271                 return true;
272         }
273         
274         void wakeup()
275         {
276                 pthread_mutex_lock( &m_mutex );
277                 pthread_cond_broadcast( &m_condition );
278                 pthread_mutex_unlock( &m_mutex );
279         }
280         
281         void wait()
282         {
283                 struct timeval tv;
284                 struct timespec ts;
285                 
286                 gettimeofday( &tv, NULL );
287                 ts.tv_sec = tv.tv_sec + 1;
288                 ts.tv_nsec = tv.tv_usec * 1000;
289                 pthread_mutex_lock( &m_mutex );
290                 pthread_cond_timedwait( &m_condition, &m_mutex, &ts );
291                 pthread_mutex_unlock( &m_mutex );
292         }
293         
294         void stop()
295         {
296                 // Stop the audio and video output streams immediately
297                 if ( m_deckLinkOutput )
298                 {
299                         m_deckLinkOutput->StopScheduledPlayback( 0, 0, 0 );
300                         m_deckLinkOutput->DisableAudioOutput();
301                         m_deckLinkOutput->DisableVideoOutput();
302                 }
303                 while ( mlt_deque_count( m_videoFrameQ ) )
304                 {
305                         IDeckLinkMutableVideoFrame* frame = (IDeckLinkMutableVideoFrame*) mlt_deque_pop_back( m_videoFrameQ );
306                         frame->Release();
307                 }
308                 if ( m_fifo ) sample_fifo_close( m_fifo );
309                 mlt_frame_close( m_frame );
310         }
311
312         void renderAudio( mlt_frame frame )
313         {
314                 mlt_audio_format format = mlt_audio_s16;
315                 int frequency = bmdAudioSampleRate48kHz;
316                 int samples = mlt_sample_calculator( m_fps, frequency, m_count );
317                 int16_t *pcm = 0;
318
319                 if ( !mlt_frame_get_audio( frame, (void**) &pcm, &format, &frequency, &m_channels, &samples ) )
320                 {
321                         int count = samples;
322
323                         if ( !m_isPrerolling )
324                         {
325                                 uint32_t audioCount = 0;
326                                 uint32_t videoCount = 0;
327
328                                 // Check for resync
329                                 m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &audioCount );
330                                 m_deckLinkOutput->GetBufferedVideoFrameCount( &videoCount );
331
332                                 // Underflow typically occurs during non-normal speed playback.
333                                 if ( audioCount < 1 || videoCount < 1 )
334                                 {
335                                         // Upon switching to normal playback, buffer some frames faster than realtime.
336                                         mlt_log_info( getConsumer(), "buffer underrun: audio buf %u video buf %u frames\n", audioCount, videoCount );
337                                         m_prerollCounter = 0;
338                                 }
339
340                                 // While rebuffering
341                                 if ( isBuffering() )
342                                 {
343                                         // Only append audio to reach the ideal level and not overbuffer.
344                                         int ideal = ( m_preroll - 1 ) * bmdAudioSampleRate48kHz / m_fps;
345                                         int actual = m_fifo->used / m_channels + audioCount;
346                                         int diff = ideal / 2 - actual;
347                                         count = diff < 0 ? 0 : diff < count ? diff : count;
348                                 }
349                         }
350                         if ( count > 0 )
351                                 sample_fifo_append( m_fifo, pcm, count * m_channels );
352                 }
353         }
354
355         bool createFrame()
356         {
357                 BMDPixelFormat format = m_isKeyer? bmdFormat8BitARGB : bmdFormat8BitYUV;
358                 IDeckLinkMutableVideoFrame* frame = 0;
359                 uint8_t *buffer = 0;
360                 int stride = m_width * ( m_isKeyer? 4 : 2 );
361
362                 // Generate a DeckLink video frame
363                 if ( S_OK != m_deckLinkOutput->CreateVideoFrame( m_width, m_height,
364                         stride, format, bmdFrameFlagDefault, &frame ) )
365                 {
366                         mlt_log_verbose( getConsumer(), "Failed to create video frame\n" );
367                         stop();
368                         return false;
369                 }
370                 
371                 // Make the first line black for field order correction.
372                 if ( S_OK == frame->GetBytes( (void**) &buffer ) && buffer )
373                 {
374                         if ( m_isKeyer )
375                         {
376                                 memset( buffer, 0, stride );
377                         }
378                         else for ( int i = 0; i < m_width; i++ )
379                         {
380                                 *buffer++ = 128;
381                                 *buffer++ = 16;
382                         }
383                 }
384                 mlt_log_debug( getConsumer(), "created video frame\n" );
385                 mlt_deque_push_back( m_videoFrameQ, frame );
386
387                 return true;
388         }
389
390         void renderVideo()
391         {
392                 mlt_image_format format = m_isKeyer? mlt_image_rgb24a : mlt_image_yuv422;
393                 uint8_t* image = 0;
394
395                 if ( !mlt_frame_get_image( m_frame, &image, &format, &m_width, &m_height, 0 ) )
396                 {
397                         IDeckLinkMutableVideoFrame* decklinkFrame = (IDeckLinkMutableVideoFrame*) mlt_deque_pop_back( m_videoFrameQ );
398                         uint8_t* buffer = 0;
399                         int stride = m_width * ( m_isKeyer? 4 : 2 );
400
401                         decklinkFrame->GetBytes( (void**) &buffer );
402                         if ( buffer )
403                         {
404                                 int progressive = mlt_properties_get_int( MLT_FRAME_PROPERTIES( m_frame ), "progressive" );
405
406                                 if ( !m_isKeyer )
407                                 {
408                                         // Normal non-keyer playout - needs byte swapping
409                                         if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
410                                                 // convert lower field first to top field first
411                                                 swab( image, buffer + stride, stride * ( m_height - 1 ) );
412                                         else
413                                                 swab( image, buffer, stride * m_height );
414                                 }
415                                 else if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES( m_frame ), "test_image" ) )
416                                 {
417                                         // Normal keyer output
418                                         int y = m_height + 1;
419                                         uint32_t* s = (uint32_t*) image;
420                                         uint32_t* d = (uint32_t*) buffer;
421
422                                         if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
423                                         {
424                                                 // Correct field order
425                                                 m_height--;
426                                                 d += stride;
427                                         }
428
429                                         // Need to relocate alpha channel RGBA => ARGB
430                                         while ( --y )
431                                         {
432                                                 int x = m_width + 1;
433                                                 while ( --x )
434                                                 {
435                                                         *d++ = ( *s << 8 ) | ( *s >> 24 );
436                                                         s++;
437                                                 }
438                                         }
439                                 }
440                                 else
441                                 {
442                                         // Keying blank frames - nullify alpha
443                                         memset( buffer, 0, stride * m_height );
444                                 }
445                                 m_deckLinkOutput->ScheduleVideoFrame( decklinkFrame, m_count * m_duration, m_duration, m_timescale );
446                         }
447                         mlt_deque_push_front( m_videoFrameQ, decklinkFrame );
448                 }
449         }
450
451         HRESULT render( mlt_frame frame )
452         {
453                 HRESULT result = S_OK;
454
455                 // Get the audio
456                 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES(frame), "_speed" );
457                 if ( m_isAudio && speed == 1.0 )
458                         renderAudio( frame );
459                 
460                 // Create video frames while pre-rolling
461                 if ( m_isPrerolling )
462                 {
463                         if ( !createFrame() )
464                         {
465                                 mlt_log_error( getConsumer(), "failed to create video frame\n" );
466                                 return S_FALSE;
467                         }
468                 }
469                 
470                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered") )
471                 {
472                         // Close the previous frame and use the new one
473                         mlt_frame_close( m_frame );
474                         m_frame = frame;
475                 }
476                 else
477                 {
478                         if ( !m_frame )
479                                 m_frame = frame;
480                         // Reuse the last frame
481                         mlt_log_verbose( getConsumer(), "dropped video frame %u\n", ++m_dropped );
482                 }
483
484                 // Get the video
485                 renderVideo();
486                 ++m_count;
487
488                 // Check for end of pre-roll
489                 if ( ++m_prerollCounter > m_preroll && m_isPrerolling )
490                 {
491                         // Start audio and video output
492                         if ( m_isAudio )
493                                 m_deckLinkOutput->EndAudioPreroll();
494                         m_deckLinkOutput->StartScheduledPlayback( 0, m_timescale, 1.0 );
495                         m_isPrerolling = false;
496                 }
497
498                 return result;
499         }
500         
501         // *** DeckLink API implementation of IDeckLinkVideoOutputCallback IDeckLinkAudioOutputCallback *** //
502
503         // IUnknown needs only a dummy implementation
504         virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppv )
505                 { return E_NOINTERFACE; }
506         virtual ULONG STDMETHODCALLTYPE AddRef()
507                 { return 1; }
508         virtual ULONG STDMETHODCALLTYPE Release()
509                 { return 1; }
510         
511         /************************* DeckLink API Delegate Methods *****************************/
512         
513         virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted( IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult completed )
514         {
515                 // When a video frame has been released by the API, schedule another video frame to be output
516                 wakeup();
517                 
518                 return S_OK;
519         }
520
521         virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped()
522         {
523                 return mlt_consumer_is_stopped( getConsumer() ) ? S_FALSE : S_OK;
524         }
525         
526         virtual HRESULT STDMETHODCALLTYPE RenderAudioSamples( bool preroll )
527         {
528                 // Provide more audio samples to the DeckLink API
529                 HRESULT result = S_OK;
530
531                 uint32_t count = m_fifo->used / m_channels;
532                 uint32_t buffered = count;
533
534                 if ( count
535                         // Stay under preferred buffer level
536                         && ( S_OK == m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &buffered ) )
537                         && buffered < m_maxAudioBuffer )
538                 {
539                         uint32_t written = 0;
540                         
541                         buffered = m_maxAudioBuffer - buffered;
542                         count = buffered > count ? count : buffered;
543                         result = m_deckLinkOutput->ScheduleAudioSamples( m_fifo->buffer, count, 0, 0, &written );
544                         if ( written )
545                                 sample_fifo_remove( m_fifo, written * m_channels );
546                 }
547
548                 return result;
549         }
550 };
551
552 /** The main thread.
553  */
554
555 static void *run( void *arg )
556 {
557         // Map the argument to the object
558         DeckLinkConsumer* decklink = (DeckLinkConsumer*) arg;
559         mlt_consumer consumer = decklink->getConsumer();
560         
561         // Get the properties
562         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
563
564         // Convenience functionality
565         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
566         int terminated = 0;
567
568         // Frame and size
569         mlt_frame frame = NULL;
570         
571         // Loop while running
572         while ( !terminated && mlt_properties_get_int( properties, "running" ) )
573         {
574                 // Get the frame
575                 if ( ( frame = mlt_consumer_rt_frame( consumer ) ) )
576                 {
577                         // Check for termination
578                         if ( terminate_on_pause )
579                                 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
580
581                         decklink->render( frame );
582                         if ( !decklink->isBuffering() )
583                                 decklink->wait();
584                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
585                 }
586         }
587
588         // Indicate that the consumer is stopped
589         decklink->stop();
590         mlt_properties_set_int( properties, "running", 0 );
591         mlt_consumer_stopped( consumer );
592
593         return NULL;
594 }
595
596 /** Start the consumer.
597  */
598
599 static int start( mlt_consumer consumer )
600 {
601         // Get the properties
602         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
603         DeckLinkConsumer* decklink = (DeckLinkConsumer*) consumer->child;
604         int result = decklink->start( mlt_properties_get_int( properties, "preroll" ) ) ? 0 : 1;
605
606         // Check that we're not already running
607         if ( !result && !mlt_properties_get_int( properties, "running" ) )
608         {
609                 // Allocate a thread
610                 pthread_t *pthread = (pthread_t*) calloc( 1, sizeof( pthread_t ) );
611
612                 // Assign the thread to properties
613                 mlt_properties_set_data( properties, "pthread", pthread, sizeof( pthread_t ), free, NULL );
614
615                 // Set the running state
616                 mlt_properties_set_int( properties, "running", 1 );
617                 mlt_properties_set_int( properties, "joined", 0 );
618
619                 // Create the thread
620                 pthread_create( pthread, NULL, run, consumer->child );
621         }
622         return result;
623 }
624
625 /** Stop the consumer.
626  */
627
628 static int stop( mlt_consumer consumer )
629 {
630         // Get the properties
631         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
632
633         // Check that we're running
634         if ( !mlt_properties_get_int( properties, "joined" ) )
635         {
636                 // Get the thread
637                 pthread_t *pthread = (pthread_t*) mlt_properties_get_data( properties, "pthread", NULL );
638                 
639                 if ( pthread )
640                 {
641                         // Stop the thread
642                         mlt_properties_set_int( properties, "running", 0 );
643                         mlt_properties_set_int( properties, "joined", 1 );
644         
645                         // Wait for termination
646                         pthread_join( *pthread, NULL );
647                 }
648         }
649
650         return 0;
651 }
652
653 /** Determine if the consumer is stopped.
654  */
655
656 static int is_stopped( mlt_consumer consumer )
657 {
658         // Get the properties
659         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
660         return !mlt_properties_get_int( properties, "running" );
661 }
662
663 /** Close the consumer.
664  */
665
666 static void close( mlt_consumer consumer )
667 {
668         // Stop the consumer
669         mlt_consumer_stop( consumer );
670
671         // Close the parent
672         consumer->close = NULL;
673         mlt_consumer_close( consumer );
674
675         // Free the memory
676         delete (DeckLinkConsumer*) consumer->child;
677 }
678
679 extern "C" {
680
681 /** Initialise the consumer.
682  */
683
684 mlt_consumer consumer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
685 {
686         // Allocate the consumer
687         DeckLinkConsumer* decklink = new DeckLinkConsumer();
688         mlt_consumer consumer = NULL;
689
690         // If allocated
691         if ( decklink && !mlt_consumer_init( decklink->getConsumer(), decklink, profile ) )
692         {
693                 // If initialises without error
694                 if ( decklink->open( arg? atoi(arg) : 0 ) )
695                 {
696                         consumer = decklink->getConsumer();
697                         
698                         // Setup callbacks
699                         consumer->close = close;
700                         consumer->start = start;
701                         consumer->stop = stop;
702                         consumer->is_stopped = is_stopped;
703                 }
704         }
705
706         // Return consumer
707         return consumer;
708 }
709
710 extern mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
711
712 static mlt_properties metadata( mlt_service_type type, const char *id, void *data )
713 {
714         char file[ PATH_MAX ];
715         const char *service_type = NULL;
716         switch ( type )
717         {
718                 case consumer_type:
719                         service_type = "consumer";
720                         break;
721                 case producer_type:
722                         service_type = "producer";
723                         break;
724                 default:
725                         return NULL;
726         }
727         snprintf( file, PATH_MAX, "%s/decklink/%s_%s.yml", mlt_environment( "MLT_DATA" ), service_type, id );
728         return mlt_properties_parse_yaml( file );
729 }
730
731 MLT_REPOSITORY
732 {
733         MLT_REGISTER( consumer_type, "decklink", consumer_decklink_init );
734         MLT_REGISTER( producer_type, "decklink", producer_decklink_init );
735         MLT_REGISTER_METADATA( consumer_type, "decklink", metadata, NULL );
736         MLT_REGISTER_METADATA( producer_type, "decklink", metadata, NULL );
737 }
738
739 } // extern C