diff options
author | Joerg Mueller <nexyon@gmail.com> | 2012-10-25 01:33:44 +0400 |
---|---|---|
committer | Joerg Mueller <nexyon@gmail.com> | 2012-10-25 01:33:44 +0400 |
commit | e66ee1ca9f15e1994811609ec803e856ddef9d8c (patch) | |
tree | 12a93a4dc9f954176f9e4964c7f07d88942f65e6 /intern/audaspace/OpenAL | |
parent | 05f563339be4eb7525751a1065b1254a027c1333 (diff) |
Audaspace:
RAII locking implementation. This should fix bug [#32096] Background music stops when playing 3D sounds.
Diffstat (limited to 'intern/audaspace/OpenAL')
-rw-r--r-- | intern/audaspace/OpenAL/AUD_OpenALDevice.cpp | 282 |
1 files changed, 153 insertions, 129 deletions
diff --git a/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp b/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp index f68d41f6a1c..c7d9612020b 100644 --- a/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp +++ b/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp @@ -31,6 +31,7 @@ #include "AUD_IFactory.h" #include "AUD_IReader.h" #include "AUD_ConverterReader.h" +#include "AUD_MutexLock.h" #include <cstring> #include <limits> @@ -125,7 +126,7 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::pause() { if(m_status) { - m_device->lock(); + AUD_MutexLock lock(*m_device); if(m_status == AUD_STATUS_PLAYING) { @@ -135,12 +136,9 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::pause() alSourcePause(m_source); m_status = AUD_STATUS_PAUSED; - m_device->unlock(); return true; } - - m_device->unlock(); } return false; @@ -150,7 +148,7 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::resume() { if(m_status) { - m_device->lock(); + AUD_MutexLock lock(*m_device); if(m_status == AUD_STATUS_PAUSED) { @@ -159,11 +157,8 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::resume() m_device->start(); m_status = AUD_STATUS_PLAYING; - m_device->unlock(); return true; } - - m_device->unlock(); } return false; @@ -174,7 +169,10 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::stop() if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; // AUD_XXX Create a reference of our own object so that it doesn't get // deleted before the end of this function @@ -185,8 +183,6 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::stop() else m_device->m_pausedSounds.remove(This); - m_device->unlock(); - alDeleteSources(1, &m_source); if(!m_isBuffered) alDeleteBuffers(CYCLE_BUFFERS, m_buffers); @@ -208,11 +204,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setKeep(bool keep) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - m_keep = keep; + if(!m_status) + return false; - m_device->unlock(); + m_keep = keep; return true; } @@ -222,7 +219,10 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::seek(float position) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; if(m_isBuffered) alSourcef(m_source, AL_SEC_OFFSET, position); @@ -272,17 +272,18 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::seek(float position) } } - m_device->unlock(); - return true; } float AUD_OpenALDevice::AUD_OpenALHandle::getPosition() { if(!m_status) - return 0.0f; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return 0.0f; float position = 0.0f; @@ -295,8 +296,6 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getPosition() CYCLE_BUFFERS) / (float)specs.rate; } - m_device->unlock(); - return position; } @@ -310,13 +309,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolume() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_GAIN, &result); return result; } @@ -326,11 +326,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolume(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_GAIN, volume); return true; } @@ -340,13 +341,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getPitch() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_PITCH, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_PITCH, &result); return result; } @@ -356,11 +358,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setPitch(float pitch) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_PITCH, pitch); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_PITCH, pitch); return true; } @@ -385,13 +388,14 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setStopCallback(stopCallback callback, if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; m_stop = callback; m_stop_data = data; - m_device->unlock(); - return true; } @@ -404,15 +408,16 @@ AUD_Vector3 AUD_OpenALDevice::AUD_OpenALHandle::getSourceLocation() AUD_Vector3 result = AUD_Vector3(0, 0, 0); if(!m_status) - return result; + return false; + + AUD_MutexLock lock(*m_device); - m_device->lock(); + if(!m_status) + return result; ALfloat p[3]; alGetSourcefv(m_source, AL_POSITION, p); - m_device->unlock(); - result = AUD_Vector3(p[0], p[1], p[2]); return result; @@ -423,11 +428,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceLocation(const AUD_Vector3& lo if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcefv(m_source, AL_POSITION, (ALfloat*)location.get()); + if(!m_status) + return false; - m_device->unlock(); + alSourcefv(m_source, AL_POSITION, (ALfloat*)location.get()); return true; } @@ -437,15 +443,16 @@ AUD_Vector3 AUD_OpenALDevice::AUD_OpenALHandle::getSourceVelocity() AUD_Vector3 result = AUD_Vector3(0, 0, 0); if(!m_status) - return result; + return false; + + AUD_MutexLock lock(*m_device); - m_device->lock(); + if(!m_status) + return result; ALfloat v[3]; alGetSourcefv(m_source, AL_VELOCITY, v); - m_device->unlock(); - result = AUD_Vector3(v[0], v[1], v[2]); return result; @@ -456,11 +463,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceVelocity(const AUD_Vector3& ve if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcefv(m_source, AL_VELOCITY, (ALfloat*)velocity.get()); + if(!m_status) + return false; - m_device->unlock(); + alSourcefv(m_source, AL_VELOCITY, (ALfloat*)velocity.get()); return true; } @@ -472,9 +480,6 @@ AUD_Quaternion AUD_OpenALDevice::AUD_OpenALHandle::getSourceOrientation() bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceOrientation(const AUD_Quaternion& orientation) { - if(!m_status) - return false; - ALfloat direction[3]; direction[0] = -2 * (orientation.w() * orientation.y() + orientation.x() * orientation.z()); @@ -482,11 +487,16 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceOrientation(const AUD_Quaterni orientation.z() * orientation.y()); direction[2] = 2 * (orientation.x() * orientation.x() + orientation.y() * orientation.y()) - 1; - m_device->lock(); - alSourcefv(m_source, AL_DIRECTION, direction); + if(!m_status) + return false; - m_device->unlock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; + + alSourcefv(m_source, AL_DIRECTION, direction); m_orientation = orientation; @@ -500,11 +510,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::isRelative() if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcei(m_source, AL_SOURCE_RELATIVE, &result); + if(!m_status) + return false; - m_device->unlock(); + alGetSourcei(m_source, AL_SOURCE_RELATIVE, &result); return result; } @@ -514,11 +525,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setRelative(bool relative) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcei(m_source, AL_SOURCE_RELATIVE, relative); + if(!m_status) + return false; - m_device->unlock(); + alSourcei(m_source, AL_SOURCE_RELATIVE, relative); return true; } @@ -528,13 +540,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolumeMaximum() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_MAX_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_MAX_GAIN, &result); return result; } @@ -544,11 +557,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolumeMaximum(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_MAX_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_MAX_GAIN, volume); return true; } @@ -558,13 +572,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolumeMinimum() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_MIN_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_MIN_GAIN, &result); return result; } @@ -574,11 +589,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolumeMinimum(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_MIN_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_MIN_GAIN, volume); return true; } @@ -588,13 +604,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getDistanceMaximum() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_MAX_DISTANCE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_MAX_DISTANCE, &result); return result; } @@ -604,11 +621,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setDistanceMaximum(float distance) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_MAX_DISTANCE, distance); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_MAX_DISTANCE, distance); return true; } @@ -618,13 +636,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getDistanceReference() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_REFERENCE_DISTANCE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_REFERENCE_DISTANCE, &result); return result; } @@ -634,11 +653,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setDistanceReference(float distance) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_REFERENCE_DISTANCE, distance); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_REFERENCE_DISTANCE, distance); return true; } @@ -648,13 +668,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getAttenuation() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_ROLLOFF_FACTOR, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_ROLLOFF_FACTOR, &result); return result; } @@ -664,11 +685,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setAttenuation(float factor) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_ROLLOFF_FACTOR, factor); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_ROLLOFF_FACTOR, factor); return true; } @@ -678,13 +700,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeAngleOuter() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_CONE_OUTER_ANGLE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_CONE_OUTER_ANGLE, &result); return result; } @@ -694,11 +717,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeAngleOuter(float angle) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_CONE_OUTER_ANGLE, angle); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_CONE_OUTER_ANGLE, angle); return true; } @@ -708,13 +732,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeAngleInner() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_CONE_INNER_ANGLE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_CONE_INNER_ANGLE, &result); return result; } @@ -724,11 +749,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeAngleInner(float angle) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_CONE_INNER_ANGLE, angle); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_CONE_INNER_ANGLE, angle); return true; } @@ -738,13 +764,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeVolumeOuter() float result = std::numeric_limits<float>::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_CONE_OUTER_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_CONE_OUTER_GAIN, &result); return result; } @@ -754,11 +781,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeVolumeOuter(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_CONE_OUTER_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_CONE_OUTER_GAIN, volume); return true; } @@ -776,7 +804,7 @@ static void *AUD_openalRunThread(void *device) void AUD_OpenALDevice::start(bool join) { - lock(); + AUD_MutexLock lock(*this); if(!m_playing) { @@ -793,8 +821,6 @@ void AUD_OpenALDevice::start(bool join) m_playing = true; } - - unlock(); } void AUD_OpenALDevice::updateStreams() @@ -1177,7 +1203,8 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IReader> rea if(!getFormat(format, specs)) return AUD_Reference<AUD_IHandle>(); - lock(); + AUD_MutexLock lock(*this); + alcSuspendContext(m_context); AUD_Reference<AUD_OpenALDevice::AUD_OpenALHandle> sound; @@ -1190,7 +1217,6 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IReader> rea catch(AUD_Exception&) { alcProcessContext(m_context); - unlock(); throw; } @@ -1201,8 +1227,6 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IReader> rea start(); - unlock(); - return AUD_Reference<AUD_IHandle>(sound); } @@ -1285,7 +1309,8 @@ AUD_Reference<AUD_IHandle> AUD_OpenALDevice::play(AUD_Reference<AUD_IFactory> fa void AUD_OpenALDevice::stopAll() { - lock(); + AUD_MutexLock lock(*this); + alcSuspendContext(m_context); while(!m_playingSounds.empty()) @@ -1295,7 +1320,6 @@ void AUD_OpenALDevice::stopAll() m_pausedSounds.front()->stop(); alcProcessContext(m_context); - unlock(); } void AUD_OpenALDevice::lock() |