Refactor audio handling with range checks, buffer threshold, and lock fixes

- Added range checks for handle to avoid invalid index access in AudioOutOutput, AudioOutSetVolume, and AudioOutGetStatus.

- Added a constant AUDIO_STREAM_BUFFER_THRESHOLD for the buffer threshold (was previously a magic number).

- Set the freq parameter correctly in the SDL_AudioSpec structure in AudioOutOpen.

- Fixed locking issues in AudioOutOutput to avoid unlocking before it's locked.

- Removed tab spaces to fix format clang error
This commit is contained in:
Mikasa-san 2024-10-02 16:58:57 +04:00 committed by GitHub
parent d63b7fcc1d
commit 4a6922bcce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -98,22 +98,22 @@ s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT; return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
} }
if (ptr == nullptr) {
return 0; // Nothing to output
}
std::shared_lock lock{m_mutex}; std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1]; auto& port = portsOut[handle - 1];
if (!port.isOpen) { if (!port.isOpen) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT; return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
} }
if (ptr == nullptr) {
return 0;
}
// TODO mixing channels const size_t data_size = port.samples_num * port.sample_size * port.channels_num;
SDL_bool result = SDL_PutAudioStreamData(
port.stream, ptr, port.samples_num * port.sample_size * port.channels_num);
lock.unlock(); // Unlock after the critical section SDL_bool result = SDL_PutAudioStreamData(port.stream, ptr, data_size);
lock.unlock(); // Unlock only after necessary operations
// TODO find a correct value 8192 is estimated
while (SDL_GetAudioStreamAvailable(port.stream) > AUDIO_STREAM_BUFFER_THRESHOLD) { while (SDL_GetAudioStreamAvailable(port.stream) > AUDIO_STREAM_BUFFER_THRESHOLD) {
SDL_Delay(0); SDL_Delay(0);
} }