Skip to content

Commit 06b024a

Browse files
authored
feat: allow conversions using ptr on the ffi (#226)
1 parent bfa62d7 commit 06b024a

File tree

9 files changed

+482
-82
lines changed

9 files changed

+482
-82
lines changed

libwebrtc/src/native/video_frame.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,21 @@ impl NativeBuffer {
191191
}
192192

193193
impl I420Buffer {
194-
pub fn new(width: u32, height: u32) -> vf::I420Buffer {
194+
pub fn new(
195+
width: u32,
196+
height: u32,
197+
stride_y: u32,
198+
stride_u: u32,
199+
stride_v: u32,
200+
) -> vf::I420Buffer {
195201
vf::I420Buffer {
196202
handle: I420Buffer {
197203
sys_handle: vfb_sys::ffi::new_i420_buffer(
198204
width.try_into().unwrap(),
199205
height.try_into().unwrap(),
206+
stride_y.try_into().unwrap(),
207+
stride_u.try_into().unwrap(),
208+
stride_v.try_into().unwrap(),
200209
),
201210
},
202211
}
@@ -401,6 +410,26 @@ impl I420ABuffer {
401410
}
402411

403412
impl I422Buffer {
413+
pub fn new(
414+
width: u32,
415+
height: u32,
416+
stride_y: u32,
417+
stride_u: u32,
418+
stride_v: u32,
419+
) -> vf::I422Buffer {
420+
vf::I422Buffer {
421+
handle: I422Buffer {
422+
sys_handle: vfb_sys::ffi::new_i422_buffer(
423+
width.try_into().unwrap(),
424+
height.try_into().unwrap(),
425+
stride_y.try_into().unwrap(),
426+
stride_u.try_into().unwrap(),
427+
stride_v.try_into().unwrap(),
428+
),
429+
},
430+
}
431+
}
432+
404433
pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer {
405434
unsafe { &*recursive_cast!(&*self.sys_handle, i422_to_yuv8, yuv8_to_yuv, yuv_to_vfb) }
406435
}
@@ -487,6 +516,26 @@ impl I422Buffer {
487516
}
488517
}
489518
impl I444Buffer {
519+
pub fn new(
520+
width: u32,
521+
height: u32,
522+
stride_y: u32,
523+
stride_u: u32,
524+
stride_v: u32,
525+
) -> vf::I444Buffer {
526+
vf::I444Buffer {
527+
handle: I444Buffer {
528+
sys_handle: vfb_sys::ffi::new_i444_buffer(
529+
width.try_into().unwrap(),
530+
height.try_into().unwrap(),
531+
stride_y.try_into().unwrap(),
532+
stride_u.try_into().unwrap(),
533+
stride_v.try_into().unwrap(),
534+
),
535+
},
536+
}
537+
}
538+
490539
pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer {
491540
unsafe { &*recursive_cast!(&*self.sys_handle, i444_to_yuv8, yuv8_to_yuv, yuv_to_vfb) }
492541
}
@@ -574,6 +623,26 @@ impl I444Buffer {
574623
}
575624

576625
impl I010Buffer {
626+
pub fn new(
627+
width: u32,
628+
height: u32,
629+
stride_y: u32,
630+
stride_u: u32,
631+
stride_v: u32,
632+
) -> vf::I010Buffer {
633+
vf::I010Buffer {
634+
handle: I010Buffer {
635+
sys_handle: vfb_sys::ffi::new_i010_buffer(
636+
width.try_into().unwrap(),
637+
height.try_into().unwrap(),
638+
stride_y.try_into().unwrap(),
639+
stride_u.try_into().unwrap(),
640+
stride_v.try_into().unwrap(),
641+
),
642+
},
643+
}
644+
}
645+
577646
pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer {
578647
unsafe { &*recursive_cast!(&*self.sys_handle, i010_to_yuv16b, yuv16b_to_yuv, yuv_to_vfb) }
579648
}
@@ -672,6 +741,19 @@ impl I010Buffer {
672741
}
673742

674743
impl NV12Buffer {
744+
pub fn new(width: u32, height: u32, stride_y: u32, stride_uv: u32) -> vf::NV12Buffer {
745+
vf::NV12Buffer {
746+
handle: NV12Buffer {
747+
sys_handle: vfb_sys::ffi::new_nv12_buffer(
748+
width.try_into().unwrap(),
749+
height.try_into().unwrap(),
750+
stride_y.try_into().unwrap(),
751+
stride_uv.try_into().unwrap(),
752+
),
753+
},
754+
}
755+
}
756+
675757
pub fn sys_handle(&self) -> &vfb_sys::ffi::VideoFrameBuffer {
676758
unsafe {
677759
&*recursive_cast!(

libwebrtc/src/video_frame.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ new_buffer_type!(I010Buffer, I010, as_i010);
195195
new_buffer_type!(NV12Buffer, NV12, as_nv12);
196196

197197
impl I420Buffer {
198+
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I420Buffer {
199+
vf_imp::I420Buffer::new(width, height, stride_y, stride_u, stride_v)
200+
}
201+
198202
pub fn chroma_width(&self) -> u32 {
199203
self.handle.chroma_width()
200204
}
@@ -267,6 +271,10 @@ impl I420ABuffer {
267271
}
268272

269273
impl I422Buffer {
274+
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I422Buffer {
275+
vf_imp::I422Buffer::new(width, height, stride_y, stride_u, stride_v)
276+
}
277+
270278
pub fn chroma_width(&self) -> u32 {
271279
self.handle.chroma_width()
272280
}
@@ -300,6 +308,10 @@ impl I422Buffer {
300308
}
301309

302310
impl I444Buffer {
311+
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I444Buffer {
312+
vf_imp::I444Buffer::new(width, height, stride_y, stride_u, stride_v)
313+
}
314+
303315
pub fn chroma_width(&self) -> u32 {
304316
self.handle.chroma_width()
305317
}
@@ -333,6 +345,10 @@ impl I444Buffer {
333345
}
334346

335347
impl I010Buffer {
348+
pub fn new(width: u32, height: u32, stride_y: u32, stride_u: u32, stride_v: u32) -> I010Buffer {
349+
vf_imp::I010Buffer::new(width, height, stride_y, stride_u, stride_v)
350+
}
351+
336352
pub fn chroma_width(&self) -> u32 {
337353
self.handle.chroma_width()
338354
}
@@ -366,6 +382,10 @@ impl I010Buffer {
366382
}
367383

368384
impl NV12Buffer {
385+
pub fn new(width: u32, height: u32, stride_y: u32, stride_uv: u32) -> NV12Buffer {
386+
vf_imp::NV12Buffer::new(width, height, stride_y, stride_uv)
387+
}
388+
369389
pub fn chroma_width(&self) -> u32 {
370390
self.handle.chroma_width()
371391
}
@@ -400,16 +420,6 @@ pub mod native {
400420

401421
new_buffer_type!(NativeBuffer, Native, as_native);
402422

403-
pub trait I420BufferExt {
404-
fn new(width: u32, height: u32) -> Self;
405-
}
406-
407-
impl I420BufferExt for I420Buffer {
408-
fn new(width: u32, height: u32) -> I420Buffer {
409-
vf_imp::I420Buffer::new(width, height)
410-
}
411-
}
412-
413423
pub trait VideoFrameBufferExt: VideoFrameBuffer {
414424
fn to_i420(&self) -> I420Buffer;
415425
fn to_argb(

livekit-ffi/protocol/video_frame.proto

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ message NewVideoSourceResponse { OwnedVideoSource source = 1; }
4949
message CaptureVideoFrameRequest {
5050
uint64 source_handle = 1;
5151
VideoFrameInfo frame = 2;
52-
uint64 buffer_handle = 3;
52+
oneof from {
53+
VideoFrameBufferInfo info = 3;
54+
uint64 handle = 4;
55+
}
5356
}
5457
message CaptureVideoFrameResponse {}
5558

@@ -59,7 +62,8 @@ message ToI420Request {
5962
bool flip_y = 1;
6063
oneof from {
6164
ArgbBufferInfo argb = 2;
62-
uint64 yuv_handle = 3; // Another yuv buffer
65+
VideoFrameBufferInfo buffer = 3;
66+
uint64 handle = 4;
6367
}
6468
}
6569
message ToI420Response {
@@ -69,7 +73,7 @@ message ToI420Response {
6973
// Convert a YUV frame to a RGBA frame
7074
// Only I420 is supported atm
7175
message ToArgbRequest {
72-
uint64 buffer_handle = 1;
76+
VideoFrameBufferInfo buffer = 1;
7377
uint64 dst_ptr = 2;
7478
VideoFormatType dst_format = 3;
7579
uint32 dst_stride = 4;

livekit-ffi/src/livekit.proto.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,19 @@ pub struct CaptureVideoFrameRequest {
583583
pub source_handle: u64,
584584
#[prost(message, optional, tag="2")]
585585
pub frame: ::core::option::Option<VideoFrameInfo>,
586-
#[prost(uint64, tag="3")]
587-
pub buffer_handle: u64,
586+
#[prost(oneof="capture_video_frame_request::From", tags="3, 4")]
587+
pub from: ::core::option::Option<capture_video_frame_request::From>,
588+
}
589+
/// Nested message and enum types in `CaptureVideoFrameRequest`.
590+
pub mod capture_video_frame_request {
591+
#[allow(clippy::derive_partial_eq_without_eq)]
592+
#[derive(Clone, PartialEq, ::prost::Oneof)]
593+
pub enum From {
594+
#[prost(message, tag="3")]
595+
Info(super::VideoFrameBufferInfo),
596+
#[prost(uint64, tag="4")]
597+
Handle(u64),
598+
}
588599
}
589600
#[allow(clippy::derive_partial_eq_without_eq)]
590601
#[derive(Clone, PartialEq, ::prost::Message)]
@@ -597,7 +608,7 @@ pub struct CaptureVideoFrameResponse {
597608
pub struct ToI420Request {
598609
#[prost(bool, tag="1")]
599610
pub flip_y: bool,
600-
#[prost(oneof="to_i420_request::From", tags="2, 3")]
611+
#[prost(oneof="to_i420_request::From", tags="2, 3, 4")]
601612
pub from: ::core::option::Option<to_i420_request::From>,
602613
}
603614
/// Nested message and enum types in `ToI420Request`.
@@ -607,9 +618,10 @@ pub mod to_i420_request {
607618
pub enum From {
608619
#[prost(message, tag="2")]
609620
Argb(super::ArgbBufferInfo),
610-
/// Another yuv buffer
611-
#[prost(uint64, tag="3")]
612-
YuvHandle(u64),
621+
#[prost(message, tag="3")]
622+
Buffer(super::VideoFrameBufferInfo),
623+
#[prost(uint64, tag="4")]
624+
Handle(u64),
613625
}
614626
}
615627
#[allow(clippy::derive_partial_eq_without_eq)]
@@ -623,8 +635,8 @@ pub struct ToI420Response {
623635
#[allow(clippy::derive_partial_eq_without_eq)]
624636
#[derive(Clone, PartialEq, ::prost::Message)]
625637
pub struct ToArgbRequest {
626-
#[prost(uint64, tag="1")]
627-
pub buffer_handle: u64,
638+
#[prost(message, optional, tag="1")]
639+
pub buffer: ::core::option::Option<VideoFrameBufferInfo>,
628640
#[prost(uint64, tag="2")]
629641
pub dst_ptr: u64,
630642
#[prost(enumeration="VideoFormatType", tag="3")]

0 commit comments

Comments
 (0)