@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
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
const maxRemoteStreamsPerPeer = 4;
|
|
2
|
+
const maxPeerAudioPrefs = 128;
|
|
3
|
+
const gestureEvents = ['pointerdown', 'pointerup', 'touchend', 'click', 'keydown'];
|
|
4
|
+
const captureOpts = { capture: true };
|
|
5
|
+
/** @internal */
|
|
6
|
+
export const defaultRemoteAudioAdapter = {
|
|
7
|
+
createAudioElement() {
|
|
8
|
+
if (typeof document === 'undefined' || !document.body)
|
|
9
|
+
return null;
|
|
10
|
+
const audio = document.createElement('audio');
|
|
11
|
+
document.body.append(audio);
|
|
12
|
+
return audio;
|
|
13
|
+
},
|
|
14
|
+
createAudioContext() {
|
|
15
|
+
if (typeof AudioContext !== 'function')
|
|
16
|
+
return null;
|
|
17
|
+
return new AudioContext();
|
|
18
|
+
},
|
|
19
|
+
attachGestureUnlock(handler) {
|
|
20
|
+
if (typeof window === 'undefined')
|
|
21
|
+
return () => { };
|
|
22
|
+
for (const type of gestureEvents) {
|
|
23
|
+
window.addEventListener(type, handler, captureOpts);
|
|
24
|
+
}
|
|
25
|
+
return () => {
|
|
26
|
+
for (const type of gestureEvents) {
|
|
27
|
+
window.removeEventListener(type, handler, captureOpts);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
attachVisibility(handler) {
|
|
32
|
+
if (typeof document === 'undefined')
|
|
33
|
+
return () => { };
|
|
34
|
+
const onVis = () => {
|
|
35
|
+
if (document.visibilityState === 'visible')
|
|
36
|
+
handler();
|
|
37
|
+
};
|
|
38
|
+
document.addEventListener('visibilitychange', onVis);
|
|
39
|
+
return () => document.removeEventListener('visibilitychange', onVis);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
function applyAudioVolume(audio, volume) {
|
|
43
|
+
try {
|
|
44
|
+
audio.volume = volume;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Ignore read-only volume property on certain iOS WebKit builds.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function validatePeerIdArg(peerId) {
|
|
51
|
+
if (typeof peerId !== 'string' || peerId.length === 0 || peerId.length > 128) {
|
|
52
|
+
throw new TypeError('Expected a non-empty peerId string');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function evictPeerPref(collection, activePeers) {
|
|
56
|
+
if (collection.size < maxPeerAudioPrefs)
|
|
57
|
+
return;
|
|
58
|
+
let victim;
|
|
59
|
+
for (const k of collection.keys()) {
|
|
60
|
+
if (!activePeers.has(k)) {
|
|
61
|
+
victim = k;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
victim ??= collection.keys().next().value;
|
|
66
|
+
if (victim !== undefined)
|
|
67
|
+
collection.delete(victim);
|
|
68
|
+
}
|
|
69
|
+
/** @internal */
|
|
70
|
+
export function createRemoteAudio(config) {
|
|
71
|
+
const { autoUnlock, speakingDetection, speakingThreshold, speakingIntervalMs, streamTag, strictStreamTag, onChange } = config;
|
|
72
|
+
const adapter = config.adapter ?? defaultRemoteAudioAdapter;
|
|
73
|
+
let volume = config.initialVolume;
|
|
74
|
+
let deafened = config.initialDeafened;
|
|
75
|
+
let autoplayBlocked = false;
|
|
76
|
+
let selfSpeaking = false;
|
|
77
|
+
let localLive = false;
|
|
78
|
+
let closed = false;
|
|
79
|
+
let audioCtx = null;
|
|
80
|
+
let silentSink = null;
|
|
81
|
+
let localMeter = null;
|
|
82
|
+
let localStreamRef = null;
|
|
83
|
+
let meterTimer;
|
|
84
|
+
let detachGestures = null;
|
|
85
|
+
let detachVisibility = null;
|
|
86
|
+
const remotePeers = new Map();
|
|
87
|
+
const speakingPeers = new Set();
|
|
88
|
+
const peerVolumes = new Map();
|
|
89
|
+
const mutedPeers = new Set();
|
|
90
|
+
const isContextPaused = () => audioCtx !== null && audioCtx.state !== 'running' && audioCtx.state !== 'closed';
|
|
91
|
+
const effectiveVolumeFor = (peerId) => Math.max(0, Math.min(1, volume * (peerVolumes.get(peerId) ?? 1)));
|
|
92
|
+
const effectiveMutedFor = (peerId) => deafened || mutedPeers.has(peerId);
|
|
93
|
+
function attachGestureListeners() {
|
|
94
|
+
if (!autoUnlock || closed || detachGestures !== null)
|
|
95
|
+
return;
|
|
96
|
+
detachGestures = adapter.attachGestureUnlock(unlockAudio);
|
|
97
|
+
}
|
|
98
|
+
function detachGestureListeners() {
|
|
99
|
+
if (!detachGestures)
|
|
100
|
+
return;
|
|
101
|
+
const fn = detachGestures;
|
|
102
|
+
detachGestures = null;
|
|
103
|
+
fn();
|
|
104
|
+
}
|
|
105
|
+
function hasBlockedRemoteAudio() {
|
|
106
|
+
for (const streams of remotePeers.values()) {
|
|
107
|
+
for (const entry of streams.values()) {
|
|
108
|
+
if (entry.blocked || entry.audio.paused)
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
function markAutoplayBlocked() {
|
|
115
|
+
if (closed)
|
|
116
|
+
return;
|
|
117
|
+
const changed = !autoplayBlocked;
|
|
118
|
+
autoplayBlocked = true;
|
|
119
|
+
attachGestureListeners();
|
|
120
|
+
if (changed)
|
|
121
|
+
onChange();
|
|
122
|
+
}
|
|
123
|
+
function reconcileUnlockState() {
|
|
124
|
+
if (closed)
|
|
125
|
+
return;
|
|
126
|
+
let changed = false;
|
|
127
|
+
if (autoplayBlocked && !hasBlockedRemoteAudio()) {
|
|
128
|
+
autoplayBlocked = false;
|
|
129
|
+
changed = true;
|
|
130
|
+
}
|
|
131
|
+
if (!autoplayBlocked && !isContextPaused()) {
|
|
132
|
+
detachGestureListeners();
|
|
133
|
+
}
|
|
134
|
+
if (changed)
|
|
135
|
+
onChange();
|
|
136
|
+
}
|
|
137
|
+
function disconnectMeter(node) {
|
|
138
|
+
if (!node)
|
|
139
|
+
return;
|
|
140
|
+
node.source.disconnect();
|
|
141
|
+
node.analyser.disconnect();
|
|
142
|
+
}
|
|
143
|
+
function createMeterNode(stream) {
|
|
144
|
+
if (!speakingDetection)
|
|
145
|
+
return null;
|
|
146
|
+
try {
|
|
147
|
+
if (!audioCtx) {
|
|
148
|
+
const ctx = adapter.createAudioContext();
|
|
149
|
+
if (!ctx)
|
|
150
|
+
return null;
|
|
151
|
+
ctx.onstatechange = () => {
|
|
152
|
+
if (closed || audioCtx !== ctx)
|
|
153
|
+
return;
|
|
154
|
+
if (isContextPaused())
|
|
155
|
+
attachGestureListeners();
|
|
156
|
+
else
|
|
157
|
+
reconcileUnlockState();
|
|
158
|
+
};
|
|
159
|
+
const gain = ctx.createGain();
|
|
160
|
+
gain.gain.value = 0;
|
|
161
|
+
gain.connect(ctx.destination);
|
|
162
|
+
audioCtx = ctx;
|
|
163
|
+
silentSink = gain;
|
|
164
|
+
}
|
|
165
|
+
if (isContextPaused()) {
|
|
166
|
+
void audioCtx.resume().catch(() => { });
|
|
167
|
+
attachGestureListeners();
|
|
168
|
+
}
|
|
169
|
+
const source = audioCtx.createMediaStreamSource(stream);
|
|
170
|
+
const analyser = audioCtx.createAnalyser();
|
|
171
|
+
analyser.fftSize = 256;
|
|
172
|
+
source.connect(analyser);
|
|
173
|
+
if (silentSink)
|
|
174
|
+
analyser.connect(silentSink);
|
|
175
|
+
return { source, analyser, data: new Uint8Array(analyser.fftSize) };
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function isMeterActive(node) {
|
|
182
|
+
if (!node)
|
|
183
|
+
return false;
|
|
184
|
+
node.analyser.getByteTimeDomainData(node.data);
|
|
185
|
+
let sum = 0;
|
|
186
|
+
for (let i = 0; i < node.data.length; i++) {
|
|
187
|
+
const d = (node.data[i] - 128) / 128;
|
|
188
|
+
sum += d * d;
|
|
189
|
+
}
|
|
190
|
+
return Math.sqrt(sum / node.data.length) > speakingThreshold;
|
|
191
|
+
}
|
|
192
|
+
function closeAudioContext() {
|
|
193
|
+
if (!audioCtx)
|
|
194
|
+
return;
|
|
195
|
+
const ctx = audioCtx;
|
|
196
|
+
audioCtx = null;
|
|
197
|
+
silentSink?.disconnect();
|
|
198
|
+
silentSink = null;
|
|
199
|
+
ctx.onstatechange = null;
|
|
200
|
+
void ctx.close().catch(() => { });
|
|
201
|
+
}
|
|
202
|
+
function ensureMeter() {
|
|
203
|
+
if (!speakingDetection || closed || meterTimer !== undefined)
|
|
204
|
+
return;
|
|
205
|
+
if (remotePeers.size === 0 && !localMeter)
|
|
206
|
+
return;
|
|
207
|
+
meterTimer = setInterval(() => {
|
|
208
|
+
if (closed)
|
|
209
|
+
return;
|
|
210
|
+
let changed = false;
|
|
211
|
+
for (const [peerId, streams] of remotePeers) {
|
|
212
|
+
let active = false;
|
|
213
|
+
for (const entry of streams.values()) {
|
|
214
|
+
if (isMeterActive(entry.meter)) {
|
|
215
|
+
active = true;
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const was = speakingPeers.has(peerId);
|
|
220
|
+
if (active && !was) {
|
|
221
|
+
speakingPeers.add(peerId);
|
|
222
|
+
changed = true;
|
|
223
|
+
}
|
|
224
|
+
else if (!active && was) {
|
|
225
|
+
speakingPeers.delete(peerId);
|
|
226
|
+
changed = true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const nextSelf = localLive && isMeterActive(localMeter);
|
|
230
|
+
if (nextSelf !== selfSpeaking) {
|
|
231
|
+
selfSpeaking = nextSelf;
|
|
232
|
+
changed = true;
|
|
233
|
+
}
|
|
234
|
+
if (changed)
|
|
235
|
+
onChange();
|
|
236
|
+
}, speakingIntervalMs);
|
|
237
|
+
}
|
|
238
|
+
function stopMeterIfEmpty() {
|
|
239
|
+
if (remotePeers.size === 0) {
|
|
240
|
+
speakingPeers.clear();
|
|
241
|
+
autoplayBlocked = false;
|
|
242
|
+
if (!isContextPaused())
|
|
243
|
+
detachGestureListeners();
|
|
244
|
+
}
|
|
245
|
+
if (remotePeers.size > 0 || localMeter)
|
|
246
|
+
return;
|
|
247
|
+
if (meterTimer !== undefined) {
|
|
248
|
+
clearInterval(meterTimer);
|
|
249
|
+
meterTimer = undefined;
|
|
250
|
+
}
|
|
251
|
+
closeAudioContext();
|
|
252
|
+
detachGestureListeners();
|
|
253
|
+
}
|
|
254
|
+
function tryPlayEntry(peerId, streamId, entry) {
|
|
255
|
+
let playPromise;
|
|
256
|
+
try {
|
|
257
|
+
playPromise = entry.audio.play();
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (!playPromise || typeof playPromise.then !== 'function')
|
|
263
|
+
return;
|
|
264
|
+
void playPromise
|
|
265
|
+
.then(() => {
|
|
266
|
+
if (closed || remotePeers.get(peerId)?.get(streamId) !== entry)
|
|
267
|
+
return;
|
|
268
|
+
entry.blocked = false;
|
|
269
|
+
reconcileUnlockState();
|
|
270
|
+
})
|
|
271
|
+
.catch((err) => {
|
|
272
|
+
if (closed || remotePeers.get(peerId)?.get(streamId) !== entry)
|
|
273
|
+
return;
|
|
274
|
+
const name = err && typeof err === 'object' && 'name' in err
|
|
275
|
+
? String(err.name)
|
|
276
|
+
: '';
|
|
277
|
+
if (name !== 'NotAllowedError')
|
|
278
|
+
return;
|
|
279
|
+
entry.blocked = true;
|
|
280
|
+
markAutoplayBlocked();
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
// Never gate on an in-flight boolean or await audioCtx.resume(): on iOS Safari,
|
|
284
|
+
// pointerdown is not a media activation gesture and leaves audioCtx.resume()
|
|
285
|
+
// pending forever until a subsequent touchend/pointerup/click calls resume() again.
|
|
286
|
+
function unlockRemoteAudio(reattachOnPausedCtx = false) {
|
|
287
|
+
if (closed)
|
|
288
|
+
return;
|
|
289
|
+
if (isContextPaused() && audioCtx) {
|
|
290
|
+
void audioCtx
|
|
291
|
+
.resume()
|
|
292
|
+
.then(() => reconcileUnlockState())
|
|
293
|
+
.catch(() => { });
|
|
294
|
+
if (reattachOnPausedCtx)
|
|
295
|
+
attachGestureListeners();
|
|
296
|
+
}
|
|
297
|
+
let attemptedPlay = false;
|
|
298
|
+
for (const [peerId, streams] of remotePeers) {
|
|
299
|
+
for (const [streamId, entry] of streams) {
|
|
300
|
+
if (entry.audio.paused) {
|
|
301
|
+
attemptedPlay = true;
|
|
302
|
+
tryPlayEntry(peerId, streamId, entry);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (!attemptedPlay && !reattachOnPausedCtx) {
|
|
307
|
+
reconcileUnlockState();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
function unlockAudio() {
|
|
311
|
+
unlockRemoteAudio(false);
|
|
312
|
+
}
|
|
313
|
+
function teardownRemoteEntry(entry) {
|
|
314
|
+
disconnectMeter(entry.meter);
|
|
315
|
+
try {
|
|
316
|
+
entry.audio.pause();
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
// Ignore non-standard audio element implementations.
|
|
320
|
+
}
|
|
321
|
+
entry.audio.srcObject = null;
|
|
322
|
+
entry.audio.remove();
|
|
323
|
+
}
|
|
324
|
+
if (autoUnlock) {
|
|
325
|
+
detachVisibility = adapter.attachVisibility(() => {
|
|
326
|
+
if (!closed)
|
|
327
|
+
unlockRemoteAudio(true);
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
get volume() {
|
|
332
|
+
return volume;
|
|
333
|
+
},
|
|
334
|
+
get deafened() {
|
|
335
|
+
return deafened;
|
|
336
|
+
},
|
|
337
|
+
get autoplayBlocked() {
|
|
338
|
+
return autoplayBlocked;
|
|
339
|
+
},
|
|
340
|
+
get selfSpeaking() {
|
|
341
|
+
return selfSpeaking;
|
|
342
|
+
},
|
|
343
|
+
get remotePeers() {
|
|
344
|
+
return Object.freeze([...remotePeers.keys()]);
|
|
345
|
+
},
|
|
346
|
+
get speakingPeers() {
|
|
347
|
+
return Object.freeze([...speakingPeers]);
|
|
348
|
+
},
|
|
349
|
+
get mutedPeers() {
|
|
350
|
+
return Object.freeze([...mutedPeers]);
|
|
351
|
+
},
|
|
352
|
+
get peerVolumes() {
|
|
353
|
+
return Object.freeze(Object.fromEntries(peerVolumes));
|
|
354
|
+
},
|
|
355
|
+
addRemoteStream(peerId, stream, meta) {
|
|
356
|
+
if (closed)
|
|
357
|
+
return;
|
|
358
|
+
if (!stream || typeof stream.getAudioTracks !== 'function')
|
|
359
|
+
return;
|
|
360
|
+
if (stream.getAudioTracks().length === 0)
|
|
361
|
+
return;
|
|
362
|
+
const incomingTag = meta?.tag;
|
|
363
|
+
if (incomingTag === undefined ? strictStreamTag : incomingTag !== streamTag)
|
|
364
|
+
return;
|
|
365
|
+
let byPeer = remotePeers.get(peerId);
|
|
366
|
+
if (!byPeer) {
|
|
367
|
+
byPeer = new Map();
|
|
368
|
+
remotePeers.set(peerId, byPeer);
|
|
369
|
+
}
|
|
370
|
+
if (byPeer.has(stream.id) || byPeer.size >= maxRemoteStreamsPerPeer)
|
|
371
|
+
return;
|
|
372
|
+
const audio = adapter.createAudioElement();
|
|
373
|
+
if (!audio) {
|
|
374
|
+
if (byPeer.size === 0)
|
|
375
|
+
remotePeers.delete(peerId);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
audio.autoplay = true;
|
|
379
|
+
audio.setAttribute('playsinline', '');
|
|
380
|
+
audio.hidden = true;
|
|
381
|
+
audio.muted = effectiveMutedFor(peerId);
|
|
382
|
+
applyAudioVolume(audio, effectiveVolumeFor(peerId));
|
|
383
|
+
audio.srcObject = stream;
|
|
384
|
+
const entry = {
|
|
385
|
+
audio,
|
|
386
|
+
blocked: false,
|
|
387
|
+
meter: createMeterNode(stream)
|
|
388
|
+
};
|
|
389
|
+
byPeer.set(stream.id, entry);
|
|
390
|
+
const onPlaybackAccepted = () => {
|
|
391
|
+
if (closed || remotePeers.get(peerId)?.get(stream.id) !== entry || entry.audio.paused)
|
|
392
|
+
return;
|
|
393
|
+
entry.blocked = false;
|
|
394
|
+
reconcileUnlockState();
|
|
395
|
+
};
|
|
396
|
+
audio.addEventListener('play', onPlaybackAccepted);
|
|
397
|
+
audio.addEventListener('playing', onPlaybackAccepted);
|
|
398
|
+
tryPlayEntry(peerId, stream.id, entry);
|
|
399
|
+
ensureMeter();
|
|
400
|
+
onChange();
|
|
401
|
+
},
|
|
402
|
+
removeRemoteStream(peerId, stream) {
|
|
403
|
+
if (closed)
|
|
404
|
+
return;
|
|
405
|
+
const byPeer = remotePeers.get(peerId);
|
|
406
|
+
const entry = byPeer?.get(stream.id);
|
|
407
|
+
if (!entry || !byPeer)
|
|
408
|
+
return;
|
|
409
|
+
teardownRemoteEntry(entry);
|
|
410
|
+
byPeer.delete(stream.id);
|
|
411
|
+
if (byPeer.size === 0) {
|
|
412
|
+
remotePeers.delete(peerId);
|
|
413
|
+
speakingPeers.delete(peerId);
|
|
414
|
+
}
|
|
415
|
+
stopMeterIfEmpty();
|
|
416
|
+
reconcileUnlockState();
|
|
417
|
+
onChange();
|
|
418
|
+
},
|
|
419
|
+
setLocalStream(stream, { isLive, rebindAnalyser = false, unlock = false }) {
|
|
420
|
+
if (closed)
|
|
421
|
+
return;
|
|
422
|
+
localLive = isLive;
|
|
423
|
+
if (!isLive)
|
|
424
|
+
selfSpeaking = false;
|
|
425
|
+
if (stream === null) {
|
|
426
|
+
disconnectMeter(localMeter);
|
|
427
|
+
localMeter = null;
|
|
428
|
+
localStreamRef = null;
|
|
429
|
+
stopMeterIfEmpty();
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (stream !== localStreamRef || rebindAnalyser) {
|
|
433
|
+
disconnectMeter(localMeter);
|
|
434
|
+
localStreamRef = stream;
|
|
435
|
+
localMeter = createMeterNode(stream);
|
|
436
|
+
ensureMeter();
|
|
437
|
+
}
|
|
438
|
+
if (unlock)
|
|
439
|
+
unlockAudio();
|
|
440
|
+
},
|
|
441
|
+
setVolume(nextVolume) {
|
|
442
|
+
if (typeof nextVolume !== 'number' || !Number.isFinite(nextVolume)) {
|
|
443
|
+
throw new RangeError('Expected a finite volume');
|
|
444
|
+
}
|
|
445
|
+
if (closed)
|
|
446
|
+
return;
|
|
447
|
+
const clamped = Math.max(0, Math.min(1, nextVolume));
|
|
448
|
+
if (clamped === volume)
|
|
449
|
+
return;
|
|
450
|
+
volume = clamped;
|
|
451
|
+
for (const [peerId, byPeer] of remotePeers) {
|
|
452
|
+
const eff = effectiveVolumeFor(peerId);
|
|
453
|
+
for (const entry of byPeer.values()) {
|
|
454
|
+
applyAudioVolume(entry.audio, eff);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
onChange();
|
|
458
|
+
},
|
|
459
|
+
setPeerVolume(peerId, nextPeerVolume) {
|
|
460
|
+
validatePeerIdArg(peerId);
|
|
461
|
+
if (typeof nextPeerVolume !== 'number' || !Number.isFinite(nextPeerVolume)) {
|
|
462
|
+
throw new RangeError('Expected a finite volume');
|
|
463
|
+
}
|
|
464
|
+
if (closed)
|
|
465
|
+
return;
|
|
466
|
+
const clamped = Math.max(0, Math.min(1, nextPeerVolume));
|
|
467
|
+
const prev = peerVolumes.get(peerId) ?? 1;
|
|
468
|
+
if (clamped === prev)
|
|
469
|
+
return;
|
|
470
|
+
if (clamped === 1) {
|
|
471
|
+
peerVolumes.delete(peerId);
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
if (!peerVolumes.has(peerId)) {
|
|
475
|
+
evictPeerPref(peerVolumes, remotePeers);
|
|
476
|
+
}
|
|
477
|
+
peerVolumes.delete(peerId);
|
|
478
|
+
peerVolumes.set(peerId, clamped);
|
|
479
|
+
}
|
|
480
|
+
const byPeer = remotePeers.get(peerId);
|
|
481
|
+
if (byPeer) {
|
|
482
|
+
const eff = effectiveVolumeFor(peerId);
|
|
483
|
+
for (const entry of byPeer.values()) {
|
|
484
|
+
applyAudioVolume(entry.audio, eff);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
onChange();
|
|
488
|
+
},
|
|
489
|
+
getPeerVolume(peerId) {
|
|
490
|
+
if (typeof peerId !== 'string' || peerId.length === 0)
|
|
491
|
+
return 1;
|
|
492
|
+
return peerVolumes.get(peerId) ?? 1;
|
|
493
|
+
},
|
|
494
|
+
setDeafened(nextDeafened) {
|
|
495
|
+
if (closed)
|
|
496
|
+
return;
|
|
497
|
+
const next = Boolean(nextDeafened);
|
|
498
|
+
if (next === deafened)
|
|
499
|
+
return;
|
|
500
|
+
deafened = next;
|
|
501
|
+
for (const [peerId, byPeer] of remotePeers) {
|
|
502
|
+
const effMuted = effectiveMutedFor(peerId);
|
|
503
|
+
for (const entry of byPeer.values()) {
|
|
504
|
+
entry.audio.muted = effMuted;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (!next)
|
|
508
|
+
unlockAudio();
|
|
509
|
+
onChange();
|
|
510
|
+
},
|
|
511
|
+
setPeerMuted(peerId, muted) {
|
|
512
|
+
validatePeerIdArg(peerId);
|
|
513
|
+
if (closed)
|
|
514
|
+
return;
|
|
515
|
+
const next = Boolean(muted);
|
|
516
|
+
const was = mutedPeers.has(peerId);
|
|
517
|
+
if (next === was)
|
|
518
|
+
return;
|
|
519
|
+
if (next) {
|
|
520
|
+
evictPeerPref(mutedPeers, remotePeers);
|
|
521
|
+
mutedPeers.add(peerId);
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
mutedPeers.delete(peerId);
|
|
525
|
+
}
|
|
526
|
+
const byPeer = remotePeers.get(peerId);
|
|
527
|
+
if (byPeer) {
|
|
528
|
+
const effMuted = effectiveMutedFor(peerId);
|
|
529
|
+
for (const entry of byPeer.values()) {
|
|
530
|
+
entry.audio.muted = effMuted;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (!next && !deafened)
|
|
534
|
+
unlockAudio();
|
|
535
|
+
onChange();
|
|
536
|
+
},
|
|
537
|
+
isPeerMuted(peerId) {
|
|
538
|
+
return typeof peerId === 'string' && mutedPeers.has(peerId);
|
|
539
|
+
},
|
|
540
|
+
unlockAudio,
|
|
541
|
+
close() {
|
|
542
|
+
if (closed)
|
|
543
|
+
return;
|
|
544
|
+
closed = true;
|
|
545
|
+
if (detachVisibility) {
|
|
546
|
+
detachVisibility();
|
|
547
|
+
detachVisibility = null;
|
|
548
|
+
}
|
|
549
|
+
detachGestureListeners();
|
|
550
|
+
disconnectMeter(localMeter);
|
|
551
|
+
localMeter = null;
|
|
552
|
+
localStreamRef = null;
|
|
553
|
+
localLive = false;
|
|
554
|
+
selfSpeaking = false;
|
|
555
|
+
for (const byPeer of remotePeers.values()) {
|
|
556
|
+
for (const entry of byPeer.values()) {
|
|
557
|
+
teardownRemoteEntry(entry);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
remotePeers.clear();
|
|
561
|
+
speakingPeers.clear();
|
|
562
|
+
peerVolumes.clear();
|
|
563
|
+
mutedPeers.clear();
|
|
564
|
+
autoplayBlocked = false;
|
|
565
|
+
stopMeterIfEmpty();
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
}
|
package/dist/voice.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { type ListenOptions, type Room } from './room.js';
|
|
2
2
|
/** Current state of the local microphone managed by `createVoice()`. */
|
|
3
3
|
export type MicStatus = 'off' | 'requesting' | 'live' | 'muted' | 'error';
|
|
4
|
+
/**
|
|
5
|
+
* Why the local microphone transitioned to `'off'`:
|
|
6
|
+
* - `'stopped'`: stopped locally via `stopMic()` or `close()`
|
|
7
|
+
* - `'ended'`: hardware microphone track ended unexpectedly (unplugged device or OS/browser permission revoked)
|
|
8
|
+
*/
|
|
9
|
+
export type MicStopReason = 'stopped' | 'ended';
|
|
4
10
|
/** Structured error codes describing why local microphone capture failed. */
|
|
5
11
|
export type MicErrorCode = 'permission-denied' | 'not-found' | 'in-use' | 'insecure-context' | 'room-closed' | 'unknown';
|
|
6
12
|
/** Structured microphone capture error exposed on `voice.micError`. */
|
|
@@ -12,6 +18,8 @@ export interface MicError {
|
|
|
12
18
|
export interface VoiceSnapshot {
|
|
13
19
|
readonly micStatus: MicStatus;
|
|
14
20
|
readonly micError: MicError | null;
|
|
21
|
+
/** Why the local microphone transitioned to `'off'` (`'stopped'`, `'ended'`, or `null` if never stopped or currently active/errored). */
|
|
22
|
+
readonly micStopReason: MicStopReason | null;
|
|
15
23
|
readonly deviceId: string | null;
|
|
16
24
|
readonly autoplayBlocked: boolean;
|
|
17
25
|
readonly deafened: boolean;
|