electron-cast 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +85 -2
- package/dist/client/cast-client.d.ts +92 -0
- package/dist/client/cast-client.js +427 -0
- package/dist/client/cast-client.js.map +1 -0
- package/dist/client/errors.d.ts +17 -0
- package/dist/client/errors.js +30 -0
- package/dist/client/errors.js.map +1 -0
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.js +5 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/namespaces.d.ts +9 -0
- package/dist/client/namespaces.js +10 -0
- package/dist/client/namespaces.js.map +1 -0
- package/dist/client/types.d.ts +27 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/discovery/cast-discovery.d.ts +84 -0
- package/dist/discovery/cast-discovery.js +234 -0
- package/dist/discovery/cast-discovery.js.map +1 -0
- package/dist/discovery/device-cache.d.ts +40 -0
- package/dist/discovery/device-cache.js +212 -0
- package/dist/discovery/device-cache.js.map +1 -0
- package/dist/discovery/dns-packet.d.ts +70 -0
- package/dist/discovery/dns-packet.js +244 -0
- package/dist/discovery/dns-packet.js.map +1 -0
- package/dist/discovery/index.d.ts +4 -0
- package/dist/discovery/index.js +5 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/udp-socket.d.ts +12 -0
- package/dist/discovery/udp-socket.js +105 -0
- package/dist/discovery/udp-socket.js.map +1 -0
- package/dist/electron/main.d.ts +49 -0
- package/dist/electron/main.js +79 -0
- package/dist/electron/main.js.map +1 -0
- package/dist/electron/preload.d.ts +22 -0
- package/dist/electron/preload.js +36 -0
- package/dist/electron/preload.js.map +1 -0
- package/dist/electron/session-service.d.ts +125 -0
- package/dist/electron/session-service.js +661 -0
- package/dist/electron/session-service.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/polyfill/bridge-types.d.ts +82 -0
- package/dist/polyfill/bridge-types.js +2 -0
- package/dist/polyfill/bridge-types.js.map +1 -0
- package/dist/polyfill/install.d.ts +12 -0
- package/dist/polyfill/install.js +546 -0
- package/dist/polyfill/install.js.map +1 -0
- package/dist/protocol/cast-message.d.ts +20 -0
- package/dist/protocol/cast-message.js +166 -0
- package/dist/protocol/cast-message.js.map +1 -0
- package/dist/protocol/errors.d.ts +4 -0
- package/dist/protocol/errors.js +8 -0
- package/dist/protocol/errors.js.map +1 -0
- package/dist/protocol/framing.d.ts +18 -0
- package/dist/protocol/framing.js +95 -0
- package/dist/protocol/framing.js.map +1 -0
- package/dist/protocol/index.d.ts +3 -0
- package/dist/protocol/index.js +4 -0
- package/dist/protocol/index.js.map +1 -0
- package/package.json +59 -4
- package/src/client/cast-client.ts +483 -0
- package/src/client/errors.ts +32 -0
- package/src/client/index.ts +4 -0
- package/src/client/namespaces.ts +10 -0
- package/src/client/types.ts +27 -0
- package/src/discovery/cast-discovery.ts +265 -0
- package/src/discovery/device-cache.ts +231 -0
- package/src/discovery/dns-packet.ts +262 -0
- package/src/discovery/index.ts +4 -0
- package/src/discovery/udp-socket.ts +121 -0
- package/src/electron/main.ts +110 -0
- package/src/electron/preload.ts +57 -0
- package/src/electron/session-service.ts +715 -0
- package/src/index.ts +8 -0
- package/src/polyfill/bridge-types.ts +26 -0
- package/src/polyfill/install.ts +576 -0
- package/src/protocol/cast-message.ts +176 -0
- package/src/protocol/errors.ts +7 -0
- package/src/protocol/framing.ts +97 -0
- package/src/protocol/index.ts +3 -0
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import { CastTimeoutError } from "../client/index.js";
|
|
2
|
+
/** Capability bit names in bit order: 1 video_out, 2 video_in, 4 audio_out, 8 audio_in, 32 multizone_group. */
|
|
3
|
+
export function capabilityNames(bits) {
|
|
4
|
+
const names = [];
|
|
5
|
+
if (bits & 1)
|
|
6
|
+
names.push("video_out");
|
|
7
|
+
if (bits & 2)
|
|
8
|
+
names.push("video_in");
|
|
9
|
+
if (bits & 4)
|
|
10
|
+
names.push("audio_out");
|
|
11
|
+
if (bits & 8)
|
|
12
|
+
names.push("audio_in");
|
|
13
|
+
if (bits & 32)
|
|
14
|
+
names.push("multizone_group");
|
|
15
|
+
return names;
|
|
16
|
+
}
|
|
17
|
+
const CAPABILITY_BITS = {
|
|
18
|
+
video_out: 1,
|
|
19
|
+
video_in: 2,
|
|
20
|
+
audio_out: 4,
|
|
21
|
+
audio_in: 8,
|
|
22
|
+
multizone_group: 32,
|
|
23
|
+
};
|
|
24
|
+
const DEFAULT_TIMEOUT_MS = 60000;
|
|
25
|
+
/** A request that must be reported as a specific chrome.cast error code. */
|
|
26
|
+
class MappedError extends Error {
|
|
27
|
+
code;
|
|
28
|
+
constructor(code, message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.code = code;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** The request bound expired while the client was still working; clean up silently. */
|
|
34
|
+
class Discarded extends Error {
|
|
35
|
+
}
|
|
36
|
+
function describe(err) {
|
|
37
|
+
return err instanceof Error ? err.message : String(err);
|
|
38
|
+
}
|
|
39
|
+
function bad(code, description) {
|
|
40
|
+
return description === undefined ? { ok: false, code } : { ok: false, code, description };
|
|
41
|
+
}
|
|
42
|
+
function good(value) {
|
|
43
|
+
return { ok: true, value };
|
|
44
|
+
}
|
|
45
|
+
function isRecord(v) {
|
|
46
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
47
|
+
}
|
|
48
|
+
function isStringArray(v) {
|
|
49
|
+
return Array.isArray(v) && v.every((x) => typeof x === "string");
|
|
50
|
+
}
|
|
51
|
+
function parseInitialize(params) {
|
|
52
|
+
if (!isRecord(params))
|
|
53
|
+
return null;
|
|
54
|
+
const { appId, capabilities, autoJoinPolicy } = params;
|
|
55
|
+
if (typeof appId !== "string" || !isStringArray(capabilities) || typeof autoJoinPolicy !== "string")
|
|
56
|
+
return null;
|
|
57
|
+
return { appId, capabilities, autoJoinPolicy };
|
|
58
|
+
}
|
|
59
|
+
function parseRequestSession(params) {
|
|
60
|
+
if (!isRecord(params))
|
|
61
|
+
return null;
|
|
62
|
+
const { appId, capabilities, timeoutMs } = params;
|
|
63
|
+
if (typeof appId !== "string" || !isStringArray(capabilities))
|
|
64
|
+
return null;
|
|
65
|
+
const timeout = timeoutMs === undefined ? DEFAULT_TIMEOUT_MS : timeoutMs;
|
|
66
|
+
if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0)
|
|
67
|
+
return null;
|
|
68
|
+
return { appId, capabilities, timeoutMs: timeout };
|
|
69
|
+
}
|
|
70
|
+
function parseSessionId(params) {
|
|
71
|
+
if (!isRecord(params) || typeof params.sessionId !== "string")
|
|
72
|
+
return null;
|
|
73
|
+
return { sessionId: params.sessionId };
|
|
74
|
+
}
|
|
75
|
+
function parseSendMessage(params) {
|
|
76
|
+
if (!isRecord(params))
|
|
77
|
+
return null;
|
|
78
|
+
const { sessionId, namespace, message } = params;
|
|
79
|
+
if (typeof sessionId !== "string" || typeof namespace !== "string" || typeof message !== "string")
|
|
80
|
+
return null;
|
|
81
|
+
return { sessionId, namespace, message };
|
|
82
|
+
}
|
|
83
|
+
function parseSetVolume(params) {
|
|
84
|
+
if (!isRecord(params))
|
|
85
|
+
return null;
|
|
86
|
+
const { sessionId, level } = params;
|
|
87
|
+
if (typeof sessionId !== "string" || typeof level !== "number" || !Number.isFinite(level) || level < 0 || level > 1) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
return { sessionId, level };
|
|
91
|
+
}
|
|
92
|
+
function parseSetMuted(params) {
|
|
93
|
+
if (!isRecord(params))
|
|
94
|
+
return null;
|
|
95
|
+
const { sessionId, muted } = params;
|
|
96
|
+
if (typeof sessionId !== "string" || typeof muted !== "boolean")
|
|
97
|
+
return null;
|
|
98
|
+
return { sessionId, muted };
|
|
99
|
+
}
|
|
100
|
+
/** True when every requested capability bit is set on the device (unknown names are ignored). */
|
|
101
|
+
function deviceMatches(device, capabilities) {
|
|
102
|
+
for (const cap of capabilities) {
|
|
103
|
+
const bit = CAPABILITY_BITS[cap];
|
|
104
|
+
if (bit !== undefined && (device.capabilities & bit) === 0)
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
function sameValue(a, b) {
|
|
110
|
+
if (Object.is(a, b))
|
|
111
|
+
return true;
|
|
112
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
113
|
+
return false;
|
|
114
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
115
|
+
return false;
|
|
116
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
117
|
+
if (a.length !== b.length)
|
|
118
|
+
return false;
|
|
119
|
+
for (let i = 0; i < a.length; i++) {
|
|
120
|
+
if (!sameValue(a[i], b[i]))
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
const ao = a;
|
|
126
|
+
const bo = b;
|
|
127
|
+
const ak = Object.keys(ao);
|
|
128
|
+
const bk = Object.keys(bo);
|
|
129
|
+
if (ak.length !== bk.length)
|
|
130
|
+
return false;
|
|
131
|
+
for (const k of ak) {
|
|
132
|
+
if (!Object.prototype.hasOwnProperty.call(bo, k))
|
|
133
|
+
return false;
|
|
134
|
+
if (!sameValue(ao[k], bo[k]))
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Main-process cast session manager: tracks initialized senders, one client per
|
|
141
|
+
* device and the sessions pages hold; pushes BridgeEvents back via options.emit.
|
|
142
|
+
*/
|
|
143
|
+
export class CastSessionService {
|
|
144
|
+
options;
|
|
145
|
+
senders = new Map();
|
|
146
|
+
sessions = new Map();
|
|
147
|
+
clients = new Map();
|
|
148
|
+
pendingRequests = new Map();
|
|
149
|
+
deviceListener = () => this.onDevicesChanged();
|
|
150
|
+
disposed = false;
|
|
151
|
+
constructor(options) {
|
|
152
|
+
this.options = options;
|
|
153
|
+
options.devices.on("deviceUp", this.deviceListener);
|
|
154
|
+
options.devices.on("deviceDown", this.deviceListener);
|
|
155
|
+
options.devices.on("deviceChanged", this.deviceListener);
|
|
156
|
+
}
|
|
157
|
+
/** Handle one bridge call; never throws or rejects. */
|
|
158
|
+
async handle(sender, method, params) {
|
|
159
|
+
try {
|
|
160
|
+
const allow = this.options.allowOrigin;
|
|
161
|
+
if (allow !== undefined && !allow(sender.origin)) {
|
|
162
|
+
return bad("extension_missing", "Casting is not allowed for this origin");
|
|
163
|
+
}
|
|
164
|
+
switch (method) {
|
|
165
|
+
case "initialize":
|
|
166
|
+
return this.handleInitialize(sender, params);
|
|
167
|
+
case "requestSession":
|
|
168
|
+
return await this.handleRequestSession(sender, params);
|
|
169
|
+
case "joinSession":
|
|
170
|
+
return this.handleJoinSession(sender, params);
|
|
171
|
+
case "sendMessage":
|
|
172
|
+
return this.handleSendMessage(sender, params);
|
|
173
|
+
case "stopSession":
|
|
174
|
+
return await this.handleStopSession(sender, params);
|
|
175
|
+
case "leaveSession":
|
|
176
|
+
return this.handleLeaveSession(sender, params);
|
|
177
|
+
case "setReceiverVolume":
|
|
178
|
+
return await this.handleSetVolume(sender, params);
|
|
179
|
+
case "setReceiverMuted":
|
|
180
|
+
return await this.handleSetMuted(sender, params);
|
|
181
|
+
default:
|
|
182
|
+
return bad("invalid_parameter", `unknown method: ${method}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
return bad("session_error", describe(err));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** Detach a sender that went away: cancel its pending request, leave its sessions, drop its bookkeeping. */
|
|
190
|
+
senderGone(senderId) {
|
|
191
|
+
const pending = this.pendingRequests.get(senderId);
|
|
192
|
+
if (pending !== undefined)
|
|
193
|
+
pending.cancelled.value = true;
|
|
194
|
+
this.pendingRequests.delete(senderId);
|
|
195
|
+
this.senders.delete(senderId);
|
|
196
|
+
for (const rec of [...this.sessions.values()]) {
|
|
197
|
+
if (!rec.members.has(senderId))
|
|
198
|
+
continue;
|
|
199
|
+
this.leave(rec, senderId);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** Unsubscribe, close every client and clear all state. */
|
|
203
|
+
async dispose() {
|
|
204
|
+
if (this.disposed)
|
|
205
|
+
return;
|
|
206
|
+
this.disposed = true;
|
|
207
|
+
const d = this.options.devices;
|
|
208
|
+
d.off("deviceUp", this.deviceListener);
|
|
209
|
+
d.off("deviceDown", this.deviceListener);
|
|
210
|
+
d.off("deviceChanged", this.deviceListener);
|
|
211
|
+
const entries = [...this.clients.values()];
|
|
212
|
+
this.clients.clear();
|
|
213
|
+
for (const entry of entries)
|
|
214
|
+
entry.client.close();
|
|
215
|
+
this.sessions.clear();
|
|
216
|
+
this.senders.clear();
|
|
217
|
+
this.pendingRequests.clear();
|
|
218
|
+
}
|
|
219
|
+
handleInitialize(sender, params) {
|
|
220
|
+
const p = parseInitialize(params);
|
|
221
|
+
if (p === null)
|
|
222
|
+
return bad("invalid_parameter");
|
|
223
|
+
const available = this.options.devices.devices().some((d) => deviceMatches(d, p.capabilities));
|
|
224
|
+
this.senders.set(sender.id, { appId: p.appId, capabilities: p.capabilities, lastAvailable: available });
|
|
225
|
+
const joined = [];
|
|
226
|
+
for (const rec of this.sessions.values()) {
|
|
227
|
+
if (rec.appId !== p.appId || rec.origin !== sender.origin)
|
|
228
|
+
continue;
|
|
229
|
+
if (p.autoJoinPolicy === "origin_scoped") {
|
|
230
|
+
rec.members.add(sender.id);
|
|
231
|
+
joined.push(this.snapshotOf(rec));
|
|
232
|
+
}
|
|
233
|
+
else if (p.autoJoinPolicy === "tab_and_origin_scoped" && rec.members.has(sender.id)) {
|
|
234
|
+
joined.push(this.snapshotOf(rec));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return good({ available, sessions: joined });
|
|
238
|
+
}
|
|
239
|
+
async handleRequestSession(sender, params) {
|
|
240
|
+
const p = parseRequestSession(params);
|
|
241
|
+
if (p === null)
|
|
242
|
+
return bad("invalid_parameter");
|
|
243
|
+
if (this.pendingRequests.has(sender.id))
|
|
244
|
+
return bad("invalid_parameter");
|
|
245
|
+
const discard = { value: false };
|
|
246
|
+
const cancelled = { value: false };
|
|
247
|
+
this.pendingRequests.set(sender.id, { discard, cancelled });
|
|
248
|
+
try {
|
|
249
|
+
const eligible = this.options.devices.devices().filter((d) => deviceMatches(d, p.capabilities));
|
|
250
|
+
if (eligible.length === 0)
|
|
251
|
+
return bad("receiver_unavailable");
|
|
252
|
+
const request = {
|
|
253
|
+
appId: p.appId,
|
|
254
|
+
capabilities: p.capabilities,
|
|
255
|
+
origin: sender.origin,
|
|
256
|
+
senderId: sender.id,
|
|
257
|
+
};
|
|
258
|
+
const current = this.currentSessionFor(sender.id, p.appId);
|
|
259
|
+
if (current !== undefined) {
|
|
260
|
+
request.session = { sessionId: current.app.sessionId, deviceId: current.device.id };
|
|
261
|
+
}
|
|
262
|
+
let choice;
|
|
263
|
+
try {
|
|
264
|
+
choice = await this.options.selectDevice(eligible, request);
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
return bad("session_error", describe(err));
|
|
268
|
+
}
|
|
269
|
+
if (choice === null)
|
|
270
|
+
return bad("cancel");
|
|
271
|
+
if (choice === "stop") {
|
|
272
|
+
const rec = request.session !== undefined ? this.sessions.get(request.session.sessionId) : undefined;
|
|
273
|
+
if (rec === undefined)
|
|
274
|
+
return bad("cancel");
|
|
275
|
+
const stopped = await this.stopSessionRecord(rec, sender.id, true);
|
|
276
|
+
return stopped.ok ? bad("cancel") : stopped;
|
|
277
|
+
}
|
|
278
|
+
if (request.session !== undefined && choice.id === request.session.deviceId) {
|
|
279
|
+
const rec = this.sessions.get(request.session.sessionId);
|
|
280
|
+
if (rec !== undefined)
|
|
281
|
+
return good(this.snapshotOf(rec));
|
|
282
|
+
}
|
|
283
|
+
// The bound starts after the picker resolves; picker time does not count.
|
|
284
|
+
let timer;
|
|
285
|
+
const bound = new Promise((_, reject) => {
|
|
286
|
+
timer = setTimeout(() => {
|
|
287
|
+
discard.value = true;
|
|
288
|
+
reject(new Discarded());
|
|
289
|
+
}, p.timeoutMs);
|
|
290
|
+
});
|
|
291
|
+
try {
|
|
292
|
+
const snapshot = await Promise.race([this.openSession(choice, p.appId, sender, discard, cancelled), bound]);
|
|
293
|
+
return good(snapshot);
|
|
294
|
+
}
|
|
295
|
+
catch (err) {
|
|
296
|
+
if (err instanceof Discarded)
|
|
297
|
+
return bad("timeout", "timed out");
|
|
298
|
+
if (err instanceof MappedError)
|
|
299
|
+
return bad(err.code, err.message);
|
|
300
|
+
throw err;
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
if (timer !== undefined)
|
|
304
|
+
clearTimeout(timer);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
finally {
|
|
308
|
+
this.pendingRequests.delete(sender.id);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
handleJoinSession(sender, params) {
|
|
312
|
+
const p = parseSessionId(params);
|
|
313
|
+
if (p === null)
|
|
314
|
+
return bad("invalid_parameter");
|
|
315
|
+
const rec = this.sessions.get(p.sessionId);
|
|
316
|
+
if (rec === undefined || rec.origin !== sender.origin)
|
|
317
|
+
return bad("session_error");
|
|
318
|
+
rec.members.add(sender.id);
|
|
319
|
+
return good(this.snapshotOf(rec));
|
|
320
|
+
}
|
|
321
|
+
handleSendMessage(sender, params) {
|
|
322
|
+
const p = parseSendMessage(params);
|
|
323
|
+
if (p === null)
|
|
324
|
+
return bad("invalid_parameter");
|
|
325
|
+
const rec = this.sessions.get(p.sessionId);
|
|
326
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
327
|
+
return bad("session_error");
|
|
328
|
+
try {
|
|
329
|
+
rec.client.send(p.namespace, rec.app.transportId, p.message);
|
|
330
|
+
}
|
|
331
|
+
catch (err) {
|
|
332
|
+
return bad("channel_error", describe(err));
|
|
333
|
+
}
|
|
334
|
+
return good(undefined);
|
|
335
|
+
}
|
|
336
|
+
async handleStopSession(sender, params) {
|
|
337
|
+
const p = parseSessionId(params);
|
|
338
|
+
if (p === null)
|
|
339
|
+
return bad("invalid_parameter");
|
|
340
|
+
const rec = this.sessions.get(p.sessionId);
|
|
341
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
342
|
+
return bad("session_error");
|
|
343
|
+
return this.stopSessionRecord(rec, sender.id, false);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Stop a session on the device and tear it down. `stoppingBy` keeps an echoed
|
|
347
|
+
* receiverStatus from notifying the caller; the other members learn from that echo
|
|
348
|
+
* or from the sessionEnded emitted here. When `tellCaller` is set (a picker "stop"),
|
|
349
|
+
* the caller gets sessionEnded too, because it did not call stop itself.
|
|
350
|
+
*/
|
|
351
|
+
async stopSessionRecord(rec, caller, tellCaller) {
|
|
352
|
+
rec.stoppingBy = caller;
|
|
353
|
+
try {
|
|
354
|
+
await rec.client.stop(rec.app.sessionId);
|
|
355
|
+
}
|
|
356
|
+
catch (err) {
|
|
357
|
+
rec.stoppingBy = undefined;
|
|
358
|
+
return bad("session_error", describe(err));
|
|
359
|
+
}
|
|
360
|
+
// An echoed receiverStatus may have already ended the session (and notified the others).
|
|
361
|
+
const current = this.sessions.get(rec.app.sessionId);
|
|
362
|
+
if (current !== undefined) {
|
|
363
|
+
this.sessions.delete(rec.app.sessionId);
|
|
364
|
+
for (const member of current.members) {
|
|
365
|
+
if (!tellCaller && member === caller)
|
|
366
|
+
continue;
|
|
367
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "stopped" });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
else if (tellCaller) {
|
|
371
|
+
this.emitTo(caller, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "stopped" });
|
|
372
|
+
}
|
|
373
|
+
this.cleanupIdle(rec.device.id, rec.client);
|
|
374
|
+
return good(undefined);
|
|
375
|
+
}
|
|
376
|
+
/** The sender's current session for the app, if it has one (the first one found). */
|
|
377
|
+
currentSessionFor(senderId, appId) {
|
|
378
|
+
for (const rec of this.sessions.values()) {
|
|
379
|
+
if (rec.appId === appId && rec.members.has(senderId))
|
|
380
|
+
return rec;
|
|
381
|
+
}
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
handleLeaveSession(sender, params) {
|
|
385
|
+
const p = parseSessionId(params);
|
|
386
|
+
if (p === null)
|
|
387
|
+
return bad("invalid_parameter");
|
|
388
|
+
const rec = this.sessions.get(p.sessionId);
|
|
389
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
390
|
+
return bad("session_error");
|
|
391
|
+
this.leave(rec, sender.id);
|
|
392
|
+
return good(undefined);
|
|
393
|
+
}
|
|
394
|
+
async handleSetVolume(sender, params) {
|
|
395
|
+
const p = parseSetVolume(params);
|
|
396
|
+
if (p === null)
|
|
397
|
+
return bad("invalid_parameter");
|
|
398
|
+
const rec = this.sessions.get(p.sessionId);
|
|
399
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
400
|
+
return bad("session_error");
|
|
401
|
+
try {
|
|
402
|
+
await rec.client.setVolume({ level: p.level });
|
|
403
|
+
}
|
|
404
|
+
catch (err) {
|
|
405
|
+
return bad("session_error", describe(err));
|
|
406
|
+
}
|
|
407
|
+
return good(undefined);
|
|
408
|
+
}
|
|
409
|
+
async handleSetMuted(sender, params) {
|
|
410
|
+
const p = parseSetMuted(params);
|
|
411
|
+
if (p === null)
|
|
412
|
+
return bad("invalid_parameter");
|
|
413
|
+
const rec = this.sessions.get(p.sessionId);
|
|
414
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
415
|
+
return bad("session_error");
|
|
416
|
+
try {
|
|
417
|
+
await rec.client.setVolume({ muted: p.muted });
|
|
418
|
+
}
|
|
419
|
+
catch (err) {
|
|
420
|
+
return bad("session_error", describe(err));
|
|
421
|
+
}
|
|
422
|
+
return good(undefined);
|
|
423
|
+
}
|
|
424
|
+
/** Connect (or reuse) the device's client, launch the app and register the session. */
|
|
425
|
+
async openSession(device, appId, sender, discard, cancelled) {
|
|
426
|
+
const existing = this.clients.get(device.id);
|
|
427
|
+
let entry = existing;
|
|
428
|
+
if (entry === undefined) {
|
|
429
|
+
const client = this.options.createClient(device);
|
|
430
|
+
this.bindClient(client, device);
|
|
431
|
+
entry = {
|
|
432
|
+
client,
|
|
433
|
+
device,
|
|
434
|
+
ready: client.connect().catch((err) => {
|
|
435
|
+
// Remove the entry and close the client once so the next request starts fresh.
|
|
436
|
+
this.dropClient(device.id, client);
|
|
437
|
+
throw err;
|
|
438
|
+
}),
|
|
439
|
+
opening: 0,
|
|
440
|
+
};
|
|
441
|
+
this.clients.set(device.id, entry);
|
|
442
|
+
}
|
|
443
|
+
const client = entry.client;
|
|
444
|
+
entry.opening += 1;
|
|
445
|
+
let registered = false;
|
|
446
|
+
try {
|
|
447
|
+
try {
|
|
448
|
+
await entry.ready;
|
|
449
|
+
}
|
|
450
|
+
catch (err) {
|
|
451
|
+
if (err instanceof CastTimeoutError)
|
|
452
|
+
throw new MappedError("timeout", err.message);
|
|
453
|
+
throw new MappedError("receiver_unavailable", describe(err));
|
|
454
|
+
}
|
|
455
|
+
if (discard.value)
|
|
456
|
+
throw new Discarded();
|
|
457
|
+
if (cancelled.value)
|
|
458
|
+
throw new MappedError("cancel", "cancelled");
|
|
459
|
+
let app;
|
|
460
|
+
try {
|
|
461
|
+
app = await client.launch(appId);
|
|
462
|
+
}
|
|
463
|
+
catch (err) {
|
|
464
|
+
if (err instanceof CastTimeoutError)
|
|
465
|
+
throw new MappedError("timeout", err.message);
|
|
466
|
+
throw new MappedError("session_error", describe(err));
|
|
467
|
+
}
|
|
468
|
+
if (discard.value) {
|
|
469
|
+
client.closeTransport(app.transportId);
|
|
470
|
+
throw new Discarded();
|
|
471
|
+
}
|
|
472
|
+
if (cancelled.value) {
|
|
473
|
+
client.closeTransport(app.transportId);
|
|
474
|
+
throw new MappedError("cancel", "cancelled");
|
|
475
|
+
}
|
|
476
|
+
client.connectTransport(app.transportId);
|
|
477
|
+
let status;
|
|
478
|
+
try {
|
|
479
|
+
status = await client.getStatus();
|
|
480
|
+
}
|
|
481
|
+
catch (err) {
|
|
482
|
+
client.closeTransport(app.transportId);
|
|
483
|
+
throw new MappedError("session_error", describe(err));
|
|
484
|
+
}
|
|
485
|
+
if (discard.value) {
|
|
486
|
+
client.closeTransport(app.transportId);
|
|
487
|
+
throw new Discarded();
|
|
488
|
+
}
|
|
489
|
+
if (cancelled.value) {
|
|
490
|
+
client.closeTransport(app.transportId);
|
|
491
|
+
throw new MappedError("cancel", "cancelled");
|
|
492
|
+
}
|
|
493
|
+
const rec = {
|
|
494
|
+
device,
|
|
495
|
+
client,
|
|
496
|
+
app,
|
|
497
|
+
origin: sender.origin,
|
|
498
|
+
appId,
|
|
499
|
+
members: new Set([sender.id]),
|
|
500
|
+
lastVolume: status.volume,
|
|
501
|
+
lastSnapshot: null,
|
|
502
|
+
};
|
|
503
|
+
const snapshot = this.snapshotOf(rec);
|
|
504
|
+
rec.lastSnapshot = snapshot;
|
|
505
|
+
this.sessions.set(app.sessionId, rec);
|
|
506
|
+
registered = true;
|
|
507
|
+
return snapshot;
|
|
508
|
+
}
|
|
509
|
+
finally {
|
|
510
|
+
entry.opening -= 1;
|
|
511
|
+
if (!registered)
|
|
512
|
+
this.cleanupIdle(device.id, client);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
/** Remove a sender from a session; when it was the last one, tear the session down. */
|
|
516
|
+
leave(rec, senderId) {
|
|
517
|
+
rec.members.delete(senderId);
|
|
518
|
+
if (rec.members.size === 0) {
|
|
519
|
+
rec.client.closeTransport(rec.app.transportId);
|
|
520
|
+
this.sessions.delete(rec.app.sessionId);
|
|
521
|
+
this.cleanupIdle(rec.device.id, rec.client);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/** Subscribe to a freshly created client's events. */
|
|
525
|
+
bindClient(client, device) {
|
|
526
|
+
client.on("message", (msg) => this.onClientMessage(client, msg));
|
|
527
|
+
client.on("receiverStatus", (status) => this.onClientStatus(client, device, status));
|
|
528
|
+
client.on("transportClosed", (transportId) => this.onTransportClosed(client, transportId));
|
|
529
|
+
client.on("close", () => this.onClientClose(client, device.id));
|
|
530
|
+
}
|
|
531
|
+
onClientMessage(client, msg) {
|
|
532
|
+
if (typeof msg.data !== "string")
|
|
533
|
+
return;
|
|
534
|
+
for (const rec of this.sessions.values()) {
|
|
535
|
+
if (rec.client !== client || rec.app.transportId !== msg.sourceId)
|
|
536
|
+
continue;
|
|
537
|
+
for (const member of rec.members) {
|
|
538
|
+
this.emitTo(member, {
|
|
539
|
+
type: "message",
|
|
540
|
+
sessionId: rec.app.sessionId,
|
|
541
|
+
namespace: msg.namespace,
|
|
542
|
+
message: msg.data,
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
onClientStatus(client, device, status) {
|
|
548
|
+
for (const rec of [...this.sessions.values()]) {
|
|
549
|
+
if (rec.client !== client)
|
|
550
|
+
continue;
|
|
551
|
+
const app = status.applications.find((a) => a.sessionId === rec.app.sessionId);
|
|
552
|
+
if (app === undefined) {
|
|
553
|
+
this.sessions.delete(rec.app.sessionId);
|
|
554
|
+
for (const member of rec.members) {
|
|
555
|
+
if (rec.stoppingBy !== undefined && member === rec.stoppingBy)
|
|
556
|
+
continue;
|
|
557
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "stopped" });
|
|
558
|
+
}
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
rec.app = app;
|
|
562
|
+
rec.lastVolume = status.volume;
|
|
563
|
+
const snapshot = this.snapshotOf(rec);
|
|
564
|
+
if (!sameValue(snapshot, rec.lastSnapshot)) {
|
|
565
|
+
rec.lastSnapshot = snapshot;
|
|
566
|
+
for (const member of rec.members) {
|
|
567
|
+
this.emitTo(member, { type: "sessionUpdated", session: snapshot });
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
this.cleanupIdle(device.id, client);
|
|
572
|
+
}
|
|
573
|
+
onTransportClosed(client, transportId) {
|
|
574
|
+
for (const rec of [...this.sessions.values()]) {
|
|
575
|
+
if (rec.client !== client || rec.app.transportId !== transportId)
|
|
576
|
+
continue;
|
|
577
|
+
this.sessions.delete(rec.app.sessionId);
|
|
578
|
+
for (const member of rec.members) {
|
|
579
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "disconnected" });
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
onClientClose(client, deviceId) {
|
|
584
|
+
const entry = this.clients.get(deviceId);
|
|
585
|
+
if (entry !== undefined && entry.client === client)
|
|
586
|
+
this.clients.delete(deviceId);
|
|
587
|
+
for (const rec of [...this.sessions.values()]) {
|
|
588
|
+
if (rec.client !== client)
|
|
589
|
+
continue;
|
|
590
|
+
this.sessions.delete(rec.app.sessionId);
|
|
591
|
+
for (const member of rec.members) {
|
|
592
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "disconnected" });
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
/** Re-check availability for every initialized sender; emit only on change. */
|
|
597
|
+
onDevicesChanged() {
|
|
598
|
+
if (this.disposed)
|
|
599
|
+
return;
|
|
600
|
+
const devices = this.options.devices.devices();
|
|
601
|
+
for (const [id, rec] of this.senders) {
|
|
602
|
+
const available = devices.some((d) => deviceMatches(d, rec.capabilities));
|
|
603
|
+
if (available !== rec.lastAvailable) {
|
|
604
|
+
rec.lastAvailable = available;
|
|
605
|
+
this.emitTo(id, { type: "receiverAvailability", available });
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
/** Close the client when no session uses it any more and no openSession is in flight. */
|
|
610
|
+
cleanupIdle(deviceId, client) {
|
|
611
|
+
const entry = this.clients.get(deviceId);
|
|
612
|
+
if (entry !== undefined && entry.client === client && entry.opening > 0)
|
|
613
|
+
return;
|
|
614
|
+
for (const rec of this.sessions.values()) {
|
|
615
|
+
if (rec.client === client)
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
this.dropClient(deviceId, client);
|
|
619
|
+
}
|
|
620
|
+
/** Forget a client and close it; a no-op unless it is still the registered entry for the device. */
|
|
621
|
+
dropClient(deviceId, client) {
|
|
622
|
+
const entry = this.clients.get(deviceId);
|
|
623
|
+
if (entry === undefined || entry.client !== client)
|
|
624
|
+
return;
|
|
625
|
+
this.clients.delete(deviceId);
|
|
626
|
+
client.close();
|
|
627
|
+
}
|
|
628
|
+
snapshotOf(rec) {
|
|
629
|
+
const app = rec.app;
|
|
630
|
+
const volume = {
|
|
631
|
+
level: rec.lastVolume.level ?? null,
|
|
632
|
+
muted: rec.lastVolume.muted ?? null,
|
|
633
|
+
};
|
|
634
|
+
if (rec.lastVolume.controlType !== undefined)
|
|
635
|
+
volume.controlType = rec.lastVolume.controlType;
|
|
636
|
+
if (rec.lastVolume.stepInterval !== undefined)
|
|
637
|
+
volume.stepInterval = rec.lastVolume.stepInterval;
|
|
638
|
+
return {
|
|
639
|
+
sessionId: app.sessionId,
|
|
640
|
+
appId: app.appId,
|
|
641
|
+
displayName: app.displayName,
|
|
642
|
+
appImages: app.iconUrl ? [{ url: app.iconUrl }] : [],
|
|
643
|
+
statusText: app.statusText ?? null,
|
|
644
|
+
namespaces: app.namespaces,
|
|
645
|
+
transportId: app.transportId,
|
|
646
|
+
receiver: {
|
|
647
|
+
label: rec.device.id,
|
|
648
|
+
friendlyName: rec.device.name,
|
|
649
|
+
capabilities: capabilityNames(rec.device.capabilities),
|
|
650
|
+
volume,
|
|
651
|
+
isActiveInput: null,
|
|
652
|
+
},
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
emitTo(senderId, event) {
|
|
656
|
+
if (this.disposed)
|
|
657
|
+
return;
|
|
658
|
+
this.options.emit(senderId, event);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
//# sourceMappingURL=session-service.js.map
|