From c63444d02dbeb41ed748733aab9e3720316cdc34 Mon Sep 17 00:00:00 2001 From: mlopezsegura <90263997+mlopezsegura@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:23:56 +0100 Subject: [PATCH] Repeat gamepad changes on the lossy channel and make TV log forwarding optional The gamepad DataChannel never retransmits, so a lost release or stick return stood until the 250 ms keepalive: buttons stuck and sticks kept pushing. Every state change is now repeated on the next two polls, and the neutral state sent when a menu opens is repeated three times since polling stops behind it. Rumble renewals no longer log two lines each, only changes of outcome, and log() keeps its lines in an array instead of re-splitting the panel per call. Forwarding the TV log to Sunshine is now a setting (Connection > Send logs to PC), off by default; turning it off drops the lines still pending. Co-Authored-By: Claude Opus 5.5 --- docs/protocol.md | 3 +- tests/TizenGamepadInputTest.js | 46 +++++++++++++++++++++- tests/TizenUiTest.js | 9 +++-- tizen/app.js | 38 ++++++++++++++---- tizen/gamepad-input.js | 72 ++++++++++++++++++++++++++++------ tizen/index.html | 1 + tizen/preferences.js | 5 +++ 7 files changed, 147 insertions(+), 27 deletions(-) diff --git a/docs/protocol.md b/docs/protocol.md index 75f1d39..700a845 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -314,7 +314,8 @@ at most once a second per session. ## TV log -Once authenticated, the TV forwards its own log to Sunshine about once a second: +When **Settings → Connection → Send logs to PC** is on (it is off by default), and once authenticated, the TV +forwards its own log to Sunshine about once a second. Turning it off drops the lines still waiting to be sent: ```json {"version":2,"type":"client-log","lines":["21:04:31.512 Video stalled for 1000 ms ..."]} diff --git a/tests/TizenGamepadInputTest.js b/tests/TizenGamepadInputTest.js index 11245f7..dae36b7 100644 --- a/tests/TizenGamepadInputTest.js +++ b/tests/TizenGamepadInputTest.js @@ -337,12 +337,23 @@ const pollPad = createGamepad(0, "Xbox Wireless Controller", null); gamepads = [pollPad]; pollManager.resume(); +// The channel never retransmits, so every change is repeated on the next two polls and +// then the controller goes quiet until it changes again or the keepalive is due. +function pollTimes(count) { + for (let index = 0; index < count; index += 1) { + pollManager.poll(); + } +} + pollManager.poll(); +assert.strictEqual(pollInput.messages.length, 1, "the first poll must publish the controller state"); +pollTimes(4); const afterFirstPoll = pollInput.messages.length; -assert.ok(afterFirstPoll > 0, "the first poll must publish the controller state"); +assert.strictEqual(afterFirstPoll, 3, + "a new state must be repeated exactly twice in case a packet is lost"); pollManager.poll(); assert.strictEqual(pollInput.messages.length, afterFirstPoll, - "an unchanged controller state must not be resent"); + "an unchanged controller state must not be resent once its repeats are spent"); pollPad.buttons[0] = { pressed: true, value: 1 }; pollManager.poll(); @@ -350,6 +361,37 @@ assert.strictEqual(pollInput.messages.length, afterFirstPoll + 1, "a changed controller state must be sent"); assert.notStrictEqual(pollInput.messages[pollInput.messages.length - 1].buttons, 0, "the pressed button was lost by field-wise change detection"); +pollTimes(3); +const pressRepeats = pollInput.messages.slice(afterFirstPoll); +assert.strictEqual(pressRepeats.length, 3, "a press must be repeated twice and then stop"); +assert.ok(pressRepeats.every(function (message) { return message.buttons === pressRepeats[0].buttons; }), + "the repeats must carry the same state"); +assert.ok(pressRepeats[2].seq > pressRepeats[1].seq && pressRepeats[1].seq > pressRepeats[0].seq, + "each repeat needs a newer sequence or the host discards it as stale"); + +pollPad.buttons[0] = { pressed: false, value: 0 }; +pollManager.poll(); +pollPad.buttons[0] = { pressed: true, value: 1 }; +pollManager.poll(); +const restarted = pollInput.messages.length; +pollTimes(3); +assert.strictEqual(pollInput.messages.length, restarted + 2, + "a change during the repeats must restart them for the newest state"); + +// The poll loop stops behind a menu, so the neutral state has to be repeated on its own. +const neutralTimers = []; +global.setTimeout = function (callback) { neutralTimers.push(callback); return neutralTimers.length; }; +const beforePause = pollInput.messages.length; +pollManager.pauseForUi(); +while (neutralTimers.length > 0) { + neutralTimers.shift()(); +} +const neutralMessages = pollInput.messages.slice(beforePause); +assert.strictEqual(neutralMessages.length, 3, "opening a menu must send the neutral state three times"); +assert.ok(neutralMessages.every(function (message) { return message.buttons === 0; }), + "the state sent behind a menu must release every button"); +global.setTimeout = function () { return 0; }; +pollManager.resume(); pollPanelVisible = false; pollDiagnostics.controllers.textContent = ""; diff --git a/tests/TizenUiTest.js b/tests/TizenUiTest.js index d20a1ba..8a85520 100644 --- a/tests/TizenUiTest.js +++ b/tests/TizenUiTest.js @@ -222,7 +222,7 @@ const persistentStorage = storage(); const preferences = global.ClientPreferences.create(persistentStorage); assert.deepStrictEqual(preferences.load(), { resolution: null, codec: null, hdr: false, bitrateKbps: null, frameRate: null, frameInterpolation: false, - performanceOverlay: false, + performanceOverlay: false, sendLogs: false, }, "empty storage must use the current defaults"); preferences.update("resolution", "3840x2160"); preferences.update("codec", "hevc"); @@ -231,19 +231,20 @@ preferences.update("bitrateKbps", 50000); preferences.update("frameRate", 120); preferences.update("frameInterpolation", true); preferences.update("performanceOverlay", true); +preferences.update("sendLogs", true); assert.deepStrictEqual(global.ClientPreferences.create(persistentStorage).load(), { resolution: "3840x2160", codec: "hevc", hdr: true, bitrateKbps: 50000, - frameRate: 120, frameInterpolation: true, performanceOverlay: true, + frameRate: 120, frameInterpolation: true, performanceOverlay: true, sendLogs: true, }, "saved preferences must survive a simulated application reload"); assert.ok(appSource.includes("fps: Number(frameRateSelect.value) || 60,") && html.includes('id="fps-select"') && html.indexOf('src="frame-rate-support.js"') >= 0, "the frame rate comes from Settings: the TV's maximum when it announces one, otherwise the user's choice"); persistentStorage.setItem(global.ClientPreferences.STORAGE_KEY, JSON.stringify({ - resolution: 4, codec: false, hdr: "true", bitrateKbps: -1, frameRate: "120", frameInterpolation: "yes", performanceOverlay: 1, + resolution: 4, codec: false, hdr: "true", bitrateKbps: -1, frameRate: "120", frameInterpolation: "yes", performanceOverlay: 1, sendLogs: "on", })); assert.deepStrictEqual(global.ClientPreferences.create(persistentStorage).load(), { resolution: null, codec: null, hdr: false, bitrateKbps: null, frameRate: null, frameInterpolation: false, - performanceOverlay: false, + performanceOverlay: false, sendLogs: false, }, "invalid persisted values must fall back safely"); assert.strictEqual(global.ClientPreferences.resolveSupported("av1", ["h264", "hevc"], "h264"), "h264", "unsupported persisted options must fall back to the current supported default"); diff --git a/tizen/app.js b/tizen/app.js index 67af093..9b83892 100644 --- a/tizen/app.js +++ b/tizen/app.js @@ -57,6 +57,7 @@ const bitrateSelect = document.getElementById("bitrate-select"); const frameRateSelect = document.getElementById("fps-select"); const interpolationSelect = document.getElementById("interpolation-select"); const performanceOverlaySelect = document.getElementById("performance-overlay-select"); +const sendLogsSelect = document.getElementById("send-logs-select"); const interpolationStatusElement = document.getElementById("interpolation-status"); const interpolationCanvas = document.getElementById("interpolation-canvas"); const settingsSelectorMenu = document.getElementById("settings-selector-menu"); @@ -287,16 +288,26 @@ const GATEWAY_WATCH_RETRY_MS = 5000; // The TV's log, forwarded to the PC: Sunshine writes each line into its own log, next to // what it was doing at the same moment, so a stall can be read from one file on the PC // without a debugger attached to the TV. Lines wait here while the PC is unreachable; the -// oldest are dropped first, and how many is reported once the PC is back. +// oldest are dropped first, and how many is reported once the PC is back. Off unless the +// viewer turns it on in Settings: it is a troubleshooting aid, not something to run always. const REMOTE_LOG_FLUSH_MS = 1000; const REMOTE_LOG_MAX_PENDING = 500; const REMOTE_LOG_BATCH_LINES = 100; let remoteLogLines = []; let remoteLogDropped = 0; let remoteLogSupported = true; +let remoteLogEnabled = false; + +function setRemoteLogEnabled(enabled) { + remoteLogEnabled = enabled; + if (!enabled) { + remoteLogLines = []; + remoteLogDropped = 0; + } +} function remoteLog(message) { - if (!remoteLogSupported) { + if (!remoteLogSupported || !remoteLogEnabled) { return; } remoteLogLines.push(new Date().toISOString().slice(11, 23) + " " + String(message)); @@ -307,7 +318,7 @@ function remoteLog(message) { } function flushRemoteLog() { - if (!remoteLogSupported || !gatewayAuthorized || !socket + if (!remoteLogSupported || !remoteLogEnabled || !gatewayAuthorized || !socket || socket.readyState !== WebSocket.OPEN || remoteLogLines.length === 0) { return; } @@ -334,14 +345,20 @@ function isUnsupportedRemoteLog(message) { && String(message.message || "").indexOf("client-log") !== -1; } +// Kept as lines rather than re-split from the element on every call: log() runs on the +// main thread next to the gamepad poll loop, and splitting the whole panel each time was +// paid for in input timing. +const LOG_PANEL_LINES = 80; +const logPanelLines = []; + function log(message) { console.log(message); remoteLog(message); - logElement.textContent += message + "\n"; - const lines = logElement.textContent.split("\n"); - if (lines.length > 80) { - logElement.textContent = lines.slice(lines.length - 80).join("\n"); + logPanelLines.push(String(message)); + if (logPanelLines.length > LOG_PANEL_LINES) { + logPanelLines.shift(); } + logElement.textContent = logPanelLines.join("\n") + "\n"; logElement.scrollTop = logElement.scrollHeight; } @@ -1162,6 +1179,7 @@ function currentPreferenceValues() { ? Number(frameRateSelect.value) : null, frameInterpolation: interpolationSelect.value === "true", performanceOverlay: performanceOverlaySelect.value === "true", + sendLogs: sendLogsSelect.value === "true", }; } @@ -3148,6 +3166,10 @@ interpolationSelect.addEventListener("change", function () { persistCurrentPreferences(); applyFrameInterpolation(); }); +sendLogsSelect.addEventListener("change", function () { + persistCurrentPreferences(); + setRemoteLogEnabled(sendLogsSelect.value === "true"); +}); function updateRemoteTracksForVisibility() { const enabled = !document.hidden; @@ -4009,6 +4031,8 @@ log("Persistent storage: " + durableStorage.describe()); // back over the saved value. interpolationSelect.value = savedPreferences.frameInterpolation ? "true" : "false"; performanceOverlaySelect.value = savedPreferences.performanceOverlay ? "true" : "false"; +sendLogsSelect.value = savedPreferences.sendLogs ? "true" : "false"; +setRemoteLogEnabled(savedPreferences.sendLogs); applyPerformanceOverlay(); restoreCachedCapabilities(); updateInterpolationStatus(); diff --git a/tizen/gamepad-input.js b/tizen/gamepad-input.js index bc889d0..8003ed7 100644 --- a/tizen/gamepad-input.js +++ b/tizen/gamepad-input.js @@ -5,6 +5,11 @@ const MAX_CONTROLLERS = 16; const POLL_INTERVAL_MS = 1000 / 120; const KEEPALIVE_INTERVAL_MS = 250; + // The gamepad channel never retransmits. A lost press, release or stick return used to + // stand until the next keepalive, so a button stuck or a stick kept pushing for up to a + // quarter of a second. Repeating every change on the next polls covers a lost packet + // within one poll interval for two extra messages per change. + const REDUNDANT_SENDS = 2; // Input is polled at 120 Hz for latency; nobody can read numbers that fast. const DIAGNOSTICS_RENDER_INTERVAL_MS = 250; // A healthy gamepad channel carries well under 30 KB/s. Anything above this means the @@ -168,6 +173,7 @@ this.lastDiagnosticsRenderTime = 0; this.lastOverlayText = null; this.lastErrorLogTime = 0; + this.neutralTimer = null; } // send() rejects a payload whenever the channel closes between the readyState check @@ -228,6 +234,7 @@ pollState: neutralState(), lastSentState: neutralState(), hasSentState: false, + redundantSendsLeft: 0, lastSendTime: 0, rateWindowStart: performance.now(), messagesInWindow: 0, @@ -244,6 +251,7 @@ leftTrigger: 0, rightTrigger: 0, status: "idle", + loggedStatus: null, }, }; this.nextControllerId += 1; @@ -383,7 +391,7 @@ + " weak=" + String(desired.weakMagnitude); if (!actuator || typeof actuator.playEffect !== "function") { record.rumble.status = "no-actuator"; - this.options.log("Rumble skipped: " + details); + this.logRumbleOutcome(record, "Rumble skipped: " + details); this.updateDiagnostics(); return; } @@ -397,26 +405,37 @@ try { const result = actuator.playEffect("dual-rumble", parameters); record.rumble.status = "sent"; - this.options.log("Rumble playEffect: " + details + " result=" + String(result)); if (result && typeof result.then === "function") { result.then(function () { record.rumble.status = "accepted"; - manager.options.log("Rumble accepted: " + details); + manager.logRumbleOutcome(record, "Rumble accepted: " + details); manager.updateDiagnostics(); }, function (error) { record.rumble.status = "rejected"; - manager.options.log("Rumble rejected: " + details + " error=" + String(error)); + manager.logRumbleOutcome(record, "Rumble rejected: " + details + " error=" + String(error)); manager.updateDiagnostics(); }); + } else { + this.logRumbleOutcome(record, "Rumble playEffect: " + details + " result=" + String(result)); } this.updateDiagnostics(); } catch (error) { record.rumble.status = "failed"; - this.options.log("Rumble failed: " + details + " error=" + String(error)); + this.logRumbleOutcome(record, "Rumble failed: " + details + " error=" + String(error)); this.updateDiagnostics(); } }; + // A game renews rumble many times a second. Logging every renewal put two log lines per + // update on the main thread, next to the poll loop; only a change of outcome is news. + GamepadInputManager.prototype.logRumbleOutcome = function (record, message) { + if (record.rumble.loggedStatus === record.rumble.status) { + return; + } + record.rumble.loggedStatus = record.rumble.status; + this.options.log(message); + }; + GamepadInputManager.prototype.handleRumble = function (message) { const logicalControllerNumber = Number(message.controllerSlot); const controllerId = Number(message.controllerId); @@ -539,11 +558,14 @@ manager.announce(record); const state = completeState(gamepad, record.pollState); manager.observeStopShortcut(record, state); - if (!record.hasSentState || !statesEqual(record.lastSentState, state) + const changed = !record.hasSentState || !statesEqual(record.lastSentState, state); + if (changed || record.redundantSendsLeft > 0 || now - record.lastSendTime >= KEEPALIVE_INTERVAL_MS) { if (manager.sendState(record, state, now)) { copyState(record.lastSentState, state); record.hasSentState = true; + record.redundantSendsLeft = changed + ? REDUNDANT_SENDS : Math.max(0, record.redundantSendsLeft - 1); } } }); @@ -574,7 +596,35 @@ this.pollTimer = setTimeout(function () { manager.poll(); }, delay); }; + GamepadInputManager.prototype.cancelNeutralRepeats = function () { + if (this.neutralTimer !== null) { + clearTimeout(this.neutralTimer); + this.neutralTimer = null; + } + }; + + // The poll loop stops while a menu is open, so nothing else would follow the neutral + // state. If that one packet was lost, the host kept the last input held — a character + // running on its own behind the menu. The neutral state is repeated like any change. + GamepadInputManager.prototype.sendNeutralToAll = function (repeatsLeft) { + this.neutralTimer = null; + const now = performance.now(); + const neutral = neutralState(); + this.recordsByIndex.forEach(function (record) { + if (record.announced && this.sendState(record, neutral, now)) { + copyState(record.lastSentState, neutral); + } + }, this); + if (repeatsLeft > 0) { + const manager = this; + this.neutralTimer = setTimeout(function () { + manager.sendNeutralToAll(repeatsLeft - 1); + }, POLL_INTERVAL_MS); + } + }; + GamepadInputManager.prototype.resume = function () { + this.cancelNeutralRepeats(); this.enumerate(); if (!this.options.isStreaming()) { this.updateDiagnostics(); @@ -594,17 +644,13 @@ clearTimeout(this.pollTimer); this.pollTimer = null; } - const now = performance.now(); - const neutral = neutralState(); - this.recordsByIndex.forEach(function (record) { - if (record.announced) { - this.sendState(record, neutral, now); - } - }, this); + this.cancelNeutralRepeats(); + this.sendNeutralToAll(REDUNDANT_SENDS); this.updateDiagnostics(); }; GamepadInputManager.prototype.suspend = function (notifyGateway) { + this.cancelNeutralRepeats(); if (this.pollTimer !== null) { clearTimeout(this.pollTimer); this.pollTimer = null; diff --git a/tizen/index.html b/tizen/index.html index e4b336e..42f36f9 100644 --- a/tizen/index.html +++ b/tizen/index.html @@ -62,6 +62,7 @@

Settings

Gateway address-
SunshineUnknown
WebRTCIdle
+