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