@dimchansky/meshpost 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/README.md +266 -106
- package/dist/channel.d.ts +37 -2
- package/dist/channel.js +226 -12
- package/dist/crypto.d.ts +34 -0
- package/dist/crypto.js +158 -0
- package/dist/identity.d.ts +45 -0
- package/dist/identity.js +264 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.js +5 -1
- package/dist/mqtt.d.ts +9 -1
- package/dist/mqtt.js +95 -87
- package/dist/nostr.d.ts +27 -0
- package/dist/nostr.js +1008 -0
- package/dist/room.d.ts +37 -13
- package/dist/room.js +584 -62
- package/dist/rpc.d.ts +48 -0
- package/dist/rpc.js +382 -0
- package/dist/signaling.d.ts +4 -8
- package/dist/signaling.js +10 -2
- package/dist/state.d.ts +56 -0
- package/dist/state.js +305 -0
- package/dist/voice.d.ts +28 -1
- package/dist/voice.js +349 -27
- package/dist/ws.d.ts +17 -2
- package/dist/ws.js +248 -33
- package/docs/behavior.md +93 -55
- package/package.json +67 -2
package/dist/voice.js
CHANGED
|
@@ -7,10 +7,13 @@ const defaultAudioConstraints = Object.freeze({
|
|
|
7
7
|
const gestureEvents = ['pointerdown', 'pointerup', 'touchend', 'click', 'keydown'];
|
|
8
8
|
const captureOpts = { capture: true };
|
|
9
9
|
const maxRemoteStreamsPerPeer = 4;
|
|
10
|
+
const maxPeerAudioPrefs = 128;
|
|
10
11
|
const minSpeakingIntervalMs = 50;
|
|
11
12
|
const maxSpeakingIntervalMs = 60_000;
|
|
12
13
|
const defaultSpeakingThreshold = 0.03;
|
|
13
14
|
const defaultSpeakingIntervalMs = 180;
|
|
15
|
+
const defaultStreamTag = 'voice';
|
|
16
|
+
const streamTagPattern = /^[a-zA-Z0-9._-]{1,32}$/;
|
|
14
17
|
function isAudioCtxPaused(ctx) {
|
|
15
18
|
return ctx !== null && ctx.state !== 'running' && ctx.state !== 'closed';
|
|
16
19
|
}
|
|
@@ -28,10 +31,13 @@ function classifyMicError(err) {
|
|
|
28
31
|
message: 'Microphone permission denied'
|
|
29
32
|
});
|
|
30
33
|
}
|
|
31
|
-
if (name === 'NotFoundError' ||
|
|
34
|
+
if (name === 'NotFoundError' ||
|
|
35
|
+
name === 'DevicesNotFoundError' ||
|
|
36
|
+
name === 'OverconstrainedError' ||
|
|
37
|
+
name === 'ConstraintNotSatisfiedError') {
|
|
32
38
|
return Object.freeze({
|
|
33
39
|
code: 'not-found',
|
|
34
|
-
message: 'No microphone device found'
|
|
40
|
+
message: 'No matching microphone device found'
|
|
35
41
|
});
|
|
36
42
|
}
|
|
37
43
|
if (name === 'NotReadableError' || name === 'TrackStartError') {
|
|
@@ -143,7 +149,7 @@ export function createVoice(room, options) {
|
|
|
143
149
|
(typeof rawConstraints !== 'object' || rawConstraints === null || Array.isArray(rawConstraints))) {
|
|
144
150
|
throw new TypeError('Expected audioConstraints to be true or a MediaTrackConstraints object');
|
|
145
151
|
}
|
|
146
|
-
const
|
|
152
|
+
const baseConstraints = rawConstraints === undefined
|
|
147
153
|
? { ...defaultAudioConstraints }
|
|
148
154
|
: rawConstraints === true
|
|
149
155
|
? true
|
|
@@ -152,8 +158,46 @@ export function createVoice(room, options) {
|
|
|
152
158
|
if (targetPeer !== undefined && typeof targetPeer !== 'string') {
|
|
153
159
|
throw new TypeError('Expected target peer ID to be a string');
|
|
154
160
|
}
|
|
161
|
+
const streamTag = options?.streamTag ?? defaultStreamTag;
|
|
162
|
+
if (typeof streamTag !== 'string' || !streamTagPattern.test(streamTag)) {
|
|
163
|
+
throw new TypeError('Invalid streamTag');
|
|
164
|
+
}
|
|
165
|
+
if (options?.strictStreamTag !== undefined && typeof options.strictStreamTag !== 'boolean') {
|
|
166
|
+
throw new TypeError('Expected strictStreamTag to be a boolean');
|
|
167
|
+
}
|
|
168
|
+
const strictStreamTag = options?.strictStreamTag ?? false;
|
|
155
169
|
const autoUnlock = options?.autoUnlock !== false;
|
|
156
170
|
const speakingDetection = options?.speakingDetection !== false;
|
|
171
|
+
let selectedDeviceId = null;
|
|
172
|
+
if (typeof baseConstraints === 'object' && baseConstraints.deviceId !== undefined) {
|
|
173
|
+
const rawDev = baseConstraints.deviceId;
|
|
174
|
+
if (typeof rawDev === 'string' && rawDev.trim().length > 0) {
|
|
175
|
+
selectedDeviceId = rawDev;
|
|
176
|
+
}
|
|
177
|
+
else if (rawDev && typeof rawDev === 'object' && !Array.isArray(rawDev)) {
|
|
178
|
+
const candidate = typeof rawDev.exact === 'string'
|
|
179
|
+
? rawDev.exact
|
|
180
|
+
: typeof rawDev.ideal === 'string'
|
|
181
|
+
? rawDev.ideal
|
|
182
|
+
: null;
|
|
183
|
+
if (candidate && candidate.trim().length > 0)
|
|
184
|
+
selectedDeviceId = candidate;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function buildAudioConstraints(deviceOverride) {
|
|
188
|
+
if (deviceOverride !== null) {
|
|
189
|
+
const base = typeof baseConstraints === 'object'
|
|
190
|
+
? { ...baseConstraints }
|
|
191
|
+
: { ...defaultAudioConstraints };
|
|
192
|
+
base.deviceId = { exact: deviceOverride };
|
|
193
|
+
return base;
|
|
194
|
+
}
|
|
195
|
+
if (baseConstraints === true)
|
|
196
|
+
return true;
|
|
197
|
+
const copy = { ...baseConstraints };
|
|
198
|
+
delete copy.deviceId;
|
|
199
|
+
return copy;
|
|
200
|
+
}
|
|
157
201
|
let volume = initialVolume;
|
|
158
202
|
let deafened = Boolean(options?.deafened);
|
|
159
203
|
let micStatus = 'off';
|
|
@@ -165,7 +209,9 @@ export function createVoice(room, options) {
|
|
|
165
209
|
let localMeter = null;
|
|
166
210
|
let localTrackCleanups = [];
|
|
167
211
|
let micEpoch = 0;
|
|
212
|
+
let switchEpoch = 0;
|
|
168
213
|
let inFlightMic = null;
|
|
214
|
+
let swapQueue = Promise.resolve();
|
|
169
215
|
let audioSessionSwitched = false;
|
|
170
216
|
let prevAudioSessionType = null;
|
|
171
217
|
let audioCtx = null;
|
|
@@ -175,17 +221,32 @@ export function createVoice(room, options) {
|
|
|
175
221
|
let visibilityListenerAttached = false;
|
|
176
222
|
const remotePeers = new Map();
|
|
177
223
|
const speakingPeers = new Set();
|
|
224
|
+
const peerVolumes = new Map();
|
|
225
|
+
const mutedPeers = new Set();
|
|
178
226
|
const changes = listeners();
|
|
227
|
+
function effectiveVolumeFor(peerId) {
|
|
228
|
+
return Math.max(0, Math.min(1, volume * (peerVolumes.get(peerId) ?? 1)));
|
|
229
|
+
}
|
|
230
|
+
function effectiveMutedFor(peerId) {
|
|
231
|
+
return deafened || mutedPeers.has(peerId);
|
|
232
|
+
}
|
|
233
|
+
function validatePeerIdArg(peerId) {
|
|
234
|
+
if (typeof peerId !== 'string' || peerId.length === 0 || peerId.length > 128) {
|
|
235
|
+
throw new TypeError('Expected a non-empty peerId string');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
179
238
|
function buildSnapshot() {
|
|
180
239
|
return Object.freeze({
|
|
181
240
|
micStatus,
|
|
182
241
|
micError,
|
|
242
|
+
deviceId: selectedDeviceId,
|
|
183
243
|
autoplayBlocked,
|
|
184
244
|
deafened,
|
|
185
245
|
volume,
|
|
186
246
|
selfSpeaking,
|
|
187
247
|
remotePeers: Object.freeze([...remotePeers.keys()]),
|
|
188
|
-
speakingPeers: Object.freeze([...speakingPeers])
|
|
248
|
+
speakingPeers: Object.freeze([...speakingPeers]),
|
|
249
|
+
mutedPeers: Object.freeze([...mutedPeers])
|
|
189
250
|
});
|
|
190
251
|
}
|
|
191
252
|
function emitChange() {
|
|
@@ -479,6 +540,22 @@ export function createVoice(room, options) {
|
|
|
479
540
|
stopMeterIfEmpty();
|
|
480
541
|
return changed;
|
|
481
542
|
}
|
|
543
|
+
function bindLocalTrackEnded(stream) {
|
|
544
|
+
for (const cleanup of localTrackCleanups)
|
|
545
|
+
cleanup();
|
|
546
|
+
localTrackCleanups = [];
|
|
547
|
+
const onTrackEnded = () => {
|
|
548
|
+
if (closed || localStream !== stream)
|
|
549
|
+
return;
|
|
550
|
+
const changed = teardownLocalStream(true);
|
|
551
|
+
if (changed)
|
|
552
|
+
emitChange();
|
|
553
|
+
};
|
|
554
|
+
for (const track of stream.getAudioTracks()) {
|
|
555
|
+
track.addEventListener('ended', onTrackEnded, { once: true });
|
|
556
|
+
localTrackCleanups.push(() => track.removeEventListener('ended', onTrackEnded));
|
|
557
|
+
}
|
|
558
|
+
}
|
|
482
559
|
function acquireMic() {
|
|
483
560
|
if (closed)
|
|
484
561
|
return Promise.resolve('off');
|
|
@@ -494,6 +571,7 @@ export function createVoice(room, options) {
|
|
|
494
571
|
return Promise.resolve('error');
|
|
495
572
|
}
|
|
496
573
|
const epoch = ++micEpoch;
|
|
574
|
+
const requestedDeviceId = selectedDeviceId;
|
|
497
575
|
micStatus = 'requesting';
|
|
498
576
|
micError = null;
|
|
499
577
|
emitChange();
|
|
@@ -503,7 +581,9 @@ export function createVoice(room, options) {
|
|
|
503
581
|
const promise = (async () => {
|
|
504
582
|
let stream;
|
|
505
583
|
try {
|
|
506
|
-
stream = await navigator.mediaDevices.getUserMedia({
|
|
584
|
+
stream = await navigator.mediaDevices.getUserMedia({
|
|
585
|
+
audio: buildAudioConstraints(requestedDeviceId)
|
|
586
|
+
});
|
|
507
587
|
}
|
|
508
588
|
catch (err) {
|
|
509
589
|
if (closed || epoch !== micEpoch)
|
|
@@ -530,7 +610,10 @@ export function createVoice(room, options) {
|
|
|
530
610
|
return 'off';
|
|
531
611
|
}
|
|
532
612
|
try {
|
|
533
|
-
room.addStream(stream,
|
|
613
|
+
room.addStream(stream, {
|
|
614
|
+
...(targetPeer !== undefined ? { to: targetPeer } : {}),
|
|
615
|
+
tag: streamTag
|
|
616
|
+
});
|
|
534
617
|
}
|
|
535
618
|
catch {
|
|
536
619
|
restoreAudioSession();
|
|
@@ -545,20 +628,13 @@ export function createVoice(room, options) {
|
|
|
545
628
|
return 'error';
|
|
546
629
|
}
|
|
547
630
|
localStream = stream;
|
|
631
|
+
const actualDeviceId = stream.getAudioTracks()[0]?.getSettings?.().deviceId;
|
|
632
|
+
selectedDeviceId =
|
|
633
|
+
requestedDeviceId ??
|
|
634
|
+
(typeof actualDeviceId === 'string' && actualDeviceId.length > 0 ? actualDeviceId : null);
|
|
548
635
|
micStatus = 'live';
|
|
549
636
|
micError = null;
|
|
550
|
-
|
|
551
|
-
if (closed || localStream !== stream)
|
|
552
|
-
return;
|
|
553
|
-
const changed = teardownLocalStream(true);
|
|
554
|
-
if (changed)
|
|
555
|
-
emitChange();
|
|
556
|
-
};
|
|
557
|
-
localTrackCleanups = [];
|
|
558
|
-
for (const track of stream.getAudioTracks()) {
|
|
559
|
-
track.addEventListener('ended', onTrackEnded, { once: true });
|
|
560
|
-
localTrackCleanups.push(() => track.removeEventListener('ended', onTrackEnded));
|
|
561
|
-
}
|
|
637
|
+
bindLocalTrackEnded(stream);
|
|
562
638
|
const meter = createAnalyser(stream);
|
|
563
639
|
localMeter = meter.analyser ? meter : null;
|
|
564
640
|
ensureMeter();
|
|
@@ -586,13 +662,16 @@ export function createVoice(room, options) {
|
|
|
586
662
|
entry.audio.srcObject = null;
|
|
587
663
|
entry.audio.remove();
|
|
588
664
|
}
|
|
589
|
-
function addRemoteStream(peerId, stream) {
|
|
665
|
+
function addRemoteStream(peerId, stream, meta) {
|
|
590
666
|
if (closed || typeof document === 'undefined' || !document.body)
|
|
591
667
|
return;
|
|
592
668
|
if (!stream || typeof stream.getAudioTracks !== 'function')
|
|
593
669
|
return;
|
|
594
670
|
if (stream.getAudioTracks().length === 0)
|
|
595
671
|
return;
|
|
672
|
+
const incomingTag = meta?.tag;
|
|
673
|
+
if (incomingTag === undefined ? strictStreamTag : incomingTag !== streamTag)
|
|
674
|
+
return;
|
|
596
675
|
let byPeer = remotePeers.get(peerId);
|
|
597
676
|
if (!byPeer) {
|
|
598
677
|
byPeer = new Map();
|
|
@@ -606,8 +685,8 @@ export function createVoice(room, options) {
|
|
|
606
685
|
audio.autoplay = true;
|
|
607
686
|
audio.setAttribute('playsinline', '');
|
|
608
687
|
audio.hidden = true;
|
|
609
|
-
audio.muted =
|
|
610
|
-
applyAudioVolume(audio,
|
|
688
|
+
audio.muted = effectiveMutedFor(peerId);
|
|
689
|
+
applyAudioVolume(audio, effectiveVolumeFor(peerId));
|
|
611
690
|
audio.srcObject = stream;
|
|
612
691
|
document.body.append(audio);
|
|
613
692
|
const meter = createAnalyser(stream);
|
|
@@ -665,6 +744,7 @@ export function createVoice(room, options) {
|
|
|
665
744
|
return;
|
|
666
745
|
closed = true;
|
|
667
746
|
micEpoch++;
|
|
747
|
+
switchEpoch++;
|
|
668
748
|
inFlightMic = null;
|
|
669
749
|
offStream();
|
|
670
750
|
offStreamEnd();
|
|
@@ -682,6 +762,8 @@ export function createVoice(room, options) {
|
|
|
682
762
|
}
|
|
683
763
|
remotePeers.clear();
|
|
684
764
|
speakingPeers.clear();
|
|
765
|
+
peerVolumes.clear();
|
|
766
|
+
mutedPeers.clear();
|
|
685
767
|
autoplayBlocked = false;
|
|
686
768
|
stopMeterIfEmpty();
|
|
687
769
|
changes.clear();
|
|
@@ -701,8 +783,8 @@ export function createVoice(room, options) {
|
|
|
701
783
|
visibilityListenerAttached = true;
|
|
702
784
|
document.addEventListener('visibilitychange', onVisibilityChange);
|
|
703
785
|
}
|
|
704
|
-
for (const [peerId, stream] of getRoomRemoteStreams(room)) {
|
|
705
|
-
addRemoteStream(peerId, stream);
|
|
786
|
+
for (const [peerId, stream, meta] of getRoomRemoteStreams(room)) {
|
|
787
|
+
addRemoteStream(peerId, stream, meta);
|
|
706
788
|
}
|
|
707
789
|
}
|
|
708
790
|
return {
|
|
@@ -712,6 +794,9 @@ export function createVoice(room, options) {
|
|
|
712
794
|
get micError() {
|
|
713
795
|
return micError;
|
|
714
796
|
},
|
|
797
|
+
get deviceId() {
|
|
798
|
+
return selectedDeviceId;
|
|
799
|
+
},
|
|
715
800
|
get autoplayBlocked() {
|
|
716
801
|
return autoplayBlocked;
|
|
717
802
|
},
|
|
@@ -730,6 +815,9 @@ export function createVoice(room, options) {
|
|
|
730
815
|
get speakingPeers() {
|
|
731
816
|
return Object.freeze([...speakingPeers]);
|
|
732
817
|
},
|
|
818
|
+
get mutedPeers() {
|
|
819
|
+
return Object.freeze([...mutedPeers]);
|
|
820
|
+
},
|
|
733
821
|
getSnapshot: buildSnapshot,
|
|
734
822
|
toggleMic() {
|
|
735
823
|
if (closed)
|
|
@@ -789,11 +877,159 @@ export function createVoice(room, options) {
|
|
|
789
877
|
return;
|
|
790
878
|
const wasRequesting = inFlightMic !== null;
|
|
791
879
|
micEpoch++;
|
|
880
|
+
switchEpoch++;
|
|
792
881
|
inFlightMic = null;
|
|
793
882
|
const changed = teardownLocalStream(true);
|
|
794
883
|
if (changed || wasRequesting)
|
|
795
884
|
emitChange();
|
|
796
885
|
},
|
|
886
|
+
async switchDevice(deviceId) {
|
|
887
|
+
let nextDeviceId = null;
|
|
888
|
+
if (deviceId !== undefined && deviceId !== null && deviceId !== '') {
|
|
889
|
+
if (typeof deviceId !== 'string' || deviceId.trim().length === 0) {
|
|
890
|
+
throw new TypeError('Expected deviceId to be a non-empty string, null, or undefined');
|
|
891
|
+
}
|
|
892
|
+
nextDeviceId = deviceId;
|
|
893
|
+
}
|
|
894
|
+
if (closed)
|
|
895
|
+
return 'off';
|
|
896
|
+
if (inFlightMic !== null) {
|
|
897
|
+
micEpoch++;
|
|
898
|
+
switchEpoch++;
|
|
899
|
+
inFlightMic = null;
|
|
900
|
+
selectedDeviceId = nextDeviceId;
|
|
901
|
+
return acquireMic();
|
|
902
|
+
}
|
|
903
|
+
if (!localStream || (micStatus !== 'live' && micStatus !== 'muted')) {
|
|
904
|
+
if (selectedDeviceId !== nextDeviceId) {
|
|
905
|
+
selectedDeviceId = nextDeviceId;
|
|
906
|
+
emitChange();
|
|
907
|
+
}
|
|
908
|
+
return micStatus;
|
|
909
|
+
}
|
|
910
|
+
if (typeof navigator === 'undefined' || !navigator.mediaDevices?.getUserMedia) {
|
|
911
|
+
micError = Object.freeze({
|
|
912
|
+
code: 'insecure-context',
|
|
913
|
+
message: 'Microphone access requires HTTPS or localhost'
|
|
914
|
+
});
|
|
915
|
+
emitChange();
|
|
916
|
+
return micStatus;
|
|
917
|
+
}
|
|
918
|
+
if (typeof room.replaceStreamTrack !== 'function') {
|
|
919
|
+
throw new TypeError('Room instance does not support replaceStreamTrack');
|
|
920
|
+
}
|
|
921
|
+
const epoch = micEpoch;
|
|
922
|
+
const sEpoch = ++switchEpoch;
|
|
923
|
+
let newStream;
|
|
924
|
+
try {
|
|
925
|
+
newStream = await navigator.mediaDevices.getUserMedia({
|
|
926
|
+
audio: buildAudioConstraints(nextDeviceId)
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
catch (err) {
|
|
930
|
+
if (closed)
|
|
931
|
+
return 'off';
|
|
932
|
+
if (epoch !== micEpoch || sEpoch !== switchEpoch)
|
|
933
|
+
return micStatus;
|
|
934
|
+
micError = classifyMicError(err);
|
|
935
|
+
emitChange();
|
|
936
|
+
return micStatus;
|
|
937
|
+
}
|
|
938
|
+
if (closed ||
|
|
939
|
+
epoch !== micEpoch ||
|
|
940
|
+
sEpoch !== switchEpoch ||
|
|
941
|
+
!localStream ||
|
|
942
|
+
isRoomClosed(room)) {
|
|
943
|
+
for (const t of newStream.getTracks())
|
|
944
|
+
t.stop();
|
|
945
|
+
return closed ? 'off' : micStatus;
|
|
946
|
+
}
|
|
947
|
+
let resultStatus = micStatus;
|
|
948
|
+
const runSwap = swapQueue.then(async () => {
|
|
949
|
+
if (closed ||
|
|
950
|
+
epoch !== micEpoch ||
|
|
951
|
+
sEpoch !== switchEpoch ||
|
|
952
|
+
!localStream ||
|
|
953
|
+
isRoomClosed(room)) {
|
|
954
|
+
for (const t of newStream.getTracks())
|
|
955
|
+
t.stop();
|
|
956
|
+
resultStatus = closed ? 'off' : micStatus;
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
const audioTracks = newStream.getAudioTracks();
|
|
960
|
+
const newTrack = audioTracks[0];
|
|
961
|
+
for (const extra of newStream.getTracks()) {
|
|
962
|
+
if (extra !== newTrack)
|
|
963
|
+
extra.stop();
|
|
964
|
+
}
|
|
965
|
+
const targetStream = localStream;
|
|
966
|
+
const oldTrack = targetStream.getAudioTracks()[0];
|
|
967
|
+
if (!newTrack || !oldTrack) {
|
|
968
|
+
newTrack?.stop();
|
|
969
|
+
resultStatus = micStatus;
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
newTrack.enabled = micStatus === 'live';
|
|
973
|
+
for (const cleanup of localTrackCleanups)
|
|
974
|
+
cleanup();
|
|
975
|
+
localTrackCleanups = [];
|
|
976
|
+
let replaceInvoked = false;
|
|
977
|
+
try {
|
|
978
|
+
replaceInvoked = true;
|
|
979
|
+
await room.replaceStreamTrack(targetStream, oldTrack, newTrack);
|
|
980
|
+
}
|
|
981
|
+
catch (err) {
|
|
982
|
+
if (replaceInvoked && !targetStream.getTracks().includes(oldTrack)) {
|
|
983
|
+
oldTrack.stop();
|
|
984
|
+
}
|
|
985
|
+
newTrack.stop();
|
|
986
|
+
if (!closed && epoch === micEpoch && localStream === targetStream) {
|
|
987
|
+
if (!targetStream.getAudioTracks().length) {
|
|
988
|
+
teardownLocalStream(true);
|
|
989
|
+
}
|
|
990
|
+
else {
|
|
991
|
+
bindLocalTrackEnded(targetStream);
|
|
992
|
+
}
|
|
993
|
+
micError = classifyMicError(err);
|
|
994
|
+
emitChange();
|
|
995
|
+
}
|
|
996
|
+
resultStatus = closed ? 'off' : micStatus;
|
|
997
|
+
return;
|
|
998
|
+
}
|
|
999
|
+
// oldTrack has been removed from targetStream; always release hardware even if cancelled mid-await.
|
|
1000
|
+
oldTrack.stop();
|
|
1001
|
+
if (closed || epoch !== micEpoch || localStream !== targetStream) {
|
|
1002
|
+
newTrack.stop();
|
|
1003
|
+
resultStatus = closed ? 'off' : micStatus;
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
1006
|
+
if (sEpoch !== switchEpoch) {
|
|
1007
|
+
// A newer switchDevice() is queued next in swapQueue and will replace newTrack!
|
|
1008
|
+
bindLocalTrackEnded(targetStream);
|
|
1009
|
+
resultStatus = micStatus;
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
bindLocalTrackEnded(targetStream);
|
|
1013
|
+
if (localMeter) {
|
|
1014
|
+
localMeter.source?.disconnect();
|
|
1015
|
+
localMeter.analyser?.disconnect();
|
|
1016
|
+
localMeter = null;
|
|
1017
|
+
}
|
|
1018
|
+
const meter = createAnalyser(targetStream);
|
|
1019
|
+
localMeter = meter.analyser ? meter : null;
|
|
1020
|
+
ensureMeter();
|
|
1021
|
+
const actualDeviceId = newTrack.getSettings?.().deviceId;
|
|
1022
|
+
selectedDeviceId =
|
|
1023
|
+
nextDeviceId ??
|
|
1024
|
+
(typeof actualDeviceId === 'string' && actualDeviceId.length > 0 ? actualDeviceId : null);
|
|
1025
|
+
micError = null;
|
|
1026
|
+
emitChange();
|
|
1027
|
+
resultStatus = micStatus;
|
|
1028
|
+
});
|
|
1029
|
+
swapQueue = runSwap.catch(() => { });
|
|
1030
|
+
await runSwap;
|
|
1031
|
+
return resultStatus;
|
|
1032
|
+
},
|
|
797
1033
|
setVolume(nextVolume) {
|
|
798
1034
|
if (typeof nextVolume !== 'number' || !Number.isFinite(nextVolume)) {
|
|
799
1035
|
throw new RangeError('Expected a finite volume');
|
|
@@ -804,13 +1040,58 @@ export function createVoice(room, options) {
|
|
|
804
1040
|
if (clamped === volume)
|
|
805
1041
|
return;
|
|
806
1042
|
volume = clamped;
|
|
807
|
-
for (const byPeer of remotePeers
|
|
1043
|
+
for (const [peerId, byPeer] of remotePeers) {
|
|
1044
|
+
const eff = effectiveVolumeFor(peerId);
|
|
808
1045
|
for (const entry of byPeer.values()) {
|
|
809
|
-
applyAudioVolume(entry.audio,
|
|
1046
|
+
applyAudioVolume(entry.audio, eff);
|
|
810
1047
|
}
|
|
811
1048
|
}
|
|
812
1049
|
emitChange();
|
|
813
1050
|
},
|
|
1051
|
+
setPeerVolume(peerId, nextPeerVolume) {
|
|
1052
|
+
validatePeerIdArg(peerId);
|
|
1053
|
+
if (typeof nextPeerVolume !== 'number' || !Number.isFinite(nextPeerVolume)) {
|
|
1054
|
+
throw new RangeError('Expected a finite volume');
|
|
1055
|
+
}
|
|
1056
|
+
if (closed)
|
|
1057
|
+
return;
|
|
1058
|
+
const clamped = Math.max(0, Math.min(1, nextPeerVolume));
|
|
1059
|
+
const prev = peerVolumes.get(peerId) ?? 1;
|
|
1060
|
+
if (clamped === prev)
|
|
1061
|
+
return;
|
|
1062
|
+
if (clamped === 1) {
|
|
1063
|
+
peerVolumes.delete(peerId);
|
|
1064
|
+
}
|
|
1065
|
+
else {
|
|
1066
|
+
if (!peerVolumes.has(peerId) && peerVolumes.size >= maxPeerAudioPrefs) {
|
|
1067
|
+
let victim;
|
|
1068
|
+
for (const k of peerVolumes.keys()) {
|
|
1069
|
+
if (!remotePeers.has(k)) {
|
|
1070
|
+
victim = k;
|
|
1071
|
+
break;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
victim ??= peerVolumes.keys().next().value;
|
|
1075
|
+
if (victim !== undefined)
|
|
1076
|
+
peerVolumes.delete(victim);
|
|
1077
|
+
}
|
|
1078
|
+
peerVolumes.delete(peerId);
|
|
1079
|
+
peerVolumes.set(peerId, clamped);
|
|
1080
|
+
}
|
|
1081
|
+
const byPeer = remotePeers.get(peerId);
|
|
1082
|
+
if (byPeer) {
|
|
1083
|
+
const eff = effectiveVolumeFor(peerId);
|
|
1084
|
+
for (const entry of byPeer.values()) {
|
|
1085
|
+
applyAudioVolume(entry.audio, eff);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
emitChange();
|
|
1089
|
+
},
|
|
1090
|
+
getPeerVolume(peerId) {
|
|
1091
|
+
if (typeof peerId !== 'string' || peerId.length === 0)
|
|
1092
|
+
return 1;
|
|
1093
|
+
return peerVolumes.get(peerId) ?? 1;
|
|
1094
|
+
},
|
|
814
1095
|
setDeafened(nextDeafened) {
|
|
815
1096
|
if (closed)
|
|
816
1097
|
return;
|
|
@@ -818,15 +1099,56 @@ export function createVoice(room, options) {
|
|
|
818
1099
|
if (next === deafened)
|
|
819
1100
|
return;
|
|
820
1101
|
deafened = next;
|
|
821
|
-
for (const byPeer of remotePeers
|
|
1102
|
+
for (const [peerId, byPeer] of remotePeers) {
|
|
1103
|
+
const effMuted = effectiveMutedFor(peerId);
|
|
822
1104
|
for (const entry of byPeer.values()) {
|
|
823
|
-
entry.audio.muted =
|
|
1105
|
+
entry.audio.muted = effMuted;
|
|
824
1106
|
}
|
|
825
1107
|
}
|
|
826
1108
|
if (!next)
|
|
827
1109
|
unlockAudio();
|
|
828
1110
|
emitChange();
|
|
829
1111
|
},
|
|
1112
|
+
setPeerMuted(peerId, muted) {
|
|
1113
|
+
validatePeerIdArg(peerId);
|
|
1114
|
+
if (closed)
|
|
1115
|
+
return;
|
|
1116
|
+
const next = Boolean(muted);
|
|
1117
|
+
const was = mutedPeers.has(peerId);
|
|
1118
|
+
if (next === was)
|
|
1119
|
+
return;
|
|
1120
|
+
if (next) {
|
|
1121
|
+
if (mutedPeers.size >= maxPeerAudioPrefs) {
|
|
1122
|
+
let victim;
|
|
1123
|
+
for (const k of mutedPeers) {
|
|
1124
|
+
if (!remotePeers.has(k)) {
|
|
1125
|
+
victim = k;
|
|
1126
|
+
break;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
victim ??= mutedPeers.values().next().value;
|
|
1130
|
+
if (victim !== undefined)
|
|
1131
|
+
mutedPeers.delete(victim);
|
|
1132
|
+
}
|
|
1133
|
+
mutedPeers.add(peerId);
|
|
1134
|
+
}
|
|
1135
|
+
else {
|
|
1136
|
+
mutedPeers.delete(peerId);
|
|
1137
|
+
}
|
|
1138
|
+
const byPeer = remotePeers.get(peerId);
|
|
1139
|
+
if (byPeer) {
|
|
1140
|
+
const effMuted = effectiveMutedFor(peerId);
|
|
1141
|
+
for (const entry of byPeer.values()) {
|
|
1142
|
+
entry.audio.muted = effMuted;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
if (!next && !deafened)
|
|
1146
|
+
unlockAudio();
|
|
1147
|
+
emitChange();
|
|
1148
|
+
},
|
|
1149
|
+
isPeerMuted(peerId) {
|
|
1150
|
+
return typeof peerId === 'string' && mutedPeers.has(peerId);
|
|
1151
|
+
},
|
|
830
1152
|
unlockAudio,
|
|
831
1153
|
onChange(fn, listenOpts) {
|
|
832
1154
|
return changes.add(fn, listenOpts);
|
package/dist/ws.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Signaling } from './signaling.js';
|
|
1
|
+
import { type Signaling, type WebSocketConstructor } from './signaling.js';
|
|
2
2
|
export interface WsSignalingOptions {
|
|
3
3
|
/** Automatically reconnect with jittered backoff when disconnected. @default true */
|
|
4
4
|
reconnect?: boolean;
|
|
@@ -6,9 +6,24 @@ export interface WsSignalingOptions {
|
|
|
6
6
|
handshakeTimeoutMs?: number;
|
|
7
7
|
/** Interval in milliseconds for client keepalive pings (`{ type: 'ping' }`). @default 25000 */
|
|
8
8
|
pingIntervalMs?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Encrypt signaling payloads end-to-end with AES-256-GCM when a room `password` is set
|
|
11
|
+
* so the WebSocket server sees only opaque `box` ciphertexts.
|
|
12
|
+
* Set `false` only for plain HTTP LAN testing where `crypto.subtle` is unavailable.
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
encrypt?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Derive the WebSocket room key cryptographically from `password` so room membership is isolated on the server
|
|
18
|
+
* (disables `'decrypt-failed'` detection when a peer enters the wrong password).
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
privateTopic?: boolean;
|
|
22
|
+
/** Optional custom `WebSocket` constructor for headless runtimes or custom transports. */
|
|
23
|
+
WebSocket?: WebSocketConstructor;
|
|
9
24
|
}
|
|
10
25
|
/**
|
|
11
26
|
* Connect peers through a WebSocket signaling server (`ws://` or `wss://`).
|
|
12
|
-
*
|
|
27
|
+
* When a room `password` is set, SDP and ICE payloads are end-to-end encrypted with AES-256-GCM by default.
|
|
13
28
|
*/
|
|
14
29
|
export declare function wsSignaling(url: string, options?: WsSignalingOptions): Signaling;
|