Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mic not fully released in some cases #1515

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
Loading