Fixed some sound and threading issues.

Details:
* Switched SDL audio mutex to RW lock. This fixes games that continiously call SetVolume in a different thread (like Ghostbusters)
* Added contition to buffer audio packets independent of video packets. This fixes choppy audio across many games.
* Increased the number of audio frame buffers from 2 to 4. Just in case.
* Migrated to std::jthread and std::mutex from pthreads.
* Fixed a race condition with joins on avplayer close that caused a crash.
This commit is contained in:
Vladislav Mikhalin 2024-08-14 21:30:44 +03:00
parent e33ff10212
commit 0d6e8e227a
17 changed files with 231 additions and 481 deletions

View file

@ -13,7 +13,7 @@ namespace Audio {
int SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq,
Libraries::AudioOut::OrbisAudioOutParamFormat format) {
using Libraries::AudioOut::OrbisAudioOutParamFormat;
std::scoped_lock lock{m_mutex};
std::unique_lock lock{m_mutex};
for (int id = 0; id < portsOut.size(); id++) {
auto& port = portsOut[id];
if (!port.isOpen) {
@ -88,7 +88,7 @@ int SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq,
}
s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
std::scoped_lock lock{m_mutex};
std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1];
if (!port.isOpen) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
@ -109,7 +109,7 @@ s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
bool SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) {
using Libraries::AudioOut::OrbisAudioOutParamFormat;
std::scoped_lock lock{m_mutex};
std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1];
if (!port.isOpen) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
@ -147,7 +147,7 @@ bool SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) {
}
bool SDLAudio::AudioOutGetStatus(s32 handle, int* type, int* channels_num) {
std::scoped_lock lock{m_mutex};
std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1];
*type = port.type;
*channels_num = port.channels_num;

View file

@ -3,7 +3,7 @@
#pragma once
#include <mutex>
#include <shared_mutex>
#include <SDL3/SDL_audio.h>
#include "core/libraries/audio/audioout.h"
@ -32,7 +32,7 @@ private:
int volume[8] = {};
SDL_AudioStream* stream = nullptr;
};
std::mutex m_mutex;
std::shared_mutex m_mutex;
std::array<PortOut, 22> portsOut; // main up to 8 ports , BGM 1 port , voice up to 4 ports ,
// personal up to 4 ports , padspk up to 5 ports , aux 1 port
};