@dimchansky/meshpost 0.6.0 → 0.7.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/dist/blob.js +36 -36
- package/dist/channel.d.ts +4 -0
- package/dist/channel.js +4 -0
- package/dist/combine.d.ts +9 -2
- package/dist/combine.js +35 -33
- package/dist/index.d.ts +2 -1
- package/dist/mqtt.js +59 -109
- package/dist/nostr.js +71 -131
- package/dist/peer-link.js +983 -0
- package/dist/presence.js +74 -0
- package/dist/public.js +16 -23
- package/dist/relay-core.js +219 -143
- package/dist/room-reclaim.js +5 -1
- package/dist/room.js +300 -463
- package/dist/rpc.js +25 -28
- package/dist/signaling.js +2 -11
- package/dist/state.js +22 -31
- package/dist/tabs.js +6 -50
- package/dist/voice-mic.js +496 -0
- package/dist/voice-remote.js +568 -0
- package/dist/voice.d.ts +8 -0
- package/dist/voice.js +71 -816
- package/dist/ws.js +20 -58
- package/docs/adr/0001-room-internals-and-channel-budget.md +31 -0
- package/docs/api.md +6 -4
- package/docs/behavior.md +4 -4
- package/package.json +4 -2
- package/dist/room-hello.js +0 -129
- package/dist/room-ice.js +0 -115
- package/dist/room-media.js +0 -477
- package/dist/voice-helpers.js +0 -100
- package/dist/voice-meter.d.ts +0 -1
- package/dist/voice-meter.js +0 -79
- /package/dist/{room-hello.d.ts → peer-link.d.ts} +0 -0
- /package/dist/{room-ice.d.ts → presence.d.ts} +0 -0
- /package/dist/{room-media.d.ts → voice-mic.d.ts} +0 -0
- /package/dist/{voice-helpers.d.ts → voice-remote.d.ts} +0 -0
package/dist/room-media.js
DELETED
|
@@ -1,477 +0,0 @@
|
|
|
1
|
-
/** @internal */
|
|
2
|
-
export const rtcControlPrefix = 'meshpost/1/rtc:';
|
|
3
|
-
/** @internal */
|
|
4
|
-
export const maxRtcControlBytes = 64 * 1024;
|
|
5
|
-
/** @internal */
|
|
6
|
-
export const maxRemoteStreamsPerPeer = 32;
|
|
7
|
-
const maxRtcQueueDepth = 16;
|
|
8
|
-
const maxRemoteStreamTags = 64;
|
|
9
|
-
const streamTagPattern = /^[a-zA-Z0-9._-]{1,32}$/;
|
|
10
|
-
const emptyStreamMeta = Object.freeze({});
|
|
11
|
-
/** @internal */
|
|
12
|
-
export function buildStreamMeta(tag) {
|
|
13
|
-
return tag !== undefined ? Object.freeze({ tag }) : emptyStreamMeta;
|
|
14
|
-
}
|
|
15
|
-
/** @internal */
|
|
16
|
-
export function parseIncomingStreamTags(raw) {
|
|
17
|
-
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
18
|
-
return null;
|
|
19
|
-
const entries = Object.entries(raw);
|
|
20
|
-
if (entries.length > maxRemoteStreamsPerPeer)
|
|
21
|
-
return null;
|
|
22
|
-
const out = [];
|
|
23
|
-
for (const [id, tag] of entries) {
|
|
24
|
-
if (id.length === 0 ||
|
|
25
|
-
id.length > 128 ||
|
|
26
|
-
id === '__proto__' ||
|
|
27
|
-
id === 'constructor' ||
|
|
28
|
-
id === 'prototype' ||
|
|
29
|
-
typeof tag !== 'string' ||
|
|
30
|
-
!streamTagPattern.test(tag)) {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
out.push([id, tag]);
|
|
34
|
-
}
|
|
35
|
-
return out;
|
|
36
|
-
}
|
|
37
|
-
function parseStreamTarget(toOrOptions, allowTag) {
|
|
38
|
-
if (typeof toOrOptions === 'string')
|
|
39
|
-
return { to: toOrOptions };
|
|
40
|
-
if (toOrOptions === undefined)
|
|
41
|
-
return {};
|
|
42
|
-
if (!toOrOptions || typeof toOrOptions !== 'object' || Array.isArray(toOrOptions)) {
|
|
43
|
-
throw new TypeError(allowTag ? 'Invalid addStream options' : 'Invalid removeStream options');
|
|
44
|
-
}
|
|
45
|
-
if (toOrOptions.to !== undefined && typeof toOrOptions.to !== 'string') {
|
|
46
|
-
throw new TypeError('Invalid target peer ID');
|
|
47
|
-
}
|
|
48
|
-
const tag = allowTag ? toOrOptions.tag : undefined;
|
|
49
|
-
if (tag !== undefined && (typeof tag !== 'string' || !streamTagPattern.test(tag))) {
|
|
50
|
-
throw new TypeError('Invalid stream tag');
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
...(toOrOptions.to !== undefined ? { to: toOrOptions.to } : {}),
|
|
54
|
-
...(tag !== undefined ? { tag } : {})
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
/** @internal */
|
|
58
|
-
export function createRoomMedia(params) {
|
|
59
|
-
const { self, isStopped, peers, onError, onStreamEnd } = params;
|
|
60
|
-
const broadcastStreams = new Set();
|
|
61
|
-
const peerStreams = new Map();
|
|
62
|
-
const localStreamTags = new Map();
|
|
63
|
-
const desiredStreamsFor = (peerId) => {
|
|
64
|
-
const set = new Set(broadcastStreams);
|
|
65
|
-
const targeted = peerStreams.get(peerId);
|
|
66
|
-
if (targeted) {
|
|
67
|
-
for (const s of targeted)
|
|
68
|
-
set.add(s);
|
|
69
|
-
}
|
|
70
|
-
return set;
|
|
71
|
-
};
|
|
72
|
-
const isStreamRegistered = (stream) => {
|
|
73
|
-
if (broadcastStreams.has(stream))
|
|
74
|
-
return true;
|
|
75
|
-
for (const set of peerStreams.values()) {
|
|
76
|
-
if (set.has(stream))
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
return false;
|
|
80
|
-
};
|
|
81
|
-
const collectLinkStreamTags = (link) => {
|
|
82
|
-
let out;
|
|
83
|
-
for (const stream of link.senders.keys()) {
|
|
84
|
-
const tag = localStreamTags.get(stream);
|
|
85
|
-
if (tag !== undefined) {
|
|
86
|
-
out ??= {};
|
|
87
|
-
out[stream.id] = tag;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return out;
|
|
91
|
-
};
|
|
92
|
-
const mergeRemoteStreamTags = (link, entries) => {
|
|
93
|
-
for (const [id, tag] of entries) {
|
|
94
|
-
if (link.remoteStreamTags.has(id)) {
|
|
95
|
-
link.remoteStreamTags.delete(id);
|
|
96
|
-
link.remoteStreamTags.set(id, tag);
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
if (link.remoteStreamTags.size >= maxRemoteStreamTags) {
|
|
100
|
-
let evicted = false;
|
|
101
|
-
for (const existingId of link.remoteStreamTags.keys()) {
|
|
102
|
-
if (!link.remoteStreams.has(existingId)) {
|
|
103
|
-
link.remoteStreamTags.delete(existingId);
|
|
104
|
-
evicted = true;
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (!evicted)
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
link.remoteStreamTags.set(id, tag);
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
const remoteMetaFor = (link, streamId) => buildStreamMeta(link.remoteStreamTags.get(streamId));
|
|
115
|
-
const sendRtcControl = (link, payload) => {
|
|
116
|
-
const reliable = link.channels.reliable;
|
|
117
|
-
if (reliable.readyState !== 'open')
|
|
118
|
-
return false;
|
|
119
|
-
try {
|
|
120
|
-
reliable.send(rtcControlPrefix + JSON.stringify(payload));
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
const attachStreamToLink = (link, stream) => {
|
|
128
|
-
if (link.senders.has(stream))
|
|
129
|
-
return false;
|
|
130
|
-
const tracks = stream.getTracks();
|
|
131
|
-
if (tracks.length === 0)
|
|
132
|
-
return false;
|
|
133
|
-
const added = [];
|
|
134
|
-
for (const track of tracks) {
|
|
135
|
-
added.push(link.pc.addTrack(track, stream));
|
|
136
|
-
}
|
|
137
|
-
link.senders.set(stream, added);
|
|
138
|
-
link.pendingStreamEnds.delete(stream.id);
|
|
139
|
-
return true;
|
|
140
|
-
};
|
|
141
|
-
const detachStreamFromLink = (link, stream) => {
|
|
142
|
-
const added = link.senders.get(stream);
|
|
143
|
-
if (!added)
|
|
144
|
-
return false;
|
|
145
|
-
link.senders.delete(stream);
|
|
146
|
-
for (const sender of added) {
|
|
147
|
-
try {
|
|
148
|
-
link.pc.removeTrack(sender);
|
|
149
|
-
}
|
|
150
|
-
catch {
|
|
151
|
-
// Ignore if connection is closing.
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
link.pendingStreamEnds.add(stream.id);
|
|
155
|
-
return true;
|
|
156
|
-
};
|
|
157
|
-
const enqueueRenegotiate = (peer, link) => {
|
|
158
|
-
link.renegotiateQueued = true;
|
|
159
|
-
link.rtcQueue = link.rtcQueue.then(async () => {
|
|
160
|
-
if (isStopped() ||
|
|
161
|
-
peer.link !== link ||
|
|
162
|
-
!link.ready ||
|
|
163
|
-
!link.renegotiateQueued ||
|
|
164
|
-
link.channels.reliable.readyState !== 'open')
|
|
165
|
-
return;
|
|
166
|
-
if (link.pc.signalingState !== 'stable')
|
|
167
|
-
return;
|
|
168
|
-
link.renegotiateQueued = false;
|
|
169
|
-
try {
|
|
170
|
-
const offer = await link.pc.createOffer();
|
|
171
|
-
if (isStopped() || peer.link !== link)
|
|
172
|
-
return;
|
|
173
|
-
await link.pc.setLocalDescription(offer);
|
|
174
|
-
if (isStopped() || peer.link !== link || !link.pc.localDescription)
|
|
175
|
-
return;
|
|
176
|
-
const tags = collectLinkStreamTags(link);
|
|
177
|
-
if (sendRtcControl(link, {
|
|
178
|
-
kind: 'offer',
|
|
179
|
-
sdp: link.pc.localDescription.sdp,
|
|
180
|
-
...(tags ? { tags } : {})
|
|
181
|
-
})) {
|
|
182
|
-
for (const id of link.pendingStreamEnds) {
|
|
183
|
-
sendRtcControl(link, { kind: 'stream-end', id });
|
|
184
|
-
}
|
|
185
|
-
link.pendingStreamEnds.clear();
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
catch {
|
|
189
|
-
if (!isStopped() && peer.link === link) {
|
|
190
|
-
onError({
|
|
191
|
-
code: 'media-error',
|
|
192
|
-
peerId: peer.id,
|
|
193
|
-
message: 'Failed to negotiate media tracks'
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
};
|
|
199
|
-
const handleRtcControl = (peer, link, raw) => {
|
|
200
|
-
if (raw.length > maxRtcControlBytes || link.rtcPending >= maxRtcQueueDepth)
|
|
201
|
-
return;
|
|
202
|
-
let msg;
|
|
203
|
-
try {
|
|
204
|
-
const parsed = JSON.parse(raw.slice(rtcControlPrefix.length));
|
|
205
|
-
msg = parsed && typeof parsed === 'object' ? parsed : null;
|
|
206
|
-
}
|
|
207
|
-
catch {
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
if (!msg || typeof msg.kind !== 'string')
|
|
211
|
-
return;
|
|
212
|
-
if (msg.kind === 'stream-end' && typeof msg.id === 'string') {
|
|
213
|
-
const streamId = msg.id;
|
|
214
|
-
link.rtcPending++;
|
|
215
|
-
link.rtcQueue = link.rtcQueue.then(() => {
|
|
216
|
-
link.rtcPending--;
|
|
217
|
-
if (isStopped() || peer.link !== link || !link.ready)
|
|
218
|
-
return;
|
|
219
|
-
const stream = link.remoteStreams.get(streamId);
|
|
220
|
-
const meta = remoteMetaFor(link, streamId);
|
|
221
|
-
link.remoteStreamTags.delete(streamId);
|
|
222
|
-
if (stream) {
|
|
223
|
-
link.remoteStreams.delete(streamId);
|
|
224
|
-
onStreamEnd(peer.id, stream, meta);
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
if ((msg.kind !== 'offer' && msg.kind !== 'answer') || typeof msg.sdp !== 'string')
|
|
230
|
-
return;
|
|
231
|
-
let incomingTags = null;
|
|
232
|
-
if (msg.tags !== undefined) {
|
|
233
|
-
incomingTags = parseIncomingStreamTags(msg.tags);
|
|
234
|
-
if (!incomingTags)
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
const kind = msg.kind;
|
|
238
|
-
const sdp = msg.sdp;
|
|
239
|
-
const polite = self > peer.id;
|
|
240
|
-
link.rtcPending++;
|
|
241
|
-
link.rtcQueue = link.rtcQueue.then(async () => {
|
|
242
|
-
try {
|
|
243
|
-
if (isStopped() || peer.link !== link || !link.ready)
|
|
244
|
-
return;
|
|
245
|
-
if (kind === 'offer') {
|
|
246
|
-
const collision = link.pc.signalingState !== 'stable';
|
|
247
|
-
if (!polite && collision)
|
|
248
|
-
return;
|
|
249
|
-
try {
|
|
250
|
-
if (polite && collision) {
|
|
251
|
-
await link.pc.setLocalDescription({ type: 'rollback' });
|
|
252
|
-
link.renegotiateQueued = true;
|
|
253
|
-
}
|
|
254
|
-
if (incomingTags)
|
|
255
|
-
mergeRemoteStreamTags(link, incomingTags);
|
|
256
|
-
await link.pc.setRemoteDescription({ type: 'offer', sdp });
|
|
257
|
-
if (isStopped() || peer.link !== link)
|
|
258
|
-
return;
|
|
259
|
-
const answer = await link.pc.createAnswer();
|
|
260
|
-
if (isStopped() || peer.link !== link)
|
|
261
|
-
return;
|
|
262
|
-
await link.pc.setLocalDescription(answer);
|
|
263
|
-
if (isStopped() || peer.link !== link || !link.pc.localDescription)
|
|
264
|
-
return;
|
|
265
|
-
const answerTags = collectLinkStreamTags(link);
|
|
266
|
-
sendRtcControl(link, {
|
|
267
|
-
kind: 'answer',
|
|
268
|
-
sdp: link.pc.localDescription.sdp,
|
|
269
|
-
...(answerTags ? { tags: answerTags } : {})
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
catch {
|
|
273
|
-
if (!isStopped() && peer.link === link) {
|
|
274
|
-
onError({
|
|
275
|
-
code: 'media-error',
|
|
276
|
-
peerId: peer.id,
|
|
277
|
-
message: 'Failed to apply media offer'
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
if (!isStopped() && peer.link === link && link.renegotiateQueued) {
|
|
282
|
-
enqueueRenegotiate(peer, link);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
if (link.pc.signalingState !== 'have-local-offer')
|
|
287
|
-
return;
|
|
288
|
-
try {
|
|
289
|
-
if (incomingTags)
|
|
290
|
-
mergeRemoteStreamTags(link, incomingTags);
|
|
291
|
-
await link.pc.setRemoteDescription({ type: 'answer', sdp });
|
|
292
|
-
}
|
|
293
|
-
catch {
|
|
294
|
-
if (!isStopped() && peer.link === link) {
|
|
295
|
-
onError({
|
|
296
|
-
code: 'media-error',
|
|
297
|
-
peerId: peer.id,
|
|
298
|
-
message: 'Failed to apply media answer'
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
if (!isStopped() && peer.link === link && link.renegotiateQueued) {
|
|
303
|
-
enqueueRenegotiate(peer, link);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
finally {
|
|
308
|
-
link.rtcPending--;
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
};
|
|
312
|
-
return {
|
|
313
|
-
desiredStreamsFor,
|
|
314
|
-
collectLinkStreamTags,
|
|
315
|
-
mergeRemoteStreamTags,
|
|
316
|
-
remoteMetaFor,
|
|
317
|
-
attachStreamToLink,
|
|
318
|
-
handleRtcControl,
|
|
319
|
-
reconcileLinkStreams(peer, link) {
|
|
320
|
-
const desired = desiredStreamsFor(peer.id);
|
|
321
|
-
let needsRenegotiate = false;
|
|
322
|
-
for (const stream of desired) {
|
|
323
|
-
if (attachStreamToLink(link, stream))
|
|
324
|
-
needsRenegotiate = true;
|
|
325
|
-
}
|
|
326
|
-
for (const stream of [...link.senders.keys()]) {
|
|
327
|
-
if (!desired.has(stream) && detachStreamFromLink(link, stream)) {
|
|
328
|
-
needsRenegotiate = true;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
if (needsRenegotiate)
|
|
332
|
-
enqueueRenegotiate(peer, link);
|
|
333
|
-
},
|
|
334
|
-
clearLinkMedia(link) {
|
|
335
|
-
link.remoteStreams.clear();
|
|
336
|
-
link.remoteStreamTags.clear();
|
|
337
|
-
link.senders.clear();
|
|
338
|
-
link.pendingStreamEnds.clear();
|
|
339
|
-
},
|
|
340
|
-
addStream(stream, toOrOptions) {
|
|
341
|
-
if (isStopped())
|
|
342
|
-
throw new Error('Room is closed');
|
|
343
|
-
if (!stream || typeof stream.getTracks !== 'function')
|
|
344
|
-
throw new TypeError('Expected a MediaStream');
|
|
345
|
-
const { to, tag } = parseStreamTarget(toOrOptions, true);
|
|
346
|
-
if (tag !== undefined)
|
|
347
|
-
localStreamTags.set(stream, tag);
|
|
348
|
-
if (to === undefined) {
|
|
349
|
-
broadcastStreams.add(stream);
|
|
350
|
-
for (const peer of peers.values()) {
|
|
351
|
-
const link = peer.link;
|
|
352
|
-
if (link?.ready && attachStreamToLink(link, stream)) {
|
|
353
|
-
enqueueRenegotiate(peer, link);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
if (to === self) {
|
|
359
|
-
if (!isStreamRegistered(stream))
|
|
360
|
-
localStreamTags.delete(stream);
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
let set = peerStreams.get(to);
|
|
364
|
-
if (!set) {
|
|
365
|
-
set = new Set();
|
|
366
|
-
peerStreams.set(to, set);
|
|
367
|
-
}
|
|
368
|
-
set.add(stream);
|
|
369
|
-
const peer = peers.get(to);
|
|
370
|
-
const link = peer?.link;
|
|
371
|
-
if (peer && link?.ready && attachStreamToLink(link, stream)) {
|
|
372
|
-
enqueueRenegotiate(peer, link);
|
|
373
|
-
}
|
|
374
|
-
},
|
|
375
|
-
removeStream(stream, toOrOptions) {
|
|
376
|
-
const { to } = parseStreamTarget(toOrOptions, false);
|
|
377
|
-
if (isStopped() || !stream)
|
|
378
|
-
return;
|
|
379
|
-
if (to === undefined) {
|
|
380
|
-
if (!broadcastStreams.delete(stream))
|
|
381
|
-
return;
|
|
382
|
-
if (!isStreamRegistered(stream))
|
|
383
|
-
localStreamTags.delete(stream);
|
|
384
|
-
for (const peer of peers.values()) {
|
|
385
|
-
if (peerStreams.get(peer.id)?.has(stream))
|
|
386
|
-
continue;
|
|
387
|
-
const link = peer.link;
|
|
388
|
-
if (link?.ready && detachStreamFromLink(link, stream)) {
|
|
389
|
-
enqueueRenegotiate(peer, link);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
const set = peerStreams.get(to);
|
|
395
|
-
if (!set?.delete(stream))
|
|
396
|
-
return;
|
|
397
|
-
if (set.size === 0)
|
|
398
|
-
peerStreams.delete(to);
|
|
399
|
-
if (!isStreamRegistered(stream))
|
|
400
|
-
localStreamTags.delete(stream);
|
|
401
|
-
if (broadcastStreams.has(stream))
|
|
402
|
-
return;
|
|
403
|
-
const peer = peers.get(to);
|
|
404
|
-
const link = peer?.link;
|
|
405
|
-
if (peer && link?.ready && detachStreamFromLink(link, stream)) {
|
|
406
|
-
enqueueRenegotiate(peer, link);
|
|
407
|
-
}
|
|
408
|
-
},
|
|
409
|
-
async replaceStreamTrack(stream, oldTrack, newTrack) {
|
|
410
|
-
if (isStopped())
|
|
411
|
-
throw new Error('Room is closed');
|
|
412
|
-
if (!stream || typeof stream.getTracks !== 'function')
|
|
413
|
-
throw new TypeError('Expected a MediaStream');
|
|
414
|
-
if (!oldTrack ||
|
|
415
|
-
typeof oldTrack.kind !== 'string' ||
|
|
416
|
-
!newTrack ||
|
|
417
|
-
typeof newTrack.kind !== 'string' ||
|
|
418
|
-
oldTrack.kind !== newTrack.kind) {
|
|
419
|
-
throw new TypeError('Expected matching-kind MediaStreamTrack instances');
|
|
420
|
-
}
|
|
421
|
-
if (!isStreamRegistered(stream)) {
|
|
422
|
-
throw new Error('Stream is not attached to this room');
|
|
423
|
-
}
|
|
424
|
-
if (oldTrack === newTrack)
|
|
425
|
-
return;
|
|
426
|
-
if (stream.getTracks().includes(oldTrack))
|
|
427
|
-
stream.removeTrack(oldTrack);
|
|
428
|
-
if (!stream.getTracks().includes(newTrack))
|
|
429
|
-
stream.addTrack(newTrack);
|
|
430
|
-
const ops = [];
|
|
431
|
-
for (const peer of peers.values()) {
|
|
432
|
-
const link = peer.link;
|
|
433
|
-
const senderList = link?.senders.get(stream);
|
|
434
|
-
if (!peer || !link || !senderList)
|
|
435
|
-
continue;
|
|
436
|
-
let targetSender = senderList.find((s) => s.track === oldTrack);
|
|
437
|
-
if (!targetSender) {
|
|
438
|
-
const sameKind = senderList.filter((s) => s.track?.kind === newTrack.kind);
|
|
439
|
-
if (sameKind.length === 1)
|
|
440
|
-
targetSender = sameKind[0];
|
|
441
|
-
}
|
|
442
|
-
if (!targetSender)
|
|
443
|
-
continue;
|
|
444
|
-
ops.push(targetSender.replaceTrack(newTrack).catch(() => {
|
|
445
|
-
if (!isStopped() && peer.link === link && link.ready) {
|
|
446
|
-
onError({
|
|
447
|
-
code: 'media-error',
|
|
448
|
-
peerId: peer.id,
|
|
449
|
-
message: 'Failed to replace media track'
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
}));
|
|
453
|
-
}
|
|
454
|
-
if (ops.length > 0)
|
|
455
|
-
await Promise.all(ops);
|
|
456
|
-
},
|
|
457
|
-
getRemoteStreams() {
|
|
458
|
-
const out = [];
|
|
459
|
-
if (isStopped())
|
|
460
|
-
return out;
|
|
461
|
-
for (const peer of peers.values()) {
|
|
462
|
-
const link = peer.link;
|
|
463
|
-
if (!link?.ready)
|
|
464
|
-
continue;
|
|
465
|
-
for (const stream of link.remoteStreams.values()) {
|
|
466
|
-
out.push([peer.id, stream, remoteMetaFor(link, stream.id)]);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return out;
|
|
470
|
-
},
|
|
471
|
-
clearAll() {
|
|
472
|
-
broadcastStreams.clear();
|
|
473
|
-
peerStreams.clear();
|
|
474
|
-
localStreamTags.clear();
|
|
475
|
-
}
|
|
476
|
-
};
|
|
477
|
-
}
|
package/dist/voice-helpers.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
/** @internal */
|
|
2
|
-
export const defaultAudioConstraints = Object.freeze({
|
|
3
|
-
echoCancellation: true,
|
|
4
|
-
noiseSuppression: true,
|
|
5
|
-
autoGainControl: true
|
|
6
|
-
});
|
|
7
|
-
/** @internal */
|
|
8
|
-
export function getIosAudioSession() {
|
|
9
|
-
if (typeof navigator === 'undefined')
|
|
10
|
-
return null;
|
|
11
|
-
const session = navigator.audioSession;
|
|
12
|
-
return session && typeof session === 'object' && typeof session.type === 'string' ? session : null;
|
|
13
|
-
}
|
|
14
|
-
/** @internal */
|
|
15
|
-
export function classifyMicError(err) {
|
|
16
|
-
const name = err && typeof err === 'object' && 'name' in err ? String(err.name) : '';
|
|
17
|
-
if (name === 'NotAllowedError' || name === 'PermissionDeniedError') {
|
|
18
|
-
return Object.freeze({
|
|
19
|
-
code: 'permission-denied',
|
|
20
|
-
message: 'Microphone permission denied'
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
if (name === 'NotFoundError' ||
|
|
24
|
-
name === 'DevicesNotFoundError' ||
|
|
25
|
-
name === 'OverconstrainedError' ||
|
|
26
|
-
name === 'ConstraintNotSatisfiedError') {
|
|
27
|
-
return Object.freeze({
|
|
28
|
-
code: 'not-found',
|
|
29
|
-
message: 'No matching microphone device found'
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
if (name === 'NotReadableError' || name === 'TrackStartError') {
|
|
33
|
-
return Object.freeze({
|
|
34
|
-
code: 'in-use',
|
|
35
|
-
message: 'Microphone is already in use by another application'
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
if (name === 'SecurityError') {
|
|
39
|
-
return Object.freeze({
|
|
40
|
-
code: 'insecure-context',
|
|
41
|
-
message: 'Microphone access requires HTTPS or localhost'
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
return Object.freeze({
|
|
45
|
-
code: 'unknown',
|
|
46
|
-
message: 'Could not access microphone'
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
/** @internal */
|
|
50
|
-
export function normalizeDeviceIdArg(deviceId) {
|
|
51
|
-
if (deviceId === undefined || deviceId === null || deviceId === '')
|
|
52
|
-
return null;
|
|
53
|
-
if (typeof deviceId !== 'string' || deviceId.trim().length === 0) {
|
|
54
|
-
throw new TypeError('Expected deviceId to be a non-empty string, null, or undefined');
|
|
55
|
-
}
|
|
56
|
-
return deviceId;
|
|
57
|
-
}
|
|
58
|
-
/** @internal */
|
|
59
|
-
export function buildAudioConstraints(baseConstraints, deviceOverride, exact) {
|
|
60
|
-
if (deviceOverride !== null) {
|
|
61
|
-
const base = typeof baseConstraints === 'object' ? { ...baseConstraints } : { ...defaultAudioConstraints };
|
|
62
|
-
base.deviceId = exact ? { exact: deviceOverride } : { ideal: deviceOverride };
|
|
63
|
-
return base;
|
|
64
|
-
}
|
|
65
|
-
if (baseConstraints === true)
|
|
66
|
-
return true;
|
|
67
|
-
const copy = { ...baseConstraints };
|
|
68
|
-
delete copy.deviceId;
|
|
69
|
-
return copy;
|
|
70
|
-
}
|
|
71
|
-
/** @internal */
|
|
72
|
-
export function applyAudioVolume(audio, volume) {
|
|
73
|
-
try {
|
|
74
|
-
audio.volume = volume;
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
// Ignore read-only volume property on certain iOS WebKit builds.
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/** @internal */
|
|
81
|
-
export function validatePeerIdArg(peerId) {
|
|
82
|
-
if (typeof peerId !== 'string' || peerId.length === 0 || peerId.length > 128) {
|
|
83
|
-
throw new TypeError('Expected a non-empty peerId string');
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/** @internal */
|
|
87
|
-
export function evictPeerPref(collection, activePeers, maxSize) {
|
|
88
|
-
if (collection.size < maxSize)
|
|
89
|
-
return;
|
|
90
|
-
let victim;
|
|
91
|
-
for (const k of collection.keys()) {
|
|
92
|
-
if (!activePeers.has(k)) {
|
|
93
|
-
victim = k;
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
victim ??= collection.keys().next().value;
|
|
98
|
-
if (victim !== undefined)
|
|
99
|
-
collection.delete(victim);
|
|
100
|
-
}
|
package/dist/voice-meter.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/voice-meter.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/** @internal */
|
|
2
|
-
export function createVoiceMeter(params) {
|
|
3
|
-
const { enabled, threshold, isClosed, onContextPaused, onContextRunning } = params;
|
|
4
|
-
let audioCtx = null;
|
|
5
|
-
let silentSink = null;
|
|
6
|
-
const isPaused = () => audioCtx !== null && audioCtx.state !== 'running' && audioCtx.state !== 'closed';
|
|
7
|
-
return {
|
|
8
|
-
isPaused,
|
|
9
|
-
resume(onResumed) {
|
|
10
|
-
if (!isPaused() || !audioCtx)
|
|
11
|
-
return;
|
|
12
|
-
void audioCtx
|
|
13
|
-
.resume()
|
|
14
|
-
.then(() => onResumed?.())
|
|
15
|
-
.catch(() => { });
|
|
16
|
-
},
|
|
17
|
-
createAnalyser(stream) {
|
|
18
|
-
if (!enabled || typeof AudioContext !== 'function')
|
|
19
|
-
return {};
|
|
20
|
-
try {
|
|
21
|
-
if (!audioCtx) {
|
|
22
|
-
const ctx = new AudioContext();
|
|
23
|
-
ctx.onstatechange = () => {
|
|
24
|
-
if (isClosed() || audioCtx !== ctx)
|
|
25
|
-
return;
|
|
26
|
-
if (isPaused())
|
|
27
|
-
onContextPaused();
|
|
28
|
-
else
|
|
29
|
-
onContextRunning();
|
|
30
|
-
};
|
|
31
|
-
const gain = ctx.createGain();
|
|
32
|
-
gain.gain.value = 0;
|
|
33
|
-
gain.connect(ctx.destination);
|
|
34
|
-
audioCtx = ctx;
|
|
35
|
-
silentSink = gain;
|
|
36
|
-
}
|
|
37
|
-
if (isPaused()) {
|
|
38
|
-
void audioCtx.resume().catch(() => { });
|
|
39
|
-
onContextPaused();
|
|
40
|
-
}
|
|
41
|
-
const source = audioCtx.createMediaStreamSource(stream);
|
|
42
|
-
const analyser = audioCtx.createAnalyser();
|
|
43
|
-
analyser.fftSize = 256;
|
|
44
|
-
source.connect(analyser);
|
|
45
|
-
if (silentSink)
|
|
46
|
-
analyser.connect(silentSink);
|
|
47
|
-
return { source, analyser, data: new Uint8Array(analyser.fftSize) };
|
|
48
|
-
}
|
|
49
|
-
catch {
|
|
50
|
-
return {};
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
isActive(entry) {
|
|
54
|
-
if (!entry?.analyser || !entry.data)
|
|
55
|
-
return false;
|
|
56
|
-
entry.analyser.getByteTimeDomainData(entry.data);
|
|
57
|
-
let sum = 0;
|
|
58
|
-
for (let i = 0; i < entry.data.length; i++) {
|
|
59
|
-
const d = (entry.data[i] - 128) / 128;
|
|
60
|
-
sum += d * d;
|
|
61
|
-
}
|
|
62
|
-
return Math.sqrt(sum / entry.data.length) > threshold;
|
|
63
|
-
},
|
|
64
|
-
disconnect(entry) {
|
|
65
|
-
entry?.source?.disconnect();
|
|
66
|
-
entry?.analyser?.disconnect();
|
|
67
|
-
},
|
|
68
|
-
closeContext() {
|
|
69
|
-
if (!audioCtx)
|
|
70
|
-
return;
|
|
71
|
-
const ctx = audioCtx;
|
|
72
|
-
audioCtx = null;
|
|
73
|
-
silentSink?.disconnect();
|
|
74
|
-
silentSink = null;
|
|
75
|
-
ctx.onstatechange = null;
|
|
76
|
-
void ctx.close().catch(() => { });
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|