]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_multi.c
fix integrity of images with 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 #include <sys/time.h>
26
27 // Forward references
28 static int start( mlt_consumer consumer );
29 static int stop( mlt_consumer consumer );
30 static int is_stopped( mlt_consumer consumer );
31 static void *consumer_thread( void *arg );
32 static void consumer_close( mlt_consumer consumer );
33
34 static mlt_properties normalisers = NULL;
35
36 /** Initialise the consumer.
37 */
38
39 mlt_consumer consumer_multi_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
40 {
41         mlt_consumer consumer = mlt_consumer_new( profile );
42
43         if ( consumer )
44         {
45                 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
46
47                 // Set defaults
48                 mlt_properties_set( properties, "resource", arg );
49                 mlt_properties_set_int( properties, "real_time", -1 );
50                 mlt_properties_set_int( properties, "terminate_on_pause", 1 );
51
52                 // Init state
53                 mlt_properties_set_int( properties, "joined", 1 );
54
55                 // Assign callbacks
56                 consumer->close = consumer_close;
57                 consumer->start = start;
58                 consumer->stop = stop;
59                 consumer->is_stopped = is_stopped;
60         }
61
62         return consumer;
63 }
64
65 static mlt_consumer create_consumer( mlt_profile profile, char *id, char *arg )
66 {
67         char *myid = id ? strdup( id ) : NULL;
68         char *myarg = ( myid && !arg ) ? strchr( myid, ':' ) : NULL;
69         if ( myarg )
70                 *myarg ++ = '\0';
71         else
72                 myarg = arg;
73         mlt_consumer consumer = mlt_factory_consumer( profile, myid, myarg );
74         if ( myid )
75                 free( myid );
76         return consumer;
77 }
78
79 static void create_filter( mlt_profile profile, mlt_service service, char *effect, int *created )
80 {
81         char *id = strdup( effect );
82         char *arg = strchr( id, ':' );
83         if ( arg != NULL )
84                 *arg ++ = '\0';
85
86         // The swscale and avcolor_space filters require resolution as arg to test compatibility
87         if ( strncmp( effect, "swscale", 7 ) == 0 || strncmp( effect, "avcolo", 6 ) == 0 )
88                 arg = (char*) mlt_properties_get_int( MLT_SERVICE_PROPERTIES( service ), "meta.media.width" );
89
90         mlt_filter filter = mlt_factory_filter( profile, id, arg );
91         if ( filter != NULL )
92         {
93                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "_loader", 1 );
94                 mlt_service_attach( service, filter );
95                 mlt_filter_close( filter );
96                 *created = 1;
97         }
98         free( id );
99 }
100
101 static void attach_normalisers( mlt_profile profile, mlt_service service )
102 {
103         // Loop variable
104         int i;
105
106         // Tokeniser
107         mlt_tokeniser tokeniser = mlt_tokeniser_init( );
108
109         // We only need to load the normalising properties once
110         if ( normalisers == NULL )
111         {
112                 char temp[ 1024 ];
113                 snprintf( temp, sizeof(temp), "%s/core/loader.ini", mlt_environment( "MLT_DATA" ) );
114                 normalisers = mlt_properties_load( temp );
115                 mlt_factory_register_for_clean_up( normalisers, ( mlt_destructor )mlt_properties_close );
116         }
117
118         // Apply normalisers
119         for ( i = 0; i < mlt_properties_count( normalisers ); i ++ )
120         {
121                 int j = 0;
122                 int created = 0;
123                 char *value = mlt_properties_get_value( normalisers, i );
124                 mlt_tokeniser_parse_new( tokeniser, value, "," );
125                 for ( j = 0; !created && j < mlt_tokeniser_count( tokeniser ); j ++ )
126                         create_filter( profile, service, mlt_tokeniser_get_string( tokeniser, j ), &created );
127         }
128
129         // Close the tokeniser
130         mlt_tokeniser_close( tokeniser );
131
132         // Attach the audio and video format converters
133         int created = 0;
134         create_filter( profile, service, "avcolor_space", &created );
135         if ( !created )
136                 create_filter( profile, service, "imageconvert", &created );
137         create_filter( profile, service, "audioconvert", &created );
138 }
139
140 static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties props, int index )
141 {
142         mlt_profile profile = NULL;
143         if ( mlt_properties_get( props, "mlt_profile" ) )
144                 profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
145         if ( !profile )
146                 profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
147         mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ),
148                 mlt_properties_get( props, "target" ) );
149
150         if ( nested )
151         {
152                 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
153                 mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
154                 char key[30];
155
156                 snprintf( key, sizeof(key), "%d.consumer", index );
157                 mlt_properties_set_data( properties, key, nested, 0, (mlt_destructor) mlt_consumer_close, NULL );
158                 snprintf( key, sizeof(key), "%d.profile", index );
159                 mlt_properties_set_data( properties, key, profile, 0, (mlt_destructor) mlt_profile_close, NULL );
160
161                 mlt_properties_set_int( nested_props, "put_mode", 1 );
162                 mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
163                 mlt_properties_set( props, "consumer", NULL );
164                 // set mlt_profile before other properties to facilitate presets
165                 mlt_properties_pass_list( nested_props, props, "mlt_profile" );
166                 mlt_properties_inherit( nested_props, props );
167
168                 attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
169         }
170         else
171         {
172                 mlt_profile_close( profile );
173         }
174         return nested;
175 }
176
177 static void foreach_consumer_init( mlt_consumer consumer )
178 {
179         const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
180         mlt_properties properties = mlt_properties_parse_yaml( resource );
181         char key[20];
182         int index = 0;
183
184         if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
185         {
186                 // Properties set directly by application
187                 mlt_properties p;
188
189                 if ( properties )
190                         mlt_properties_close( properties );
191                 properties = MLT_CONSUMER_PROPERTIES(consumer);
192                 do {
193                         snprintf( key, sizeof(key), "%d", index );
194                         if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
195                                 generate_consumer( consumer, p, index++ );
196                 } while ( p );
197         }
198         else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
199         {
200                 // YAML file supplied
201                 mlt_properties p;
202
203                 do {
204                         snprintf( key, sizeof(key), "%d", index );
205                         if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
206                                 generate_consumer( consumer, p, index++ );
207                 } while ( p );
208                 mlt_properties_close( properties );
209         }
210         else
211         {
212                 // properties file supplied or properties on this consumer
213                 const char *s;
214
215                 if ( properties )
216                         mlt_properties_close( properties );
217                 if ( resource )
218                         properties = mlt_properties_load( resource );
219                 else
220                         properties = MLT_CONSUMER_PROPERTIES( consumer );
221
222                 do {
223                         snprintf( key, sizeof(key), "%d", index );
224                         if ( ( s = mlt_properties_get( properties, key ) ) )
225                         {
226                                 mlt_properties p = mlt_properties_new();
227                                 int i, count;
228
229                                 if ( !p ) break;
230                                 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
231                                 snprintf( key, sizeof(key), "%d.", index );
232
233                                 count = mlt_properties_count( properties );
234                                 for ( i = 0; i < count; i++ )
235                                 {
236                                         char *name = mlt_properties_get_name( properties, i );
237                                         if ( !strncmp( name, key, strlen(key) ) )
238                                                 mlt_properties_set( p, name + strlen(key),
239                                                         mlt_properties_get_value( properties, i ) );
240                                 }
241                                 generate_consumer( consumer, p, index++ );
242                                 mlt_properties_close( p );
243                         }
244                 } while ( s );
245                 if ( resource )
246                         mlt_properties_close( properties );
247         }
248 }
249
250 static void foreach_consumer_start( mlt_consumer consumer )
251 {
252         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
253         mlt_consumer nested = NULL;
254         char key[30];
255         int index = 0;
256
257         do {
258                 snprintf( key, sizeof(key), "%d.consumer", index++ );
259                 nested = mlt_properties_get_data( properties, key, NULL );
260                 if ( nested )
261                 {
262                         mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
263                         mlt_properties_set_position( nested_props, "_multi_position", 0 );
264                         mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
265                         mlt_properties_set_int( nested_props, "_multi_samples", 0 );
266                         mlt_consumer_start( nested );
267                 }
268         } while ( nested );
269 }
270
271 static void foreach_consumer_refresh( mlt_consumer consumer )
272 {
273         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
274         mlt_consumer nested = NULL;
275         char key[30];
276         int index = 0;
277
278         do {
279                 snprintf( key, sizeof(key), "%d.consumer", index++ );
280                 nested = mlt_properties_get_data( properties, key, NULL );
281                 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
282         } while ( nested );
283 }
284
285 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
286 {
287         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
288         mlt_consumer nested = NULL;
289         char key[30];
290         int index = 0;
291
292         do {
293                 snprintf( key, sizeof(key), "%d.consumer", index++ );
294                 nested = mlt_properties_get_data( properties, key, NULL );
295                 if ( nested )
296                 {
297                         mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
298                         double self_fps = mlt_properties_get_double( properties, "fps" );
299                         double nested_fps = mlt_properties_get_double( nested_props, "fps" );
300                         mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
301                         mlt_position self_pos = mlt_frame_get_position( frame );
302                         double self_time = self_pos / self_fps;
303                         double nested_time = nested_pos / nested_fps;
304
305                         // get the audio for the current frame
306                         uint8_t *buffer = NULL;
307                         mlt_audio_format format = mlt_audio_s16;
308                         int channels = mlt_properties_get_int( properties, "channels" );
309                         int frequency = mlt_properties_get_int( properties, "frequency" );
310                         int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
311                         mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, &current_samples );
312                         int current_size = mlt_audio_format_size( format, current_samples, channels );
313
314                         // get any leftover audio
315                         int prev_size = 0;
316                         uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
317                         uint8_t *new_buffer = NULL;
318                         if ( prev_size > 0 )
319                         {
320                                 new_buffer = mlt_pool_alloc( prev_size + current_size );
321                                 memcpy( new_buffer, prev_buffer, prev_size );
322                                 memcpy( new_buffer + prev_size, buffer, current_size );
323                                 buffer = new_buffer;
324                         }
325                         current_size += prev_size;
326                         current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
327
328                         while ( nested_time <= self_time )
329                         {
330                                 // put ideal number of samples into cloned frame
331                                 int deeply = index > 1 ? 1 : 0;
332                                 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
333                                 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
334                                 // -10 is an optimization to avoid tiny amounts of leftover samples
335                                 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
336                                 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
337                                 if ( nested_size > 0 )
338                                 {
339                                         prev_buffer = mlt_pool_alloc( nested_size );
340                                         memcpy( prev_buffer, buffer, nested_size );
341                                 }
342                                 else
343                                 {
344                                         prev_buffer = NULL;
345                                         nested_size = 0;
346                                 }
347                                 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
348                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
349                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
350                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
351
352                                 // chomp the audio
353                                 current_samples -= nested_samples;
354                                 current_size -= nested_size;
355                                 buffer += nested_size;
356
357                                 // send frame to nested consumer
358                                 mlt_consumer_put_frame( nested, clone_frame );
359                                 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
360                                 nested_time = nested_pos / nested_fps;
361                         }
362
363                         // save any remaining audio
364                         if ( current_size > 0 )
365                         {
366                                 prev_buffer = mlt_pool_alloc( current_size );
367                                 memcpy( prev_buffer, buffer, current_size );
368                         }
369                         else
370                         {
371                                 prev_buffer = NULL;
372                                 current_size = 0;
373                         }
374                         mlt_pool_release( new_buffer );
375                         mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
376                         mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
377                 }
378         } while ( nested );
379 }
380
381 static void foreach_consumer_stop( mlt_consumer consumer )
382 {
383         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
384         mlt_consumer nested = NULL;
385         char key[30];
386         int index = 0;
387         struct timespec tm = { 0, 1000 * 1000 };
388
389         do {
390                 snprintf( key, sizeof(key), "%d.consumer", index++ );
391                 nested = mlt_properties_get_data( properties, key, NULL );
392                 if ( nested )
393                 {
394                         // Let consumer with terminate_on_pause stop on their own
395                         if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
396                         {
397                                 // Send additional dummy frame to unlatch nested consumer's threads
398                                 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
399                                 // wait for stop
400                                 while ( !mlt_consumer_is_stopped( nested ) )
401                                         nanosleep( &tm, NULL );
402                         }
403                         else
404                         {
405                                 mlt_consumer_stop( nested );
406                         }
407                 }
408         } while ( nested );
409 }
410
411 /** Start the consumer.
412 */
413
414 static int start( mlt_consumer consumer )
415 {
416         // Check that we're not already running
417         if ( is_stopped( consumer ) )
418         {
419                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
420                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
421
422                 // Assign the thread to properties with automatic dealloc
423                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
424
425                 // Set the running state
426                 mlt_properties_set_int( properties, "running", 1 );
427                 mlt_properties_set_int( properties, "joined", 0 );
428
429                 // Construct and start nested consumers
430                 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
431                         foreach_consumer_init( consumer );
432                 foreach_consumer_start( consumer );
433
434                 // Create the thread
435                 pthread_create( thread, NULL, consumer_thread, consumer );
436         }
437         return 0;
438 }
439
440 /** Stop the consumer.
441 */
442
443 static int stop( mlt_consumer consumer )
444 {
445         // Check that we're running
446         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
447         {
448                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
449                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
450
451                 // Stop the thread
452                 mlt_properties_set_int( properties, "running", 0 );
453
454                 // Wait for termination
455                 if ( thread )
456                 {
457                         foreach_consumer_refresh( consumer );
458                         pthread_join( *thread, NULL );
459                 }
460                 mlt_properties_set_int( properties, "joined", 1 );
461
462                 // Stop nested consumers
463                 foreach_consumer_stop( consumer );
464         }
465
466         return 0;
467 }
468
469 /** Determine if the consumer is stopped.
470 */
471
472 static int is_stopped( mlt_consumer consumer )
473 {
474         return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
475 }
476
477 /** The main thread - the argument is simply the consumer.
478 */
479
480 static void *consumer_thread( void *arg )
481 {
482         mlt_consumer consumer = arg;
483         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
484         mlt_frame frame = NULL;
485
486         // Determine whether to stop at end-of-media
487         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
488         int terminated = 0;
489
490         // Loop while running
491         while ( !terminated && !is_stopped( consumer ) )
492         {
493                 // Get the next frame
494                 frame = mlt_consumer_rt_frame( consumer );
495
496                 // Check for termination
497                 if ( terminate_on_pause && frame )
498                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
499
500                 // Check that we have a frame to work with
501                 if ( frame && !terminated && !is_stopped( consumer ) )
502                 {
503                         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
504                         {
505                                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
506                                         foreach_consumer_refresh( consumer );
507                                 foreach_consumer_put( consumer, frame );
508                                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
509                         }
510                         else
511                         {
512                                 int dropped = mlt_properties_get_int( properties, "_dropped" );
513                                 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
514                                 mlt_properties_set_int( properties, "_dropped", dropped );
515                         }
516                         mlt_frame_close( frame );
517                 }
518                 else
519                 {
520                         if ( frame && terminated )
521                         {
522                                 // Send this termination frame to nested consumers for their cancellation
523                                 foreach_consumer_put( consumer, frame );
524                                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
525                         }
526                         if ( frame )
527                                 mlt_frame_close( frame );
528                         terminated = 1;
529                 }
530         }
531
532         // Indicate that the consumer is stopped
533         mlt_consumer_stopped( consumer );
534
535         return NULL;
536 }
537
538 /** Close the consumer.
539 */
540
541 static void consumer_close( mlt_consumer consumer )
542 {
543         mlt_consumer_stop( consumer );
544
545         // Close the parent
546         mlt_consumer_close( consumer );
547         free( consumer );
548 }