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 int deeply = index > 1 ? 1 : 0;
332 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
333 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
334 // -10 is an optimization to avoid tiny amounts of leftover samples
335 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
336 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
337 if ( nested_size > 0 )
339 prev_buffer = mlt_pool_alloc( nested_size );
340 memcpy( prev_buffer, buffer, nested_size );
347 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
348 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
349 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
350 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
353 current_samples -= nested_samples;
354 current_size -= nested_size;
355 buffer += nested_size;
357 // send frame to nested consumer
358 mlt_consumer_put_frame( nested, clone_frame );
359 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
360 nested_time = nested_pos / nested_fps;
363 // save any remaining audio
364 if ( current_size > 0 )
366 prev_buffer = mlt_pool_alloc( current_size );
367 memcpy( prev_buffer, buffer, current_size );
374 mlt_pool_release( new_buffer );
375 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
376 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
381 static void foreach_consumer_stop( mlt_consumer consumer )
383 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
384 mlt_consumer nested = NULL;
387 struct timespec tm = { 0, 1000 * 1000 };
390 snprintf( key, sizeof(key), "%d.consumer", index++ );
391 nested = mlt_properties_get_data( properties, key, NULL );
394 // Let consumer with terminate_on_pause stop on their own
395 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
397 // Send additional dummy frame to unlatch nested consumer's threads
398 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
400 while ( !mlt_consumer_is_stopped( nested ) )
401 nanosleep( &tm, NULL );
405 mlt_consumer_stop( nested );
411 /** Start the consumer.
414 static int start( mlt_consumer consumer )
416 // Check that we're not already running
417 if ( is_stopped( consumer ) )
419 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
420 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
422 // Assign the thread to properties with automatic dealloc
423 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
425 // Set the running state
426 mlt_properties_set_int( properties, "running", 1 );
427 mlt_properties_set_int( properties, "joined", 0 );
429 // Construct and start nested consumers
430 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
431 foreach_consumer_init( consumer );
432 foreach_consumer_start( consumer );
435 pthread_create( thread, NULL, consumer_thread, consumer );
440 /** Stop the consumer.
443 static int stop( mlt_consumer consumer )
445 // Check that we're running
446 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
448 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
449 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
452 mlt_properties_set_int( properties, "running", 0 );
454 // Wait for termination
457 foreach_consumer_refresh( consumer );
458 pthread_join( *thread, NULL );
460 mlt_properties_set_int( properties, "joined", 1 );
462 // Stop nested consumers
463 foreach_consumer_stop( consumer );
469 /** Determine if the consumer is stopped.
472 static int is_stopped( mlt_consumer consumer )
474 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
477 /** The main thread - the argument is simply the consumer.
480 static void *consumer_thread( void *arg )
482 mlt_consumer consumer = arg;
483 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
484 mlt_frame frame = NULL;
486 // Determine whether to stop at end-of-media
487 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
490 // Loop while running
491 while ( !terminated && !is_stopped( consumer ) )
493 // Get the next frame
494 frame = mlt_consumer_rt_frame( consumer );
496 // Check for termination
497 if ( terminate_on_pause && frame )
498 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
500 // Check that we have a frame to work with
501 if ( frame && !terminated && !is_stopped( consumer ) )
503 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
505 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
506 foreach_consumer_refresh( consumer );
507 foreach_consumer_put( consumer, frame );
508 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
512 int dropped = mlt_properties_get_int( properties, "_dropped" );
513 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
514 mlt_properties_set_int( properties, "_dropped", dropped );
516 mlt_frame_close( frame );
520 if ( frame && terminated )
522 // Send this termination frame to nested consumers for their cancellation
523 foreach_consumer_put( consumer, frame );
524 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
527 mlt_frame_close( frame );
532 // Indicate that the consumer is stopped
533 mlt_consumer_stopped( consumer );
538 /** Close the consumer.
541 static void consumer_close( mlt_consumer consumer )
543 mlt_consumer_stop( consumer );
546 mlt_consumer_close( consumer );