livekit-utils crate + VideoRenderer proto

This commit is contained in:
Théo Monnom
2022-12-02 16:39:42 +01:00
parent 59e7ccbffe
commit 686f5db969
30 changed files with 2208 additions and 87 deletions
+1
View File
@@ -9,3 +9,4 @@ pub mod rtp_transceiver;
pub mod video_frame;
pub mod video_frame_buffer;
pub mod webrtc;
pub mod yuv_helper;
@@ -1,5 +1,6 @@
use cxx::UniquePtr;
use libwebrtc_sys::video_frame_buffer as vfb_sys;
use livekit_utils::enum_dispatch;
use std::pin::Pin;
use std::slice;
use vfb_sys::ffi::VideoFrameBufferType;
@@ -53,6 +54,15 @@ impl VideoFrameBuffer {
}
}
impl VideoFrameBufferTrait for VideoFrameBuffer {
enum_dispatch!(
[Native, I420, I420A, I422, I444, I010, NV12]
fnc!(width, &Self, [], i32);
fnc!(height, &Self, [], i32);
fnc!(to_i420, Self, [], I420Buffer);
);
}
macro_rules! recursive_cast {
($ptr:expr $(, $fnc:ident)*) => {
{
@@ -146,21 +156,23 @@ macro_rules! impl_yuv8_buffer {
fn data_y(&self) -> &[u8] {
let ptr = recursive_cast!(&*self.cxx_handle $(, $cast)*);
unsafe {
slice::from_raw_parts((*ptr).data_y(), self.stride_y().try_into().unwrap())
slice::from_raw_parts((*ptr).data_y(), (self.width() * self.height()) as usize)
}
}
fn data_u(&self) -> &[u8] {
let ptr = recursive_cast!(&*self.cxx_handle $(, $cast)*);
unsafe {
slice::from_raw_parts((*ptr).data_u(), self.stride_u().try_into().unwrap())
let chroma_height = (self.height() + 1) / 2;
slice::from_raw_parts((*ptr).data_u(), (self.stride_u() * chroma_height) as usize)
}
}
fn data_v(&self) -> &[u8] {
let ptr = recursive_cast!(&*self.cxx_handle $(, $cast)*);
unsafe {
slice::from_raw_parts((*ptr).data_v(), self.stride_v().try_into().unwrap())
let chroma_height = (self.height() + 1) / 2;
slice::from_raw_parts((*ptr).data_v(), (self.stride_v() * chroma_height) as usize)
}
}
}
+43
View File
@@ -0,0 +1,43 @@
use std::convert::TryInto;
use libwebrtc_sys::yuv_helper as yuv_sys;
pub fn i420_to_abgr(
src_y: &[u8],
src_stride_y: i32,
src_u: &[u8],
src_stride_u: i32,
src_v: &[u8],
src_stride_v: i32,
dst_abgr: &mut [u8],
dst_stride_abgr: i32,
width: i32,
height: i32,
) {
// Assert minimum capacity for safety
let chroma_height = (height + 1) / 2; // the buffer should be padded?
let min_y: usize = (src_stride_y * height).try_into().unwrap();
let min_u: usize = (src_stride_u * chroma_height).try_into().unwrap();
let min_v: usize = (src_stride_v * chroma_height).try_into().unwrap();
let min_abgr: usize = (dst_stride_abgr * height).try_into().unwrap();
assert!(src_y.len() >= min_y);
assert!(src_u.len() >= min_u);
assert!(src_v.len() >= min_v);
assert!(dst_abgr.len() >= min_abgr);
unsafe {
yuv_sys::ffi::i420_to_abgr(
src_y.as_ptr(),
src_stride_y,
src_u.as_ptr(),
src_stride_u,
src_v.as_ptr(),
src_stride_v,
dst_abgr.as_mut_ptr(),
dst_stride_abgr,
width,
height,
);
}
}