Simplify renderer interface for live-kit-client
This commit is contained in:
parent
69472f7823
commit
99aa1219d2
6 changed files with 70 additions and 70 deletions
|
@ -55,7 +55,7 @@ extern "C" {
|
|||
|
||||
fn LKVideoRendererCreate(
|
||||
callback_data: *mut c_void,
|
||||
on_frame: extern "C" fn(callback_data: *mut c_void, frame: CVImageBufferRef),
|
||||
on_frame: extern "C" fn(callback_data: *mut c_void, frame: CVImageBufferRef) -> bool,
|
||||
on_drop: extern "C" fn(callback_data: *mut c_void),
|
||||
) -> *const c_void;
|
||||
|
||||
|
@ -364,32 +364,43 @@ impl RemoteVideoTrack {
|
|||
&self.publisher_id
|
||||
}
|
||||
|
||||
pub fn add_renderer<F>(&self, callback: F)
|
||||
where
|
||||
F: 'static + Send + Sync + FnMut(Frame),
|
||||
{
|
||||
extern "C" fn on_frame<F>(callback_data: *mut c_void, frame: CVImageBufferRef)
|
||||
where
|
||||
F: FnMut(Frame),
|
||||
{
|
||||
pub fn frames(&self) -> async_broadcast::Receiver<Frame> {
|
||||
extern "C" fn on_frame(callback_data: *mut c_void, frame: CVImageBufferRef) -> bool {
|
||||
unsafe {
|
||||
let tx = Box::from_raw(callback_data as *mut async_broadcast::Sender<Frame>);
|
||||
let buffer = CVImageBuffer::wrap_under_get_rule(frame);
|
||||
let callback = &mut *(callback_data as *mut F);
|
||||
callback(Frame(buffer));
|
||||
let result = tx.try_broadcast(Frame(buffer));
|
||||
let _ = Box::into_raw(tx);
|
||||
match result {
|
||||
Ok(_) => true,
|
||||
Err(async_broadcast::TrySendError::Closed(_))
|
||||
| Err(async_broadcast::TrySendError::Inactive(_)) => {
|
||||
log::warn!("no active receiver for frame");
|
||||
false
|
||||
}
|
||||
Err(async_broadcast::TrySendError::Full(_)) => {
|
||||
log::warn!("skipping frame as receiver is not keeping up");
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn on_drop<F>(callback_data: *mut c_void) {
|
||||
extern "C" fn on_drop(callback_data: *mut c_void) {
|
||||
unsafe {
|
||||
let _ = Box::from_raw(callback_data as *mut F);
|
||||
let _ = Box::from_raw(callback_data as *mut async_broadcast::Sender<Frame>);
|
||||
}
|
||||
}
|
||||
|
||||
let callback_data = Box::into_raw(Box::new(callback));
|
||||
let (tx, rx) = async_broadcast::broadcast(64);
|
||||
unsafe {
|
||||
let renderer =
|
||||
LKVideoRendererCreate(callback_data as *mut c_void, on_frame::<F>, on_drop::<F>);
|
||||
let renderer = LKVideoRendererCreate(
|
||||
Box::into_raw(Box::new(tx)) as *mut c_void,
|
||||
on_frame,
|
||||
on_drop,
|
||||
);
|
||||
LKVideoTrackAddRenderer(self.native_track, renderer);
|
||||
rx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -422,6 +433,7 @@ impl Drop for MacOSDisplay {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Frame(CVImageBuffer);
|
||||
|
||||
impl Frame {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use collections::HashMap;
|
||||
use futures::{Stream, StreamExt};
|
||||
use gpui::executor::{self, Background};
|
||||
use futures::Stream;
|
||||
use gpui::executor::Background;
|
||||
use lazy_static::lazy_static;
|
||||
use live_kit_server::token;
|
||||
use media::core_video::CVImageBuffer;
|
||||
|
@ -160,7 +160,6 @@ impl TestServer {
|
|||
sid: nanoid::nanoid!(17),
|
||||
publisher_id: identity.clone(),
|
||||
frames_rx: local_track.frames_rx.clone(),
|
||||
background: self.background.clone(),
|
||||
}));
|
||||
|
||||
for (id, client_room) in &room.client_rooms {
|
||||
|
@ -353,7 +352,6 @@ pub struct RemoteVideoTrack {
|
|||
sid: Sid,
|
||||
publisher_id: Sid,
|
||||
frames_rx: async_broadcast::Receiver<Frame>,
|
||||
background: Arc<executor::Background>,
|
||||
}
|
||||
|
||||
impl RemoteVideoTrack {
|
||||
|
@ -365,18 +363,8 @@ impl RemoteVideoTrack {
|
|||
&self.publisher_id
|
||||
}
|
||||
|
||||
pub fn add_renderer<F>(&self, mut callback: F)
|
||||
where
|
||||
F: 'static + Send + Sync + FnMut(Frame),
|
||||
{
|
||||
let mut frames_rx = self.frames_rx.clone();
|
||||
self.background
|
||||
.spawn(async move {
|
||||
while let Some(frame) = frames_rx.next().await {
|
||||
callback(frame)
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
pub fn frames(&self) -> async_broadcast::Receiver<Frame> {
|
||||
self.frames_rx.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue