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 );
191 properties = mlt_properties_load( resource );
193 properties = MLT_CONSUMER_PROPERTIES( consumer );
196 snprintf( key, sizeof(key), "%d", index );
197 if ( ( s = mlt_properties_get( properties, key ) ) )
199 mlt_properties p = mlt_properties_new();
203 mlt_properties_set( p, "consumer", mlt_properties_get( properties, key ) );
204 snprintf( key, sizeof(key), "%d.", index );
206 count = mlt_properties_count( properties );
207 for ( i = 0; i < count; i++ )
209 char *name = mlt_properties_get_name( properties, i );
210 if ( !strncmp( name, key, strlen(key) ) )
211 mlt_properties_set( p, name + strlen(key),
212 mlt_properties_get_value( properties, i ) );
214 generate_consumer( consumer, p, index++ );
215 mlt_properties_close( p );
219 mlt_properties_close( properties );
223 static void foreach_consumer_start( mlt_consumer consumer )
225 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
226 mlt_consumer nested = NULL;
231 snprintf( key, sizeof(key), "%d.consumer", index++ );
232 nested = mlt_properties_get_data( properties, key, NULL );
233 if ( nested ) mlt_consumer_start( nested );
237 static void foreach_consumer_refresh( mlt_consumer consumer )
239 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
240 mlt_consumer nested = NULL;
245 snprintf( key, sizeof(key), "%d.consumer", index++ );
246 nested = mlt_properties_get_data( properties, key, NULL );
247 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
251 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
253 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
254 mlt_consumer nested = NULL;
259 snprintf( key, sizeof(key), "%d.consumer", index++ );
260 nested = mlt_properties_get_data( properties, key, NULL );
261 if ( nested ) mlt_consumer_put_frame( nested, mlt_frame_clone( frame ) );
265 static void foreach_consumer_stop( mlt_consumer consumer )
267 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
268 mlt_consumer nested = NULL;
273 snprintf( key, sizeof(key), "%d.consumer", index++ );
274 nested = mlt_properties_get_data( properties, key, NULL );
275 if ( nested ) mlt_consumer_stop( nested );
279 /** Start the consumer.
282 static int start( mlt_consumer consumer )
284 // Check that we're not already running
285 if ( is_stopped( consumer ) )
287 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
288 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
290 // Assign the thread to properties with automatic dealloc
291 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
293 // Set the running state
294 mlt_properties_set_int( properties, "running", 1 );
296 // Construct and start nested consumers
297 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
298 foreach_consumer_init( consumer );
299 foreach_consumer_start( consumer );
302 pthread_create( thread, NULL, consumer_thread, consumer );
307 /** Stop the consumer.
310 static int stop( mlt_consumer consumer )
312 // Check that we're running
313 if ( !is_stopped( consumer ) )
315 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
316 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
319 mlt_properties_set_int( properties, "running", 0 );
321 // Wait for termination
324 foreach_consumer_refresh( consumer );
325 pthread_join( *thread, NULL );
328 // Stop nested consumers
329 foreach_consumer_stop( consumer );
335 /** Determine if the consumer is stopped.
338 static int is_stopped( mlt_consumer consumer )
340 return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
343 /** The main thread - the argument is simply the consumer.
346 static void *consumer_thread( void *arg )
348 mlt_consumer consumer = arg;
349 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
350 mlt_frame frame = NULL;
352 // Determine whether to stop at end-of-media
353 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
356 // Loop while running
357 while ( !terminated && !is_stopped( consumer ) )
359 // Get the next frame
360 frame = mlt_consumer_rt_frame( consumer );
362 // Check for termination
363 if ( terminate_on_pause && frame )
364 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
366 // Check that we have a frame to work with
367 if ( frame && !terminated && !is_stopped( consumer ) )
369 if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
371 int dropped = mlt_properties_get_int( properties, "_dropped" );
372 mlt_frame_close( frame );
373 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
374 mlt_properties_set_int( properties, "_dropped", dropped );
377 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
378 foreach_consumer_refresh( consumer );
379 foreach_consumer_put( consumer, frame );
380 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
381 mlt_frame_close( frame );
385 if ( frame ) mlt_frame_close( frame );
386 foreach_consumer_put( consumer, NULL );
391 // Indicate that the consumer is stopped
392 mlt_properties_set_int( properties, "running", 0 );
393 mlt_consumer_stopped( consumer );
398 /** Close the consumer.
401 static void consumer_close( mlt_consumer consumer )
403 mlt_consumer_stop( consumer );
405 mlt_consumer_close( consumer );