|
| 1 | +# Copyright 2026 LiveKit, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from dataclasses import dataclass |
| 18 | + |
| 19 | +from ._ffi_client import FfiClient, FfiHandle |
| 20 | +from ._proto import ffi_pb2 as proto_ffi |
| 21 | +from ._proto import video_frame_pb2 as proto_video |
| 22 | +from ._utils import get_address |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(frozen=True) |
| 26 | +class EncodedVideoFrame: |
| 27 | + """One complete encoded access unit, without a container or RTP headers. |
| 28 | +
|
| 29 | + ``data`` must contain a single frame in ``codec`` (Annex B for H.264/H.265). |
| 30 | + ``timestamp_us`` is its capture time in microseconds, increasing along the |
| 31 | + stream. The codec must match the publication and remain fixed for its lifetime. |
| 32 | + """ |
| 33 | + |
| 34 | + data: bytes |
| 35 | + width: int |
| 36 | + height: int |
| 37 | + codec: proto_video.VideoCodec.ValueType |
| 38 | + frame_type: proto_video.EncodedFrameType.ValueType |
| 39 | + timestamp_us: int |
| 40 | + metadata: proto_video.FrameMetadata | None = None |
| 41 | + |
| 42 | + def __post_init__(self) -> None: |
| 43 | + if not isinstance(self.data, bytes): |
| 44 | + raise TypeError("encoded frame data must be bytes") |
| 45 | + if not self.data: |
| 46 | + raise ValueError("encoded frame data must not be empty") |
| 47 | + if self.width <= 0 or self.height <= 0: |
| 48 | + raise ValueError("encoded frame dimensions must be positive") |
| 49 | + |
| 50 | + |
| 51 | +@dataclass(frozen=True) |
| 52 | +class EncodedRateControl: |
| 53 | + """The latest bitrate and frame-rate targets requested by WebRTC.""" |
| 54 | + |
| 55 | + target_bitrate_bps: int |
| 56 | + framerate_fps: float |
| 57 | + |
| 58 | + |
| 59 | +@dataclass(frozen=True) |
| 60 | +class EncodedVideoSourceFeedback: |
| 61 | + """Encoder feedback consumed since the previous call to ``take_feedback``.""" |
| 62 | + |
| 63 | + keyframe_requested: bool |
| 64 | + rate_control: EncodedRateControl | None |
| 65 | + |
| 66 | + |
| 67 | +class EncodedVideoSource: |
| 68 | + """Publish pre-encoded video without decoding or re-encoding it. |
| 69 | +
|
| 70 | + Create a ``LocalVideoTrack`` with this source and publish it with a matching |
| 71 | + ``video_codec``, ``video_encoder=ENCODER_BACKEND_PRE_ENCODED`` and |
| 72 | + ``simulcast=False``. Feed complete access units at the intended playback rate; |
| 73 | + this source does not pace or buffer a file for playback. |
| 74 | +
|
| 75 | + Poll ``take_feedback`` and forward keyframe and rate-control requests to the |
| 76 | + upstream encoder. A demuxer alone cannot produce a new keyframe or adapt the |
| 77 | + encoded bitrate. Call ``capture_frame`` from a single producer. |
| 78 | + """ |
| 79 | + |
| 80 | + def __init__(self, width: int, height: int) -> None: |
| 81 | + """Create a source with the initial encoded frame dimensions.""" |
| 82 | + if width <= 0 or height <= 0: |
| 83 | + raise ValueError("encoded source dimensions must be positive") |
| 84 | + req = proto_ffi.FfiRequest() |
| 85 | + req.new_video_source.type = proto_video.VideoSourceType.VIDEO_SOURCE_ENCODED |
| 86 | + req.new_video_source.resolution.width = width |
| 87 | + req.new_video_source.resolution.height = height |
| 88 | + resp = FfiClient.instance.request(req) |
| 89 | + self._ffi_handle = FfiHandle(resp.new_video_source.source.handle.id) |
| 90 | + |
| 91 | + def capture_frame(self, frame: EncodedVideoFrame) -> bool: |
| 92 | + """Submit a frame; return whether the native source accepted it. |
| 93 | +
|
| 94 | + The native implementation copies the payload before this call returns. |
| 95 | + Acceptance does not guarantee delivery to subscribers. |
| 96 | + """ |
| 97 | + req = proto_ffi.FfiRequest() |
| 98 | + capture = req.capture_encoded_video_frame |
| 99 | + capture.source_handle = self._ffi_handle.handle |
| 100 | + capture.buffer.data_ptr = get_address(frame.data) |
| 101 | + capture.buffer.data_len = len(frame.data) |
| 102 | + capture.codec = frame.codec |
| 103 | + capture.frame_type = frame.frame_type |
| 104 | + capture.width = frame.width |
| 105 | + capture.height = frame.height |
| 106 | + capture.timestamp_us = frame.timestamp_us |
| 107 | + if frame.metadata is not None: |
| 108 | + capture.metadata.CopyFrom(frame.metadata) |
| 109 | + response: proto_ffi.FfiResponse = FfiClient.instance.request(req) |
| 110 | + return response.capture_encoded_video_frame.accepted |
| 111 | + |
| 112 | + def take_feedback(self) -> EncodedVideoSourceFeedback: |
| 113 | + """Consume pending feedback, including requests from late subscribers.""" |
| 114 | + req = proto_ffi.FfiRequest() |
| 115 | + req.take_encoded_video_source_feedback.source_handle = self._ffi_handle.handle |
| 116 | + feedback = FfiClient.instance.request(req).take_encoded_video_source_feedback |
| 117 | + rate_control = None |
| 118 | + if feedback.HasField("rate_control"): |
| 119 | + rate_control = EncodedRateControl( |
| 120 | + target_bitrate_bps=feedback.rate_control.target_bitrate_bps, |
| 121 | + framerate_fps=feedback.rate_control.framerate_fps, |
| 122 | + ) |
| 123 | + return EncodedVideoSourceFeedback(feedback.keyframe_requested, rate_control) |
| 124 | + |
| 125 | + async def aclose(self) -> None: |
| 126 | + """Release the native source handle.""" |
| 127 | + self._ffi_handle.dispose() |
0 commit comments