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

Clear pending track resolver on track unpublish #363

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/itchy-beans-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Decrease publication timeout to 10s and clean local state on failed unpublish attempts
36 changes: 30 additions & 6 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit

private _isClosed: boolean = true;

private pendingTrackResolvers: { [key: string]: (info: TrackInfo) => void } = {};
private pendingTrackResolvers: {
[key: string]: { resolve: (info: TrackInfo) => void; reject: () => void };
} = {};

// true if publisher connection has already been established.
// this is helpful to know if we need to restart ICE on the publisher connection
Expand Down Expand Up @@ -166,18 +168,40 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
}
return new Promise<TrackInfo>((resolve, reject) => {
const publicationTimeout = setTimeout(() => {
delete this.pendingTrackResolvers[req.cid];
reject(
new ConnectionError('publication of local track timed out, no response from server'),
);
}, 15_000);
this.pendingTrackResolvers[req.cid] = (info: TrackInfo) => {
clearTimeout(publicationTimeout);
resolve(info);
}, 10_000);
this.pendingTrackResolvers[req.cid] = {
resolve: (info: TrackInfo) => {
clearTimeout(publicationTimeout);
resolve(info);
},
reject: () => {
clearTimeout(publicationTimeout);
reject('Cancelled publication by calling unpublish');
},
};
this.client.sendAddTrack(req);
});
}

removeTrack(sender: RTCRtpSender) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

if (sender.track && this.pendingTrackResolvers[sender.track.id]) {
const { reject } = this.pendingTrackResolvers[sender.track.id];
if (reject) {
reject();
}
delete this.pendingTrackResolvers[sender.track.id];
}
try {
this.publisher?.pc.removeTrack(sender);
} catch (e: unknown) {
log.warn('failed to remove track', { error: e, method: 'removeTrack' });
}
}

updateMuteStatus(trackSid: string, muted: boolean) {
this.client.sendMuteTrack(trackSid, muted);
}
Expand Down Expand Up @@ -338,7 +362,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit

this.client.onLocalTrackPublished = (res: TrackPublishedResponse) => {
log.debug('received trackPublishedResponse', res);
const resolve = this.pendingTrackResolvers[res.cid];
const { resolve } = this.pendingTrackResolvers[res.cid];
if (!resolve) {
log.error(`missing track resolver for ${res.cid}`);
return;
Expand Down
6 changes: 3 additions & 3 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,18 +722,18 @@ export default class LocalParticipant extends Participant {
track.sender
) {
try {
this.engine.publisher.pc.removeTrack(track.sender);
this.engine.removeTrack(track.sender);
if (track instanceof LocalVideoTrack) {
for (const [, trackInfo] of track.simulcastCodecs) {
if (trackInfo.sender) {
this.engine.publisher.pc.removeTrack(trackInfo.sender);
this.engine.removeTrack(trackInfo.sender);
trackInfo.sender = undefined;
}
}
track.simulcastCodecs.clear();
}
} catch (e) {
log.warn('failed to remove track', { error: e, method: 'unpublishTrack' });
log.warn('failed to unpublish track', { error: e, method: 'unpublishTrack' });
} finally {
this.engine.negotiate();
}
Expand Down