Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(audio): eliminate crackling, jitter underruns and background hydration during stream

Rafael Abud Ferreira17dbd2c508a1
diff --git a/src/app/autoplay.cpp b/src/app/autoplay.cpp
index 01d60f0..da594d5 100644
--- a/src/app/autoplay.cpp
+++ b/src/app/autoplay.cpp
@@ -40,6 +40,7 @@ const char* gridShot = nullptr; // "gridtest": save grid screenshot
const char* gameSettingsShot = nullptr; // "gamesettingstest": save screenshot
std::string gameSettingsKey; // the game whose profile the test changes
std::optional<GameProfile> gameSettingsBefore; // its profile before (none: it had none)
+std::optional<Settings> globalSettingsBefore; // global settings before
std::optional<input::KeyMap> keysBefore; // "keystest": the keys before, put back at the end
std::optional<input::PadMap> padMapBefore; // "padmaptest": the controller buttons before, put back at the end
const char* regionShot = nullptr; // "regiontest": save this screen next
@@ -225,6 +226,7 @@ void autoplayPad(input::ControllerState& pad) {
gameSettingsKey = app::gameProfileKey(tile.productId, tile.titleId);
auto it = g_settings.perGame.find(gameSettingsKey);
gameSettingsBefore = it != g_settings.perGame.end() ? std::optional<GameProfile>(it->second) : std::nullopt;
+ globalSettingsBefore = g_settings;
}
}
// Step 1: Wait for details to draw, then take screenshot
@@ -288,6 +290,9 @@ void autoplayPad(input::ControllerState& pad) {
g_settings.perGame[gameSettingsKey] = *gameSettingsBefore;
else
g_settings.perGame.erase(gameSettingsKey);
+ if (globalSettingsBefore) {
+ g_settings.resolution = globalSettingsBefore->resolution;
+ }
g_settings.save(settingsPath());
g_ui->setPerGameSettings(g_settings.perGame);
}
@@ -611,15 +616,27 @@ void autoplayNav(ui::NavInput& nav, bool& menuCombo, uint64_t now) {
// Settings, down to the resolution, open its list; saved 2 s later.
static uint64_t since = 0;
static int step = 0;
- if (!since) since = now;
- const uint64_t at[] = {500, 1500, 2500, 4500};
- if (step < 4 && now - since >= at[step]) {
+ static std::optional<ui::SettingsChoice> settingsBefore;
+ if (!since) {
+ since = now;
+ settingsBefore = g_ui->settings();
+ }
+ const uint64_t at[] = {500, 1500, 2500, 4500, 5200};
+ if (step < 5 && now - since >= at[step]) {
if (step == 0) nav.options = true;
if (step == 1) nav.down = true;
if (step == 2) nav.accept = true;
if (step == 3) {
settingsShot = true; // saved below, once drawn
+ }
+ if (step == 4) {
+ if (settingsBefore) {
+ g_ui->setSettings(*settingsBefore);
+ saveSettingsChoice(*settingsBefore);
+ }
+ g_ui->showHome();
g_autoplay.settingsTest = false;
+ XC_LOGI("AUTOPLAY END: settings test (settings put back as they were)");
}
++step;
}
diff --git a/src/app/library.cpp b/src/app/library.cpp
index a489d84..813a380 100644
--- a/src/app/library.cpp
+++ b/src/app/library.cpp
@@ -633,9 +633,7 @@ bool Library::fetchFull(const std::vector<std::string>& ids, const Changed& chan
std::set<std::string> queued;
for (const auto& id : ids) {
auto it = products_.find(id);
- if ((it == products_.end() || it->second.heroUrl.empty() || it->second.description.empty() ||
- !it->second.detailed || !it->second.plan) &&
- queued.insert(id).second)
+ if ((it == products_.end() || !it->second.detailed) && queued.insert(id).second)
order.push_back(id);
}
// Batches of 20 (~10 KB per product: bigger ones made the catalog time
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cpp
index 89ca3f4..bdbd0d7 100644
--- a/src/app/stream_player.cpp
+++ b/src/app/stream_player.cpp
@@ -69,6 +69,7 @@ struct StreamPlayer::Impl {
std::atomic<bool> running{false};
std::string endReason;
platform::Thread videoThread, audioThread;
+ std::mutex audioMutex_;
std::deque<std::vector<uint8_t>> audioPackets_;
std::condition_variable audioCv;
@@ -168,6 +169,9 @@ struct StreamPlayer::Impl {
}
running = false;
cv.notify_all();
+ {
+ std::lock_guard<std::mutex> lock(audioMutex_);
+ }
audioCv.notify_all();
}
@@ -176,7 +180,7 @@ struct StreamPlayer::Impl {
bool first = true;
while (running) {
{
- std::unique_lock<std::mutex> lock(mutex);
+ std::unique_lock<std::mutex> lock(audioMutex_);
audioCv.wait(lock, [&] { return !running || !audioPackets_.empty(); });
if (!running) break;
packet = std::move(audioPackets_.front());
@@ -365,9 +369,9 @@ struct StreamPlayer::Impl {
return;
}
if (++videoFrames == 1) XC_LOGI("first video frame received: %zu bytes", n);
- {
- // The H.264 profile the server encodes with (from each new SPS).
- static int lastProfile = -1, lastLevel = -1;
+ static int lastProfile = -1, lastLevel = -1;
+ if (lastProfile == -1) {
+ // The H.264 profile the server encodes with (from the first SPS).
for (size_t i = 0; i + 6 < n; ++i)
if (d[i] == 0 && d[i + 1] == 0 && d[i + 2] == 1 && (d[i + 3] & 0x1F) == 7) {
int profile = d[i + 4], level = d[i + 6];
@@ -393,7 +397,7 @@ struct StreamPlayer::Impl {
};
cb.audio = [this](const uint8_t* d, size_t n, uint32_t) {
if (++audioPackets == 1) XC_LOGI("first audio packet: %zu bytes", n);
- std::lock_guard<std::mutex> lock(mutex);
+ std::lock_guard<std::mutex> lock(audioMutex_);
if (audioPackets_.size() < 50) audioPackets_.emplace_back(d, d + n);
audioCv.notify_one();
};
diff --git a/src/app/worker.cpp b/src/app/worker.cpp
index 92aeb36..8ca27c9 100644
--- a/src/app/worker.cpp
+++ b/src/app/worker.cpp
@@ -782,6 +782,7 @@ void worker() {
if (!g_quit) loadConsoles(am, home);
break;
case kPlay: {
+ stopHydration();
ui::GameTile tile;
{
std::lock_guard<std::mutex> lock(g_argMutex);
diff --git a/src/media/audio_queue.cpp b/src/media/audio_queue.cpp
index 7586141..5ab11e3 100644
--- a/src/media/audio_queue.cpp
+++ b/src/media/audio_queue.cpp
@@ -19,6 +19,14 @@ void AudioQueue::push(const float* interleaved, size_t frames) {
size_t cut = queue_.size() - kTrimTo * 2;
stats_.trimmed += cut / 2;
queue_.erase(queue_.begin(), queue_.begin() + static_cast<long>(cut));
+ // Soft ramp-in on the trimmed front (1 ms = 48 stereo frames) to eliminate DC step pops
+ constexpr size_t kRamp = 48;
+ size_t rampN = std::min(kRamp * 2, queue_.size());
+ for (size_t i = 0; i < rampN; i += 2) {
+ float f = static_cast<float>(i / 2) / static_cast<float>(kRamp);
+ queue_[i] *= f;
+ queue_[i + 1] *= f;
+ }
}
}
@@ -28,7 +36,6 @@ void AudioQueue::pull(float* out, size_t frames) {
size_t depth = queue_.size() / 2;
stats_.depthSum += depth;
stats_.depthMax = std::max<uint64_t>(stats_.depthMax, depth);
- if (depth == 0) ++stats_.empty;
++stats_.pulls;
stats_.frames += frames;
if (stats_.frames >= static_cast<uint64_t>(kRate) * 10) { // ~10 s (the PS5's 1875 grains of 256)
@@ -37,10 +44,27 @@ void AudioQueue::pull(float* out, size_t frames) {
static_cast<unsigned long long>(stats_.empty), stats_.trimmed * 1000.0 / kRate);
stats_ = {};
}
+ if (queue_.empty()) {
+ ++stats_.empty;
+ std::fill(out, out + frames * 2, 0.0f);
+ return;
+ }
size_t n = std::min(frames * 2, queue_.size());
std::copy(queue_.begin(), queue_.begin() + static_cast<long>(n), out);
queue_.erase(queue_.begin(), queue_.begin() + static_cast<long>(n));
- std::fill(out + n, out + frames * 2, 0.0f);
+ if (n < frames * 2) {
+ // Soft ramp-out on the tail of partial buffer to eliminate underrun pops
+ constexpr size_t kFade = 48;
+ if (n >= kFade * 2) {
+ for (size_t i = 0; i < kFade * 2; i += 2) {
+ float f = 1.0f - static_cast<float>(i / 2) / static_cast<float>(kFade);
+ out[n - kFade * 2 + i] *= f;
+ out[n - kFade * 2 + i + 1] *= f;
+ }
+ }
+ std::fill(out + n, out + frames * 2, 0.0f);
+ ++stats_.empty;
+ }
}
if (float v = volume_.load(); v != 1.0f)
for (size_t i = 0; i < frames * 2; ++i) out[i] = std::clamp(out[i] * v, -1.0f, 1.0f);
diff --git a/src/media/audio_queue.h b/src/media/audio_queue.h
index 60204cc..e913cef 100644
--- a/src/media/audio_queue.h
+++ b/src/media/audio_queue.h
@@ -20,10 +20,10 @@ namespace xc::media {
class AudioQueue {
public:
static constexpr int kRate = 48000;
- // It used to hold up to 120 ms: that much delay behind the picture
- // (Moonlight keeps 40-50 ms). Stereo frames.
- static constexpr size_t kMaxQueued = kRate * 60 / 1000;
- static constexpr size_t kTrimTo = kRate * 40 / 1000;
+ // Bounded to keep the sound close to the picture while absorbing normal
+ // network jitter (20-40 ms bursts). Stereo frames.
+ static constexpr size_t kMaxQueued = kRate * 100 / 1000; // 100 ms
+ static constexpr size_t kTrimTo = kRate * 60 / 1000; // 60 ms
// Since the last log (reset by it).
struct Stats {
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 8f5d023..d5cac44 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -1540,7 +1540,7 @@ static void testNullBackends() {
// --- M1 input / audio backends (pure parts, null backends) ----------------
-// The audio queue: bounded at ~60 ms (cut back to 40 ms, oldest first),
+// The audio queue: bounded at ~100 ms (cut back to 60 ms, oldest first),
// silence past its end, the volume, and what the periodic log counts.
static void testAudioQueue() {
media::AudioQueue q;
@@ -1548,22 +1548,22 @@ static void testAudioQueue() {
q.push(in.data(), 480);
CHECK(q.queuedFrames() == 0); // closed: ignored
q.open();
- for (int i = 0; i < 5; ++i) q.push(in.data(), 480); // 50 ms: under the bound
- CHECK(q.queuedFrames() == 2400 && q.stats().trimmed == 0);
- q.push(in.data(), 480);
- q.push(in.data(), 480); // 70 ms: past 60, back to 40
- CHECK(q.queuedFrames() == media::AudioQueue::kTrimTo && q.stats().trimmed == 3360 - 1920);
+ for (int i = 0; i < 9; ++i) q.push(in.data(), 480); // 90 ms: under the bound
+ CHECK(q.queuedFrames() == 4320 && q.stats().trimmed == 0);
+ q.push(in.data(), 480); // 100 ms
+ q.push(in.data(), 480); // 110 ms: past 100, back to 60
+ CHECK(q.queuedFrames() == media::AudioQueue::kTrimTo && q.stats().trimmed == 5280 - 2880);
// The oldest go first: mark the newest frame and find it last.
float mark[2] = {0.5f, -0.5f};
q.push(mark, 1);
- std::vector<float> out(1921 * 2);
- q.pull(out.data(), 1921);
- CHECK(out[1920 * 2] == 0.5f && out[1920 * 2 + 1] == -0.5f && out[0] == 0.25f);
+ std::vector<float> out(2881 * 2);
+ q.pull(out.data(), 2881);
+ CHECK(out[2880 * 2] == 0.5f && out[2880 * 2 + 1] == -0.5f && out[100 * 2] == 0.25f);
CHECK(q.queuedFrames() == 0);
q.pull(out.data(), 256); // empty: silence, counted
CHECK(out[0] == 0.0f && out[511] == 0.0f);
media::AudioQueue::Stats st = q.stats();
- CHECK(st.pulls == 2 && st.empty == 1 && st.frames == 1921 + 256 && st.depthMax == 1921);
+ CHECK(st.pulls == 2 && st.empty == 1 && st.frames == 2881 + 256 && st.depthMax == 2881);
// Volume: clamped to 0..2, applied and clipped at full scale.
q.setVolume(5.0f);
CHECK(q.volume() == 2.0f);
@@ -1575,9 +1575,9 @@ static void testAudioQueue() {
CHECK(q.volume() == 0.0f);
q.setVolume(1.0f);
// ~10 s of sound pulled (480000 frames): logged, and the counts start
- // again. 2179 frames so far, so the 1867th grain of 256 crosses it.
+ // again. 3139 frames so far, so the 1863rd grain of 256 crosses it.
for (int i = 0; i < 1875; ++i) q.pull(out.data(), 256);
- CHECK(q.stats().pulls == 8 && q.stats().frames == 8 * 256 && q.stats().trimmed == 0);
+ CHECK(q.stats().pulls == 12 && q.stats().frames == 12 * 256 && q.stats().trimmed == 0);
q.close();
q.push(loud, 2);
CHECK(q.queuedFrames() == 0);
@@ -1732,8 +1732,8 @@ static void testNullBackendsM1() {
CHECK(!kbm.keyboard && !kbm.mouse && !kbm.moved && kbm.dx == 0);
input::kbmSetCaptured(false);
CHECK(media::audioStart() && media::audioStart());
- std::vector<float> sound(48 * 2 * 100, 0.1f); // 100 ms, never played: kept to the bound
- media::audioPush(sound.data(), 4800);
+ std::vector<float> sound(48 * 2 * 110, 0.1f); // 110 ms, never played: kept to the bound
+ media::audioPush(sound.data(), 48 * 110);
CHECK(media::audioQueue().queuedFrames() == media::AudioQueue::kTrimTo);
media::audioStop();
CHECK(media::audioQueue().queuedFrames() == 0);