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