Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/libs/prx/libSceAudioOut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ if(BUILD_TESTING)
target_include_directories(audio_out_vibration_device_tests PRIVATE ${LIBS_INCLUDE_DIR} ${ANYPS5_SDL2_INCLUDE_DIR})
target_link_libraries(audio_out_vibration_device_tests PRIVATE libc libkernel libScePad)
configure_windows_unwind(audio_out_vibration_device_tests)
foreach(vibrationDeviceMode IN ITEMS quad pulse open_failure timing cadence discovery_failure)
foreach(vibrationDeviceMode IN ITEMS quad pulse open_failure timing cadence discovery_failure disconnect)
set(vibrationDeviceTest "audio_out_vibration_device_${vibrationDeviceMode}")
add_test(NAME ${vibrationDeviceTest} COMMAND audio_out_vibration_device_tests ${vibrationDeviceMode})
set_tests_properties(${vibrationDeviceTest} PROPERTIES TIMEOUT 5)
Expand All @@ -86,7 +86,7 @@ if(BUILD_TESTING)
target_include_directories(audio_out_default_open_tests PRIVATE ${LIBS_INCLUDE_DIR} ${ANYPS5_SDL2_INCLUDE_DIR})
target_link_libraries(audio_out_default_open_tests PRIVATE libc libkernel libScePad)
configure_windows_unwind(audio_out_default_open_tests)
foreach(defaultOpenMode IN ITEMS zero_default wasapi_zero_default no_endpoint positive unknown)
foreach(defaultOpenMode IN ITEMS zero_default wasapi_zero_default no_endpoint positive unknown disconnect)
set(defaultOpenTest "audio_out_default_open_${defaultOpenMode}")
add_test(NAME ${defaultOpenTest} COMMAND audio_out_default_open_tests ${defaultOpenMode})
set_tests_properties(${defaultOpenTest} PROPERTIES TIMEOUT 5)
Expand Down
17 changes: 17 additions & 0 deletions core/libs/prx/libSceAudioOut/src/AudioOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ static void closeDevice(Port& port) {
port.spec = {};
}

static void reopenStoppedDevice(Port& port) {
if (port.device == 0 || SDL_GetAudioDeviceStatus(port.device) != SDL_AUDIO_STOPPED) return;
closeDevice(port);
if (port.type == PORT_TYPE_VIBRATION) {
try {
openVibrationDevice(port);
} catch (const std::runtime_error&) {
port.device = 0;
port.spec = {};
}
} else {
openDevice(port);
}
}

static constexpr std::uint32_t STD_8CH_MAP[8] = {0, 1, 2, 3, 6, 7, 4, 5};

static const void* prepareBuffer(const Port& port, const void* data, std::vector<std::uint8_t>& buf) {
Expand Down Expand Up @@ -496,6 +511,7 @@ int APS5_VABI sceAudioOutOutput(int handle, const void* ptr) try {
return -2144993277;
}

reopenStoppedDevice(*port);
if (port->device == 0) {
outputVirtualPort(*port, ptr, lock);
} else {
Expand Down Expand Up @@ -533,6 +549,7 @@ int APS5_VABI sceAudioOutOutputs(AudioOutOutputParam* param, std::uint32_t num)
Port& first = *getPort(param[0].handle);
for (std::uint32_t i = 0; i < num; i++) {
Port& port = *getPort(param[i].handle);
reopenStoppedDevice(port);
if (port.device == 0) {
outputVirtualPort(port, param[i].ptr, lock);
} else {
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAudioOut/tests/AudioOutBufferSize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ extern "C" int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID id, const void* data, Ui
}
extern "C" void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID id) { Require(id == device, "wrong queue cleared"); }
extern "C" void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID id) { Require(id == device, "wrong device closed"); ++closed; }
extern "C" SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID) { return SDL_AUDIO_PLAYING; }

int main(int argc, char** argv) {
try {
Expand Down
34 changes: 32 additions & 2 deletions core/libs/prx/libSceAudioOut/tests/AudioOutDefaultOpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@
#include "SDL.h"
#include "prx/libSceAudioOut/include/AudioOutSdl.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <array>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <string_view>

namespace {
constexpr SDL_AudioDeviceID device = 7;
SDL_AudioDeviceID device = 7;
const char* driver = "wasapi";
int outputDevices = 0;
bool endpointAbsent = false;
bool unplugged = false;
int probes = 0;
int opened = 0;
int closed = 0;
int queuedLive = 0;
int queuedDead = 0;

void Require(bool value, const char* message) {
if (!value) throw std::runtime_error(message);
}

bool live(SDL_AudioDeviceID id) { return id == device && !unplugged; }
}

int InitializeSdlAudio_nid_no_patch() { return 0; }
Expand All @@ -33,15 +40,23 @@ extern "C" const char* SDLCALL SDL_GetCurrentAudioDriver() { return driver; }
extern "C" const char* SDLCALL SDL_GetError() { return "test error"; }
extern "C" SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(const char* name, int capture, const SDL_AudioSpec* desired, SDL_AudioSpec* obtained, int) {
Require(name == nullptr && capture == 0, "the main port did not open the default output");
device = static_cast<SDL_AudioDeviceID>(7 + opened);
++opened;
unplugged = false;
*obtained = *desired;
return device;
}
extern "C" void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID id, int) { Require(id == device, "wrong device paused"); }
extern "C" Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID id) { Require(id == device, "wrong queue queried"); return 0; }
extern "C" int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID id, const void*, Uint32) { Require(id == device, "wrong queue written"); return 0; }
extern "C" int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID id, const void*, Uint32) {
if (!unplugged) Require(id == device, "wrong queue written");
if (live(id)) ++queuedLive;
else ++queuedDead;
return 0;
}
extern "C" void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID id) { Require(id == device, "wrong queue cleared"); }
extern "C" void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID id) { Require(id == device, "wrong device closed"); ++closed; }
extern "C" SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID id) { return live(id) ? SDL_AUDIO_PLAYING : SDL_AUDIO_STOPPED; }

int main(int argc, char** argv) {
try {
Expand All @@ -65,9 +80,24 @@ int main(int argc, char** argv) {
} else if (mode == "unknown") {
driver = "wasapi";
outputDevices = -1;
} else if (mode == "disconnect") {
driver = "wasapi";
outputDevices = 2;
} else {
Require(false, "invalid default open mode");
}
if (mode == "disconnect") {
const int port = sceAudioOutOpen(0x10000000, 0, 0, 256, 48000, 1);
Require(port >= 1 && opened == 1, "the main port did not open the default output");
unplugged = true;
std::array<std::int16_t, 256 * 2> samples{};
Require(sceAudioOutOutput(port, samples.data()) == 256, "output after the host removed the default output failed");
Require(opened == 2 && closed == 1, "the removed default output was not replaced");
Require(queuedLive == 1 && queuedDead == 0, "output was queued to the removed default output");
Require(sceAudioOutClose(port) == 0 && closed == 2, "the replacement output was not closed with its port");
std::puts("default output open tests passed");
return 0;
}
const int handle = sceAudioOutOpen(0x10000000, 0, 0, 256, 48000, 1);
Require(handle >= 1, "the main port did not open");
Require(opened == (expectOpen ? 1 : 0), expectOpen ? "the default output was not opened" : "the default output was opened with no WASAPI endpoint");
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAudioOut/tests/AudioOutPortLocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern "C" std::uint64_t APS5_VABI sceKernelGetProcessTime(void) {
extern "C" int SDLCALL SDL_InitSubSystem(Uint32) { return 0; }
extern "C" Uint32 SDLCALL SDL_WasInit(Uint32) { return SDL_INIT_AUDIO; }
extern "C" void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID, int) {}
extern "C" SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID) { return SDL_AUDIO_PLAYING; }

extern "C" SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(const char*, int, const SDL_AudioSpec* desired, SDL_AudioSpec* obtained, int) {
*obtained = *desired;
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAudioOut/tests/AudioOutResampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void TestBoundary(std::uint32_t sourceRate) {
extern "C" int SDLCALL SDL_InitSubSystem(Uint32) { return 0; }
extern "C" Uint32 SDLCALL SDL_WasInit(Uint32) { return SDL_INIT_AUDIO; }
extern "C" void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID, int) {}
extern "C" SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID) { return SDL_AUDIO_PLAYING; }
extern "C" Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID) { return 0; }
extern "C" void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID) {}

Expand Down
23 changes: 22 additions & 1 deletion core/libs/prx/libSceAudioOut/tests/AudioOutVibrationDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ constexpr std::uint32_t frames = 256;
bool available = true;
bool failOpen = false;
bool failInitialize = false;
bool stopped = false;
int queuedWhileStopped = 0;
bool failEnumeration = false;
bool pulse = false;
std::uint32_t frequency = 48000;
Expand Down Expand Up @@ -206,6 +208,7 @@ extern "C" SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(const char* name, int c
Require(capture == 0 && flags == 0 && desired->callback == nullptr && desired->freq == static_cast<int>(frequency) && desired->format == AUDIO_F32SYS && desired->channels == (pulse ? 6 : 4) && desired->samples == frames, "native controller request changed");
if (failOpen) return 0;
++opened;
stopped = false;
*obtained = *desired;
return 42;
}
Expand All @@ -216,17 +219,19 @@ extern "C" int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID device, const void* data
const auto* samples = static_cast<const float*>(data);
const std::size_t sampleCount = bytes / sizeof(float);
played.assign(samples, samples + sampleCount);
if (stopped) ++queuedWhileStopped;
++queued;
return 0;
}
extern "C" void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID device) { Require(device == 42, "wrong native queue cleared"); }
extern "C" void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID device) { Require(device == 42, "wrong native device closed"); ++closed; }
extern "C" SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID device) { return device == 42 && !stopped ? SDL_AUDIO_PLAYING : SDL_AUDIO_STOPPED; }

int main(int argc, char** argv) {
try {
Require(argc == 2, "invalid test arguments");
const std::string_view mode = argv[1];
Require(mode == "quad" || mode == "pulse" || mode == "open_failure" || mode == "timing" || mode == "cadence" || mode == "discovery_failure", "invalid device mode");
Require(mode == "quad" || mode == "pulse" || mode == "open_failure" || mode == "timing" || mode == "cadence" || mode == "discovery_failure" || mode == "disconnect", "invalid device mode");
pulse = mode == "pulse";
Pad::Initialize();
Pad::SetVibrationMode(1);
Expand All @@ -242,6 +247,22 @@ int main(int argc, char** argv) {
std::puts("legacy vibration device tests passed");
return 0;
}
if (mode == "disconnect") {
const int port = sceAudioOutOpen(0x10000000, 10, 0, frames, frequency, 1);
Require(port > 0 && opened == 1, "the controller port did not open its sound card");
stopped = true;
std::array<std::int16_t, frames * 2> samples{};
for (std::uint32_t frame = 0; frame < frames; ++frame) {
samples[frame * 2] = 8192;
samples[frame * 2 + 1] = -16384;
}
Require(sceAudioOutOutput(port, samples.data()) == static_cast<int>(frames), "vibration output after the controller stopped failed");
Require(opened == 2 && closed == 1, "the stopped controller sound card was not reopened");
Require(queued == 1 && queuedWhileStopped == 0, "vibration PCM was queued to the stopped controller sound card");
Require(sceAudioOutClose(port) == 0 && closed == 2, "the reopened controller sound card was not closed with its port");
std::puts("legacy vibration device tests passed");
return 0;
}
if (mode == "open_failure") {
failOpen = true;
bool threw = false;
Expand Down
2 changes: 2 additions & 0 deletions docs/dev/debt/libSceAudioOut.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@

### Functional

- [AudioOut2 main output](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) - a context's main host device is opened once at sceAudioOut2ContextCreate and is not reopened when SDL reports it stopped, so its audio stays silent for the rest of the context's life; only the controller sound card of a context is probed again. The console's behaviour when its output disappears is not measured
- [Host output reopen](../../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - the console's behaviour when a port's output disappears is not measured. Here a port whose host device SDL reports as stopped reopens its default output or controller sound card before its next output, a host policy with no console counterpart. When that reopen fails, the port runs as a virtual port for the rest of its life and is not retried
- [sceAudioOut2ContextPush](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Context.cpp) (libSceAudioOut) - a blocking Push waits for room before it queues its grain and again after, so it returns once fewer grains than the queue depth wait to be played (each wait ends after at most 200 ms, so on a queue that never drains a blocking Push returns after up to 400 ms). Inferred from PPSA02530, whose sound engine pushes one grain per call from a feeder thread and wakes its renderer after each push: while Push returned as soon as the grain was queued, the renderer found the queue full and slept until the next rendered frame. When the console's blocking Push returns is unmeasured. Both waits release the context lock, so destroying a context while a push is waiting on it is not supported.
- [sceAudioOutMasteringInit, sceAudioOutMasteringTerm and sceAudioOutMasteringSetParam](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - non-zero flags, a null pointer and a params id other than 1 throw; a repeated Init, a Term without Init and a SetParam or GetState before Init are unknown. There is no mastering stage, so Init and Term keep no state and SetParam reads only the id. The signatures and the default params id 1 follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Audio/AudioOut.cs#L312-L344), not a console measurement. SharpProspero documents flag bit 0 as `SwapEqCompressor`, and shadPS4's PS4 stub [rejects any non-zero Init flags](https://github.com/shadps4-emu/shadPS4/blob/8d50471b7dc141dd700b51276b8edd1f293bce26/src/core/libraries/audio/audioout.cpp#L956-L962) with 0x80260201
- [sceAudioOutMasteringGetState](../../../core/libs/prx/libSceAudioOut/src/AudioOut2Mastering.cpp) (libSceAudioOut) - the states id 1 and the meter offsets 32, 128, 240, 272 and 304 in a 336-byte struct follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Audio/AudioOutMastering.cs#L97-L110), not a console measurement. With no mastering stage it zeroes the compressor input RMS and gain, and the limiter input, output and gain peaks, and leaves the other bytes, including the descriptors the AudioOut2 call writes, untouched; zero is not known to be what the console reports for an idle chain. A null pointer and a states id other than 1 throw; the console error codes are unknown
Expand Down
Loading