Skip to content

fix: keep a paused upstream paused when the track is republished - #2162

Open
ydiarra wants to merge 2 commits into
livekit:mainfrom
ydiarra:fix/republish-paused-upstream
Open

ydiarra wants to merge 2 commits into
livekit:mainfrom
ydiarra:fix/republish-paused-upstream

Conversation

@ydiarra

@ydiarra ydiarra commented Oct 20, 2026 •

Copy link
Copy Markdown

Problem

pauseUpstream() stops sending a track without stopping it locally, so the app can keep showing its own preview. The paused state is stored on the LocalTrack (_isUpstreamPaused), and the null track is set on the RTCRtpSender only.

After a full reconnect, republishAllTracks() unpublishes every track and publishes it again with publishOrRepublishTrack(). The track keeps isUpstreamPaused === true, but:

  • the new sender is created with the real MediaStreamTrack, so media flows to the SFU again;
  • the AddTrackRequest is sent with muted: track.isMuted, so the server sees the track as unmuted;
  • calling pauseUpstream() again does nothing, because of the if (this._isUpstreamPaused === true) return guard.

updateInfo() resends the mute state (pub.isMuted || track.isUpstreamPaused), but only for tracks that are in a participant update received after the republish, so the server can stay out of sync. Even when it does fix the mute state, the sender still sends media. The app can only get out of this with resumeUpstream() followed by pauseUpstream().

The same thing happens to a backup codec published while the track is paused: createSimulcastSender() attaches the clone even though pauseUpstream() had nulled the other senders.

Repro

  1. Publish a camera or a microphone, then call track.pauseUpstream().
  2. Run room.simulateScenario('full-reconnect').
  3. Remote participants receive the media again, and the server reports the track as unmuted, while track.isUpstreamPaused is still true.

Fix

  • RTCEngine.createSender() and createSimulcastSender() call replaceTrack(null) on the new sender when the track is paused. This runs before the sender is assigned to the track and before negotiation, so no media is sent. resumeUpstream() works as before: it puts mediaStreamTrack back on track.sender and the backup codec senders.
  • The AddTrackRequest announces muted: track.isMuted || track.isUpstreamPaused, which is what onTrackUpstreamPaused and updateInfo() already send. The backup codec request from publishAdditionalCodecForTrack() does the same.

The change applies to every path that publishes a paused track again: full reconnect, E2EE toggle (setE2EEEnabled → republishAllTracks), or the app publishing a track it unpublished while paused.

Tests

Each new test failed before the fix and passes after it:

  • RTCEngine.test.ts: a paused track gets replaceTrack(null) on its new sender and on a new backup codec sender; a track that is not paused is left on its sender.
  • LocalParticipant.test.ts: the add track request of a paused track has muted: true, and so does the request for its backup codec.

pnpm test, tsc --noEmit and eslint pass. I didn't run the e2e suite against a server.

Context

We hit this in WorkAdventure, which never unpublishes the camera or microphone and uses pauseUpstream() instead. We currently work around it on LocalTrackPublished with resumeUpstream() and then pauseUpstream() (workadventure/workadventure#6800). We'll remove that once this is released.

@changeset-bot

changeset-bot Bot commented Oct 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1bc82e7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
livekit-client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

devin-ai-integration[bot]

This comment was marked as resolved.

@lukasIO lukasIO self-assigned this Oct 20, 2026
@dklima

dklima commented Nov 10, 2026

Copy link
Copy Markdown

This PR also affects a pause that the SDK starts. We see this on iOS Safari (iOS 26, livekit-client 2.23.0).

Steps

  1. Publish a camera and a microphone from createLocalTracks.
  2. Turn on airplane mode for 1 minute. We think that iOS mutes the capture. After 5 s, LocalTrack calls pauseUpstream() (LocalTrack.ts:490-497).
  3. Turn off airplane mode. The SDK does a full reconnect.
  4. republishAllTracks() unpublishes the track and removes its sender (LocalParticipant.ts:1641). Then it calls restartTrack() (LocalParticipant.ts:1732-1746).
  5. restartTrack() gets a new live capture and calls resumeUpstream() (LocalTrack.ts:230). The track has no sender, so resumeUpstream() stops (LocalTrack.ts:566-568).
  6. isUpstreamPaused stays true. updateInfo() sends the track as muted (LocalParticipant.ts:1989).

Result: Remote participants see the camera as muted. Only a page reload fixes it.

Effect of this PR: The PR keeps a paused track paused after a republish. This is correct for a pause from the app. For a pause from the SDK, it keeps a live camera paused.

Possible fix: Record why the upstream is paused. Clear a pause from the capture mute event when restartTrack() gets a live capture. Keep a pause from the app.

Our workaround: On RoomEvent.Reconnected, we call resumeUpstream() on each paused track whose raw capture is live and not muted. We read the raw capture from track.mediaStream, because mediaStreamTrack returns the processor output. This works for us because our app never pauses a track. We tested it on an iPhone.

Line numbers are from the v2.23.0 tag.

name: opts.name,
type: Track.kindToProto(track.kind),
muted: track.isMuted,
// the server sees a paused upstream as muted, see onTrackUpstreamPaused

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// the server sees a paused upstream as muted, see onTrackUpstreamPaused

cid: simulcastTrack.mediaStreamTrack.id,
type: Track.kindToProto(track.kind),
muted: track.isMuted,
// match the primary publish request, the backup sender starts without a paused track

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// match the primary publish request, the backup sender starts without a paused track

Comment thread src/room/RTCEngine.ts
throw new UnexpectedConnectionState('Required webRTC APIs not supported on this device');
}
this.setupFrameMetadataSender(sender, opts);
await this.keepPausedTrackOffSender(track, sender);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This also catches a pause the SDK starts itself (capture mute → pauseUpstream() after 5s). On a full reconnect republishAllTracks() clears track.sender before restartTrack(), so the resumeUpstream() in setMediaStreamTrack returns early and the flag stays true on a live capture. Same for an unmute that fires between the unpublish and the new sender being assigned.

So far media still flowed in those cases. With this change the track stays dead until the app calls resumeUpstream(), which is what dklima describes above.

Could LocalTrack record who paused (app vs. device), clear a device pause when the restart yields a live capture, and keep the app one?

Comment thread src/room/RTCEngine.ts
Comment on lines +1124 to +1130
// a track republished while its upstream is paused (e.g. after a full reconnect) gets a new
// sender, which pauseUpstream() never saw: the sender has to start without the track
private async keepPausedTrackOffSender(track: LocalTrack, sender: RTCRtpSender) {
if (track.isUpstreamPaused) {
await sender.replaceTrack(null);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could this live on the track instead of the engine? LocalTrack / LocalVideoTrack own _isUpstreamPaused and every other replaceTrack(null). setSimulcastTrackSender() already receives the backup sender, and the primary could go through a small track method where publish() assigns track.sender.

Comment thread src/room/RTCEngine.ts
// sender, which pauseUpstream() never saw: the sender has to start without the track
private async keepPausedTrackOffSender(track: LocalTrack, sender: RTCRtpSender) {
if (track.isUpstreamPaused) {
await sender.replaceTrack(null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you get to try this against a server on Firefox and Safari? In Chromium the first offer keeps a=msid with the track id after replaceTrack(null), so the cid still matches there. I have not checked the other browsers, or how the server treats a new publication that never receives RTP.

});
});

describe('createSender', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we add a case for the restart path: pause → republishAllTracks() with restartTracks = true → still paused, and resumeUpstream() puts the track back on the new sender?

The app pause only survives that path today because resumeUpstream() is a no-op without a sender. Any other restart clears the pause, so this is easy to break.

type: Track.kindToProto(track.kind),
muted: track.isMuted,
// the server sees a paused upstream as muted, see onTrackUpstreamPaused
muted: track.isMuted || track.isUpstreamPaused,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: isMuted || isUpstreamPaused now exists in four places (updateInfo, onTrackUnmuted, here, and the backup codec request). Worth a getter on LocalTrack

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants