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 // We cannot use GLSL-based filters here.
91 if ( strncmp( effect, "movit.", 6 ) && strncmp( effect, "glsl.", 5 ) )
93 mlt_filter filter = mlt_factory_filter( profile, id, arg );
96 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
97 mlt_service_attach( service, filter );
98 mlt_filter_close( filter );
105 static void attach_normalisers( mlt_profile profile, mlt_service service )
111 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
113 // We only need to load the normalising properties once
114 if ( normalisers == NULL )
117 snprintf( temp, sizeof(temp), "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
118 normalisers = mlt_properties_load( temp );
119 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
123 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
127 char *value = mlt_properties_get_value( normalisers, i );
128 mlt_tokeniser_parse_new( tokeniser, value, "," );
129 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
130 create_filter( profile, service, mlt_tokeniser_get_string( tokeniser, j ), &created );
133 // Close the tokeniser
134 mlt_tokeniser_close( tokeniser );
136 // Attach the audio and video format converters
138 // movit.convert skips setting the frame->convert_image pointer if GLSL cannot be used.
139 mlt_filter filter = mlt_factory_filter( profile, "movit.convert", NULL );
140 if ( filter != NULL )
142 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
143 mlt_service_attach( service, filter );
144 mlt_filter_close( filter );
147 // avcolor_space and imageconvert only set frame->convert_image if it has not been set.
148 create_filter( profile, service, "avcolor_space", &created );
150 create_filter( profile, service, "imageconvert", &created );
151 create_filter( profile, service, "audioconvert", &created );
154 static void on_frame_show( void *dummy, mlt_properties properties, mlt_frame frame )
156 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
159 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
161 mlt_profile profile = NULL;
162 if ( mlt_properties_get( props, "mlt_profile" ) )
163 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
165 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
166 mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ),
167 mlt_properties_get( props, "target" ) );
171 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
172 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
175 snprintf( key, sizeof(key), "%d.consumer", index );
176 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
177 snprintf( key, sizeof(key), "%d.profile", index );
178 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
180 mlt_properties_set_int( nested_props, "put_mode", 1 );
181 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
182 mlt_properties_set( props, "consumer", NULL );
183 // set mlt_profile before other properties to facilitate presets
184 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
185 mlt_properties_inherit( nested_props, props );
187 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
189 // Relay the first available consumer-frame-show event
190 mlt_event event = mlt_properties_get_data( properties, "frame-show-event", NULL );
193 event = mlt_events_listen( nested_props, properties, "consumer-frame-show", (mlt_listener) on_frame_show );
194 mlt_properties_set_data( properties, "frame-show-event", event, 0, /*mlt_event_close*/ NULL, NULL );
199 mlt_profile_close( profile );
204 static void foreach_consumer_init( mlt_consumer consumer )
206 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
207 mlt_properties properties = mlt_properties_parse_yaml( resource );
211 if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
213 // Properties set directly by application
217 mlt_properties_close( properties );
218 properties = MLT_CONSUMER_PROPERTIES(consumer);
220 snprintf( key, sizeof(key), "%d", index );
221 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
222 generate_consumer( consumer, p, index++ );
225 else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
227 // YAML file supplied
231 snprintf( key, sizeof(key), "%d", index );
232 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
233 generate_consumer( consumer, p, index++ );
235 mlt_properties_close( properties );
239 // properties file supplied or properties on this consumer
243 mlt_properties_close( properties );
245 properties = mlt_properties_load( resource );
247 properties = MLT_CONSUMER_PROPERTIES( consumer );
250 snprintf( key, sizeof(key), "%d", index );
251 if ( ( s = mlt_properties_get( properties, key ) ) )
253 mlt_properties p = mlt_properties_new();
257 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
258 snprintf( key, sizeof(key), "%d.", index );
260 count = mlt_properties_count( properties );
261 for ( i = 0; i < count; i++ )
263 char *name = mlt_properties_get_name( properties, i );
264 if ( !strncmp( name, key, strlen(key) ) )
265 mlt_properties_set( p, name + strlen(key),
266 mlt_properties_get_value( properties, i ) );
268 generate_consumer( consumer, p, index++ );
269 mlt_properties_close( p );
273 mlt_properties_close( properties );
277 static void foreach_consumer_start( mlt_consumer consumer )
279 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
280 mlt_consumer nested = NULL;
285 snprintf( key, sizeof(key), "%d.consumer", index++ );
286 nested = mlt_properties_get_data( properties, key, NULL );
289 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
290 mlt_properties_set_position( nested_props, "_multi_position", 0 );
291 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
292 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
293 mlt_consumer_start( nested );
298 static void foreach_consumer_refresh( mlt_consumer consumer )
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 );
308 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
312 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
314 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
315 mlt_consumer nested = NULL;
320 snprintf( key, sizeof(key), "%d.consumer", index++ );
321 nested = mlt_properties_get_data( properties, key, NULL );
324 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
325 double self_fps = mlt_properties_get_double( properties, "fps" );
326 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
327 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
328 mlt_position self_pos = mlt_frame_get_position( frame );
329 double self_time = self_pos / self_fps;
330 double nested_time = nested_pos / nested_fps;
332 // get the audio for the current frame
333 uint8_t *buffer = NULL;
334 mlt_audio_format format = mlt_audio_s16;
335 int channels = mlt_properties_get_int( properties, "channels" );
336 int frequency = mlt_properties_get_int( properties, "frequency" );
337 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
338 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
339 int current_size = mlt_audio_format_size( format, current_samples, channels );
341 // get any leftover audio
343 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
344 uint8_t *new_buffer = NULL;
347 new_buffer = mlt_pool_alloc( prev_size + current_size );
348 memcpy( new_buffer, prev_buffer, prev_size );
349 memcpy( new_buffer + prev_size, buffer, current_size );
352 current_size += prev_size;
353 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
355 while ( nested_time <= self_time )
357 // put ideal number of samples into cloned frame
358 int deeply = index > 1 ? 1 : 0;
359 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
360 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
361 // -10 is an optimization to avoid tiny amounts of leftover samples
362 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
363 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
364 if ( nested_size > 0 )
366 prev_buffer = mlt_pool_alloc( nested_size );
367 memcpy( prev_buffer, buffer, nested_size );
374 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
375 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
376 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
377 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
380 current_samples -= nested_samples;
381 current_size -= nested_size;
382 buffer += nested_size;
384 // send frame to nested consumer
385 mlt_consumer_put_frame( nested, clone_frame );
386 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
387 nested_time = nested_pos / nested_fps;
390 // save any remaining audio
391 if ( current_size > 0 )
393 prev_buffer = mlt_pool_alloc( current_size );
394 memcpy( prev_buffer, buffer, current_size );
401 mlt_pool_release( new_buffer );
402 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
403 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
408 static void foreach_consumer_stop( mlt_consumer consumer )
410 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
411 mlt_consumer nested = NULL;
414 struct timespec tm = { 0, 1000 * 1000 };
417 snprintf( key, sizeof(key), "%d.consumer", index++ );
418 nested = mlt_properties_get_data( properties, key, NULL );
421 // Let consumer with terminate_on_pause stop on their own
422 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
424 // Send additional dummy frame to unlatch nested consumer's threads
425 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
427 while ( !mlt_consumer_is_stopped( nested ) )
428 nanosleep( &tm, NULL );
432 mlt_consumer_stop( nested );
438 /** Start the consumer.
441 static int start( mlt_consumer consumer )
443 // Check that we're not already running
444 if ( is_stopped( consumer ) )
446 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
447 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
449 // Assign the thread to properties with automatic dealloc
450 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
452 // Set the running state
453 mlt_properties_set_int( properties, "running", 1 );
454 mlt_properties_set_int( properties, "joined", 0 );
456 // Construct and start nested consumers
457 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
458 foreach_consumer_init( consumer );
459 foreach_consumer_start( consumer );
462 pthread_create( thread, NULL, consumer_thread, consumer );
467 /** Stop the consumer.
470 static int stop( mlt_consumer consumer )
472 // Check that we're running
473 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
475 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
476 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
479 mlt_properties_set_int( properties, "running", 0 );
481 // Wait for termination
484 foreach_consumer_refresh( consumer );
485 pthread_join( *thread, NULL );
487 mlt_properties_set_int( properties, "joined", 1 );
489 // Stop nested consumers
490 foreach_consumer_stop( consumer );
496 /** Determine if the consumer is stopped.
499 static int is_stopped( mlt_consumer consumer )
501 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
504 /** The main thread - the argument is simply the consumer.
507 static void *consumer_thread( void *arg )
509 mlt_consumer consumer = arg;
510 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
511 mlt_frame frame = NULL;
513 // Determine whether to stop at end-of-media
514 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
517 // Loop while running
518 while ( !terminated && !is_stopped( consumer ) )
520 // Get the next frame
521 frame = mlt_consumer_rt_frame( consumer );
523 // Check for termination
524 if ( terminate_on_pause && frame )
525 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
527 // Check that we have a frame to work with
528 if ( frame && !terminated && !is_stopped( consumer ) )
530 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
532 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
533 foreach_consumer_refresh( consumer );
534 foreach_consumer_put( consumer, frame );
538 int dropped = mlt_properties_get_int( properties, "_dropped" );
539 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
540 mlt_properties_set_int( properties, "_dropped", dropped );
542 mlt_frame_close( frame );
546 if ( frame && terminated )
548 // Send this termination frame to nested consumers for their cancellation
549 foreach_consumer_put( consumer, frame );
552 mlt_frame_close( frame );
557 // Indicate that the consumer is stopped
558 mlt_consumer_stopped( consumer );
563 /** Close the consumer.
566 static void consumer_close( mlt_consumer consumer )
568 mlt_consumer_stop( consumer );
571 mlt_consumer_close( consumer );