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