Skip to content
Draft
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/flexfec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit": minor
---

Update livekit-ffi to add FlexFEC support: `RemoteTrackPublication.set_fec_protection` requests repair packets for a subscribed video track, and `TrackPublishOptions.fec` sets the protection a published video track sends.
37 changes: 37 additions & 0 deletions examples/local_video/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ class FrameFormat(str, Enum):
I420 = "i420"


_FEC_MODES = {
"adaptive": rtc.FecProtectionMode.FEC_MODE_ADAPTIVE,
"fixed": rtc.FecProtectionMode.FEC_MODE_FIXED,
"minimum": rtc.FecProtectionMode.FEC_MODE_MINIMUM,
}
_FEC_LEVELS = {
"low": rtc.FecProtectionLevel.FEC_LEVEL_LOW,
"medium": rtc.FecProtectionLevel.FEC_LEVEL_MEDIUM,
"high": rtc.FecProtectionLevel.FEC_LEVEL_HIGH,
}
FEC_CHOICES = ["none", "adaptive"] + [
f"{mode}-{level}" for mode in ("fixed", "minimum") for level in _FEC_LEVELS
]


def _fec_protection(choice: str) -> rtc.FecProtection | None:
"""Map a --fec choice such as 'fixed-high' to an rtc.FecProtection."""
if choice == "none":
return None
mode, _, level = choice.partition("-")
protection = rtc.FecProtection(mode=_FEC_MODES[mode])
if level:
protection.level = _FEC_LEVELS[level]
return protection


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Publish a local camera track with optional frame metadata.",
Expand Down Expand Up @@ -61,6 +87,13 @@ def parse_args() -> argparse.Namespace:
choices=list(FrameFormat),
help="Frame format: 'i420' (default, efficient) or 'rgba' (original behavior)",
)
parser.add_argument(
"--fec",
choices=FEC_CHOICES,
default="none",
help="FlexFEC protection sent with the track; disables simulcast, "
"since simulcast tracks do not send FlexFEC",
)
return parser.parse_args()


Expand Down Expand Up @@ -263,6 +296,10 @@ async def run(args: argparse.Namespace, stop_event: asyncio.Event) -> None:
),
frame_metadata_features=_frame_metadata_features(args),
)
fec = _fec_protection(args.fec)
if fec is not None:
options.simulcast = False
options.fec.CopyFrom(fec)
publication = await room.local_participant.publish_track(track, options)
logging.info(
"published camera track %s with frame metadata features %s",
Expand Down
37 changes: 37 additions & 0 deletions examples/local_video/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ class SubscribedVideoTrack:
participant: rtc.RemoteParticipant


_FEC_MODES = {
"adaptive": rtc.FecProtectionMode.FEC_MODE_ADAPTIVE,
"fixed": rtc.FecProtectionMode.FEC_MODE_FIXED,
"minimum": rtc.FecProtectionMode.FEC_MODE_MINIMUM,
}
_FEC_LEVELS = {
"low": rtc.FecProtectionLevel.FEC_LEVEL_LOW,
"medium": rtc.FecProtectionLevel.FEC_LEVEL_MEDIUM,
"high": rtc.FecProtectionLevel.FEC_LEVEL_HIGH,
}
FEC_CHOICES = ["none", "adaptive"] + [
f"{mode}-{level}" for mode in ("fixed", "minimum") for level in _FEC_LEVELS
]


def _fec_protection(choice: str) -> rtc.FecProtection | None:
"""Map a --fec choice such as 'fixed-high' to an rtc.FecProtection."""
if choice == "none":
return None
mode, _, level = choice.partition("-")
protection = rtc.FecProtection(mode=_FEC_MODES[mode])
if level:
protection.level = _FEC_LEVELS[level]
return protection


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Subscribe to a LiveKit video track and optionally display frame metadata.",
Expand All @@ -55,6 +81,12 @@ def parse_args() -> argparse.Namespace:
action="store_true",
help="Overlay frame ID, publisher timestamp, receive timestamp, and latency",
)
parser.add_argument(
"--fec",
choices=FEC_CHOICES,
default="none",
help="FlexFEC protection to request from the server for the subscribed track",
)
return parser.parse_args()


Expand Down Expand Up @@ -265,6 +297,11 @@ def on_track_subscribed(
)
return

fec = _fec_protection(args.fec)
if fec is not None:
logging.info("requesting FEC protection %s for %s", args.fec, publication.sid)
publication.set_fec_protection(fec)

track_queue.put_nowait(
SubscribedVideoTrack(
track=track,
Expand Down
6 changes: 6 additions & 0 deletions livekit-rtc/livekit/rtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
ConnectionState,
ContinualGatheringPolicy,
DataPacketKind,
FecProtection,
FecProtectionLevel,
FecProtectionMode,
IceServer,
IceTransportType,
SimulateScenarioKind,
Expand Down Expand Up @@ -162,6 +165,9 @@
"DataPacketKind",
"DegradationPreference",
"TrackPublishOptions",
"FecProtection",
"FecProtectionLevel",
"FecProtectionMode",
"IceTransportType",
"ContinualGatheringPolicy",
"IceServer",
Expand Down
38 changes: 21 additions & 17 deletions livekit-rtc/livekit/rtc/_proto/capture_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading