From 74dae4258c23ac50e3bcd3ef55d4395c8615e041 Mon Sep 17 00:00:00 2001 From: csantosm <4a.santos@gmail.com> Date: Mon, 21 Feb 2022 11:02:25 +0100 Subject: [PATCH] openvidu-browser: Fixed echo when replace audio track. Fixes #642 Replacing an audio track caused echo. Creating a new MediaStream from scratch and assign it to the final mediaStream solve this problem. --- openvidu-browser/src/OpenVidu/Publisher.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/openvidu-browser/src/OpenVidu/Publisher.ts b/openvidu-browser/src/OpenVidu/Publisher.ts index 960c7807..8d17ffd1 100644 --- a/openvidu-browser/src/OpenVidu/Publisher.ts +++ b/openvidu-browser/src/OpenVidu/Publisher.ts @@ -292,15 +292,19 @@ export class Publisher extends StreamManager { const replaceTrackInMediaStream = (): Promise => { return new Promise((resolve, reject) => { const mediaStream: MediaStream = this.stream.displayMyRemote() ? this.stream.localMediaStreamWhenSubscribedToRemote! : this.stream.getMediaStream(); - let removedTrack: MediaStreamTrack; + const newMediaStream: MediaStream = new MediaStream(); + // Added new audio or video track to new MediaStream + newMediaStream.addTrack(track); + if (track.kind === 'video') { - removedTrack = mediaStream.getVideoTracks()[0]; + newMediaStream.addTrack(mediaStream.getAudioTracks()[0]); } else { - removedTrack = mediaStream.getAudioTracks()[0]; + newMediaStream.addTrack(mediaStream.getVideoTracks()[0]); } - mediaStream.removeTrack(removedTrack); - removedTrack.stop(); - mediaStream.addTrack(track); + + this.stream.setMediaStream(newMediaStream); + this.stream.streamManager.updateMediaStream(newMediaStream); + if (track.kind === 'video' && this.stream.isLocalStreamPublished) { this.openvidu.sendNewVideoDimensionsIfRequired(this, 'trackReplaced', 50, 30); this.session.sendVideoData(this.stream.streamManager, 5, true, 5);