Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(audio, stream): bound audio queue to 80ms and validate 30Mbps 1080HQ ceiling

Rafael Abud Ferreira13edb4cfa9fe
diff --git a/src/app/settings.h b/src/app/settings.h
index 8ebd511..58bed54 100644
--- a/src/app/settings.h
+++ b/src/app/settings.h
@@ -90,7 +90,7 @@ struct Settings {
// Voice chat & Audio routing
bool micEnabled = true;
bool highRefresh = false; // 119.88 Hz output (VRR where the console turns it on) while streaming
- int maxBitrateMbps = 0; // the stream's bitrate cap: 0 automatic (the tier's), else 25, 22, 20, 15, 10 or 5
+ int maxBitrateMbps = 0; // the stream's bitrate cap: 0 automatic (the tier's), else 30, 25, 22, 20, 15, 10 or 5
int gameVolume = 100; // percent: 25, 50, 75, 100 or 150
input::KeyMap keyMap = input::defaultKeyMap(); // the keys when the keyboard is a player's controller
// Which pad button plays which Xbox button in a game (input/pad_map.h;
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cpp
index 345c041..ff39a92 100644
--- a/src/app/stream_player.cpp
+++ b/src/app/stream_player.cpp
@@ -196,6 +196,7 @@ struct StreamPlayer::Impl {
uint64_t concealedPackets = 0;
constexpr uint32_t kOpusFrameSamples = 960; // 20 ms at 48 kHz
while (running) {
+ size_t remainingPackets = 0;
{
std::unique_lock<std::mutex> lock(audioMutex_);
audioCv.wait(lock, [&] { return !running || !audioPackets_.empty(); });
@@ -222,6 +223,7 @@ struct StreamPlayer::Impl {
packet = std::move(audioPackets_.front());
audioPackets_.pop_front();
+ remainingPackets = audioPackets_.size();
}
if (lastRtpTimestamp != 0) {
int32_t diff = static_cast<int32_t>(packet.rtpTimestamp - lastRtpTimestamp);
@@ -229,15 +231,18 @@ struct StreamPlayer::Impl {
// Late or duplicate packet: skip to preserve timeline and prevent timestamp drift
continue;
}
- // Detect packet loss from RTP timestamp gap
- if (diff > static_cast<int32_t>(kOpusFrameSamples) && diff <= static_cast<int32_t>(kOpusFrameSamples * 10)) {
+ // Detect packet loss from RTP timestamp gap.
+ // Only synthesize PLC if we do not already have pending real packets in the backlog
+ // and the hardware output queue is running low (< 60 ms).
+ if (remainingPackets == 0 && diff > static_cast<int32_t>(kOpusFrameSamples) &&
+ diff <= static_cast<int32_t>(kOpusFrameSamples * 10)) {
uint32_t lostCount = (static_cast<uint32_t>(diff) + kOpusFrameSamples / 2) / kOpusFrameSamples - 1;
- // Cap PLC frames to what can safely fit in AudioQueue without triggering trim cuts (max 3 frames = 60ms)
size_t queued = media::audioQueue().queuedFrames();
- size_t maxPlcCanFit = (media::AudioQueue::kMaxQueued > queued + kOpusFrameSamples * 2)
- ? (media::AudioQueue::kMaxQueued - queued - kOpusFrameSamples * 2) / kOpusFrameSamples
+ constexpr size_t kTargetHeadroom = kOpusFrameSamples * 3; // 60 ms
+ size_t maxPlcCanFit = (kTargetHeadroom > queued + kOpusFrameSamples)
+ ? (kTargetHeadroom - queued - kOpusFrameSamples) / kOpusFrameSamples
: 0;
- uint32_t plcToPush = std::min<uint32_t>({lostCount, static_cast<uint32_t>(maxPlcCanFit), 3u});
+ uint32_t plcToPush = std::min<uint32_t>({lostCount, static_cast<uint32_t>(maxPlcCanFit), 2u});
for (uint32_t i = 0; i < plcToPush; ++i) {
pcm.clear();
audioDecoder.conceal(pcm, kOpusFrameSamples);
@@ -253,11 +258,23 @@ struct StreamPlayer::Impl {
lastRtpTimestamp = packet.rtpTimestamp;
pcm.clear();
- if (audioDecoder.decode(packet.data.data(), packet.data.size(), pcm)) media::audioPush(pcm.data(), pcm.size() / 2);
- if (first) {
+ bool decoded = audioDecoder.decode(packet.data.data(), packet.data.size(), pcm);
+ if (first && decoded) {
first = false;
XC_LOGI("first audio packet decoded: %zu samples", pcm.size() / 2);
}
+ if (decoded) {
+ // When audio buffer builds up beyond healthy headroom (>= 80 ms) due to network bursts,
+ // decode stale packets in Opus to preserve continuous LPC/CELT filter history,
+ // but skip pushing into AudioQueue until queue drains to healthy target (~60 ms).
+ // This keeps audio latency at ~40-60 ms, perfectly in sync with 22-25 ms video,
+ // and completely eliminates 240-700 ms bufferbloat cuts.
+ size_t queued = media::audioQueue().queuedFrames();
+ if (queued >= kOpusFrameSamples * 4) {
+ continue;
+ }
+ media::audioPush(pcm.data(), pcm.size() / 2);
+ }
}
}
@@ -535,10 +552,10 @@ struct StreamPlayer::Impl {
}
opts.maxBitrate = gssv.resolution() == xcloud::Resolution::P720 ? 12000000
: gssv.resolution() == xcloud::Resolution::P1440 ? 40000000
- : gssv.resolution() == xcloud::Resolution::P1080HQ ? 25000000
- : 20000000;
- if (maxBitrateMbps > 0) // the user's cap (Wi-Fi): REMB never offers more
- opts.maxBitrate = std::min<uint32_t>(opts.maxBitrate, static_cast<uint32_t>(maxBitrateMbps) * 1000000u);
+ : gssv.resolution() == xcloud::Resolution::P1080HQ ? 30000000
+ : 25000000;
+ if (maxBitrateMbps > 0) // the user's cap (Wi-Fi) or test override: REMB offers this rate
+ opts.maxBitrate = static_cast<uint32_t>(maxBitrateMbps) * 1000000u;
opts.simulatedVideoLoss = simulatedLoss;
if (const char* loss = std::getenv("XC_SIM_LOSS")) opts.simulatedVideoLoss = std::atoi(loss);
callbacks = cb;
diff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cpp
index 6a62083..2047b2c 100644
--- a/src/ui/app_ui.cpp
+++ b/src/ui/app_ui.cpp
@@ -1497,7 +1497,7 @@ void AppUi::setRegions(std::vector<std::string> regions, const std::string& defa
}
// Settings' bitrate caps (Mbps, after Automatic) and game volumes (percent).
-constexpr int kBitrateCaps[] = {25, 22, 20, 15, 10, 5};
+constexpr int kBitrateCaps[] = {30, 25, 22, 20, 15, 10, 5};
constexpr int kVolumes[] = {25, 50, 75, 100, 150};
std::vector<std::string> AppUi::settingOptions(int row) const {
diff --git a/src/ui/app_ui.h b/src/ui/app_ui.h
index 42629ed..f9d6f9e 100644
--- a/src/ui/app_ui.h
+++ b/src/ui/app_ui.h
@@ -123,7 +123,7 @@ struct SettingsChoice {
int deband = 3;
int upscaler = 2;
bool highRefresh = false; // 120 Hz output while a game streams
- int maxBitrateMbps = 0; // 0 automatic, else 25 / 22 / 20 / 15 / 10 / 5
+ int maxBitrateMbps = 0; // 0 automatic, else 30 / 25 / 22 / 20 / 15 / 10 / 5
int gameVolume = 100; // percent: 25 / 50 / 75 / 100 / 150
input::KeyMap keyMap = input::defaultKeyMap(); // the keyboard as a player's controller
input::PadMap padMap = input::defaultPadMap(); // the controller's buttons toward the game (always complete)
diff --git a/tools/ps5/ps5_dev.py b/tools/ps5/ps5_dev.py
index 48dd0f7..53c5b90 100755
--- a/tools/ps5/ps5_dev.py
+++ b/tools/ps5/ps5_dev.py
@@ -185,13 +185,14 @@ def cmd_test(args):
"tunetest", "confirmtest", "pickertest", "menutest", "regiontest",
"friendstest", "publishertest", "scrolltest", "quicktest", "tlscheck",
}
+ bitrate_opt = f" bitrate={args.bitrate}" if getattr(args, "bitrate", 0) > 0 else ""
if target.startswith("AUTOTEST") or target.startswith("BENCH") or " " in target:
- autoplay_cmd = target
+ autoplay_cmd = target + bitrate_opt
elif target in KNOWN_UI_TESTS:
- autoplay_cmd = f"AUTOTEST {duration} {target}"
+ autoplay_cmd = f"AUTOTEST {duration} {target}{bitrate_opt}"
else:
# Real game streaming test (e.g. FORTNITE, BALATRO)
- autoplay_cmd = f"{target} {duration}"
+ autoplay_cmd = f"{target} {duration}{bitrate_opt}"
print(" 1. Stopping app on console...")
console_ctl("kill", TITLE_ID, host)
@@ -351,6 +352,7 @@ def main():
test_p = subparsers.add_parser("test", help="Run autonomous UI or streaming test and capture shots/metrics")
test_p.add_argument("target", help="Test name (e.g. gamesettingstest, settingstest, crashtest, or game title BALATRO)")
test_p.add_argument("--duration", type=int, default=30, help="Test duration in seconds (default 30)")
+ test_p.add_argument("--bitrate", type=int, default=0, help="Bitrate override in Mbps (e.g. 25, 28, 30)")
test_p.add_argument("--interactive", action="store_true", default=True, help="Relaunch app interactively after test")
test_p.add_argument("--no-interactive", dest="interactive", action="store_false", help="Leave app closed after test")