]> git.sesse.net Git - mlt/blobdiff - src/modules/core/consumer_multi.c
fix stopping in multi consumer
[mlt] / src / modules / core / consumer_multi.c
index f2916cd797518d007e3b3ceddcf57ae7496140a1..566c95f8f63a267c4744c1ac64a5caba0e2d51b3 100644 (file)
@@ -41,13 +41,21 @@ mlt_consumer consumer_multi_init( mlt_profile profile, mlt_service_type type, co
 
        if ( consumer )
        {
+               mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
+
+               // Set defaults
+               mlt_properties_set( properties, "resource", arg );
+               mlt_properties_set_int( properties, "real_time", -1 );
+               mlt_properties_set_int( properties, "terminate_on_pause", 1 );
+
+               // Init state
+               mlt_properties_set_int( properties, "joined", 1 );
+
                // Assign callbacks
                consumer->close = consumer_close;
                consumer->start = start;
                consumer->stop = stop;
                consumer->is_stopped = is_stopped;
-
-               mlt_properties_set( MLT_CONSUMER_PROPERTIES(consumer), "resource", arg );
        }
 
        return consumer;
@@ -133,7 +141,7 @@ static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties pro
                profile = mlt_profile_init( mlt_properties_get( props, "mlt_profile" ) );
        if ( !profile )
                profile = mlt_profile_clone( mlt_service_profile( MLT_CONSUMER_SERVICE(consumer) ) );
-       mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "consumer" ) );
+       mlt_consumer nested = create_consumer( profile, mlt_properties_get( props, "mlt_service" ) );
 
        if ( nested )
        {
@@ -149,7 +157,8 @@ static mlt_consumer generate_consumer( mlt_consumer consumer, mlt_properties pro
                mlt_properties_set_int( nested_props, "put_mode", 1 );
                mlt_properties_pass_list( nested_props, properties, "terminate_on_pause" );
                mlt_properties_set( props, "consumer", NULL );
-               mlt_properties_set( props, "mlt_profile", NULL );
+               // set mlt_profile before other properties to facilitate presets
+               mlt_properties_pass_list( nested_props, props, "mlt_profile" );
                mlt_properties_inherit( nested_props, props );
 
                attach_normalisers( profile, MLT_CONSUMER_SERVICE(nested) );
@@ -165,12 +174,27 @@ static void foreach_consumer_init( mlt_consumer consumer )
 {
        const char *resource = mlt_properties_get( MLT_CONSUMER_PROPERTIES(consumer), "resource" );
        mlt_properties properties = mlt_properties_parse_yaml( resource );
+       char key[20];
+       int index = 0;
+
+       if ( mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "0", NULL ) )
+       {
+               // Properties set directly by application
+               mlt_properties p;
 
-       if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
+               if ( properties )
+                       mlt_properties_close( properties );
+               properties = MLT_CONSUMER_PROPERTIES(consumer);
+               do {
+                       snprintf( key, sizeof(key), "%d", index );
+                       if ( ( p = mlt_properties_get_data( properties, key, NULL ) ) )
+                               generate_consumer( consumer, p, index++ );
+               } while ( p );
+       }
+       else if ( properties && mlt_properties_get_data( properties, "0", NULL ) )
        {
-               mlt_properties p = NULL;
-               char key[20];
-               int index = 0;
+               // YAML file supplied
+               mlt_properties p;
 
                do {
                        snprintf( key, sizeof(key), "%d", index );
@@ -181,13 +205,15 @@ static void foreach_consumer_init( mlt_consumer consumer )
        }
        else
        {
-               const char *s = NULL;
-               char key[20];
-               int index = 0;
+               // properties file supplied or properties on this consumer
+               const char *s;
 
                if ( properties )
                        mlt_properties_close( properties );
-               properties = mlt_properties_load( resource );
+               if ( resource )
+                       properties = mlt_properties_load( resource );
+               else
+                       properties = MLT_CONSUMER_PROPERTIES( consumer );
 
                do {
                        snprintf( key, sizeof(key), "%d", index );
@@ -197,7 +223,7 @@ static void foreach_consumer_init( mlt_consumer consumer )
                                int i, count;
 
                                if ( !p ) break;
-                               mlt_properties_set( p, "consumer", mlt_properties_get( properties, key ) );
+                               mlt_properties_set( p, "mlt_service", mlt_properties_get( properties, key ) );
                                snprintf( key, sizeof(key), "%d.", index );
 
                                count = mlt_properties_count( properties );
@@ -212,7 +238,8 @@ static void foreach_consumer_init( mlt_consumer consumer )
                                mlt_properties_close( p );
                        }
                } while ( s );
-               mlt_properties_close( properties );
+               if ( resource )
+                       mlt_properties_close( properties );
        }
 }
 
@@ -226,7 +253,14 @@ static void foreach_consumer_start( mlt_consumer consumer )
        do {
                snprintf( key, sizeof(key), "%d.consumer", index++ );
                nested = mlt_properties_get_data( properties, key, NULL );
-               if ( nested ) mlt_consumer_start( nested );
+               if ( nested )
+               {
+                       mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
+                       mlt_properties_set_position( nested_props, "_multi_position", 0 );
+                       mlt_properties_set_data( nested_props, "_multi_audio", NULL, 0, NULL, NULL );
+                       mlt_properties_set_int( nested_props, "_multi_samples", 0 );
+                       mlt_consumer_start( nested );
+               }
        } while ( nested );
 }
 
@@ -254,7 +288,88 @@ static void foreach_consumer_put( mlt_consumer consumer, mlt_frame frame )
        do {
                snprintf( key, sizeof(key), "%d.consumer", index++ );
                nested = mlt_properties_get_data( properties, key, NULL );
-               if ( nested ) mlt_consumer_put_frame( nested, mlt_frame_clone( frame ) );
+               if ( nested )
+               {
+                       mlt_properties nested_props = MLT_CONSUMER_PROPERTIES(nested);
+                       double self_fps = mlt_properties_get_double( properties, "fps" );
+                       double nested_fps = mlt_properties_get_double( nested_props, "fps" );
+                       mlt_position nested_pos = mlt_properties_get_position( nested_props, "_multi_position" );
+                       mlt_position self_pos = mlt_frame_get_position( frame );
+                       double self_time = self_pos / self_fps;
+                       double nested_time = nested_pos / nested_fps;
+
+                       // get the audio for the current frame
+                       uint8_t *buffer = NULL;
+                       mlt_audio_format format = mlt_audio_s16;
+                       int channels = mlt_properties_get_int( properties, "channels" );
+                       int frequency = mlt_properties_get_int( properties, "frequency" );
+                       int current_samples = mlt_sample_calculator( self_fps, frequency, self_pos );
+                       mlt_frame_get_audio( frame, (void**) &buffer, &format, &frequency, &channels, &current_samples );
+                       int current_size = mlt_audio_format_size( format, current_samples, channels );
+
+                       // get any leftover audio
+                       int prev_size = 0;
+                       uint8_t *prev_buffer = mlt_properties_get_data( nested_props, "_multi_audio", &prev_size );
+                       uint8_t *new_buffer = NULL;
+                       if ( prev_size > 0 )
+                       {
+                               new_buffer = mlt_pool_alloc( prev_size + current_size );
+                               memcpy( new_buffer, prev_buffer, prev_size );
+                               memcpy( new_buffer + prev_size, buffer, current_size );
+                               buffer = new_buffer;
+                       }
+                       current_size += prev_size;
+                       current_samples += mlt_properties_get_int( nested_props, "_multi_samples" );
+
+                       while ( nested_time <= self_time )
+                       {
+                               // put ideal number of samples into cloned frame
+                               mlt_frame clone_frame = mlt_frame_clone( frame, 0 );
+                               int nested_samples = mlt_sample_calculator( nested_fps, frequency, nested_pos );
+                               // -10 is an optimization to avoid tiny amounts of leftover samples
+                               nested_samples = nested_samples > current_samples - 10 ? current_samples : nested_samples;
+                               int nested_size = mlt_audio_format_size( format, nested_samples, channels );
+                               if ( nested_size > 0 )
+                               {
+                                       prev_buffer = mlt_pool_alloc( nested_size );
+                                       memcpy( prev_buffer, buffer, nested_size );
+                               }
+                               else
+                               {
+                                       prev_buffer = NULL;
+                                       nested_size = 0;
+                               }
+                               mlt_frame_set_audio( clone_frame, prev_buffer, format, nested_size, mlt_pool_release );
+                               mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_samples", nested_samples );
+                               mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_frequency", frequency );
+                               mlt_properties_set_int( MLT_FRAME_PROPERTIES(clone_frame), "audio_channels", channels );
+
+                               // chomp the audio
+                               current_samples -= nested_samples;
+                               current_size -= nested_size;
+                               buffer += nested_size;
+
+                               // send frame to nested consumer
+                               mlt_consumer_put_frame( nested, clone_frame );
+                               mlt_properties_set_position( nested_props, "_multi_position", ++nested_pos );
+                               nested_time = nested_pos / nested_fps;
+                       }
+
+                       // save any remaining audio
+                       if ( current_size > 0 )
+                       {
+                               prev_buffer = mlt_pool_alloc( current_size );
+                               memcpy( prev_buffer, buffer, current_size );
+                       }
+                       else
+                       {
+                               prev_buffer = NULL;
+                               current_size = 0;
+                       }
+                       mlt_pool_release( new_buffer );
+                       mlt_properties_set_data( nested_props, "_multi_audio", prev_buffer, current_size, mlt_pool_release, NULL );
+                       mlt_properties_set_int( nested_props, "_multi_samples", current_samples );
+               }
        } while ( nested );
 }
 
@@ -288,6 +403,7 @@ static int start( mlt_consumer consumer )
 
                // Set the running state
                mlt_properties_set_int( properties, "running", 1 );
+               mlt_properties_set_int( properties, "joined", 0 );
 
                // Construct and start nested consumers
                if ( !mlt_properties_get_data( properties, "0.consumer", NULL ) )
@@ -306,7 +422,7 @@ static int start( mlt_consumer consumer )
 static int stop( mlt_consumer consumer )
 {
        // Check that we're running
-       if ( !is_stopped( consumer ) )
+       if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES(consumer), "joined" ) )
        {
                mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
                pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
@@ -320,6 +436,7 @@ static int stop( mlt_consumer consumer )
                        foreach_consumer_refresh( consumer );
                        pthread_join( *thread, NULL );
                }
+               mlt_properties_set_int( properties, "joined", 1 );
 
                // Stop nested consumers
                foreach_consumer_stop( consumer );
@@ -362,30 +479,29 @@ static void *consumer_thread( void *arg )
                // Check that we have a frame to work with
                if ( frame && !terminated && !is_stopped( consumer ) )
                {
-                       if ( !mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
+                       if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "rendered" ) )
+                       {
+                               if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
+                                       foreach_consumer_refresh( consumer );
+                               foreach_consumer_put( consumer, frame );
+                               mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
+                       }
+                       else
                        {
                                int dropped = mlt_properties_get_int( properties, "_dropped" );
-                               mlt_frame_close( frame );
                                mlt_log_info( MLT_CONSUMER_SERVICE(consumer), "dropped frame %d\n", ++dropped );
                                mlt_properties_set_int( properties, "_dropped", dropped );
-                               continue;
                        }
-                       if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES(frame), "_speed" ) == 0 )
-                               foreach_consumer_refresh( consumer );
-                       foreach_consumer_put( consumer, frame );
-                       mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
                        mlt_frame_close( frame );
                }
                else
                {
                        if ( frame ) mlt_frame_close( frame );
-                       foreach_consumer_put( consumer, NULL );
                        terminated = 1;
                }
        }
 
        // Indicate that the consumer is stopped
-       mlt_properties_set_int( properties, "running", 0 );
        mlt_consumer_stopped( consumer );
 
        return NULL;
@@ -397,6 +513,7 @@ static void *consumer_thread( void *arg )
 static void consumer_close( mlt_consumer consumer )
 {
        mlt_consumer_stop( consumer );
+
        // Close the parent
        mlt_consumer_close( consumer );
        free( consumer );