mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-19 18:36:53 +02:00
DualSense haptics and controller speaker sound bluethooth (#969)
* pad: send DualSense speaker and haptic audio over Bluetooth * ci: fix Opus clang-cl build and macOS memory tracker test * docs: remove Bluetooth notes from project README * pad: keep Bluetooth audio work off emulation timing path * pad: smooth DualSense speaker audio on USB and Bluetooth * tests: find available macOS guest range for page manager * pad: keep haptic waveform when Bluetooth queue overflows * audio: reject zero output sample frequency * Simplify DualSense audio routing and fix Bluetooth batching --------- Co-authored-by: Codex <codex@openai.com> Co-authored-by: nmzik <Nmzik@mail.ru>
This commit is contained in:
+638
-38
@@ -1,11 +1,17 @@
|
||||
// Exercise the production module with real SDL streams and fake USB endpoints.
|
||||
#include <SDL3/SDL.h>
|
||||
#include <opus.h>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <semaphore>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
@@ -24,23 +30,32 @@ struct Rumble {
|
||||
Uint16 large, small;
|
||||
Uint32 duration;
|
||||
};
|
||||
std::vector<Device> devices;
|
||||
std::vector<SDL_AudioStream*> streams;
|
||||
Rumble rumble {};
|
||||
Uint64 now = 1000;
|
||||
int audio_refs = 0, opens = 0, rumble_calls = 0;
|
||||
bool fail_open = false, fail_resume = false;
|
||||
int actual_channels = 4;
|
||||
SDL_AudioDeviceID opened_device = 0;
|
||||
SDL_AudioSpec opened_spec {};
|
||||
std::array<Rumble, 4> pad_rumble {};
|
||||
std::array<std::vector<uint8_t>, 4> effects;
|
||||
std::vector<SDL_JoystickID> connected_pads {1};
|
||||
std::array<Rumble, 4> cached_rumble {};
|
||||
bool fail_effect = false, wireless = false;
|
||||
SDL_AudioStream* default_stream = nullptr;
|
||||
bool fail_default_resume = false;
|
||||
int active_controller = 1;
|
||||
std::vector<Device> devices;
|
||||
std::vector<SDL_AudioStream*> streams;
|
||||
Rumble rumble {};
|
||||
Uint64 now = 1000;
|
||||
int audio_refs = 0, opens = 0, rumble_calls = 0;
|
||||
bool fail_open = false, fail_resume = false;
|
||||
int actual_channels = 4;
|
||||
SDL_AudioDeviceID opened_device = 0;
|
||||
SDL_AudioSpec opened_spec {};
|
||||
std::array<Rumble, 4> pad_rumble {};
|
||||
std::array<std::vector<uint8_t>, 4> effects;
|
||||
std::vector<SDL_JoystickID> connected_pads {1};
|
||||
std::array<Rumble, 4> cached_rumble {};
|
||||
bool fail_effect = false, wireless = false, wireless_pad3 = false;
|
||||
bool hid_available = false, fail_hid_write = false;
|
||||
bool duplicate_hid = false;
|
||||
int hid_opens = 0;
|
||||
int opened_hid_pad = 0;
|
||||
std::vector<std::array<uint8_t, 547>> hid_reports;
|
||||
std::atomic_bool block_opus_encode {false}, block_hid_write {false};
|
||||
std::atomic_int hid_closes {0};
|
||||
std::binary_semaphore encode_started {0}, allow_encode {0};
|
||||
std::binary_semaphore write_started {0}, allow_write {0};
|
||||
SDL_AudioStream* default_stream = nullptr;
|
||||
bool fail_default_resume = false;
|
||||
int active_controller = 1;
|
||||
} // namespace
|
||||
|
||||
namespace Fake {
|
||||
@@ -116,8 +131,9 @@ void DestroyAudioStream(SDL_AudioStream* stream) {
|
||||
return;
|
||||
}
|
||||
const auto it = std::find(streams.begin(), streams.end(), stream);
|
||||
Check(it != streams.end(), "stream destroyed twice");
|
||||
streams.erase(it);
|
||||
if (it != streams.end()) {
|
||||
streams.erase(it);
|
||||
}
|
||||
if (stream == default_stream) {
|
||||
default_stream = nullptr;
|
||||
}
|
||||
@@ -134,6 +150,73 @@ SDL_Gamepad* GetGamepadFromID(SDL_JoystickID id) {
|
||||
return id == 1 || id == 3 ? reinterpret_cast<SDL_Gamepad*>(static_cast<uintptr_t>(id))
|
||||
: nullptr;
|
||||
}
|
||||
SDL_GamepadType GetGamepadType(SDL_Gamepad* pad) {
|
||||
return GetGamepadTypeForID(static_cast<SDL_JoystickID>(reinterpret_cast<uintptr_t>(pad)));
|
||||
}
|
||||
Uint16 GetGamepadVendor(SDL_Gamepad*) {
|
||||
return 0x054c;
|
||||
}
|
||||
Uint16 GetGamepadProduct(SDL_Gamepad*) {
|
||||
return 0x0ce6;
|
||||
}
|
||||
const char* GetGamepadSerial(SDL_Gamepad* pad) {
|
||||
return reinterpret_cast<uintptr_t>(pad) == 3 ? "aa-bb-cc-dd-ee-03" : "aa-bb-cc-dd-ee-01";
|
||||
}
|
||||
Uint64 GetTicksNS() {
|
||||
return now * 1000000;
|
||||
}
|
||||
SDL_Thread* CreateThread(SDL_ThreadFunction, const char*, void*) {
|
||||
return reinterpret_cast<SDL_Thread*>(1);
|
||||
}
|
||||
void WaitThread(SDL_Thread*, int*) {}
|
||||
SDL_hid_device_info* HidEnumerate(unsigned short, unsigned short) {
|
||||
if (!hid_available) {
|
||||
return nullptr;
|
||||
}
|
||||
static wchar_t serial1[] = L"AA:BB:CC:DD:EE:01";
|
||||
static wchar_t serial3[] = L"AA:BB:CC:DD:EE:03";
|
||||
static char path1[] = "pad-1";
|
||||
static char path3[] = "pad-3";
|
||||
static SDL_hid_device_info pad3 {}, pad1 {};
|
||||
pad3.path = path3;
|
||||
pad3.serial_number = duplicate_hid ? serial1 : serial3;
|
||||
pad3.bus_type = SDL_HID_API_BUS_BLUETOOTH;
|
||||
pad1.path = path1;
|
||||
pad1.serial_number = serial1;
|
||||
pad1.bus_type = SDL_HID_API_BUS_BLUETOOTH;
|
||||
pad1.next = &pad3;
|
||||
return &pad1;
|
||||
}
|
||||
void HidFreeEnumeration(SDL_hid_device_info*) {}
|
||||
SDL_hid_device* HidOpenPath(const char* path) {
|
||||
hid_opens++;
|
||||
opened_hid_pad = path[4] == '1' ? 1 : 3;
|
||||
return reinterpret_cast<SDL_hid_device*>(static_cast<uintptr_t>(opened_hid_pad));
|
||||
}
|
||||
void HidClose(SDL_hid_device*) {
|
||||
hid_closes++;
|
||||
}
|
||||
int HidWrite(SDL_hid_device*, const unsigned char* data, size_t size) {
|
||||
if (block_hid_write.exchange(false)) {
|
||||
write_started.release();
|
||||
allow_write.acquire();
|
||||
}
|
||||
if (fail_hid_write || size != 547) {
|
||||
return -1;
|
||||
}
|
||||
std::array<uint8_t, 547> report {};
|
||||
std::copy_n(data, report.size(), report.begin());
|
||||
hid_reports.push_back(report);
|
||||
return static_cast<int>(size);
|
||||
}
|
||||
opus_int32 OpusEncodeFloat(OpusEncoder* encoder, const float* pcm, int frames,
|
||||
unsigned char* packet, opus_int32 bytes) {
|
||||
if (block_opus_encode.exchange(false)) {
|
||||
encode_started.release();
|
||||
allow_encode.acquire();
|
||||
}
|
||||
return ::opus_encode_float(encoder, pcm, frames, packet, bytes);
|
||||
}
|
||||
SDL_JoystickID* GetGamepads(int* count) {
|
||||
*count = static_cast<int>(connected_pads.size());
|
||||
auto* ids = static_cast<SDL_JoystickID*>(
|
||||
@@ -142,8 +225,10 @@ SDL_JoystickID* GetGamepads(int* count) {
|
||||
ids[connected_pads.size()] = 0;
|
||||
return ids;
|
||||
}
|
||||
SDL_JoystickConnectionState GetGamepadConnectionState(SDL_Gamepad*) {
|
||||
return wireless ? SDL_JOYSTICK_CONNECTION_WIRELESS : SDL_JOYSTICK_CONNECTION_WIRED;
|
||||
SDL_JoystickConnectionState GetGamepadConnectionState(SDL_Gamepad* pad) {
|
||||
return wireless || (wireless_pad3 && reinterpret_cast<uintptr_t>(pad) == 3)
|
||||
? SDL_JOYSTICK_CONNECTION_WIRELESS
|
||||
: SDL_JOYSTICK_CONNECTION_WIRED;
|
||||
}
|
||||
bool RumbleGamepad(SDL_Gamepad* pad, Uint16 large, Uint16 small, Uint32 duration) {
|
||||
const auto id = reinterpret_cast<uintptr_t>(pad);
|
||||
@@ -173,22 +258,37 @@ bool SendGamepadEffect(SDL_Gamepad* pad, const void* data, int size) {
|
||||
}
|
||||
} // namespace Fake
|
||||
|
||||
#define SDL_GetTicks Fake::GetTicks
|
||||
#define SDL_InitSubSystem Fake::InitSubSystem
|
||||
#define SDL_QuitSubSystem Fake::QuitSubSystem
|
||||
#define SDL_GetAudioPlaybackDevices Fake::GetAudioPlaybackDevices
|
||||
#define SDL_GetAudioDeviceName Fake::GetAudioDeviceName
|
||||
#define SDL_GetAudioDeviceFormat Fake::GetAudioDeviceFormat
|
||||
#define SDL_OpenAudioDeviceStream Fake::OpenAudioDeviceStream
|
||||
#define SDL_ResumeAudioStreamDevice Fake::ResumeAudioStreamDevice
|
||||
#define SDL_DestroyAudioStream Fake::DestroyAudioStream
|
||||
#define SDL_GetAudioStreamDevice Fake::GetAudioStreamDevice
|
||||
#define SDL_GetGamepadTypeForID Fake::GetGamepadTypeForID
|
||||
#define SDL_GetTicks Fake::GetTicks
|
||||
#define SDL_InitSubSystem Fake::InitSubSystem
|
||||
#define SDL_QuitSubSystem Fake::QuitSubSystem
|
||||
#define SDL_GetAudioPlaybackDevices Fake::GetAudioPlaybackDevices
|
||||
#define SDL_GetAudioDeviceName Fake::GetAudioDeviceName
|
||||
#define SDL_GetAudioDeviceFormat Fake::GetAudioDeviceFormat
|
||||
#define SDL_OpenAudioDeviceStream Fake::OpenAudioDeviceStream
|
||||
#define SDL_ResumeAudioStreamDevice Fake::ResumeAudioStreamDevice
|
||||
#define SDL_DestroyAudioStream Fake::DestroyAudioStream
|
||||
#define SDL_GetAudioStreamDevice Fake::GetAudioStreamDevice
|
||||
#define SDL_GetGamepadTypeForID Fake::GetGamepadTypeForID
|
||||
#define SDL_GetGamepadType Fake::GetGamepadType
|
||||
#define SDL_GetGamepadVendor Fake::GetGamepadVendor
|
||||
#define SDL_GetGamepadProduct Fake::GetGamepadProduct
|
||||
#define SDL_GetGamepadSerial Fake::GetGamepadSerial
|
||||
#define SDL_GetTicksNS Fake::GetTicksNS
|
||||
#undef SDL_CreateThread
|
||||
#define SDL_CreateThread Fake::CreateThread
|
||||
#define SDL_WaitThread Fake::WaitThread
|
||||
#define SDL_hid_enumerate Fake::HidEnumerate
|
||||
#define SDL_hid_free_enumeration Fake::HidFreeEnumeration
|
||||
#define SDL_hid_open_path Fake::HidOpenPath
|
||||
#define SDL_hid_close Fake::HidClose
|
||||
#define SDL_hid_write Fake::HidWrite
|
||||
#define opus_encode_float Fake::OpusEncodeFloat
|
||||
#define SDL_GetGamepadFromID Fake::GetGamepadFromID
|
||||
#define SDL_RumbleGamepad Fake::RumbleGamepad
|
||||
#define SDL_SendGamepadEffect Fake::SendGamepadEffect
|
||||
#define SDL_GetGamepads Fake::GetGamepads
|
||||
#define SDL_GetGamepadConnectionState Fake::GetGamepadConnectionState
|
||||
#include "libs/dualSenseBluetooth.cpp"
|
||||
#include "libs/dualSenseHaptics.cpp"
|
||||
#include "libs/audio.cpp"
|
||||
#undef SDL_GetTicks
|
||||
@@ -202,6 +302,19 @@ bool SendGamepadEffect(SDL_Gamepad* pad, const void* data, int size) {
|
||||
#undef SDL_DestroyAudioStream
|
||||
#undef SDL_GetAudioStreamDevice
|
||||
#undef SDL_GetGamepadTypeForID
|
||||
#undef SDL_GetGamepadType
|
||||
#undef SDL_GetGamepadVendor
|
||||
#undef SDL_GetGamepadProduct
|
||||
#undef SDL_GetGamepadSerial
|
||||
#undef SDL_GetTicksNS
|
||||
#undef SDL_CreateThread
|
||||
#undef SDL_WaitThread
|
||||
#undef SDL_hid_enumerate
|
||||
#undef SDL_hid_free_enumeration
|
||||
#undef SDL_hid_open_path
|
||||
#undef SDL_hid_close
|
||||
#undef SDL_hid_write
|
||||
#undef opus_encode_float
|
||||
#undef SDL_GetGamepadFromID
|
||||
#undef SDL_RumbleGamepad
|
||||
#undef SDL_SendGamepadEffect
|
||||
@@ -228,6 +341,7 @@ double GetTimeMs() {
|
||||
|
||||
namespace {
|
||||
namespace Haptics = Libs::Controller::DualSenseHaptics;
|
||||
namespace Bluetooth = Libs::Controller::DualSenseBluetooth;
|
||||
using Port = std::unique_ptr<Haptics::Stream, decltype(&Haptics::Close)>;
|
||||
constexpr std::array<int, 2> unity {32768, 32768};
|
||||
constexpr std::array<float, 4> pcm {0.5f, 0.5f, 0.5f, 0.5f};
|
||||
@@ -240,11 +354,17 @@ struct Fixture {
|
||||
cached_rumble = {};
|
||||
effects = {};
|
||||
connected_pads = {1};
|
||||
fail_effect = wireless = false;
|
||||
default_stream = nullptr;
|
||||
fail_effect = wireless = wireless_pad3 = false;
|
||||
hid_available = fail_hid_write = false;
|
||||
duplicate_hid = false;
|
||||
hid_opens = 0;
|
||||
hid_closes = 0;
|
||||
opened_hid_pad = 0;
|
||||
hid_reports.clear();
|
||||
default_stream = nullptr;
|
||||
fail_default_resume = false;
|
||||
active_controller = 1;
|
||||
now = 1000;
|
||||
active_controller = 1;
|
||||
now = 1000;
|
||||
opens = rumble_calls = 0;
|
||||
fail_open = fail_resume = false;
|
||||
actual_channels = 4;
|
||||
@@ -255,8 +375,8 @@ struct Fixture {
|
||||
}
|
||||
};
|
||||
|
||||
Port Open(bool speaker = false) {
|
||||
auto* stream = Haptics::Open(48000, speaker);
|
||||
Port Open(bool speaker = false, uint32_t freq = 48000) {
|
||||
auto* stream = Haptics::Open(freq, speaker);
|
||||
Check(stream != nullptr, "port open failed");
|
||||
return Port(stream, Haptics::Close);
|
||||
}
|
||||
@@ -516,6 +636,446 @@ void TestSpeakerRequiresUnambiguousUsbDevice() {
|
||||
"USB to Bluetooth change retained the USB speaker");
|
||||
}
|
||||
|
||||
void TestBluetoothAudioAndIdentity() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
connected_pads = {1, 3};
|
||||
devices.clear(); // Bluetooth does not enumerate a quad USB audio endpoint.
|
||||
auto vibration = Open();
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> haptic(2048, 0.5f);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Check(Haptics::SetVibration(1, 100, 80) && pad_rumble[1].large == 100 * 0x101,
|
||||
"Bluetooth controller did not receive normal rumble");
|
||||
Check(Haptics::Queue(vibration.get(), 1, haptic.data(), 1024, 2, true, unity.data()) != 0,
|
||||
"Bluetooth haptics did not open");
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true, unity.data()) != 0,
|
||||
"Bluetooth speaker did not open");
|
||||
Check(hid_opens == 1 && opened_hid_pad == 1 && effects[1][7] == 0x30,
|
||||
"Bluetooth audio was not matched to the active controller");
|
||||
Check(pad_rumble[1].large == 0, "Bluetooth haptics did not suppress motor rumble");
|
||||
auto pending = Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000);
|
||||
Check(pending.size() == 1, "Bluetooth streams were not combined in one report");
|
||||
Libs::Controller::DualSenseBluetooth::Send(pending);
|
||||
Check(hid_reports.size() == 1, "Bluetooth audio report was not sent");
|
||||
const auto& report = hid_reports.back();
|
||||
Check(report[0] == 0x39 && report[2] == 0x91 && report[3] == 6 && report[4] == 0x7e &&
|
||||
report[10] == 0xd2 && report[11] == 64 && report[140] == 0xd3 && report[141] == 200,
|
||||
"Bluetooth audio sub-packets are malformed");
|
||||
Check(report[12] > 0 && report[13] > 0,
|
||||
"haptic waveform was not downsampled into the actuator channels");
|
||||
uint8_t prefix = 0xa2;
|
||||
auto crc = SDL_crc32(0, &prefix, 1);
|
||||
crc = SDL_crc32(crc, report.data(), report.size() - 4);
|
||||
for (size_t i = 0; i < 4; i++) {
|
||||
Check(report[report.size() - 4 + i] == static_cast<uint8_t>(crc >> (8 * i)),
|
||||
"Bluetooth audio CRC is invalid");
|
||||
}
|
||||
int opus_error = OPUS_OK;
|
||||
auto* decoder = opus_decoder_create(48000, 2, &opus_error);
|
||||
Check(decoder != nullptr && opus_error == OPUS_OK, "Opus decoder unavailable in test");
|
||||
std::array<float, 960> decoded {};
|
||||
Check(opus_decode_float(decoder, report.data() + 142, 200, decoded.data(), 480, 0) == 480,
|
||||
"Bluetooth speaker packet could not be decoded");
|
||||
opus_decoder_destroy(decoder);
|
||||
const auto mean = std::accumulate(decoded.begin(), decoded.end(), 0.0f) / decoded.size();
|
||||
Check(mean > 0.05f && mean < 0.35f, "Bluetooth speaker audio was not preserved");
|
||||
Check(Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000).empty(),
|
||||
"Bluetooth sender ignored report pacing");
|
||||
now += 300;
|
||||
Haptics::UpdateBluetoothRumble();
|
||||
Check(pad_rumble[1].large == 100 * 0x101,
|
||||
"Bluetooth haptics did not restore normal rumble afterward");
|
||||
|
||||
// A different active DualSense has a different HID address, even if both are
|
||||
// paired.
|
||||
Check(Haptics::Queue(vibration.get(), 3, haptic.data(), 1024, 2, true, unity.data()) != 0 &&
|
||||
opened_hid_pad == 3,
|
||||
"Bluetooth stream followed the wrong controller");
|
||||
Check(Haptics::Queue(speaker.get(), 3, sound.data(), 1024, 2, true, unity.data()) != 0,
|
||||
"Bluetooth speaker did not follow the selected controller");
|
||||
Check(effects[1][7] == 0, "old Bluetooth controller remained speaker-routed");
|
||||
wireless = false;
|
||||
connected_pads = {3};
|
||||
devices = {{20, "Speakers (DualSense Wireless Controller)", 4}};
|
||||
Check(Haptics::Queue(speaker.get(), 3, sound.data(), 1024, 2, true, unity.data()) != 0 &&
|
||||
opened_device == 20 && effects[3][7] == 0x30,
|
||||
"Bluetooth to USB change did not reopen the USB speaker immediately");
|
||||
speaker.reset();
|
||||
vibration.reset();
|
||||
}
|
||||
|
||||
void TestBluetoothFailureFallsBack() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true, unity.data()) != 0,
|
||||
"Bluetooth port did not initially open");
|
||||
fail_hid_write = true;
|
||||
auto pending = Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000);
|
||||
Libs::Controller::DualSenseBluetooth::Send(pending);
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true, unity.data()) == 0 &&
|
||||
effects[1][7] == 0,
|
||||
"failed HID write did not restore the normal audio fallback");
|
||||
speaker.reset();
|
||||
}
|
||||
|
||||
void TestBluetoothFormatsAndRoutingFailure() {
|
||||
Fixture f;
|
||||
wireless = hid_available = fail_effect = true;
|
||||
auto speaker = Open(true);
|
||||
Check(Haptics::Queue(speaker.get(), 1, pcm.data(), 2, 2, true, unity.data()) == 0 &&
|
||||
!Haptics::UsesBluetooth(speaker.get()) && hid_closes == 1 && effects[1].empty(),
|
||||
"failed Bluetooth speaker routing leaked a session or suppressed fallback");
|
||||
fail_effect = false;
|
||||
now += 2001;
|
||||
std::vector<int16_t> mono(1024, 16384);
|
||||
const int volume = 16384;
|
||||
Queue(speaker, mono.data(), 1024, 1, false, &volume);
|
||||
auto pending = Bluetooth::Prepare(now * 1000000);
|
||||
Check(pending.size() == 1 && hid_opens == 2 && effects[1][7] == 0x30 &&
|
||||
std::abs(pending[0].audio[200] - 0.25f) < 1e-4f &&
|
||||
std::abs(pending[0].audio[201] - 0.25f) < 1e-4f,
|
||||
"Bluetooth mono int16 volume mapping or speaker routing recovery failed");
|
||||
}
|
||||
|
||||
void TestBluetoothEncodingDoesNotBlockAudioQueue() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true, unity.data()) != 0,
|
||||
"Bluetooth speaker did not open for concurrent encoding test");
|
||||
block_opus_encode = true;
|
||||
std::thread encoder([&] {
|
||||
auto pending = Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000);
|
||||
Libs::Controller::DualSenseBluetooth::Send(pending);
|
||||
});
|
||||
encode_started.acquire();
|
||||
std::binary_semaphore queue_finished {0};
|
||||
std::atomic_bool queue_ok {false};
|
||||
std::thread audio([&] {
|
||||
queue_ok = Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true,
|
||||
unity.data()) != 0;
|
||||
queue_finished.release();
|
||||
});
|
||||
const bool queued_without_encoder =
|
||||
queue_finished.try_acquire_for(std::chrono::seconds(2));
|
||||
allow_encode.release();
|
||||
audio.join();
|
||||
encoder.join();
|
||||
Check(queued_without_encoder && queue_ok,
|
||||
"Bluetooth audio queue waited for Opus encoding");
|
||||
speaker.reset();
|
||||
}
|
||||
|
||||
void TestBluetoothCloseCancelsPreparedReport() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true);
|
||||
auto vibration = Open();
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Queue(speaker, sound.data(), 1024);
|
||||
Queue(vibration, sound.data(), 1024);
|
||||
const auto base = now * 1000000;
|
||||
auto pending = Bluetooth::Prepare(base);
|
||||
Check(pending.size() == 1, "Bluetooth report was not prepared before stream close");
|
||||
speaker.reset();
|
||||
Bluetooth::Send(pending);
|
||||
Check(hid_reports.empty(), "closed speaker's prepared report was sent");
|
||||
Queue(vibration, sound.data(), 1024);
|
||||
Bluetooth::Send(Bluetooth::Prepare(base + Bluetooth::PERIOD_NS));
|
||||
Check(hid_reports.size() == 1 && hid_reports.back()[140] == 0,
|
||||
"closing the speaker prevented the remaining haptic stream from sending");
|
||||
}
|
||||
|
||||
void TestBluetoothCloseDuringEncoding() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Queue(speaker, sound.data(), 1024);
|
||||
block_opus_encode = true;
|
||||
std::thread encoder([&] { Bluetooth::Send(Bluetooth::Prepare(now * 1000000)); });
|
||||
encode_started.acquire();
|
||||
std::binary_semaphore close_finished {0};
|
||||
std::thread closer([&] {
|
||||
speaker.reset();
|
||||
close_finished.release();
|
||||
});
|
||||
const bool closed_without_encoder = close_finished.try_acquire_for(std::chrono::seconds(2));
|
||||
const bool hid_alive_during_encode = hid_closes == 0;
|
||||
allow_encode.release();
|
||||
closer.join();
|
||||
encoder.join();
|
||||
Check(closed_without_encoder && hid_alive_during_encode && hid_closes == 1 &&
|
||||
hid_reports.empty(),
|
||||
"closing during encoding blocked, destroyed a live session, or sent stale audio");
|
||||
}
|
||||
|
||||
void TestBluetoothCloseWaitsForWriteWithoutBlockingQueue() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true);
|
||||
auto vibration = Open();
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Queue(speaker, sound.data(), 1024);
|
||||
Queue(vibration, sound.data(), 1024);
|
||||
auto pending = Bluetooth::Prepare(now * 1000000);
|
||||
Check(pending.size() == 1, "Bluetooth report was not prepared before blocked write");
|
||||
block_hid_write = true;
|
||||
std::thread sender([&] { Bluetooth::Send(pending); });
|
||||
write_started.acquire();
|
||||
std::binary_semaphore close_started {0}, close_finished {0}, queue_finished {0};
|
||||
std::thread closer([&] {
|
||||
close_started.release();
|
||||
speaker.reset();
|
||||
close_finished.release();
|
||||
});
|
||||
close_started.acquire();
|
||||
const bool closed_before_write = close_finished.try_acquire_for(std::chrono::milliseconds(50));
|
||||
std::atomic_bool queue_ok {false};
|
||||
std::thread audio([&] {
|
||||
queue_ok =
|
||||
Haptics::Queue(vibration.get(), 1, sound.data(), 1024, 2, true, unity.data()) != 0;
|
||||
queue_finished.release();
|
||||
});
|
||||
const bool queued_without_write = queue_finished.try_acquire_for(std::chrono::seconds(2));
|
||||
allow_write.release();
|
||||
audio.join();
|
||||
closer.join();
|
||||
sender.join();
|
||||
Check(!closed_before_write && queued_without_write && queue_ok && hid_reports.size() == 1 &&
|
||||
effects[1][7] == 0,
|
||||
"close failed to drain the HID write independently of the audio queue");
|
||||
}
|
||||
|
||||
void TestBluetoothSpeakerWaitsForItsAudioBlock() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto vibration = Open();
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> haptic(2048, 0.5f);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Check(Haptics::Queue(vibration.get(), 1, haptic.data(), 1024, 2, true, unity.data()) != 0,
|
||||
"Bluetooth vibration stream did not open");
|
||||
Check(Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000).empty(),
|
||||
"haptics sent a silent speaker packet before the audio batch arrived");
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 256, 2, true, unity.data()) != 0,
|
||||
"Bluetooth speaker did not accept a short block");
|
||||
Check(Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000).empty(),
|
||||
"short Bluetooth speaker block was padded with silence immediately");
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 768, 2, true, unity.data()) != 0,
|
||||
"Bluetooth speaker did not accept the remainder of its block");
|
||||
auto reports = Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000);
|
||||
Check(reports.size() == 1 && reports[0].audio[100] > 0.2f && reports[0].data[12] > 0,
|
||||
"Bluetooth speaker and haptics were not combined into a complete report");
|
||||
speaker.reset();
|
||||
vibration.reset();
|
||||
}
|
||||
|
||||
void TestBluetoothShortHapticBlock() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto vibration = Open();
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Queue(vibration, sound.data(), 256);
|
||||
const auto base = now * 1000000;
|
||||
Check(Bluetooth::Prepare(base).empty() && Bluetooth::Prepare(base + 1000000).empty(),
|
||||
"short haptic block was padded with silence before its remainder arrived");
|
||||
Queue(vibration, sound.data(), 768);
|
||||
Check(Bluetooth::Prepare(base + 1000000).empty(),
|
||||
"completed haptic block skipped its speaker coalescing window");
|
||||
auto pending = Bluetooth::Prepare(base + 2000000);
|
||||
Check(pending.size() == 1 && pending[0].data[12 + 100] > 0,
|
||||
"complete haptic block did not play after its remainder arrived");
|
||||
}
|
||||
|
||||
void TestBluetoothIncrementalBatchCoalesces() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto vibration = Open();
|
||||
auto speaker = Open(true);
|
||||
std::array<float, 512> sound;
|
||||
sound.fill(0.25f);
|
||||
for (int block = 0; block < 3; ++block) {
|
||||
Queue(vibration, sound.data(), 256);
|
||||
Queue(speaker, sound.data(), 256);
|
||||
Check(Bluetooth::Prepare(now * 1000000).empty(),
|
||||
"incremental Bluetooth batch was padded before it was complete");
|
||||
now += 5;
|
||||
}
|
||||
Queue(vibration, sound.data(), 256);
|
||||
Check(Bluetooth::Prepare(now * 1000000).empty(),
|
||||
"completed haptics sent before the same batch's final speaker block arrived");
|
||||
Queue(speaker, sound.data(), 256);
|
||||
auto pending = Bluetooth::Prepare(now * 1000000);
|
||||
Check(pending.size() == 1 && pending[0].audio[1800] > 0.2f && pending[0].data[12 + 100] > 0,
|
||||
"completed incremental speaker and haptics batch did not play immediately");
|
||||
}
|
||||
|
||||
void TestBluetoothLowRateCompleteBlock() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true, 8000);
|
||||
std::array<float, 171 * 2> sound;
|
||||
sound.fill(0.25f);
|
||||
Queue(speaker, sound.data(), 171);
|
||||
auto pending = Bluetooth::Prepare(now * 1000000);
|
||||
Check(pending.size() == 1 && pending[0].audio[1000] > 0.2f,
|
||||
"resampler lookahead delayed a complete low-rate speaker block");
|
||||
}
|
||||
|
||||
void TestBluetoothCompleteStreamDoesNotWaitForPartialPeer() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto complete = Open(true);
|
||||
auto partial = Open(true);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Queue(complete, sound.data(), 1024);
|
||||
Queue(partial, sound.data(), 256);
|
||||
auto pending = Bluetooth::Prepare(now * 1000000);
|
||||
Check(pending.size() == 1 && pending[0].audio[100] > 0.4f && pending[0].audio[1000] > 0.2f,
|
||||
"complete speaker stream waited for a partial peer instead of mixing available audio");
|
||||
}
|
||||
|
||||
void TestBluetoothShortFinalBlock() {
|
||||
for (bool speaker: {false, true}) {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto port = Open(speaker);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Queue(port, sound.data(), 256);
|
||||
const auto base = now * 1000000;
|
||||
Check(Bluetooth::Prepare(base).empty(),
|
||||
"short final Bluetooth block sent before more audio could arrive");
|
||||
Check(Bluetooth::Prepare(base + Bluetooth::PERIOD_NS).size() == 1,
|
||||
"short final Bluetooth block was never played");
|
||||
}
|
||||
}
|
||||
|
||||
void TestBluetoothOverflowKeepsRecentAudio() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> sound(2048);
|
||||
for (int block = 1; block <= 4; ++block) {
|
||||
std::fill(sound.begin(), sound.end(), block * 0.1f);
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true,
|
||||
unity.data()) != 0,
|
||||
"Bluetooth speaker overflow queue failed");
|
||||
}
|
||||
auto first = Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000);
|
||||
Check(first.size() == 1 && first[0].audio[200] > 0.15f && first[0].audio[200] < 0.25f,
|
||||
"Bluetooth queue overflow lost all buffered audio instead of the oldest block");
|
||||
auto second = Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000 +
|
||||
Libs::Controller::DualSenseBluetooth::PERIOD_NS);
|
||||
Check(second.size() == 1 && second[0].audio[200] > 0.25f && second[0].audio[200] < 0.35f,
|
||||
"Bluetooth queue overflow did not preserve the next audio block");
|
||||
speaker.reset();
|
||||
}
|
||||
|
||||
void TestBluetoothHapticOverflowKeepsRecentAudio() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto vibration = Open();
|
||||
std::vector<float> haptic(2048);
|
||||
for (int block = 1; block <= 4; ++block) {
|
||||
std::fill(haptic.begin(), haptic.end(), block * 0.1f);
|
||||
Check(Haptics::Queue(vibration.get(), 1, haptic.data(), 1024, 2, true,
|
||||
unity.data()) != 0,
|
||||
"Bluetooth haptic overflow queue failed");
|
||||
}
|
||||
const auto base = now * 1000000;
|
||||
Check(Libs::Controller::DualSenseBluetooth::Prepare(base).empty(),
|
||||
"haptic-only report skipped its coalescing window");
|
||||
auto first = Libs::Controller::DualSenseBluetooth::Prepare(base + 1000000);
|
||||
Check(first.size() == 1 && first[0].data[12 + 32] > 18 && first[0].data[12 + 32] < 32,
|
||||
"Bluetooth haptic overflow discarded buffered waveform instead of one report");
|
||||
vibration.reset();
|
||||
}
|
||||
|
||||
void TestBluetoothRecoversAfterLateWrite() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true,
|
||||
unity.data()) != 0,
|
||||
"Bluetooth speaker could not buffer three reports");
|
||||
}
|
||||
const auto base = now * 1000000;
|
||||
const auto period = Libs::Controller::DualSenseBluetooth::PERIOD_NS;
|
||||
Check(Libs::Controller::DualSenseBluetooth::Prepare(base).size() == 1,
|
||||
"first Bluetooth report was not prepared");
|
||||
Check(Libs::Controller::DualSenseBluetooth::Prepare(base + period + 10000000).size() == 1,
|
||||
"late Bluetooth report was not prepared");
|
||||
Check(Libs::Controller::DualSenseBluetooth::Prepare(base + 2 * period).size() == 1,
|
||||
"Bluetooth sender did not recover its schedule after a late write");
|
||||
speaker.reset();
|
||||
}
|
||||
|
||||
void TestUsbOverflowKeepsRecentAudio() {
|
||||
Fixture f;
|
||||
auto speaker = Open(true);
|
||||
std::vector<float> sound(2048);
|
||||
for (int block = 1; block <= 4; ++block) {
|
||||
std::fill(sound.begin(), sound.end(), block * 0.1f);
|
||||
Check(Haptics::Queue(speaker.get(), 1, sound.data(), 1024, 2, true,
|
||||
unity.data()) != 0,
|
||||
"USB speaker overflow queue failed");
|
||||
}
|
||||
std::array<float, 4> first {};
|
||||
Check(!streams.empty() &&
|
||||
SDL_GetAudioStreamData(streams.back(), first.data(), sizeof(first)) ==
|
||||
sizeof(first) &&
|
||||
first[0] > 0.15f && first[0] < 0.25f,
|
||||
"USB queue overflow did not preserve the newest buffered audio");
|
||||
// A single oversized input must also preserve only its newest 80 ms.
|
||||
sound.assign(4096 * 2, 0.1f);
|
||||
std::fill(sound.begin() + 256 * 2, sound.end(), 0.5f);
|
||||
const auto queued = Haptics::Queue(speaker.get(), 1, sound.data(), 4096, 2, true, unity.data());
|
||||
Check(queued == 80000 && SDL_GetAudioStreamQueued(streams.back()) == 3840 * 4 * sizeof(float) &&
|
||||
SDL_GetAudioStreamData(streams.back(), first.data(), sizeof(first)) ==
|
||||
sizeof(first) &&
|
||||
first[0] == 0.5f && first[1] == 0.5f,
|
||||
"oversized USB block did not retain only the newest 80 ms");
|
||||
speaker.reset();
|
||||
}
|
||||
|
||||
void TestBluetoothAmbiguousDevice() {
|
||||
Fixture f;
|
||||
wireless = hid_available = duplicate_hid = true;
|
||||
auto speaker = Open(true);
|
||||
Check(Haptics::Queue(speaker.get(), 1, pcm.data(), 2, 2, true, unity.data()) == 0 &&
|
||||
hid_opens == 0 && effects[1].empty(),
|
||||
"ambiguous Bluetooth devices were routed arbitrarily");
|
||||
speaker.reset();
|
||||
}
|
||||
|
||||
void TestUsbAndBluetoothFeatureParity() {
|
||||
Fixture f;
|
||||
connected_pads = {1, 3};
|
||||
wireless_pad3 = hid_available = true;
|
||||
auto wired_speaker = Open(true);
|
||||
auto wireless_speaker = Open(true);
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Check(Haptics::Queue(wired_speaker.get(), 1, sound.data(), 1024, 2, true, unity.data()) != 0 &&
|
||||
opened_device == 10,
|
||||
"Bluetooth pad disabled the wired pad speaker");
|
||||
Check(Haptics::Queue(wireless_speaker.get(), 3, sound.data(), 1024, 2, true, unity.data()) !=
|
||||
0 &&
|
||||
opened_hid_pad == 3,
|
||||
"wired pad disabled the Bluetooth speaker");
|
||||
Check(Haptics::Queue(wired_speaker.get(), 1, sound.data(), 1024, 2, true, unity.data()) != 0 &&
|
||||
effects[1][7] == 0x30 && effects[3][7] == 0,
|
||||
"returning to the wired speaker left Bluetooth routing active");
|
||||
wireless_speaker.reset();
|
||||
wired_speaker.reset();
|
||||
}
|
||||
|
||||
void TestAudioSpeakerFallback() {
|
||||
Fixture f;
|
||||
Libs::Audio::Audio audio;
|
||||
@@ -545,6 +1105,26 @@ void TestAudioSpeakerRouting() {
|
||||
ExpectPcm({0.5f, 0.5f, 0, 0, 0.5f, 0.5f, 0, 0});
|
||||
}
|
||||
|
||||
void TestBluetoothAudioSpeakerRouting() {
|
||||
Fixture f;
|
||||
wireless = hid_available = true;
|
||||
devices.clear();
|
||||
Libs::Audio::Audio audio;
|
||||
const auto port = audio.AudioOutOpen(4, 1024, 48000, Libs::Audio::Audio::Format::FloatStereo);
|
||||
Check(port.IsValid() && default_stream != nullptr, "Bluetooth pad speaker port did not open");
|
||||
std::vector<float> sound(2048, 0.25f);
|
||||
Libs::Audio::Audio::OutputParam output {port, sound.data()};
|
||||
Check(audio.AudioOutOutputs(&output, 1, false) == 1024 && hid_opens == 1 &&
|
||||
SDL_GetAudioStreamQueued(default_stream) == 0,
|
||||
"Bluetooth pad speaker also played on the host output");
|
||||
fail_hid_write = true;
|
||||
auto pending = Libs::Controller::DualSenseBluetooth::Prepare(now * 1000000);
|
||||
Libs::Controller::DualSenseBluetooth::Send(pending);
|
||||
Check(audio.AudioOutOutputs(&output, 1, false) == 1024 &&
|
||||
SDL_GetAudioStreamQueued(default_stream) > 0,
|
||||
"Bluetooth write failure did not use the normal speaker output");
|
||||
}
|
||||
|
||||
void TestAudioDefaultResumeFailure() {
|
||||
Fixture f;
|
||||
Libs::Audio::Audio audio;
|
||||
@@ -564,6 +1144,7 @@ int main() {
|
||||
SDL_SetHint(SDL_HINT_AUDIO_DRIVER, "dummy");
|
||||
TestAudioSpeakerFallback();
|
||||
TestAudioSpeakerRouting();
|
||||
TestBluetoothAudioSpeakerRouting();
|
||||
TestAudioDefaultResumeFailure();
|
||||
TestFormatsAndVolume();
|
||||
TestDiscoveryAndHotplug();
|
||||
@@ -571,6 +1152,25 @@ int main() {
|
||||
TestSpeakerUnplug();
|
||||
TestSpeakerSwitchAndFailures();
|
||||
TestSpeakerRequiresUnambiguousUsbDevice();
|
||||
TestBluetoothAudioAndIdentity();
|
||||
TestBluetoothFailureFallsBack();
|
||||
TestBluetoothFormatsAndRoutingFailure();
|
||||
TestBluetoothEncodingDoesNotBlockAudioQueue();
|
||||
TestBluetoothCloseCancelsPreparedReport();
|
||||
TestBluetoothCloseDuringEncoding();
|
||||
TestBluetoothCloseWaitsForWriteWithoutBlockingQueue();
|
||||
TestBluetoothSpeakerWaitsForItsAudioBlock();
|
||||
TestBluetoothShortHapticBlock();
|
||||
TestBluetoothIncrementalBatchCoalesces();
|
||||
TestBluetoothLowRateCompleteBlock();
|
||||
TestBluetoothCompleteStreamDoesNotWaitForPartialPeer();
|
||||
TestBluetoothShortFinalBlock();
|
||||
TestBluetoothOverflowKeepsRecentAudio();
|
||||
TestBluetoothHapticOverflowKeepsRecentAudio();
|
||||
TestBluetoothRecoversAfterLateWrite();
|
||||
TestUsbOverflowKeepsRecentAudio();
|
||||
TestBluetoothAmbiguousDevice();
|
||||
TestUsbAndBluetoothFeatureParity();
|
||||
TestFailuresAndBoundedQueue();
|
||||
TestRumbleLeaseAndDuration();
|
||||
TestSwitchStopsOldRumble();
|
||||
|
||||
Reference in New Issue
Block a user