audio_core: Fix return value types and shift some error handling to library. (#1212)

This commit is contained in:
squidbus 2024-10-02 21:01:39 -07:00 committed by GitHub
parent 7e533ccf50
commit 388d717205
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 38 deletions

View File

@ -16,7 +16,7 @@ namespace Audio {
constexpr int AUDIO_STREAM_BUFFER_THRESHOLD = 65536; // Define constant for buffer threshold constexpr int AUDIO_STREAM_BUFFER_THRESHOLD = 65536; // Define constant for buffer threshold
int SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq, s32 SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq,
Libraries::AudioOut::OrbisAudioOutParamFormat format) { Libraries::AudioOut::OrbisAudioOutParamFormat format) {
using Libraries::AudioOut::OrbisAudioOutParamFormat; using Libraries::AudioOut::OrbisAudioOutParamFormat;
std::unique_lock lock{m_mutex}; std::unique_lock lock{m_mutex};
@ -90,18 +90,11 @@ int SDLAudio::AudioOutOpen(int type, u32 samples_num, u32 freq,
} }
} }
return -1; // all ports are used LOG_ERROR(Lib_AudioOut, "Audio ports are full");
return ORBIS_AUDIO_OUT_ERROR_PORT_FULL; // all ports are used
} }
s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) { s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
if (handle < 1 || handle > portsOut.size()) { // Add handle range check
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) {
@ -121,11 +114,7 @@ s32 SDLAudio::AudioOutOutput(s32 handle, const void* ptr) {
return result ? ORBIS_OK : -1; return result ? ORBIS_OK : -1;
} }
bool SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) { s32 SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) {
if (handle < 1 || handle > portsOut.size()) { // Add handle range check
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
}
using Libraries::AudioOut::OrbisAudioOutParamFormat; using Libraries::AudioOut::OrbisAudioOutParamFormat;
std::shared_lock lock{m_mutex}; std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1]; auto& port = portsOut[handle - 1];
@ -162,20 +151,16 @@ bool SDLAudio::AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume) {
} }
} }
return true; return ORBIS_OK;
}
bool SDLAudio::AudioOutGetStatus(s32 handle, int* type, int* channels_num) {
if (handle < 1 || handle > portsOut.size()) { // Add handle range check
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
} }
s32 SDLAudio::AudioOutGetStatus(s32 handle, int* type, int* channels_num) {
std::shared_lock lock{m_mutex}; std::shared_lock lock{m_mutex};
auto& port = portsOut[handle - 1]; auto& port = portsOut[handle - 1];
*type = port.type; *type = port.type;
*channels_num = port.channels_num; *channels_num = port.channels_num;
return true; return ORBIS_OK;
} }
} // namespace Audio } // namespace Audio

View File

@ -14,11 +14,11 @@ public:
SDLAudio() = default; SDLAudio() = default;
virtual ~SDLAudio() = default; virtual ~SDLAudio() = default;
int AudioOutOpen(int type, u32 samples_num, u32 freq, s32 AudioOutOpen(int type, u32 samples_num, u32 freq,
Libraries::AudioOut::OrbisAudioOutParamFormat format); Libraries::AudioOut::OrbisAudioOutParamFormat format);
s32 AudioOutOutput(s32 handle, const void* ptr); s32 AudioOutOutput(s32 handle, const void* ptr);
bool AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume); s32 AudioOutSetVolume(s32 handle, s32 bitflag, s32* volume);
bool AudioOutGetStatus(s32 handle, int* type, int* channels_num); s32 AudioOutGetStatus(s32 handle, int* type, int* channels_num);
private: private:
struct PortOut { struct PortOut {
@ -33,8 +33,7 @@ private:
bool isOpen = false; bool isOpen = false;
}; };
std::shared_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 , std::array<PortOut, Libraries::AudioOut::SCE_AUDIO_OUT_NUM_PORTS> portsOut;
// personal up to 4 ports , padspk up to 5 ports , aux 1 port
}; };
} // namespace Audio } // namespace Audio

View File

@ -176,11 +176,15 @@ int PS4_SYSV_ABI sceAudioOutGetLastOutputTime() {
} }
int PS4_SYSV_ABI sceAudioOutGetPortState(s32 handle, OrbisAudioOutPortState* state) { int PS4_SYSV_ABI sceAudioOutGetPortState(s32 handle, OrbisAudioOutPortState* state) {
if (handle < 1 || handle > SCE_AUDIO_OUT_NUM_PORTS) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
}
int type = 0; int type = 0;
int channels_num = 0; int channels_num = 0;
if (!audio->AudioOutGetStatus(handle, &type, &channels_num)) { if (const auto err = audio->AudioOutGetStatus(handle, &type, &channels_num); err != ORBIS_OK) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT; return err;
} }
state->rerouteCounter = 0; state->rerouteCounter = 0;
@ -310,12 +314,7 @@ s32 PS4_SYSV_ABI sceAudioOutOpen(UserService::OrbisUserServiceUserId user_id,
LOG_ERROR(Lib_AudioOut, "Invalid format attribute"); LOG_ERROR(Lib_AudioOut, "Invalid format attribute");
return ORBIS_AUDIO_OUT_ERROR_INVALID_FORMAT; return ORBIS_AUDIO_OUT_ERROR_INVALID_FORMAT;
} }
int result = audio->AudioOutOpen(port_type, length, sample_rate, format); return audio->AudioOutOpen(port_type, length, sample_rate, format);
if (result == -1) {
LOG_ERROR(Lib_AudioOut, "Audio ports are full");
return ORBIS_AUDIO_OUT_ERROR_PORT_FULL;
}
return result;
} }
int PS4_SYSV_ABI sceAudioOutOpenEx() { int PS4_SYSV_ABI sceAudioOutOpenEx() {
@ -324,12 +323,19 @@ int PS4_SYSV_ABI sceAudioOutOpenEx() {
} }
s32 PS4_SYSV_ABI sceAudioOutOutput(s32 handle, const void* ptr) { s32 PS4_SYSV_ABI sceAudioOutOutput(s32 handle, const void* ptr) {
if (handle < 1 || handle > SCE_AUDIO_OUT_NUM_PORTS) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
}
if (ptr == nullptr) {
// Nothing to output
return ORBIS_OK;
}
return audio->AudioOutOutput(handle, ptr); return audio->AudioOutOutput(handle, ptr);
} }
int PS4_SYSV_ABI sceAudioOutOutputs(OrbisAudioOutOutputParam* param, u32 num) { int PS4_SYSV_ABI sceAudioOutOutputs(OrbisAudioOutOutputParam* param, u32 num) {
for (u32 i = 0; i < num; i++) { for (u32 i = 0; i < num; i++) {
if (auto err = audio->AudioOutOutput(param[i].handle, param[i].ptr); err != 0) if (const auto err = sceAudioOutOutput(param[i].handle, param[i].ptr); err != 0)
return err; return err;
} }
return ORBIS_OK; return ORBIS_OK;
@ -426,10 +432,10 @@ int PS4_SYSV_ABI sceAudioOutSetUsbVolume() {
} }
s32 PS4_SYSV_ABI sceAudioOutSetVolume(s32 handle, s32 flag, s32* vol) { s32 PS4_SYSV_ABI sceAudioOutSetVolume(s32 handle, s32 flag, s32* vol) {
if (!audio->AudioOutSetVolume(handle, flag, vol)) { if (handle < 1 || handle > SCE_AUDIO_OUT_NUM_PORTS) {
return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT; return ORBIS_AUDIO_OUT_ERROR_INVALID_PORT;
} }
return ORBIS_OK; return audio->AudioOutSetVolume(handle, flag, vol);
} }
int PS4_SYSV_ABI sceAudioOutSetVolumeDown() { int PS4_SYSV_ABI sceAudioOutSetVolumeDown() {

View File

@ -11,6 +11,10 @@ namespace Libraries::AudioOut {
constexpr int SCE_AUDIO_OUT_VOLUME_0DB = 32768; // max volume value constexpr int SCE_AUDIO_OUT_VOLUME_0DB = 32768; // max volume value
// 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
constexpr int SCE_AUDIO_OUT_NUM_PORTS = 22;
enum OrbisAudioOutPort { enum OrbisAudioOutPort {
ORBIS_AUDIO_OUT_PORT_TYPE_MAIN = 0, ORBIS_AUDIO_OUT_PORT_TYPE_MAIN = 0,
ORBIS_AUDIO_OUT_PORT_TYPE_BGM = 1, ORBIS_AUDIO_OUT_PORT_TYPE_BGM = 1,