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
18 changes: 12 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Tests every push and pull request, builds MoonlightWebRTC.wgt, and publishes it as a GitHub
# release when a version tag (v2.0.0, v2.1.0-beta.1, ...) is pushed.
# Tests every push and pull request, builds MoonlightWebRTC.wgt (Game Mode on) and
# MoonlightWebRTC-NoGameMode.wgt, and publishes both as a GitHub release when a version tag
# (v2.0.0, v2.1.0-beta.1, ...) is pushed.
name: Build and release

on:
Expand Down Expand Up @@ -103,19 +104,23 @@ jobs:
throw "Unable to extract the Emscripten SDK."
}

- name: Build MoonlightWebRTC.wgt
- name: Build the WGT packages
shell: pwsh
run: |
./packaging/tizen/build-package.ps1 `
-TizenCli (Join-Path $env:TIZEN_STUDIO 'tools\ide\bin\tizen.bat') `
-EmsdkRoot (Join-Path $env:EMSDK_DIRECTORY 'emscripten-release-bundle\emsdk')
$buildArguments = @{
TizenCli = Join-Path $env:TIZEN_STUDIO 'tools\ide\bin\tizen.bat'
EmsdkRoot = Join-Path $env:EMSDK_DIRECTORY 'emscripten-release-bundle\emsdk'
}
./packaging/tizen/build-package.ps1 @buildArguments
./packaging/tizen/build-package.ps1 @buildArguments -NoGameMode
./packaging/release/generate-checksums.ps1

- uses: actions/upload-artifact@v4
with:
name: MoonlightWebRTC
path: |
dist/tizen/MoonlightWebRTC.wgt
dist/tizen/MoonlightWebRTC-NoGameMode.wgt
dist/SHA256SUMS.txt
if-no-files-found: error

Expand All @@ -141,6 +146,7 @@ jobs:
if [[ "$TAG" == *-* ]]; then prerelease=(--prerelease); fi
gh release create "$TAG" \
release/tizen/MoonlightWebRTC.wgt \
release/tizen/MoonlightWebRTC-NoGameMode.wgt \
release/SHA256SUMS.txt \
--repo "$GITHUB_REPOSITORY" \
--title "Moonlight WebRTC $TAG" \
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ Sunshine-Web-RTC: capture → encode → WebRTC RTP → TV
PC, so the encoder gets the bitrate the TV asked for, less only audio and RTP overhead. WebRTC on the LAN
repairs losses with retransmission and keyframe requests instead.
- **Faster recovery.** When the TV asks for a keyframe, the request goes directly to Sunshine's encoder.
- **No waiting for the audio or the jitter buffer.** Sunshine marks every video packet with a zero
playout delay, so the TV shows each frame the moment it is decoded instead of buffering it, as Stadia
did; the TV also asks for the shortest jitter buffer and does not hold the picture back to stay in
sync with the audio, as Moonlight does not either. Screen to screen, 1080p60 dropped from 54 to 38 ms
in Sunshine-Web-RTC's latency test.
- **Latency you can see.** The stream overlay and the top of the statistics panel show the latency from
the PC to the screen, drawing included, its session average, and the share of packets lost. The
*Performance overlay* setting, also in the stream menu, keeps them fixed at the top right of the stream,
with the frames per second actually arriving at the top left.
- **Nothing extra to install or keep running.** There is no Windows service, tray application or second
pairing. The TV pairs with Sunshine itself.

Expand Down Expand Up @@ -306,6 +315,14 @@ added by hand needs it entered on the TV.
Download `MoonlightWebRTC.wgt` from the [Releases page](https://github.com/mlopezsegura/moonlight-webrtc-tizen/releases)
and install it with Apps2Samsung, as described in [Installing on the TV](#installing-on-the-tv).

Each release has two packages:

- `MoonlightWebRTC.wgt` asks the TV to switch to **Game Mode** while the app is open, for the lowest input lag.
- `MoonlightWebRTC-NoGameMode.wgt` leaves the TV's picture mode alone. Use it if Game Mode causes problems on
your TV or you prefer to choose the picture mode yourself.

Both install as the same app, so installing one replaces the other.

## 3 — Add your PC and pair

1. Open Moonlight WebRTC on the TV. The TV looks for PCs running Sunshine-Web-RTC on the local network and
Expand Down Expand Up @@ -439,7 +456,9 @@ The TV package needs [Samsung's Emscripten SDK](https://developer.samsung.com/sm
GitHub Actions (`.github/workflows/build.yml`) runs the tests and builds `MoonlightWebRTC.wgt` on every push
and pull request. To publish a release, set the version in `tizen/config.xml` and `tizen/update-check.js`,
then push a matching tag, such as `v2.0.0` or `v2.0.0-beta.1` (published as a pre-release). The workflow
attaches the package and `SHA256SUMS.txt` to the release.
attaches both packages (`MoonlightWebRTC.wgt` and `MoonlightWebRTC-NoGameMode.wgt`) and `SHA256SUMS.txt` to
the release. To build the package without Game Mode locally, pass `-NoGameMode` to
`packaging/tizen/build-package.ps1`.

---

Expand Down
27 changes: 27 additions & 0 deletions docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,33 @@ contain that ID, so late signaling from a stopped session can be ignored.

When the TV requests a keyframe (RTCP PLI), Sunshine's encoder produces one directly.

The TV application can also ask for one itself, on the `control` data channel:

```json
{"v":1,"type":"request-keyframe","reason":"not-decoding"}
```

It does so when its picture stops advancing for a second while video keeps arriving, a stall the TV's
WebRTC stack does not always answer with a PLI of its own. `reason` is `not-decoding`, `not-presenting`
(frames decode but never reach the screen) or `reattached`, and is informative only. If the picture is
still stopped after 2.5 seconds the application also re-attaches the stream to its video element, which
rebuilds the renderer without touching the connection. Sunshine logs keyframe requests and IDR frames sent
at most once a second per session.

## TV log

Once authenticated, the TV forwards its own log to Sunshine about once a second:

```json
{"version":2,"type":"client-log","lines":["21:04:31.512 Video stalled for 1000 ms ..."]}
```

Sunshine writes each line into `sunshine.log` as `WebRTC TV <name>: <line>`, so what the TV saw and what the host
did sit in one file. A message carries at most 100 lines; control characters are replaced and lines longer than
4000 characters are cut. Besides its normal log the TV sends a stream diagnostics line every five seconds and,
while the picture is stopped, one line per stall-watchdog sample with its decoder and network counters. A
Sunshine that does not know the message answers `unsupported-message`, and the TV then stops sending it.

## Stop a session

```json
Expand Down
4 changes: 4 additions & 0 deletions packaging/release/generate-checksums.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ $assets = @(
[pscustomobject]@{
Name = 'MoonlightWebRTC.wgt'
Path = Join-Path $DistributionDirectory 'tizen\MoonlightWebRTC.wgt'
},
[pscustomobject]@{
Name = 'MoonlightWebRTC-NoGameMode.wgt'
Path = Join-Path $DistributionDirectory 'tizen\MoonlightWebRTC-NoGameMode.wgt'
}
)

Expand Down
20 changes: 18 additions & 2 deletions packaging/tizen/build-package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ param(
[string]$TizenCli = 'C:\tizen-studio\tools\ide\bin\tizen.bat',
[string]$SigningProfile,
# Samsung's Emscripten SDK, required for the Wake-on-LAN module; see build-wake-on-lan.ps1.
[string]$EmsdkRoot
[string]$EmsdkRoot,
# Leaves out Samsung's Game Mode metadata, for TVs or setups where Game Mode gets in the way.
[switch]$NoGameMode
)

$ErrorActionPreference = 'Stop'
Expand All @@ -14,7 +16,8 @@ $outputDirectory = Join-Path $repositoryRoot 'dist\tizen'
$sourceDirectory = Join-Path $outputDirectory '.source'
$buildDirectory = Join-Path $outputDirectory '.build'
$packageDirectory = Join-Path $outputDirectory '.package'
$artifact = Join-Path $outputDirectory 'MoonlightWebRTC.wgt'
$artifactName = if ($NoGameMode) { 'MoonlightWebRTC-NoGameMode.wgt' } else { 'MoonlightWebRTC.wgt' }
$artifact = Join-Path $outputDirectory $artifactName

if (-not (Test-Path -LiteralPath $TizenCli -PathType Leaf)) {
throw "Tizen CLI was not found: $TizenCli"
Expand Down Expand Up @@ -48,7 +51,10 @@ try {
'gateway-ipv4.js',
'gateway-store.js',
'index.html',
'low-latency.js',
'preferences.js',
'stall-watchdog.js',
'stream-latency.js',
'tizen_web_project.yaml',
'ui.css',
'ui.js',
Expand All @@ -63,6 +69,16 @@ try {
}
Copy-Item -LiteralPath $runtimePath -Destination $sourceDirectory
}
if ($NoGameMode) {
$configPath = Join-Path $sourceDirectory 'config.xml'
$config = Get-Content -LiteralPath $configPath -Raw
$gameModePattern = '(?m)^[ \t]*<tizen:metadata key="http://samsung\.com/tv/metadata/use\.game\.mode"[^>]*/>\r?\n'
if ($config -notmatch $gameModePattern) {
throw "Game Mode metadata was not found in $configPath."
}
$config = $config -replace $gameModePattern, ''
[System.IO.File]::WriteAllText($configPath, $config, (New-Object System.Text.UTF8Encoding $false))
}
Copy-Item -LiteralPath (Join-Path $projectDirectory 'assets') -Destination $sourceDirectory -Recurse
& (Join-Path $PSScriptRoot 'build-wake-on-lan.ps1') -OutputDirectory (Join-Path $sourceDirectory 'wasm') -EmsdkRoot $EmsdkRoot

Expand Down
51 changes: 51 additions & 0 deletions tests/TizenLowLatencyTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

const assert = require("assert");

global.window = global;
require("../tizen/low-latency.js");
const LowLatency = global.LowLatency;

const offer = [
"v=0",
"m=video 9 UDP/TLS/RTP/SAVPF 96",
"a=msid:moonlight video",
"a=ssrc:1 cname:sunshine",
"a=ssrc:1 msid:moonlight video",
"m=audio 9 UDP/TLS/RTP/SAVPF 111",
"a=msid:moonlight audio",
"a=ssrc:2 cname:sunshine",
"a=ssrc:2 msid:moonlight audio",
"m=application 9 UDP/DTLS/SCTP webrtc-datachannel",
"",
].join("\r\n");

const separated = LowLatency.separateAudioSync(offer);
assert.ok(separated.indexOf("a=msid:moonlight-audio audio\r\n") >= 0, "the audio section moves to a stream of its own");
assert.ok(separated.indexOf("a=ssrc:2 msid:moonlight-audio audio\r\n") >= 0, "and so does its SSRC description");
assert.ok(separated.indexOf("a=msid:moonlight video\r\n") >= 0, "the video keeps its stream");
assert.ok(separated.indexOf("a=ssrc:1 msid:moonlight video\r\n") >= 0);
assert.strictEqual(separated.split("\r\n").length, offer.split("\r\n").length, "no line is added or lost");
assert.strictEqual(LowLatency.separateAudioSync(separated), separated, "a separated offer stays as it is");
assert.strictEqual(LowLatency.separateAudioSync(offer.replace(/\r\n/g, "\n")),
separated.replace(/\r\n/g, "\n"), "an offer with bare newlines keeps them");
assert.strictEqual(LowLatency.separateAudioSync("m=audio 9 RTP 111\r\na=msid:- audio"),
"m=audio 9 RTP 111\r\na=msid:- audio", "a track without a stream has nothing to sync");
assert.strictEqual(LowLatency.separateAudioSync(undefined), undefined);

const modern = { jitterBufferTarget: null, playoutDelayHint: 1 };
assert.strictEqual(LowLatency.minimizeReceiverDelay(modern), "jitterBufferTarget", "newer Chromium takes the target");
assert.strictEqual(modern.jitterBufferTarget, 0);
assert.strictEqual(modern.playoutDelayHint, 1, "and the older hint is left alone");

const older = { playoutDelayHint: undefined };
assert.strictEqual(LowLatency.minimizeReceiverDelay(older), "playoutDelayHint", "older Chromium takes the hint");
assert.strictEqual(older.playoutDelayHint, 0);

assert.strictEqual(LowLatency.minimizeReceiverDelay({}), null, "a TV with neither reports it");
assert.strictEqual(LowLatency.minimizeReceiverDelay(null), null);
const refusing = {};
Object.defineProperty(refusing, "jitterBufferTarget", { get: function () { return null; }, set: function () { throw new RangeError("no"); } });
assert.strictEqual(LowLatency.minimizeReceiverDelay(refusing), null, "a receiver that refuses the value does not stop the stream");

console.log("Tizen low latency tests passed");
112 changes: 112 additions & 0 deletions tests/TizenStallWatchdogTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"use strict";

const assert = require("assert");

global.window = global;
require("../tizen/stall-watchdog.js");
const StallWatchdog = global.StallWatchdog;

// Feeds one sample every 500 ms, as the app does, and returns the actions taken.
function run(watchdog, samples) {
return samples.map(function (sample, index) {
return watchdog.sample(Object.assign({ time: index * 500 }, sample)).action;
});
}

function healthy(index) {
return { bytesReceived: index * 1000, framesDecoded: index * 30, framesPresented: index * 30 };
}

// A healthy stream is never touched.
{
const watchdog = StallWatchdog.create();
const actions = run(watchdog, [0, 1, 2, 3, 4, 5, 6, 7].map(healthy));
assert.deepStrictEqual(actions, ["none", "none", "none", "none", "none", "none", "none", "none"]);
assert.strictEqual(watchdog.counters().stalls, 0);
}

// A decoder that stops while video arrives gets a keyframe after a second, again every
// second, and the renderer re-attached once the stall reaches 2.5 s.
{
const watchdog = StallWatchdog.create();
const samples = [healthy(0), healthy(1)];
for (let index = 2; index < 10; index += 1) {
samples.push({ bytesReceived: index * 1000, framesDecoded: 30, framesPresented: 30 });
}
const actions = run(watchdog, samples);
// The stall starts at the last healthy sample (500 ms): 1 s at 1500 ms, 2.5 s at 3000 ms.
assert.deepStrictEqual(actions,
["none", "none", "none", "keyframe", "none", "keyframe", "reattach", "none", "keyframe", "none"]);
const counters = watchdog.counters();
assert.strictEqual(counters.stalls, 1, "one stall, however many actions it took");
assert.strictEqual(counters.keyframeRequests, 3);
assert.strictEqual(counters.reattachments, 1);
assert.strictEqual(counters.stalled, true);
assert.strictEqual(watchdog.sample({ time: 5000, bytesReceived: 20000, framesDecoded: 60, framesPresented: 60 }).action, "none");
assert.strictEqual(watchdog.counters().stalled, false, "the picture moving again ends the stall");
assert.strictEqual(watchdog.counters().lastStallMs, 4500);
}

// Frames that decode but never reach the screen are a stall too, reported as decoding.
{
const watchdog = StallWatchdog.create();
const samples = [healthy(0), healthy(1)];
for (let index = 2; index < 5; index += 1) {
samples.push({ bytesReceived: index * 1000, framesDecoded: index * 30, framesPresented: 30 });
}
let verdict = null;
samples.forEach(function (sample, index) {
const result = watchdog.sample(Object.assign({ time: index * 500 }, sample));
if (result.action !== "none") {
verdict = result;
}
});
assert.strictEqual(verdict.action, "keyframe");
assert.strictEqual(verdict.decoding, true);
}

// Nothing arriving is not the TV's stall to recover.
{
const watchdog = StallWatchdog.create();
const samples = [healthy(0), healthy(1)];
for (let index = 2; index < 10; index += 1) {
samples.push({ bytesReceived: 1000, framesDecoded: 30, framesPresented: 30 });
}
assert.ok(run(watchdog, samples).every(function (action) { return action === "none"; }));
assert.strictEqual(watchdog.counters().stalls, 0);
}

// A counter the TV never advances is ignored rather than read as a permanent stall.
{
const watchdog = StallWatchdog.create();
const samples = [];
for (let index = 0; index < 10; index += 1) {
samples.push({ bytesReceived: index * 1000, framesDecoded: index * 30, framesPresented: 0 });
}
assert.ok(run(watchdog, samples).every(function (action) { return action === "none"; }),
"a presented-frame counter stuck from the start is never trusted");
const missing = StallWatchdog.create();
const unreported = [];
for (let index = 0; index < 10; index += 1) {
unreported.push({ bytesReceived: index * 1000 });
}
assert.ok(run(missing, unreported).every(function (action) { return action === "none"; }),
"a TV reporting no frame counters at all is never acted on");
}

// reset() forgets a previous session's stall and counters.
{
const watchdog = StallWatchdog.create();
const samples = [healthy(0), healthy(1)];
for (let index = 2; index < 6; index += 1) {
samples.push({ bytesReceived: index * 1000, framesDecoded: 30, framesPresented: 30 });
}
run(watchdog, samples);
assert.ok(watchdog.counters().stalls > 0);
watchdog.reset();
assert.deepStrictEqual(watchdog.counters(),
{ stalls: 0, keyframeRequests: 0, reattachments: 0, lastStallMs: 0, stalled: false });
assert.strictEqual(watchdog.sample({ time: 0, bytesReceived: 0, framesDecoded: 0 }).action, "none");
}

console.log("TizenStallWatchdogTest passed");
Loading
Loading