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 );
33 static void purge( mlt_consumer consumer );
35 static mlt_properties normalisers = NULL;
37 /** Initialise the consumer.
40 mlt_consumer consumer_multi_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
42 mlt_consumer consumer = mlt_consumer_new( profile );
46 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
49 mlt_properties_set( properties, "resource", arg );
50 mlt_properties_set_int( properties, "real_time", -1 );
51 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
54 mlt_properties_set_int( properties, "joined", 1 );
57 consumer->close = consumer_close;
58 consumer->start = start;
59 consumer->stop = stop;
60 consumer->is_stopped = is_stopped;
61 consumer->purge = purge;
67 static mlt_consumer create_consumer( mlt_profile profile, char *id, char *arg )
69 char *myid = id ? strdup( id ) : NULL;
70 char *myarg = ( myid && !arg ) ? strchr( myid, ':' ) : NULL;
75 mlt_consumer consumer = mlt_factory_consumer( profile, myid, myarg );
81 static void create_filter( mlt_profile profile, mlt_service service, char *effect, int *created )
83 char *id = strdup( effect );
84 char *arg = strchr( id, ':' );
88 // We cannot use GLSL-based filters here.
89 if ( strncmp( effect, "movit.", 6 ) && strncmp( effect, "glsl.", 5 ) )
92 // The swscale and avcolor_space filters require resolution as arg to test compatibility
93 if ( strncmp( effect, "swscale", 7 ) == 0 || strncmp( effect, "avcolo", 6 ) == 0 )
95 int width = mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "meta.media.width" );
96 filter = mlt_factory_filter( profile, id, &width );
100 filter = mlt_factory_filter( profile, id, arg );
104 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
105 mlt_service_attach( service, filter );
106 mlt_filter_close( filter );
113 static void attach_normalisers( mlt_profile profile, mlt_service service )
119 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
121 // We only need to load the normalising properties once
122 if ( normalisers == NULL )
125 snprintf( temp, sizeof(temp), "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
126 normalisers = mlt_properties_load( temp );
127 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
131 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
135 char *value = mlt_properties_get_value( normalisers, i );
136 mlt_tokeniser_parse_new( tokeniser, value, "," );
137 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
138 create_filter( profile, service, mlt_tokeniser_get_string( tokeniser, j ), &created );
141 // Close the tokeniser
142 mlt_tokeniser_close( tokeniser );
144 // Attach the audio and video format converters
146 // movit.convert skips setting the frame->convert_image pointer if GLSL cannot be used.
147 mlt_filter filter = mlt_factory_filter( profile, "movit.convert", NULL );
148 if ( filter != NULL )
150 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
151 mlt_service_attach( service, filter );
152 mlt_filter_close( filter );
155 // avcolor_space and imageconvert only set frame->convert_image if it has not been set.
156 create_filter( profile, service, "avcolor_space", &created );
158 create_filter( profile, service, "imageconvert", &created );
159 create_filter( profile, service, "audioconvert", &created );
162 static void on_frame_show( void *dummy, mlt_properties properties, mlt_frame frame )
164 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
167 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
169 mlt_profile profile = NULL;
170 if ( mlt_properties_get( props, "mlt_profile" ) )
171 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
173 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
174 mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ),
175 mlt_properties_get( props, "target" ) );
179 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
180 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
183 snprintf( key, sizeof(key), "%d.consumer", index );
184 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
185 snprintf( key, sizeof(key), "%d.profile", index );
186 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
188 mlt_properties_set_int( nested_props, "put_mode", 1 );
189 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
190 mlt_properties_set( props, "consumer", NULL );
191 // set mlt_profile before other properties to facilitate presets
192 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
193 mlt_properties_inherit( nested_props, props );
195 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
197 // Relay the first available consumer-frame-show event
198 mlt_event event = mlt_properties_get_data( properties, "frame-show-event", NULL );
201 event = mlt_events_listen( nested_props, properties, "consumer-frame-show", (mlt_listener) on_frame_show );
202 mlt_properties_set_data( properties, "frame-show-event", event, 0, /*mlt_event_close*/ NULL, NULL );
207 mlt_profile_close( profile );
212 static void foreach_consumer_init( mlt_consumer consumer )
214 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
215 mlt_properties properties = mlt_properties_parse_yaml( resource );
219 if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
221 // Properties set directly by application
225 mlt_properties_close( properties );
226 properties = MLT_CONSUMER_PROPERTIES(consumer);
228 snprintf( key, sizeof(key), "%d", index );
229 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
230 generate_consumer( consumer, p, index++ );
233 else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
235 // YAML file supplied
239 snprintf( key, sizeof(key), "%d", index );
240 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
241 generate_consumer( consumer, p, index++ );
243 mlt_properties_close( properties );
247 // properties file supplied or properties on this consumer
251 mlt_properties_close( properties );
253 properties = mlt_properties_load( resource );
255 properties = MLT_CONSUMER_PROPERTIES( consumer );
258 snprintf( key, sizeof(key), "%d", index );
259 if ( ( s = mlt_properties_get( properties, key ) ) )
261 mlt_properties p = mlt_properties_new();
265 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
266 snprintf( key, sizeof(key), "%d.", index );
268 count = mlt_properties_count( properties );
269 for ( i = 0; i < count; i++ )
271 char *name = mlt_properties_get_name( properties, i );
272 if ( !strncmp( name, key, strlen(key) ) )
273 mlt_properties_set( p, name + strlen(key),
274 mlt_properties_get_value( properties, i ) );
276 generate_consumer( consumer, p, index++ );
277 mlt_properties_close( p );
281 mlt_properties_close( properties );
285 static void foreach_consumer_start( mlt_consumer consumer )
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 mlt_properties_set_position( nested_props, "_multi_position", 0 );
299 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
300 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
301 mlt_consumer_start( nested );
306 static void foreach_consumer_refresh( mlt_consumer consumer )
308 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
309 mlt_consumer nested = NULL;
314 snprintf( key, sizeof(key), "%d.consumer", index++ );
315 nested = mlt_properties_get_data( properties, key, NULL );
316 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
320 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
322 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
323 mlt_consumer nested = NULL;
328 snprintf( key, sizeof(key), "%d.consumer", index++ );
329 nested = mlt_properties_get_data( properties, key, NULL );
332 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
333 double self_fps = mlt_properties_get_double( properties, "fps" );
334 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
335 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
336 mlt_position self_pos = mlt_frame_get_position( frame );
337 double self_time = self_pos / self_fps;
338 double nested_time = nested_pos / nested_fps;
340 // get the audio for the current frame
341 uint8_t *buffer = NULL;
342 mlt_audio_format format = mlt_audio_s16;
343 int channels = mlt_properties_get_int( properties, "channels" );
344 int frequency = mlt_properties_get_int( properties, "frequency" );
345 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
346 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
347 int current_size = mlt_audio_format_size( format, current_samples, channels );
349 // get any leftover audio
351 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
352 uint8_t *new_buffer = NULL;
355 new_buffer = mlt_pool_alloc( prev_size + current_size );
356 memcpy( new_buffer, prev_buffer, prev_size );
357 memcpy( new_buffer + prev_size, buffer, current_size );
360 current_size += prev_size;
361 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
363 while ( nested_time <= self_time )
365 // put ideal number of samples into cloned frame
366 int deeply = index > 1 ? 1 : 0;
367 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
368 mlt_properties clone_props = MLT_FRAME_PROPERTIES( clone_frame );
369 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
370 // -10 is an optimization to avoid tiny amounts of leftover samples
371 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
372 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
373 if ( nested_size > 0 )
375 prev_buffer = mlt_pool_alloc( nested_size );
376 memcpy( prev_buffer, buffer, nested_size );
383 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
384 mlt_properties_set_int( clone_props, "audio_samples", nested_samples );
385 mlt_properties_set_int( clone_props, "audio_frequency", frequency );
386 mlt_properties_set_int( clone_props, "audio_channels", channels );
389 current_samples -= nested_samples;
390 current_size -= nested_size;
391 buffer += nested_size;
394 mlt_properties_set_int( clone_props, "meta.media.width",
395 mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "width" ) );
396 mlt_properties_set_int( clone_props, "meta.media.height",
397 mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "height" ) );
399 // send frame to nested consumer
400 mlt_consumer_put_frame( nested, clone_frame );
401 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
402 nested_time = nested_pos / nested_fps;
405 // save any remaining audio
406 if ( current_size > 0 )
408 prev_buffer = mlt_pool_alloc( current_size );
409 memcpy( prev_buffer, buffer, current_size );
416 mlt_pool_release( new_buffer );
417 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
418 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
423 static void foreach_consumer_stop( mlt_consumer consumer )
425 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
426 mlt_consumer nested = NULL;
429 struct timespec tm = { 0, 1000 * 1000 };
432 snprintf( key, sizeof(key), "%d.consumer", index++ );
433 nested = mlt_properties_get_data( properties, key, NULL );
436 // Let consumer with terminate_on_pause stop on their own
437 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
439 // Send additional dummy frame to unlatch nested consumer's threads
440 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
442 while ( !mlt_consumer_is_stopped( nested ) )
443 nanosleep( &tm, NULL );
447 mlt_consumer_stop( nested );
453 /** Start the consumer.
456 static int start( mlt_consumer consumer )
458 // Check that we're not already running
459 if ( is_stopped( consumer ) )
461 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
462 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
464 // Assign the thread to properties with automatic dealloc
465 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
467 // Set the running state
468 mlt_properties_set_int( properties, "running", 1 );
469 mlt_properties_set_int( properties, "joined", 0 );
471 // Construct and start nested consumers
472 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
473 foreach_consumer_init( consumer );
474 foreach_consumer_start( consumer );
477 pthread_create( thread, NULL, consumer_thread, consumer );
482 /** Stop the consumer.
485 static int stop( mlt_consumer consumer )
487 // Check that we're running
488 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
490 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
491 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
494 mlt_properties_set_int( properties, "running", 0 );
496 // Wait for termination
499 foreach_consumer_refresh( consumer );
500 pthread_join( *thread, NULL );
502 mlt_properties_set_int( properties, "joined", 1 );
504 // Stop nested consumers
505 foreach_consumer_stop( consumer );
511 /** Determine if the consumer is stopped.
514 static int is_stopped( mlt_consumer consumer )
516 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
519 /** Purge each of the child consumers.
522 static void purge( mlt_consumer consumer )
524 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
525 if ( mlt_properties_get_int( properties, "running" ) )
527 mlt_consumer nested = NULL;
532 snprintf( key, sizeof(key), "%d.consumer", index++ );
533 nested = mlt_properties_get_data( properties, key, NULL );
535 mlt_consumer_purge( nested );
540 /** The main thread - the argument is simply the consumer.
543 static void *consumer_thread( void *arg )
545 mlt_consumer consumer = arg;
546 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
547 mlt_frame frame = NULL;
549 // Determine whether to stop at end-of-media
550 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
553 // Loop while running
554 while ( !terminated && !is_stopped( consumer ) )
556 // Get the next frame
557 frame = mlt_consumer_rt_frame( consumer );
559 // Check for termination
560 if ( terminate_on_pause && frame )
561 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
563 // Check that we have a frame to work with
564 if ( frame && !terminated && !is_stopped( consumer ) )
566 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
568 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
569 foreach_consumer_refresh( consumer );
570 foreach_consumer_put( consumer, frame );
574 int dropped = mlt_properties_get_int( properties, "_dropped" );
575 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
576 mlt_properties_set_int( properties, "_dropped", dropped );
578 mlt_frame_close( frame );
582 if ( frame && terminated )
584 // Send this termination frame to nested consumers for their cancellation
585 foreach_consumer_put( consumer, frame );
588 mlt_frame_close( frame );
593 // Indicate that the consumer is stopped
594 mlt_consumer_stopped( consumer );
599 /** Close the consumer.
602 static void consumer_close( mlt_consumer consumer )
604 mlt_consumer_stop( consumer );
607 mlt_consumer_close( consumer );