xlight-vnc 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AUTHORS +1 -0
- package/LICENSE.txt +62 -0
- package/README-XLIGHT.md +46 -0
- package/README.md +265 -2
- package/app/audio-mse.js +176 -0
- package/app/audio.js +937 -0
- package/app/error-handler.js +87 -0
- package/app/images/alt.svg +92 -0
- package/app/images/audio-muted.svg +5 -0
- package/app/images/audio.svg +5 -0
- package/app/images/clipboard.svg +106 -0
- package/app/images/connect.svg +96 -0
- package/app/images/ctrl.svg +96 -0
- package/app/images/ctrlaltdel.svg +100 -0
- package/app/images/disconnect.svg +94 -0
- package/app/images/drag.svg +76 -0
- package/app/images/error.svg +81 -0
- package/app/images/esc.svg +92 -0
- package/app/images/expander.svg +69 -0
- package/app/images/fullscreen.svg +93 -0
- package/app/images/handle.svg +82 -0
- package/app/images/handle_bg.svg +172 -0
- package/app/images/icons/Makefile +42 -0
- package/app/images/icons/xlight-ios-120.png +0 -0
- package/app/images/icons/xlight-ios-152.png +0 -0
- package/app/images/icons/xlight-ios-167.png +0 -0
- package/app/images/icons/xlight-ios-180.png +0 -0
- package/app/images/icons/xlight-ios-40.png +0 -0
- package/app/images/icons/xlight-ios-58.png +0 -0
- package/app/images/icons/xlight-ios-60.png +0 -0
- package/app/images/icons/xlight-ios-80.png +0 -0
- package/app/images/icons/xlight-ios-87.png +0 -0
- package/app/images/icons/xlight-logo-192.png +0 -0
- package/app/images/icons/xlight-logo-32.png +0 -0
- package/app/images/icons/xlight-logo-512.png +0 -0
- package/app/images/icons/xlight-logo.svg +60 -0
- package/app/images/icons/xlight.ico +0 -0
- package/app/images/info.svg +81 -0
- package/app/images/keyboard.svg +88 -0
- package/app/images/power.svg +87 -0
- package/app/images/settings.svg +76 -0
- package/app/images/tab.svg +86 -0
- package/app/images/toggleextrakeys.svg +90 -0
- package/app/images/warning.svg +81 -0
- package/app/images/windows.svg +65 -0
- package/app/images/xlight-collapse.svg +12 -0
- package/app/locale/README +1 -0
- package/app/locale/cs.json +71 -0
- package/app/locale/de.json +74 -0
- package/app/locale/el.json +100 -0
- package/app/locale/es.json +68 -0
- package/app/locale/fr.json +82 -0
- package/app/locale/hu.json +80 -0
- package/app/locale/it.json +68 -0
- package/app/locale/ja.json +81 -0
- package/app/locale/ko.json +70 -0
- package/app/locale/nl.json +95 -0
- package/app/locale/pl.json +80 -0
- package/app/locale/pt_BR.json +72 -0
- package/app/locale/ru.json +72 -0
- package/app/locale/sv.json +83 -0
- package/app/locale/tr.json +69 -0
- package/app/locale/uk.json +81 -0
- package/app/locale/zh_CN.json +93 -0
- package/app/locale/zh_TW.json +69 -0
- package/app/localization.js +206 -0
- package/app/sounds/CREDITS +4 -0
- package/app/sounds/bell.mp3 +0 -0
- package/app/sounds/bell.oga +0 -0
- package/app/styles/Orbitron700.ttf +0 -0
- package/app/styles/Orbitron700.woff +0 -0
- package/app/styles/audio.css +28 -0
- package/app/styles/base.css +1106 -0
- package/app/styles/constants.css +30 -0
- package/app/styles/input.css +628 -0
- package/app/styles/xlight-glass.css +690 -0
- package/app/ui.js +2091 -0
- package/app/wakelock.js +199 -0
- package/app/webutil.js +250 -0
- package/app/xlight-features.js +343 -0
- package/app/xlight-tunl.js +315 -0
- package/cli/xlight.mjs +1477 -0
- package/codespace-setup.sh +196 -0
- package/core/base64.js +104 -0
- package/core/clipboard.js +72 -0
- package/core/crypto/aes.js +178 -0
- package/core/crypto/bigint.js +34 -0
- package/core/crypto/crypto.js +90 -0
- package/core/crypto/des.js +330 -0
- package/core/crypto/dh.js +55 -0
- package/core/crypto/md5.js +82 -0
- package/core/crypto/rsa.js +132 -0
- package/core/decoders/copyrect.js +27 -0
- package/core/decoders/h264.js +321 -0
- package/core/decoders/hextile.js +181 -0
- package/core/decoders/jpeg.js +161 -0
- package/core/decoders/raw.js +59 -0
- package/core/decoders/rre.js +44 -0
- package/core/decoders/tight.js +393 -0
- package/core/decoders/tightpng.js +27 -0
- package/core/decoders/zlib.js +51 -0
- package/core/decoders/zrle.js +185 -0
- package/core/deflator.js +84 -0
- package/core/display.js +605 -0
- package/core/encodings.js +54 -0
- package/core/inflator.js +65 -0
- package/core/input/domkeytable.js +311 -0
- package/core/input/fixedkeys.js +129 -0
- package/core/input/gesturehandler.js +567 -0
- package/core/input/keyboard.js +386 -0
- package/core/input/keysym.js +616 -0
- package/core/input/keysymdef.js +688 -0
- package/core/input/util.js +191 -0
- package/core/input/vkeys.js +116 -0
- package/core/input/xtscancodes.js +173 -0
- package/core/ra2.js +312 -0
- package/core/rfb.js +3566 -0
- package/core/util/browser.js +272 -0
- package/core/util/cursor.js +249 -0
- package/core/util/element.js +32 -0
- package/core/util/events.js +138 -0
- package/core/util/eventtarget.js +35 -0
- package/core/util/int.js +15 -0
- package/core/util/logging.js +56 -0
- package/core/util/strings.js +28 -0
- package/core/websock.js +369 -0
- package/defaults.json +1 -0
- package/docs/API-internal.md +105 -0
- package/docs/API.md +541 -0
- package/docs/EMBEDDING.md +130 -0
- package/docs/LIBRARY.md +31 -0
- package/docs/LICENSE.BSD-2-Clause +22 -0
- package/docs/LICENSE.BSD-3-Clause +24 -0
- package/docs/LICENSE.MPL-2.0 +373 -0
- package/docs/LICENSE.OFL-1.1 +91 -0
- package/docs/flash_policy.txt +4 -0
- package/docs/links +76 -0
- package/docs/notes +5 -0
- package/docs/novnc_proxy.1 +37 -0
- package/docs/rfb_notes +147 -0
- package/docs/rfbproto-3.3.pdf +0 -0
- package/docs/rfbproto-3.7.pdf +0 -0
- package/docs/rfbproto-3.8.pdf +0 -0
- package/host/README.md +166 -0
- package/host/audio-proxy.sh +144 -0
- package/host/audio-server.py +426 -0
- package/host/package-lock.json +67 -0
- package/host/package.json +1 -0
- package/host/pulse-setup.sh +75 -0
- package/host/tunl-server.mjs +320 -0
- package/host/xlight-audio.service +33 -0
- package/index.html +12 -0
- package/mandatory.json +1 -0
- package/package.json +80 -4
- package/po/Makefile +36 -0
- package/po/cs.po +294 -0
- package/po/de.po +303 -0
- package/po/el.po +399 -0
- package/po/es.po +284 -0
- package/po/fr.po +345 -0
- package/po/hr.po +338 -0
- package/po/hu.po +332 -0
- package/po/it.po +300 -0
- package/po/ja.po +342 -0
- package/po/ko.po +290 -0
- package/po/nl.po +400 -0
- package/po/pl.po +325 -0
- package/po/po2js +42 -0
- package/po/pt_BR.po +299 -0
- package/po/ru.po +302 -0
- package/po/sv.po +352 -0
- package/po/tr.po +288 -0
- package/po/uk.po +341 -0
- package/po/xgettext-html +119 -0
- package/po/xlight-vnc.pot +337 -0
- package/po/zh_CN.po +373 -0
- package/po/zh_TW.po +285 -0
- package/utils/README.md +14 -0
- package/utils/b64-to-binary.pl +17 -0
- package/utils/genkeysymdef.js +127 -0
- package/utils/novnc_proxy +232 -0
- package/utils/u2x11 +28 -0
- package/utils/validate +84 -0
- package/vendor/pako/LICENSE +21 -0
- package/vendor/pako/README.md +6 -0
- package/vendor/pako/lib/utils/common.js +45 -0
- package/vendor/pako/lib/zlib/adler32.js +27 -0
- package/vendor/pako/lib/zlib/constants.js +47 -0
- package/vendor/pako/lib/zlib/crc32.js +36 -0
- package/vendor/pako/lib/zlib/deflate.js +1846 -0
- package/vendor/pako/lib/zlib/gzheader.js +35 -0
- package/vendor/pako/lib/zlib/inffast.js +324 -0
- package/vendor/pako/lib/zlib/inflate.js +1527 -0
- package/vendor/pako/lib/zlib/inftrees.js +322 -0
- package/vendor/pako/lib/zlib/messages.js +11 -0
- package/vendor/pako/lib/zlib/trees.js +1195 -0
- package/vendor/pako/lib/zlib/zstream.js +24 -0
- package/xlight.html +826 -0
- package/xlight_lite.html +200 -0
package/app/audio.js
ADDED
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xLight VNC — remote-system audio in the browser (own code).
|
|
3
|
+
*
|
|
4
|
+
* Two transports (audio-plan.md §2/§5/§6/§7):
|
|
5
|
+
* 1. WebRTC — Opus over SRTP. Signalling is minimal newline-delimited
|
|
6
|
+
* JSON over the websockify gateway (token=audio-signal) to the host
|
|
7
|
+
* audio server (host/audio-server.py, port 5712); the gateway
|
|
8
|
+
* unwraps WS frames to raw TCP, so the server speaks NDJSON, not WS.
|
|
9
|
+
* 2. WebSocket + WebM/Opus via MSE — fallback, see ./audio-mse.js
|
|
10
|
+
* (host/audio-proxy.sh, port 5711).
|
|
11
|
+
*
|
|
12
|
+
* Audio starts only after a user gesture (autoplay rule, §6.2). One
|
|
13
|
+
* automatic transport retry, then a surfaced error — never a retry loop.
|
|
14
|
+
* xlight_lite.html is NOT supported in v1 (no control bar / settings there).
|
|
15
|
+
*
|
|
16
|
+
* This module never touches core/rfb.js or core/websock.js. It reads its
|
|
17
|
+
* settings through app/webutil.js (the same store UI.initSetting uses),
|
|
18
|
+
* so there is no import cycle with app/ui.js: ui.js calls Audio.init()
|
|
19
|
+
* once at startup and Audio.startIfEnabled()/Audio.stop() on connect /
|
|
20
|
+
* disconnect, and registers a status handler for UI.showStatus.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import * as Log from "../core/util/logging.js";
|
|
24
|
+
import * as WebUtil from "./webutil.js";
|
|
25
|
+
import { MediaSourcePlayer, isMSESupported } from "./audio-mse.js";
|
|
26
|
+
|
|
27
|
+
const DEFAULT_STUN = "stun:stun.l.google.com:19302";
|
|
28
|
+
const SIGNAL_TIMEOUT_MS = 5000; // signalling WS connect
|
|
29
|
+
const ANSWER_TIMEOUT_MS = 10000; // server answer
|
|
30
|
+
const FIRST_AUDIO_TIMEOUT_MS = 5000; // first audio after ontrack
|
|
31
|
+
const FIRST_RTP_TIMEOUT_MS = 10000; // first RTP packet after ontrack
|
|
32
|
+
const WS_RETRY_DELAY_MS = 2000;
|
|
33
|
+
const STATS_INTERVAL_MS = 10000;
|
|
34
|
+
const LOSS_STEP_DOWN = 0.05; // >5% loss sustained -> one bitrate step down
|
|
35
|
+
const BITRATES = [64000, 96000, 128000];
|
|
36
|
+
|
|
37
|
+
const Audio = {
|
|
38
|
+
// State: 'off' | 'waiting-for-gesture' | 'connecting' | 'on' | 'failed'
|
|
39
|
+
state: "off",
|
|
40
|
+
transport: null, // 'webrtc' | 'websocket' | null
|
|
41
|
+
muted: false,
|
|
42
|
+
volume: 80,
|
|
43
|
+
gestureSeen: false,
|
|
44
|
+
_statusHandler: null,
|
|
45
|
+
_pc: null,
|
|
46
|
+
_signalWs: null,
|
|
47
|
+
_ws: null,
|
|
48
|
+
_mse: null,
|
|
49
|
+
_timers: [],
|
|
50
|
+
_statsTimer: null,
|
|
51
|
+
_wsRetried: false,
|
|
52
|
+
_transportRetried: false,
|
|
53
|
+
_intentionalStop: false,
|
|
54
|
+
_lossWindows: 0,
|
|
55
|
+
_lastStats: null,
|
|
56
|
+
_audioElCache: null,
|
|
57
|
+
|
|
58
|
+
// ---------------------------------------------------------------
|
|
59
|
+
// Startup / UI wiring (called once from UI.start, after initSettings)
|
|
60
|
+
// ---------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
init() {
|
|
63
|
+
this._installGestureListeners();
|
|
64
|
+
this._buildSettingsUI();
|
|
65
|
+
this._wireControlBar();
|
|
66
|
+
this._applyVolumeFromSettings();
|
|
67
|
+
document.addEventListener("visibilitychange", () => {
|
|
68
|
+
if (!document.hidden && this.state === "on" &&
|
|
69
|
+
this._audioEl() && this._audioEl().paused) {
|
|
70
|
+
this._audioEl().play().catch(() => {
|
|
71
|
+
// Gesture may be needed again; fall back to waiting.
|
|
72
|
+
this._setState("waiting-for-gesture");
|
|
73
|
+
this._status("Audio: click anywhere to resume sound", "warning");
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
setStatusHandler(fn) {
|
|
80
|
+
this._statusHandler = fn;
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
_status(text, type) {
|
|
84
|
+
if (this._statusHandler) {
|
|
85
|
+
this._statusHandler(text, type || "normal");
|
|
86
|
+
} else {
|
|
87
|
+
Log.Debug("xLight audio: " + text);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
_audioEl() {
|
|
92
|
+
if (!this._audioElCache) {
|
|
93
|
+
this._audioElCache = document.getElementById("xlight_audio");
|
|
94
|
+
}
|
|
95
|
+
return this._audioElCache;
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
_installGestureListeners() {
|
|
99
|
+
const mark = () => {
|
|
100
|
+
if (!this.gestureSeen) {
|
|
101
|
+
this.gestureSeen = true;
|
|
102
|
+
if (this.state === "waiting-for-gesture") {
|
|
103
|
+
this.startIfEnabled();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
// pointerdown fires before the Connect click handler runs, so the
|
|
108
|
+
// Connect click itself counts as the gesture.
|
|
109
|
+
document.addEventListener("pointerdown", mark, { capture: true });
|
|
110
|
+
document.addEventListener("keydown", mark, { capture: true });
|
|
111
|
+
// One-shot click retry (plugin pattern): covers the case where the
|
|
112
|
+
// first gesture happened before audio was armed.
|
|
113
|
+
document.body.addEventListener("click", mark,
|
|
114
|
+
{ capture: true, once: false });
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
_buildSettingsUI() {
|
|
118
|
+
const settingsList = document.querySelector("#noVNC_settings ul");
|
|
119
|
+
if (!settingsList) {
|
|
120
|
+
return; // xlight_lite.html: no settings panel, audio unsupported.
|
|
121
|
+
}
|
|
122
|
+
if (document.getElementById("noVNC_setting_audio_enabled")) {
|
|
123
|
+
return; // Already built.
|
|
124
|
+
}
|
|
125
|
+
const li = document.createElement("li");
|
|
126
|
+
li.innerHTML =
|
|
127
|
+
'<div class="noVNC_expander">Audio</div>' +
|
|
128
|
+
'<div><ul>' +
|
|
129
|
+
'<li><label><input id="noVNC_setting_audio_enabled" ' +
|
|
130
|
+
'type="checkbox" class="toggle"> Enable audio</label></li>' +
|
|
131
|
+
'<li><label for="noVNC_setting_audio_transport">Transport:</label>' +
|
|
132
|
+
'<select id="noVNC_setting_audio_transport">' +
|
|
133
|
+
'<option value="auto">Auto</option>' +
|
|
134
|
+
'<option value="webrtc">WebRTC</option>' +
|
|
135
|
+
'<option value="websocket">WebSocket</option>' +
|
|
136
|
+
'</select></li>' +
|
|
137
|
+
'<li><label for="noVNC_setting_audio_bitrate">Bitrate:</label>' +
|
|
138
|
+
'<select id="noVNC_setting_audio_bitrate">' +
|
|
139
|
+
'<option value="64000">64 kbps</option>' +
|
|
140
|
+
'<option value="96000">96 kbps</option>' +
|
|
141
|
+
'<option value="128000">128 kbps</option>' +
|
|
142
|
+
'</select></li>' +
|
|
143
|
+
'<li><label for="noVNC_setting_audio_volume">Volume:</label>' +
|
|
144
|
+
'<input id="noVNC_setting_audio_volume" type="range" ' +
|
|
145
|
+
'min="0" max="100" value="80"></li>' +
|
|
146
|
+
'<li><label><input id="noVNC_setting_audio_force_turn" ' +
|
|
147
|
+
'type="checkbox" class="toggle"> Force TURN (relay only)</label></li>' +
|
|
148
|
+
'</ul></div>';
|
|
149
|
+
settingsList.appendChild(li);
|
|
150
|
+
|
|
151
|
+
// Mirror UI.setupSettingLabels: link labels so disableSetting()
|
|
152
|
+
// can grey them out while connected.
|
|
153
|
+
for (const name of ["audio_enabled", "audio_transport",
|
|
154
|
+
"audio_bitrate", "audio_volume",
|
|
155
|
+
"audio_force_turn"]) {
|
|
156
|
+
const ctrl = document.getElementById("noVNC_setting_" + name);
|
|
157
|
+
if (!ctrl) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const label = ctrl.closest("label") ||
|
|
161
|
+
document.querySelector('label[for="noVNC_setting_' + name + '"]');
|
|
162
|
+
if (label) {
|
|
163
|
+
ctrl.label = label;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Apply stored values to the freshly built controls.
|
|
167
|
+
this._syncSettingsControls();
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
_syncSettingsControls() {
|
|
171
|
+
const enabled = document.getElementById("noVNC_setting_audio_enabled");
|
|
172
|
+
if (enabled) {
|
|
173
|
+
enabled.checked = !!this._setting("audio_enabled", false);
|
|
174
|
+
}
|
|
175
|
+
const transport = document.getElementById("noVNC_setting_audio_transport");
|
|
176
|
+
if (transport) {
|
|
177
|
+
transport.value = String(this._setting("audio_transport", "auto"));
|
|
178
|
+
}
|
|
179
|
+
const bitrate = document.getElementById("noVNC_setting_audio_bitrate");
|
|
180
|
+
if (bitrate) {
|
|
181
|
+
bitrate.value = String(this._setting("audio_bitrate", 96000));
|
|
182
|
+
}
|
|
183
|
+
const volume = document.getElementById("noVNC_setting_audio_volume");
|
|
184
|
+
if (volume) {
|
|
185
|
+
volume.value = String(this._setting("audio_volume", 80));
|
|
186
|
+
}
|
|
187
|
+
const forceTurn = document.getElementById("noVNC_setting_audio_force_turn");
|
|
188
|
+
if (forceTurn) {
|
|
189
|
+
forceTurn.checked = !!this._setting("audio_force_turn", false);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
_wireControlBar() {
|
|
194
|
+
const btn = document.getElementById("xlight_audio_button");
|
|
195
|
+
if (btn) {
|
|
196
|
+
btn.addEventListener("click", () => this.toggleMute());
|
|
197
|
+
}
|
|
198
|
+
const slider = document.getElementById("xlight_volume");
|
|
199
|
+
if (slider) {
|
|
200
|
+
slider.addEventListener("input", () => {
|
|
201
|
+
this.setVolume(parseInt(slider.value, 10));
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
this._updateButton();
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
// ---------------------------------------------------------------
|
|
208
|
+
// Settings
|
|
209
|
+
// ---------------------------------------------------------------
|
|
210
|
+
|
|
211
|
+
_setting(name, defVal) {
|
|
212
|
+
const val = WebUtil.readSetting(name, defVal);
|
|
213
|
+
return (val === undefined || val === null) ? defVal : val;
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
_bitrate() {
|
|
217
|
+
const b = parseInt(this._setting("audio_bitrate", 96000), 10);
|
|
218
|
+
return BITRATES.includes(b) ? b : 96000;
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
_applyVolumeFromSettings() {
|
|
222
|
+
const el = this._audioEl();
|
|
223
|
+
this.volume = parseInt(this._setting("audio_volume", 80), 10) || 0;
|
|
224
|
+
this.muted = this._setting("audio_muted", false) === true ||
|
|
225
|
+
this._setting("audio_muted", false) === "true";
|
|
226
|
+
if (el) {
|
|
227
|
+
el.volume = this.volume / 100;
|
|
228
|
+
el.muted = this.muted;
|
|
229
|
+
}
|
|
230
|
+
const slider = document.getElementById("xlight_volume");
|
|
231
|
+
if (slider) {
|
|
232
|
+
slider.value = String(this.volume);
|
|
233
|
+
}
|
|
234
|
+
this._updateButton();
|
|
235
|
+
},
|
|
236
|
+
|
|
237
|
+
setVolume(percent) {
|
|
238
|
+
this.volume = Math.max(0, Math.min(100, parseInt(percent, 10) || 0));
|
|
239
|
+
const el = this._audioEl();
|
|
240
|
+
if (el) {
|
|
241
|
+
el.volume = this.volume / 100;
|
|
242
|
+
}
|
|
243
|
+
WebUtil.writeSetting("audio_volume", this.volume);
|
|
244
|
+
const slider = document.getElementById("xlight_volume");
|
|
245
|
+
if (slider && parseInt(slider.value, 10) !== this.volume) {
|
|
246
|
+
slider.value = String(this.volume);
|
|
247
|
+
}
|
|
248
|
+
if (this.volume === 0) {
|
|
249
|
+
this.muted = true;
|
|
250
|
+
} else if (this.muted && this.volume > 0) {
|
|
251
|
+
this.muted = false;
|
|
252
|
+
if (el) {
|
|
253
|
+
el.muted = false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
WebUtil.writeSetting("audio_muted", this.muted);
|
|
257
|
+
this._updateButton();
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
toggleMute() {
|
|
261
|
+
this.muted = !this.muted;
|
|
262
|
+
const el = this._audioEl();
|
|
263
|
+
if (el) {
|
|
264
|
+
el.muted = this.muted;
|
|
265
|
+
}
|
|
266
|
+
WebUtil.writeSetting("audio_muted", this.muted);
|
|
267
|
+
this._updateButton();
|
|
268
|
+
this._status(this.muted ? "Audio: muted"
|
|
269
|
+
: "Audio: unmuted (volume " + this.volume + "%)");
|
|
270
|
+
},
|
|
271
|
+
|
|
272
|
+
_updateButton() {
|
|
273
|
+
const btn = document.getElementById("xlight_audio_button");
|
|
274
|
+
if (!btn) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
btn.src = this.muted ? "app/images/audio-muted.svg"
|
|
278
|
+
: "app/images/audio.svg";
|
|
279
|
+
btn.title = this.muted ? "Audio muted — click to unmute"
|
|
280
|
+
: "Audio on — click to mute";
|
|
281
|
+
const label = document.getElementById("xlight_audio_status");
|
|
282
|
+
if (label) {
|
|
283
|
+
label.textContent = this._stateLabel();
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
|
|
287
|
+
_stateLabel() {
|
|
288
|
+
switch (this.state) {
|
|
289
|
+
case "on":
|
|
290
|
+
return "on (" + (this.transport === "webrtc" ? "WebRTC" : "WebSocket") + ")";
|
|
291
|
+
case "connecting":
|
|
292
|
+
return "connecting…";
|
|
293
|
+
case "waiting-for-gesture":
|
|
294
|
+
return "click to enable sound";
|
|
295
|
+
case "failed":
|
|
296
|
+
return "failed";
|
|
297
|
+
default:
|
|
298
|
+
return "off";
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
|
|
302
|
+
_setState(state) {
|
|
303
|
+
this.state = state;
|
|
304
|
+
this._updateButton();
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
// ---------------------------------------------------------------
|
|
308
|
+
// Lifecycle
|
|
309
|
+
// ---------------------------------------------------------------
|
|
310
|
+
|
|
311
|
+
getState() {
|
|
312
|
+
return this.state;
|
|
313
|
+
},
|
|
314
|
+
|
|
315
|
+
getTransport() {
|
|
316
|
+
return this.transport;
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
startIfEnabled() {
|
|
320
|
+
if (!this._setting("audio_enabled", false)) {
|
|
321
|
+
this._setState("off");
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
if (this.state === "on" || this.state === "connecting") {
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
if (!this.gestureSeen) {
|
|
328
|
+
// Autoconnect (or any start without a prior click): wait for
|
|
329
|
+
// the first gesture instead of failing on autoplay (§6.2).
|
|
330
|
+
this._setState("waiting-for-gesture");
|
|
331
|
+
this._status("Audio: click anywhere to enable sound", "warning");
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
this._intentionalStop = false;
|
|
335
|
+
this._transportRetried = false;
|
|
336
|
+
this._wsRetried = false;
|
|
337
|
+
this._start();
|
|
338
|
+
return true;
|
|
339
|
+
},
|
|
340
|
+
|
|
341
|
+
async _start() {
|
|
342
|
+
this._setState("connecting");
|
|
343
|
+
const wanted = String(this._setting("audio_transport", "auto"));
|
|
344
|
+
const order = wanted === "webrtc" ? ["webrtc", "websocket"]
|
|
345
|
+
: wanted === "websocket" ? ["websocket", "webrtc"]
|
|
346
|
+
: ["webrtc", "websocket"];
|
|
347
|
+
// Auto: prefer WebRTC, fall back to WS. Forced: try the forced one
|
|
348
|
+
// first, then the other one once (§7).
|
|
349
|
+
for (let i = 0; i < order.length; i++) {
|
|
350
|
+
const transport = order[i];
|
|
351
|
+
if (transport === "websocket" && !isMSESupported()) {
|
|
352
|
+
// iOS Safari and friends: no MSE Opus — WebRTC only.
|
|
353
|
+
this._status("Audio: WebSocket fallback not supported " +
|
|
354
|
+
"in this browser, using WebRTC", "warning");
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
try {
|
|
358
|
+
if (transport === "webrtc") {
|
|
359
|
+
await this._startWebRTC();
|
|
360
|
+
} else {
|
|
361
|
+
await this._startWebSocket();
|
|
362
|
+
}
|
|
363
|
+
this.transport = transport;
|
|
364
|
+
this._setState("on");
|
|
365
|
+
this._applyVolumeFromSettings();
|
|
366
|
+
this._status("Audio: connected (" +
|
|
367
|
+
(transport === "webrtc" ? "WebRTC" : "WebSocket") +
|
|
368
|
+
", Opus " + (this._bitrate() / 1000) + " kbps)");
|
|
369
|
+
if (transport === "webrtc") {
|
|
370
|
+
this._startStatsWatcher();
|
|
371
|
+
}
|
|
372
|
+
return;
|
|
373
|
+
} catch (err) {
|
|
374
|
+
this._stopTransport();
|
|
375
|
+
if (i === 0) {
|
|
376
|
+
this._status("Audio: " + transport + " failed (" +
|
|
377
|
+
(err && err.message ? err.message : err) +
|
|
378
|
+
"), trying " + order[1] + "…", "warning");
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
this._setState("failed");
|
|
382
|
+
this._status("Audio failed: " +
|
|
383
|
+
(err && err.message ? err.message : err), "error");
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
this._setState("failed");
|
|
388
|
+
this._status("Audio failed: no usable transport in this browser", "error");
|
|
389
|
+
},
|
|
390
|
+
|
|
391
|
+
stop() {
|
|
392
|
+
this._intentionalStop = true;
|
|
393
|
+
this._stopTransport();
|
|
394
|
+
this._setState("off");
|
|
395
|
+
this.transport = null;
|
|
396
|
+
},
|
|
397
|
+
|
|
398
|
+
_stopTransport() {
|
|
399
|
+
for (const t of this._timers) {
|
|
400
|
+
window.clearTimeout(t);
|
|
401
|
+
}
|
|
402
|
+
this._timers = [];
|
|
403
|
+
if (this._statsTimer !== null) {
|
|
404
|
+
window.clearInterval(this._statsTimer);
|
|
405
|
+
this._statsTimer = null;
|
|
406
|
+
}
|
|
407
|
+
if (this._pc) {
|
|
408
|
+
try { this._pc.close(); } catch (err) { /* already closed */ }
|
|
409
|
+
this._pc = null;
|
|
410
|
+
}
|
|
411
|
+
if (this._signalWs) {
|
|
412
|
+
try { this._signalWs.close(); } catch (err) { /* already closed */ }
|
|
413
|
+
this._signalWs = null;
|
|
414
|
+
}
|
|
415
|
+
if (this._ws) {
|
|
416
|
+
try { this._ws.close(); } catch (err) { /* already closed */ }
|
|
417
|
+
this._ws = null;
|
|
418
|
+
}
|
|
419
|
+
if (this._mse) {
|
|
420
|
+
this._mse.close();
|
|
421
|
+
this._mse = null;
|
|
422
|
+
}
|
|
423
|
+
const el = this._audioEl();
|
|
424
|
+
if (el) {
|
|
425
|
+
el.pause();
|
|
426
|
+
el.removeAttribute("src");
|
|
427
|
+
el.srcObject = null;
|
|
428
|
+
el.load();
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
|
|
432
|
+
_later(ms) {
|
|
433
|
+
return new Promise((resolve) => {
|
|
434
|
+
const t = window.setTimeout(resolve, ms);
|
|
435
|
+
this._timers.push(t);
|
|
436
|
+
});
|
|
437
|
+
},
|
|
438
|
+
|
|
439
|
+
// ---------------------------------------------------------------
|
|
440
|
+
// URL helpers — audio rides the same gateway as VNC (websockify
|
|
441
|
+
// token routes, §3.4): `audio` -> :5711, `audio-signal` -> :5712.
|
|
442
|
+
// ---------------------------------------------------------------
|
|
443
|
+
|
|
444
|
+
_wsBase() {
|
|
445
|
+
const scheme = window.location.protocol === "https:" ? "wss://" : "ws://";
|
|
446
|
+
const host = this._setting("host", "") || window.location.hostname;
|
|
447
|
+
let port = parseInt(this._setting("port", 0), 10);
|
|
448
|
+
if (!port) {
|
|
449
|
+
port = window.location.port ? parseInt(window.location.port, 10)
|
|
450
|
+
: (window.location.protocol === "https:" ? 443 : 80);
|
|
451
|
+
}
|
|
452
|
+
const path = String(this._setting("path", "websockify") || "websockify")
|
|
453
|
+
.replace(/^\/+/, "");
|
|
454
|
+
return scheme + host + ":" + port + "/" + path;
|
|
455
|
+
},
|
|
456
|
+
|
|
457
|
+
_sessionToken() {
|
|
458
|
+
// Same VNC session token the RFB connection uses (the password).
|
|
459
|
+
return String(this._setting("password", "") || "");
|
|
460
|
+
},
|
|
461
|
+
|
|
462
|
+
// websockify kills TEXT frames ("Unsupported: Text frames are not
|
|
463
|
+
// supported", close 1003) — everything the browser sends through
|
|
464
|
+
// the gateway must be a BINARY frame, so JSON is sent UTF-8 encoded.
|
|
465
|
+
_sendJson(ws, obj) {
|
|
466
|
+
// Newline-delimited JSON: the signalling server reads raw TCP
|
|
467
|
+
// lines through the websockify gateway (see host/audio-server.py).
|
|
468
|
+
ws.send(new TextEncoder().encode(JSON.stringify(obj) + "\n"));
|
|
469
|
+
},
|
|
470
|
+
|
|
471
|
+
// ---------------------------------------------------------------
|
|
472
|
+
// WebRTC path (§5)
|
|
473
|
+
// ---------------------------------------------------------------
|
|
474
|
+
|
|
475
|
+
_startWebRTC() {
|
|
476
|
+
return new Promise((resolve, reject) => {
|
|
477
|
+
let settled = false;
|
|
478
|
+
const fail = (msg) => {
|
|
479
|
+
if (!settled) {
|
|
480
|
+
settled = true;
|
|
481
|
+
reject(new Error(msg));
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
const done = () => {
|
|
485
|
+
if (!settled) {
|
|
486
|
+
settled = true;
|
|
487
|
+
resolve();
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
const signalUrl = this._wsBase() + "?token=audio-signal";
|
|
492
|
+
let ws;
|
|
493
|
+
try {
|
|
494
|
+
ws = new WebSocket(signalUrl);
|
|
495
|
+
} catch (err) {
|
|
496
|
+
fail("signalling socket failed: " + err.message);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
this._signalWs = ws;
|
|
500
|
+
const connectTimer = window.setTimeout(() => {
|
|
501
|
+
fail("signalling connect timed out (" +
|
|
502
|
+
(SIGNAL_TIMEOUT_MS / 1000) + "s)");
|
|
503
|
+
try { ws.close(); } catch (err) { /* ignore */ }
|
|
504
|
+
}, SIGNAL_TIMEOUT_MS);
|
|
505
|
+
this._timers.push(connectTimer);
|
|
506
|
+
|
|
507
|
+
let pc = null;
|
|
508
|
+
let answerTimer = null;
|
|
509
|
+
|
|
510
|
+
ws.onopen = () => {
|
|
511
|
+
window.clearTimeout(connectTimer);
|
|
512
|
+
this._sendJson(ws, {
|
|
513
|
+
type: "hello",
|
|
514
|
+
token: this._sessionToken(),
|
|
515
|
+
want: "audio",
|
|
516
|
+
bitrate: this._bitrate(),
|
|
517
|
+
});
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// Signalling is newline-delimited JSON over the gateway's
|
|
521
|
+
// raw TCP stream (websockify unwraps WS frames); a line may
|
|
522
|
+
// split or coalesce across frames, so buffer and split on "\n".
|
|
523
|
+
this._signalBuf = "";
|
|
524
|
+
ws.onmessage = async (event) => {
|
|
525
|
+
let text = event.data;
|
|
526
|
+
if (text instanceof ArrayBuffer) {
|
|
527
|
+
text = new TextDecoder().decode(text);
|
|
528
|
+
} else if (typeof Blob !== "undefined" && text instanceof Blob) {
|
|
529
|
+
text = await text.text();
|
|
530
|
+
}
|
|
531
|
+
this._signalBuf += text;
|
|
532
|
+
let nl;
|
|
533
|
+
while ((nl = this._signalBuf.indexOf("\n")) !== -1) {
|
|
534
|
+
const line = this._signalBuf.slice(0, nl).trim();
|
|
535
|
+
this._signalBuf = this._signalBuf.slice(nl + 1);
|
|
536
|
+
if (!line) {
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
let msg;
|
|
540
|
+
try {
|
|
541
|
+
msg = JSON.parse(line);
|
|
542
|
+
} catch (err) {
|
|
543
|
+
fail("bad signalling message");
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (msg.type === "error") {
|
|
547
|
+
fail(msg.message || ("signalling error: " + msg.code));
|
|
548
|
+
try { ws.close(); } catch (err) { /* ignore */ }
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (msg.type === "hello-ok") {
|
|
552
|
+
const iceServers = (msg.iceServers && msg.iceServers.length)
|
|
553
|
+
? msg.iceServers
|
|
554
|
+
: [{ urls: DEFAULT_STUN }];
|
|
555
|
+
const forceTurn = !!this._setting("audio_force_turn", false);
|
|
556
|
+
pc = new RTCPeerConnection({
|
|
557
|
+
iceServers,
|
|
558
|
+
iceTransportPolicy: forceTurn ? "relay" : "all",
|
|
559
|
+
});
|
|
560
|
+
this._pc = pc;
|
|
561
|
+
pc.onicecandidate = (e) => {
|
|
562
|
+
this._sendJson(ws, {
|
|
563
|
+
type: "ice",
|
|
564
|
+
candidate: e.candidate ? e.candidate.candidate : null,
|
|
565
|
+
});
|
|
566
|
+
};
|
|
567
|
+
pc.ontrack = (e) => {
|
|
568
|
+
const el = this._audioEl();
|
|
569
|
+
if (!el) {
|
|
570
|
+
fail("no audio element");
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
el.srcObject = e.streams[0];
|
|
574
|
+
// play() resolving does NOT prove audio is
|
|
575
|
+
// flowing: a MediaStream element starts
|
|
576
|
+
// "playing" even with zero RTP, so resolving
|
|
577
|
+
// here would report "on (WebRTC)" over
|
|
578
|
+
// silence and never trigger the WebSocket
|
|
579
|
+
// fallback. Resolve only when real RTP
|
|
580
|
+
// arrives (watcher below).
|
|
581
|
+
el.play().catch((err) => {
|
|
582
|
+
if (err && err.name === "NotAllowedError") {
|
|
583
|
+
// Gesture lost (e.g. reconnect): wait for one.
|
|
584
|
+
this._setState("waiting-for-gesture");
|
|
585
|
+
this._status("Audio: click anywhere to enable sound",
|
|
586
|
+
"warning");
|
|
587
|
+
done();
|
|
588
|
+
} else {
|
|
589
|
+
fail("audio play failed: " + err.message);
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
// First-RTP watcher: poll getStats() until an
|
|
593
|
+
// inbound-rtp audio report shows received
|
|
594
|
+
// bytes/packets. No RTP within the timeout
|
|
595
|
+
// means the WebRTC path is silent (ICE never
|
|
596
|
+
// connected) — fail so the transport loop
|
|
597
|
+
// falls back to WebSocket.
|
|
598
|
+
const rtpDeadline = Date.now() + FIRST_RTP_TIMEOUT_MS;
|
|
599
|
+
const rtpTimer = window.setInterval(async () => {
|
|
600
|
+
if (settled) {
|
|
601
|
+
window.clearInterval(rtpTimer);
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
let stats = null;
|
|
605
|
+
try {
|
|
606
|
+
stats = await pc.getStats();
|
|
607
|
+
} catch (err) {
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
let gotRtp = false;
|
|
611
|
+
stats.forEach((report) => {
|
|
612
|
+
if (report.type === "inbound-rtp" &&
|
|
613
|
+
report.kind === "audio" &&
|
|
614
|
+
((report.bytesReceived || 0) > 0 ||
|
|
615
|
+
(report.packetsReceived || 0) > 0)) {
|
|
616
|
+
gotRtp = true;
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
if (gotRtp) {
|
|
620
|
+
window.clearInterval(rtpTimer);
|
|
621
|
+
done();
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
if (Date.now() >= rtpDeadline) {
|
|
625
|
+
window.clearInterval(rtpTimer);
|
|
626
|
+
fail("no audio within 10s of ontrack (no RTP received)");
|
|
627
|
+
}
|
|
628
|
+
}, 500);
|
|
629
|
+
this._timers.push(rtpTimer);
|
|
630
|
+
// First-audio timeout: ontrack fired but nothing plays.
|
|
631
|
+
const t = window.setTimeout(() => {
|
|
632
|
+
if (el.paused && !settled) {
|
|
633
|
+
fail("no audio within " +
|
|
634
|
+
(FIRST_AUDIO_TIMEOUT_MS / 1000) + "s of ontrack");
|
|
635
|
+
}
|
|
636
|
+
}, FIRST_AUDIO_TIMEOUT_MS);
|
|
637
|
+
this._timers.push(t);
|
|
638
|
+
};
|
|
639
|
+
pc.addTransceiver("audio", { direction: "recvonly" });
|
|
640
|
+
let offer;
|
|
641
|
+
try {
|
|
642
|
+
offer = await pc.createOffer();
|
|
643
|
+
} catch (err) {
|
|
644
|
+
fail("createOffer failed: " + err.message);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
await pc.setLocalDescription(offer);
|
|
648
|
+
this._sendJson(ws, { type: "offer", sdp: offer.sdp });
|
|
649
|
+
answerTimer = window.setTimeout(() => {
|
|
650
|
+
fail("server answer timed out (" +
|
|
651
|
+
(ANSWER_TIMEOUT_MS / 1000) + "s)");
|
|
652
|
+
}, ANSWER_TIMEOUT_MS);
|
|
653
|
+
this._timers.push(answerTimer);
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
if (msg.type === "answer") {
|
|
657
|
+
if (answerTimer !== null) {
|
|
658
|
+
window.clearTimeout(answerTimer);
|
|
659
|
+
}
|
|
660
|
+
if (!pc) {
|
|
661
|
+
fail("answer before peer connection");
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
try {
|
|
665
|
+
await pc.setRemoteDescription({
|
|
666
|
+
type: "answer",
|
|
667
|
+
sdp: this._mungeAnswerSdp(msg.sdp),
|
|
668
|
+
});
|
|
669
|
+
} catch (err) {
|
|
670
|
+
fail("setRemoteDescription failed: " + err.message);
|
|
671
|
+
}
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
if (msg.type === "ice") {
|
|
675
|
+
if (!pc || msg.candidate === undefined) {
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
if (msg.candidate === null) {
|
|
679
|
+
return; // end-of-candidates
|
|
680
|
+
}
|
|
681
|
+
try {
|
|
682
|
+
await pc.addIceCandidate({ candidate: msg.candidate });
|
|
683
|
+
} catch (err) {
|
|
684
|
+
// A single bad candidate must not kill the session.
|
|
685
|
+
Log.Warn("xLight audio: bad ICE candidate", err);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
} // end while: next NDJSON line
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
ws.onerror = () => {
|
|
692
|
+
fail("signalling socket error");
|
|
693
|
+
};
|
|
694
|
+
ws.onclose = () => {
|
|
695
|
+
if (!settled && this.state === "connecting") {
|
|
696
|
+
fail("signalling socket closed");
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
});
|
|
700
|
+
},
|
|
701
|
+
|
|
702
|
+
// Ask the sender for music-grade Opus: FEC on, stereo, and the
|
|
703
|
+
// user-chosen bitrate cap (§4). SDP fmtp for Opus PT 111.
|
|
704
|
+
_mungeAnswerSdp(sdp) {
|
|
705
|
+
const bitrate = this._bitrate();
|
|
706
|
+
return sdp.split("\r\n").map((line) => {
|
|
707
|
+
if (line.startsWith("a=fmtp:111 ")) {
|
|
708
|
+
return "a=fmtp:111 minptime=10;useinbandfec=1;stereo=1;" +
|
|
709
|
+
"sprop-stereo=1;maxaveragebitrate=" + bitrate;
|
|
710
|
+
}
|
|
711
|
+
return line;
|
|
712
|
+
}).join("\r\n");
|
|
713
|
+
},
|
|
714
|
+
|
|
715
|
+
// Adaptive bitrate (§4): sustained >5% loss -> step down one notch
|
|
716
|
+
// and renegotiate maxaveragebitrate; step back up only after 60s clean.
|
|
717
|
+
_startStatsWatcher() {
|
|
718
|
+
let cleanSeconds = 0;
|
|
719
|
+
this._statsTimer = window.setInterval(async () => {
|
|
720
|
+
const pc = this._pc;
|
|
721
|
+
if (!pc || this.state !== "on") {
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
let stats;
|
|
725
|
+
try {
|
|
726
|
+
stats = await pc.getStats();
|
|
727
|
+
} catch (err) {
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
let inbound = null;
|
|
731
|
+
stats.forEach((report) => {
|
|
732
|
+
if (report.type === "inbound-rtp" && report.kind === "audio") {
|
|
733
|
+
inbound = report;
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
if (!inbound || !inbound.packetsReceived) {
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
const loss = inbound.packetsLost / inbound.packetsReceived;
|
|
740
|
+
this._lastStats = {
|
|
741
|
+
packetsLost: inbound.packetsLost,
|
|
742
|
+
packetsReceived: inbound.packetsReceived,
|
|
743
|
+
jitterBufferDelay: inbound.jitterBufferDelay,
|
|
744
|
+
jitterBufferEmittedCount: inbound.jitterBufferEmittedCount,
|
|
745
|
+
};
|
|
746
|
+
if (loss > LOSS_STEP_DOWN) {
|
|
747
|
+
this._lossWindows += 1;
|
|
748
|
+
cleanSeconds = 0;
|
|
749
|
+
if (this._lossWindows >= 2) {
|
|
750
|
+
this._lossWindows = 0;
|
|
751
|
+
this._stepBitrateDown();
|
|
752
|
+
}
|
|
753
|
+
} else {
|
|
754
|
+
this._lossWindows = 0;
|
|
755
|
+
cleanSeconds += STATS_INTERVAL_MS / 1000;
|
|
756
|
+
if (cleanSeconds >= 60) {
|
|
757
|
+
cleanSeconds = 0;
|
|
758
|
+
this._stepBitrateUp();
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}, STATS_INTERVAL_MS);
|
|
762
|
+
},
|
|
763
|
+
|
|
764
|
+
_stepBitrateDown() {
|
|
765
|
+
const current = this._bitrate();
|
|
766
|
+
const idx = BITRATES.indexOf(current);
|
|
767
|
+
if (idx <= 0) {
|
|
768
|
+
return; // Already at the 64k floor; never adapt below it.
|
|
769
|
+
}
|
|
770
|
+
this._applyBitrate(BITRATES[idx - 1], "loss");
|
|
771
|
+
},
|
|
772
|
+
|
|
773
|
+
_stepBitrateUp() {
|
|
774
|
+
const current = this._bitrate();
|
|
775
|
+
const idx = BITRATES.indexOf(current);
|
|
776
|
+
if (idx < 0 || idx >= BITRATES.length - 1) {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
this._applyBitrate(BITRATES[idx + 1], "recovery");
|
|
780
|
+
},
|
|
781
|
+
|
|
782
|
+
async _applyBitrate(bitrate, reason) {
|
|
783
|
+
WebUtil.writeSetting("audio_bitrate", bitrate);
|
|
784
|
+
const bitrateCtrl = document.getElementById("noVNC_setting_audio_bitrate");
|
|
785
|
+
if (bitrateCtrl) {
|
|
786
|
+
bitrateCtrl.value = String(bitrate);
|
|
787
|
+
}
|
|
788
|
+
const pc = this._pc;
|
|
789
|
+
if (!pc) {
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
try {
|
|
793
|
+
const offer = await pc.createOffer();
|
|
794
|
+
offer.sdp = this._mungeAnswerSdp(offer.sdp);
|
|
795
|
+
await pc.setLocalDescription(offer);
|
|
796
|
+
if (this._signalWs && this._signalWs.readyState === WebSocket.OPEN) {
|
|
797
|
+
this._sendJson(this._signalWs, {
|
|
798
|
+
type: "offer", sdp: offer.sdp,
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
this._status("Audio: bitrate " +
|
|
802
|
+
(reason === "loss" ? "reduced" : "restored") +
|
|
803
|
+
" to " + (bitrate / 1000) + " kbps", "warning");
|
|
804
|
+
} catch (err) {
|
|
805
|
+
Log.Warn("xLight audio: bitrate renegotiation failed", err);
|
|
806
|
+
}
|
|
807
|
+
},
|
|
808
|
+
|
|
809
|
+
// ---------------------------------------------------------------
|
|
810
|
+
// WebSocket / MSE fallback path (§6.1, audio-mse.js)
|
|
811
|
+
// ---------------------------------------------------------------
|
|
812
|
+
|
|
813
|
+
_startWebSocket() {
|
|
814
|
+
return new Promise((resolve, reject) => {
|
|
815
|
+
let settled = false;
|
|
816
|
+
const fail = (msg) => {
|
|
817
|
+
if (!settled) {
|
|
818
|
+
settled = true;
|
|
819
|
+
reject(new Error(msg));
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
const done = () => {
|
|
823
|
+
if (!settled) {
|
|
824
|
+
settled = true;
|
|
825
|
+
resolve();
|
|
826
|
+
}
|
|
827
|
+
};
|
|
828
|
+
|
|
829
|
+
const el = this._audioEl();
|
|
830
|
+
if (!el) {
|
|
831
|
+
fail("no audio element");
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
const wsUrl = this._wsBase() + "?token=audio";
|
|
835
|
+
let ws;
|
|
836
|
+
try {
|
|
837
|
+
ws = new WebSocket(wsUrl);
|
|
838
|
+
} catch (err) {
|
|
839
|
+
fail("audio socket failed: " + err.message);
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
ws.binaryType = "arraybuffer";
|
|
843
|
+
this._ws = ws;
|
|
844
|
+
|
|
845
|
+
const player = new MediaSourcePlayer(el);
|
|
846
|
+
player.onError = err => fail("MSE error: " + (err && err.message ? err.message : err));
|
|
847
|
+
this._mse = player;
|
|
848
|
+
player.attach();
|
|
849
|
+
|
|
850
|
+
const connectTimer = window.setTimeout(() => {
|
|
851
|
+
fail("audio socket connect timed out (" +
|
|
852
|
+
(SIGNAL_TIMEOUT_MS / 1000) + "s)");
|
|
853
|
+
try { ws.close(); } catch (err) { /* ignore */ }
|
|
854
|
+
}, SIGNAL_TIMEOUT_MS);
|
|
855
|
+
this._timers.push(connectTimer);
|
|
856
|
+
|
|
857
|
+
ws.onopen = () => {
|
|
858
|
+
window.clearTimeout(connectTimer);
|
|
859
|
+
// Handshake (text, before binary frames): codec, bitrate,
|
|
860
|
+
// sample rate. The gateway already authenticated the token.
|
|
861
|
+
ws.send(new TextEncoder().encode(
|
|
862
|
+
"CD:opus\nBR:" + this._bitrate() + "\nSR:48000\n\n"));
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
ws.onmessage = (event) => {
|
|
866
|
+
if (typeof event.data === "string") {
|
|
867
|
+
if (event.data === "READY") {
|
|
868
|
+
player.play().then(done).catch((err) => {
|
|
869
|
+
if (err && err.name === "NotAllowedError") {
|
|
870
|
+
this._setState("waiting-for-gesture");
|
|
871
|
+
this._status("Audio: click anywhere to enable sound",
|
|
872
|
+
"warning");
|
|
873
|
+
done();
|
|
874
|
+
} else {
|
|
875
|
+
fail("audio play failed: " + err.message);
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
} else if (event.data.startsWith("ERR:")) {
|
|
879
|
+
fail(event.data.slice(4));
|
|
880
|
+
}
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
player.feed(event.data);
|
|
884
|
+
// First chunk arrived: start playback (some proxies send
|
|
885
|
+
// no READY text — the first binary frame is the signal).
|
|
886
|
+
if (!settled) {
|
|
887
|
+
player.play().then(done).catch((err) => {
|
|
888
|
+
if (err && err.name === "NotAllowedError") {
|
|
889
|
+
this._setState("waiting-for-gesture");
|
|
890
|
+
this._status("Audio: click anywhere to enable sound",
|
|
891
|
+
"warning");
|
|
892
|
+
done();
|
|
893
|
+
} else {
|
|
894
|
+
fail("audio play failed: " + err.message);
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
ws.onerror = () => {
|
|
901
|
+
if (!settled) {
|
|
902
|
+
fail("audio socket error");
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
ws.onclose = () => {
|
|
907
|
+
if (this._intentionalStop) {
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
if (!settled) {
|
|
911
|
+
fail("audio socket closed before start");
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
if (this.state === "on" && !this._wsRetried) {
|
|
915
|
+
// Audio WS dropped while VNC stays up: retry once
|
|
916
|
+
// after 2s with a fresh MSE instance (§7).
|
|
917
|
+
this._wsRetried = true;
|
|
918
|
+
this._status("Audio: connection lost, retrying…", "warning");
|
|
919
|
+
window.setTimeout(() => {
|
|
920
|
+
if (this._intentionalStop || this.state !== "on") {
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
this._stopTransport();
|
|
924
|
+
this._startWebSocket().then(() => {
|
|
925
|
+
this._setState("on");
|
|
926
|
+
}).catch((err) => {
|
|
927
|
+
this._setState("failed");
|
|
928
|
+
this._status("Audio failed: " + err.message, "error");
|
|
929
|
+
});
|
|
930
|
+
}, WS_RETRY_DELAY_MS);
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
});
|
|
934
|
+
},
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
export default Audio;
|