From 7b6a0f9b72950ef6752c8477e973666d476b5e51 Mon Sep 17 00:00:00 2001 From: ayuanx Date: Fri, 18 Dec 2009 19:52:04 +0000 Subject: [PATCH] Thread safe. PS: The OpenAL is a mess. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4710 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/AudioCommon/Src/AOSoundStream.cpp | 23 ++++++----- Source/Core/AudioCommon/Src/DSoundStream.cpp | 20 ++++----- Source/Core/AudioCommon/Src/OpenALStream.cpp | 41 +++++++++++-------- Source/Core/AudioCommon/Src/OpenALStream.h | 3 +- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/Source/Core/AudioCommon/Src/AOSoundStream.cpp b/Source/Core/AudioCommon/Src/AOSoundStream.cpp index 632bc450c8..d26bdd9755 100644 --- a/Source/Core/AudioCommon/Src/AOSoundStream.cpp +++ b/Source/Core/AudioCommon/Src/AOSoundStream.cpp @@ -49,16 +49,13 @@ void AOSound::SoundLoop() while (!threadData) { - soundCriticalSection.Enter(); m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2); + soundCriticalSection.Enter(); ao_play(device, (char*)realtimeBuffer, numBytesToRender); soundCriticalSection.Leave(); - if (! threadData) - soundSyncEvent.Wait(); + soundSyncEvent.Wait(); } - ao_close(device); - device = NULL; } void *soundThread(void *args) @@ -84,19 +81,23 @@ void AOSound::Update() void AOSound::Stop() { - soundCriticalSection.Enter(); threadData = 1; soundSyncEvent.Set(); - soundCriticalSection.Leave(); - delete thread; + soundCriticalSection.Enter(); + delete thread; thread = NULL; + + ao_shutdown(); + ao_close(device); + device = NULL; + soundCriticalSection.Leave(); + soundSyncEvent.Shutdown(); } -AOSound::~AOSound() { - // FIXME: crashes dolphin - // ao_shutdown(); +AOSound::~AOSound() +{ } #endif diff --git a/Source/Core/AudioCommon/Src/DSoundStream.cpp b/Source/Core/AudioCommon/Src/DSoundStream.cpp index a51b3f9af5..61613dbd4a 100644 --- a/Source/Core/AudioCommon/Src/DSoundStream.cpp +++ b/Source/Core/AudioCommon/Src/DSoundStream.cpp @@ -101,8 +101,8 @@ void DSound::SoundLoop() { currentPos = 0; lastPos = 0; + dsBuffer->Play(0, 0, DSBPLAY_LOOPING); - HRESULT hr = dsBuffer->Play(0, 0, DSBPLAY_LOOPING); while (!threadData) { // No blocking inside the csection @@ -119,19 +119,15 @@ void DSound::SoundLoop() totalRenderedBytes += numBytesToRender; lastPos = currentPos; } - soundCriticalSection.Leave(); - if (threadData) - break; soundSyncEvent.Wait(); } - - dsBuffer->Stop(); } bool DSound::Start() { soundSyncEvent.Init(); + if (FAILED(DirectSoundCreate8(0, &ds, 0))) return false; if (g_dspInitialize.hWnd) @@ -170,6 +166,8 @@ void DSound::Update() void DSound::Clear(bool mute) { m_muted = mute; + + soundCriticalSection.Enter(); if (m_muted) { dsBuffer->Stop(); @@ -178,21 +176,23 @@ void DSound::Clear(bool mute) { dsBuffer->Play(0, 0, DSBPLAY_LOOPING); } + soundCriticalSection.Leave(); } void DSound::Stop() { - soundCriticalSection.Enter(); threadData = 1; // kick the thread if it's waiting soundSyncEvent.Set(); - soundCriticalSection.Leave(); - delete thread; + soundCriticalSection.Enter(); + delete thread; + thread = NULL; + dsBuffer->Stop(); dsBuffer->Release(); ds->Release(); + soundCriticalSection.Leave(); soundSyncEvent.Shutdown(); - thread = NULL; } diff --git a/Source/Core/AudioCommon/Src/OpenALStream.cpp b/Source/Core/AudioCommon/Src/OpenALStream.cpp index 908573719d..6374b1eb04 100644 --- a/Source/Core/AudioCommon/Src/OpenALStream.cpp +++ b/Source/Core/AudioCommon/Src/OpenALStream.cpp @@ -31,6 +31,9 @@ bool OpenALStream::Start() bool bReturn = false; g_uiSource = 0; + g_uiBuffers = NULL; + + soundSyncEvent.Init(); pDeviceList = new ALDeviceList(); if ((pDeviceList) && (pDeviceList->GetNumDevices())) @@ -68,22 +71,30 @@ void OpenALStream::Stop() ALCcontext *pContext; ALCdevice *pDevice; - soundCriticalSection.Enter(); threadData = 1; // kick the thread if it's waiting soundSyncEvent.Set(); - soundCriticalSection.Leave(); + + soundCriticalSection.Enter(); delete thread; - + thread = NULL; + + alSourceStop(g_uiSource); + alSourcei(g_uiSource, AL_BUFFER, 0); + + // Clean up buffers and sources + alDeleteSources(1, &g_uiSource); + alDeleteBuffers(AUDIO_NUMBUFFERS, g_uiBuffers); + pContext = alcGetCurrentContext(); pDevice = alcGetContextsDevice(pContext); alcMakeContextCurrent(NULL); alcDestroyContext(pContext); alcCloseDevice(pDevice); + soundCriticalSection.Leave(); soundSyncEvent.Shutdown(); - thread = NULL; } void OpenALStream::Update() @@ -97,6 +108,8 @@ void OpenALStream::Update() void OpenALStream::Clear(bool mute) { m_muted = mute; + + soundCriticalSection.Enter(); if(m_muted) { alSourceStop(g_uiSource); @@ -105,6 +118,7 @@ void OpenALStream::Clear(bool mute) { alSourcePlay(g_uiSource); } + soundCriticalSection.Leave(); } THREAD_RETURN OpenALStream::ThreadFunc(void* args) @@ -115,8 +129,8 @@ THREAD_RETURN OpenALStream::ThreadFunc(void* args) void OpenALStream::SoundLoop() { - ALuint uiBuffers[AUDIO_NUMBUFFERS] = {0}; - ALuint uiSource = 0; + ALuint uiBuffers[AUDIO_NUMBUFFERS] = {0}; + ALuint uiSource = 0; ALenum err; u32 ulFrequency = m_mixer->GetSampleRate(); // Generate some AL Buffers for streaming @@ -135,19 +149,19 @@ void OpenALStream::SoundLoop() //*/ g_uiSource = uiSource; + g_uiBuffers = (ALuint *)uiBuffers; alSourcePlay(uiSource); err = alGetError(); while (!threadData) { - soundCriticalSection.Enter(); int numBytesToRender = 32768; //ya, this is a hack, we need real data count /*int numBytesRender =*/ m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2); - soundCriticalSection.Leave(); //if (numBytesRender) //here need debug { + soundCriticalSection.Enter(); ALint iBuffersProcessed = 0; alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed); @@ -170,18 +184,11 @@ void OpenALStream::SoundLoop() } alSourceQueueBuffers(uiSource, 1, &uiTempBuffer); } + soundCriticalSection.Leave(); } - if (!threadData) - soundSyncEvent.Wait(); + soundSyncEvent.Wait(); } - alSourceStop(uiSource); - alSourcei(uiSource, AL_BUFFER, 0); - - // Clean up buffers and sources - alDeleteSources(1, &uiSource); - alDeleteBuffers(AUDIO_NUMBUFFERS, (ALuint *)uiBuffers); - } #endif //HAVE_OPENAL diff --git a/Source/Core/AudioCommon/Src/OpenALStream.h b/Source/Core/AudioCommon/Src/OpenALStream.h index 5e5d7d3463..75ea2b1e06 100644 --- a/Source/Core/AudioCommon/Src/OpenALStream.h +++ b/Source/Core/AudioCommon/Src/OpenALStream.h @@ -61,7 +61,8 @@ private: Common::Event soundSyncEvent; short realtimeBuffer[OAL_BUFFER_SIZE]; - ALuint g_uiSource; + ALuint g_uiSource; + ALuint *g_uiBuffers; #else public: OpenALStream(CMixer *mixer, void *hWnd = NULL): SoundStream(mixer) {}