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 void on_frame_show( void *dummy, mlt_properties properties, mlt_frame frame )
142 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
145 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
147 mlt_profile profile = NULL;
148 if ( mlt_properties_get( props, "mlt_profile" ) )
149 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
151 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
152 mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ),
153 mlt_properties_get( props, "target" ) );
157 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
158 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
161 snprintf( key, sizeof(key), "%d.consumer", index );
162 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
163 snprintf( key, sizeof(key), "%d.profile", index );
164 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
166 mlt_properties_set_int( nested_props, "put_mode", 1 );
167 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
168 mlt_properties_set( props, "consumer", NULL );
169 // set mlt_profile before other properties to facilitate presets
170 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
171 mlt_properties_inherit( nested_props, props );
173 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
175 // Relay the first available consumer-frame-show event
176 mlt_event event = mlt_properties_get_data( properties, "frame-show-event", NULL );
179 event = mlt_events_listen( nested_props, properties, "consumer-frame-show", (mlt_listener) on_frame_show );
180 mlt_properties_set_data( properties, "frame-show-event", event, 0, /*mlt_event_close*/ NULL, NULL );
185 mlt_profile_close( profile );
190 static void foreach_consumer_init( mlt_consumer consumer )
192 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
193 mlt_properties properties = mlt_properties_parse_yaml( resource );
197 if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
199 // Properties set directly by application
203 mlt_properties_close( properties );
204 properties = MLT_CONSUMER_PROPERTIES(consumer);
206 snprintf( key, sizeof(key), "%d", index );
207 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
208 generate_consumer( consumer, p, index++ );
211 else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
213 // YAML file supplied
217 snprintf( key, sizeof(key), "%d", index );
218 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
219 generate_consumer( consumer, p, index++ );
221 mlt_properties_close( properties );
225 // properties file supplied or properties on this consumer
229 mlt_properties_close( properties );
231 properties = mlt_properties_load( resource );
233 properties = MLT_CONSUMER_PROPERTIES( consumer );
236 snprintf( key, sizeof(key), "%d", index );
237 if ( ( s = mlt_properties_get( properties, key ) ) )
239 mlt_properties p = mlt_properties_new();
243 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
244 snprintf( key, sizeof(key), "%d.", index );
246 count = mlt_properties_count( properties );
247 for ( i = 0; i < count; i++ )
249 char *name = mlt_properties_get_name( properties, i );
250 if ( !strncmp( name, key, strlen(key) ) )
251 mlt_properties_set( p, name + strlen(key),
252 mlt_properties_get_value( properties, i ) );
254 generate_consumer( consumer, p, index++ );
255 mlt_properties_close( p );
259 mlt_properties_close( properties );
263 static void foreach_consumer_start( mlt_consumer consumer )
265 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
266 mlt_consumer nested = NULL;
271 snprintf( key, sizeof(key), "%d.consumer", index++ );
272 nested = mlt_properties_get_data( properties, key, NULL );
275 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
276 mlt_properties_set_position( nested_props, "_multi_position", 0 );
277 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
278 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
279 mlt_consumer_start( nested );
284 static void foreach_consumer_refresh( mlt_consumer consumer )
286 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
287 mlt_consumer nested = NULL;
292 snprintf( key, sizeof(key), "%d.consumer", index++ );
293 nested = mlt_properties_get_data( properties, key, NULL );
294 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
298 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
300 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
301 mlt_consumer nested = NULL;
306 snprintf( key, sizeof(key), "%d.consumer", index++ );
307 nested = mlt_properties_get_data( properties, key, NULL );
310 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
311 double self_fps = mlt_properties_get_double( properties, "fps" );
312 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
313 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
314 mlt_position self_pos = mlt_frame_get_position( frame );
315 double self_time = self_pos / self_fps;
316 double nested_time = nested_pos / nested_fps;
318 // get the audio for the current frame
319 uint8_t *buffer = NULL;
320 mlt_audio_format format = mlt_audio_s16;
321 int channels = mlt_properties_get_int( properties, "channels" );
322 int frequency = mlt_properties_get_int( properties, "frequency" );
323 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
324 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
325 int current_size = mlt_audio_format_size( format, current_samples, channels );
327 // get any leftover audio
329 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
330 uint8_t *new_buffer = NULL;
333 new_buffer = mlt_pool_alloc( prev_size + current_size );
334 memcpy( new_buffer, prev_buffer, prev_size );
335 memcpy( new_buffer + prev_size, buffer, current_size );
338 current_size += prev_size;
339 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
341 while ( nested_time <= self_time )
343 // put ideal number of samples into cloned frame
344 int deeply = index > 1 ? 1 : 0;
345 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
346 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
347 // -10 is an optimization to avoid tiny amounts of leftover samples
348 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
349 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
350 if ( nested_size > 0 )
352 prev_buffer = mlt_pool_alloc( nested_size );
353 memcpy( prev_buffer, buffer, nested_size );
360 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
361 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
362 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
363 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
366 current_samples -= nested_samples;
367 current_size -= nested_size;
368 buffer += nested_size;
370 // send frame to nested consumer
371 mlt_consumer_put_frame( nested, clone_frame );
372 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
373 nested_time = nested_pos / nested_fps;
376 // save any remaining audio
377 if ( current_size > 0 )
379 prev_buffer = mlt_pool_alloc( current_size );
380 memcpy( prev_buffer, buffer, current_size );
387 mlt_pool_release( new_buffer );
388 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
389 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
394 static void foreach_consumer_stop( mlt_consumer consumer )
396 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
397 mlt_consumer nested = NULL;
400 struct timespec tm = { 0, 1000 * 1000 };
403 snprintf( key, sizeof(key), "%d.consumer", index++ );
404 nested = mlt_properties_get_data( properties, key, NULL );
407 // Let consumer with terminate_on_pause stop on their own
408 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
410 // Send additional dummy frame to unlatch nested consumer's threads
411 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
413 while ( !mlt_consumer_is_stopped( nested ) )
414 nanosleep( &tm, NULL );
418 mlt_consumer_stop( nested );
424 /** Start the consumer.
427 static int start( mlt_consumer consumer )
429 // Check that we're not already running
430 if ( is_stopped( consumer ) )
432 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
433 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
435 // Assign the thread to properties with automatic dealloc
436 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
438 // Set the running state
439 mlt_properties_set_int( properties, "running", 1 );
440 mlt_properties_set_int( properties, "joined", 0 );
442 // Construct and start nested consumers
443 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
444 foreach_consumer_init( consumer );
445 foreach_consumer_start( consumer );
448 pthread_create( thread, NULL, consumer_thread, consumer );
453 /** Stop the consumer.
456 static int stop( mlt_consumer consumer )
458 // Check that we're running
459 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
461 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
462 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
465 mlt_properties_set_int( properties, "running", 0 );
467 // Wait for termination
470 foreach_consumer_refresh( consumer );
471 pthread_join( *thread, NULL );
473 mlt_properties_set_int( properties, "joined", 1 );
475 // Stop nested consumers
476 foreach_consumer_stop( consumer );
482 /** Determine if the consumer is stopped.
485 static int is_stopped( mlt_consumer consumer )
487 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
490 /** The main thread - the argument is simply the consumer.
493 static void *consumer_thread( void *arg )
495 mlt_consumer consumer = arg;
496 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
497 mlt_frame frame = NULL;
499 // Determine whether to stop at end-of-media
500 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
503 // Loop while running
504 while ( !terminated && !is_stopped( consumer ) )
506 // Get the next frame
507 frame = mlt_consumer_rt_frame( consumer );
509 // Check for termination
510 if ( terminate_on_pause && frame )
511 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
513 // Check that we have a frame to work with
514 if ( frame && !terminated && !is_stopped( consumer ) )
516 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
518 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
519 foreach_consumer_refresh( consumer );
520 foreach_consumer_put( consumer, frame );
524 int dropped = mlt_properties_get_int( properties, "_dropped" );
525 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
526 mlt_properties_set_int( properties, "_dropped", dropped );
528 mlt_frame_close( frame );
532 if ( frame && terminated )
534 // Send this termination frame to nested consumers for their cancellation
535 foreach_consumer_put( consumer, frame );
538 mlt_frame_close( frame );
543 // Indicate that the consumer is stopped
544 mlt_consumer_stopped( consumer );
549 /** Close the consumer.
552 static void consumer_close( mlt_consumer consumer )
554 mlt_consumer_stop( consumer );
557 mlt_consumer_close( consumer );