[Jahshaka-cvs] SF.net SVN: openlibraries:[1432] trunk/src/openmedialib/plugins/openal/ openal_plugi
Status: Beta
Brought to you by:
jahshaka
From: <tim...@us...> - 2008-11-10 15:46:00
|
Revision: 1432 http://openlibraries.svn.sourceforge.net/openlibraries/?rev=1432&view=rev Author: timdewhirst Date: 2008-11-10 15:45:52 +0000 (Mon, 10 Nov 2008) Log Message: ----------- olibs * fix for unguarded initialization of openal plugin Modified Paths: -------------- trunk/src/openmedialib/plugins/openal/openal_plugin.cpp Modified: trunk/src/openmedialib/plugins/openal/openal_plugin.cpp =================================================================== --- trunk/src/openmedialib/plugins/openal/openal_plugin.cpp 2008-11-06 10:03:26 UTC (rev 1431) +++ trunk/src/openmedialib/plugins/openal/openal_plugin.cpp 2008-11-10 15:45:52 UTC (rev 1432) @@ -55,32 +55,41 @@ static ALCdevice *device = NULL; static ALCcontext *context = NULL; + static boost::recursive_mutex mutex; + void reflib( int init ) - { - boost::recursive_mutex mutex; + { + boost::recursive_mutex::scoped_lock lock( mutex ); - static long refs = 0; + static long refs = 0; - assert( refs >= 0 && L"openal_plugin::refinit: refs is negative." ); + assert( refs >= 0 && L"openal_plugin::refinit: refs is negative." ); - if( init > 0 && ++refs == 1 ) + if( init > 0 && ++refs == 1 ) + { + assert( context==NULL && L"openal_plugin::refinit: already have context." ); + assert( device==NULL && L"openal_plugin::refinit: already have device." ); + + // Initialise the op + // Initialise the openal libs + device = alcOpenDevice( NULL ); + if( alcGetError( device ) == ALC_NO_ERROR ) + context = alcCreateContext( device, NULL ); + if ( context != NULL ) + alcMakeContextCurrent( context ); + } + else if( init < 0 && --refs == 0 ) + { + // Uninitialise + if ( context != NULL ) { - // Initialise the openal libs - device = alcOpenDevice( NULL ); - if( alcGetError( device ) == ALC_NO_ERROR ) - context = alcCreateContext( device, NULL ); - if ( context != NULL ) - alcMakeContextCurrent( context ); + alcMakeContextCurrent( NULL ); + alcDestroyContext( context ); + context = NULL; + alcCloseDevice( device ); + device = NULL; } - else if( init < 0 && --refs == 0 ) - { - // Uninitialise - if ( context != NULL ) - { - alcDestroyContext( context ); - alcCloseDevice( device ); - } - } + } } } @@ -123,8 +132,8 @@ void play( ) { - ALenum state; - alGetSourcei( source_, AL_SOURCE_STATE, &state ); + ALenum state; + alGetSourcei( source_, AL_SOURCE_STATE, &state ); if ( state != AL_PLAYING ) alSourcePlay( source_ ); } @@ -142,7 +151,7 @@ do { // Determine how many buffers are processed - alGetSourcei( source_, AL_BUFFERS_PROCESSED, &processed ); + alGetSourcei( source_, AL_BUFFERS_PROCESSED, &processed ); // Special case - if we have no buffers processed and no more available // for use, then we need to wait until we recover at least one @@ -152,12 +161,12 @@ while ( processed == 0 && buffers_.size( ) == 0 ); // Unqueue the processed buffers and return them to our pool - while( processed -- ) - { - ALuint buffer; - alSourceUnqueueBuffers( source_, 1, &buffer ); + while( processed -- ) + { + ALuint buffer; + alSourceUnqueueBuffers( source_, 1, &buffer ); buffers_.push_back( buffer ); - } + } } virtual bool init( ) @@ -203,7 +212,7 @@ recover( ); // If we have audio in this frame then schedule for playout - if ( aud != 0 && buffers_.size( ) > 0 ) + if ( aud != 0 && buffers_.size( ) > 0 ) { // TODO: complete mapping from audio type (combination of af and channels) if( aud->channels( ) == 1 ) @@ -222,7 +231,7 @@ for( int i = 0; i < aud->size( ) / sizeof( short ); ++i ) buf[ i ] = ( buf[ i ] >> 8 ) | ( buf[ i ] << 8 ); # endif - alBufferData( *buffers_.begin( ), format_, aud->data( ), aud->size( ), aud->frequency( ) ); + alBufferData( *buffers_.begin( ), format_, aud->data( ), aud->size( ), aud->frequency( ) ); alSourceQueueBuffers( source_, 1, &( *buffers_.begin( ) ) ); buffers_.pop_front( ); } @@ -247,20 +256,20 @@ play( ); // Recover all buffers - ALenum state; - alGetSourcei( source_, AL_SOURCE_STATE, &state ); + ALenum state; + alGetSourcei( source_, AL_SOURCE_STATE, &state ); while ( state == AL_PLAYING && buffers_.size( ) < prop_preroll_.value< int >( ) ) { recover( ); - alGetSourcei( source_, AL_SOURCE_STATE, &state ); + alGetSourcei( source_, AL_SOURCE_STATE, &state ); } alSourceStop( source_ ); } virtual bool empty( ) { - ALenum state; - alGetSourcei( source_, AL_SOURCE_STATE, &state ); + ALenum state; + alGetSourcei( source_, AL_SOURCE_STATE, &state ); return state == AL_PLAYING && buffers_.size( ) > 2; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |