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