Connect to LiveKit room in capture example

This commit is contained in:
Nathan Sobo 2022-09-06 13:19:17 -06:00 committed by Antonio Scandurra
parent 45d83b557b
commit 5347c7d678
12 changed files with 173 additions and 474 deletions

View file

@ -1,8 +1,20 @@
use core_foundation::{
base::TCFType,
string::{CFString, CFStringRef},
};
use futures::{channel::oneshot, Future};
use std::ffi::c_void;
extern "C" {
fn LKRoomCreate() -> *const c_void;
fn LKRoomDestroy(ptr: *const c_void);
fn LKRoomDestroy(room: *const c_void);
fn LKRoomConnect(
room: *const c_void,
url: CFStringRef,
token: CFStringRef,
callback: extern "C" fn(*mut c_void) -> (),
callback_data: *mut c_void,
);
}
pub struct Room {
@ -15,6 +27,29 @@ impl Room {
native_room: unsafe { LKRoomCreate() },
}
}
pub fn connect(&self, url: &str, token: &str) -> impl Future<Output = ()> {
let url = CFString::new(url);
let token = CFString::new(token);
let (tx, rx) = oneshot::channel();
extern "C" fn did_connect(tx: *mut c_void) {
let tx = unsafe { Box::from_raw(tx as *mut oneshot::Sender<()>) };
let _ = tx.send(());
}
unsafe {
LKRoomConnect(
self.native_room,
url.as_concrete_TypeRef(),
token.as_concrete_TypeRef(),
did_connect,
Box::into_raw(Box::new(tx)) as *mut c_void,
)
}
async { rx.await.unwrap() }
}
}
impl Drop for Room {