electron-cast 0.3.0 → 0.4.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/CHANGELOG.md +66 -0
- package/README.md +122 -12
- package/dist/cjs/client/auth/build-time.d.ts +7 -0
- package/dist/cjs/client/auth/build-time.js +11 -0
- package/dist/cjs/client/auth/build-time.js.map +1 -0
- package/dist/cjs/client/auth/cast-auth.d.ts +44 -0
- package/dist/cjs/client/auth/cast-auth.js +145 -0
- package/dist/cjs/client/auth/cast-auth.js.map +1 -0
- package/dist/cjs/client/auth/cast-cert-validator.d.ts +34 -0
- package/dist/cjs/client/auth/cast-cert-validator.js +89 -0
- package/dist/cjs/client/auth/cast-cert-validator.js.map +1 -0
- package/dist/cjs/client/auth/crl.d.ts +45 -0
- package/dist/cjs/client/auth/crl.js +227 -0
- package/dist/cjs/client/auth/crl.js.map +1 -0
- package/dist/cjs/client/auth/der.d.ts +62 -0
- package/dist/cjs/client/auth/der.js +208 -0
- package/dist/cjs/client/auth/der.js.map +1 -0
- package/dist/cjs/client/auth/names.d.ts +52 -0
- package/dist/cjs/client/auth/names.js +447 -0
- package/dist/cjs/client/auth/names.js.map +1 -0
- package/dist/cjs/client/auth/path-validator.d.ts +29 -0
- package/dist/cjs/client/auth/path-validator.js +421 -0
- package/dist/cjs/client/auth/path-validator.js.map +1 -0
- package/dist/cjs/client/auth/trust-data.d.ts +24 -0
- package/dist/cjs/client/auth/trust-data.js +97 -0
- package/dist/cjs/client/auth/trust-data.js.map +1 -0
- package/dist/cjs/client/auth/x509.d.ts +109 -0
- package/dist/cjs/client/auth/x509.js +442 -0
- package/dist/cjs/client/auth/x509.js.map +1 -0
- package/dist/cjs/client/cast-client.d.ts +111 -0
- package/dist/cjs/client/cast-client.js +505 -0
- package/dist/cjs/client/cast-client.js.map +1 -0
- package/dist/cjs/client/errors.d.ts +28 -0
- package/dist/cjs/client/errors.js +50 -0
- package/dist/cjs/client/errors.js.map +1 -0
- package/dist/cjs/client/index.d.ts +6 -0
- package/dist/cjs/client/index.js +21 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/client/namespaces.d.ts +9 -0
- package/dist/cjs/client/namespaces.js +13 -0
- package/dist/cjs/client/namespaces.js.map +1 -0
- package/dist/cjs/client/types.d.ts +27 -0
- package/dist/cjs/client/types.js +3 -0
- package/dist/cjs/client/types.js.map +1 -0
- package/dist/cjs/discovery/cast-discovery.d.ts +103 -0
- package/dist/cjs/discovery/cast-discovery.js +323 -0
- package/dist/cjs/discovery/cast-discovery.js.map +1 -0
- package/dist/cjs/discovery/device-cache.d.ts +45 -0
- package/dist/cjs/discovery/device-cache.js +240 -0
- package/dist/cjs/discovery/device-cache.js.map +1 -0
- package/dist/cjs/discovery/dns-packet.d.ts +70 -0
- package/dist/cjs/discovery/dns-packet.js +251 -0
- package/dist/cjs/discovery/dns-packet.js.map +1 -0
- package/dist/cjs/discovery/index.d.ts +4 -0
- package/dist/cjs/discovery/index.js +21 -0
- package/dist/cjs/discovery/index.js.map +1 -0
- package/dist/cjs/discovery/udp-socket.d.ts +16 -0
- package/dist/cjs/discovery/udp-socket.js +163 -0
- package/dist/cjs/discovery/udp-socket.js.map +1 -0
- package/dist/cjs/electron/main.d.ts +86 -0
- package/dist/cjs/electron/main.js +408 -0
- package/dist/cjs/electron/main.js.map +1 -0
- package/dist/cjs/electron/manager-api.d.ts +132 -0
- package/dist/cjs/electron/manager-api.js +207 -0
- package/dist/cjs/electron/manager-api.js.map +1 -0
- package/dist/cjs/electron/preload.d.ts +39 -0
- package/dist/cjs/electron/preload.js +96 -0
- package/dist/cjs/electron/preload.js.map +1 -0
- package/dist/cjs/electron/presentation-envelope.d.ts +60 -0
- package/dist/cjs/electron/presentation-envelope.js +318 -0
- package/dist/cjs/electron/presentation-envelope.js.map +1 -0
- package/dist/cjs/electron/presentation-handler.d.ts +100 -0
- package/dist/cjs/electron/presentation-handler.js +499 -0
- package/dist/cjs/electron/presentation-handler.js.map +1 -0
- package/dist/cjs/electron/session-service.d.ts +174 -0
- package/dist/cjs/electron/session-service.js +891 -0
- package/dist/cjs/electron/session-service.js.map +1 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/polyfill/bridge-types.d.ts +86 -0
- package/dist/cjs/polyfill/bridge-types.js +3 -0
- package/dist/cjs/polyfill/bridge-types.js.map +1 -0
- package/dist/cjs/polyfill/install.d.ts +12 -0
- package/dist/cjs/polyfill/install.js +1569 -0
- package/dist/cjs/polyfill/install.js.map +1 -0
- package/dist/cjs/polyfill/presentation-standin.d.ts +13 -0
- package/dist/cjs/polyfill/presentation-standin.js +279 -0
- package/dist/cjs/polyfill/presentation-standin.js.map +1 -0
- package/dist/cjs/protocol/cast-message.d.ts +20 -0
- package/dist/cjs/protocol/cast-message.js +171 -0
- package/dist/cjs/protocol/cast-message.js.map +1 -0
- package/dist/cjs/protocol/device-auth.d.ts +61 -0
- package/dist/cjs/protocol/device-auth.js +113 -0
- package/dist/cjs/protocol/device-auth.js.map +1 -0
- package/dist/cjs/protocol/errors.d.ts +4 -0
- package/dist/cjs/protocol/errors.js +12 -0
- package/dist/cjs/protocol/errors.js.map +1 -0
- package/dist/cjs/protocol/framing.d.ts +18 -0
- package/dist/cjs/protocol/framing.js +100 -0
- package/dist/cjs/protocol/framing.js.map +1 -0
- package/dist/cjs/protocol/index.d.ts +4 -0
- package/dist/cjs/protocol/index.js +21 -0
- package/dist/cjs/protocol/index.js.map +1 -0
- package/dist/cjs/protocol/proto-wire.d.ts +11 -0
- package/dist/cjs/protocol/proto-wire.js +64 -0
- package/dist/cjs/protocol/proto-wire.js.map +1 -0
- package/dist/client/auth/build-time.d.ts +7 -0
- package/dist/client/auth/build-time.js +8 -0
- package/dist/client/auth/build-time.js.map +1 -0
- package/dist/client/auth/cast-auth.d.ts +44 -0
- package/dist/client/auth/cast-auth.js +141 -0
- package/dist/client/auth/cast-auth.js.map +1 -0
- package/dist/client/auth/cast-cert-validator.d.ts +34 -0
- package/dist/client/auth/cast-cert-validator.js +86 -0
- package/dist/client/auth/cast-cert-validator.js.map +1 -0
- package/dist/client/auth/crl.d.ts +45 -0
- package/dist/client/auth/crl.js +221 -0
- package/dist/client/auth/crl.js.map +1 -0
- package/dist/client/auth/der.d.ts +62 -0
- package/dist/client/auth/der.js +196 -0
- package/dist/client/auth/der.js.map +1 -0
- package/dist/client/auth/names.d.ts +52 -0
- package/dist/client/auth/names.js +438 -0
- package/dist/client/auth/names.js.map +1 -0
- package/dist/client/auth/path-validator.d.ts +29 -0
- package/dist/client/auth/path-validator.js +417 -0
- package/dist/client/auth/path-validator.js.map +1 -0
- package/dist/client/auth/trust-data.d.ts +24 -0
- package/dist/client/auth/trust-data.js +94 -0
- package/dist/client/auth/trust-data.js.map +1 -0
- package/dist/client/auth/x509.d.ts +109 -0
- package/dist/client/auth/x509.js +430 -0
- package/dist/client/auth/x509.js.map +1 -0
- package/dist/client/cast-client.d.ts +20 -1
- package/dist/client/cast-client.js +74 -3
- package/dist/client/cast-client.js.map +1 -1
- package/dist/client/errors.d.ts +11 -0
- package/dist/client/errors.js +13 -0
- package/dist/client/errors.js.map +1 -1
- package/dist/client/index.d.ts +2 -0
- package/dist/discovery/cast-discovery.d.ts +21 -2
- package/dist/discovery/cast-discovery.js +94 -14
- package/dist/discovery/cast-discovery.js.map +1 -1
- package/dist/discovery/device-cache.d.ts +6 -1
- package/dist/discovery/device-cache.js +31 -7
- package/dist/discovery/device-cache.js.map +1 -1
- package/dist/discovery/udp-socket.d.ts +5 -1
- package/dist/discovery/udp-socket.js +56 -5
- package/dist/discovery/udp-socket.js.map +1 -1
- package/dist/electron/main.d.ts +26 -4
- package/dist/electron/main.js +193 -55
- package/dist/electron/main.js.map +1 -1
- package/dist/electron/manager-api.d.ts +132 -0
- package/dist/electron/manager-api.js +198 -0
- package/dist/electron/manager-api.js.map +1 -0
- package/dist/electron/preload.d.ts +5 -0
- package/dist/electron/preload.js +9 -1
- package/dist/electron/preload.js.map +1 -1
- package/dist/electron/presentation-envelope.js +7 -1
- package/dist/electron/presentation-envelope.js.map +1 -1
- package/dist/electron/presentation-handler.js +16 -9
- package/dist/electron/presentation-handler.js.map +1 -1
- package/dist/electron/session-service.d.ts +50 -1
- package/dist/electron/session-service.js +256 -33
- package/dist/electron/session-service.js.map +1 -1
- package/dist/polyfill/presentation-standin.js +3 -2
- package/dist/polyfill/presentation-standin.js.map +1 -1
- package/dist/protocol/device-auth.d.ts +61 -0
- package/dist/protocol/device-auth.js +106 -0
- package/dist/protocol/device-auth.js.map +1 -0
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/proto-wire.d.ts +11 -0
- package/dist/protocol/proto-wire.js +60 -0
- package/dist/protocol/proto-wire.js.map +1 -0
- package/package.json +47 -12
- package/src/client/auth/build-time.ts +7 -0
- package/src/client/auth/cast-auth.ts +184 -0
- package/src/client/auth/cast-cert-validator.ts +133 -0
- package/src/client/auth/crl.ts +228 -0
- package/src/client/auth/der.ts +193 -0
- package/src/client/auth/names.ts +411 -0
- package/src/client/auth/path-validator.ts +449 -0
- package/src/client/auth/trust-data.ts +95 -0
- package/src/client/auth/x509.ts +488 -0
- package/src/client/cast-client.ts +85 -3
- package/src/client/errors.ts +29 -0
- package/src/client/index.ts +2 -0
- package/src/discovery/cast-discovery.ts +104 -19
- package/src/discovery/device-cache.ts +38 -8
- package/src/discovery/udp-socket.ts +63 -4
- package/src/electron/main.ts +220 -60
- package/src/electron/manager-api.ts +312 -0
- package/src/electron/preload.ts +15 -1
- package/src/electron/presentation-envelope.ts +7 -1
- package/src/electron/presentation-handler.ts +16 -10
- package/src/electron/session-service.ts +265 -34
- package/src/polyfill/presentation-standin.ts +3 -2
- package/src/protocol/device-auth.ts +126 -0
- package/src/protocol/index.ts +1 -0
- package/src/protocol/proto-wire.ts +55 -0
|
@@ -0,0 +1,891 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CastSessionService = void 0;
|
|
4
|
+
exports.capabilityNames = capabilityNames;
|
|
5
|
+
const index_ts_1 = require("../client/index.js");
|
|
6
|
+
const manager_api_ts_1 = require("./manager-api.js");
|
|
7
|
+
/** Capability bit names in bit order: 1 video_out, 2 video_in, 4 audio_out, 8 audio_in, 32 multizone_group. */
|
|
8
|
+
function capabilityNames(bits) {
|
|
9
|
+
const names = [];
|
|
10
|
+
if (bits & 1)
|
|
11
|
+
names.push("video_out");
|
|
12
|
+
if (bits & 2)
|
|
13
|
+
names.push("video_in");
|
|
14
|
+
if (bits & 4)
|
|
15
|
+
names.push("audio_out");
|
|
16
|
+
if (bits & 8)
|
|
17
|
+
names.push("audio_in");
|
|
18
|
+
if (bits & 32)
|
|
19
|
+
names.push("multizone_group");
|
|
20
|
+
return names;
|
|
21
|
+
}
|
|
22
|
+
const CAPABILITY_BITS = {
|
|
23
|
+
video_out: 1,
|
|
24
|
+
video_in: 2,
|
|
25
|
+
audio_out: 4,
|
|
26
|
+
audio_in: 8,
|
|
27
|
+
multizone_group: 32,
|
|
28
|
+
};
|
|
29
|
+
const DEFAULT_TIMEOUT_MS = 60000;
|
|
30
|
+
/** The caller id of a stop that comes from the main process, not from a page. */
|
|
31
|
+
const MAIN_CALLER = -1;
|
|
32
|
+
/** A request that must be reported as a specific chrome.cast error code. */
|
|
33
|
+
class MappedError extends Error {
|
|
34
|
+
code;
|
|
35
|
+
constructor(code, message) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.code = code;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The request bound expired while the client was still working; clean up silently. */
|
|
41
|
+
class Discarded extends Error {
|
|
42
|
+
}
|
|
43
|
+
function describe(err) {
|
|
44
|
+
return err instanceof Error ? err.message : String(err);
|
|
45
|
+
}
|
|
46
|
+
function bad(code, description) {
|
|
47
|
+
return description === undefined ? { ok: false, code } : { ok: false, code, description };
|
|
48
|
+
}
|
|
49
|
+
function good(value) {
|
|
50
|
+
return { ok: true, value };
|
|
51
|
+
}
|
|
52
|
+
function isRecord(v) {
|
|
53
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
54
|
+
}
|
|
55
|
+
function isStringArray(v) {
|
|
56
|
+
return Array.isArray(v) && v.every((x) => typeof x === "string");
|
|
57
|
+
}
|
|
58
|
+
function parseInitialize(params) {
|
|
59
|
+
if (!isRecord(params))
|
|
60
|
+
return null;
|
|
61
|
+
const { appId, capabilities, autoJoinPolicy } = params;
|
|
62
|
+
if (typeof appId !== "string" || !isStringArray(capabilities) || typeof autoJoinPolicy !== "string")
|
|
63
|
+
return null;
|
|
64
|
+
return { appId, capabilities, autoJoinPolicy };
|
|
65
|
+
}
|
|
66
|
+
function parseRequestSession(params) {
|
|
67
|
+
if (!isRecord(params))
|
|
68
|
+
return null;
|
|
69
|
+
const { appId, capabilities, timeoutMs } = params;
|
|
70
|
+
if (typeof appId !== "string" || !isStringArray(capabilities))
|
|
71
|
+
return null;
|
|
72
|
+
const timeout = timeoutMs === undefined ? DEFAULT_TIMEOUT_MS : timeoutMs;
|
|
73
|
+
if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0)
|
|
74
|
+
return null;
|
|
75
|
+
return { appId, capabilities, timeoutMs: timeout };
|
|
76
|
+
}
|
|
77
|
+
function parseSessionId(params) {
|
|
78
|
+
if (!isRecord(params) || typeof params.sessionId !== "string")
|
|
79
|
+
return null;
|
|
80
|
+
return { sessionId: params.sessionId };
|
|
81
|
+
}
|
|
82
|
+
function parseSendMessage(params) {
|
|
83
|
+
if (!isRecord(params))
|
|
84
|
+
return null;
|
|
85
|
+
const { sessionId, namespace, message } = params;
|
|
86
|
+
if (typeof sessionId !== "string" || typeof namespace !== "string" || typeof message !== "string")
|
|
87
|
+
return null;
|
|
88
|
+
return { sessionId, namespace, message };
|
|
89
|
+
}
|
|
90
|
+
function parseSetVolume(params) {
|
|
91
|
+
if (!isRecord(params))
|
|
92
|
+
return null;
|
|
93
|
+
const { sessionId, level } = params;
|
|
94
|
+
if (typeof sessionId !== "string" || typeof level !== "number" || !Number.isFinite(level) || level < 0 || level > 1) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return { sessionId, level };
|
|
98
|
+
}
|
|
99
|
+
function parseSetMuted(params) {
|
|
100
|
+
if (!isRecord(params))
|
|
101
|
+
return null;
|
|
102
|
+
const { sessionId, muted } = params;
|
|
103
|
+
if (typeof sessionId !== "string" || typeof muted !== "boolean")
|
|
104
|
+
return null;
|
|
105
|
+
return { sessionId, muted };
|
|
106
|
+
}
|
|
107
|
+
/** True when every requested capability bit is set on the device (unknown names are ignored). */
|
|
108
|
+
function deviceMatches(device, capabilities) {
|
|
109
|
+
for (const cap of capabilities) {
|
|
110
|
+
const bit = CAPABILITY_BITS[cap];
|
|
111
|
+
if (bit !== undefined && (device.capabilities & bit) === 0)
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
function sameValue(a, b) {
|
|
117
|
+
if (Object.is(a, b))
|
|
118
|
+
return true;
|
|
119
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
120
|
+
return false;
|
|
121
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
122
|
+
return false;
|
|
123
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
124
|
+
if (a.length !== b.length)
|
|
125
|
+
return false;
|
|
126
|
+
for (let i = 0; i < a.length; i++) {
|
|
127
|
+
if (!sameValue(a[i], b[i]))
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
const ao = a;
|
|
133
|
+
const bo = b;
|
|
134
|
+
const ak = Object.keys(ao);
|
|
135
|
+
const bk = Object.keys(bo);
|
|
136
|
+
if (ak.length !== bk.length)
|
|
137
|
+
return false;
|
|
138
|
+
for (const k of ak) {
|
|
139
|
+
if (!Object.prototype.hasOwnProperty.call(bo, k))
|
|
140
|
+
return false;
|
|
141
|
+
if (!sameValue(ao[k], bo[k]))
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Main-process cast session manager: tracks initialized senders, one client per
|
|
148
|
+
* device and the sessions pages hold; pushes BridgeEvents back via options.emit.
|
|
149
|
+
*/
|
|
150
|
+
class CastSessionService {
|
|
151
|
+
options;
|
|
152
|
+
senders = new Map();
|
|
153
|
+
sessions = new manager_api_ts_1.ObservedMap((kind, rec) => this.notifySession(kind, rec));
|
|
154
|
+
sessionListeners = new Set();
|
|
155
|
+
sinks;
|
|
156
|
+
clients = new Map();
|
|
157
|
+
pendingRequests = new Map();
|
|
158
|
+
deviceListener = () => this.onDevicesChanged();
|
|
159
|
+
disposed = false;
|
|
160
|
+
constructor(options) {
|
|
161
|
+
this.options = options;
|
|
162
|
+
this.sinks = (0, manager_api_ts_1.createSinksObservers)(options.devices);
|
|
163
|
+
options.devices.on("deviceUp", this.deviceListener);
|
|
164
|
+
options.devices.on("deviceDown", this.deviceListener);
|
|
165
|
+
options.devices.on("deviceChanged", this.deviceListener);
|
|
166
|
+
}
|
|
167
|
+
/** Handle one bridge call; never throws or rejects. */
|
|
168
|
+
async handle(sender, method, params) {
|
|
169
|
+
try {
|
|
170
|
+
const allow = this.options.allowOrigin;
|
|
171
|
+
if (allow !== undefined && !allow(sender.origin)) {
|
|
172
|
+
return bad("extension_missing", "Casting is not allowed for this origin");
|
|
173
|
+
}
|
|
174
|
+
switch (method) {
|
|
175
|
+
case "initialize":
|
|
176
|
+
return this.handleInitialize(sender, params);
|
|
177
|
+
case "requestSession":
|
|
178
|
+
return await this.handleRequestSession(sender, params);
|
|
179
|
+
case "joinSession":
|
|
180
|
+
return this.handleJoinSession(sender, params, true);
|
|
181
|
+
case "joinRunningSession":
|
|
182
|
+
return await this.handleJoinRunningSession(sender, params);
|
|
183
|
+
case "sendMessage":
|
|
184
|
+
return this.handleSendMessage(sender, params);
|
|
185
|
+
case "stopSession":
|
|
186
|
+
return await this.handleStopSession(sender, params);
|
|
187
|
+
case "leaveSession":
|
|
188
|
+
return this.handleLeaveSession(sender, params);
|
|
189
|
+
case "setReceiverVolume":
|
|
190
|
+
return await this.handleSetVolume(sender, params);
|
|
191
|
+
case "setReceiverMuted":
|
|
192
|
+
return await this.handleSetMuted(sender, params);
|
|
193
|
+
default:
|
|
194
|
+
return bad("invalid_parameter", `unknown method: ${method}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
return bad("session_error", describe(err));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** Detach a sender that went away: cancel its pending request, leave its sessions, drop its bookkeeping. */
|
|
202
|
+
senderGone(senderId) {
|
|
203
|
+
this.forgetSender(senderId);
|
|
204
|
+
for (const rec of [...this.sessions.values()]) {
|
|
205
|
+
if (!rec.members.has(senderId) && !rec.parked.has(senderId))
|
|
206
|
+
continue;
|
|
207
|
+
this.leave(rec, senderId);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The sender's page navigated (a reload or a new page): cancel its pending request and drop its bookkeeping, but
|
|
212
|
+
* keep its sessions open on the device, parked, so the next page can rejoin them (auto-join or joinSession) as it
|
|
213
|
+
* would in Chrome. A parked session ends as usual if the receiver app stops or the device drops; senderGone ends
|
|
214
|
+
* the sender's share of it for good.
|
|
215
|
+
*/
|
|
216
|
+
senderNavigated(senderId) {
|
|
217
|
+
this.forgetSender(senderId);
|
|
218
|
+
for (const rec of this.sessions.values()) {
|
|
219
|
+
if (!rec.members.delete(senderId))
|
|
220
|
+
continue;
|
|
221
|
+
rec.parked.add(senderId);
|
|
222
|
+
this.notifySession("updated", rec);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
forgetSender(senderId) {
|
|
226
|
+
const pending = this.pendingRequests.get(senderId);
|
|
227
|
+
if (pending !== undefined) {
|
|
228
|
+
pending.cancelled.value = true;
|
|
229
|
+
pending.picker?.close();
|
|
230
|
+
}
|
|
231
|
+
this.pendingRequests.delete(senderId);
|
|
232
|
+
this.senders.delete(senderId);
|
|
233
|
+
}
|
|
234
|
+
/** Unsubscribe, close every client and clear all state. */
|
|
235
|
+
async dispose() {
|
|
236
|
+
if (this.disposed)
|
|
237
|
+
return;
|
|
238
|
+
this.disposed = true;
|
|
239
|
+
this.sinks.dispose();
|
|
240
|
+
const d = this.options.devices;
|
|
241
|
+
d.off("deviceUp", this.deviceListener);
|
|
242
|
+
d.off("deviceDown", this.deviceListener);
|
|
243
|
+
d.off("deviceChanged", this.deviceListener);
|
|
244
|
+
for (const pending of this.pendingRequests.values())
|
|
245
|
+
pending.picker?.close();
|
|
246
|
+
const entries = [...this.clients.values()];
|
|
247
|
+
this.clients.clear();
|
|
248
|
+
for (const entry of entries)
|
|
249
|
+
entry.client.close();
|
|
250
|
+
this.sessions.clear();
|
|
251
|
+
this.senders.clear();
|
|
252
|
+
this.pendingRequests.clear();
|
|
253
|
+
}
|
|
254
|
+
handleInitialize(sender, params) {
|
|
255
|
+
const p = parseInitialize(params);
|
|
256
|
+
if (p === null)
|
|
257
|
+
return bad("invalid_parameter");
|
|
258
|
+
const available = this.options.devices.devices().some((d) => deviceMatches(d, p.capabilities));
|
|
259
|
+
this.senders.set(sender.id, { appId: p.appId, capabilities: p.capabilities, lastAvailable: available });
|
|
260
|
+
const joined = [];
|
|
261
|
+
for (const rec of this.sessions.values()) {
|
|
262
|
+
if (rec.appId !== p.appId || rec.origin !== sender.origin)
|
|
263
|
+
continue;
|
|
264
|
+
// tab_and_origin_scoped: this sender (the same tab) is a member, or was one before its page navigated
|
|
265
|
+
const sameTab = rec.members.has(sender.id) || rec.parked.has(sender.id);
|
|
266
|
+
if (p.autoJoinPolicy === "origin_scoped" || (p.autoJoinPolicy === "tab_and_origin_scoped" && sameTab)) {
|
|
267
|
+
rec.parked.delete(sender.id);
|
|
268
|
+
rec.members.add(sender.id);
|
|
269
|
+
this.notifySession("updated", rec);
|
|
270
|
+
joined.push(this.snapshotOf(rec));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return good({ available, sessions: joined });
|
|
274
|
+
}
|
|
275
|
+
async handleRequestSession(sender, params) {
|
|
276
|
+
const p = parseRequestSession(params);
|
|
277
|
+
if (p === null)
|
|
278
|
+
return bad("invalid_parameter");
|
|
279
|
+
if (this.pendingRequests.has(sender.id))
|
|
280
|
+
return bad("invalid_parameter");
|
|
281
|
+
const discard = { value: false };
|
|
282
|
+
const cancelled = { value: false };
|
|
283
|
+
const pending = { discard, cancelled };
|
|
284
|
+
this.pendingRequests.set(sender.id, pending);
|
|
285
|
+
try {
|
|
286
|
+
const eligible = this.options.devices.devices().filter((d) => deviceMatches(d, p.capabilities));
|
|
287
|
+
if (eligible.length === 0)
|
|
288
|
+
return bad("receiver_unavailable");
|
|
289
|
+
const request = {
|
|
290
|
+
appId: p.appId,
|
|
291
|
+
capabilities: p.capabilities,
|
|
292
|
+
origin: sender.origin,
|
|
293
|
+
senderId: sender.id,
|
|
294
|
+
};
|
|
295
|
+
const current = this.currentSessionFor(sender.id, p.appId);
|
|
296
|
+
if (current !== undefined) {
|
|
297
|
+
request.session = { sessionId: current.app.sessionId, deviceId: current.device.id };
|
|
298
|
+
}
|
|
299
|
+
let choice;
|
|
300
|
+
// The picker gets live device updates until it answers; closing aborts its signal.
|
|
301
|
+
const pick = (0, manager_api_ts_1.createPickerContext)(this.sinks, (d) => deviceMatches(d, p.capabilities));
|
|
302
|
+
pending.picker = pick;
|
|
303
|
+
try {
|
|
304
|
+
choice = await this.options.selectDevice(eligible, request, pick.context);
|
|
305
|
+
}
|
|
306
|
+
catch (err) {
|
|
307
|
+
return bad("session_error", describe(err));
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
pick.close();
|
|
311
|
+
}
|
|
312
|
+
if (choice === null)
|
|
313
|
+
return bad("cancel");
|
|
314
|
+
if (choice === "stop") {
|
|
315
|
+
const rec = request.session !== undefined ? this.sessions.get(request.session.sessionId) : undefined;
|
|
316
|
+
if (rec === undefined)
|
|
317
|
+
return bad("cancel");
|
|
318
|
+
// Like Chrome, tell the caller's receiver action listeners about the stop before the session ends.
|
|
319
|
+
this.emitTo(sender.id, { type: "receiverAction", sessionId: rec.app.sessionId, action: "stop" });
|
|
320
|
+
const stopped = await this.stopSessionRecord(rec, sender.id, true);
|
|
321
|
+
return stopped.ok ? bad("cancel") : stopped;
|
|
322
|
+
}
|
|
323
|
+
if (request.session !== undefined && choice.id === request.session.deviceId) {
|
|
324
|
+
const rec = this.sessions.get(request.session.sessionId);
|
|
325
|
+
if (rec !== undefined)
|
|
326
|
+
return good(this.snapshotOf(rec));
|
|
327
|
+
}
|
|
328
|
+
// The bound starts after the picker resolves; picker time does not count.
|
|
329
|
+
let timer;
|
|
330
|
+
const bound = new Promise((_, reject) => {
|
|
331
|
+
timer = setTimeout(() => {
|
|
332
|
+
discard.value = true;
|
|
333
|
+
reject(new Discarded());
|
|
334
|
+
}, p.timeoutMs);
|
|
335
|
+
});
|
|
336
|
+
try {
|
|
337
|
+
const snapshot = await Promise.race([this.openSession(choice, p.appId, sender, discard, cancelled), bound]);
|
|
338
|
+
return good(snapshot);
|
|
339
|
+
}
|
|
340
|
+
catch (err) {
|
|
341
|
+
if (err instanceof Discarded)
|
|
342
|
+
return bad("timeout", "timed out");
|
|
343
|
+
if (err instanceof MappedError)
|
|
344
|
+
return bad(err.code, err.message);
|
|
345
|
+
throw err;
|
|
346
|
+
}
|
|
347
|
+
finally {
|
|
348
|
+
if (timer !== undefined)
|
|
349
|
+
clearTimeout(timer);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
finally {
|
|
353
|
+
this.pendingRequests.delete(sender.id);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* `checkOrigin` is joinSession's own rule (mode "chromecast", which is frozen and has no
|
|
358
|
+
* Chromium counterpart). Joining by id, as Chrome's Presentation API path does, has none: AppActivity::CanJoinSession checks only
|
|
359
|
+
* the app and that the client is not connected yet.
|
|
360
|
+
*/
|
|
361
|
+
handleJoinSession(sender, params, checkOrigin) {
|
|
362
|
+
const p = parseSessionId(params);
|
|
363
|
+
if (p === null)
|
|
364
|
+
return bad("invalid_parameter");
|
|
365
|
+
const rec = this.sessions.get(p.sessionId);
|
|
366
|
+
if (rec === undefined || (checkOrigin && rec.origin !== sender.origin))
|
|
367
|
+
return bad("session_error");
|
|
368
|
+
rec.parked.delete(sender.id);
|
|
369
|
+
rec.members.add(sender.id);
|
|
370
|
+
this.notifySession("updated", rec);
|
|
371
|
+
return good(this.snapshotOf(rec));
|
|
372
|
+
}
|
|
373
|
+
handleSendMessage(sender, params) {
|
|
374
|
+
const p = parseSendMessage(params);
|
|
375
|
+
if (p === null)
|
|
376
|
+
return bad("invalid_parameter");
|
|
377
|
+
const rec = this.sessions.get(p.sessionId);
|
|
378
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
379
|
+
return bad("session_error");
|
|
380
|
+
try {
|
|
381
|
+
rec.client.send(p.namespace, rec.app.transportId, p.message);
|
|
382
|
+
}
|
|
383
|
+
catch (err) {
|
|
384
|
+
return bad("channel_error", describe(err));
|
|
385
|
+
}
|
|
386
|
+
return good(undefined);
|
|
387
|
+
}
|
|
388
|
+
async handleStopSession(sender, params) {
|
|
389
|
+
const p = parseSessionId(params);
|
|
390
|
+
if (p === null)
|
|
391
|
+
return bad("invalid_parameter");
|
|
392
|
+
const rec = this.sessions.get(p.sessionId);
|
|
393
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
394
|
+
return bad("session_error");
|
|
395
|
+
return this.stopSessionRecord(rec, sender.id, false);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Stop a session on the device and tear it down. `stoppingBy` keeps an echoed
|
|
399
|
+
* receiverStatus from notifying the caller; the other members learn from that echo
|
|
400
|
+
* or from the sessionEnded emitted here. When `tellCaller` is set (a picker "stop"),
|
|
401
|
+
* the caller gets sessionEnded too, because it did not call stop itself.
|
|
402
|
+
*/
|
|
403
|
+
async stopSessionRecord(rec, caller, tellCaller) {
|
|
404
|
+
rec.stoppingBy = caller;
|
|
405
|
+
try {
|
|
406
|
+
await rec.client.stop(rec.app.sessionId);
|
|
407
|
+
}
|
|
408
|
+
catch (err) {
|
|
409
|
+
rec.stoppingBy = undefined;
|
|
410
|
+
return bad("session_error", describe(err));
|
|
411
|
+
}
|
|
412
|
+
// An echoed receiverStatus may have already ended the session (and notified the others).
|
|
413
|
+
const current = this.sessions.get(rec.app.sessionId);
|
|
414
|
+
if (current !== undefined) {
|
|
415
|
+
this.sessions.delete(rec.app.sessionId);
|
|
416
|
+
for (const member of current.members) {
|
|
417
|
+
if (!tellCaller && member === caller)
|
|
418
|
+
continue;
|
|
419
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "stopped" });
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else if (tellCaller) {
|
|
423
|
+
this.emitTo(caller, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "stopped" });
|
|
424
|
+
}
|
|
425
|
+
this.cleanupIdle(rec.device.id, rec.client);
|
|
426
|
+
return good(undefined);
|
|
427
|
+
}
|
|
428
|
+
/** The sender's current session for the app, if it has one (the first one found). */
|
|
429
|
+
currentSessionFor(senderId, appId) {
|
|
430
|
+
for (const rec of this.sessions.values()) {
|
|
431
|
+
if (rec.appId === appId && rec.members.has(senderId))
|
|
432
|
+
return rec;
|
|
433
|
+
}
|
|
434
|
+
return undefined;
|
|
435
|
+
}
|
|
436
|
+
handleLeaveSession(sender, params) {
|
|
437
|
+
const p = parseSessionId(params);
|
|
438
|
+
if (p === null)
|
|
439
|
+
return bad("invalid_parameter");
|
|
440
|
+
const rec = this.sessions.get(p.sessionId);
|
|
441
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
442
|
+
return bad("session_error");
|
|
443
|
+
this.leave(rec, sender.id);
|
|
444
|
+
return good(undefined);
|
|
445
|
+
}
|
|
446
|
+
async handleSetVolume(sender, params) {
|
|
447
|
+
const p = parseSetVolume(params);
|
|
448
|
+
if (p === null)
|
|
449
|
+
return bad("invalid_parameter");
|
|
450
|
+
const rec = this.sessions.get(p.sessionId);
|
|
451
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
452
|
+
return bad("session_error");
|
|
453
|
+
try {
|
|
454
|
+
await rec.client.setVolume({ level: p.level });
|
|
455
|
+
}
|
|
456
|
+
catch (err) {
|
|
457
|
+
return bad("session_error", describe(err));
|
|
458
|
+
}
|
|
459
|
+
return good(undefined);
|
|
460
|
+
}
|
|
461
|
+
async handleSetMuted(sender, params) {
|
|
462
|
+
const p = parseSetMuted(params);
|
|
463
|
+
if (p === null)
|
|
464
|
+
return bad("invalid_parameter");
|
|
465
|
+
const rec = this.sessions.get(p.sessionId);
|
|
466
|
+
if (rec === undefined || !rec.members.has(sender.id))
|
|
467
|
+
return bad("session_error");
|
|
468
|
+
try {
|
|
469
|
+
await rec.client.setVolume({ muted: p.muted });
|
|
470
|
+
}
|
|
471
|
+
catch (err) {
|
|
472
|
+
return bad("session_error", describe(err));
|
|
473
|
+
}
|
|
474
|
+
return good(undefined);
|
|
475
|
+
}
|
|
476
|
+
/** The device's client entry, created and connecting on first use. */
|
|
477
|
+
clientFor(device) {
|
|
478
|
+
const existing = this.clients.get(device.id);
|
|
479
|
+
if (existing !== undefined)
|
|
480
|
+
return existing;
|
|
481
|
+
const client = this.options.createClient(device);
|
|
482
|
+
this.bindClient(client, device);
|
|
483
|
+
const entry = {
|
|
484
|
+
client,
|
|
485
|
+
device,
|
|
486
|
+
ready: client.connect().catch((err) => {
|
|
487
|
+
// Remove the entry and close the client once so the next request starts fresh.
|
|
488
|
+
this.dropClient(device.id, client);
|
|
489
|
+
throw err;
|
|
490
|
+
}),
|
|
491
|
+
opening: 0,
|
|
492
|
+
};
|
|
493
|
+
// A lookup may never await this; openSession still sees the rejection.
|
|
494
|
+
entry.ready.catch(() => { });
|
|
495
|
+
this.clients.set(device.id, entry);
|
|
496
|
+
return entry;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Join a session by its id, as Chrome's Media Router does for `reconnect("cast-session_<id>")`. A session this
|
|
500
|
+
* service already holds is joined without an origin check, like one found on a device. Otherwise the id is looked up
|
|
501
|
+
* in the receiver status of
|
|
502
|
+
* every known device: Chrome's CastSessionTracker learns the sessions running on all sinks and
|
|
503
|
+
* CastActivityManager::AddNonLocalActivity makes them joinable by id (and only by id: auto-join skips them, and no
|
|
504
|
+
* origin is checked). `appId`, when given, must be the session's app (CastActivity::CanJoinSession). Unlike Chrome
|
|
505
|
+
* the lookup runs when asked instead of tracking every device all the time.
|
|
506
|
+
*/
|
|
507
|
+
async handleJoinRunningSession(sender, params) {
|
|
508
|
+
const p = parseSessionId(params);
|
|
509
|
+
if (p === null)
|
|
510
|
+
return bad("invalid_parameter");
|
|
511
|
+
const appId = isRecord(params) && typeof params.appId === "string" ? params.appId : undefined;
|
|
512
|
+
const known = this.sessions.get(p.sessionId);
|
|
513
|
+
if (known !== undefined) {
|
|
514
|
+
if (appId !== undefined && known.appId !== appId)
|
|
515
|
+
return bad("session_error");
|
|
516
|
+
return this.handleJoinSession(sender, params, false);
|
|
517
|
+
}
|
|
518
|
+
const found = await this.findRunningSession(p.sessionId);
|
|
519
|
+
if (found === undefined)
|
|
520
|
+
return bad("session_error");
|
|
521
|
+
const { entry, app, volume } = found;
|
|
522
|
+
try {
|
|
523
|
+
if (this.disposed || (appId !== undefined && app.appId !== appId))
|
|
524
|
+
return bad("session_error");
|
|
525
|
+
// Another join of the same session finished while this one looked.
|
|
526
|
+
if (this.sessions.has(app.sessionId))
|
|
527
|
+
return this.handleJoinSession(sender, params, false);
|
|
528
|
+
entry.client.connectTransport(app.transportId);
|
|
529
|
+
const rec = {
|
|
530
|
+
device: entry.device,
|
|
531
|
+
client: entry.client,
|
|
532
|
+
app,
|
|
533
|
+
origin: sender.origin,
|
|
534
|
+
appId: app.appId,
|
|
535
|
+
members: new Set([sender.id]),
|
|
536
|
+
parked: new Set(),
|
|
537
|
+
lastVolume: volume,
|
|
538
|
+
lastSnapshot: null,
|
|
539
|
+
};
|
|
540
|
+
rec.lastSnapshot = this.snapshotOf(rec);
|
|
541
|
+
this.sessions.set(app.sessionId, rec);
|
|
542
|
+
return good(rec.lastSnapshot);
|
|
543
|
+
}
|
|
544
|
+
finally {
|
|
545
|
+
entry.opening -= 1;
|
|
546
|
+
if (!this.sessions.has(app.sessionId))
|
|
547
|
+
this.cleanupIdle(entry.device.id, entry.client);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Ask every known device what it runs and resolve with the one running `sessionId`. The winner's `opening` count
|
|
552
|
+
* stays raised for the caller to release; a device that cannot be reached or runs something else is let go.
|
|
553
|
+
*/
|
|
554
|
+
findRunningSession(sessionId) {
|
|
555
|
+
const devices = this.options.devices.devices();
|
|
556
|
+
return new Promise((resolve) => {
|
|
557
|
+
let pending = devices.length;
|
|
558
|
+
let won = false;
|
|
559
|
+
if (pending === 0)
|
|
560
|
+
resolve(undefined);
|
|
561
|
+
for (const device of devices) {
|
|
562
|
+
const entry = this.clientFor(device);
|
|
563
|
+
entry.opening += 1;
|
|
564
|
+
void (async () => {
|
|
565
|
+
let app;
|
|
566
|
+
let volume;
|
|
567
|
+
try {
|
|
568
|
+
await entry.ready;
|
|
569
|
+
const status = await entry.client.getStatus();
|
|
570
|
+
app = status.applications.find((a) => a.sessionId === sessionId);
|
|
571
|
+
volume = status.volume;
|
|
572
|
+
}
|
|
573
|
+
catch {
|
|
574
|
+
// unreachable: not this device
|
|
575
|
+
}
|
|
576
|
+
if (app !== undefined && volume !== undefined && !won) {
|
|
577
|
+
won = true;
|
|
578
|
+
resolve({ entry, app, volume });
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
entry.opening -= 1;
|
|
582
|
+
this.cleanupIdle(device.id, entry.client);
|
|
583
|
+
pending -= 1;
|
|
584
|
+
if (pending === 0 && !won)
|
|
585
|
+
resolve(undefined);
|
|
586
|
+
})();
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
/** Connect (or reuse) the device's client, launch the app and register the session. */
|
|
591
|
+
async openSession(device, appId, sender, discard, cancelled) {
|
|
592
|
+
const entry = this.clientFor(device);
|
|
593
|
+
const client = entry.client;
|
|
594
|
+
entry.opening += 1;
|
|
595
|
+
let registered = false;
|
|
596
|
+
try {
|
|
597
|
+
try {
|
|
598
|
+
await entry.ready;
|
|
599
|
+
}
|
|
600
|
+
catch (err) {
|
|
601
|
+
if (err instanceof index_ts_1.CastTimeoutError)
|
|
602
|
+
throw new MappedError("timeout", err.message);
|
|
603
|
+
throw new MappedError("receiver_unavailable", describe(err));
|
|
604
|
+
}
|
|
605
|
+
if (discard.value)
|
|
606
|
+
throw new Discarded();
|
|
607
|
+
if (cancelled.value)
|
|
608
|
+
throw new MappedError("cancel", "cancelled");
|
|
609
|
+
let app;
|
|
610
|
+
try {
|
|
611
|
+
app = await client.launch(appId);
|
|
612
|
+
}
|
|
613
|
+
catch (err) {
|
|
614
|
+
if (err instanceof index_ts_1.CastTimeoutError)
|
|
615
|
+
throw new MappedError("timeout", err.message);
|
|
616
|
+
throw new MappedError("session_error", describe(err));
|
|
617
|
+
}
|
|
618
|
+
if (discard.value) {
|
|
619
|
+
client.closeTransport(app.transportId);
|
|
620
|
+
throw new Discarded();
|
|
621
|
+
}
|
|
622
|
+
if (cancelled.value) {
|
|
623
|
+
client.closeTransport(app.transportId);
|
|
624
|
+
throw new MappedError("cancel", "cancelled");
|
|
625
|
+
}
|
|
626
|
+
client.connectTransport(app.transportId);
|
|
627
|
+
let status;
|
|
628
|
+
try {
|
|
629
|
+
status = await client.getStatus();
|
|
630
|
+
}
|
|
631
|
+
catch (err) {
|
|
632
|
+
client.closeTransport(app.transportId);
|
|
633
|
+
throw new MappedError("session_error", describe(err));
|
|
634
|
+
}
|
|
635
|
+
if (discard.value) {
|
|
636
|
+
client.closeTransport(app.transportId);
|
|
637
|
+
throw new Discarded();
|
|
638
|
+
}
|
|
639
|
+
if (cancelled.value) {
|
|
640
|
+
client.closeTransport(app.transportId);
|
|
641
|
+
throw new MappedError("cancel", "cancelled");
|
|
642
|
+
}
|
|
643
|
+
const rec = {
|
|
644
|
+
device,
|
|
645
|
+
client,
|
|
646
|
+
app,
|
|
647
|
+
origin: sender.origin,
|
|
648
|
+
appId,
|
|
649
|
+
members: new Set([sender.id]),
|
|
650
|
+
parked: new Set(),
|
|
651
|
+
lastVolume: status.volume,
|
|
652
|
+
lastSnapshot: null,
|
|
653
|
+
};
|
|
654
|
+
const snapshot = this.snapshotOf(rec);
|
|
655
|
+
rec.lastSnapshot = snapshot;
|
|
656
|
+
this.sessions.set(app.sessionId, rec);
|
|
657
|
+
registered = true;
|
|
658
|
+
return snapshot;
|
|
659
|
+
}
|
|
660
|
+
finally {
|
|
661
|
+
entry.opening -= 1;
|
|
662
|
+
if (!registered)
|
|
663
|
+
this.cleanupIdle(device.id, client);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
/** Remove a sender from a session; when it was the last one, tear the session down. */
|
|
667
|
+
leave(rec, senderId) {
|
|
668
|
+
rec.members.delete(senderId);
|
|
669
|
+
rec.parked.delete(senderId);
|
|
670
|
+
if (rec.members.size === 0 && rec.parked.size === 0) {
|
|
671
|
+
rec.client.closeTransport(rec.app.transportId);
|
|
672
|
+
this.sessions.delete(rec.app.sessionId);
|
|
673
|
+
this.cleanupIdle(rec.device.id, rec.client);
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
this.notifySession("updated", rec);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
/** Subscribe to a freshly created client's events. */
|
|
680
|
+
bindClient(client, device) {
|
|
681
|
+
client.on("message", (msg) => this.onClientMessage(client, msg));
|
|
682
|
+
client.on("receiverStatus", (status) => this.onClientStatus(client, device, status));
|
|
683
|
+
client.on("transportClosed", (transportId) => this.onTransportClosed(client, transportId));
|
|
684
|
+
client.on("close", () => this.onClientClose(client, device.id));
|
|
685
|
+
}
|
|
686
|
+
onClientMessage(client, msg) {
|
|
687
|
+
if (typeof msg.data !== "string")
|
|
688
|
+
return;
|
|
689
|
+
for (const rec of this.sessions.values()) {
|
|
690
|
+
if (rec.client !== client || rec.app.transportId !== msg.sourceId)
|
|
691
|
+
continue;
|
|
692
|
+
for (const member of rec.members) {
|
|
693
|
+
this.emitTo(member, {
|
|
694
|
+
type: "message",
|
|
695
|
+
sessionId: rec.app.sessionId,
|
|
696
|
+
namespace: msg.namespace,
|
|
697
|
+
message: msg.data,
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
onClientStatus(client, device, status) {
|
|
703
|
+
for (const rec of [...this.sessions.values()]) {
|
|
704
|
+
if (rec.client !== client)
|
|
705
|
+
continue;
|
|
706
|
+
const app = status.applications.find((a) => a.sessionId === rec.app.sessionId);
|
|
707
|
+
if (app === undefined) {
|
|
708
|
+
this.sessions.delete(rec.app.sessionId);
|
|
709
|
+
for (const member of rec.members) {
|
|
710
|
+
if (rec.stoppingBy !== undefined && member === rec.stoppingBy)
|
|
711
|
+
continue;
|
|
712
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "stopped" });
|
|
713
|
+
}
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
rec.app = app;
|
|
717
|
+
rec.lastVolume = status.volume;
|
|
718
|
+
const snapshot = this.snapshotOf(rec);
|
|
719
|
+
if (!sameValue(snapshot, rec.lastSnapshot)) {
|
|
720
|
+
rec.lastSnapshot = snapshot;
|
|
721
|
+
this.notifySession("updated", rec);
|
|
722
|
+
for (const member of rec.members) {
|
|
723
|
+
this.emitTo(member, { type: "sessionUpdated", session: snapshot });
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
this.cleanupIdle(device.id, client);
|
|
728
|
+
}
|
|
729
|
+
onTransportClosed(client, transportId) {
|
|
730
|
+
for (const rec of [...this.sessions.values()]) {
|
|
731
|
+
if (rec.client !== client || rec.app.transportId !== transportId)
|
|
732
|
+
continue;
|
|
733
|
+
this.sessions.delete(rec.app.sessionId);
|
|
734
|
+
for (const member of rec.members) {
|
|
735
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "disconnected" });
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
onClientClose(client, deviceId) {
|
|
740
|
+
const entry = this.clients.get(deviceId);
|
|
741
|
+
if (entry !== undefined && entry.client === client)
|
|
742
|
+
this.clients.delete(deviceId);
|
|
743
|
+
for (const rec of [...this.sessions.values()]) {
|
|
744
|
+
if (rec.client !== client)
|
|
745
|
+
continue;
|
|
746
|
+
this.sessions.delete(rec.app.sessionId);
|
|
747
|
+
for (const member of rec.members) {
|
|
748
|
+
this.emitTo(member, { type: "sessionEnded", sessionId: rec.app.sessionId, status: "disconnected" });
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
/** Re-check availability for every initialized sender; emit only on change. */
|
|
753
|
+
onDevicesChanged() {
|
|
754
|
+
if (this.disposed)
|
|
755
|
+
return;
|
|
756
|
+
const devices = this.options.devices.devices();
|
|
757
|
+
for (const [id, rec] of this.senders) {
|
|
758
|
+
const available = devices.some((d) => deviceMatches(d, rec.capabilities));
|
|
759
|
+
if (available !== rec.lastAvailable) {
|
|
760
|
+
rec.lastAvailable = available;
|
|
761
|
+
this.emitTo(id, { type: "receiverAvailability", available });
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
/** Close the client when no session uses it any more and no openSession is in flight. */
|
|
766
|
+
cleanupIdle(deviceId, client) {
|
|
767
|
+
const entry = this.clients.get(deviceId);
|
|
768
|
+
if (entry !== undefined && entry.client === client && entry.opening > 0)
|
|
769
|
+
return;
|
|
770
|
+
for (const rec of this.sessions.values()) {
|
|
771
|
+
if (rec.client === client)
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
this.dropClient(deviceId, client);
|
|
775
|
+
}
|
|
776
|
+
/** Forget a client and close it; a no-op unless it is still the registered entry for the device. */
|
|
777
|
+
dropClient(deviceId, client) {
|
|
778
|
+
const entry = this.clients.get(deviceId);
|
|
779
|
+
if (entry === undefined || entry.client !== client)
|
|
780
|
+
return;
|
|
781
|
+
this.clients.delete(deviceId);
|
|
782
|
+
client.close();
|
|
783
|
+
}
|
|
784
|
+
// Main-process API (see manager-api.ts). Control goes through the paths the pages use, so pages see the same events.
|
|
785
|
+
/** Every live session, parked ones included. */
|
|
786
|
+
mediaRoutes() {
|
|
787
|
+
return [...this.sessions.values()].map((rec) => this.routeOf(rec));
|
|
788
|
+
}
|
|
789
|
+
/** Be told when a session starts, changes (status, volume, pages) or ends; returns the unsubscribe function. */
|
|
790
|
+
observeSessions(listener) {
|
|
791
|
+
this.sessionListeners.add(listener);
|
|
792
|
+
return () => void this.sessionListeners.delete(listener);
|
|
793
|
+
}
|
|
794
|
+
/** Stop a session on the device for every page, as if a member had called stopSession. */
|
|
795
|
+
async terminateSession(sessionId) {
|
|
796
|
+
const rec = this.sessions.get(sessionId);
|
|
797
|
+
if (rec === undefined)
|
|
798
|
+
return bad("session_error", "unknown session");
|
|
799
|
+
// No page is the caller, so every member is told sessionEnded.
|
|
800
|
+
return this.stopSessionRecord(rec, MAIN_CALLER, false);
|
|
801
|
+
}
|
|
802
|
+
async setSessionVolume(sessionId, volume) {
|
|
803
|
+
const rec = this.sessions.get(sessionId);
|
|
804
|
+
if (rec === undefined)
|
|
805
|
+
return bad("session_error", "unknown session");
|
|
806
|
+
try {
|
|
807
|
+
await rec.client.setVolume(volume);
|
|
808
|
+
}
|
|
809
|
+
catch (err) {
|
|
810
|
+
return bad("session_error", describe(err));
|
|
811
|
+
}
|
|
812
|
+
return good(undefined);
|
|
813
|
+
}
|
|
814
|
+
/** Detach every page from the session and close its transport; the receiver app keeps running. */
|
|
815
|
+
detachSession(sessionId) {
|
|
816
|
+
const rec = this.sessions.get(sessionId);
|
|
817
|
+
if (rec === undefined)
|
|
818
|
+
return bad("session_error", "unknown session");
|
|
819
|
+
for (const member of rec.members) {
|
|
820
|
+
this.emitTo(member, { type: "sessionEnded", sessionId, status: "disconnected" });
|
|
821
|
+
}
|
|
822
|
+
rec.members.clear();
|
|
823
|
+
rec.parked.clear();
|
|
824
|
+
rec.client.closeTransport(rec.app.transportId);
|
|
825
|
+
this.sessions.delete(sessionId);
|
|
826
|
+
this.cleanupIdle(rec.device.id, rec.client);
|
|
827
|
+
return good(undefined);
|
|
828
|
+
}
|
|
829
|
+
/** Tell the route observers something changed (the arguments only fit the ObservedMap callback). */
|
|
830
|
+
notifySession(_kind, _rec) {
|
|
831
|
+
if (this.disposed)
|
|
832
|
+
return;
|
|
833
|
+
for (const listener of [...this.sessionListeners]) {
|
|
834
|
+
try {
|
|
835
|
+
listener();
|
|
836
|
+
}
|
|
837
|
+
catch (err) {
|
|
838
|
+
console.error("electron-cast: a session listener failed", err);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
routeOf(rec) {
|
|
843
|
+
return {
|
|
844
|
+
id: rec.app.sessionId,
|
|
845
|
+
mediaSource: `cast:${rec.appId}`,
|
|
846
|
+
sinkId: rec.device.id,
|
|
847
|
+
description: rec.app.statusText || rec.app.displayName,
|
|
848
|
+
appId: rec.appId,
|
|
849
|
+
displayName: rec.app.displayName,
|
|
850
|
+
origin: rec.origin,
|
|
851
|
+
volume: { level: rec.lastVolume.level ?? null, muted: rec.lastVolume.muted ?? null },
|
|
852
|
+
members: [...rec.members],
|
|
853
|
+
parkedSenders: [...rec.parked],
|
|
854
|
+
parked: rec.members.size === 0,
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
snapshotOf(rec) {
|
|
858
|
+
const app = rec.app;
|
|
859
|
+
const volume = {
|
|
860
|
+
level: rec.lastVolume.level ?? null,
|
|
861
|
+
muted: rec.lastVolume.muted ?? null,
|
|
862
|
+
};
|
|
863
|
+
if (rec.lastVolume.controlType !== undefined)
|
|
864
|
+
volume.controlType = rec.lastVolume.controlType;
|
|
865
|
+
if (rec.lastVolume.stepInterval !== undefined)
|
|
866
|
+
volume.stepInterval = rec.lastVolume.stepInterval;
|
|
867
|
+
return {
|
|
868
|
+
sessionId: app.sessionId,
|
|
869
|
+
appId: app.appId,
|
|
870
|
+
displayName: app.displayName,
|
|
871
|
+
appImages: app.iconUrl ? [{ url: app.iconUrl }] : [],
|
|
872
|
+
statusText: app.statusText ?? null,
|
|
873
|
+
namespaces: app.namespaces,
|
|
874
|
+
transportId: app.transportId,
|
|
875
|
+
receiver: {
|
|
876
|
+
label: rec.device.id,
|
|
877
|
+
friendlyName: rec.device.name,
|
|
878
|
+
capabilities: capabilityNames(rec.device.capabilities),
|
|
879
|
+
volume,
|
|
880
|
+
isActiveInput: null,
|
|
881
|
+
},
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
emitTo(senderId, event) {
|
|
885
|
+
if (this.disposed)
|
|
886
|
+
return;
|
|
887
|
+
this.options.emit(senderId, event);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
exports.CastSessionService = CastSessionService;
|
|
891
|
+
//# sourceMappingURL=session-service.js.map
|