Start on implementing a fake live-kit server
This commit is contained in:
parent
fb5c6493cf
commit
288c039929
6 changed files with 134 additions and 35 deletions
|
@ -12,19 +12,25 @@ doctest = false
|
|||
name = "test_app"
|
||||
|
||||
[features]
|
||||
test-support = []
|
||||
test-support = ["collections/test-support", "gpui/test-support", "lazy_static", "live_kit_server"]
|
||||
|
||||
[dependencies]
|
||||
collections = { path = "../collections", optional = true }
|
||||
gpui = { path = "../gpui", optional = true }
|
||||
live_kit_server = { path = "../live_kit_server", optional = true }
|
||||
media = { path = "../media" }
|
||||
|
||||
anyhow = "1.0.38"
|
||||
core-foundation = "0.9.3"
|
||||
core-graphics = "0.22.3"
|
||||
futures = "0.3"
|
||||
lazy_static = { version = "1.4", optional = true }
|
||||
parking_lot = "0.11.1"
|
||||
|
||||
[dev-dependencies]
|
||||
gpui = { path = "../gpui" }
|
||||
collections = { path = "../collections", features = ["test-support"] }
|
||||
gpui = { path = "../gpui", features = ["test-support"] }
|
||||
lazy_static = "1.4"
|
||||
live_kit_server = { path = "../live_kit_server" }
|
||||
media = { path = "../media" }
|
||||
|
||||
|
|
|
@ -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::*;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue