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