| 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 std::fmt::Debug; |
| 16 | |
| 17 | use thiserror::Error; |
| 18 | |
| 19 | use crate::{imp::video_frame as vf_imp, video_source::VideoResolution}; |
| 20 | |
| 21 | #[derive(Debug, Error)] |
| 22 | pub enum SinkError { |
| 23 | #[error("platform error: {0}")] |
| 24 | Platform(String), |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 28 | pub enum VideoRotation { |
| 29 | VideoRotation0 = 0, |
| 30 | VideoRotation90 = 90, |
| 31 | VideoRotation180 = 180, |
| 32 | VideoRotation270 = 270, |
| 33 | } |
| 34 | |
| 35 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 36 | pub enum VideoFormatType { |
| 37 | ARGB, |
| 38 | BGRA, |
| 39 | ABGR, |
| 40 | RGBA, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 44 | #[non_exhaustive] |
| 45 | pub enum VideoBufferType { |
| 46 | Native, |
| 47 | I420, |
| 48 | I420A, |
| 49 | I422, |
| 50 | I444, |
| 51 | I010, |
| 52 | NV12, |
| 53 | } |
| 54 | |
| 55 | /// Metadata carried alongside a video frame via the packet trailer mechanism. |
| 56 | /// |
| 57 | /// Each field corresponds to an independently negotiable packet trailer feature |
| 58 | /// (`PTF_USER_TIMESTAMP`, `PTF_FRAME_ID`, `PTF_USER_DATA`), so individual fields |
| 59 | /// are `Option`. |
| 60 | #[derive(Debug, Clone)] |
| 61 | pub struct FrameMetadata { |
| 62 | /// Wall-clock capture time in microseconds, when `PTF_USER_TIMESTAMP` is enabled. |
| 63 | pub user_timestamp: Option<u64>, |
| 64 | /// Monotonically increasing frame identifier, when `PTF_FRAME_ID` is enabled. |
| 65 | pub frame_id: Option<u32>, |
| 66 | /// Arbitrary application-supplied bytes, when `PTF_USER_DATA` is enabled. |
| 67 | /// |
| 68 | /// Bounded by the packet trailer size budget (~232 bytes when the other |
| 69 | /// features are also active); oversize payloads are dropped on the send |
| 70 | /// side rather than truncated. |
| 71 | pub user_data: Option<Vec<u8>>, |
| 72 | } |
| 73 | |
| 74 | /// Codec carried by a pre-encoded video access unit. |
| 75 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 76 | #[non_exhaustive] |
| 77 | pub enum EncodedVideoCodec { |
| 78 | /// H.264/AVC video. |
| 79 | H264, |
| 80 | /// H.265/HEVC video. |
| 81 | H265, |
| 82 | /// VP8 video. |
| 83 | VP8, |
| 84 | /// VP9 video. |
| 85 | VP9, |
| 86 | /// AV1 video. |
| 87 | AV1, |
| 88 | } |
| 89 | |
| 90 | /// Frame type of a pre-encoded video access unit. |
| 91 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 92 | pub enum EncodedFrameType { |
| 93 | /// A key frame. |
| 94 | Key, |
| 95 | /// A delta frame. |
| 96 | Delta, |
| 97 | } |
| 98 | |
| 99 | /// A pre-encoded video access unit ready for passthrough publishing. |
| 100 | #[derive(Debug, Clone)] |
| 101 | pub struct EncodedVideoFrame<'a> { |
| 102 | /// Encoded video codec. |
| 103 | pub codec: EncodedVideoCodec, |
| 104 | /// Encoded access-unit payload. |
| 105 | pub payload: &'a [u8], |
| 106 | /// Capture timestamp in microseconds. |
| 107 | pub timestamp_us: i64, |
| 108 | /// Encoded frame type. |
| 109 | pub frame_type: EncodedFrameType, |
| 110 | /// Encoded frame resolution in pixels. |
| 111 | pub resolution: VideoResolution, |
| 112 | /// Optional metadata to attach through packet trailers. |
| 113 | pub frame_metadata: Option<FrameMetadata>, |
| 114 | } |
| 115 | |
| 116 | #[cfg(not(target_arch = "wasm32"))] |
| 117 | impl From<EncodedVideoCodec> for webrtc_sys::video_track::ffi::EncodedVideoCodec { |
| 118 | fn from(value: EncodedVideoCodec) -> Self { |
| 119 | match value { |
| 120 | EncodedVideoCodec::H264 => Self::H264, |
| 121 | EncodedVideoCodec::H265 => Self::H265, |
| 122 | EncodedVideoCodec::VP8 => Self::VP8, |
| 123 | EncodedVideoCodec::VP9 => Self::VP9, |
| 124 | EncodedVideoCodec::AV1 => Self::AV1, |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | #[cfg(not(target_arch = "wasm32"))] |
| 130 | impl From<EncodedFrameType> for webrtc_sys::video_track::ffi::EncodedFrameType { |
| 131 | fn from(value: EncodedFrameType) -> Self { |
| 132 | match value { |
| 133 | EncodedFrameType::Key => Self::Key, |
| 134 | EncodedFrameType::Delta => Self::Delta, |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[derive(Debug)] |
| 140 | pub struct VideoFrame<T> |
| 141 | where |
| 142 | T: AsRef<dyn VideoBuffer>, |
| 143 | { |
| 144 | pub rotation: VideoRotation, |
| 145 | pub timestamp_us: i64, // When the frame was captured in microseconds |
| 146 | /// Packet-trailer metadata, if any trailer features are active. |
| 147 | pub frame_metadata: Option<FrameMetadata>, |
| 148 | pub buffer: T, |
| 149 | } |
| 150 | |
| 151 | impl<T: AsRef<dyn VideoBuffer>> VideoFrame<T> { |
| 152 | pub fn new(rotation: VideoRotation, buffer: T) -> Self { |
| 153 | Self { rotation, timestamp_us: 0, frame_metadata: None, buffer } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | pub type BoxVideoBuffer = Box<dyn VideoBuffer>; |
| 158 | pub type BoxVideoFrame = VideoFrame<BoxVideoBuffer>; |
| 159 | |
| 160 | pub(crate) mod internal { |
| 161 | use super::{I420Buffer, VideoFormatType}; |
| 162 | |
| 163 | pub trait BufferSealed: Send + Sync { |
| 164 | #[cfg(not(target_arch = "wasm32"))] |
| 165 | fn sys_handle(&self) -> &webrtc_sys::video_frame_buffer::ffi::VideoFrameBuffer; |
| 166 | |
| 167 | #[cfg(not(target_arch = "wasm32"))] |
| 168 | fn to_i420(&self) -> I420Buffer; |
| 169 | |
| 170 | #[cfg(not(target_arch = "wasm32"))] |
| 171 | fn to_argb( |
| 172 | &self, |
| 173 | format: VideoFormatType, |
| 174 | dst: &mut [u8], |
| 175 | dst_stride: u32, |
| 176 | dst_width: i32, |
| 177 | dst_height: i32, |
| 178 | ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | pub trait VideoBuffer: internal::BufferSealed + Debug { |
| 183 | fn width(&self) -> u32; |
| 184 | fn height(&self) -> u32; |
| 185 | fn buffer_type(&self) -> VideoBufferType; |
| 186 | |
| 187 | #[cfg(not(target_arch = "wasm32"))] |
| 188 | fn as_native(&self) -> Option<&native::NativeBuffer> { |
| 189 | None |
| 190 | } |
| 191 | |
| 192 | fn as_i420(&self) -> Option<&I420Buffer> { |
| 193 | None |
| 194 | } |
| 195 | |
| 196 | fn as_i420a(&self) -> Option<&I420ABuffer> { |
| 197 | None |
| 198 | } |
| 199 | |
| 200 | fn as_i422(&self) -> Option<&I422Buffer> { |
| 201 | None |
| 202 | } |
| 203 | |
| 204 | fn as_i444(&self) -> Option<&I444Buffer> { |
| 205 | None |
| 206 | } |
| 207 | |
| 208 | fn as_i010(&self) -> Option<&I010Buffer> { |
| 209 | None |
| 210 | } |
| 211 | |
| 212 | fn as_nv12(&self) -> Option<&NV12Buffer> { |
| 213 | None |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | macro_rules! new_buffer_type { |
| 218 | ($type:ident, $variant:ident, $as:ident) => { |
| 219 | pub struct $type { |
| 220 | pub(crate) handle: vf_imp::$type, |
| 221 | } |
| 222 | |
| 223 | impl $crate::video_frame::internal::BufferSealed for $type { |
| 224 | #[cfg(not(target_arch = "wasm32"))] |
| 225 | fn sys_handle(&self) -> &webrtc_sys::video_frame_buffer::ffi::VideoFrameBuffer { |
| 226 | self.handle.sys_handle() |
| 227 | } |
| 228 | |
| 229 | #[cfg(not(target_arch = "wasm32"))] |
| 230 | fn to_i420(&self) -> I420Buffer { |
| 231 | I420Buffer { handle: self.handle.to_i420() } |
| 232 | } |
| 233 | |
| 234 | #[cfg(not(target_arch = "wasm32"))] |
| 235 | fn to_argb( |
| 236 | &self, |
| 237 | format: VideoFormatType, |
| 238 | dst: &mut [u8], |
| 239 | stride: u32, |
| 240 | width: i32, |
| 241 | height: i32, |
| 242 | ) { |
| 243 | self.handle.to_argb(format, dst, stride, width, height) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | impl VideoBuffer for $type { |
| 248 | fn width(&self) -> u32 { |
| 249 | self.handle.width() |
| 250 | } |
| 251 | |
| 252 | fn height(&self) -> u32 { |
| 253 | self.handle.height() |
| 254 | } |
| 255 | |
| 256 | fn buffer_type(&self) -> VideoBufferType { |
| 257 | VideoBufferType::$variant |
| 258 | } |
| 259 | |
| 260 | fn $as(&self) -> Option<&$type> { |
| 261 | Some(self) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | impl Debug for $type { |
| 266 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 267 | f.debug_struct(stringify!($type)) |
| 268 | .field("width", &self.width()) |
| 269 | .field("height", &self.height()) |
| 270 | .finish() |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | impl AsRef<dyn VideoBuffer> for $type { |
| 275 | fn as_ref(&self) -> &(dyn VideoBuffer + 'static) { |
| 276 | self |
| 277 | } |
| 278 | } |
| 279 | }; |
| 280 | } |
| 281 | |
| 282 | new_buffer_type!(I420Buffer, I420, as_i420); |
| 283 | new_buffer_type!(I420ABuffer, I420A, as_i420a); |
| 284 | new_buffer_type!(I422Buffer, I422, as_i422); |
| 285 | new_buffer_type!(I444Buffer, I444, as_i444); |
| 286 | new_buffer_type!(I010Buffer, I010, as_i010); |
| 287 | new_buffer_type!(NV12Buffer, NV12, as_nv12); |
| 288 | |
| 289 | impl I420Buffer { |
| 290 | pub fn with_strides( |
| 291 | width: u32, |
| 292 | height: u32, |
| 293 | stride_y: u32, |
| 294 | stride_u: u32, |
| 295 | stride_v: u32, |
| 296 | ) -> I420Buffer { |
| 297 | vf_imp::I420Buffer::new(width, height, stride_y, stride_u, stride_v) |
| 298 | } |
| 299 | |
| 300 | pub fn new(width: u32, height: u32) -> I420Buffer { |
| 301 | Self::with_strides(width, height, width, (width + 1) / 2, (width + 1) / 2) |
| 302 | } |
| 303 | |
| 304 | /// Like [`I420Buffer::new`], but with the pixel data initialized to black |
| 305 | /// (Y=0, U=V=128) instead of left uninitialized. |
| 306 | pub fn new_black(width: u32, height: u32) -> I420Buffer { |
| 307 | vf_imp::I420Buffer::new_black(width, height, width, (width + 1) / 2, (width + 1) / 2) |
| 308 | } |
| 309 | |
| 310 | pub fn chroma_width(&self) -> u32 { |
| 311 | self.handle.chroma_width() |
| 312 | } |
| 313 | |
| 314 | pub fn chroma_height(&self) -> u32 { |
| 315 | self.handle.chroma_height() |
| 316 | } |
| 317 | |
| 318 | pub fn strides(&self) -> (u32, u32, u32) { |
| 319 | (self.handle.stride_y(), self.handle.stride_u(), self.handle.stride_v()) |
| 320 | } |
| 321 | |
| 322 | pub fn data(&self) -> (&[u8], &[u8], &[u8]) { |
| 323 | self.handle.data() |
| 324 | } |
| 325 | |
| 326 | pub fn data_mut(&mut self) -> (&mut [u8], &mut [u8], &mut [u8]) { |
| 327 | let (data_y, data_u, data_v) = self.handle.data(); |
| 328 | unsafe { |
| 329 | ( |
| 330 | std::slice::from_raw_parts_mut(data_y.as_ptr() as *mut u8, data_y.len()), |
| 331 | std::slice::from_raw_parts_mut(data_u.as_ptr() as *mut u8, data_u.len()), |
| 332 | std::slice::from_raw_parts_mut(data_v.as_ptr() as *mut u8, data_v.len()), |
| 333 | ) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> I420Buffer { |
| 338 | self.handle.scale(scaled_width, scaled_height) |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | impl I420ABuffer { |
| 343 | pub fn chroma_width(&self) -> u32 { |
| 344 | self.handle.chroma_width() |
| 345 | } |
| 346 | |
| 347 | pub fn chroma_height(&self) -> u32 { |
| 348 | self.handle.chroma_height() |
| 349 | } |
| 350 | |
| 351 | pub fn strides(&self) -> (u32, u32, u32, u32) { |
| 352 | ( |
| 353 | self.handle.stride_y(), |
| 354 | self.handle.stride_u(), |
| 355 | self.handle.stride_v(), |
| 356 | self.handle.stride_a(), |
| 357 | ) |
| 358 | } |
| 359 | |
| 360 | #[allow(clippy::type_complexity)] |
| 361 | pub fn data(&self) -> (&[u8], &[u8], &[u8], Option<&[u8]>) { |
| 362 | self.handle.data() |
| 363 | } |
| 364 | |
| 365 | #[allow(clippy::type_complexity)] |
| 366 | pub fn data_mut(&self) -> (&mut [u8], &mut [u8], &mut [u8], Option<&mut [u8]>) { |
| 367 | let (data_y, data_u, data_v, data_a) = self.handle.data(); |
| 368 | unsafe { |
| 369 | ( |
| 370 | std::slice::from_raw_parts_mut(data_y.as_ptr() as *mut u8, data_y.len()), |
| 371 | std::slice::from_raw_parts_mut(data_u.as_ptr() as *mut u8, data_u.len()), |
| 372 | std::slice::from_raw_parts_mut(data_v.as_ptr() as *mut u8, data_v.len()), |
| 373 | data_a.map(|data_a| { |
| 374 | std::slice::from_raw_parts_mut(data_a.as_ptr() as *mut u8, data_a.len()) |
| 375 | }), |
| 376 | ) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> I420ABuffer { |
| 381 | self.handle.scale(scaled_width, scaled_height) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | impl I422Buffer { |
| 386 | pub fn with_strides( |
| 387 | width: u32, |
| 388 | height: u32, |
| 389 | stride_y: u32, |
| 390 | stride_u: u32, |
| 391 | stride_v: u32, |
| 392 | ) -> I422Buffer { |
| 393 | vf_imp::I422Buffer::new(width, height, stride_y, stride_u, stride_v) |
| 394 | } |
| 395 | |
| 396 | pub fn new(width: u32, height: u32) -> I422Buffer { |
| 397 | Self::with_strides(width, height, width, (width + 1) / 2, (width + 1) / 2) |
| 398 | } |
| 399 | |
| 400 | pub fn chroma_width(&self) -> u32 { |
| 401 | self.handle.chroma_width() |
| 402 | } |
| 403 | |
| 404 | pub fn chroma_height(&self) -> u32 { |
| 405 | self.handle.chroma_height() |
| 406 | } |
| 407 | |
| 408 | pub fn strides(&self) -> (u32, u32, u32) { |
| 409 | (self.handle.stride_y(), self.handle.stride_u(), self.handle.stride_v()) |
| 410 | } |
| 411 | |
| 412 | pub fn data(&self) -> (&[u8], &[u8], &[u8]) { |
| 413 | self.handle.data() |
| 414 | } |
| 415 | |
| 416 | pub fn data_mut(&mut self) -> (&mut [u8], &mut [u8], &mut [u8]) { |
| 417 | let (data_y, data_u, data_v) = self.handle.data(); |
| 418 | unsafe { |
| 419 | ( |
| 420 | std::slice::from_raw_parts_mut(data_y.as_ptr() as *mut u8, data_y.len()), |
| 421 | std::slice::from_raw_parts_mut(data_u.as_ptr() as *mut u8, data_u.len()), |
| 422 | std::slice::from_raw_parts_mut(data_v.as_ptr() as *mut u8, data_v.len()), |
| 423 | ) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> I422Buffer { |
| 428 | self.handle.scale(scaled_width, scaled_height) |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | impl I444Buffer { |
| 433 | pub fn with_strides( |
| 434 | width: u32, |
| 435 | height: u32, |
| 436 | stride_y: u32, |
| 437 | stride_u: u32, |
| 438 | stride_v: u32, |
| 439 | ) -> I444Buffer { |
| 440 | vf_imp::I444Buffer::new(width, height, stride_y, stride_u, stride_v) |
| 441 | } |
| 442 | |
| 443 | pub fn new(width: u32, height: u32) -> I444Buffer { |
| 444 | Self::with_strides(width, height, width, width, width) |
| 445 | } |
| 446 | |
| 447 | pub fn chroma_width(&self) -> u32 { |
| 448 | self.handle.chroma_width() |
| 449 | } |
| 450 | |
| 451 | pub fn chroma_height(&self) -> u32 { |
| 452 | self.handle.chroma_height() |
| 453 | } |
| 454 | |
| 455 | pub fn strides(&self) -> (u32, u32, u32) { |
| 456 | (self.handle.stride_y(), self.handle.stride_u(), self.handle.stride_v()) |
| 457 | } |
| 458 | |
| 459 | pub fn data(&self) -> (&[u8], &[u8], &[u8]) { |
| 460 | self.handle.data() |
| 461 | } |
| 462 | |
| 463 | pub fn data_mut(&mut self) -> (&mut [u8], &mut [u8], &mut [u8]) { |
| 464 | let (data_y, data_u, data_v) = self.handle.data(); |
| 465 | unsafe { |
| 466 | ( |
| 467 | std::slice::from_raw_parts_mut(data_y.as_ptr() as *mut u8, data_y.len()), |
| 468 | std::slice::from_raw_parts_mut(data_u.as_ptr() as *mut u8, data_u.len()), |
| 469 | std::slice::from_raw_parts_mut(data_v.as_ptr() as *mut u8, data_v.len()), |
| 470 | ) |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> I444Buffer { |
| 475 | self.handle.scale(scaled_width, scaled_height) |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | impl I010Buffer { |
| 480 | pub fn with_strides( |
| 481 | width: u32, |
| 482 | height: u32, |
| 483 | stride_y: u32, |
| 484 | stride_u: u32, |
| 485 | stride_v: u32, |
| 486 | ) -> I010Buffer { |
| 487 | vf_imp::I010Buffer::new(width, height, stride_y, stride_u, stride_v) |
| 488 | } |
| 489 | |
| 490 | pub fn new(width: u32, height: u32) -> I010Buffer { |
| 491 | Self::with_strides(width, height, width, (width + 1) / 2, (width + 1) / 2) |
| 492 | } |
| 493 | |
| 494 | pub fn chroma_width(&self) -> u32 { |
| 495 | self.handle.chroma_width() |
| 496 | } |
| 497 | |
| 498 | pub fn chroma_height(&self) -> u32 { |
| 499 | self.handle.chroma_height() |
| 500 | } |
| 501 | |
| 502 | pub fn strides(&self) -> (u32, u32, u32) { |
| 503 | (self.handle.stride_y(), self.handle.stride_u(), self.handle.stride_v()) |
| 504 | } |
| 505 | |
| 506 | pub fn data(&self) -> (&[u16], &[u16], &[u16]) { |
| 507 | self.handle.data() |
| 508 | } |
| 509 | |
| 510 | pub fn data_mut(&mut self) -> (&mut [u16], &mut [u16], &mut [u16]) { |
| 511 | let (data_y, data_u, data_v) = self.handle.data(); |
| 512 | unsafe { |
| 513 | ( |
| 514 | std::slice::from_raw_parts_mut(data_y.as_ptr() as *mut u16, data_y.len()), |
| 515 | std::slice::from_raw_parts_mut(data_u.as_ptr() as *mut u16, data_u.len()), |
| 516 | std::slice::from_raw_parts_mut(data_v.as_ptr() as *mut u16, data_v.len()), |
| 517 | ) |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> I010Buffer { |
| 522 | self.handle.scale(scaled_width, scaled_height) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | impl NV12Buffer { |
| 527 | pub fn with_strides(width: u32, height: u32, stride_y: u32, stride_uv: u32) -> NV12Buffer { |
| 528 | vf_imp::NV12Buffer::new(width, height, stride_y, stride_uv) |
| 529 | } |
| 530 | |
| 531 | pub fn new(width: u32, height: u32) -> NV12Buffer { |
| 532 | Self::with_strides(width, height, width, width + width % 2) |
| 533 | } |
| 534 | |
| 535 | pub fn chroma_width(&self) -> u32 { |
| 536 | self.handle.chroma_width() |
| 537 | } |
| 538 | |
| 539 | pub fn chroma_height(&self) -> u32 { |
| 540 | self.handle.chroma_height() |
| 541 | } |
| 542 | |
| 543 | pub fn strides(&self) -> (u32, u32) { |
| 544 | (self.handle.stride_y(), self.handle.stride_uv()) |
| 545 | } |
| 546 | |
| 547 | pub fn data(&self) -> (&[u8], &[u8]) { |
| 548 | self.handle.data() |
| 549 | } |
| 550 | |
| 551 | pub fn data_mut(&mut self) -> (&mut [u8], &mut [u8]) { |
| 552 | let (data_y, data_uv) = self.handle.data(); |
| 553 | unsafe { |
| 554 | ( |
| 555 | std::slice::from_raw_parts_mut(data_y.as_ptr() as *mut u8, data_y.len()), |
| 556 | std::slice::from_raw_parts_mut(data_uv.as_ptr() as *mut u8, data_uv.len()), |
| 557 | ) |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | pub fn scale(&mut self, scaled_width: i32, scaled_height: i32) -> NV12Buffer { |
| 562 | self.handle.scale(scaled_width, scaled_height) |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | #[cfg(not(target_arch = "wasm32"))] |
| 567 | pub mod native { |
| 568 | use std::fmt::Debug; |
| 569 | |
| 570 | use super::{vf_imp, I420Buffer, VideoBuffer, VideoBufferType, VideoFormatType}; |
| 571 | #[cfg(target_os = "linux")] |
| 572 | use crate::video_source::VideoResolution; |
| 573 | |
| 574 | new_buffer_type!(NativeBuffer, Native, as_native); |
| 575 | |
| 576 | /// A borrowed DMA-BUF file descriptor. |
| 577 | /// |
| 578 | /// This is a plain [`RawFd`](std::os::fd::RawFd) (a C `int`, hence `i32`): |
| 579 | /// APIs taking this type borrow the descriptor rather than own it, so the |
| 580 | /// caller remains responsible for keeping it valid and closing it. |
| 581 | #[cfg(target_os = "linux")] |
| 582 | pub type DmaBufFileDescriptor = std::os::fd::RawFd; |
| 583 | |
| 584 | /// Pixel format of a DMA-BUF backed video buffer. |
| 585 | #[cfg(target_os = "linux")] |
| 586 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 587 | #[non_exhaustive] |
| 588 | pub enum DmaBufPixelFormat { |
| 589 | NV12 = 0, |
| 590 | YUV420M = 1, |
| 591 | } |
| 592 | |
| 593 | impl NativeBuffer { |
| 594 | /// Creates a `NativeBuffer` from a `CVPixelBufferRef` pointer. |
| 595 | /// |
| 596 | /// This function does not bump the reference count of the pixel buffer. |
| 597 | /// |
| 598 | /// Safety: The given pointer must be a valid `CVPixelBufferRef`. |
| 599 | #[cfg(any(target_os = "macos", target_os = "ios"))] |
| 600 | pub unsafe fn from_cv_pixel_buffer(cv_pixel_buffer: *mut std::ffi::c_void) -> Self { |
| 601 | vf_imp::NativeBuffer::from_cv_pixel_buffer(cv_pixel_buffer) |
| 602 | } |
| 603 | |
| 604 | /// Returns the `CVPixelBufferRef` that backs this buffer, or `null` if |
| 605 | /// this buffer is not currently backed by a `CVPixelBufferRef`. |
| 606 | /// |
| 607 | /// This function does not bump the reference count of the pixel buffer. |
| 608 | #[cfg(any(target_os = "macos", target_os = "ios"))] |
| 609 | pub fn get_cv_pixel_buffer(&self) -> *mut std::ffi::c_void { |
| 610 | self.handle.get_cv_pixel_buffer() |
| 611 | } |
| 612 | |
| 613 | /// Creates a `NativeBuffer` backed by a DMA-BUF file descriptor |
| 614 | /// (Jetson `NvBufSurface`), for zero-copy hand-off to the hardware |
| 615 | /// encoder. |
| 616 | /// |
| 617 | /// This function does not take ownership of the fd: the caller must |
| 618 | /// keep it valid until every frame captured from it has been consumed |
| 619 | /// by the encoder. |
| 620 | #[cfg(target_os = "linux")] |
| 621 | pub fn from_dmabuf( |
| 622 | dmabuf_fd: DmaBufFileDescriptor, |
| 623 | resolution: VideoResolution, |
| 624 | pixel_format: DmaBufPixelFormat, |
| 625 | ) -> Self { |
| 626 | vf_imp::NativeBuffer::from_dmabuf(dmabuf_fd, resolution, pixel_format) |
| 627 | } |
| 628 | |
| 629 | /// Returns the DMA-BUF fd that backs this buffer, or `None` if this |
| 630 | /// buffer is not DMA-BUF backed. |
| 631 | /// |
| 632 | /// This function does not transfer ownership of the fd. |
| 633 | #[cfg(target_os = "linux")] |
| 634 | pub fn get_dmabuf_fd(&self) -> Option<DmaBufFileDescriptor> { |
| 635 | self.handle.get_dmabuf_fd() |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | pub trait VideoFrameBufferExt: VideoBuffer { |
| 640 | fn to_i420(&self) -> I420Buffer; |
| 641 | fn to_argb( |
| 642 | &self, |
| 643 | format: VideoFormatType, |
| 644 | dst: &mut [u8], |
| 645 | dst_stride: u32, |
| 646 | dst_width: i32, |
| 647 | dst_height: i32, |
| 648 | ); |
| 649 | } |
| 650 | |
| 651 | impl<T: VideoBuffer> VideoFrameBufferExt for T { |
| 652 | fn to_i420(&self) -> I420Buffer { |
| 653 | self.to_i420() |
| 654 | } |
| 655 | |
| 656 | fn to_argb( |
| 657 | &self, |
| 658 | format: VideoFormatType, |
| 659 | dst: &mut [u8], |
| 660 | dst_stride: u32, |
| 661 | dst_width: i32, |
| 662 | dst_height: i32, |
| 663 | ) { |
| 664 | self.to_argb(format, dst, dst_stride, dst_width, dst_height) |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | #[cfg(target_arch = "wasm32")] |
| 670 | pub mod web { |
| 671 | use super::VideoFrameBuffer; |
| 672 | |
| 673 | #[derive(Debug)] |
| 674 | pub struct WebGlBuffer {} |
| 675 | |
| 676 | impl VideoFrameBuffer for WebGlBuffer {} |
| 677 | } |
| 678 | |
| 679 | #[cfg(all(test, not(target_arch = "wasm32")))] |
| 680 | mod tests { |
| 681 | use super::I420Buffer; |
| 682 | |
| 683 | #[test] |
| 684 | fn new_black_initializes_every_plane() { |
| 685 | let buffer = I420Buffer::new_black(64, 32); |
| 686 | let (data_y, data_u, data_v) = buffer.data(); |
| 687 | assert!(data_y.iter().all(|&px| px == 0)); |
| 688 | assert!(data_u.iter().all(|&px| px == 128)); |
| 689 | assert!(data_v.iter().all(|&px| px == 128)); |
| 690 | } |
| 691 | } |