mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #624 from DotDebian/feat/audioout-get-last-output-time
feat(libSceAudioOut): implement sceAudioOutGetLastOutputTime
This commit is contained in:
@@ -42,4 +42,14 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(audio_out_mix_level_pad_spk PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAudioOut>")
|
||||
endif()
|
||||
|
||||
add_test_executable(audio_out_last_output_time_tests tests/AudioOutLastOutputTime.cpp)
|
||||
target_include_directories(audio_out_last_output_time_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(audio_out_last_output_time_tests PRIVATE libSceAudioOut libkernel libc)
|
||||
configure_windows_unwind(audio_out_last_output_time_tests)
|
||||
add_test(NAME audio_out_last_output_time COMMAND audio_out_last_output_time_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(audio_out_last_output_time PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAudioOut>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -89,6 +89,7 @@ struct Port {
|
||||
int volume[8] = {};
|
||||
int mixLevel = DEFAULT_VOLUME;
|
||||
std::uint64_t lastOutputTime = 0;
|
||||
std::uint64_t lastDataOutputTime = 0;
|
||||
SDL_AudioDeviceID device = 0;
|
||||
SDL_AudioSpec spec = {};
|
||||
};
|
||||
@@ -328,6 +329,7 @@ int APS5_VABI sceAudioOutOpen(int userId, int type, int index, std::uint32_t len
|
||||
port.format = format;
|
||||
port.channels = channelsForFormat(format);
|
||||
port.lastOutputTime = 0;
|
||||
port.lastDataOutputTime = 0;
|
||||
port.mixLevel = type == PORT_TYPE_PADSPK ? DEFAULT_PADSPK_MIX_LEVEL : DEFAULT_VOLUME;
|
||||
for (int c = 0; c < port.channels; c++) {
|
||||
port.volume[c] = DEFAULT_VOLUME;
|
||||
@@ -374,6 +376,7 @@ int APS5_VABI sceAudioOutOutput(int handle, const void* ptr) {
|
||||
|
||||
queueAudio(*port, ptr);
|
||||
port->lastOutputTime = sceKernelGetProcessTime();
|
||||
if (ptr != nullptr) port->lastDataOutputTime = port->lastOutputTime;
|
||||
return static_cast<int>(port->samplesNum);
|
||||
}
|
||||
|
||||
@@ -425,7 +428,10 @@ int APS5_VABI sceAudioOutOutputs(AudioOutOutputParam* param, std::uint32_t num)
|
||||
|
||||
const std::uint64_t done = sceKernelGetProcessTime();
|
||||
for (std::uint32_t i = 0; i < num; i++) {
|
||||
if (auto* port = getPort(param[i].handle)) port->lastOutputTime = done;
|
||||
if (auto* port = getPort(param[i].handle)) {
|
||||
port->lastOutputTime = done;
|
||||
if (param[i].ptr != nullptr) port->lastDataOutputTime = done;
|
||||
}
|
||||
}
|
||||
|
||||
return static_cast<int>(first.samplesNum);
|
||||
@@ -457,6 +463,19 @@ int APS5_VABI sceAudioOutSetVolume(int handle, std::uint32_t flag, int* vol) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceAudioOutGetLastOutputTime(int handle, std::uint64_t* outputTime) {
|
||||
if (outputTime == nullptr) {
|
||||
return -2144993276;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
Port* port = getPort(handle);
|
||||
if (port == nullptr) {
|
||||
return -2144993277;
|
||||
}
|
||||
*outputTime = port->lastDataOutputTime;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceAudioOutGetPortState(int handle, AudioOutPortState* state) {
|
||||
if (state == nullptr) {
|
||||
return -2144993276;
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
extern "C" {
|
||||
int APS5_VABI sceAudioOutOpen(int, int, int, std::uint32_t, std::uint32_t, std::uint32_t);
|
||||
int APS5_VABI sceAudioOutClose(int);
|
||||
int APS5_VABI sceAudioOutOutput(int, const void*);
|
||||
int APS5_VABI sceAudioOutOutputs(AudioOutOutputParam*, std::uint32_t);
|
||||
int APS5_VABI sceAudioOutGetLastOutputTime(int, std::uint64_t*);
|
||||
std::uint64_t APS5_VABI sceKernelGetProcessTime();
|
||||
}
|
||||
|
||||
static void Require(bool value, const char* message) {
|
||||
if (value) return;
|
||||
std::fprintf(stderr, "%s\n", message);
|
||||
std::abort();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int user = 0x10000000;
|
||||
constexpr int portTypeMain = 0;
|
||||
constexpr int portTypeVibration = 10;
|
||||
constexpr std::uint32_t formatS16Mono = 0;
|
||||
constexpr std::uint32_t frames = 256;
|
||||
constexpr std::uint32_t frequency = 48000;
|
||||
constexpr std::uint64_t untouched = 0xA5A5A5A5A5A5A5A5ull;
|
||||
constexpr int invalidPort = static_cast<int>(0x80260003);
|
||||
constexpr int invalidPointer = static_cast<int>(0x80260004);
|
||||
|
||||
int Open(int type) {
|
||||
const int handle = sceAudioOutOpen(user, type, 0, frames, frequency, formatS16Mono);
|
||||
Require(handle > 0, "port must open");
|
||||
return handle;
|
||||
}
|
||||
|
||||
std::uint64_t LastOutputTime(int handle) {
|
||||
std::uint64_t time = untouched;
|
||||
Require(sceAudioOutGetLastOutputTime(handle, &time) == 0, "an open port must report its last output time");
|
||||
return time;
|
||||
}
|
||||
|
||||
void Pause() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
|
||||
std::uint64_t OutputAndCheck(int handle, const std::vector<std::int16_t>& block) {
|
||||
Pause();
|
||||
const std::uint64_t before = sceKernelGetProcessTime();
|
||||
Require(sceAudioOutOutput(handle, block.data()) == static_cast<int>(frames), "output must accept the block");
|
||||
const std::uint64_t after = sceKernelGetProcessTime();
|
||||
const std::uint64_t time = LastOutputTime(handle);
|
||||
Require(time >= before && time <= after, "last output time must be the process time of the output");
|
||||
return time;
|
||||
}
|
||||
|
||||
void TestOutput(int type) {
|
||||
const std::vector<std::int16_t> block(frames, 256);
|
||||
const int handle = Open(type);
|
||||
Require(LastOutputTime(handle) == 0, "a port that never output must report 0");
|
||||
|
||||
const std::uint64_t first = OutputAndCheck(handle, block);
|
||||
Pause();
|
||||
Require(LastOutputTime(handle) == first, "the time must not move without an output");
|
||||
|
||||
const std::uint64_t second = OutputAndCheck(handle, block);
|
||||
Require(second > first, "a later output must report a later time");
|
||||
|
||||
Pause();
|
||||
Require(sceAudioOutOutput(handle, nullptr) == static_cast<int>(frames), "waiting for the output must succeed");
|
||||
Require(LastOutputTime(handle) == second, "waiting without data must not move the time");
|
||||
|
||||
Require(sceAudioOutClose(handle) == 0, "port must close");
|
||||
}
|
||||
|
||||
void TestPortsAreIndependent() {
|
||||
const std::vector<std::int16_t> block(frames, 256);
|
||||
const int played = Open(portTypeMain);
|
||||
const int silent = Open(portTypeMain);
|
||||
Require(played != silent, "two open ports must have different handles");
|
||||
OutputAndCheck(played, block);
|
||||
Require(LastOutputTime(silent) == 0, "an output must not move the time of another port");
|
||||
Require(sceAudioOutClose(played) == 0, "port must close");
|
||||
Require(sceAudioOutClose(silent) == 0, "port must close");
|
||||
}
|
||||
|
||||
void TestOutputs() {
|
||||
const std::vector<std::int16_t> block(frames, 256);
|
||||
const int first = Open(portTypeMain);
|
||||
const int second = Open(portTypeMain);
|
||||
const int waiting = Open(portTypeMain);
|
||||
AudioOutOutputParam params[3] = {{first, block.data()}, {second, block.data()}, {waiting, nullptr}};
|
||||
|
||||
Pause();
|
||||
const std::uint64_t before = sceKernelGetProcessTime();
|
||||
Require(sceAudioOutOutputs(params, 3) == static_cast<int>(frames), "outputs must accept the blocks");
|
||||
const std::uint64_t after = sceKernelGetProcessTime();
|
||||
|
||||
for (const int handle : {first, second}) {
|
||||
const std::uint64_t time = LastOutputTime(handle);
|
||||
Require(time >= before && time <= after, "outputs must set the time of every port that received data");
|
||||
}
|
||||
Require(LastOutputTime(waiting) == 0, "outputs must not move the time of a port that received no data");
|
||||
|
||||
for (const int handle : {first, second, waiting}) Require(sceAudioOutClose(handle) == 0, "port must close");
|
||||
}
|
||||
|
||||
void TestReopenedPort() {
|
||||
const std::vector<std::int16_t> block(frames, 256);
|
||||
const int handle = Open(portTypeMain);
|
||||
OutputAndCheck(handle, block);
|
||||
Require(sceAudioOutClose(handle) == 0, "port must close");
|
||||
const int reopened = Open(portTypeMain);
|
||||
Require(LastOutputTime(reopened) == 0, "a port opened after a close must not keep the time of the closed one");
|
||||
Require(sceAudioOutClose(reopened) == 0, "port must close");
|
||||
}
|
||||
|
||||
void TestErrors() {
|
||||
const std::vector<std::int16_t> block(frames, 256);
|
||||
const int handle = Open(portTypeMain);
|
||||
const std::uint64_t time = OutputAndCheck(handle, block);
|
||||
Require(sceAudioOutGetLastOutputTime(handle, nullptr) == invalidPointer, "a null destination must be rejected");
|
||||
Require(LastOutputTime(handle) == time, "a rejected call must not move the time");
|
||||
|
||||
std::uint64_t destination = untouched;
|
||||
Require(sceAudioOutGetLastOutputTime(0, &destination) == invalidPort, "handle 0 must be rejected");
|
||||
Require(sceAudioOutGetLastOutputTime(-1, &destination) == invalidPort, "a negative handle must be rejected");
|
||||
Require(sceAudioOutGetLastOutputTime(handle + 1, &destination) == invalidPort, "a port that is not open must be rejected");
|
||||
Require(sceAudioOutGetLastOutputTime(1000, &destination) == invalidPort, "an out of range handle must be rejected");
|
||||
Require(sceAudioOutClose(handle) == 0, "port must close");
|
||||
Require(sceAudioOutGetLastOutputTime(handle, &destination) == invalidPort, "a closed port must be rejected");
|
||||
Require(destination == untouched, "a rejected call must not write the destination");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
#ifdef _WIN32
|
||||
_putenv_s("SDL_AUDIODRIVER", "dummy");
|
||||
#else
|
||||
setenv("SDL_AUDIODRIVER", "dummy", 1);
|
||||
#endif
|
||||
sceKernelGetProcessTime();
|
||||
TestOutput(portTypeMain);
|
||||
TestOutput(portTypeVibration);
|
||||
TestPortsAreIndependent();
|
||||
TestOutputs();
|
||||
TestReopenedPort();
|
||||
TestErrors();
|
||||
return 0;
|
||||
}
|
||||
@@ -50,6 +50,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- [X+4jdIS75P0](../../core/libs/prx/libSceAudioIn/Export.cpp) (libSceAudioIn) - unknown name, signature
|
||||
- [sceAudioOutSetMixLevelPadSpk](../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - effect of a negative mix level unknown; only 0 to 32768 (0 dB) is accepted
|
||||
- [sceAudioOutSetMixLevelPadSpk](../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - no source applies the level (shadPS4 only stores it); it is assumed to be a linear gain on the port's own output, and the -9 dB default (11626) is applied to every pad speaker port, including those of titles that never call the function
|
||||
- [sceAudioOutGetLastOutputTime](../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - signature and behaviour taken from the [shadPS4](https://github.com/shadps4-emu/shadPS4) and [fpPS4](https://github.com/red-prig/fpPS4) reimplementations of the PS4 library, not confirmed on a PS5 title: the time is the process time at which `sceAudioOutOutput` or `sceAudioOutOutputs` accepted the port's last block, not the time the block reached the device
|
||||
- [AOWqIYsgVHs](../../core/libs/prx/libSceContentExport/Export.cpp) (libSceContentExport) - unknown name, signature
|
||||
- [GQTObcITIXI](../../core/libs/prx/libSceShare/Export.cpp) (libSceShare) - unknown name, signature
|
||||
- [BnMAMrsfVWo](../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libc) - unknown name, signature
|
||||
|
||||
Reference in New Issue
Block a user