Skip to content

Commit

Permalink
Replaced std mutexes and lock guards with SDL_mutex, since they weren…
Browse files Browse the repository at this point in the history
…'t working properly on Vita
  • Loading branch information
Northfear committed Jun 2, 2024
1 parent 6cfb013 commit 2102561
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ if(WIN32)
endif()

if(VITA)
#set(VITA_FLAGS "-Ofast -mcpu=cortex-a9 -mfpu=neon -fgraphite-identity -floop-parallelize-all -floop-nest-optimize -ftree-parallelize-loops=3 -flto -fno-peephole2 -DNDEBUG")
set(VITA_FLAGS "-O2 -g -mcpu=cortex-a9 -mfpu=neon -fno-peephole2 -DNDEBUG")
set(VITA_FLAGS "-O2 -mcpu=cortex-a9 -mfpu=neon -DNDEBUG")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VITA_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VITA_FLAGS}")

Expand Down
73 changes: 57 additions & 16 deletions src/audio_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <string.h>

#include <mutex>

#include <SDL.h>

namespace fallout {
Expand All @@ -22,7 +20,7 @@ struct AudioEngineSoundBuffer {
bool looping;
unsigned int pos;
SDL_AudioStream* stream;
std::recursive_mutex mutex;
SDL_mutex* mutex;
};

extern bool gProgramIsActive;
Expand Down Expand Up @@ -54,7 +52,10 @@ static void audioEngineMixin(void* userData, Uint8* stream, int length)

for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
// for some strange reason std::lock_guard isn't locking the mutex properly on Vita
// which results in audioEngineSoundBufferRelease call during this loop and subsequent crash
// replacing the std::recursive_mutex with SDL_mutex is fixing this issue
SDL_LockMutex(soundBuffer->mutex);

if (soundBuffer->active && soundBuffer->playing) {
int srcFrameSize = soundBuffer->bitsPerSample / 8 * soundBuffer->channels;
Expand Down Expand Up @@ -90,11 +91,18 @@ static void audioEngineMixin(void* userData, Uint8* stream, int length)
pos += bytesRead;
}
}

SDL_UnlockMutex(soundBuffer->mutex);
}
}

bool audioEngineInit()
{
for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
soundBuffer->mutex = SDL_CreateMutex();
}

if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1) {
return false;
}
Expand Down Expand Up @@ -126,6 +134,11 @@ void audioEngineExit()
if (SDL_WasInit(SDL_INIT_AUDIO)) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}

for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
SDL_DestroyMutex(soundBuffer->mutex);
}
}

void audioEnginePause()
Expand All @@ -150,7 +163,7 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann

for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
soundBuffer->active = true;
Expand All @@ -164,8 +177,11 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann
soundBuffer->pos = 0;
soundBuffer->data = malloc(size);
soundBuffer->stream = SDL_NewAudioStream(bitsPerSample == 16 ? AUDIO_S16 : AUDIO_S8, channels, rate, gAudioEngineSpec.format, gAudioEngineSpec.channels, gAudioEngineSpec.freq);
SDL_UnlockMutex(soundBuffer->mutex);
return index;
}

SDL_UnlockMutex(soundBuffer->mutex);
}

return -1;
Expand All @@ -182,9 +198,10 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex)
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

Expand All @@ -196,6 +213,7 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex)
SDL_FreeAudioStream(soundBuffer->stream);
soundBuffer->stream = nullptr;

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -210,14 +228,16 @@ bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume)
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

soundBuffer->volume = volume;

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -232,14 +252,16 @@ bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr)
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

*volumePtr = soundBuffer->volume;

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -254,15 +276,17 @@ bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan)
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

// NOTE: Audio engine does not support sound panning. I'm not sure it's
// even needed. For now this value is silently ignored.

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -277,9 +301,10 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags)
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

Expand All @@ -289,6 +314,7 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags)
soundBuffer->looping = true;
}

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -303,14 +329,16 @@ bool audioEngineSoundBufferStop(int soundBufferIndex)
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

soundBuffer->playing = false;

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -325,9 +353,10 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

Expand All @@ -346,6 +375,7 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int
}
}

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -360,14 +390,16 @@ bool audioEngineSoundBufferSetCurrentPosition(int soundBufferIndex, unsigned int
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

soundBuffer->pos = pos % soundBuffer->size;

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -382,18 +414,21 @@ bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, uns
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

if (audioBytes1 == nullptr) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

if ((flags & AUDIO_ENGINE_SOUND_BUFFER_LOCK_FROM_WRITE_POS) != 0) {
if (!audioEngineSoundBufferGetCurrentPosition(soundBufferIndex, nullptr, &writePos)) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}
}
Expand Down Expand Up @@ -429,6 +464,7 @@ bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, uns

// TODO: Mark range as locked.

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -443,14 +479,16 @@ bool audioEngineSoundBufferUnlock(int soundBufferIndex, void* audioPtr1, unsigne
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

// TODO: Mark range as unlocked.

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand All @@ -465,13 +503,15 @@ bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusP
}

AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[soundBufferIndex]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
SDL_LockMutex(soundBuffer->mutex);

if (!soundBuffer->active) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

if (statusPtr == nullptr) {
SDL_UnlockMutex(soundBuffer->mutex);
return false;
}

Expand All @@ -485,6 +525,7 @@ bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusP
}
}

SDL_UnlockMutex(soundBuffer->mutex);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion vita/vita.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(VITA_APP_NAME "Fallout 2 CE")
set(VITA_TITLEID "FOUT00002")

set(EXECUTABLE_NAME fallout2-ce)
set(VITA_VERSION "01.00")
set(VITA_VERSION "01.30")
set(VITA_MKSFOEX_FLAGS "-d ATTRIBUTE2=12")

vita_create_self(${EXECUTABLE_NAME}.self ${EXECUTABLE_NAME})
Expand Down

0 comments on commit 2102561

Please sign in to comment.