From 7bc6033de98eb315de80b667bdcc7d5b07af7359 Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Thu, 26 Sep 2024 06:51:59 +0200 Subject: [PATCH 1/4] cleanup the speech detector --- packages/client/src/helpers/RNSpeechDetector.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/client/src/helpers/RNSpeechDetector.ts b/packages/client/src/helpers/RNSpeechDetector.ts index 4a59264c1a..b3da9a0b52 100644 --- a/packages/client/src/helpers/RNSpeechDetector.ts +++ b/packages/client/src/helpers/RNSpeechDetector.ts @@ -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( @@ -97,4 +100,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(); + } + } } From 218c57d8f002a7934e2f2dbdf644b2d3680b593c Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Thu, 10 Oct 2024 10:16:07 +0200 Subject: [PATCH 2/4] cleanup on speech detector stop --- packages/client/src/helpers/RNSpeechDetector.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/client/src/helpers/RNSpeechDetector.ts b/packages/client/src/helpers/RNSpeechDetector.ts index b3da9a0b52..828ed1d155 100644 --- a/packages/client/src/helpers/RNSpeechDetector.ts +++ b/packages/client/src/helpers/RNSpeechDetector.ts @@ -58,6 +58,7 @@ export class RNSpeechDetector { public stop() { this.pc1.close(); this.pc2.close(); + this.cleanupAudioStream(); if (this.intervalId) { clearInterval(this.intervalId); } From a4d887e4ea0e251fd34e3efeee17a5f7d8287804 Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Thu, 10 Oct 2024 11:58:50 +0200 Subject: [PATCH 3/4] fix: leave call on most call states --- packages/client/src/events/call.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/client/src/events/call.ts b/packages/client/src/events/call.ts index f0de9b6ea6..11afcbea7f 100644 --- a/packages/client/src/events/call.ts +++ b/packages/client/src/events/call.ts @@ -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); From a2428d4df32633c5184fe0cb391cd60c91029da4 Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Thu, 10 Oct 2024 12:05:51 +0200 Subject: [PATCH 4/4] fix: improper check --- packages/client/src/events/call.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/events/call.ts b/packages/client/src/events/call.ts index 11afcbea7f..5111703849 100644 --- a/packages/client/src/events/call.ts +++ b/packages/client/src/events/call.ts @@ -77,7 +77,7 @@ export const watchCallEnded = (call: Call) => { const { callingState } = call.state; if ( callingState !== CallingState.IDLE && - callingState === CallingState.LEFT + callingState !== CallingState.LEFT ) { call.leave({ reason: 'call.ended event received' }).catch((err) => { call.logger('error', 'Failed to leave call after call.ended ', err);