Skip to content

Commit a830601

Browse files
committed
Merge branch 'speech-detector-cleanup' into mic-release-investigation
2 parents bcbb3f9 + 218c57d commit a830601

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

packages/client/src/helpers/RNSpeechDetector.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ export class RNSpeechDetector {
88
private pc1 = new RTCPeerConnection({});
99
private pc2 = new RTCPeerConnection({});
1010
private intervalId: NodeJS.Timeout | undefined;
11+
private audioStream: MediaStream | undefined;
1112

1213
/**
1314
* Starts the speech detection.
1415
*/
1516
public async start() {
1617
try {
18+
this.cleanupAudioStream();
1719
const audioStream = await navigator.mediaDevices.getUserMedia({
1820
audio: true,
1921
});
22+
this.audioStream = audioStream;
2023

2124
this.pc1.addEventListener('icecandidate', async (e) => {
2225
await this.pc2.addIceCandidate(
@@ -55,6 +58,7 @@ export class RNSpeechDetector {
5558
public stop() {
5659
this.pc1.close();
5760
this.pc2.close();
61+
this.cleanupAudioStream();
5862
if (this.intervalId) {
5963
clearInterval(this.intervalId);
6064
}
@@ -97,4 +101,18 @@ export class RNSpeechDetector {
97101
clearInterval(this.intervalId);
98102
};
99103
}
104+
105+
private cleanupAudioStream() {
106+
if (!this.audioStream) {
107+
return;
108+
}
109+
this.audioStream.getTracks().forEach((track) => track.stop());
110+
if (
111+
// @ts-expect-error release() is present in react-native-webrtc
112+
typeof this.audioStream.release === 'function'
113+
) {
114+
// @ts-expect-error called to dispose the stream in RN
115+
this.audioStream.release();
116+
}
117+
}
100118
}

0 commit comments

Comments
 (0)