Start on implementing a fake live-kit server

This commit is contained in:
Antonio Scandurra 2022-10-19 14:58:50 +02:00
parent fb5c6493cf
commit 288c039929
6 changed files with 134 additions and 35 deletions

View file

@ -1,8 +1,10 @@
pub mod prod;
pub mod test;
#[cfg(not(any(test, feature = "test-support")))]
pub use prod::*;
#[cfg(any(test, feature = "test-support"))]
mod test;
#[cfg(any(test, feature = "test-support"))]
pub use test::*;

View file

@ -1,8 +1,80 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use collections::HashMap;
use futures::{channel::mpsc, future};
use gpui::executor::Background;
use lazy_static::lazy_static;
use media::core_video::CVImageBuffer;
use parking_lot::Mutex;
use std::{future::Future, sync::Arc};
lazy_static! {
static ref SERVERS: Mutex<HashMap<String, Arc<FakeServer>>> = Default::default();
}
pub struct FakeServer {
url: String,
secret_key: String,
rooms: Mutex<HashMap<String, FakeServerRoom>>,
background: Arc<Background>,
}
impl FakeServer {
pub fn create(
url: String,
secret_key: String,
background: Arc<Background>,
) -> Result<Arc<FakeServer>> {
let mut servers = SERVERS.lock();
if servers.contains_key(&url) {
Err(anyhow!("a server with url {:?} already exists", url))
} else {
let server = Arc::new(FakeServer {
url: url.clone(),
secret_key,
rooms: Default::default(),
background,
});
servers.insert(url, server.clone());
Ok(server)
}
}
fn get(url: &str) -> Result<Arc<FakeServer>> {
Ok(SERVERS
.lock()
.get(url)
.ok_or_else(|| anyhow!("no server found for url"))?
.clone())
}
pub fn teardown(&self) -> Result<()> {
SERVERS
.lock()
.remove(&self.url)
.ok_or_else(|| anyhow!("server with url {:?} does not exist", self.url))?;
Ok(())
}
async fn join_room(&self, token: String, client_room: Arc<Room>) -> Result<()> {
self.background.simulate_random_delay().await;
let claims = live_kit_server::token::validate(&token, &self.secret_key)?;
let identity = claims.sub.unwrap().to_string();
let room = claims.video.room.unwrap();
let mut server_rooms = self.rooms.lock();
let room = server_rooms
.get_mut(&*room)
.ok_or_else(|| anyhow!("room {} does not exist", room))?;
room.clients.insert(identity, client_room);
Ok(())
}
}
struct FakeServerRoom {
clients: HashMap<Sid, Arc<Room>>,
}
impl FakeServerRoom {}
pub type Sid = String;
pub struct Room;
@ -12,8 +84,15 @@ impl Room {
Arc::new(Self)
}
pub fn connect(&self, url: &str, token: &str) -> impl Future<Output = Result<()>> {
future::pending()
pub fn connect(self: &Arc<Self>, url: &str, token: &str) -> impl Future<Output = Result<()>> {
let this = self.clone();
let url = url.to_string();
let token = token.to_string();
async move {
let server = FakeServer::get(&url)?;
server.join_room(token, this).await?;
Ok(())
}
}
pub fn publish_video_track(
@ -34,6 +113,12 @@ impl Room {
}
}
impl Drop for Room {
fn drop(&mut self) {
todo!()
}
}
pub struct LocalTrackPublication;
pub struct LocalVideoTrack;