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, "consumer" ) );
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 mlt_properties_set( props, "mlt_profile", NULL );
153 mlt_properties_inherit( nested_props, props );
155 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
159 mlt_profile_close( profile );
164 static void foreach_consumer_init( mlt_consumer consumer )
166 const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
167 mlt_properties properties = mlt_properties_parse_yaml( resource );
169 if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
171 mlt_properties p = NULL;
176 snprintf( key, sizeof(key), "%d", index );
177 if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
178 generate_consumer( consumer, p, index++ );
180 mlt_properties_close( properties );
184 const char *s = NULL;
189 mlt_properties_close( properties );
190 properties = mlt_properties_load( resource );
193 snprintf( key, sizeof(key), "%d", index );
194 if ( ( s = mlt_properties_get( properties, key ) ) )
196 mlt_properties p = mlt_properties_new();
200 mlt_properties_set( p, "consumer", mlt_properties_get( properties, key ) );
201 snprintf( key, sizeof(key), "%d.", index );
203 count = mlt_properties_count( properties );
204 for ( i = 0; i < count; i++ )
206 char *name = mlt_properties_get_name( properties, i );
207 if ( !strncmp( name, key, strlen(key) ) )
208 mlt_properties_set( p, name + strlen(key),
209 mlt_properties_get_value( properties, i ) );
211 generate_consumer( consumer, p, index++ );
212 mlt_properties_close( p );
215 mlt_properties_close( properties );
219 static void foreach_consumer_start( mlt_consumer consumer )
221 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
222 mlt_consumer nested = NULL;
227 snprintf( key, sizeof(key), "%d.consumer", index++ );
228 nested = mlt_properties_get_data( properties, key, NULL );
229 if ( nested ) mlt_consumer_start( nested );
233 static void foreach_consumer_refresh( mlt_consumer consumer )
235 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
236 mlt_consumer nested = NULL;
241 snprintf( key, sizeof(key), "%d.consumer", index++ );
242 nested = mlt_properties_get_data( properties, key, NULL );
243 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
247 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
249 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
250 mlt_consumer nested = NULL;
255 snprintf( key, sizeof(key), "%d.consumer", index++ );
256 nested = mlt_properties_get_data( properties, key, NULL );
257 if ( nested ) mlt_consumer_put_frame( nested, mlt_frame_clone( frame ) );
261 static void foreach_consumer_stop( mlt_consumer consumer )
263 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
264 mlt_consumer nested = NULL;
269 snprintf( key, sizeof(key), "%d.consumer", index++ );
270 nested = mlt_properties_get_data( properties, key, NULL );
271 if ( nested ) mlt_consumer_stop( nested );
275 /** Start the consumer.
278 static int start( mlt_consumer consumer )
280 // Check that we're not already running
281 if ( is_stopped( consumer ) )
283 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
284 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
286 // Assign the thread to properties with automatic dealloc
287 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
289 // Set the running state
290 mlt_properties_set_int( properties, "running", 1 );
292 // Construct and start nested consumers
293 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
294 foreach_consumer_init( consumer );
295 foreach_consumer_start( consumer );
298 pthread_create( thread, NULL, consumer_thread, consumer );
303 /** Stop the consumer.
306 static int stop( mlt_consumer consumer )
308 // Check that we're running
309 if ( !is_stopped( consumer ) )
311 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
312 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
315 mlt_properties_set_int( properties, "running", 0 );
317 // Wait for termination
320 foreach_consumer_refresh( consumer );
321 pthread_join( *thread, NULL );
324 // Stop nested consumers
325 foreach_consumer_stop( consumer );
331 /** Determine if the consumer is stopped.
334 static int is_stopped( mlt_consumer consumer )
336 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
339 /** The main thread - the argument is simply the consumer.
342 static void *consumer_thread( void *arg )
344 mlt_consumer consumer = arg;
345 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
346 mlt_frame frame = NULL;
348 // Determine whether to stop at end-of-media
349 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
352 // Loop while running
353 while ( !terminated && !is_stopped( consumer ) )
355 // Get the next frame
356 frame = mlt_consumer_rt_frame( consumer );
358 // Check for termination
359 if ( terminate_on_pause && frame )
360 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
362 // Check that we have a frame to work with
363 if ( frame && !terminated && !is_stopped( consumer ) )
365 if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
367 int dropped = mlt_properties_get_int( properties, "_dropped" );
368 mlt_frame_close( frame );
369 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
370 mlt_properties_set_int( properties, "_dropped", dropped );
373 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
374 foreach_consumer_refresh( consumer );
375 foreach_consumer_put( consumer, frame );
376 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
377 mlt_frame_close( frame );
381 if ( frame ) mlt_frame_close( frame );
382 foreach_consumer_put( consumer, NULL );
387 // Indicate that the consumer is stopped
388 mlt_properties_set_int( properties, "running", 0 );
389 mlt_consumer_stopped( consumer );
394 /** Close the consumer.
397 static void consumer_close( mlt_consumer consumer )
399 mlt_consumer_stop( consumer );
401 mlt_consumer_close( consumer );