Skip to content

Commit

Permalink
Remove old tracks before adding/replacing new ones in janus.js (#3203)
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero committed Apr 27, 2023
1 parent c0948f2 commit 013155d
Showing 1 changed file with 37 additions and 46 deletions.
83 changes: 37 additions & 46 deletions html/janus.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 013155d

Please sign in to comment.