| 1 | // Copyright 2025 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 | use crate::{enum_dispatch, imp::video_source as vs_imp}; |
| 16 | |
| 17 | #[derive(Debug, Clone)] |
| 18 | pub struct VideoResolution { |
| 19 | pub width: u32, |
| 20 | pub height: u32, |
| 21 | } |
| 22 | |
| 23 | /// Encoder rate-control target requested by WebRTC for a pre-encoded source. |
| 24 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 25 | pub struct EncodedRateControl { |
| 26 | /// Target bitrate in bits per second. |
| 27 | pub target_bitrate_bps: u64, |
| 28 | /// Target frame rate in frames per second. |
| 29 | pub framerate_fps: f64, |
| 30 | } |
| 31 | |
| 32 | impl Default for VideoResolution { |
| 33 | // Default to 720p |
| 34 | fn default() -> Self { |
| 35 | VideoResolution { width: 1280, height: 720 } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[non_exhaustive] |
| 40 | #[derive(Debug, Clone)] |
| 41 | pub enum RtcVideoSource { |
| 42 | // TODO(theomonnom): Web video sources (eq. to tracks on browsers?) |
| 43 | #[cfg(not(target_arch = "wasm32"))] |
| 44 | Native(native::NativeVideoSource), |
| 45 | } |
| 46 | |
| 47 | // TODO(theomonnom): Support enum dispatch with conditional compilation? |
| 48 | impl RtcVideoSource { |
| 49 | enum_dispatch!( |
| 50 | [Native]; |
| 51 | pub fn video_resolution(self: &Self) -> VideoResolution; |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | #[cfg(not(target_arch = "wasm32"))] |
| 56 | pub mod native { |
| 57 | use std::fmt::{Debug, Formatter}; |
| 58 | |
| 59 | use super::*; |
| 60 | use crate::native::packet_trailer::PacketTrailerHandler; |
| 61 | #[cfg(target_os = "linux")] |
| 62 | use crate::video_frame::{ |
| 63 | native::{DmaBufPixelFormat, NativeBuffer}, |
| 64 | FrameMetadata, VideoRotation, |
| 65 | }; |
| 66 | use crate::video_frame::{EncodedVideoFrame, VideoBuffer, VideoFrame}; |
| 67 | |
| 68 | #[derive(Clone)] |
| 69 | pub struct NativeVideoSource { |
| 70 | pub(crate) handle: vs_imp::NativeVideoSource, |
| 71 | } |
| 72 | |
| 73 | impl Debug for NativeVideoSource { |
| 74 | fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { |
| 75 | f.debug_struct("NativeVideoSource").finish() |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl Default for NativeVideoSource { |
| 80 | fn default() -> Self { |
| 81 | Self::new(VideoResolution::default(), false) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl NativeVideoSource { |
| 86 | pub fn new(resolution: VideoResolution, is_screencast: bool) -> Self { |
| 87 | Self { handle: vs_imp::NativeVideoSource::new(resolution, is_screencast) } |
| 88 | } |
| 89 | |
| 90 | /// Creates a source for pre-encoded access units: no raw black-frame |
| 91 | /// keepalive is injected before the first capture. |
| 92 | pub fn new_encoded(resolution: VideoResolution) -> Self { |
| 93 | Self { handle: vs_imp::NativeVideoSource::new_encoded(resolution) } |
| 94 | } |
| 95 | |
| 96 | pub fn capture_frame<T: AsRef<dyn VideoBuffer>>(&self, frame: &VideoFrame<T>) -> bool { |
| 97 | self.handle.capture_frame(frame) |
| 98 | } |
| 99 | |
| 100 | /// Captures a Jetson DMA-buffer backed video frame. |
| 101 | /// |
| 102 | /// `pixel_format` is `0` for NV12 and `1` for YUV420M. |
| 103 | #[cfg(target_os = "linux")] |
| 104 | #[deprecated( |
| 105 | note = "wrap the fd with `NativeBuffer::from_dmabuf` and use `capture_frame` instead" |
| 106 | )] |
| 107 | #[allow(deprecated)] |
| 108 | pub fn capture_dmabuf_frame( |
| 109 | &self, |
| 110 | dmabuf_fd: i32, |
| 111 | width: u32, |
| 112 | height: u32, |
| 113 | pixel_format: i32, |
| 114 | timestamp_us: i64, |
| 115 | ) -> bool { |
| 116 | self.capture_dmabuf_frame_with_metadata( |
| 117 | dmabuf_fd, |
| 118 | width, |
| 119 | height, |
| 120 | pixel_format, |
| 121 | timestamp_us, |
| 122 | None, |
| 123 | ) |
| 124 | } |
| 125 | |
| 126 | /// Captures a Jetson DMA-buffer backed video frame with packet trailer metadata. |
| 127 | /// |
| 128 | /// `pixel_format` is `0` for NV12 and `1` for YUV420M. |
| 129 | #[cfg(target_os = "linux")] |
| 130 | #[deprecated( |
| 131 | note = "wrap the fd with `NativeBuffer::from_dmabuf` and use `capture_frame` instead" |
| 132 | )] |
| 133 | pub fn capture_dmabuf_frame_with_metadata( |
| 134 | &self, |
| 135 | dmabuf_fd: i32, |
| 136 | width: u32, |
| 137 | height: u32, |
| 138 | pixel_format: i32, |
| 139 | timestamp_us: i64, |
| 140 | frame_metadata: Option<FrameMetadata>, |
| 141 | ) -> bool { |
| 142 | let pixel_format = if pixel_format == 0 { |
| 143 | DmaBufPixelFormat::NV12 |
| 144 | } else { |
| 145 | DmaBufPixelFormat::YUV420M |
| 146 | }; |
| 147 | self.handle.capture_frame(&VideoFrame { |
| 148 | rotation: VideoRotation::VideoRotation0, |
| 149 | timestamp_us, |
| 150 | frame_metadata, |
| 151 | buffer: NativeBuffer::from_dmabuf( |
| 152 | dmabuf_fd, |
| 153 | VideoResolution { width, height }, |
| 154 | pixel_format, |
| 155 | ), |
| 156 | }) |
| 157 | } |
| 158 | |
| 159 | /// Captures one pre-encoded video access unit. |
| 160 | pub fn capture_encoded_frame(&self, frame: &EncodedVideoFrame<'_>) -> bool { |
| 161 | self.handle.capture_encoded_frame(frame) |
| 162 | } |
| 163 | |
| 164 | /// Returns and clears the pending keyframe request raised by the |
| 165 | /// pass-through encoder (PLI/FIR or reconfiguration). |
| 166 | pub fn take_keyframe_request(&self) -> bool { |
| 167 | self.handle.take_keyframe_request() |
| 168 | } |
| 169 | |
| 170 | /// Returns and clears the pending rate-control target raised by the |
| 171 | /// pass-through encoder. |
| 172 | pub fn take_rate_control_request(&self) -> Option<EncodedRateControl> { |
| 173 | self.handle.take_rate_control_request() |
| 174 | } |
| 175 | |
| 176 | /// Set the packet trailer handler used by this source. |
| 177 | /// |
| 178 | /// When set, any frame captured with a `user_timestamp` value will |
| 179 | /// automatically have its timestamp stored in the handler (keyed by |
| 180 | /// the TimestampAligner-adjusted capture timestamp) so the |
| 181 | /// `PacketTrailerTransformer` can embed it into the encoded frame. |
| 182 | pub fn set_packet_trailer_handler(&self, handler: PacketTrailerHandler) { |
| 183 | self.handle.set_packet_trailer_handler(handler) |
| 184 | } |
| 185 | |
| 186 | pub fn video_resolution(&self) -> VideoResolution { |
| 187 | self.handle.video_resolution() |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | #[cfg(target_arch = "wasm32")] |
| 193 | pub mod web {} |