]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_multi.c
05372009964813406400e6634f5d24dc6b84fd22
[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, "mlt_service" ) );
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, "mlt_service", 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 )
249                 {
250                         mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
251                         mlt_properties_set_position( nested_props, "_multi_position", 0 );
252                         mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
253                         mlt_properties_set_int( nested_props, "_multi_samples", 0 );
254                         mlt_consumer_start( nested );
255                 }
256         } while ( nested );
257 }
258
259 static void foreach_consumer_refresh( mlt_consumer consumer )
260 {
261         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
262         mlt_consumer nested = NULL;
263         char key[30];
264         int index = 0;
265
266         do {
267                 snprintf( key, sizeof(key), "%d.consumer", index++ );
268                 nested = mlt_properties_get_data( properties, key, NULL );
269                 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
270         } while ( nested );
271 }
272
273 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
274 {
275         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
276         mlt_consumer nested = NULL;
277         char key[30];
278         int index = 0;
279
280         do {
281                 snprintf( key, sizeof(key), "%d.consumer", index++ );
282                 nested = mlt_properties_get_data( properties, key, NULL );
283                 if ( nested )
284                 {
285                         mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
286                         double self_fps = mlt_properties_get_double( properties, "fps" );
287                         double nested_fps = mlt_properties_get_double( nested_props, "fps" );
288                         mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
289                         mlt_position self_pos = mlt_frame_get_position( frame );
290                         double self_time = self_pos / self_fps;
291                         double nested_time = nested_pos / nested_fps;
292
293                         // get the audio for the current frame
294                         uint8_t *buffer = NULL;
295                         mlt_audio_format format = mlt_audio_s16;
296                         int channels = mlt_properties_get_int( properties, "channels" );
297                         int frequency = mlt_properties_get_int( properties, "frequency" );
298                         int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
299                         mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, &current_samples );
300                         int current_size = mlt_audio_format_size( format, current_samples, channels );
301
302                         // get any leftover audio
303                         int prev_size = 0;
304                         uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
305                         uint8_t *new_buffer = NULL;
306                         if ( prev_size > 0 )
307                         {
308                                 new_buffer = mlt_pool_alloc( prev_size + current_size );
309                                 memcpy( new_buffer, prev_buffer, prev_size );
310                                 memcpy( new_buffer + prev_size, buffer, current_size );
311                                 buffer = new_buffer;
312                         }
313                         current_size += prev_size;
314                         current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
315
316                         while ( nested_time <= self_time )
317                         {
318                                 // put ideal number of samples into cloned frame
319                                 mlt_frame clone_frame = mlt_frame_clone( frame, 0 );
320                                 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
321                                 // -10 is an optimization to avoid tiny amounts of leftover samples
322                                 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
323                                 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
324                                 if ( nested_size > 0 )
325                                 {
326                                         prev_buffer = mlt_pool_alloc( nested_size );
327                                         memcpy( prev_buffer, buffer, nested_size );
328                                 }
329                                 else
330                                 {
331                                         prev_buffer = NULL;
332                                         nested_size = 0;
333                                 }
334                                 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
335                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
336                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
337                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
338
339                                 // chomp the audio
340                                 current_samples -= nested_samples;
341                                 current_size -= nested_size;
342                                 buffer += nested_size;
343
344                                 // send frame to nested consumer
345                                 mlt_consumer_put_frame( nested, clone_frame );
346                                 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
347                                 nested_time = nested_pos / nested_fps;
348                         }
349
350                         // save any remaining audio
351                         if ( current_size > 0 )
352                         {
353                                 prev_buffer = mlt_pool_alloc( current_size );
354                                 memcpy( prev_buffer, buffer, current_size );
355                         }
356                         else
357                         {
358                                 prev_buffer = NULL;
359                                 current_size = 0;
360                         }
361                         mlt_pool_release( new_buffer );
362                         mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
363                         mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
364                 }
365         } while ( nested );
366 }
367
368 static void foreach_consumer_stop( mlt_consumer consumer )
369 {
370         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
371         mlt_consumer nested = NULL;
372         char key[30];
373         int index = 0;
374
375         do {
376                 snprintf( key, sizeof(key), "%d.consumer", index++ );
377                 nested = mlt_properties_get_data( properties, key, NULL );
378                 if ( nested ) mlt_consumer_stop( nested );
379         } while ( nested );
380 }
381
382 /** Start the consumer.
383 */
384
385 static int start( mlt_consumer consumer )
386 {
387         // Check that we're not already running
388         if ( is_stopped( consumer ) )
389         {
390                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
391                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
392
393                 // Assign the thread to properties with automatic dealloc
394                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
395
396                 // Set the running state
397                 mlt_properties_set_int( properties, "running", 1 );
398
399                 // Construct and start nested consumers
400                 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
401                         foreach_consumer_init( consumer );
402                 foreach_consumer_start( consumer );
403
404                 // Create the thread
405                 pthread_create( thread, NULL, consumer_thread, consumer );
406         }
407         return 0;
408 }
409
410 /** Stop the consumer.
411 */
412
413 static int stop( mlt_consumer consumer )
414 {
415         // Check that we're running
416         if ( !is_stopped( consumer ) )
417         {
418                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
419                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
420
421                 // Stop the thread
422                 mlt_properties_set_int( properties, "running", 0 );
423
424                 // Wait for termination
425                 if ( thread )
426                 {
427                         foreach_consumer_refresh( consumer );
428                         pthread_join( *thread, NULL );
429                 }
430
431                 // Stop nested consumers
432                 foreach_consumer_stop( consumer );
433         }
434
435         return 0;
436 }
437
438 /** Determine if the consumer is stopped.
439 */
440
441 static int is_stopped( mlt_consumer consumer )
442 {
443         return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
444 }
445
446 /** The main thread - the argument is simply the consumer.
447 */
448
449 static void *consumer_thread( void *arg )
450 {
451         mlt_consumer consumer = arg;
452         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
453         mlt_frame frame = NULL;
454
455         // Determine whether to stop at end-of-media
456         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
457         int terminated = 0;
458
459         // Loop while running
460         while ( !terminated && !is_stopped( consumer ) )
461         {
462                 // Get the next frame
463                 frame = mlt_consumer_rt_frame( consumer );
464
465                 // Check for termination
466                 if ( terminate_on_pause && frame )
467                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
468
469                 // Check that we have a frame to work with
470                 if ( frame && !terminated && !is_stopped( consumer ) )
471                 {
472                         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
473                         {
474                                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
475                                         foreach_consumer_refresh( consumer );
476                                 foreach_consumer_put( consumer, frame );
477                                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
478                         }
479                         else
480                         {
481                                 int dropped = mlt_properties_get_int( properties, "_dropped" );
482                                 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
483                                 mlt_properties_set_int( properties, "_dropped", dropped );
484                         }
485                         mlt_frame_close( frame );
486                 }
487                 else
488                 {
489                         if ( frame ) mlt_frame_close( frame );
490                         foreach_consumer_put( consumer, NULL );
491                         terminated = 1;
492                 }
493         }
494
495         // Indicate that the consumer is stopped
496         mlt_properties_set_int( properties, "running", 0 );
497         mlt_consumer_stopped( consumer );
498
499         return NULL;
500 }
501
502 /** Close the consumer.
503 */
504
505 static void consumer_close( mlt_consumer consumer )
506 {
507         mlt_consumer_stop( consumer );
508         // Close the parent
509         mlt_consumer_close( consumer );
510         free( consumer );
511 }