@stream-io/video-client 1.46.1 → 1.48.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 +16 -0
- package/dist/index.browser.es.js +31 -197
- package/dist/index.browser.es.js.map +1 -1
- package/dist/index.cjs.js +30 -197
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.es.js +31 -197
- package/dist/index.es.js.map +1 -1
- package/dist/src/devices/MicrophoneManager.d.ts +0 -1
- package/dist/src/gen/coordinator/index.d.ts +6 -0
- package/dist/src/types.d.ts +11 -0
- package/index.ts +0 -1
- package/package.json +1 -1
- package/src/devices/CameraManager.ts +9 -2
- package/src/devices/DeviceManager.ts +13 -3
- package/src/devices/MicrophoneManager.ts +17 -6
- package/src/devices/SpeakerManager.ts +16 -4
- package/src/devices/__tests__/CameraManager.test.ts +32 -0
- package/src/devices/__tests__/DeviceManager.test.ts +71 -0
- package/src/devices/__tests__/MicrophoneManager.test.ts +23 -0
- package/src/devices/__tests__/MicrophoneManagerRN.test.ts +28 -29
- package/src/devices/__tests__/SpeakerManager.test.ts +28 -0
- package/src/gen/coordinator/index.ts +6 -0
- package/src/types.ts +9 -0
- package/dist/src/helpers/RNSpeechDetector.d.ts +0 -23
- package/src/helpers/RNSpeechDetector.ts +0 -224
- package/src/helpers/__tests__/RNSpeechDetector.test.ts +0 -52
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { BaseStats, flatten } from '../stats';
|
|
2
|
-
import { SoundStateChangeHandler } from './sound-detector';
|
|
3
|
-
import { videoLoggerSystem } from '../logger';
|
|
4
|
-
|
|
5
|
-
export class RNSpeechDetector {
|
|
6
|
-
private readonly pc1 = new RTCPeerConnection({});
|
|
7
|
-
private readonly pc2 = new RTCPeerConnection({});
|
|
8
|
-
private audioStream: MediaStream | undefined;
|
|
9
|
-
private externalAudioStream: MediaStream | undefined;
|
|
10
|
-
private isStopped = false;
|
|
11
|
-
|
|
12
|
-
constructor(externalAudioStream?: MediaStream) {
|
|
13
|
-
this.externalAudioStream = externalAudioStream;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Starts the speech detection.
|
|
18
|
-
*/
|
|
19
|
-
public async start(onSoundDetectedStateChanged: SoundStateChangeHandler) {
|
|
20
|
-
let detachListeners: (() => void) | undefined;
|
|
21
|
-
let unsubscribe: (() => void) | undefined;
|
|
22
|
-
|
|
23
|
-
try {
|
|
24
|
-
this.isStopped = false;
|
|
25
|
-
const audioStream =
|
|
26
|
-
this.externalAudioStream != null
|
|
27
|
-
? this.externalAudioStream
|
|
28
|
-
: await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
29
|
-
this.audioStream = audioStream;
|
|
30
|
-
|
|
31
|
-
const onPc1IceCandidate = (e: RTCPeerConnectionIceEvent) => {
|
|
32
|
-
this.forwardIceCandidate(this.pc2, e.candidate);
|
|
33
|
-
};
|
|
34
|
-
const onPc2IceCandidate = (e: RTCPeerConnectionIceEvent) => {
|
|
35
|
-
this.forwardIceCandidate(this.pc1, e.candidate);
|
|
36
|
-
};
|
|
37
|
-
const onTrackPc2 = (e: RTCTrackEvent) => {
|
|
38
|
-
e.streams[0].getTracks().forEach((track) => {
|
|
39
|
-
// In RN, the remote track is automatically added to the audio output device
|
|
40
|
-
// so we need to mute it to avoid hearing the audio back
|
|
41
|
-
// @ts-expect-error _setVolume is a private method in react-native-webrtc
|
|
42
|
-
track._setVolume(0);
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
this.pc1.addEventListener('icecandidate', onPc1IceCandidate);
|
|
47
|
-
this.pc2.addEventListener('icecandidate', onPc2IceCandidate);
|
|
48
|
-
this.pc2.addEventListener('track', onTrackPc2);
|
|
49
|
-
detachListeners = () => {
|
|
50
|
-
this.pc1.removeEventListener('icecandidate', onPc1IceCandidate);
|
|
51
|
-
this.pc2.removeEventListener('icecandidate', onPc2IceCandidate);
|
|
52
|
-
this.pc2.removeEventListener('track', onTrackPc2);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
audioStream
|
|
56
|
-
.getTracks()
|
|
57
|
-
.forEach((track) => this.pc1.addTrack(track, audioStream));
|
|
58
|
-
const offer = await this.pc1.createOffer({});
|
|
59
|
-
await this.pc2.setRemoteDescription(offer);
|
|
60
|
-
await this.pc1.setLocalDescription(offer);
|
|
61
|
-
const answer = await this.pc2.createAnswer();
|
|
62
|
-
await this.pc1.setRemoteDescription(answer);
|
|
63
|
-
await this.pc2.setLocalDescription(answer);
|
|
64
|
-
unsubscribe = this.onSpeakingDetectedStateChange(
|
|
65
|
-
onSoundDetectedStateChanged,
|
|
66
|
-
);
|
|
67
|
-
return () => {
|
|
68
|
-
detachListeners?.();
|
|
69
|
-
unsubscribe?.();
|
|
70
|
-
this.stop();
|
|
71
|
-
};
|
|
72
|
-
} catch (error) {
|
|
73
|
-
detachListeners?.();
|
|
74
|
-
unsubscribe?.();
|
|
75
|
-
this.stop();
|
|
76
|
-
|
|
77
|
-
const logger = videoLoggerSystem.getLogger('RNSpeechDetector');
|
|
78
|
-
logger.error('error handling permissions: ', error);
|
|
79
|
-
return () => {};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Stops the speech detection and releases all allocated resources.
|
|
85
|
-
*/
|
|
86
|
-
private stop() {
|
|
87
|
-
if (this.isStopped) return;
|
|
88
|
-
this.isStopped = true;
|
|
89
|
-
|
|
90
|
-
this.pc1.close();
|
|
91
|
-
this.pc2.close();
|
|
92
|
-
|
|
93
|
-
if (this.externalAudioStream != null) {
|
|
94
|
-
this.externalAudioStream = undefined;
|
|
95
|
-
} else {
|
|
96
|
-
this.cleanupAudioStream();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Public method that detects the audio levels and returns the status.
|
|
102
|
-
*/
|
|
103
|
-
private onSpeakingDetectedStateChange(
|
|
104
|
-
onSoundDetectedStateChanged: SoundStateChangeHandler,
|
|
105
|
-
) {
|
|
106
|
-
const initialBaselineNoiseLevel = 0.13;
|
|
107
|
-
let baselineNoiseLevel = initialBaselineNoiseLevel;
|
|
108
|
-
let speechDetected = false;
|
|
109
|
-
let speechTimer: NodeJS.Timeout | undefined;
|
|
110
|
-
let silenceTimer: NodeJS.Timeout | undefined;
|
|
111
|
-
const audioLevelHistory: number[] = []; // Store recent audio levels for smoother detection
|
|
112
|
-
const historyLength = 10;
|
|
113
|
-
const silenceThreshold = 1.1;
|
|
114
|
-
const resetThreshold = 0.9;
|
|
115
|
-
const speechTimeout = 500; // Speech is set to true after 500ms of audio detection
|
|
116
|
-
const silenceTimeout = 5000; // Reset baseline after 5 seconds of silence
|
|
117
|
-
|
|
118
|
-
const checkAudioLevel = async () => {
|
|
119
|
-
try {
|
|
120
|
-
const stats = await this.pc1.getStats();
|
|
121
|
-
const report = flatten(stats);
|
|
122
|
-
// Audio levels are present inside stats of type `media-source` and of kind `audio`
|
|
123
|
-
const audioMediaSourceStats = report.find(
|
|
124
|
-
(stat) =>
|
|
125
|
-
stat.type === 'media-source' &&
|
|
126
|
-
(stat as RTCRtpStreamStats).kind === 'audio',
|
|
127
|
-
) as BaseStats;
|
|
128
|
-
if (audioMediaSourceStats) {
|
|
129
|
-
const { audioLevel } = audioMediaSourceStats;
|
|
130
|
-
if (audioLevel) {
|
|
131
|
-
// Update audio level history (with max historyLength sized array)
|
|
132
|
-
audioLevelHistory.push(audioLevel);
|
|
133
|
-
if (audioLevelHistory.length > historyLength) {
|
|
134
|
-
audioLevelHistory.shift();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Calculate average audio level
|
|
138
|
-
const avgAudioLevel =
|
|
139
|
-
audioLevelHistory.reduce((a, b) => a + b, 0) /
|
|
140
|
-
audioLevelHistory.length;
|
|
141
|
-
|
|
142
|
-
// Update baseline (if necessary) based on silence detection
|
|
143
|
-
if (avgAudioLevel < baselineNoiseLevel * silenceThreshold) {
|
|
144
|
-
if (!silenceTimer) {
|
|
145
|
-
silenceTimer = setTimeout(() => {
|
|
146
|
-
baselineNoiseLevel = Math.min(
|
|
147
|
-
avgAudioLevel * resetThreshold,
|
|
148
|
-
initialBaselineNoiseLevel,
|
|
149
|
-
);
|
|
150
|
-
}, silenceTimeout);
|
|
151
|
-
}
|
|
152
|
-
} else {
|
|
153
|
-
clearTimeout(silenceTimer);
|
|
154
|
-
silenceTimer = undefined;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Speech detection with hysteresis
|
|
158
|
-
if (avgAudioLevel > baselineNoiseLevel * 1.5) {
|
|
159
|
-
if (!speechDetected) {
|
|
160
|
-
speechDetected = true;
|
|
161
|
-
onSoundDetectedStateChanged({
|
|
162
|
-
isSoundDetected: true,
|
|
163
|
-
audioLevel,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
clearTimeout(speechTimer);
|
|
168
|
-
|
|
169
|
-
speechTimer = setTimeout(() => {
|
|
170
|
-
speechDetected = false;
|
|
171
|
-
onSoundDetectedStateChanged({
|
|
172
|
-
isSoundDetected: false,
|
|
173
|
-
audioLevel: 0,
|
|
174
|
-
});
|
|
175
|
-
}, speechTimeout);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
} catch (error) {
|
|
180
|
-
const logger = videoLoggerSystem.getLogger('RNSpeechDetector');
|
|
181
|
-
logger.error('error checking audio level from stats', error);
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
const intervalId = setInterval(checkAudioLevel, 250);
|
|
186
|
-
return () => {
|
|
187
|
-
clearInterval(intervalId);
|
|
188
|
-
clearTimeout(speechTimer);
|
|
189
|
-
clearTimeout(silenceTimer);
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
private cleanupAudioStream() {
|
|
194
|
-
if (!this.audioStream) {
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
this.audioStream.getTracks().forEach((track) => track.stop());
|
|
198
|
-
if (
|
|
199
|
-
// @ts-expect-error release() is present in react-native-webrtc
|
|
200
|
-
typeof this.audioStream.release === 'function'
|
|
201
|
-
) {
|
|
202
|
-
// @ts-expect-error called to dispose the stream in RN
|
|
203
|
-
this.audioStream.release();
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
private forwardIceCandidate(
|
|
208
|
-
destination: RTCPeerConnection,
|
|
209
|
-
candidate: RTCIceCandidate | null,
|
|
210
|
-
) {
|
|
211
|
-
if (
|
|
212
|
-
this.isStopped ||
|
|
213
|
-
!candidate ||
|
|
214
|
-
destination.signalingState === 'closed'
|
|
215
|
-
) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
destination.addIceCandidate(candidate).catch(() => {
|
|
219
|
-
// silently ignore the error
|
|
220
|
-
const logger = videoLoggerSystem.getLogger('RNSpeechDetector');
|
|
221
|
-
logger.info('cannot add ice candidate - ignoring');
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import '../../rtc/__tests__/mocks/webrtc.mocks';
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
-
import { RNSpeechDetector } from '../RNSpeechDetector';
|
|
4
|
-
|
|
5
|
-
describe('RNSpeechDetector', () => {
|
|
6
|
-
// Shared test setup stubs RTCPeerConnection with a vi.fn constructor.
|
|
7
|
-
// We keep a typed handle to that constructor to inspect created instances.
|
|
8
|
-
let rtcPeerConnectionMockCtor: ReturnType<typeof vi.fn>;
|
|
9
|
-
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
rtcPeerConnectionMockCtor =
|
|
12
|
-
globalThis.RTCPeerConnection as unknown as ReturnType<typeof vi.fn>;
|
|
13
|
-
rtcPeerConnectionMockCtor.mockClear();
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
vi.restoreAllMocks();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('ignores late ICE candidates after cleanup', async () => {
|
|
21
|
-
const stream = {
|
|
22
|
-
getTracks: () => [],
|
|
23
|
-
} as unknown as MediaStream;
|
|
24
|
-
const detector = new RNSpeechDetector(stream);
|
|
25
|
-
|
|
26
|
-
const cleanup = await detector.start(() => {});
|
|
27
|
-
cleanup();
|
|
28
|
-
|
|
29
|
-
// start() creates two peer connections (pc1 and pc2). We pull them from
|
|
30
|
-
// constructor call results to inspect listener wiring and ICE forwarding.
|
|
31
|
-
const [pc1, pc2] = rtcPeerConnectionMockCtor.mock.results.map(
|
|
32
|
-
(result) => result.value,
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
// Find the registered ICE callback and invoke it manually after cleanup to
|
|
36
|
-
// simulate a late ICE event arriving during teardown.
|
|
37
|
-
const onIceCandidate = pc1.addEventListener.mock.calls.find(
|
|
38
|
-
([eventName]: [string]) => eventName === 'icecandidate',
|
|
39
|
-
)?.[1] as ((e: RTCPeerConnectionIceEvent) => void) | undefined;
|
|
40
|
-
|
|
41
|
-
expect(onIceCandidate).toBeDefined();
|
|
42
|
-
onIceCandidate?.({
|
|
43
|
-
candidate: { candidate: 'candidate:1 1 UDP 0 127.0.0.1 11111 typ host' },
|
|
44
|
-
} as unknown as RTCPeerConnectionIceEvent);
|
|
45
|
-
|
|
46
|
-
expect(pc1.removeEventListener).toHaveBeenCalledWith(
|
|
47
|
-
'icecandidate',
|
|
48
|
-
onIceCandidate,
|
|
49
|
-
);
|
|
50
|
-
expect(pc2.addIceCandidate).not.toHaveBeenCalled();
|
|
51
|
-
});
|
|
52
|
-
});
|