Skip to content

Commit

Permalink
fix: mic not fully released in some cases (#1515)
Browse files Browse the repository at this point in the history
* RNSpeechDetector could create multiple audio streams if started
multiple times - fixed
* On call ended when on reconnection we did not leave - fixed
  • Loading branch information
santhoshvai authored Oct 10, 2024
1 parent bcbb3f9 commit b7bf90b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/client/src/events/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ export const watchCallEnded = (call: Call) => {
return function onCallEnded() {
const { callingState } = call.state;
if (
callingState === CallingState.RINGING ||
callingState === CallingState.JOINED ||
callingState === CallingState.JOINING
callingState !== CallingState.IDLE &&
callingState !== CallingState.LEFT
) {
call.leave({ reason: 'call.ended event received' }).catch((err) => {
call.logger('error', 'Failed to leave call after call.ended ', err);
Expand Down
18 changes: 18 additions & 0 deletions packages/client/src/helpers/RNSpeechDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ export class RNSpeechDetector {
private pc1 = new RTCPeerConnection({});
private pc2 = new RTCPeerConnection({});
private intervalId: NodeJS.Timeout | undefined;
private audioStream: MediaStream | undefined;

/**
* Starts the speech detection.
*/
public async start() {
try {
this.cleanupAudioStream();
const audioStream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
this.audioStream = audioStream;

this.pc1.addEventListener('icecandidate', async (e) => {
await this.pc2.addIceCandidate(
Expand Down Expand Up @@ -55,6 +58,7 @@ export class RNSpeechDetector {
public stop() {
this.pc1.close();
this.pc2.close();
this.cleanupAudioStream();
if (this.intervalId) {
clearInterval(this.intervalId);
}
Expand Down Expand Up @@ -97,4 +101,18 @@ export class RNSpeechDetector {
clearInterval(this.intervalId);
};
}

private cleanupAudioStream() {
if (!this.audioStream) {
return;
}
this.audioStream.getTracks().forEach((track) => track.stop());
if (
// @ts-expect-error release() is present in react-native-webrtc
typeof this.audioStream.release === 'function'
) {
// @ts-expect-error called to dispose the stream in RN
this.audioStream.release();
}
}
}

0 comments on commit b7bf90b

Please sign in to comment.