]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_multi.c
add support for app-supplied properties objects on multi consumer
[mlt] / src / modules / core / consumer_multi.c
1 /*
2  * Copyright (C) 2011 Ushodaya Enterprises Limited
3  * Author: Dan Dennedy <dan@dennedy.org>
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <framework/mlt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <pthread.h>
25
26 // Forward references
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 );
32
33 static mlt_properties normalisers = NULL;
34
35 /** Initialise the consumer.
36 */
37
38 mlt_consumer consumer_multi_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
39 {
40         mlt_consumer consumer = mlt_consumer_new( profile );
41
42         if ( consumer )
43         {
44                 // Assign callbacks
45                 consumer->close = consumer_close;
46                 consumer->start = start;
47                 consumer->stop = stop;
48                 consumer->is_stopped = is_stopped;
49
50                 mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "resource", arg );
51         }
52
53         return consumer;
54 }
55
56 static mlt_consumer create_consumer( mlt_profile profile, char *id )
57 {
58         char *myid = id ? strdup( id ) : NULL;
59         char *arg = myid ? strchr( myid, ':' ) : NULL;
60         if ( arg != NULL )
61                 *arg ++ = '\0';
62         mlt_consumer consumer = mlt_factory_consumer( profile, myid, arg );
63         if ( myid )
64                 free( myid );
65         return consumer;
66 }
67
68 static void create_filter( mlt_profile profile, mlt_service service, char *effect, int *created )
69 {
70         char *id = strdup( effect );
71         char *arg = strchr( id, ':' );
72         if ( arg != NULL )
73                 *arg ++ = '\0';
74
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" );
78
79         mlt_filter filter = mlt_factory_filter( profile, id, arg );
80         if ( filter != NULL )
81         {
82                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
83                 mlt_service_attach( service, filter );
84                 mlt_filter_close( filter );
85                 *created = 1;
86         }
87         free( id );
88 }
89
90 static void attach_normalisers( mlt_profile profile, mlt_service service )
91 {
92         // Loop variable
93         int i;
94
95         // Tokeniser
96         mlt_tokeniser tokeniser = mlt_tokeniser_init( );
97
98         // We only need to load the normalising properties once
99         if ( normalisers == NULL )
100         {
101                 char temp[ 1024 ];
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 );
105         }
106
107         // Apply normalisers
108         for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
109         {
110                 int j = 0;
111                 int created = 0;
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 );
116         }
117
118         // Close the tokeniser
119         mlt_tokeniser_close( tokeniser );
120
121         // Attach the audio and video format converters
122         int created = 0;
123         create_filter( profile, service, "avcolor_space", &created );
124         if ( !created )
125                 create_filter( profile, service, "imageconvert", &created );
126         create_filter( profile, service, "audioconvert", &created );
127 }
128
129 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
130 {
131         mlt_profile profile = NULL;
132         if ( mlt_properties_get( props, "mlt_profile" ) )
133                 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
134         if ( !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" ) );
137
138         if ( nested )
139         {
140                 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
141                 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
142                 char key[30];
143
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 );
148
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 );
155
156                 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
157         }
158         else
159         {
160                 mlt_profile_close( profile );
161         }
162         return nested;
163 }
164
165 static void foreach_consumer_init( mlt_consumer consumer )
166 {
167         const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
168         mlt_properties properties = mlt_properties_parse_yaml( resource );
169         char key[20];
170         int index = 0;
171
172         if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
173         {
174                 // Properties set directly by application
175                 mlt_properties p;
176
177                 if ( properties )
178                         mlt_properties_close( properties );
179                 properties = MLT_CONSUMER_PROPERTIES(consumer);
180                 do {
181                         snprintf( key, sizeof(key), "%d", index );
182                         if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
183                                 generate_consumer( consumer, p, index++ );
184                 } while ( p );
185         }
186         else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
187         {
188                 // YAML file supplied
189                 mlt_properties p;
190
191                 do {
192                         snprintf( key, sizeof(key), "%d", index );
193                         if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
194                                 generate_consumer( consumer, p, index++ );
195                 } while ( p );
196                 mlt_properties_close( properties );
197         }
198         else
199         {
200                 // properties file supplied or properties on this consumer
201                 const char *s;
202
203                 if ( properties )
204                         mlt_properties_close( properties );
205                 if ( resource )
206                         properties = mlt_properties_load( resource );
207                 else
208                         properties = MLT_CONSUMER_PROPERTIES( consumer );
209
210                 do {
211                         snprintf( key, sizeof(key), "%d", index );
212                         if ( ( s = mlt_properties_get( properties, key ) ) )
213                         {
214                                 mlt_properties p = mlt_properties_new();
215                                 int i, count;
216
217                                 if ( !p ) break;
218                                 mlt_properties_set( p, "consumer", mlt_properties_get( properties, key ) );
219                                 snprintf( key, sizeof(key), "%d.", index );
220
221                                 count = mlt_properties_count( properties );
222                                 for ( i = 0; i < count; i++ )
223                                 {
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 ) );
228                                 }
229                                 generate_consumer( consumer, p, index++ );
230                                 mlt_properties_close( p );
231                         }
232                 } while ( s );
233                 if ( resource )
234                         mlt_properties_close( properties );
235         }
236 }
237
238 static void foreach_consumer_start( mlt_consumer consumer )
239 {
240         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
241         mlt_consumer nested = NULL;
242         char key[30];
243         int index = 0;
244
245         do {
246                 snprintf( key, sizeof(key), "%d.consumer", index++ );
247                 nested = mlt_properties_get_data( properties, key, NULL );
248                 if ( nested ) mlt_consumer_start( nested );
249         } while ( nested );
250 }
251
252 static void foreach_consumer_refresh( mlt_consumer consumer )
253 {
254         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
255         mlt_consumer nested = NULL;
256         char key[30];
257         int index = 0;
258
259         do {
260                 snprintf( key, sizeof(key), "%d.consumer", index++ );
261                 nested = mlt_properties_get_data( properties, key, NULL );
262                 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
263         } while ( nested );
264 }
265
266 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
267 {
268         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
269         mlt_consumer nested = NULL;
270         char key[30];
271         int index = 0;
272
273         do {
274                 snprintf( key, sizeof(key), "%d.consumer", index++ );
275                 nested = mlt_properties_get_data( properties, key, NULL );
276                 if ( nested ) mlt_consumer_put_frame( nested, mlt_frame_clone( frame, 0 ) );
277         } while ( nested );
278 }
279
280 static void foreach_consumer_stop( mlt_consumer consumer )
281 {
282         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
283         mlt_consumer nested = NULL;
284         char key[30];
285         int index = 0;
286
287         do {
288                 snprintf( key, sizeof(key), "%d.consumer", index++ );
289                 nested = mlt_properties_get_data( properties, key, NULL );
290                 if ( nested ) mlt_consumer_stop( nested );
291         } while ( nested );
292 }
293
294 /** Start the consumer.
295 */
296
297 static int start( mlt_consumer consumer )
298 {
299         // Check that we're not already running
300         if ( is_stopped( consumer ) )
301         {
302                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
303                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
304
305                 // Assign the thread to properties with automatic dealloc
306                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
307
308                 // Set the running state
309                 mlt_properties_set_int( properties, "running", 1 );
310
311                 // Construct and start nested consumers
312                 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
313                         foreach_consumer_init( consumer );
314                 foreach_consumer_start( consumer );
315
316                 // Create the thread
317                 pthread_create( thread, NULL, consumer_thread, consumer );
318         }
319         return 0;
320 }
321
322 /** Stop the consumer.
323 */
324
325 static int stop( mlt_consumer consumer )
326 {
327         // Check that we're running
328         if ( !is_stopped( consumer ) )
329         {
330                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
331                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
332
333                 // Stop the thread
334                 mlt_properties_set_int( properties, "running", 0 );
335
336                 // Wait for termination
337                 if ( thread )
338                 {
339                         foreach_consumer_refresh( consumer );
340                         pthread_join( *thread, NULL );
341                 }
342
343                 // Stop nested consumers
344                 foreach_consumer_stop( consumer );
345         }
346
347         return 0;
348 }
349
350 /** Determine if the consumer is stopped.
351 */
352
353 static int is_stopped( mlt_consumer consumer )
354 {
355         return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
356 }
357
358 /** The main thread - the argument is simply the consumer.
359 */
360
361 static void *consumer_thread( void *arg )
362 {
363         mlt_consumer consumer = arg;
364         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
365         mlt_frame frame = NULL;
366
367         // Determine whether to stop at end-of-media
368         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
369         int terminated = 0;
370
371         // Loop while running
372         while ( !terminated && !is_stopped( consumer ) )
373         {
374                 // Get the next frame
375                 frame = mlt_consumer_rt_frame( consumer );
376
377                 // Check for termination
378                 if ( terminate_on_pause && frame )
379                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
380
381                 // Check that we have a frame to work with
382                 if ( frame && !terminated && !is_stopped( consumer ) )
383                 {
384                         if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
385                         {
386                                 int dropped = mlt_properties_get_int( properties, "_dropped" );
387                                 mlt_frame_close( frame );
388                                 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
389                                 mlt_properties_set_int( properties, "_dropped", dropped );
390                                 continue;
391                         }
392                         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
393                                 foreach_consumer_refresh( consumer );
394                         foreach_consumer_put( consumer, frame );
395                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
396                         mlt_frame_close( frame );
397                 }
398                 else
399                 {
400                         if ( frame ) mlt_frame_close( frame );
401                         foreach_consumer_put( consumer, NULL );
402                         terminated = 1;
403                 }
404         }
405
406         // Indicate that the consumer is stopped
407         mlt_properties_set_int( properties, "running", 0 );
408         mlt_consumer_stopped( consumer );
409
410         return NULL;
411 }
412
413 /** Close the consumer.
414 */
415
416 static void consumer_close( mlt_consumer consumer )
417 {
418         mlt_consumer_stop( consumer );
419         // Close the parent
420         mlt_consumer_close( consumer );
421         free( consumer );
422 }