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