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();
264 // Terminate mlt_service value at the argument delimiter if supplied.
265 // Needed here instead of just relying upon create_consumer() so that
266 // a properties preset is picked up correctly.
267 char *service = strdup( mlt_properties_get( properties, key ) );
268 char *arg = strchr( service, ':' );
271 mlt_properties_set( p, "target", arg );
273 mlt_properties_set( p, "mlt_service", service );
276 snprintf( key, sizeof(key), "%d.", index );
279 count = mlt_properties_count( properties );
280 for ( i = 0; i < count; i++ )
282 char *name = mlt_properties_get_name( properties, i );
283 if ( !strncmp( name, key, strlen(key) ) )
284 mlt_properties_set( p, name + strlen(key),
285 mlt_properties_get_value( properties, i ) );
287 generate_consumer( consumer, p, index++ );
288 mlt_properties_close( p );
292 mlt_properties_close( properties );
296 static void foreach_consumer_start( mlt_consumer consumer )
298 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
299 mlt_consumer nested = NULL;
304 snprintf( key, sizeof(key), "%d.consumer", index++ );
305 nested = mlt_properties_get_data( properties, key, NULL );
308 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
309 mlt_properties_set_position( nested_props, "_multi_position", 0 );
310 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
311 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
312 mlt_consumer_start( nested );
317 static void foreach_consumer_refresh( mlt_consumer consumer )
319 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
320 mlt_consumer nested = NULL;
325 snprintf( key, sizeof(key), "%d.consumer", index++ );
326 nested = mlt_properties_get_data( properties, key, NULL );
327 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
331 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
333 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
334 mlt_consumer nested = NULL;
339 snprintf( key, sizeof(key), "%d.consumer", index++ );
340 nested = mlt_properties_get_data( properties, key, NULL );
343 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
344 double self_fps = mlt_properties_get_double( properties, "fps" );
345 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
346 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
347 mlt_position self_pos = mlt_frame_get_position( frame );
348 double self_time = self_pos / self_fps;
349 double nested_time = nested_pos / nested_fps;
351 // get the audio for the current frame
352 uint8_t *buffer = NULL;
353 mlt_audio_format format = mlt_audio_s16;
354 int channels = mlt_properties_get_int( properties, "channels" );
355 int frequency = mlt_properties_get_int( properties, "frequency" );
356 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
357 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
358 int current_size = mlt_audio_format_size( format, current_samples, channels );
360 // get any leftover audio
362 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
363 uint8_t *new_buffer = NULL;
366 new_buffer = mlt_pool_alloc( prev_size + current_size );
367 memcpy( new_buffer, prev_buffer, prev_size );
368 memcpy( new_buffer + prev_size, buffer, current_size );
371 current_size += prev_size;
372 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
374 while ( nested_time <= self_time )
376 // put ideal number of samples into cloned frame
377 int deeply = index > 1 ? 1 : 0;
378 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
379 mlt_properties clone_props = MLT_FRAME_PROPERTIES( clone_frame );
380 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
381 // -10 is an optimization to avoid tiny amounts of leftover samples
382 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
383 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
384 if ( nested_size > 0 )
386 prev_buffer = mlt_pool_alloc( nested_size );
387 memcpy( prev_buffer, buffer, nested_size );
394 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
395 mlt_properties_set_int( clone_props, "audio_samples", nested_samples );
396 mlt_properties_set_int( clone_props, "audio_frequency", frequency );
397 mlt_properties_set_int( clone_props, "audio_channels", channels );
400 current_samples -= nested_samples;
401 current_size -= nested_size;
402 buffer += nested_size;
405 mlt_properties_set_int( clone_props, "meta.media.width",
406 mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "width" ) );
407 mlt_properties_set_int( clone_props, "meta.media.height",
408 mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "height" ) );
410 // send frame to nested consumer
411 mlt_consumer_put_frame( nested, clone_frame );
412 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
413 nested_time = nested_pos / nested_fps;
416 // save any remaining audio
417 if ( current_size > 0 )
419 prev_buffer = mlt_pool_alloc( current_size );
420 memcpy( prev_buffer, buffer, current_size );
427 mlt_pool_release( new_buffer );
428 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
429 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
434 static void foreach_consumer_stop( mlt_consumer consumer )
436 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
437 mlt_consumer nested = NULL;
440 struct timespec tm = { 0, 1000 * 1000 };
443 snprintf( key, sizeof(key), "%d.consumer", index++ );
444 nested = mlt_properties_get_data( properties, key, NULL );
447 // Let consumer with terminate_on_pause stop on their own
448 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
450 // Send additional dummy frame to unlatch nested consumer's threads
451 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
453 while ( !mlt_consumer_is_stopped( nested ) )
454 nanosleep( &tm, NULL );
458 mlt_consumer_stop( nested );
464 /** Start the consumer.
467 static int start( mlt_consumer consumer )
469 // Check that we're not already running
470 if ( is_stopped( consumer ) )
472 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
473 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
475 // Assign the thread to properties with automatic dealloc
476 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
478 // Set the running state
479 mlt_properties_set_int( properties, "running", 1 );
480 mlt_properties_set_int( properties, "joined", 0 );
482 // Construct and start nested consumers
483 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
484 foreach_consumer_init( consumer );
485 foreach_consumer_start( consumer );
488 pthread_create( thread, NULL, consumer_thread, consumer );
493 /** Stop the consumer.
496 static int stop( mlt_consumer consumer )
498 // Check that we're running
499 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
501 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
502 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
505 mlt_properties_set_int( properties, "running", 0 );
507 // Wait for termination
510 foreach_consumer_refresh( consumer );
511 pthread_join( *thread, NULL );
513 mlt_properties_set_int( properties, "joined", 1 );
515 // Stop nested consumers
516 foreach_consumer_stop( consumer );
522 /** Determine if the consumer is stopped.
525 static int is_stopped( mlt_consumer consumer )
527 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
530 /** Purge each of the child consumers.
533 static void purge( mlt_consumer consumer )
535 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
536 if ( mlt_properties_get_int( properties, "running" ) )
538 mlt_consumer nested = NULL;
543 snprintf( key, sizeof(key), "%d.consumer", index++ );
544 nested = mlt_properties_get_data( properties, key, NULL );
546 mlt_consumer_purge( nested );
551 /** The main thread - the argument is simply the consumer.
554 static void *consumer_thread( void *arg )
556 mlt_consumer consumer = arg;
557 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
558 mlt_frame frame = NULL;
560 // Determine whether to stop at end-of-media
561 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
564 // Loop while running
565 while ( !terminated && !is_stopped( consumer ) )
567 // Get the next frame
568 frame = mlt_consumer_rt_frame( consumer );
570 // Check for termination
571 if ( terminate_on_pause && frame )
572 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
574 // Check that we have a frame to work with
575 if ( frame && !terminated && !is_stopped( consumer ) )
577 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
579 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
580 foreach_consumer_refresh( consumer );
581 foreach_consumer_put( consumer, frame );
585 int dropped = mlt_properties_get_int( properties, "_dropped" );
586 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
587 mlt_properties_set_int( properties, "_dropped", dropped );
589 mlt_frame_close( frame );
593 if ( frame && terminated )
595 // Send this termination frame to nested consumers for their cancellation
596 foreach_consumer_put( consumer, frame );
599 mlt_frame_close( frame );
604 // Indicate that the consumer is stopped
605 mlt_consumer_stopped( consumer );
610 /** Close the consumer.
613 static void consumer_close( mlt_consumer consumer )
615 mlt_consumer_stop( consumer );
618 mlt_consumer_close( consumer );