]> git.sesse.net Git - mlt/blob - src/modules/decklink/consumer_decklink.cpp
Fix decklink consumer on devices without keyer.
[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                 IDeckLinkAttributes *deckLinkAttributes = 0;
189                 m_deckLinkKeyer = 0;
190                 if ( m_deckLink->QueryInterface( IID_IDeckLinkAttributes, (void**) &deckLinkAttributes ) == S_OK )
191                 {
192                         bool flag = false;
193                         if ( deckLinkAttributes->GetFlag( BMDDeckLinkSupportsInternalKeying, &flag ) == S_OK && flag )
194                         {
195                                 if ( m_deckLink->QueryInterface( IID_IDeckLinkKeyer, (void**) &m_deckLinkKeyer ) != S_OK )
196                                 {
197                                         mlt_log_error( getConsumer(), "Failed to get keyer\n" );
198                                         m_deckLinkOutput->Release();
199                                         m_deckLinkOutput = 0;
200                                         m_deckLink->Release();
201                                         m_deckLink = 0;
202                                         return false;
203                                 }
204                         }
205                         deckLinkAttributes->Release();
206                 }
207
208                 // Provide this class as a delegate to the audio and video output interfaces
209                 m_deckLinkOutput->SetScheduledFrameCompletionCallback( this );
210                 m_deckLinkOutput->SetAudioCallback( this );
211                 
212                 pthread_mutex_init( &m_mutex, NULL );
213                 pthread_cond_init( &m_condition, NULL );
214                 m_maxAudioBuffer = bmdAudioSampleRate48kHz;
215                 m_videoFrameQ = mlt_deque_init();
216                 
217                 return true;
218         }
219         
220         bool start( unsigned preroll )
221         {
222                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( getConsumer() );
223
224                 // Initialize members
225                 m_count = 0;
226                 m_frame = 0;
227                 m_dropped = 0;
228                 m_isPrerolling = true;
229                 m_prerollCounter = 0;
230                 m_preroll = preroll < PREROLL_MINIMUM ? PREROLL_MINIMUM : preroll;
231                 m_channels = mlt_properties_get_int( properties, "channels" );
232                 m_isAudio = !mlt_properties_get_int( properties, "audio_off" );
233
234                 m_displayMode = getDisplayMode();
235                 if ( !m_displayMode )
236                 {
237                         mlt_log_error( getConsumer(), "Profile is not compatible with decklink.\n" );
238                         return false;
239                 }
240                 
241                 // Set the keyer
242                 if ( m_deckLinkKeyer && ( m_isKeyer = mlt_properties_get_int( properties, "keyer" ) ) )
243                 {
244                         bool external = false;
245                         double level = mlt_properties_get_double( properties, "keyer_level" );
246
247                         if ( m_deckLinkKeyer->Enable( external ) != S_OK )
248                                 mlt_log_error( getConsumer(), "Failed to enable keyer\n" );
249                         m_deckLinkKeyer->SetLevel( level <= 1 ? ( level > 0 ? 255 * level : 255 ) : 255 );
250                         m_preroll = 0;
251                         m_isAudio = false;
252                 }
253                 else if ( m_deckLinkKeyer )
254                 {
255                         m_deckLinkKeyer->Disable();
256                 }
257
258                 // Set the video output mode
259                 if ( S_OK != m_deckLinkOutput->EnableVideoOutput( m_displayMode->GetDisplayMode(), bmdVideoOutputFlagDefault) )
260                 {
261                         mlt_log_error( getConsumer(), "Failed to enable video output\n" );
262                         return false;
263                 }
264
265                 // Set the audio output mode
266                 if ( !m_isAudio )
267                 {
268                         m_deckLinkOutput->DisableAudioOutput();
269                         return true;
270                 }
271                 if ( S_OK != m_deckLinkOutput->EnableAudioOutput( bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
272                         m_channels, bmdAudioOutputStreamContinuous ) )
273                 {
274                         mlt_log_error( getConsumer(), "Failed to enable audio output\n" );
275                         stop();
276                         return false;
277                 }
278                 m_fifo = sample_fifo_init();
279                 m_deckLinkOutput->BeginAudioPreroll();
280                 
281                 return true;
282         }
283         
284         void wakeup()
285         {
286                 pthread_mutex_lock( &m_mutex );
287                 pthread_cond_broadcast( &m_condition );
288                 pthread_mutex_unlock( &m_mutex );
289         }
290         
291         void wait()
292         {
293                 struct timeval tv;
294                 struct timespec ts;
295                 
296                 gettimeofday( &tv, NULL );
297                 ts.tv_sec = tv.tv_sec + 1;
298                 ts.tv_nsec = tv.tv_usec * 1000;
299                 pthread_mutex_lock( &m_mutex );
300                 pthread_cond_timedwait( &m_condition, &m_mutex, &ts );
301                 pthread_mutex_unlock( &m_mutex );
302         }
303         
304         void stop()
305         {
306                 // Stop the audio and video output streams immediately
307                 if ( m_deckLinkOutput )
308                 {
309                         m_deckLinkOutput->StopScheduledPlayback( 0, 0, 0 );
310                         m_deckLinkOutput->DisableAudioOutput();
311                         m_deckLinkOutput->DisableVideoOutput();
312                 }
313                 while ( mlt_deque_count( m_videoFrameQ ) )
314                 {
315                         IDeckLinkMutableVideoFrame* frame = (IDeckLinkMutableVideoFrame*) mlt_deque_pop_back( m_videoFrameQ );
316                         frame->Release();
317                 }
318                 if ( m_fifo ) sample_fifo_close( m_fifo );
319                 mlt_frame_close( m_frame );
320         }
321
322         void renderAudio( mlt_frame frame )
323         {
324                 mlt_audio_format format = mlt_audio_s16;
325                 int frequency = bmdAudioSampleRate48kHz;
326                 int samples = mlt_sample_calculator( m_fps, frequency, m_count );
327                 int16_t *pcm = 0;
328
329                 if ( !mlt_frame_get_audio( frame, (void**) &pcm, &format, &frequency, &m_channels, &samples ) )
330                 {
331                         int count = samples;
332
333                         if ( !m_isPrerolling )
334                         {
335                                 uint32_t audioCount = 0;
336                                 uint32_t videoCount = 0;
337
338                                 // Check for resync
339                                 m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &audioCount );
340                                 m_deckLinkOutput->GetBufferedVideoFrameCount( &videoCount );
341
342                                 // Underflow typically occurs during non-normal speed playback.
343                                 if ( audioCount < 1 || videoCount < 1 )
344                                 {
345                                         // Upon switching to normal playback, buffer some frames faster than realtime.
346                                         mlt_log_info( getConsumer(), "buffer underrun: audio buf %u video buf %u frames\n", audioCount, videoCount );
347                                         m_prerollCounter = 0;
348                                 }
349
350                                 // While rebuffering
351                                 if ( isBuffering() )
352                                 {
353                                         // Only append audio to reach the ideal level and not overbuffer.
354                                         int ideal = ( m_preroll - 1 ) * bmdAudioSampleRate48kHz / m_fps;
355                                         int actual = m_fifo->used / m_channels + audioCount;
356                                         int diff = ideal / 2 - actual;
357                                         count = diff < 0 ? 0 : diff < count ? diff : count;
358                                 }
359                         }
360                         if ( count > 0 )
361                                 sample_fifo_append( m_fifo, pcm, count * m_channels );
362                 }
363         }
364
365         bool createFrame()
366         {
367                 BMDPixelFormat format = m_isKeyer? bmdFormat8BitARGB : bmdFormat8BitYUV;
368                 IDeckLinkMutableVideoFrame* frame = 0;
369                 uint8_t *buffer = 0;
370                 int stride = m_width * ( m_isKeyer? 4 : 2 );
371
372                 // Generate a DeckLink video frame
373                 if ( S_OK != m_deckLinkOutput->CreateVideoFrame( m_width, m_height,
374                         stride, format, bmdFrameFlagDefault, &frame ) )
375                 {
376                         mlt_log_verbose( getConsumer(), "Failed to create video frame\n" );
377                         stop();
378                         return false;
379                 }
380                 
381                 // Make the first line black for field order correction.
382                 if ( S_OK == frame->GetBytes( (void**) &buffer ) && buffer )
383                 {
384                         if ( m_isKeyer )
385                         {
386                                 memset( buffer, 0, stride );
387                         }
388                         else for ( int i = 0; i < m_width; i++ )
389                         {
390                                 *buffer++ = 128;
391                                 *buffer++ = 16;
392                         }
393                 }
394                 mlt_log_debug( getConsumer(), "created video frame\n" );
395                 mlt_deque_push_back( m_videoFrameQ, frame );
396
397                 return true;
398         }
399
400         void renderVideo()
401         {
402                 mlt_image_format format = m_isKeyer? mlt_image_rgb24a : mlt_image_yuv422;
403                 uint8_t* image = 0;
404
405                 if ( !mlt_frame_get_image( m_frame, &image, &format, &m_width, &m_height, 0 ) )
406                 {
407                         IDeckLinkMutableVideoFrame* decklinkFrame = (IDeckLinkMutableVideoFrame*) mlt_deque_pop_back( m_videoFrameQ );
408                         uint8_t* buffer = 0;
409                         int stride = m_width * ( m_isKeyer? 4 : 2 );
410
411                         decklinkFrame->GetBytes( (void**) &buffer );
412                         if ( buffer )
413                         {
414                                 int progressive = mlt_properties_get_int( MLT_FRAME_PROPERTIES( m_frame ), "progressive" );
415
416                                 if ( !m_isKeyer )
417                                 {
418                                         // Normal non-keyer playout - needs byte swapping
419                                         if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
420                                                 // convert lower field first to top field first
421                                                 swab( image, buffer + stride, stride * ( m_height - 1 ) );
422                                         else
423                                                 swab( image, buffer, stride * m_height );
424                                 }
425                                 else if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES( m_frame ), "test_image" ) )
426                                 {
427                                         // Normal keyer output
428                                         int y = m_height + 1;
429                                         uint32_t* s = (uint32_t*) image;
430                                         uint32_t* d = (uint32_t*) buffer;
431
432                                         if ( !progressive && m_displayMode->GetFieldDominance() == bmdUpperFieldFirst )
433                                         {
434                                                 // Correct field order
435                                                 m_height--;
436                                                 d += stride;
437                                         }
438
439                                         // Need to relocate alpha channel RGBA => ARGB
440                                         while ( --y )
441                                         {
442                                                 int x = m_width + 1;
443                                                 while ( --x )
444                                                 {
445                                                         *d++ = ( *s << 8 ) | ( *s >> 24 );
446                                                         s++;
447                                                 }
448                                         }
449                                 }
450                                 else
451                                 {
452                                         // Keying blank frames - nullify alpha
453                                         memset( buffer, 0, stride * m_height );
454                                 }
455                                 m_deckLinkOutput->ScheduleVideoFrame( decklinkFrame, m_count * m_duration, m_duration, m_timescale );
456                         }
457                         mlt_deque_push_front( m_videoFrameQ, decklinkFrame );
458                 }
459         }
460
461         HRESULT render( mlt_frame frame )
462         {
463                 HRESULT result = S_OK;
464
465                 // Get the audio
466                 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES(frame), "_speed" );
467                 if ( m_isAudio && speed == 1.0 )
468                         renderAudio( frame );
469                 
470                 // Create video frames while pre-rolling
471                 if ( m_isPrerolling )
472                 {
473                         if ( !createFrame() )
474                         {
475                                 mlt_log_error( getConsumer(), "failed to create video frame\n" );
476                                 return S_FALSE;
477                         }
478                 }
479                 
480                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered") )
481                 {
482                         // Close the previous frame and use the new one
483                         mlt_frame_close( m_frame );
484                         m_frame = frame;
485                 }
486                 else
487                 {
488                         if ( !m_frame )
489                                 m_frame = frame;
490                         // Reuse the last frame
491                         mlt_log_verbose( getConsumer(), "dropped video frame %u\n", ++m_dropped );
492                 }
493
494                 // Get the video
495                 renderVideo();
496                 ++m_count;
497
498                 // Check for end of pre-roll
499                 if ( ++m_prerollCounter > m_preroll && m_isPrerolling )
500                 {
501                         // Start audio and video output
502                         if ( m_isAudio )
503                                 m_deckLinkOutput->EndAudioPreroll();
504                         m_deckLinkOutput->StartScheduledPlayback( 0, m_timescale, 1.0 );
505                         m_isPrerolling = false;
506                 }
507
508                 return result;
509         }
510         
511         // *** DeckLink API implementation of IDeckLinkVideoOutputCallback IDeckLinkAudioOutputCallback *** //
512
513         // IUnknown needs only a dummy implementation
514         virtual HRESULT STDMETHODCALLTYPE QueryInterface( REFIID iid, LPVOID *ppv )
515                 { return E_NOINTERFACE; }
516         virtual ULONG STDMETHODCALLTYPE AddRef()
517                 { return 1; }
518         virtual ULONG STDMETHODCALLTYPE Release()
519                 { return 1; }
520         
521         /************************* DeckLink API Delegate Methods *****************************/
522         
523         virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted( IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult completed )
524         {
525                 // When a video frame has been released by the API, schedule another video frame to be output
526                 wakeup();
527                 
528                 return S_OK;
529         }
530
531         virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped()
532         {
533                 return mlt_consumer_is_stopped( getConsumer() ) ? S_FALSE : S_OK;
534         }
535         
536         virtual HRESULT STDMETHODCALLTYPE RenderAudioSamples( bool preroll )
537         {
538                 // Provide more audio samples to the DeckLink API
539                 HRESULT result = S_OK;
540
541                 uint32_t count = m_fifo->used / m_channels;
542                 uint32_t buffered = count;
543
544                 if ( count
545                         // Stay under preferred buffer level
546                         && ( S_OK == m_deckLinkOutput->GetBufferedAudioSampleFrameCount( &buffered ) )
547                         && buffered < m_maxAudioBuffer )
548                 {
549                         uint32_t written = 0;
550                         
551                         buffered = m_maxAudioBuffer - buffered;
552                         count = buffered > count ? count : buffered;
553                         result = m_deckLinkOutput->ScheduleAudioSamples( m_fifo->buffer, count, 0, 0, &written );
554                         if ( written )
555                                 sample_fifo_remove( m_fifo, written * m_channels );
556                 }
557
558                 return result;
559         }
560 };
561
562 /** The main thread.
563  */
564
565 static void *run( void *arg )
566 {
567         // Map the argument to the object
568         DeckLinkConsumer* decklink = (DeckLinkConsumer*) arg;
569         mlt_consumer consumer = decklink->getConsumer();
570         
571         // Get the properties
572         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
573
574         // Convenience functionality
575         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
576         int terminated = 0;
577
578         // Frame and size
579         mlt_frame frame = NULL;
580         
581         // Loop while running
582         while ( !terminated && mlt_properties_get_int( properties, "running" ) )
583         {
584                 // Get the frame
585                 if ( ( frame = mlt_consumer_rt_frame( consumer ) ) )
586                 {
587                         // Check for termination
588                         if ( terminate_on_pause )
589                                 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
590
591                         decklink->render( frame );
592                         if ( !decklink->isBuffering() )
593                                 decklink->wait();
594                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
595                 }
596         }
597
598         // Indicate that the consumer is stopped
599         decklink->stop();
600         mlt_properties_set_int( properties, "running", 0 );
601         mlt_consumer_stopped( consumer );
602
603         return NULL;
604 }
605
606 /** Start the consumer.
607  */
608
609 static int start( mlt_consumer consumer )
610 {
611         // Get the properties
612         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
613         DeckLinkConsumer* decklink = (DeckLinkConsumer*) consumer->child;
614         int result = decklink->start( mlt_properties_get_int( properties, "preroll" ) ) ? 0 : 1;
615
616         // Check that we're not already running
617         if ( !result && !mlt_properties_get_int( properties, "running" ) )
618         {
619                 // Allocate a thread
620                 pthread_t *pthread = (pthread_t*) calloc( 1, sizeof( pthread_t ) );
621
622                 // Assign the thread to properties
623                 mlt_properties_set_data( properties, "pthread", pthread, sizeof( pthread_t ), free, NULL );
624
625                 // Set the running state
626                 mlt_properties_set_int( properties, "running", 1 );
627                 mlt_properties_set_int( properties, "joined", 0 );
628
629                 // Create the thread
630                 pthread_create( pthread, NULL, run, consumer->child );
631         }
632         return result;
633 }
634
635 /** Stop the consumer.
636  */
637
638 static int stop( mlt_consumer consumer )
639 {
640         // Get the properties
641         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
642
643         // Check that we're running
644         if ( !mlt_properties_get_int( properties, "joined" ) )
645         {
646                 // Get the thread
647                 pthread_t *pthread = (pthread_t*) mlt_properties_get_data( properties, "pthread", NULL );
648                 
649                 if ( pthread )
650                 {
651                         // Stop the thread
652                         mlt_properties_set_int( properties, "running", 0 );
653                         mlt_properties_set_int( properties, "joined", 1 );
654         
655                         // Wait for termination
656                         pthread_join( *pthread, NULL );
657                 }
658         }
659
660         return 0;
661 }
662
663 /** Determine if the consumer is stopped.
664  */
665
666 static int is_stopped( mlt_consumer consumer )
667 {
668         // Get the properties
669         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
670         return !mlt_properties_get_int( properties, "running" );
671 }
672
673 /** Close the consumer.
674  */
675
676 static void close( mlt_consumer consumer )
677 {
678         // Stop the consumer
679         mlt_consumer_stop( consumer );
680
681         // Close the parent
682         consumer->close = NULL;
683         mlt_consumer_close( consumer );
684
685         // Free the memory
686         delete (DeckLinkConsumer*) consumer->child;
687 }
688
689 extern "C" {
690
691 /** Initialise the consumer.
692  */
693
694 mlt_consumer consumer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
695 {
696         // Allocate the consumer
697         DeckLinkConsumer* decklink = new DeckLinkConsumer();
698         mlt_consumer consumer = NULL;
699
700         // If allocated
701         if ( decklink && !mlt_consumer_init( decklink->getConsumer(), decklink, profile ) )
702         {
703                 // If initialises without error
704                 if ( decklink->open( arg? atoi(arg) : 0 ) )
705                 {
706                         consumer = decklink->getConsumer();
707                         
708                         // Setup callbacks
709                         consumer->close = close;
710                         consumer->start = start;
711                         consumer->stop = stop;
712                         consumer->is_stopped = is_stopped;
713                 }
714         }
715
716         // Return consumer
717         return consumer;
718 }
719
720 extern mlt_producer producer_decklink_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
721
722 static mlt_properties metadata( mlt_service_type type, const char *id, void *data )
723 {
724         char file[ PATH_MAX ];
725         const char *service_type = NULL;
726         switch ( type )
727         {
728                 case consumer_type:
729                         service_type = "consumer";
730                         break;
731                 case producer_type:
732                         service_type = "producer";
733                         break;
734                 default:
735                         return NULL;
736         }
737         snprintf( file, PATH_MAX, "%s/decklink/%s_%s.yml", mlt_environment( "MLT_DATA" ), service_type, id );
738         return mlt_properties_parse_yaml( file );
739 }
740
741 MLT_REPOSITORY
742 {
743         MLT_REGISTER( consumer_type, "decklink", consumer_decklink_init );
744         MLT_REGISTER( producer_type, "decklink", producer_decklink_init );
745         MLT_REGISTER_METADATA( consumer_type, "decklink", metadata, NULL );
746         MLT_REGISTER_METADATA( producer_type, "decklink", metadata, NULL );
747 }
748
749 } // extern C