livekit-utils crate + VideoRenderer proto
This commit is contained in:
@@ -6,10 +6,11 @@ homepage = "https://livekit.io"
|
||||
|
||||
[dependencies]
|
||||
libwebrtc-sys = { path = "./libwebrtc-sys" }
|
||||
livekit-utils = { path = "../livekit-utils" }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
cxx = "1.0"
|
||||
log = "0.4"
|
||||
thiserror = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.9"
|
||||
env_logger = "0.9"
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
/libwebrtc
|
||||
/cmake-build-debug
|
||||
@@ -55,17 +55,17 @@ fn main() {
|
||||
let target_os = "windows";
|
||||
//let target_arch = "arm64";
|
||||
|
||||
let libwebrtc_dir = path::PathBuf::from("libwebrtc");
|
||||
let libwebrtc_dir = path::PathBuf::from("libwebrtc/src");
|
||||
|
||||
// Just required for the bridge build to succeed.
|
||||
let includes = &[
|
||||
path::PathBuf::from("./include"),
|
||||
libwebrtc_dir.join("include/"),
|
||||
libwebrtc_dir.join("include/third_party/abseil-cpp/"),
|
||||
libwebrtc_dir.join("include/third_party/libc++/"),
|
||||
libwebrtc_dir.clone(),
|
||||
libwebrtc_dir.join("third_party/abseil-cpp/"),
|
||||
libwebrtc_dir.join("third_party/libc++/"),
|
||||
// For mac & ios
|
||||
libwebrtc_dir.join("include/sdk/objc"),
|
||||
libwebrtc_dir.join("include/sdk/objc/base"),
|
||||
libwebrtc_dir.join("sdk/objc"),
|
||||
libwebrtc_dir.join("sdk/objc/base"),
|
||||
];
|
||||
|
||||
let mut builder = cxx_build::bridges(&[
|
||||
@@ -81,6 +81,7 @@ fn main() {
|
||||
"src/webrtc.rs",
|
||||
"src/video_frame.rs",
|
||||
"src/video_frame_buffer.rs",
|
||||
"src/yuv_helper.rs",
|
||||
]);
|
||||
|
||||
builder.file("src/peer_connection.cpp");
|
||||
@@ -100,7 +101,12 @@ fn main() {
|
||||
|
||||
println!(
|
||||
"cargo:rustc-link-search=native={}",
|
||||
libwebrtc_dir.canonicalize().unwrap().to_str().unwrap()
|
||||
libwebrtc_dir
|
||||
.join("out/Default/obj")
|
||||
.canonicalize()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
match target_os {
|
||||
@@ -124,6 +130,7 @@ fn main() {
|
||||
.flag("/std:c++17")
|
||||
.flag("/EHsc")
|
||||
.define("WEBRTC_WIN", None)
|
||||
//.define("WEBRTC_ENABLE_SYMBOL_EXPORT", None) Not necessary when using WebRTC as a static library
|
||||
.define("NOMINMAX", None);
|
||||
}
|
||||
"macos" => {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
-xc++
|
||||
-std=c++17
|
||||
-Iinclude
|
||||
-Ilibwebrtc/include
|
||||
-Ilibwebrtc/include/third_party/abseil-cpp
|
||||
-Ilibwebrtc/include/third_party/libc++
|
||||
-Ilibwebrtc/src
|
||||
-Ilibwebrtc/src/third_party/abseil-cpp
|
||||
-Ilibwebrtc/src/third_party/libc++
|
||||
-I../../../target/cxxbridge
|
||||
-DWEBRTC_WIN
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by Théo Monnom on 01/12/2022.
|
||||
//
|
||||
|
||||
#ifndef CLIENT_SDK_NATIVE_YUV_HELPER_H
|
||||
#define CLIENT_SDK_NATIVE_YUV_HELPER_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "api/video/yuv_helper.h"
|
||||
|
||||
namespace livekit {
|
||||
|
||||
static void i420_to_abgr(const uint8_t* src_y,
|
||||
int src_stride_y,
|
||||
const uint8_t* src_u,
|
||||
int src_stride_u,
|
||||
const uint8_t* src_v,
|
||||
int src_stride_v,
|
||||
uint8_t* dst_rgba,
|
||||
int dst_stride_abgr,
|
||||
int width,
|
||||
int height) {
|
||||
webrtc::I420ToABGR(src_y, src_stride_y, src_u, src_stride_u, src_v,
|
||||
src_stride_v, dst_rgba, dst_stride_abgr, width, height);
|
||||
}
|
||||
|
||||
} // namespace livekit
|
||||
|
||||
#endif // CLIENT_SDK_NATIVE_YUV_HELPER_H
|
||||
@@ -0,0 +1,11 @@
|
||||
solutions = [
|
||||
{
|
||||
"name" : 'src',
|
||||
"url" : 'https://github.com/webrtc-sdk/webrtc.git',
|
||||
"deps_file" : 'DEPS',
|
||||
"managed" : False,
|
||||
"custom_deps" : {
|
||||
},
|
||||
"custom_vars": {},
|
||||
},
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
.cipd
|
||||
src
|
||||
.gclient_*
|
||||
@@ -0,0 +1,19 @@
|
||||
import subprocess
|
||||
|
||||
GN_ARGS = [
|
||||
"is_debug=false",
|
||||
"treat_warnings_as_errors=false",
|
||||
'target_os="win"',
|
||||
'target_cpu="x64"',
|
||||
"rtc_include_tests=false",
|
||||
"rtc_use_h264=false",
|
||||
"is_component_build=false",
|
||||
"rtc_build_examples=false",
|
||||
"use_rtti=true",
|
||||
"rtc_build_tools=false",
|
||||
"use_custom_libcxx=false",
|
||||
"strip_debug_info=true",
|
||||
"symbol_level=0"
|
||||
]
|
||||
|
||||
subprocess.call(["gn", "gen", "out/Default", "--args=" + ' '.join(GN_ARGS)], shell=True)
|
||||
@@ -10,6 +10,7 @@ pub mod rtp_transceiver;
|
||||
pub mod video_frame;
|
||||
pub mod video_frame_buffer;
|
||||
pub mod webrtc;
|
||||
pub mod yuv_helper;
|
||||
|
||||
pub const MEDIA_TYPE_VIDEO: &str = "video";
|
||||
pub const MEDIA_TYPE_AUDIO: &str = "audio";
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#[cxx::bridge(namespace = "livekit")]
|
||||
pub mod ffi {
|
||||
unsafe extern "C++" {
|
||||
include!("livekit/yuv_helper.h");
|
||||
|
||||
unsafe fn i420_to_abgr(
|
||||
src_y: *const u8,
|
||||
src_stride_y: i32,
|
||||
src_u: *const u8,
|
||||
src_stride_u: i32,
|
||||
src_v: *const u8,
|
||||
src_stride_v: i32,
|
||||
dst_abgr: *mut u8,
|
||||
dst_stride_abgr: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user