Add Backup Codec Support to Python SDK #510
Open
+39
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds support for backup codec configuration when publishing tracks, bringing the Python SDK to feature parity with the JavaScript SDK. This allows developers to specify codec fallback behavior for improved compatibility across different clients and network conditions.
Background
While investigating the Python SDK, I discovered that the
BackupCodecPolicy
enum exists at the protocol level, but the high-levelTrackPublishOptions
API doesn't expose it. The JavaScript SDK already has this feature via thebackupCodec
option inTrackPublishOptions
.Reference conversation with LiveKit team confirmed that:
TrackInfo
(protocol level) hasbackup_codec_policy
fieldBackupCodecPolicy
enum exists with values: PREFER_REGRESSION (0), SIMULCAST (1), REGRESSION (2)Changes
Files Modified
livekit-rtc/livekit/rtc/__init__.py
BackupCodecPolicy
enum fromlivekit.protocol.models
__all__
for public API accesslivekit-rtc/livekit/rtc/participant.py
LocalParticipant.publish_track()
with two new keyword-only parameters:backup_codec_policy: Optional[int]
- Accepts BackupCodecPolicy enum valuesbackup_codec: Optional[str]
- Accepts codec name (e.g., "vp8", "h264")Usage
Basic Example
Advanced Example
BackupCodecPolicy Values
PREFER_REGRESSION
(0) - Prefer codec regression over simulcast for backupSIMULCAST
(1) - Use simulcast for backup codecREGRESSION
(2) - Use codec regression for backupCompatibility
Backward Compatibility
✅ Fully backward compatible - All existing code continues to work without modifications. The new parameters are optional keyword-only arguments.
Forward Compatibility
✅ Graceful degradation - If FFI protocol doesn't support backup codec fields:
JavaScript SDK Parity
JavaScript:
Python (now):
Implementation Details
The implementation attempts to set backup codec fields on the protobuf message dynamically:
This approach ensures that:
Testing
Notes
FFI Protocol Dependency: The implementation assumes FFI protocol support for
backup_codec_policy
andbackup_codec
fields inTrackPublishOptions
. If these fields aren't present in the current protocol version, warnings will be logged and publishing continues without backup codec support.Checklist