]> git.sesse.net Git - mlt/blobdiff - src/modules/sdl/consumer_sdl_preview.c
the audio also stutters on Linux when paused
[mlt] / src / modules / sdl / consumer_sdl_preview.c
index dc9361156a9a2a2a2b10a3f1c270540cd8ab67b6..bd5fe3661473a8ab2cafc24ad795b6f4974bdc9a 100644 (file)
 #include <framework/mlt_frame.h>
 #include <framework/mlt_factory.h>
 #include <framework/mlt_producer.h>
+#include <framework/mlt_log.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
 #include <SDL.h>
 #include <SDL_syswm.h>
-#include <assert.h>
+#include <sys/time.h>
 
 extern pthread_mutex_t mlt_sdl_mutex;
 
@@ -46,6 +47,7 @@ struct consumer_sdl_s
        int sdl_flags;
        double last_speed;
        mlt_position last_position;
+
        pthread_cond_t refresh_cond;
        pthread_mutex_t refresh_mutex;
        int refresh_count;
@@ -88,9 +90,9 @@ mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type ty
                // Create child consumers
                this->play = mlt_factory_consumer( profile, "sdl", arg );
                this->still = mlt_factory_consumer( profile, "sdl_still", arg );
-               mlt_properties_set( properties, "real_time", "0" );
                mlt_properties_set( properties, "rescale", "nearest" );
                mlt_properties_set( properties, "deinterlace_method", "onefield" );
+               mlt_properties_set_int( properties, "prefill", 1 );
                parent->close = consumer_close;
                parent->start = consumer_start;
                parent->stop = consumer_stop;
@@ -129,7 +131,7 @@ static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *na
        {
                consumer_sdl this = parent->child;
                pthread_mutex_lock( &this->refresh_mutex );
-               this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count ++;
+               this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count + 1;
                pthread_cond_broadcast( &this->refresh_cond );
                pthread_mutex_unlock( &this->refresh_mutex );
        }
@@ -152,7 +154,6 @@ static int consumer_start( mlt_consumer parent )
                char *audio_device = mlt_properties_get( properties, "audio_device" );
                char *output_display = mlt_properties_get( properties, "output_display" );
                int progressive = mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" );
-               int audio_off = mlt_properties_get_int( properties, "audio_off" );
 
                consumer_stop( parent );
 
@@ -187,10 +188,6 @@ static int consumer_start( mlt_consumer parent )
                SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
                SDL_EnableUNICODE( 1 );
 
-               // init audio sub-system (if enabled)
-               if ( !audio_off )
-                       SDL_InitSubSystem( SDL_INIT_AUDIO );
-
                // Pass properties down
                mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
                mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
@@ -200,12 +197,11 @@ static int consumer_start( mlt_consumer parent )
                mlt_properties_set_int( play, "progressive", progressive );
                mlt_properties_set_int( still, "progressive", progressive );
 
-               mlt_properties_pass_list( play, properties, "resize,rescale,width,height,aspect_ratio,display_ratio,volume" );
-               mlt_properties_pass_list( still, properties, "resize,rescale,width,height,aspect_ratio,display_ratio" );
-               mlt_properties_pass_list( play, properties, "deinterlace_method" );
-               mlt_properties_pass_list( still, properties, "deinterlace_method" );
-               mlt_properties_pass_list( play, properties, "preview_off,preview_format,window_background" );
-               mlt_properties_pass_list( still, properties, "preview_off,preview_format,window_background" );
+               mlt_properties_pass_list( play, properties,
+                       "deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background"
+                       ",volume,real_time,buffer,prefill,audio_off,frequency,drop_max" );
+               mlt_properties_pass_list( still, properties,
+                       "deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background" );
 
                mlt_properties_pass( play, properties, "play." );
                mlt_properties_pass( still, properties, "still." );
@@ -227,9 +223,6 @@ static int consumer_start( mlt_consumer parent )
                mlt_properties_set_int( play, "sdl_started", 1 );
                mlt_properties_set_int( still, "sdl_started", 1 );
 
-               // Inform the sdl child consumer to not quit the audio sub-system
-               mlt_properties_set_int( play, "quit_audio_subsystem", 0 );
-
                pthread_create( &this->thread, NULL, consumer_thread, this );
        }
 
@@ -243,9 +236,6 @@ static int consumer_stop( mlt_consumer parent )
 
        if ( this->joined == 0 )
        {
-               // inform child sdl consumer to quit the audio sub-system
-               mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this->play ), "quit_audio_subsystem", 1 );
-
                mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
                int app_locked = mlt_properties_get_int( properties, "app_locked" );
                void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
@@ -259,11 +249,14 @@ static int consumer_stop( mlt_consumer parent )
                pthread_mutex_lock( &this->refresh_mutex );
                pthread_cond_broadcast( &this->refresh_cond );
                pthread_mutex_unlock( &this->refresh_mutex );
+#ifndef WIN32
                if ( this->thread )
+#endif
                        pthread_join( this->thread, NULL );
                this->joined = 1;
 
                if ( app_locked && lock ) lock( );
+               
                pthread_mutex_lock( &mlt_sdl_mutex );
                SDL_Quit( );
                pthread_mutex_unlock( &mlt_sdl_mutex );
@@ -350,9 +343,10 @@ static void *consumer_thread( void *arg )
                        if ( speed != 1 )
                        {
                                mlt_producer producer = MLT_PRODUCER( mlt_service_get_producer( MLT_CONSUMER_SERVICE( consumer ) ) );
-                               mlt_position duration = mlt_producer_get_playtime( producer );
+                               mlt_position duration = producer? mlt_producer_get_playtime( producer ) : -1;
                                int pause = 0;
 
+#ifndef SKIP_WAIT_EOS
                                if ( this->active == this->play )
                                {
                                        // Do not interrupt the play consumer near the end
@@ -367,10 +361,11 @@ static void *consumer_thread( void *arg )
                                        }
                                        else
                                        {
-                                               // Send frame with speed 0 once to stop it
-                                               if ( frame && !eos && speed == 0.0 )
+                                               // Send frame with speed 0 to stop it
+                                               if ( frame && !mlt_consumer_is_stopped( this->play ) )
                                                {
                                                        mlt_consumer_put_frame( this->play, frame );
+                                                       frame = NULL;
                                                        eos = 1;
                                                }
 
@@ -378,11 +373,21 @@ static void *consumer_thread( void *arg )
                                                if ( mlt_consumer_is_stopped( this->play ) )
                                                {
                                                        // Stream has ended
+                                                       mlt_log_verbose( MLT_CONSUMER_SERVICE( consumer ), "END OF STREAM\n" );
                                                        pause = 1;
-                                                       eos = 0; // reset eof indicator
+                                                       eos = 0; // reset eos indicator
+                                               }
+                                               else
+                                               {
+                                                       // Prevent a tight busy loop
+                                                       struct timespec tm = { 0, 100000L }; // 100 usec
+                                                       nanosleep( &tm, NULL );
                                                }
                                        }
                                }
+#else
+                               pause = this->active == this->play;
+#endif
                                if ( pause )
                                {
                                        // Start the still consumer