I am in the process of creating an Angular application that enables two users to have a video call using the Openvidu calling solution.
As part of this application, I have implemented a feature that allows users to switch between different cameras or microphones while on the call.
When a new microphone is selected, the previous microphone track is stopped and removed from the stream before adding the new one. This functionality is demonstrated in the code snippet below:
async onMicrophoneSelected(event: any) {
var currentMediaStream: MediaStream = this.localUsersService.getWebcamPublisher().stream.getMediaStream();
var currentAudioTrack: MediaStreamTrack;
var currentVideoTrack: MediaStreamTrack;
var newAudioInfo: MediaDeviceInfo;
var newAudioTrack: MediaStreamTrack;
var newVideoTrack: MediaStreamTrack;
// Identifying current video & audio tracks in use
currentMediaStream.getTracks().forEach((track) => {
if (track.kind === 'audio') {
currentAudioTrack = track;
currentAudioTrack.stop();
}
if (track.kind === 'video') {
currentVideoTrack = track;
}
});
await navigator.mediaDevices.enumerateDevices().then((res) => {
res.forEach((device) => {
if (device.kind === 'audioinput' && device.deviceId === event.value) {
newAudioInfo = device;
}
});
});
await navigator.mediaDevices.getUserMedia({ audio: { deviceId: { exact: newAudioInfo.deviceId } } }).then((stream) => {
newAudioTrack = stream.getAudioTracks()[0];
});
this.localUsersService
.getWebcamPublisher()
.replaceTrack(newAudioTrack)
.then(() => {
console.log(currentMediaStream.getTracks(), '<<<-- checking stream after changes');
});
}
Once the above code executes successfully, the active microphone should be switched to the newly selected one during the call.
While this change works as intended, I am experiencing an issue where there is a loud echo when switching microphones, causing me to hear myself through the new microphone. Any suggestions on how to resolve this would be greatly appreciated. Thank you for your assistance.
Note: echoCancellation
was not able to solve this problem.