From 82ec808dee4e0c052079d91cbb944f581f2594b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Monnom?= Date: Tue, 8 Nov 2022 10:31:09 +0100 Subject: [PATCH] changing computer ( started tracks ) I used my Mac on the company trip --- crates/livekit-core/protocol | 2 +- crates/livekit-core/src/room/mod.rs | 2 +- crates/livekit-core/src/room/participant.rs | 12 +-- .../src/room/remote_participant.rs | 65 +++++++++++++++-- .../src/room/{track.rs => track/mod.rs} | 50 ++++++++++++- .../src/room/track_publication.rs | 73 +++++++++++++++++-- crates/livekit-webrtc/libwebrtc-sys/build.rs | 2 +- 7 files changed, 178 insertions(+), 28 deletions(-) rename crates/livekit-core/src/room/{track.rs => track/mod.rs} (81%) diff --git a/crates/livekit-core/protocol b/crates/livekit-core/protocol index dc2a7bc..8449c11 160000 --- a/crates/livekit-core/protocol +++ b/crates/livekit-core/protocol @@ -1 +1 @@ -Subproject commit dc2a7bc3a05fe37f5c64cdb83ac36aeb00672778 +Subproject commit 8449c1106929b3f5cf9fdcc97ab3e63a1c5e6a76 diff --git a/crates/livekit-core/src/room/mod.rs b/crates/livekit-core/src/room/mod.rs index 853d992..84f9285 100644 --- a/crates/livekit-core/src/room/mod.rs +++ b/crates/livekit-core/src/room/mod.rs @@ -1,7 +1,6 @@ use futures_util::future::BoxFuture; use parking_lot::lock_api::RwLockUpgradableReadGuard; use parking_lot::{Mutex, RwLock}; -use std::borrow::Cow; use std::collections::HashMap; use std::future::Future; use std::sync::atomic::AtomicU8; @@ -36,6 +35,7 @@ pub enum RoomError { type RoomResult = Result; +#[derive(Debug)] pub enum ConnectionState { Disconnected, Connecting, diff --git a/crates/livekit-core/src/room/participant.rs b/crates/livekit-core/src/room/participant.rs index b36c60c..425d32e 100644 --- a/crates/livekit-core/src/room/participant.rs +++ b/crates/livekit-core/src/room/participant.rs @@ -10,7 +10,7 @@ pub(super) struct ParticipantShared { pub(super) identity: Mutex, pub(super) name: Mutex, pub(super) metadata: Mutex, - pub(super) tracks: RwLock>>, + pub(super) tracks: RwLock>, } impl ParticipantShared { @@ -50,7 +50,7 @@ pub enum Participant { Remote(RemoteParticipant), } -macro_rules! shared_method { +macro_rules! shared_getter { ($x:ident, $ret:ident) => { fn $x(&self) -> $ret { match self { @@ -62,10 +62,10 @@ macro_rules! shared_method { } impl ParticipantTrait for Participant { - shared_method!(sid, ParticipantSid); - shared_method!(identity, ParticipantIdentity); - shared_method!(name, String); - shared_method!(metadata, String); + shared_getter!(sid, ParticipantSid); + shared_getter!(identity, ParticipantIdentity); + shared_getter!(name, String); + shared_getter!(metadata, String); fn update_info(&self, info: ParticipantInfo) { match self { diff --git a/crates/livekit-core/src/room/remote_participant.rs b/crates/livekit-core/src/room/remote_participant.rs index 5751cdd..341c731 100644 --- a/crates/livekit-core/src/room/remote_participant.rs +++ b/crates/livekit-core/src/room/remote_participant.rs @@ -1,7 +1,18 @@ -use std::sync::Arc; +use crate::room::id::TrackSid; use crate::room::participant::{impl_participant_trait, ParticipantShared}; -use crate::room::track_publication::RemoteTrackPublication; +use crate::room::track::{RemoteAudioTrack, RemoteTrack, RemoteVideoTrack, TrackKind}; +use crate::room::track_publication::{ + RemoteTrackPublication, TrackPublication, TrackPublicationTrait, +}; +use livekit_webrtc::media_stream::MediaStreamTrack; +use std::time::Duration; +use tokio::time::{sleep, timeout}; +const ADD_TRACK_TIMEOUT: Duration = Duration::from_secs(5); + + +// It should be fine to add event listeners in this structure +// Registering after should be ParticipantConnected is fine to avoid missing events pub struct RemoteParticipant { shared: ParticipantShared, } @@ -18,16 +29,54 @@ impl RemoteParticipant { } } - pub(super) async fn add_subscribed_media_track() { + pub(super) async fn add_subscribed_media_track( + &self, + sid: &TrackSid, + media_track: MediaStreamTrack, + ) { + let wait_publication = async { + loop { + let publication = self.get_track_publication(sid); + if let Some(publication) = publication { + return publication; + } + + sleep(Duration::from_millis(50)).await; + } + }; + + let res = timeout(ADD_TRACK_TIMEOUT, wait_publication).await; + + if let Ok(remote_publication) = res { + let track = match remote_publication.kind() { + TrackKind::Audio => { + let audio_track = RemoteAudioTrack::new(); + RemoteTrack::Audio(audio_track) + } + TrackKind::Video => { + let video_track = RemoteVideoTrack::new(); + RemoteTrack::Video(video_track) + } + _ => unreachable!(), + }; + + // TODO(theomonnom): call OnTrackSubscribed here + + } else { + // TODO(theomonnom): send error + } } - fn get_track_publication(&self, sid: &str) -> Option { - let track = self.shared.tracks.read().get(&sid.to_string().into()).unwrap().clone(); - - - None + fn get_track_publication(&self, sid: &TrackSid) -> Option { + self.shared.tracks.read().get(sid).map(|track| { + if let TrackPublication::Remote(remote) = track { + remote.clone() + } else { + unreachable!() + } + }) } } diff --git a/crates/livekit-core/src/room/track.rs b/crates/livekit-core/src/room/track/mod.rs similarity index 81% rename from crates/livekit-core/src/room/track.rs rename to crates/livekit-core/src/room/track/mod.rs index 8b88b74..1627418 100644 --- a/crates/livekit-core/src/room/track.rs +++ b/crates/livekit-core/src/room/track/mod.rs @@ -1,28 +1,70 @@ +#[derive(Debug)] pub enum TrackKind { + Unknown, Audio, - Video + Video, } +impl From for TrackKind { + fn from(val: u8) -> Self { + match val { + 1 => Self::Audio, + 2 => Self::Video, + _ => Self::Unknown, + } + } +} + +#[derive(Debug)] pub enum StreamState { + Unknown, Active, Paused, - Unknown } +#[derive(Debug)] pub enum TrackSource { + Unknown, Camera, Microphone, Screenshare, ScreenshareAudio, - Unknown, +} + +impl From for TrackSource { + fn from(val: u8) -> Self { + match val { + 1 => Self::Camera, + 2 => Self::Microphone, + 3 => Self::Screenshare, + 4 => Self::ScreenshareAudio, + _ => Self::Unknown, + } + } } pub struct LocalVideoTrack {} pub struct RemoteVideoTrack {} pub struct LocalAudioTrack {} -pub struct RemoteAudioTrack {} +pub struct RemoteAudioTrack { + + +} + +impl RemoteVideoTrack { + pub(crate) fn new() -> Self { + Self {} + } +} + +impl RemoteAudioTrack { + pub(crate) fn new() -> Self { + Self {} + } +} + pub enum RemoteTrack { Audio(RemoteAudioTrack), Video(RemoteVideoTrack), diff --git a/crates/livekit-core/src/room/track_publication.rs b/crates/livekit-core/src/room/track_publication.rs index e2c6361..2c351db 100644 --- a/crates/livekit-core/src/room/track_publication.rs +++ b/crates/livekit-core/src/room/track_publication.rs @@ -1,7 +1,16 @@ use std::sync::Arc; -use std::sync::atomic::{AtomicBool, AtomicU8}; +use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; use parking_lot::Mutex; -use crate::room::id::TrackSid; +use crate::room::id::{ParticipantIdentity, ParticipantSid, TrackSid}; +use crate::room::track::{TrackKind, TrackSource}; + +pub trait TrackPublicationTrait { + fn name(&self) -> String; + fn sid(&self) -> TrackSid; + fn kind(&self) -> TrackKind; + fn source(&self) -> TrackSource; + fn simulcasted(&self) -> bool; +} pub(super) struct TrackPublicationShared { pub(super) name: Mutex, @@ -11,6 +20,59 @@ pub(super) struct TrackPublicationShared { pub(super) simulcasted: AtomicBool } +#[derive(Clone)] +pub enum TrackPublication { + Local(LocalTrackPublication), + Remote(RemoteTrackPublication) +} + +macro_rules! shared_getter { + ($x:ident, $ret:ident) => { + fn $x(&self) -> $ret { + match self { + TrackPublication::Local(p) => p.$x(), + TrackPublication::Remote(p) => p.$x(), + } + } + }; +} + +impl TrackPublicationTrait for TrackPublication { + shared_getter!(name, String); + shared_getter!(sid, TrackSid); + shared_getter!(kind, TrackKind); + shared_getter!(source, TrackSource); + shared_getter!(simulcasted, bool); +} + +macro_rules! impl_publication_trait { + ($x:ident) => { + impl TrackPublicationTrait for $x { + fn name(&self) -> String { + self.shared.name.lock().clone() + } + + fn sid(&self) -> TrackSid { + self.shared.sid.lock().clone() + } + + fn kind(&self) -> TrackKind { + self.shared.kind.load(Ordering::SeqCst).into() + } + + fn source(&self) -> TrackSource { + self.shared.source.load(Ordering::SeqCst).into() + } + + fn simulcasted(&self) -> bool { + self.shared.simulcasted.load(Ordering::SeqCst) + } + } + } +} + + + #[derive(Clone)] pub struct LocalTrackPublication { shared: Arc @@ -21,8 +83,5 @@ pub struct RemoteTrackPublication { shared: Arc } -#[derive(Clone)] -pub enum TrackPublication { - Local(LocalTrackPublication), - Remote(RemoteTrackPublication) -} \ No newline at end of file +impl_publication_trait!(LocalTrackPublication); +impl_publication_trait!(RemoteTrackPublication); diff --git a/crates/livekit-webrtc/libwebrtc-sys/build.rs b/crates/livekit-webrtc/libwebrtc-sys/build.rs index c6d95cc..a65a667 100644 --- a/crates/livekit-webrtc/libwebrtc-sys/build.rs +++ b/crates/livekit-webrtc/libwebrtc-sys/build.rs @@ -52,7 +52,7 @@ fn macos_link_search_path() -> Option { fn main() { // TODO Download precompiled binaries of WebRTC for the target_os - let target_os = "windows"; + let target_os = "macos"; //let target_arch = "arm64"; let libwebrtc_dir = path::PathBuf::from("libwebrtc");