Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..."]}
Expand Down
46 changes: 44 additions & 2 deletions tests/TizenGamepadInputTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,61 @@ 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();
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 = "";
Expand Down
9 changes: 5 additions & 4 deletions tests/TizenUiTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
38 changes: 31 additions & 7 deletions tizen/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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));
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -1162,6 +1179,7 @@ function currentPreferenceValues() {
? Number(frameRateSelect.value) : null,
frameInterpolation: interpolationSelect.value === "true",
performanceOverlay: performanceOverlaySelect.value === "true",
sendLogs: sendLogsSelect.value === "true",
};
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
72 changes: 59 additions & 13 deletions tizen/gamepad-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -228,6 +234,7 @@
pollState: neutralState(),
lastSentState: neutralState(),
hasSentState: false,
redundantSendsLeft: 0,
lastSendTime: 0,
rateWindowStart: performance.now(),
messagesInWindow: 0,
Expand All @@ -244,6 +251,7 @@
leftTrigger: 0,
rightTrigger: 0,
status: "idle",
loggedStatus: null,
},
};
this.nextControllerId += 1;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
});
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tizen/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ <h1 id="settings-heading" class="visually-hidden">Settings</h1>
<div class="setting-row setting-row-readonly"><span>Gateway address</span><span id="connection-gateway-address" class="setting-value"><strong>-</strong></span></div>
<div class="setting-row setting-row-readonly"><span>Sunshine</span><span id="connection-sunshine-state" class="setting-value"><strong>Unknown</strong></span></div>
<div class="setting-row setting-row-readonly"><span>WebRTC</span><span id="connection-webrtc-state" class="setting-value"><strong>Idle</strong></span></div>
<label class="setting-row" for="send-logs-select"><span>Send logs to PC<small class="setting-note">Writes the TV log into Sunshine's log, for troubleshooting</small></span><select id="send-logs-select" class="setting-select focusable"><option value="false">Off</option><option value="true">On</option></select></label>
</section>
<section class="settings-panel" data-settings-panel="about" hidden aria-label="About Moonlight WebRTC Client">
<div class="setting-row setting-row-readonly"><span>Application</span><span class="setting-value"><strong>Moonlight WebRTC Client</strong></span></div>
Expand Down
5 changes: 5 additions & 0 deletions tizen/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
frameRate: null,
frameInterpolation: false,
performanceOverlay: false,
sendLogs: false,
});

function copyDefaults() {
Expand All @@ -21,6 +22,7 @@
frameRate: DEFAULTS.frameRate,
frameInterpolation: DEFAULTS.frameInterpolation,
performanceOverlay: DEFAULTS.performanceOverlay,
sendLogs: DEFAULTS.sendLogs,
};
}

Expand Down Expand Up @@ -50,6 +52,9 @@
if (typeof candidate.performanceOverlay === "boolean") {
values.performanceOverlay = candidate.performanceOverlay;
}
if (typeof candidate.sendLogs === "boolean") {
values.sendLogs = candidate.sendLogs;
}
return values;
}

Expand Down
Loading