Skip to content

Commit

Permalink
fix: redundant blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Nov 30, 2023
1 parent 0b2f433 commit 106a3de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
7 changes: 0 additions & 7 deletions packages/client/src/devices/MicrophoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export class MicrophoneManager extends InputMediaDeviceManager<MicrophoneManager
if (callingState !== CallingState.JOINED) {
if (callingState === CallingState.LEFT) {
await this.stopSpeakingWhileMutedDetection();
if (isReactNative()) {
this.rnSpeechDetector?.stop();
}
}
return;
}
Expand Down Expand Up @@ -64,7 +61,6 @@ export class MicrophoneManager extends InputMediaDeviceManager<MicrophoneManager

private async startSpeakingWhileMutedDetection(deviceId?: string) {
await this.stopSpeakingWhileMutedDetection();

if (isReactNative()) {
this.rnSpeechDetector = new RNSpeechDetector();
await this.rnSpeechDetector.start();
Expand All @@ -90,9 +86,6 @@ export class MicrophoneManager extends InputMediaDeviceManager<MicrophoneManager
}

private async stopSpeakingWhileMutedDetection() {
if (isReactNative()) {
this.rnSpeechDetector?.stop();
}
if (!this.soundDetectorCleanup) {
return;
}
Expand Down
58 changes: 28 additions & 30 deletions packages/client/src/helpers/RNSpeechDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,33 @@ export class RNSpeechDetector {
*/
public async start() {
try {
if (this.pc1 && this.pc2) {
const audioStream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
const audioStream = await navigator.mediaDevices.getUserMedia({
audio: true,
});

this.pc1.addEventListener('icecandidate', async (e) => {
await this.pc2?.addIceCandidate(
e.candidate as RTCIceCandidateInit | undefined,
);
});
this.pc2.addEventListener('icecandidate', async (e) => {
await this.pc1?.addIceCandidate(
e.candidate as RTCIceCandidateInit | undefined,
);
});
this.pc1.addEventListener('icecandidate', async (e) => {
await this.pc2.addIceCandidate(
e.candidate as RTCIceCandidateInit | undefined,
);
});
this.pc2.addEventListener('icecandidate', async (e) => {
await this.pc1.addIceCandidate(
e.candidate as RTCIceCandidateInit | undefined,
);
});

audioStream
.getTracks()
.forEach((track) => this.pc1?.addTrack(track, audioStream));
const offer = await this.pc1.createOffer({});
await this.pc2.setRemoteDescription(offer);
await this.pc1.setLocalDescription(offer);
const answer = await this.pc2.createAnswer();
await this.pc1.setRemoteDescription(answer);
await this.pc2.setLocalDescription(answer);
const audioTracks = audioStream.getAudioTracks();
// We need to mute the audio track for this temporary stream, or else you will hear yourself twice while in the call.
audioTracks.forEach((track) => (track.enabled = false));
}
audioStream
.getTracks()
.forEach((track) => this.pc1.addTrack(track, audioStream));
const offer = await this.pc1.createOffer({});
await this.pc2.setRemoteDescription(offer);
await this.pc1.setLocalDescription(offer);
const answer = await this.pc2.createAnswer();
await this.pc1.setRemoteDescription(answer);
await this.pc2.setLocalDescription(answer);
const audioTracks = audioStream.getAudioTracks();
// We need to mute the audio track for this temporary stream, or else you will hear yourself twice while in the call.
audioTracks.forEach((track) => (track.enabled = false));
} catch (error) {
console.error(
'Error connecting and negotiating between PeerConnections:',
Expand All @@ -67,8 +65,8 @@ export class RNSpeechDetector {
* Stops the speech detection and releases all allocated resources.
*/
public stop() {
this.pc1?.close();
this.pc2?.close();
this.pc1.close();
this.pc2.close();
if (this.intervalId) {
clearInterval(this.intervalId);
}
Expand All @@ -81,7 +79,7 @@ export class RNSpeechDetector {
onSoundDetectedStateChanged: SoundStateChangeHandler,
) {
this.intervalId = setInterval(async () => {
const stats = (await this.pc1?.getStats()) as RTCStatsReport;
const stats = (await this.pc1.getStats()) as RTCStatsReport;
const report = flatten(stats);
// Audio levels are present inside stats of type `media-source` and of kind `audio`
const audioMediaSourceStats = report.find(
Expand Down

0 comments on commit 106a3de

Please sign in to comment.