Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

fix(ps4): guard saveScreen and readBackRgb memory allocation and extend autotest queue margin

Rafael Abud Ferreira9398a0fa7b95
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cpp
index 4f5e99d..387d7f4 100644
--- a/src/app/stream_player.cpp
+++ b/src/app/stream_player.cpp
@@ -736,16 +736,25 @@ struct StreamPlayer::Impl {
// The scan-out buffer just presented, at full resolution.
void saveScreen() {
- std::vector<uint8_t> rgb;
- int w = 0, h = 0;
- if (!display::readBackRgb(rgb, w, h)) return;
- std::string path = screenPath;
- std::thread([path, rgb = std::move(rgb), w, h]() mutable {
- std::string data = "P6\n" + std::to_string(w) + " " + std::to_string(h) + "\n255\n";
- data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());
- bool ok = platform::writeFileAtomic(path, data);
- XC_LOGI("screen -> %s: %s", path.c_str(), ok ? "ok" : "failed");
- }).detach();
+ try {
+ std::vector<uint8_t> rgb;
+ int w = 0, h = 0;
+ if (!display::readBackRgb(rgb, w, h)) return;
+ std::string path = screenPath;
+ std::thread([path, rgb = std::move(rgb), w, h]() mutable {
+ try {
+ std::string data = "P6\n" + std::to_string(w) + " " + std::to_string(h) + "\n255\n";
+ data.append(reinterpret_cast<const char*>(rgb.data()), rgb.size());
+ std::vector<uint8_t>().swap(rgb); // release the 6.2 MB vector immediately
+ bool ok = platform::writeFileAtomic(path, data);
+ XC_LOGI("screen -> %s: %s", path.c_str(), ok ? "ok" : "failed");
+ } catch (const std::exception& e) {
+ XC_LOGW("screen thread error: %s", e.what());
+ }
+ }).detach();
+ } catch (const std::exception& e) {
+ XC_LOGW("saveScreen failed: %s", e.what());
+ }
}
// Video thread only (data channel sends are thread-safe).
diff --git a/src/display/display_ps4.cpp b/src/display/display_ps4.cpp
index 0063906..d8099f2 100644
--- a/src/display/display_ps4.cpp
+++ b/src/display/display_ps4.cpp
@@ -545,19 +545,24 @@ bool lastShown(uint64_t& id, uint64_t& atUs) {
return true;
}
bool readBackRgb(std::vector<uint8_t>& rgb, int& width, int& height) {
- std::lock_guard<std::recursive_mutex> lock(g_frameMutex);
- if (!g_buffers[g_currentBuffer]) return false;
- width = kWidth;
- height = kHeight;
- const uint32_t* base = reinterpret_cast<const uint32_t*>(g_buffers[g_currentBuffer]);
- rgb.resize(static_cast<size_t>(kWidth) * kHeight * 3);
- for (size_t i = 0; i < static_cast<size_t>(kWidth) * kHeight; ++i) {
- uint32_t p = base[i];
- rgb[i * 3 + 0] = static_cast<uint8_t>(p);
- rgb[i * 3 + 1] = static_cast<uint8_t>(p >> 8);
- rgb[i * 3 + 2] = static_cast<uint8_t>(p >> 16);
+ try {
+ std::lock_guard<std::recursive_mutex> lock(g_frameMutex);
+ if (!g_buffers[g_currentBuffer]) return false;
+ width = kWidth;
+ height = kHeight;
+ const uint32_t* base = reinterpret_cast<const uint32_t*>(g_buffers[g_currentBuffer]);
+ rgb.resize(static_cast<size_t>(kWidth) * kHeight * 3);
+ for (size_t i = 0; i < static_cast<size_t>(kWidth) * kHeight; ++i) {
+ uint32_t p = base[i];
+ rgb[i * 3 + 0] = static_cast<uint8_t>(p);
+ rgb[i * 3 + 1] = static_cast<uint8_t>(p >> 8);
+ rgb[i * 3 + 2] = static_cast<uint8_t>(p >> 16);
+ }
+ return true;
+ } catch (const std::exception& e) {
+ XC_LOGW("display: readBackRgb failed: %s", e.what());
+ return false;
}
- return true;
}
} // namespace xc::display
diff --git a/src/stream/stream_session.cpp b/src/stream/stream_session.cpp
index 889d387..e24ad57 100644
--- a/src/stream/stream_session.cpp
+++ b/src/stream/stream_session.cpp
@@ -454,11 +454,17 @@ struct StreamSession::Impl {
// Same channels, in the same order, as the web client.
// Input channel is configured unordered and unreliable (0 retransmissions)
// to eliminate Head-of-Line Blocking input latency under network jitter/loss.
- chat = makeChannel("chat", "chatV1");
- chat->onMessage([](rtc::message_variant m) { XC_LOGI("chat channel: %s", asText(m).c_str()); });
- control = makeChannel("control", "controlV1");
- input = makeChannel("input", "1.0", /*unordered=*/true, /*maxRetransmits=*/0);
- message = makeChannel("message", "messageV1");
+ try {
+ chat = makeChannel("chat", "chatV1");
+ chat->onMessage([](rtc::message_variant m) { XC_LOGI("chat channel: %s", asText(m).c_str()); });
+ control = makeChannel("control", "controlV1");
+ input = makeChannel("input", "1.0", /*unordered=*/true, /*maxRetransmits=*/0);
+ message = makeChannel("message", "messageV1");
+ } catch (const std::exception& e) {
+ XC_LOGE("webrtc: createDataChannel failed: %s", e.what());
+ fail(std::string("WebRTC channel creation failed: ") + e.what());
+ return;
+ }
message->onOpen([this] {
XC_LOGI("data channel message open: sending handshake");
@@ -578,7 +584,12 @@ struct StreamSession::Impl {
return false;
}
XC_LOGI("webrtc: calling pc->setLocalDescription");
- pc->setLocalDescription(rtc::Description::Type::Offer);
+ try {
+ pc->setLocalDescription(rtc::Description::Type::Offer);
+ } catch (const std::exception& e) {
+ err = std::string("setLocalDescription failed: ") + e.what();
+ return false;
+ }
XC_LOGI("webrtc: setLocalDescription returned; waiting for ICE gathering");
if (!waitFor([this] { return gatheringDone; }, 10000))
XC_LOGW("ICE gathering did not complete; continuing with %zu candidates", localCandidates.size());
diff --git a/tools/ps4/autotest.sh b/tools/ps4/autotest.sh
index 1899860..eb16052 100755
--- a/tools/ps4/autotest.sh
+++ b/tools/ps4/autotest.sh
@@ -67,7 +67,7 @@ cleanup() {
}
trap cleanup EXIT
-deadline=$(( $(date +%s) + seconds + 90 ))
+deadline=$(( $(date +%s) + seconds + 180 ))
result="timeout"
echo "==> Waiting for test completion (deadline ${seconds}s + margin)..."