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 create_filter( profile, service, "avcolor_space", &created );
140 create_filter( profile, service, "imageconvert", &created );
141 create_filter( profile, service, "audioconvert", &created );
144 static void on_frame_show( void *dummy, mlt_properties properties, mlt_frame frame )
146 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
149 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
151 mlt_profile profile = NULL;
152 if ( mlt_properties_get( props, "mlt_profile" ) )
153 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
155 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
156 mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ),
157 mlt_properties_get( props, "target" ) );
161 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
162 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
165 snprintf( key, sizeof(key), "%d.consumer", index );
166 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
167 snprintf( key, sizeof(key), "%d.profile", index );
168 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
170 mlt_properties_set_int( nested_props, "put_mode", 1 );
171 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
172 mlt_properties_set( props, "consumer", NULL );
173 // set mlt_profile before other properties to facilitate presets
174 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
175 mlt_properties_inherit( nested_props, props );
177 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
179 // Relay the first available consumer-frame-show event
180 mlt_event event = mlt_properties_get_data( properties, "frame-show-event", NULL );
183 event = mlt_events_listen( nested_props, properties, "consumer-frame-show", (mlt_listener) on_frame_show );
184 mlt_properties_set_data( properties, "frame-show-event", event, 0, /*mlt_event_close*/ NULL, NULL );
189 mlt_profile_close( profile );
194 static void foreach_consumer_init( mlt_consumer consumer )
196 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
197 mlt_properties properties = mlt_properties_parse_yaml( resource );
201 if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
203 // Properties set directly by application
207 mlt_properties_close( properties );
208 properties = MLT_CONSUMER_PROPERTIES(consumer);
210 snprintf( key, sizeof(key), "%d", index );
211 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
212 generate_consumer( consumer, p, index++ );
215 else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
217 // YAML file supplied
221 snprintf( key, sizeof(key), "%d", index );
222 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
223 generate_consumer( consumer, p, index++ );
225 mlt_properties_close( properties );
229 // properties file supplied or properties on this consumer
233 mlt_properties_close( properties );
235 properties = mlt_properties_load( resource );
237 properties = MLT_CONSUMER_PROPERTIES( consumer );
240 snprintf( key, sizeof(key), "%d", index );
241 if ( ( s = mlt_properties_get( properties, key ) ) )
243 mlt_properties p = mlt_properties_new();
247 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
248 snprintf( key, sizeof(key), "%d.", index );
250 count = mlt_properties_count( properties );
251 for ( i = 0; i < count; i++ )
253 char *name = mlt_properties_get_name( properties, i );
254 if ( !strncmp( name, key, strlen(key) ) )
255 mlt_properties_set( p, name + strlen(key),
256 mlt_properties_get_value( properties, i ) );
258 generate_consumer( consumer, p, index++ );
259 mlt_properties_close( p );
263 mlt_properties_close( properties );
267 static void foreach_consumer_start( mlt_consumer consumer )
269 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
270 mlt_consumer nested = NULL;
275 snprintf( key, sizeof(key), "%d.consumer", index++ );
276 nested = mlt_properties_get_data( properties, key, NULL );
279 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
280 mlt_properties_set_position( nested_props, "_multi_position", 0 );
281 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
282 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
283 mlt_consumer_start( nested );
288 static void foreach_consumer_refresh( mlt_consumer consumer )
290 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
291 mlt_consumer nested = NULL;
296 snprintf( key, sizeof(key), "%d.consumer", index++ );
297 nested = mlt_properties_get_data( properties, key, NULL );
298 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
302 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
304 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
305 mlt_consumer nested = NULL;
310 snprintf( key, sizeof(key), "%d.consumer", index++ );
311 nested = mlt_properties_get_data( properties, key, NULL );
314 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
315 double self_fps = mlt_properties_get_double( properties, "fps" );
316 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
317 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
318 mlt_position self_pos = mlt_frame_get_position( frame );
319 double self_time = self_pos / self_fps;
320 double nested_time = nested_pos / nested_fps;
322 // get the audio for the current frame
323 uint8_t *buffer = NULL;
324 mlt_audio_format format = mlt_audio_s16;
325 int channels = mlt_properties_get_int( properties, "channels" );
326 int frequency = mlt_properties_get_int( properties, "frequency" );
327 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
328 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
329 int current_size = mlt_audio_format_size( format, current_samples, channels );
331 // get any leftover audio
333 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
334 uint8_t *new_buffer = NULL;
337 new_buffer = mlt_pool_alloc( prev_size + current_size );
338 memcpy( new_buffer, prev_buffer, prev_size );
339 memcpy( new_buffer + prev_size, buffer, current_size );
342 current_size += prev_size;
343 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
345 while ( nested_time <= self_time )
347 // put ideal number of samples into cloned frame
348 int deeply = index > 1 ? 1 : 0;
349 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
350 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
351 // -10 is an optimization to avoid tiny amounts of leftover samples
352 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
353 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
354 if ( nested_size > 0 )
356 prev_buffer = mlt_pool_alloc( nested_size );
357 memcpy( prev_buffer, buffer, nested_size );
364 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
365 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
366 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
367 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
370 current_samples -= nested_samples;
371 current_size -= nested_size;
372 buffer += nested_size;
374 // send frame to nested consumer
375 mlt_consumer_put_frame( nested, clone_frame );
376 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
377 nested_time = nested_pos / nested_fps;
380 // save any remaining audio
381 if ( current_size > 0 )
383 prev_buffer = mlt_pool_alloc( current_size );
384 memcpy( prev_buffer, buffer, current_size );
391 mlt_pool_release( new_buffer );
392 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
393 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
398 static void foreach_consumer_stop( mlt_consumer consumer )
400 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
401 mlt_consumer nested = NULL;
404 struct timespec tm = { 0, 1000 * 1000 };
407 snprintf( key, sizeof(key), "%d.consumer", index++ );
408 nested = mlt_properties_get_data( properties, key, NULL );
411 // Let consumer with terminate_on_pause stop on their own
412 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
414 // Send additional dummy frame to unlatch nested consumer's threads
415 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
417 while ( !mlt_consumer_is_stopped( nested ) )
418 nanosleep( &tm, NULL );
422 mlt_consumer_stop( nested );
428 /** Start the consumer.
431 static int start( mlt_consumer consumer )
433 // Check that we're not already running
434 if ( is_stopped( consumer ) )
436 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
437 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
439 // Assign the thread to properties with automatic dealloc
440 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
442 // Set the running state
443 mlt_properties_set_int( properties, "running", 1 );
444 mlt_properties_set_int( properties, "joined", 0 );
446 // Construct and start nested consumers
447 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
448 foreach_consumer_init( consumer );
449 foreach_consumer_start( consumer );
452 pthread_create( thread, NULL, consumer_thread, consumer );
457 /** Stop the consumer.
460 static int stop( mlt_consumer consumer )
462 // Check that we're running
463 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
465 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
466 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
469 mlt_properties_set_int( properties, "running", 0 );
471 // Wait for termination
474 foreach_consumer_refresh( consumer );
475 pthread_join( *thread, NULL );
477 mlt_properties_set_int( properties, "joined", 1 );
479 // Stop nested consumers
480 foreach_consumer_stop( consumer );
486 /** Determine if the consumer is stopped.
489 static int is_stopped( mlt_consumer consumer )
491 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
494 /** The main thread - the argument is simply the consumer.
497 static void *consumer_thread( void *arg )
499 mlt_consumer consumer = arg;
500 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
501 mlt_frame frame = NULL;
503 // Determine whether to stop at end-of-media
504 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
507 // Loop while running
508 while ( !terminated && !is_stopped( consumer ) )
510 // Get the next frame
511 frame = mlt_consumer_rt_frame( consumer );
513 // Check for termination
514 if ( terminate_on_pause && frame )
515 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
517 // Check that we have a frame to work with
518 if ( frame && !terminated && !is_stopped( consumer ) )
520 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
522 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
523 foreach_consumer_refresh( consumer );
524 foreach_consumer_put( consumer, frame );
528 int dropped = mlt_properties_get_int( properties, "_dropped" );
529 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
530 mlt_properties_set_int( properties, "_dropped", dropped );
532 mlt_frame_close( frame );
536 if ( frame && terminated )
538 // Send this termination frame to nested consumers for their cancellation
539 foreach_consumer_put( consumer, frame );
542 mlt_frame_close( frame );
547 // Indicate that the consumer is stopped
548 mlt_consumer_stopped( consumer );
553 /** Close the consumer.
556 static void consumer_close( mlt_consumer consumer )
558 mlt_consumer_stop( consumer );
561 mlt_consumer_close( consumer );