From 013155d6e806277f8eee6272388dc95ab5294ef8 Mon Sep 17 00:00:00 2001 From: Lorenzo Miniero Date: Thu, 27 Apr 2023 17:30:00 +0900 Subject: [PATCH] Remove old tracks before adding/replacing new ones in janus.js (#3203) --- html/janus.js | 83 +++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/html/janus.js b/html/janus.js index 888ec32bd1..53d8c26251 100644 --- a/html/janus.js +++ b/html/janus.js @@ -2390,11 +2390,46 @@ function Janus(gatewayCallbacks) { } // Capture the new track, if we need to let nt = null, trackId = null; - if(track.remove) { + if(track.remove || track.replace) { Janus.log('Removing track from PeerConnection', track); trackId = sender.track ? sender.track.id : null; await sender.replaceTrack(null); - } else if(track.capture) { + // Get rid of the old track + if(trackId && config.myStream) { + let rt = null; + if(kind === 'audio' && config.myStream.getAudioTracks() && config.myStream.getAudioTracks().length) { + for(let t of config.myStream.getAudioTracks()) { + if(t.id === trackId) { + rt = t; + Janus.log('Removing audio track:', rt); + } + } + } else if(kind === 'video' && config.myStream.getVideoTracks() && config.myStream.getVideoTracks().length) { + for(let t of config.myStream.getVideoTracks()) { + if(t.id === trackId) { + rt = t; + Janus.log('Removing video track:', rt); + } + } + } + if(rt) { + // Remove the track and notify the application + try { + config.myStream.removeTrack(rt); + pluginHandle.onlocaltrack(rt, false); + } catch(e) { + Janus.error("Error calling onlocaltrack on removal for renegotiation", e); + } + // Close the old track (unless we've been asked not to) + if(rt.dontStop !== true) { + try { + rt.stop(); + } catch(e) {} + } + } + } + } + if(track.capture) { if(track.gumGroup && groups[track.gumGroup] && groups[track.gumGroup].stream) { // We did a getUserMedia before already let stream = groups[track.gumGroup].stream; @@ -2664,50 +2699,6 @@ function Janus(gatewayCallbacks) { } } } - // Get rid of the old track - // FIXME We should probably do this *before* capturing the new - // track, since this prevents, for instance, just changing the - // resolution of the same webcam we're capturing already (the - // existing resolution would be returned, or an overconstrained - // error). On the other end, closing the track before we capture - // the new device means we'd end up with a period of time where - // no video is sent (changing device takes some time), and - // media would be stopped entirely in case capturing the new - // device results in an error. To keep things simpler, we're - // doing it after: we can make this configurable in the future. - if(trackId && config.myStream) { - let rt = null; - if(kind === 'audio' && config.myStream.getAudioTracks() && config.myStream.getAudioTracks().length) { - for(let t of config.myStream.getAudioTracks()) { - if(t.id === trackId) { - rt = t; - Janus.log('Removing audio track:', rt); - } - } - } else if(kind === 'video' && config.myStream.getVideoTracks() && config.myStream.getVideoTracks().length) { - for(let t of config.myStream.getVideoTracks()) { - if(t.id === trackId) { - rt = t; - Janus.log('Removing video track:', rt); - } - } - } - if(rt) { - // Remove the track and notify the application - try { - config.myStream.removeTrack(rt); - pluginHandle.onlocaltrack(rt, false); - } catch(e) { - Janus.error("Error calling onlocaltrack on removal for renegotiation", e); - } - // Close the old track (unless we've been asked not to) - if(rt.dontStop !== true) { - try { - rt.stop(); - } catch(e) {} - } - } - } if(nt) { // FIXME Add the new track locally config.myStream.addTrack(nt);