]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_multi.c
make sure nested consumer gets mlt_profile before 'properties' preset
[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
170         if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
171         {
172                 // YAML file supplied
173                 mlt_properties p = NULL;
174                 char key[20];
175                 int index = 0;
176
177                 do {
178                         snprintf( key, sizeof(key), "%d", index );
179                         if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
180                                 generate_consumer( consumer, p, index++ );
181                 } while ( p );
182                 mlt_properties_close( properties );
183         }
184         else
185         {
186                 // properties file supplied or properties on this consumer
187                 const char *s = NULL;
188                 char key[20];
189                 int index = 0;
190
191                 if ( properties )
192                         mlt_properties_close( properties );
193                 if ( resource )
194                         properties = mlt_properties_load( resource );
195                 else
196                         properties = MLT_CONSUMER_PROPERTIES( consumer );
197
198                 do {
199                         snprintf( key, sizeof(key), "%d", index );
200                         if ( ( s = mlt_properties_get( properties, key ) ) )
201                         {
202                                 mlt_properties p = mlt_properties_new();
203                                 int i, count;
204
205                                 if ( !p ) break;
206                                 mlt_properties_set( p, "consumer", mlt_properties_get( properties, key ) );
207                                 snprintf( key, sizeof(key), "%d.", index );
208
209                                 count = mlt_properties_count( properties );
210                                 for ( i = 0; i < count; i++ )
211                                 {
212                                         char *name = mlt_properties_get_name( properties, i );
213                                         if ( !strncmp( name, key, strlen(key) ) )
214                                                 mlt_properties_set( p, name + strlen(key),
215                                                         mlt_properties_get_value( properties, i ) );
216                                 }
217                                 generate_consumer( consumer, p, index++ );
218                                 mlt_properties_close( p );
219                         }
220                 } while ( s );
221                 if ( resource )
222                         mlt_properties_close( properties );
223         }
224 }
225
226 static void foreach_consumer_start( mlt_consumer consumer )
227 {
228         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
229         mlt_consumer nested = NULL;
230         char key[30];
231         int index = 0;
232
233         do {
234                 snprintf( key, sizeof(key), "%d.consumer", index++ );
235                 nested = mlt_properties_get_data( properties, key, NULL );
236                 if ( nested ) mlt_consumer_start( nested );
237         } while ( nested );
238 }
239
240 static void foreach_consumer_refresh( mlt_consumer consumer )
241 {
242         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
243         mlt_consumer nested = NULL;
244         char key[30];
245         int index = 0;
246
247         do {
248                 snprintf( key, sizeof(key), "%d.consumer", index++ );
249                 nested = mlt_properties_get_data( properties, key, NULL );
250                 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
251         } while ( nested );
252 }
253
254 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
255 {
256         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
257         mlt_consumer nested = NULL;
258         char key[30];
259         int index = 0;
260
261         do {
262                 snprintf( key, sizeof(key), "%d.consumer", index++ );
263                 nested = mlt_properties_get_data( properties, key, NULL );
264                 if ( nested ) mlt_consumer_put_frame( nested, mlt_frame_clone( frame ) );
265         } while ( nested );
266 }
267
268 static void foreach_consumer_stop( mlt_consumer consumer )
269 {
270         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
271         mlt_consumer nested = NULL;
272         char key[30];
273         int index = 0;
274
275         do {
276                 snprintf( key, sizeof(key), "%d.consumer", index++ );
277                 nested = mlt_properties_get_data( properties, key, NULL );
278                 if ( nested ) mlt_consumer_stop( nested );
279         } while ( nested );
280 }
281
282 /** Start the consumer.
283 */
284
285 static int start( mlt_consumer consumer )
286 {
287         // Check that we're not already running
288         if ( is_stopped( consumer ) )
289         {
290                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
291                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
292
293                 // Assign the thread to properties with automatic dealloc
294                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
295
296                 // Set the running state
297                 mlt_properties_set_int( properties, "running", 1 );
298
299                 // Construct and start nested consumers
300                 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
301                         foreach_consumer_init( consumer );
302                 foreach_consumer_start( consumer );
303
304                 // Create the thread
305                 pthread_create( thread, NULL, consumer_thread, consumer );
306         }
307         return 0;
308 }
309
310 /** Stop the consumer.
311 */
312
313 static int stop( mlt_consumer consumer )
314 {
315         // Check that we're running
316         if ( !is_stopped( consumer ) )
317         {
318                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
319                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
320
321                 // Stop the thread
322                 mlt_properties_set_int( properties, "running", 0 );
323
324                 // Wait for termination
325                 if ( thread )
326                 {
327                         foreach_consumer_refresh( consumer );
328                         pthread_join( *thread, NULL );
329                 }
330
331                 // Stop nested consumers
332                 foreach_consumer_stop( consumer );
333         }
334
335         return 0;
336 }
337
338 /** Determine if the consumer is stopped.
339 */
340
341 static int is_stopped( mlt_consumer consumer )
342 {
343         return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
344 }
345
346 /** The main thread - the argument is simply the consumer.
347 */
348
349 static void *consumer_thread( void *arg )
350 {
351         mlt_consumer consumer = arg;
352         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
353         mlt_frame frame = NULL;
354
355         // Determine whether to stop at end-of-media
356         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
357         int terminated = 0;
358
359         // Loop while running
360         while ( !terminated && !is_stopped( consumer ) )
361         {
362                 // Get the next frame
363                 frame = mlt_consumer_rt_frame( consumer );
364
365                 // Check for termination
366                 if ( terminate_on_pause && frame )
367                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
368
369                 // Check that we have a frame to work with
370                 if ( frame && !terminated && !is_stopped( consumer ) )
371                 {
372                         if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
373                         {
374                                 int dropped = mlt_properties_get_int( properties, "_dropped" );
375                                 mlt_frame_close( frame );
376                                 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
377                                 mlt_properties_set_int( properties, "_dropped", dropped );
378                                 continue;
379                         }
380                         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
381                                 foreach_consumer_refresh( consumer );
382                         foreach_consumer_put( consumer, frame );
383                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
384                         mlt_frame_close( frame );
385                 }
386                 else
387                 {
388                         if ( frame ) mlt_frame_close( frame );
389                         foreach_consumer_put( consumer, NULL );
390                         terminated = 1;
391                 }
392         }
393
394         // Indicate that the consumer is stopped
395         mlt_properties_set_int( properties, "running", 0 );
396         mlt_consumer_stopped( consumer );
397
398         return NULL;
399 }
400
401 /** Close the consumer.
402 */
403
404 static void consumer_close( mlt_consumer consumer )
405 {
406         mlt_consumer_stop( consumer );
407         // Close the parent
408         mlt_consumer_close( consumer );
409         free( consumer );
410 }