]> git.sesse.net Git - mlt/commitdiff
Fix race conditions in SDL (kdenlive-1711).
authorDan Dennedy <dan@dennedy.org>
Wed, 29 Sep 2010 02:22:47 +0000 (19:22 -0700)
committerDan Dennedy <dan@dennedy.org>
Wed, 29 Sep 2010 02:22:47 +0000 (19:22 -0700)
Contributed patch by 'jem' - thanks!

src/modules/sdl/consumer_sdl.c
src/modules/sdl/consumer_sdl_audio.c
src/modules/sdl/consumer_sdl_preview.c
src/modules/sdl/consumer_sdl_still.c

index 8d53c19e8779e765e34d3e1c0ba944cf9650536d..56234f238a730982da9a73ab3c991b5de286524a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * consumer_sdl.c -- A Simple DirectMedia Layer consumer
- * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
+ * Copyright (C) 2003-2004, 2010 Ushodaya Enterprises Limited
  * Author: Dan Dennedy <dan@dennedy.org>
  *
  * This library is free software; you can redistribute it and/or
@@ -188,7 +188,10 @@ int consumer_start( mlt_consumer parent )
 
                if ( sdl_started == 0 && display_off == 0 )
                {
-                       if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
+                       pthread_mutex_lock( &mlt_sdl_mutex );
+                       int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
+                       pthread_mutex_unlock( &mlt_sdl_mutex );
+                       if ( ret < 0 )
                        {
                                mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
                                return -1;
@@ -199,7 +202,9 @@ int consumer_start( mlt_consumer parent )
                }
                else if ( display_off == 0 )
                {
+                       pthread_mutex_lock( &mlt_sdl_mutex );
                        this->sdl_screen = SDL_GetVideoSurface( );
+                       pthread_mutex_unlock( &mlt_sdl_mutex );
                }
 
                if ( audio_off == 0 )
@@ -214,7 +219,10 @@ int consumer_start( mlt_consumer parent )
                {
                        if ( mlt_properties_get_int( this->properties, "fullscreen" ) )
                        {
-                               const SDL_VideoInfo *vi = SDL_GetVideoInfo();
+                               const SDL_VideoInfo *vi;
+                               pthread_mutex_lock( &mlt_sdl_mutex );
+                               vi = SDL_GetVideoInfo();
+                               pthread_mutex_unlock( &mlt_sdl_mutex );
                                this->window_width = vi->current_w;
                                this->window_height = vi->current_h;
                                this->sdl_flags |= SDL_FULLSCREEN;
index 9550ea6a45814c303a52312f1dd94b03a8e5fefe..508dcc7d0afefccf5ae2d7369c55a9768154596d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * consumer_sdl_audio.c -- A Simple DirectMedia Layer audio-only consumer
- * Copyright (C) 2009 Ushodaya Enterprises Limited
+ * Copyright (C) 2009, 2010 Ushodaya Enterprises Limited
  * Author: Dan Dennedy <dan@dennedy.org>
  *
  * This library is free software; you can redistribute it and/or
@@ -31,6 +31,8 @@
 #include <SDL/SDL.h>
 #include <sys/time.h>
 
+extern pthread_mutex_t mlt_sdl_mutex;
+
 /** This classes definition.
 */
 
@@ -158,7 +160,11 @@ int consumer_start( mlt_consumer parent )
                this->running = 1;
                this->joined = 0;
 
-               if ( SDL_Init( SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
+
+               pthread_mutex_lock( &mlt_sdl_mutex );
+               int ret = SDL_Init( SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE );
+               pthread_mutex_unlock( &mlt_sdl_mutex );
+               if ( ret < 0 )
                {
                        mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
                        return -1;
index 2412acc7358c6c3832faf41a3cc73aaad9ae7f42..852f6049189eb2a78fdb854153a7f987f65cabd0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
- * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
+ * Copyright (C) 2004-2005, 2010 Ushodaya Enterprises Limited
  * Author: Charles Yates
  *
  * This library is free software; you can redistribute it and/or
@@ -174,7 +174,10 @@ static int consumer_start( mlt_consumer parent )
                if ( audio_device != NULL )
                        setenv( "AUDIODEV", audio_device, 1 );
 
-               if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
+               pthread_mutex_lock( &mlt_sdl_mutex );
+               int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
+               pthread_mutex_unlock( &mlt_sdl_mutex );
+               if ( ret < 0 )
                {
                        fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
                        return -1;
index 7653c954b463f10aad289bc934a945f229d51fa3..16eced9289c50461ab39fc5727b7fa0614607f86 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * consumer_sdl_still.c -- A Simple DirectMedia Layer consumer
- * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
+ * Copyright (C) 2003-2004, 2010 Ushodaya Enterprises Limited
  * Author: Charles Yates
  *
  * This library is free software; you can redistribute it and/or
@@ -168,7 +168,10 @@ static int consumer_start( mlt_consumer parent )
 
                if ( sdl_started == 0 && preview_off == 0 )
                {
-                       if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
+                       pthread_mutex_lock( &mlt_sdl_mutex );
+                       int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
+                       pthread_mutex_unlock( &mlt_sdl_mutex );
+                       if ( ret < 0 )
                        {
                                fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
                                return -1;
@@ -179,9 +182,12 @@ static int consumer_start( mlt_consumer parent )
                }
                else if ( preview_off == 0 )
                {
-                       if ( SDL_GetVideoSurface( ) != NULL )
+                       pthread_mutex_lock( &mlt_sdl_mutex );
+                       SDL_Surface *screen = SDL_GetVideoSurface( );
+                       pthread_mutex_unlock( &mlt_sdl_mutex );
+                       if ( screen != NULL )
                        {
-                               this->sdl_screen = SDL_GetVideoSurface( );
+                               this->sdl_screen = screen;
                        }
                }
 
@@ -505,7 +511,10 @@ static int consumer_play_video( consumer_sdl this, mlt_frame frame )
                mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
        }
        
-       if ( !mlt_consumer_is_stopped( &this->parent ) && SDL_GetVideoSurface( ) != NULL && this->sdl_screen != NULL && this->sdl_screen->pixels != NULL )
+       pthread_mutex_lock( &mlt_sdl_mutex );
+       SDL_Surface *screen = SDL_GetVideoSurface( );
+       pthread_mutex_unlock( &mlt_sdl_mutex );
+       if ( !mlt_consumer_is_stopped( &this->parent ) && screen != NULL && this->sdl_screen != NULL && this->sdl_screen->pixels != NULL )
        {
                switch( this->sdl_screen->format->BytesPerPixel )
                {