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