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 );
51 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(consumer), "real_time", -1 );
57 static mlt_consumer create_consumer( mlt_profile profile, char *id )
59 char *myid = id ? strdup( id ) : NULL;
60 char *arg = myid ? strchr( myid, ':' ) : NULL;
63 mlt_consumer consumer = mlt_factory_consumer( profile, myid, arg );
69 static void create_filter( mlt_profile profile, mlt_service service, char *effect, int *created )
71 char *id = strdup( effect );
72 char *arg = strchr( id, ':' );
76 // The swscale and avcolor_space filters require resolution as arg to test compatibility
77 if ( strncmp( effect, "swscale", 7 ) == 0 || strncmp( effect, "avcolo", 6 ) == 0 )
78 arg = (char*) mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "_real_width" );
80 mlt_filter filter = mlt_factory_filter( profile, id, arg );
83 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
84 mlt_service_attach( service, filter );
85 mlt_filter_close( filter );
91 static void attach_normalisers( mlt_profile profile, mlt_service service )
97 mlt_tokeniser tokeniser = mlt_tokeniser_init( );
99 // We only need to load the normalising properties once
100 if ( normalisers == NULL )
103 snprintf( temp, sizeof(temp), "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
104 normalisers = mlt_properties_load( temp );
105 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
109 for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
113 char *value = mlt_properties_get_value( normalisers, i );
114 mlt_tokeniser_parse_new( tokeniser, value, "," );
115 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
116 create_filter( profile, service, mlt_tokeniser_get_string( tokeniser, j ), &created );
119 // Close the tokeniser
120 mlt_tokeniser_close( tokeniser );
122 // Attach the audio and video format converters
124 create_filter( profile, service, "avcolor_space", &created );
126 create_filter( profile, service, "imageconvert", &created );
127 create_filter( profile, service, "audioconvert", &created );
130 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
132 mlt_profile profile = NULL;
133 if ( mlt_properties_get( props, "mlt_profile" ) )
134 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
136 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
137 mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ) );
141 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
142 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
145 snprintf( key, sizeof(key), "%d.consumer", index );
146 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
147 snprintf( key, sizeof(key), "%d.profile", index );
148 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
150 mlt_properties_set_int( nested_props, "put_mode", 1 );
151 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
152 mlt_properties_set( props, "consumer", NULL );
153 // set mlt_profile before other properties to facilitate presets
154 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
155 mlt_properties_inherit( nested_props, props );
157 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
161 mlt_profile_close( profile );
166 static void foreach_consumer_init( mlt_consumer consumer )
168 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
169 mlt_properties properties = mlt_properties_parse_yaml( resource );
173 if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
175 // Properties set directly by application
179 mlt_properties_close( properties );
180 properties = MLT_CONSUMER_PROPERTIES(consumer);
182 snprintf( key, sizeof(key), "%d", index );
183 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
184 generate_consumer( consumer, p, index++ );
187 else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
189 // YAML file supplied
193 snprintf( key, sizeof(key), "%d", index );
194 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
195 generate_consumer( consumer, p, index++ );
197 mlt_properties_close( properties );
201 // properties file supplied or properties on this consumer
205 mlt_properties_close( properties );
207 properties = mlt_properties_load( resource );
209 properties = MLT_CONSUMER_PROPERTIES( consumer );
212 snprintf( key, sizeof(key), "%d", index );
213 if ( ( s = mlt_properties_get( properties, key ) ) )
215 mlt_properties p = mlt_properties_new();
219 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
220 snprintf( key, sizeof(key), "%d.", index );
222 count = mlt_properties_count( properties );
223 for ( i = 0; i < count; i++ )
225 char *name = mlt_properties_get_name( properties, i );
226 if ( !strncmp( name, key, strlen(key) ) )
227 mlt_properties_set( p, name + strlen(key),
228 mlt_properties_get_value( properties, i ) );
230 generate_consumer( consumer, p, index++ );
231 mlt_properties_close( p );
235 mlt_properties_close( properties );
239 static void foreach_consumer_start( mlt_consumer consumer )
241 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
242 mlt_consumer nested = NULL;
247 snprintf( key, sizeof(key), "%d.consumer", index++ );
248 nested = mlt_properties_get_data( properties, key, NULL );
251 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
252 mlt_properties_set_position( nested_props, "_multi_position", 0 );
253 mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
254 mlt_properties_set_int( nested_props, "_multi_samples", 0 );
255 mlt_consumer_start( nested );
260 static void foreach_consumer_refresh( mlt_consumer consumer )
262 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
263 mlt_consumer nested = NULL;
268 snprintf( key, sizeof(key), "%d.consumer", index++ );
269 nested = mlt_properties_get_data( properties, key, NULL );
270 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
274 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
276 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
277 mlt_consumer nested = NULL;
282 snprintf( key, sizeof(key), "%d.consumer", index++ );
283 nested = mlt_properties_get_data( properties, key, NULL );
286 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
287 double self_fps = mlt_properties_get_double( properties, "fps" );
288 double nested_fps = mlt_properties_get_double( nested_props, "fps" );
289 mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
290 mlt_position self_pos = mlt_frame_get_position( frame );
291 double self_time = self_pos / self_fps;
292 double nested_time = nested_pos / nested_fps;
294 // get the audio for the current frame
295 uint8_t *buffer = NULL;
296 mlt_audio_format format = mlt_audio_s16;
297 int channels = mlt_properties_get_int( properties, "channels" );
298 int frequency = mlt_properties_get_int( properties, "frequency" );
299 int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
300 mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, ¤t_samples );
301 int current_size = mlt_audio_format_size( format, current_samples, channels );
303 // get any leftover audio
305 uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
306 uint8_t *new_buffer = NULL;
309 new_buffer = mlt_pool_alloc( prev_size + current_size );
310 memcpy( new_buffer, prev_buffer, prev_size );
311 memcpy( new_buffer + prev_size, buffer, current_size );
314 current_size += prev_size;
315 current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
317 while ( nested_time <= self_time )
319 // put ideal number of samples into cloned frame
320 mlt_frame clone_frame = mlt_frame_clone( frame, 0 );
321 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
322 // -10 is an optimization to avoid tiny amounts of leftover samples
323 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
324 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
325 if ( nested_size > 0 )
327 prev_buffer = mlt_pool_alloc( nested_size );
328 memcpy( prev_buffer, buffer, nested_size );
335 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
336 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
337 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
338 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
341 current_samples -= nested_samples;
342 current_size -= nested_size;
343 buffer += nested_size;
345 // send frame to nested consumer
346 mlt_consumer_put_frame( nested, clone_frame );
347 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
348 nested_time = nested_pos / nested_fps;
351 // save any remaining audio
352 if ( current_size > 0 )
354 prev_buffer = mlt_pool_alloc( current_size );
355 memcpy( prev_buffer, buffer, current_size );
362 mlt_pool_release( new_buffer );
363 mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
364 mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
369 static void foreach_consumer_stop( mlt_consumer consumer )
371 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
372 mlt_consumer nested = NULL;
377 snprintf( key, sizeof(key), "%d.consumer", index++ );
378 nested = mlt_properties_get_data( properties, key, NULL );
379 if ( nested ) mlt_consumer_stop( nested );
383 /** Start the consumer.
386 static int start( mlt_consumer consumer )
388 // Check that we're not already running
389 if ( is_stopped( consumer ) )
391 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
392 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
394 // Assign the thread to properties with automatic dealloc
395 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
397 // Set the running state
398 mlt_properties_set_int( properties, "running", 1 );
400 // Construct and start nested consumers
401 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
402 foreach_consumer_init( consumer );
403 foreach_consumer_start( consumer );
406 pthread_create( thread, NULL, consumer_thread, consumer );
411 /** Stop the consumer.
414 static int stop( mlt_consumer consumer )
416 // Check that we're running
417 if ( !is_stopped( consumer ) )
419 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
420 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
423 mlt_properties_set_int( properties, "running", 0 );
425 // Wait for termination
428 foreach_consumer_refresh( consumer );
429 pthread_join( *thread, NULL );
432 // Stop nested consumers
433 foreach_consumer_stop( consumer );
439 /** Determine if the consumer is stopped.
442 static int is_stopped( mlt_consumer consumer )
444 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
447 /** The main thread - the argument is simply the consumer.
450 static void *consumer_thread( void *arg )
452 mlt_consumer consumer = arg;
453 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
454 mlt_frame frame = NULL;
456 // Determine whether to stop at end-of-media
457 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
460 // Loop while running
461 while ( !terminated && !is_stopped( consumer ) )
463 // Get the next frame
464 frame = mlt_consumer_rt_frame( consumer );
466 // Check for termination
467 if ( terminate_on_pause && frame )
468 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
470 // Check that we have a frame to work with
471 if ( frame && !terminated && !is_stopped( consumer ) )
473 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
475 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
476 foreach_consumer_refresh( consumer );
477 foreach_consumer_put( consumer, frame );
478 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
482 int dropped = mlt_properties_get_int( properties, "_dropped" );
483 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
484 mlt_properties_set_int( properties, "_dropped", dropped );
486 mlt_frame_close( frame );
490 if ( frame ) mlt_frame_close( frame );
491 foreach_consumer_put( consumer, NULL );
496 // Indicate that the consumer is stopped
497 mlt_properties_set_int( properties, "running", 0 );
498 mlt_consumer_stopped( consumer );
503 /** Close the consumer.
506 static void consumer_close( mlt_consumer consumer )
508 mlt_consumer_stop( consumer );
510 mlt_consumer_close( consumer );