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>
27 static int start( mlt_consumer consumer );
28 static int stop( mlt_consumer consumer );
29 static int is_stopped( mlt_consumer consumer );
30 static void *consumer_thread( void *arg );
31 static void consumer_close( mlt_consumer consumer );
33 static mlt_properties normalisers = NULL;
35 /** Initialise the consumer.
38 mlt_consumer consumer_multi_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
40 mlt_consumer consumer = mlt_consumer_new( profile );
45 consumer->close = consumer_close;
46 consumer->start = start;
47 consumer->stop = stop;
48 consumer->is_stopped = is_stopped;
50 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "resource", arg );
56 static mlt_consumer create_consumer( mlt_profile profile, char *id )
58 char *myid = id ? strdup( id ) : NULL;
59 char *arg = myid ? strchr( myid, ':' ) : NULL;
62 mlt_consumer consumer = mlt_factory_consumer( profile, myid, arg );
68 static void create_filter( mlt_profile profile, mlt_service service, char *effect, int *created )
70 char *id = strdup( effect );
71 char *arg = strchr( id, ':' );
75 // The swscale and avcolor_space filters require resolution as arg to test compatibility
76 if ( strncmp( effect, "swscale", 7 ) == 0 || strncmp( effect, "avcolo", 6 ) == 0 )
77 arg = (char*) mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "_real_width" );
79 mlt_filter filter = mlt_factory_filter( profile, id, arg );
82 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
83 mlt_service_attach( service, filter );
84 mlt_filter_close( filter );
90 static void attach_normalisers( mlt_profile profile, mlt_service service )
96 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
98 // We only need to load the normalising properties once
99 if ( normalisers == NULL )
102 snprintf( temp, sizeof(temp), "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
103 normalisers = mlt_properties_load( temp );
104 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
108 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
112 char *value = mlt_properties_get_value( normalisers, i );
113 mlt_tokeniser_parse_new( tokeniser, value, "," );
114 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
115 create_filter( profile, service, mlt_tokeniser_get_string( tokeniser, j ), &created );
118 // Close the tokeniser
119 mlt_tokeniser_close( tokeniser );
121 // Attach the audio and video format converters
123 create_filter( profile, service, "avcolor_space", &created );
125 create_filter( profile, service, "imageconvert", &created );
126 create_filter( profile, service, "audioconvert", &created );
129 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
131 mlt_profile profile = NULL;
132 if ( mlt_properties_get( props, "mlt_profile" ) )
133 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
135 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
136 mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ) );
140 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
141 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
144 snprintf( key, sizeof(key), "%d.consumer", index );
145 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
146 snprintf( key, sizeof(key), "%d.profile", index );
147 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
149 mlt_properties_set_int( nested_props, "put_mode", 1 );
150 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
151 mlt_properties_set( props, "consumer", NULL );
152 // set mlt_profile before other properties to facilitate presets
153 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
154 mlt_properties_inherit( nested_props, props );
156 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
160 mlt_profile_close( profile );
165 static void foreach_consumer_init( mlt_consumer consumer )
167 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
168 mlt_properties properties = mlt_properties_parse_yaml( resource );
172 if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
174 // Properties set directly by application
178 mlt_properties_close( properties );
179 properties = MLT_CONSUMER_PROPERTIES(consumer);
181 snprintf( key, sizeof(key), "%d", index );
182 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
183 generate_consumer( consumer, p, index++ );
186 else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
188 // YAML file supplied
192 snprintf( key, sizeof(key), "%d", index );
193 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
194 generate_consumer( consumer, p, index++ );
196 mlt_properties_close( properties );
200 // properties file supplied or properties on this consumer
204 mlt_properties_close( properties );
206 properties = mlt_properties_load( resource );
208 properties = MLT_CONSUMER_PROPERTIES( consumer );
211 snprintf( key, sizeof(key), "%d", index );
212 if ( ( s = mlt_properties_get( properties, key ) ) )
214 mlt_properties p = mlt_properties_new();
218 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
219 snprintf( key, sizeof(key), "%d.", index );
221 count = mlt_properties_count( properties );
222 for ( i = 0; i < count; i++ )
224 char *name = mlt_properties_get_name( properties, i );
225 if ( !strncmp( name, key, strlen(key) ) )
226 mlt_properties_set( p, name + strlen(key),
227 mlt_properties_get_value( properties, i ) );
229 generate_consumer( consumer, p, index++ );
230 mlt_properties_close( p );
234 mlt_properties_close( properties );
238 static void foreach_consumer_start( mlt_consumer consumer )
240 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
241 mlt_consumer nested = NULL;
246 snprintf( key, sizeof(key), "%d.consumer", index++ );
247 nested = mlt_properties_get_data( properties, key, NULL );
250 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
251 mlt_properties_set_position( nested_props, "_multi_position", 0 );
252 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
253 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
254 mlt_consumer_start( nested );
259 static void foreach_consumer_refresh( mlt_consumer consumer )
261 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
262 mlt_consumer nested = NULL;
267 snprintf( key, sizeof(key), "%d.consumer", index++ );
268 nested = mlt_properties_get_data( properties, key, NULL );
269 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
273 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
275 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
276 mlt_consumer nested = NULL;
281 snprintf( key, sizeof(key), "%d.consumer", index++ );
282 nested = mlt_properties_get_data( properties, key, NULL );
285 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
286 double self_fps = mlt_properties_get_double( properties, "fps" );
287 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
288 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
289 mlt_position self_pos = mlt_frame_get_position( frame );
290 double self_time = self_pos / self_fps;
291 double nested_time = nested_pos / nested_fps;
293 // get the audio for the current frame
294 uint8_t *buffer = NULL;
295 mlt_audio_format format = mlt_audio_s16;
296 int channels = mlt_properties_get_int( properties, "channels" );
297 int frequency = mlt_properties_get_int( properties, "frequency" );
298 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
299 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
300 int current_size = mlt_audio_format_size( format, current_samples, channels );
302 // get any leftover audio
304 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
305 uint8_t *new_buffer = NULL;
308 new_buffer = mlt_pool_alloc( prev_size + current_size );
309 memcpy( new_buffer, prev_buffer, prev_size );
310 memcpy( new_buffer + prev_size, buffer, current_size );
313 current_size += prev_size;
314 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
316 while ( nested_time <= self_time )
318 // put ideal number of samples into cloned frame
319 mlt_frame clone_frame = mlt_frame_clone( frame, 0 );
320 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
321 // -10 is an optimization to avoid tiny amounts of leftover samples
322 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
323 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
324 if ( nested_size > 0 )
326 prev_buffer = mlt_pool_alloc( nested_size );
327 memcpy( prev_buffer, buffer, nested_size );
334 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
335 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
336 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
337 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
340 current_samples -= nested_samples;
341 current_size -= nested_size;
342 buffer += nested_size;
344 // send frame to nested consumer
345 mlt_consumer_put_frame( nested, clone_frame );
346 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
347 nested_time = nested_pos / nested_fps;
350 // save any remaining audio
351 if ( current_size > 0 )
353 prev_buffer = mlt_pool_alloc( current_size );
354 memcpy( prev_buffer, buffer, current_size );
361 mlt_pool_release( new_buffer );
362 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
363 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
368 static void foreach_consumer_stop( mlt_consumer consumer )
370 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
371 mlt_consumer nested = NULL;
376 snprintf( key, sizeof(key), "%d.consumer", index++ );
377 nested = mlt_properties_get_data( properties, key, NULL );
378 if ( nested ) mlt_consumer_stop( nested );
382 /** Start the consumer.
385 static int start( mlt_consumer consumer )
387 // Check that we're not already running
388 if ( is_stopped( consumer ) )
390 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
391 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
393 // Assign the thread to properties with automatic dealloc
394 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
396 // Set the running state
397 mlt_properties_set_int( properties, "running", 1 );
399 // Construct and start nested consumers
400 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
401 foreach_consumer_init( consumer );
402 foreach_consumer_start( consumer );
405 pthread_create( thread, NULL, consumer_thread, consumer );
410 /** Stop the consumer.
413 static int stop( mlt_consumer consumer )
415 // Check that we're running
416 if ( !is_stopped( consumer ) )
418 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
419 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
422 mlt_properties_set_int( properties, "running", 0 );
424 // Wait for termination
427 foreach_consumer_refresh( consumer );
428 pthread_join( *thread, NULL );
431 // Stop nested consumers
432 foreach_consumer_stop( consumer );
438 /** Determine if the consumer is stopped.
441 static int is_stopped( mlt_consumer consumer )
443 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
446 /** The main thread - the argument is simply the consumer.
449 static void *consumer_thread( void *arg )
451 mlt_consumer consumer = arg;
452 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
453 mlt_frame frame = NULL;
455 // Determine whether to stop at end-of-media
456 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
459 // Loop while running
460 while ( !terminated && !is_stopped( consumer ) )
462 // Get the next frame
463 frame = mlt_consumer_rt_frame( consumer );
465 // Check for termination
466 if ( terminate_on_pause && frame )
467 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
469 // Check that we have a frame to work with
470 if ( frame && !terminated && !is_stopped( consumer ) )
472 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
474 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
475 foreach_consumer_refresh( consumer );
476 foreach_consumer_put( consumer, frame );
477 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
481 int dropped = mlt_properties_get_int( properties, "_dropped" );
482 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
483 mlt_properties_set_int( properties, "_dropped", dropped );
485 mlt_frame_close( frame );
489 if ( frame ) mlt_frame_close( frame );
490 foreach_consumer_put( consumer, NULL );
495 // Indicate that the consumer is stopped
496 mlt_properties_set_int( properties, "running", 0 );
497 mlt_consumer_stopped( consumer );
502 /** Close the consumer.
505 static void consumer_close( mlt_consumer consumer )
507 mlt_consumer_stop( consumer );
509 mlt_consumer_close( consumer );