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

Handle RTCPeerConnection error on replaceTrack #1321

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion src/room/track/LocalTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default abstract class LocalTrack<
return;
}
if (this._mediaStreamTrack) {
this.log.debug('replacing MediaStreamTrack', { ...this.logContext, mediaStreamTrack: this._mediaStreamTrack });
// detach
this.attachedElements.forEach((el) => {
detachTrack(this._mediaStreamTrack, el);
Expand All @@ -133,6 +134,8 @@ export default abstract class LocalTrack<
this._mediaStreamTrack.removeEventListener('ended', this.handleEnded);
this._mediaStreamTrack.removeEventListener('mute', this.handleTrackMuteEvent);
this._mediaStreamTrack.removeEventListener('unmute', this.handleTrackUnmuteEvent);
} else {
this.log.debug('setting MediaStreamTrack for the first time', this.logContext);
}

this.mediaStream = new MediaStream([newTrack]);
Expand Down Expand Up @@ -172,7 +175,33 @@ export default abstract class LocalTrack<
}
}
if (this.sender) {
await this.sender.replaceTrack(processedTrack ?? newTrack);
const currentTrack = this._mediaStreamTrack;
const replacementTrack = processedTrack ?? newTrack;
const trackLogContext = {
...this.logContext,
currentTrack,
newTrack,
processedTrack,
replacementTrack,
sender: this.sender
};

if (replacementTrack.readyState == "ended") {
this.log.warn('track is ended, not replacing track on sender', trackLogContext);
return;
}

try {
await this.sender.replaceTrack(replacementTrack);
this.log.debug('successfully replaced track on sender', trackLogContext);
} catch (e: any) {
const errorMessage = `failed to replace track on sender: ${e.message}`;
this.log.error(errorMessage, {
...trackLogContext,
error: e,
});
throw new TrackInvalidError(errorMessage);
}
}
// if `newTrack` is different from the existing track, stop the
// older track just before replacing it
Expand Down
Loading