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.
pull/700/head
csantosm 2022-02-21 11:02:25 +01:00
parent c972751e42
commit 74dae4258c
1 changed files with 10 additions and 6 deletions

View File

@ -292,15 +292,19 @@ export class Publisher extends StreamManager {
const replaceTrackInMediaStream = (): Promise<void> => { const replaceTrackInMediaStream = (): Promise<void> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const mediaStream: MediaStream = this.stream.displayMyRemote() ? this.stream.localMediaStreamWhenSubscribedToRemote! : this.stream.getMediaStream(); 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') { if (track.kind === 'video') {
removedTrack = mediaStream.getVideoTracks()[0]; newMediaStream.addTrack(mediaStream.getAudioTracks()[0]);
} else { } else {
removedTrack = mediaStream.getAudioTracks()[0]; newMediaStream.addTrack(mediaStream.getVideoTracks()[0]);
} }
mediaStream.removeTrack(removedTrack);
removedTrack.stop(); this.stream.setMediaStream(newMediaStream);
mediaStream.addTrack(track); this.stream.streamManager.updateMediaStream(newMediaStream);
if (track.kind === 'video' && this.stream.isLocalStreamPublished) { if (track.kind === 'video' && this.stream.isLocalStreamPublished) {
this.openvidu.sendNewVideoDimensionsIfRequired(this, 'trackReplaced', 50, 30); this.openvidu.sendNewVideoDimensionsIfRequired(this, 'trackReplaced', 50, 30);
this.session.sendVideoData(this.stream.streamManager, 5, true, 5); this.session.sendVideoData(this.stream.streamManager, 5, true, 5);