consumer_sdl.c
[melted] / src / modules / sdl / consumer_sdl_still.c
index 418e1c4..9957dd1 100644 (file)
@@ -117,9 +117,7 @@ mlt_consumer consumer_sdl_still_init( char *arg )
                this->window_height = this->height;
 
                // Set the sdl flags
-               //this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
-               // Experimental settings
-               this->sdl_flags = SDL_RESIZABLE | SDL_DOUBLEBUF;
+               this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
 
                // Allow thread to be started/stopped
                parent->start = consumer_start;
@@ -175,6 +173,29 @@ static int consumer_start( mlt_consumer parent )
                        mlt_properties_set_int( this->properties, "height", this->height );
                }
 
+               if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
+               {
+                       if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
+                       {
+                               fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
+                               return -1;
+                       }
+
+                       SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
+                       SDL_EnableUNICODE( 1 );
+               }
+               else
+               {
+                       if ( SDL_GetVideoSurface( ) != NULL )
+                       {
+                               this->sdl_screen = SDL_GetVideoSurface( );
+                               consumer_get_dimensions( &this->window_width, &this->window_height );
+                       }
+               }
+
+               if ( this->sdl_screen == NULL )
+                       this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
+
                pthread_create( &this->thread, NULL, consumer_thread, this );
        }
 
@@ -193,6 +214,11 @@ static int consumer_stop( mlt_consumer parent )
 
                pthread_join( this->thread, NULL );
                this->joined = 1;
+
+               if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
+                       SDL_Quit( );
+
+               this->sdl_screen = NULL;
        }
 
        return 0;
@@ -393,20 +419,17 @@ static int consumer_play_video( consumer_sdl this, mlt_frame frame )
                }
        }
 
-       if ( this->sdl_screen == NULL || changed || mlt_properties_get_int( properties, "changed" ) == 2 )
+       if ( this->sdl_screen == NULL || changed )
        {
                // open SDL window 
                this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
                if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
                        this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
-
                changed = 1;
-               mlt_properties_set_int( properties, "changed", 0 );
        }
        else
        {
                changed = 1;
-               mlt_properties_set_int( properties, "changed", 0 );
        }
 
        if ( changed == 0 &&
@@ -510,28 +533,6 @@ static void *consumer_thread( void *arg )
        // internal intialization
        mlt_frame frame = NULL;
 
-       if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
-       {
-               if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
-               {
-                       fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
-                       return NULL;
-               }
-
-               SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
-               SDL_EnableUNICODE( 1 );
-       }
-       else
-       {
-               mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "changed", 2 );
-               if ( SDL_GetVideoSurface( ) != NULL )
-               {
-                       this->sdl_screen = SDL_GetVideoSurface( );
-                       consumer_get_dimensions( &this->window_width, &this->window_height );
-                       mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "changed", 0 );
-               }
-       }
-
        // Loop until told not to
        while( this->running )
        {
@@ -539,22 +540,18 @@ static void *consumer_thread( void *arg )
                frame = mlt_consumer_rt_frame( consumer );
 
                // Ensure that we have a frame
-               if ( frame != NULL )
+               if ( this->running && frame != NULL )
                {
                        consumer_play_video( this, frame );
                        mlt_frame_close( frame );
                }
                else
                {
+                       if ( frame ) mlt_frame_close( frame );
                        this->running = 0;
                }
        }
 
-       if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
-               SDL_Quit( );
-
-       this->sdl_screen = NULL;
-
        return NULL;
 }