From: Dan Dennedy Date: Wed, 29 Sep 2010 02:22:47 +0000 (-0700) Subject: Fix race conditions in SDL (kdenlive-1711). X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=6a2267990b797e1beba794a197f1f476ce6e33bc;p=mlt Fix race conditions in SDL (kdenlive-1711). Contributed patch by 'jem' - thanks! --- diff --git a/src/modules/sdl/consumer_sdl.c b/src/modules/sdl/consumer_sdl.c index 8d53c19e..56234f23 100644 --- a/src/modules/sdl/consumer_sdl.c +++ b/src/modules/sdl/consumer_sdl.c @@ -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 * * 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; diff --git a/src/modules/sdl/consumer_sdl_audio.c b/src/modules/sdl/consumer_sdl_audio.c index 9550ea6a..508dcc7d 100644 --- a/src/modules/sdl/consumer_sdl_audio.c +++ b/src/modules/sdl/consumer_sdl_audio.c @@ -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 * * This library is free software; you can redistribute it and/or @@ -31,6 +31,8 @@ #include #include +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; diff --git a/src/modules/sdl/consumer_sdl_preview.c b/src/modules/sdl/consumer_sdl_preview.c index 2412acc7..852f6049 100644 --- a/src/modules/sdl/consumer_sdl_preview.c +++ b/src/modules/sdl/consumer_sdl_preview.c @@ -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; diff --git a/src/modules/sdl/consumer_sdl_still.c b/src/modules/sdl/consumer_sdl_still.c index 7653c954..16eced92 100644 --- a/src/modules/sdl/consumer_sdl_still.c +++ b/src/modules/sdl/consumer_sdl_still.c @@ -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 ) {