2 * Copyright (C) 2011 Ushodaya Enterprises Limited
3 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <framework/mlt.h>
28 static int start( mlt_consumer consumer );
29 static int stop( mlt_consumer consumer );
30 static int is_stopped( mlt_consumer consumer );
31 static void *consumer_thread( void *arg );
32 static void consumer_close( mlt_consumer consumer );
34 static mlt_properties normalisers = NULL;
36 /** Initialise the consumer.
39 mlt_consumer consumer_multi_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
41 mlt_consumer consumer = mlt_consumer_new( profile );
45 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
48 mlt_properties_set( properties, "resource", arg );
49 mlt_properties_set_int( properties, "real_time", -1 );
50 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
53 mlt_properties_set_int( properties, "joined", 1 );
56 consumer->close = consumer_close;
57 consumer->start = start;
58 consumer->stop = stop;
59 consumer->is_stopped = is_stopped;
65 static mlt_consumer create_consumer( mlt_profile profile, char *id, char *arg )
67 char *myid = id ? strdup( id ) : NULL;
68 char *myarg = ( myid && !arg ) ? strchr( myid, ':' ) : NULL;
73 mlt_consumer consumer = mlt_factory_consumer( profile, myid, myarg );
79 static void create_filter( mlt_profile profile, mlt_service service, char *effect, int *created )
81 char *id = strdup( effect );
82 char *arg = strchr( id, ':' );
86 // The swscale and avcolor_space filters require resolution as arg to test compatibility
87 if ( strncmp( effect, "swscale", 7 ) == 0 || strncmp( effect, "avcolo", 6 ) == 0 )
88 arg = (char*) mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "meta.media.width" );
90 mlt_filter filter = mlt_factory_filter( profile, id, arg );
93 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
94 mlt_service_attach( service, filter );
95 mlt_filter_close( filter );
101 static void attach_normalisers( mlt_profile profile, mlt_service service )
107 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
109 // We only need to load the normalising properties once
110 if ( normalisers == NULL )
113 snprintf( temp, sizeof(temp), "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
114 normalisers = mlt_properties_load( temp );
115 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
119 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
123 char *value = mlt_properties_get_value( normalisers, i );
124 mlt_tokeniser_parse_new( tokeniser, value, "," );
125 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
126 create_filter( profile, service, mlt_tokeniser_get_string( tokeniser, j ), &created );
129 // Close the tokeniser
130 mlt_tokeniser_close( tokeniser );
132 // Attach the audio and video format converters
134 create_filter( profile, service, "avcolor_space", &created );
136 create_filter( profile, service, "imageconvert", &created );
137 create_filter( profile, service, "audioconvert", &created );
140 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
142 mlt_profile profile = NULL;
143 if ( mlt_properties_get( props, "mlt_profile" ) )
144 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
146 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
147 mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ),
148 mlt_properties_get( props, "target" ) );
152 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
153 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
156 snprintf( key, sizeof(key), "%d.consumer", index );
157 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
158 snprintf( key, sizeof(key), "%d.profile", index );
159 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
161 mlt_properties_set_int( nested_props, "put_mode", 1 );
162 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
163 mlt_properties_set( props, "consumer", NULL );
164 // set mlt_profile before other properties to facilitate presets
165 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
166 mlt_properties_inherit( nested_props, props );
168 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
172 mlt_profile_close( profile );
177 static void foreach_consumer_init( mlt_consumer consumer )
179 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
180 mlt_properties properties = mlt_properties_parse_yaml( resource );
184 if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
186 // Properties set directly by application
190 mlt_properties_close( properties );
191 properties = MLT_CONSUMER_PROPERTIES(consumer);
193 snprintf( key, sizeof(key), "%d", index );
194 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
195 generate_consumer( consumer, p, index++ );
198 else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
200 // YAML file supplied
204 snprintf( key, sizeof(key), "%d", index );
205 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
206 generate_consumer( consumer, p, index++ );
208 mlt_properties_close( properties );
212 // properties file supplied or properties on this consumer
216 mlt_properties_close( properties );
218 properties = mlt_properties_load( resource );
220 properties = MLT_CONSUMER_PROPERTIES( consumer );
223 snprintf( key, sizeof(key), "%d", index );
224 if ( ( s = mlt_properties_get( properties, key ) ) )
226 mlt_properties p = mlt_properties_new();
230 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
231 snprintf( key, sizeof(key), "%d.", index );
233 count = mlt_properties_count( properties );
234 for ( i = 0; i < count; i++ )
236 char *name = mlt_properties_get_name( properties, i );
237 if ( !strncmp( name, key, strlen(key) ) )
238 mlt_properties_set( p, name + strlen(key),
239 mlt_properties_get_value( properties, i ) );
241 generate_consumer( consumer, p, index++ );
242 mlt_properties_close( p );
246 mlt_properties_close( properties );
250 static void foreach_consumer_start( mlt_consumer consumer )
252 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
253 mlt_consumer nested = NULL;
258 snprintf( key, sizeof(key), "%d.consumer", index++ );
259 nested = mlt_properties_get_data( properties, key, NULL );
262 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
263 mlt_properties_set_position( nested_props, "_multi_position", 0 );
264 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
265 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
266 mlt_consumer_start( nested );
271 static void foreach_consumer_refresh( mlt_consumer consumer )
273 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
274 mlt_consumer nested = NULL;
279 snprintf( key, sizeof(key), "%d.consumer", index++ );
280 nested = mlt_properties_get_data( properties, key, NULL );
281 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
285 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
287 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
288 mlt_consumer nested = NULL;
293 snprintf( key, sizeof(key), "%d.consumer", index++ );
294 nested = mlt_properties_get_data( properties, key, NULL );
297 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
298 double self_fps = mlt_properties_get_double( properties, "fps" );
299 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
300 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
301 mlt_position self_pos = mlt_frame_get_position( frame );
302 double self_time = self_pos / self_fps;
303 double nested_time = nested_pos / nested_fps;
305 // get the audio for the current frame
306 uint8_t *buffer = NULL;
307 mlt_audio_format format = mlt_audio_s16;
308 int channels = mlt_properties_get_int( properties, "channels" );
309 int frequency = mlt_properties_get_int( properties, "frequency" );
310 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
311 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
312 int current_size = mlt_audio_format_size( format, current_samples, channels );
314 // get any leftover audio
316 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
317 uint8_t *new_buffer = NULL;
320 new_buffer = mlt_pool_alloc( prev_size + current_size );
321 memcpy( new_buffer, prev_buffer, prev_size );
322 memcpy( new_buffer + prev_size, buffer, current_size );
325 current_size += prev_size;
326 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
328 while ( nested_time <= self_time )
330 // put ideal number of samples into cloned frame
331 mlt_frame clone_frame = mlt_frame_clone( frame, 0 );
332 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
333 // -10 is an optimization to avoid tiny amounts of leftover samples
334 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
335 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
336 if ( nested_size > 0 )
338 prev_buffer = mlt_pool_alloc( nested_size );
339 memcpy( prev_buffer, buffer, nested_size );
346 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
347 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
348 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
349 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
352 current_samples -= nested_samples;
353 current_size -= nested_size;
354 buffer += nested_size;
356 // send frame to nested consumer
357 mlt_consumer_put_frame( nested, clone_frame );
358 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
359 nested_time = nested_pos / nested_fps;
362 // save any remaining audio
363 if ( current_size > 0 )
365 prev_buffer = mlt_pool_alloc( current_size );
366 memcpy( prev_buffer, buffer, current_size );
373 mlt_pool_release( new_buffer );
374 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
375 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
380 static void foreach_consumer_stop( mlt_consumer consumer )
382 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
383 mlt_consumer nested = NULL;
386 struct timespec tm = { 0, 1000 * 1000 };
389 snprintf( key, sizeof(key), "%d.consumer", index++ );
390 nested = mlt_properties_get_data( properties, key, NULL );
393 // Let consumer with terminate_on_pause stop on their own
394 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
396 // Send additional dummy frame to unlatch nested consumer's threads
397 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
399 while ( !mlt_consumer_is_stopped( nested ) )
400 nanosleep( &tm, NULL );
404 mlt_consumer_stop( nested );
410 /** Start the consumer.
413 static int start( mlt_consumer consumer )
415 // Check that we're not already running
416 if ( is_stopped( consumer ) )
418 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
419 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
421 // Assign the thread to properties with automatic dealloc
422 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
424 // Set the running state
425 mlt_properties_set_int( properties, "running", 1 );
426 mlt_properties_set_int( properties, "joined", 0 );
428 // Construct and start nested consumers
429 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
430 foreach_consumer_init( consumer );
431 foreach_consumer_start( consumer );
434 pthread_create( thread, NULL, consumer_thread, consumer );
439 /** Stop the consumer.
442 static int stop( mlt_consumer consumer )
444 // Check that we're running
445 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
447 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
448 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
451 mlt_properties_set_int( properties, "running", 0 );
453 // Wait for termination
456 foreach_consumer_refresh( consumer );
457 pthread_join( *thread, NULL );
459 mlt_properties_set_int( properties, "joined", 1 );
461 // Stop nested consumers
462 foreach_consumer_stop( consumer );
468 /** Determine if the consumer is stopped.
471 static int is_stopped( mlt_consumer consumer )
473 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
476 /** The main thread - the argument is simply the consumer.
479 static void *consumer_thread( void *arg )
481 mlt_consumer consumer = arg;
482 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
483 mlt_frame frame = NULL;
485 // Determine whether to stop at end-of-media
486 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
489 // Loop while running
490 while ( !terminated && !is_stopped( consumer ) )
492 // Get the next frame
493 frame = mlt_consumer_rt_frame( consumer );
495 // Check for termination
496 if ( terminate_on_pause && frame )
497 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
499 // Check that we have a frame to work with
500 if ( frame && !terminated && !is_stopped( consumer ) )
502 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
504 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
505 foreach_consumer_refresh( consumer );
506 foreach_consumer_put( consumer, frame );
507 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
511 int dropped = mlt_properties_get_int( properties, "_dropped" );
512 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
513 mlt_properties_set_int( properties, "_dropped", dropped );
515 mlt_frame_close( frame );
519 if ( frame && terminated )
521 // Send this termination frame to nested consumers for their cancellation
522 foreach_consumer_put( consumer, frame );
523 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
526 mlt_frame_close( frame );
531 // Indicate that the consumer is stopped
532 mlt_consumer_stopped( consumer );
537 /** Close the consumer.
540 static void consumer_close( mlt_consumer consumer )
542 mlt_consumer_stop( consumer );
545 mlt_consumer_close( consumer );