Skip to content

Expose subscriber-side remote track enable/disable in Python - #977

Open
zhao-xuan wants to merge 2 commits into
livekit:mainfrom
zhao-xuan:feat/remote-track-publication-enabled
Open

zhao-xuan wants to merge 2 commits into
livekit:mainfrom
zhao-xuan:feat/remote-track-publication-enabled

Conversation

@zhao-xuan

Copy link
Copy Markdown

Closes #976.

Motivation

Real-time voice and language-learning agents need to pause incoming microphone media between speaking attempts while keeping the subscription intact. Unsubscribing and resubscribing can require negotiation and stream recreation, risking loss of initial speech. Discarding frames in an agent input gate does not stop downstream media transfer. Publisher mute is a separate user-controlled operation.

Changes

  • Add RemoteTrackPublication.set_enabled(enabled: bool) to expose the existing EnableRemoteTrackPublicationRequest FFI operation.
  • Document subscriber-side semantics, the requirement to call it after subscribing, and the asynchronous media-delivery boundary.
  • Add parameterized unit tests verifying both enable and disable requests, the publication handle and unchanged local subscription state.
publication.set_enabled(False)
publication.set_enabled(True)

The existing generated Python protocol and pinned Rust FFI handler already implement the request; no native SDK or protobuf regeneration is needed.

Validation

  • Unit tests added for both boolean values (not run locally).
  • End-to-end media delivery, RTP transfer reduction, subscription lifecycle, and resume latency remain to be verified against a LiveKit server.

This API is intended for frequent subscriber-side toggles, not remote microphone unmute. It does not guarantee zero latency or immediate drainage of in-flight media.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@devin-ai-integration devin-ai-integration Bot left a comment

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.

Devin Review found 3 potential issues.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

req = proto_ffi.FfiRequest()
req.enable_remote_track_publication.track_publication_handle = self._ffi_handle.handle
req.enable_remote_track_publication.enabled = enabled
FfiClient.instance.request(req)

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.

🔴 Track toggle crashes during unpublish

When a publisher removes a track during set_enabled, the native toggle can unwrap a track that no longer exists. The FFI panic callback terminates the Python process.

Learn more

The native request handler forwards the toggle to the Rust publication. The Rust method checks for an attached media track, checks whether that track is enabled, then reads the track again and unwraps it. Each lookup takes a separate read lock in track. A publisher can unpublish concurrently, and unpublish_track_inner clears the track. The native C bridge catches the resulting panic and forwards it to the Python callback, which sends SIGTERM.

Example: An agent pauses an incoming microphone while the other participant stops publishing it. If the unpublish clears the track after the enabled check but before the final lookup, the agent process exits instead of ignoring the toggle on the removed track.

Recommended fix: Fix the pinned Rust implementation to take one optional track snapshot, return if it is absent, and compare and update that snapshot with the requested value. Verify whether signaling also needs synchronization with detach. A Python subscribed check cannot close the native race.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +133 to +134
req.enable_remote_track_publication.enabled = enabled
FfiClient.instance.request(req)

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.

🔴 Stale pause overrides track resume

Successive set_enabled calls spawn separate signaling tasks, so a pause can reach the server after its matching resume. The native toggle skips another resume because the local track already reads as enabled.

Learn more

The native toggle changes the local track synchronously but spawns each server update independently. The tasks run on a multi-threaded runtime. The signaling writer sends requests in channel arrival order, not in Python call order. If the earlier task reaches that channel last, the media server applies the older value. A repeat call requesting the current local value is a no-op.

Example: An agent calls set_enabled(False) and then set_enabled(True) while both updates are still being sent. If the resume task enqueues first, the server processes resume and then pause. The local track stays enabled, but incoming media stays paused.

Recommended fix: Serialize settings updates per publication in the native SDK, or coalesce pending updates into a single ordered sender that always transmits the newest desired settings last. Test two sequential API calls with the first send deliberately delayed.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +132 to +134
req.enable_remote_track_publication.track_publication_handle = self._ffi_handle.handle
req.enable_remote_track_publication.enabled = enabled
FfiClient.instance.request(req)

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.

🟡 Media toggles overwrite simulcast settings

On a simulcast track, set_enabled(True) overwrites the layer selected by set_video_quality with Low. Changing quality while paused also tells the media server to resume forwarding.

Learn more

Both operations send the same subscriber-settings message to the media server. The native enable callback fills in disabled but leaves quality at its protocol default, Low. The native quality callback fills in quality but leaves disabled at false. LiveKit Server's mergeSubscriberSettings preserves omitted FEC settings, not quality or disabled, and SubscribedTrack.applySettings uses the resulting settings for forwarding and layer selection.

Example: A subscriber selects High for a simulcast camera, pauses it, then resumes it. The resume request carries Low, so the server switches to the lowest layer. If the subscriber selects High during the pause instead, the settings request clears the server pause even though the local track stays disabled.

Recommended fix: Keep a single set of desired subscriber settings in the Rust SDK and send the full set on either update, preserving both the requested layer and whether forwarding is paused. Cover each call order with a server-backed simulcast test.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

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.

Expose subscriber-side RemoteTrackPublication.set_enabled() for audio tracks

2 participants