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