Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old tracks before adding/replacing new ones in janus.js #3203

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 37 additions & 46 deletions html/janus.js
Original file line number Diff line number Diff line change
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