cleanup: room & dependencies (#76)

This commit is contained in:
Théo Monnom
2023-05-25 20:13:34 +02:00
committed by GitHub
parent a9e6865137
commit 05ad1c95af
46 changed files with 1094 additions and 1031 deletions
+1 -1
View File
@@ -66,7 +66,7 @@ impl proto::RoomEvent {
}
impl proto::RoomInfo {
pub fn from_session(handle_id: FfiHandleId, session: &RoomSession) -> Self {
pub fn from_room(handle_id: FfiHandleId, session: &Room) -> Self {
Self {
handle: Some(handle_id.into()),
sid: session.sid().into(),
+1
View File
@@ -60,6 +60,7 @@ impl_publication_into!(&TrackPublication);
macro_rules! impl_track_into {
($fnc:ident, $t:ty) => {
impl proto::TrackInfo {
#[allow(dead_code)]
pub fn $fnc(handle_id: FfiHandleId, track: $t) -> Self {
Self {
opt_handle: Some(handle_id.into()),
+1 -2
View File
@@ -157,8 +157,7 @@ impl FfiAudioSource {
proto::AudioSourceType::AudioSourceNative => {
let audio_source = NativeAudioSource::default();
Ok::<AudioSource, FfiError>(AudioSource::Native(audio_source))
}
_ => return Err(FfiError::InvalidRequest("unsupported audio source type")),
} //_ => return Err(FfiError::InvalidRequest("unsupported audio source type")),
}?;
let audio_source = Self {
+4 -4
View File
@@ -209,12 +209,12 @@ impl FfiServer {
.ok_or(FfiError::InvalidRequest("room_handle is empty"))?
.id as FfiHandleId;
let room = self
let ffi_room = self
.ffi_handles
.get(&room_handle)
.ok_or(FfiError::InvalidRequest("room not found"))?;
let room = room
let ffi_room = ffi_room
.downcast_ref::<room::FfiRoom>()
.ok_or(FfiError::InvalidRequest("room is not a FfiRoom"))?;
@@ -233,8 +233,8 @@ impl FfiServer {
.downcast_ref::<LocalTrack>()
.ok_or(FfiError::InvalidRequest("track is not a LocalTrack"))?;
let publication = room
.session()
let publication = ffi_room
.room()
.local_participant()
.publish_track(
track.clone(),
+16 -19
View File
@@ -1,12 +1,12 @@
use crate::server::FfiServer;
use crate::{proto, FfiHandleId, FfiResult};
use livekit::prelude::*;
use std::sync::Arc;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
pub struct FfiRoom {
room: Room,
handle_id: FfiHandleId,
room: Arc<Room>,
handle: JoinHandle<()>,
close_tx: oneshot::Sender<()>,
}
@@ -17,45 +17,42 @@ impl FfiRoom {
connect: proto::ConnectRequest,
) -> FfiResult<proto::RoomInfo> {
let (room, events) = Room::connect(&connect.url, &connect.token).await?;
let room = Arc::new(room);
let (close_tx, close_rx) = oneshot::channel();
let session = room.session();
let next_id = server.next_id() as FfiHandleId;
let handle = server.async_runtime.spawn(room_task(
server,
session.clone(),
next_id,
events,
close_rx,
));
let room_info = proto::RoomInfo::from_session(next_id, &session);
let handle =
server
.async_runtime
.spawn(room_task(server, room.clone(), next_id, events, close_rx));
let room_info = proto::RoomInfo::from_room(next_id, &room);
let ffi_room = Self {
handle_id: next_id,
room,
room: room.clone(),
handle,
close_tx,
};
server.ffi_handles().insert(next_id, Box::new(ffi_room));
server.rooms().lock().insert(session.sid(), next_id);
server.rooms().lock().insert(room.sid(), next_id);
Ok(room_info)
}
pub async fn close(self) {
self.room.close().await;
let _ = self.room.close().await;
let _ = self.close_tx.send(());
let _ = self.handle.await;
}
pub fn session(&self) -> RoomSession {
self.room.session()
pub fn room(&self) -> &Arc<Room> {
&self.room
}
}
async fn room_task(
server: &'static FfiServer,
session: RoomSession,
room: Arc<Room>,
room_handle: FfiHandleId,
mut events: mpsc::UnboundedReceiver<livekit::RoomEvent>,
mut close_rx: oneshot::Receiver<()>,
@@ -63,7 +60,7 @@ async fn room_task(
server
.async_runtime
.spawn(participant_task(Participant::Local(
session.local_participant(),
room.local_participant(),
)));
loop {
+5 -6
View File
@@ -12,8 +12,8 @@ mod client {
INVALID_HANDLE,
};
use lazy_static::lazy_static;
use parking_lot::Mutex;
use prost::Message;
use std::sync::Mutex;
use tokio::sync::mpsc;
lazy_static! {
@@ -31,7 +31,7 @@ mod client {
impl Default for FfiClient {
fn default() -> Self {
let (event_tx, event_rx) = mpsc::unbounded_channel();
*EVENT_TX.lock().unwrap() = Some(event_tx);
*EVENT_TX.lock() = Some(event_tx);
Self { event_rx }
}
}
@@ -88,7 +88,6 @@ mod client {
let event = proto::FfiEvent::decode(data).unwrap();
EVENT_TX
.lock()
.unwrap()
.as_ref()
.unwrap()
.send(event.message.unwrap())
@@ -99,9 +98,9 @@ mod client {
struct TestScope {}
impl TestScope {
fn new() -> (Self, std::sync::MutexGuard<'static, client::FfiClient>) {
fn new() -> (Self, parking_lot::MutexGuard<'static, client::FfiClient>) {
// Run one test at a time
let client = client::FFI_CLIENT.lock().unwrap();
let client = client::FFI_CLIENT.lock();
(TestScope {}, client)
}
@@ -176,7 +175,7 @@ fn create_i420_buffer() {
#[test]
#[ignore] // Ignore for now ( need to setup GHA )
fn publish_video_track() {
let (test, mut client) = TestScope::new();
let (_test, mut client) = TestScope::new();
let (lk_url, lk_api_key, lk_api_secret) = test_env();
tokio::runtime::Builder::new_multi_thread()
+4 -4
View File
@@ -7,17 +7,17 @@ pub fn find_remote_track(
participant_sid: &ParticipantSid,
room_handle: FfiHandleId,
) -> FfiResult<RemoteTrack> {
let room = server
let ffi_room = server
.ffi_handles()
.get(&room_handle)
.ok_or(FfiError::InvalidRequest("room not found"))?;
let room = room
let ffi_room = ffi_room
.downcast_ref::<server::room::FfiRoom>()
.ok_or(FfiError::InvalidRequest("room is not ffi room"))?;
let session = room.session();
let participants = session.participants();
let room = ffi_room.room();
let participants = room.participants();
let participant = participants
.get(participant_sid)
.ok_or(FfiError::InvalidRequest("participant not found"))?;
+2 -3
View File
@@ -160,10 +160,9 @@ impl FfiVideoSource {
let source_inner = match source_type {
proto::VideoSourceType::VideoSourceNative => {
let video_source = NativeVideoSource::default();
Ok(VideoSource::Native(video_source))
VideoSource::Native(video_source)
}
_ => Err(FfiError::InvalidRequest("unsupported video source type")),
}?;
};
let video_source = Self {
handle_id: server.next_id(),