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