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