]> git.sesse.net Git - mlt/blob - src/modules/core/consumer_multi.c
074196bbc589deee452311445b2a2cb62a5c88fe
[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                                 int i, count;
263
264                                 if ( !p ) break;
265                                 mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
266                                 snprintf( key, sizeof(key), "%d.", index );
267
268                                 count = mlt_properties_count( properties );
269                                 for ( i = 0; i < count; i++ )
270                                 {
271                                         char *name = mlt_properties_get_name( properties, i );
272                                         if ( !strncmp( name, key, strlen(key) ) )
273                                                 mlt_properties_set( p, name + strlen(key),
274                                                         mlt_properties_get_value( properties, i ) );
275                                 }
276                                 generate_consumer( consumer, p, index++ );
277                                 mlt_properties_close( p );
278                         }
279                 } while ( s );
280                 if ( resource )
281                         mlt_properties_close( properties );
282         }
283 }
284
285 static void foreach_consumer_start( mlt_consumer consumer )
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                         mlt_properties_set_position( nested_props, "_multi_position", 0 );
299                         mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
300                         mlt_properties_set_int( nested_props, "_multi_samples", 0 );
301                         mlt_consumer_start( nested );
302                 }
303         } while ( nested );
304 }
305
306 static void foreach_consumer_refresh( mlt_consumer consumer )
307 {
308         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
309         mlt_consumer nested = NULL;
310         char key[30];
311         int index = 0;
312
313         do {
314                 snprintf( key, sizeof(key), "%d.consumer", index++ );
315                 nested = mlt_properties_get_data( properties, key, NULL );
316                 if ( nested ) mlt_properties_set_int( MLT_CONSUMER_PROPERTIES(nested), "refresh", 1 );
317         } while ( nested );
318 }
319
320 static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
321 {
322         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
323         mlt_consumer nested = NULL;
324         char key[30];
325         int index = 0;
326
327         do {
328                 snprintf( key, sizeof(key), "%d.consumer", index++ );
329                 nested = mlt_properties_get_data( properties, key, NULL );
330                 if ( nested )
331                 {
332                         mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
333                         double self_fps = mlt_properties_get_double( properties, "fps" );
334                         double nested_fps = mlt_properties_get_double( nested_props, "fps" );
335                         mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
336                         mlt_position self_pos = mlt_frame_get_position( frame );
337                         double self_time = self_pos / self_fps;
338                         double nested_time = nested_pos / nested_fps;
339
340                         // get the audio for the current frame
341                         uint8_t *buffer = NULL;
342                         mlt_audio_format format = mlt_audio_s16;
343                         int channels = mlt_properties_get_int( properties, "channels" );
344                         int frequency = mlt_properties_get_int( properties, "frequency" );
345                         int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
346                         mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, &current_samples );
347                         int current_size = mlt_audio_format_size( format, current_samples, channels );
348
349                         // get any leftover audio
350                         int prev_size = 0;
351                         uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
352                         uint8_t *new_buffer = NULL;
353                         if ( prev_size > 0 )
354                         {
355                                 new_buffer = mlt_pool_alloc( prev_size + current_size );
356                                 memcpy( new_buffer, prev_buffer, prev_size );
357                                 memcpy( new_buffer + prev_size, buffer, current_size );
358                                 buffer = new_buffer;
359                         }
360                         current_size += prev_size;
361                         current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
362
363                         while ( nested_time <= self_time )
364                         {
365                                 // put ideal number of samples into cloned frame
366                                 int deeply = index > 1 ? 1 : 0;
367                                 mlt_frame clone_frame = mlt_frame_clone( frame, deeply );
368                                 int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
369                                 // -10 is an optimization to avoid tiny amounts of leftover samples
370                                 nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
371                                 int nested_size = mlt_audio_format_size( format, nested_samples, channels );
372                                 if ( nested_size > 0 )
373                                 {
374                                         prev_buffer = mlt_pool_alloc( nested_size );
375                                         memcpy( prev_buffer, buffer, nested_size );
376                                 }
377                                 else
378                                 {
379                                         prev_buffer = NULL;
380                                         nested_size = 0;
381                                 }
382                                 mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
383                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
384                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
385                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
386
387                                 // chomp the audio
388                                 current_samples -= nested_samples;
389                                 current_size -= nested_size;
390                                 buffer += nested_size;
391
392                                 // send frame to nested consumer
393                                 mlt_consumer_put_frame( nested, clone_frame );
394                                 mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
395                                 nested_time = nested_pos / nested_fps;
396                         }
397
398                         // save any remaining audio
399                         if ( current_size > 0 )
400                         {
401                                 prev_buffer = mlt_pool_alloc( current_size );
402                                 memcpy( prev_buffer, buffer, current_size );
403                         }
404                         else
405                         {
406                                 prev_buffer = NULL;
407                                 current_size = 0;
408                         }
409                         mlt_pool_release( new_buffer );
410                         mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
411                         mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
412                 }
413         } while ( nested );
414 }
415
416 static void foreach_consumer_stop( mlt_consumer consumer )
417 {
418         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
419         mlt_consumer nested = NULL;
420         char key[30];
421         int index = 0;
422         struct timespec tm = { 0, 1000 * 1000 };
423
424         do {
425                 snprintf( key, sizeof(key), "%d.consumer", index++ );
426                 nested = mlt_properties_get_data( properties, key, NULL );
427                 if ( nested )
428                 {
429                         // Let consumer with terminate_on_pause stop on their own
430                         if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(nested), "terminate_on_pause" ) )
431                         {
432                                 // Send additional dummy frame to unlatch nested consumer's threads
433                                 mlt_consumer_put_frame( nested, mlt_frame_init( MLT_CONSUMER_SERVICE(consumer) ) );
434                                 // wait for stop
435                                 while ( !mlt_consumer_is_stopped( nested ) )
436                                         nanosleep( &tm, NULL );
437                         }
438                         else
439                         {
440                                 mlt_consumer_stop( nested );
441                         }
442                 }
443         } while ( nested );
444 }
445
446 /** Start the consumer.
447 */
448
449 static int start( mlt_consumer consumer )
450 {
451         // Check that we're not already running
452         if ( is_stopped( consumer ) )
453         {
454                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
455                 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
456
457                 // Assign the thread to properties with automatic dealloc
458                 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
459
460                 // Set the running state
461                 mlt_properties_set_int( properties, "running", 1 );
462                 mlt_properties_set_int( properties, "joined", 0 );
463
464                 // Construct and start nested consumers
465                 if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
466                         foreach_consumer_init( consumer );
467                 foreach_consumer_start( consumer );
468
469                 // Create the thread
470                 pthread_create( thread, NULL, consumer_thread, consumer );
471         }
472         return 0;
473 }
474
475 /** Stop the consumer.
476 */
477
478 static int stop( mlt_consumer consumer )
479 {
480         // Check that we're running
481         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
482         {
483                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
484                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
485
486                 // Stop the thread
487                 mlt_properties_set_int( properties, "running", 0 );
488
489                 // Wait for termination
490                 if ( thread )
491                 {
492                         foreach_consumer_refresh( consumer );
493                         pthread_join( *thread, NULL );
494                 }
495                 mlt_properties_set_int( properties, "joined", 1 );
496
497                 // Stop nested consumers
498                 foreach_consumer_stop( consumer );
499         }
500
501         return 0;
502 }
503
504 /** Determine if the consumer is stopped.
505 */
506
507 static int is_stopped( mlt_consumer consumer )
508 {
509         return !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "running" );
510 }
511
512 /** Purge each of the child consumers.
513 */
514
515 static void purge( mlt_consumer consumer )
516 {
517         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
518         if ( mlt_properties_get_int( properties, "running" ) )
519         {
520                 mlt_consumer nested = NULL;
521                 char key[30];
522                 int index = 0;
523
524                 do {
525                         snprintf( key, sizeof(key), "%d.consumer", index++ );
526                         nested = mlt_properties_get_data( properties, key, NULL );
527                         if ( nested )
528                                 mlt_consumer_purge( nested );
529                 } while ( nested );
530         }
531 }
532
533 /** The main thread - the argument is simply the consumer.
534 */
535
536 static void *consumer_thread( void *arg )
537 {
538         mlt_consumer consumer = arg;
539         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
540         mlt_frame frame = NULL;
541
542         // Determine whether to stop at end-of-media
543         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
544         int terminated = 0;
545
546         // Loop while running
547         while ( !terminated && !is_stopped( consumer ) )
548         {
549                 // Get the next frame
550                 frame = mlt_consumer_rt_frame( consumer );
551
552                 // Check for termination
553                 if ( terminate_on_pause && frame )
554                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
555
556                 // Check that we have a frame to work with
557                 if ( frame && !terminated && !is_stopped( consumer ) )
558                 {
559                         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
560                         {
561                                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
562                                         foreach_consumer_refresh( consumer );
563                                 foreach_consumer_put( consumer, frame );
564                         }
565                         else
566                         {
567                                 int dropped = mlt_properties_get_int( properties, "_dropped" );
568                                 mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
569                                 mlt_properties_set_int( properties, "_dropped", dropped );
570                         }
571                         mlt_frame_close( frame );
572                 }
573                 else
574                 {
575                         if ( frame && terminated )
576                         {
577                                 // Send this termination frame to nested consumers for their cancellation
578                                 foreach_consumer_put( consumer, frame );
579                         }
580                         if ( frame )
581                                 mlt_frame_close( frame );
582                         terminated = 1;
583                 }
584         }
585
586         // Indicate that the consumer is stopped
587         mlt_consumer_stopped( consumer );
588
589         return NULL;
590 }
591
592 /** Close the consumer.
593 */
594
595 static void consumer_close( mlt_consumer consumer )
596 {
597         mlt_consumer_stop( consumer );
598
599         // Close the parent
600         mlt_consumer_close( consumer );
601         free( consumer );
602 }