Initial downstream tracks
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
use futures_util::future::BoxFuture;
|
||||
use thiserror::Error;
|
||||
|
||||
type EventHandler<T> = Box<dyn FnMut(T) -> BoxFuture<'static, ()> + Send + Sync>;
|
||||
|
||||
macro_rules! event_setter {
|
||||
($fnc:ident, $event:ty) => {
|
||||
pub fn $fnc<F, Fut>(&self, mut callback: F)
|
||||
where
|
||||
F: FnMut($event) -> Fut + Send + Sync + 'static,
|
||||
Fut: Future<Output = ()> + Send + Sync + 'static,
|
||||
{
|
||||
*self.$fnc.lock() = Some(Box::new(move |event| Box::pin(callback(event))));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Error, Debug, Clone)]
|
||||
pub enum TrackError {
|
||||
#[error("could not find published track with sid: {0}")]
|
||||
TrackNotFound(String),
|
||||
}
|
||||
|
||||
pub mod room {
|
||||
use super::{EventHandler, TrackError};
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::participant::remote_participant::RemoteParticipant;
|
||||
use crate::room::publication::RemoteTrackPublication;
|
||||
use crate::room::track::remote_track::RemoteTrackHandle;
|
||||
use crate::room::RoomHandle;
|
||||
use futures::future::Future;
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ParticipantConnectedEvent {
|
||||
pub room_handle: RoomHandle,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ParticipantDisconnectedEvent {
|
||||
pub room_handle: RoomHandle,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TrackSubscribedEvent {
|
||||
pub room_handle: RoomHandle,
|
||||
pub track: RemoteTrackHandle,
|
||||
pub publication: RemoteTrackPublication,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TrackPublishedEvent {
|
||||
pub room_handle: RoomHandle,
|
||||
pub publication: RemoteTrackPublication,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TrackSubscriptionFailedEvent {
|
||||
pub room_handle: RoomHandle,
|
||||
pub error: TrackError,
|
||||
pub sid: TrackSid,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
pub(crate) type OnParticipantConnectedHandler = EventHandler<ParticipantConnectedEvent>;
|
||||
pub(crate) type OnParticipantDisconnectedHandler = EventHandler<ParticipantDisconnectedEvent>;
|
||||
pub(crate) type OnTrackSubscribedEventHandler = EventHandler<TrackSubscribedEvent>;
|
||||
pub(crate) type OnTrackPublishedEventHandler = EventHandler<TrackPublishedEvent>;
|
||||
pub(crate) type OnTrackSubscriptionFailedHandler = EventHandler<TrackSubscriptionFailedEvent>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RoomEvents {
|
||||
pub(crate) on_participant_connected: Mutex<Option<OnParticipantConnectedHandler>>,
|
||||
pub(crate) on_participant_disconnected: Mutex<Option<OnParticipantDisconnectedHandler>>,
|
||||
pub(crate) on_track_subscribed: Mutex<Option<OnTrackSubscribedEventHandler>>,
|
||||
pub(crate) on_track_published: Mutex<Option<OnTrackPublishedEventHandler>>,
|
||||
pub(crate) on_track_subscription_failed: Mutex<Option<OnTrackSubscriptionFailedHandler>>,
|
||||
}
|
||||
|
||||
impl RoomEvents {
|
||||
event_setter!(on_participant_connected, ParticipantConnectedEvent);
|
||||
event_setter!(on_participant_disconnected, ParticipantDisconnectedEvent);
|
||||
event_setter!(on_track_subscribed, TrackSubscribedEvent);
|
||||
event_setter!(on_track_published, TrackPublishedEvent);
|
||||
event_setter!(on_track_subscription_failed, TrackSubscriptionFailedEvent);
|
||||
}
|
||||
}
|
||||
|
||||
pub mod participant {
|
||||
use super::{EventHandler, TrackError};
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::participant::remote_participant::RemoteParticipant;
|
||||
use crate::room::publication::RemoteTrackPublication;
|
||||
use crate::room::track::remote_track::RemoteTrackHandle;
|
||||
use futures::future::Future;
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TrackPublishedEvent {
|
||||
pub publication: RemoteTrackPublication,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TrackSubscribedEvent {
|
||||
pub track: RemoteTrackHandle,
|
||||
pub publication: RemoteTrackPublication,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TrackSubscriptionFailedEvent {
|
||||
pub sid: TrackSid,
|
||||
pub error: TrackError,
|
||||
pub participant: Arc<RemoteParticipant>,
|
||||
}
|
||||
|
||||
pub(crate) type TrackPublishedHandler = EventHandler<TrackPublishedEvent>;
|
||||
pub(crate) type TrackSubscribedHandler = EventHandler<TrackSubscribedEvent>;
|
||||
pub(crate) type TrackSubscriptionFailedHandler = EventHandler<TrackSubscriptionFailedEvent>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ParticipantEvents {
|
||||
pub(crate) on_track_published: Mutex<Option<TrackPublishedHandler>>,
|
||||
pub(crate) on_track_subscribed: Mutex<Option<TrackSubscribedHandler>>,
|
||||
pub(crate) on_track_subscription_failed: Mutex<Option<TrackSubscriptionFailedHandler>>,
|
||||
}
|
||||
|
||||
impl ParticipantEvents {
|
||||
event_setter!(on_track_published, TrackPublishedEvent);
|
||||
event_setter!(on_track_subscribed, TrackSubscribedEvent);
|
||||
event_setter!(on_track_subscription_failed, TrackSubscriptionFailedEvent);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
extern crate core;
|
||||
|
||||
pub mod proto {
|
||||
include!(concat!(env!("OUT_DIR"), "/livekit.rs"));
|
||||
}
|
||||
|
||||
mod events;
|
||||
mod rtc_engine;
|
||||
mod signal_client;
|
||||
mod utils;
|
||||
|
||||
pub mod room;
|
||||
|
||||
@@ -1,29 +1,27 @@
|
||||
use futures_util::future::BoxFuture;
|
||||
use parking_lot::lock_api::RwLockUpgradableReadGuard;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use std::collections::HashMap;
|
||||
use std::future::Future;
|
||||
use std::sync::atomic::AtomicU8;
|
||||
use std::sync::Arc;
|
||||
|
||||
use self::id::ParticipantSid;
|
||||
use self::participant::local_participant::LocalParticipant;
|
||||
use self::participant::remote_participant::RemoteParticipant;
|
||||
use self::participant::ParticipantInternalTrait;
|
||||
use self::participant::ParticipantTrait;
|
||||
use crate::events::room::{ParticipantConnectedEvent, ParticipantDisconnectedEvent, RoomEvents};
|
||||
use crate::proto;
|
||||
use crate::proto::{participant_info, ParticipantInfo};
|
||||
use crate::room::id::{ParticipantIdentity, ParticipantSid};
|
||||
use crate::room::local_participant::LocalParticipant;
|
||||
use crate::room::participant::ParticipantTrait;
|
||||
use crate::room::remote_participant::RemoteParticipant;
|
||||
use crate::proto::participant_info;
|
||||
use thiserror::Error;
|
||||
use tracing::error;
|
||||
|
||||
use crate::rtc_engine::{EngineError, EngineEvent, EngineEvents, RTCEngine};
|
||||
use crate::signal_client::SignalOptions;
|
||||
|
||||
mod id;
|
||||
mod local_participant;
|
||||
mod participant;
|
||||
mod remote_participant;
|
||||
mod track;
|
||||
mod track_publication;
|
||||
pub mod id;
|
||||
pub mod participant;
|
||||
pub mod publication;
|
||||
pub mod track;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RoomError {
|
||||
@@ -52,15 +50,6 @@ struct RoomInner {
|
||||
local_participant: Arc<LocalParticipant>,
|
||||
}
|
||||
|
||||
type OnParticipantConnectedHandler =
|
||||
Box<dyn FnMut(RoomHandle, Arc<RemoteParticipant>) -> BoxFuture<'static, ()> + Send + Sync>;
|
||||
type OnParticipantDisconnectedHandler = OnParticipantConnectedHandler;
|
||||
|
||||
struct RoomEvents {
|
||||
on_participant_connected_handler: Mutex<Option<OnParticipantConnectedHandler>>,
|
||||
on_participant_disconnected_handler: Mutex<Option<OnParticipantDisconnectedHandler>>,
|
||||
}
|
||||
|
||||
pub struct Room {
|
||||
inner: Option<Arc<RoomInner>>,
|
||||
events: Arc<RoomEvents>,
|
||||
@@ -70,13 +59,14 @@ impl Room {
|
||||
pub fn new() -> Room {
|
||||
Self {
|
||||
inner: None,
|
||||
events: Arc::new(RoomEvents {
|
||||
on_participant_connected_handler: Default::default(),
|
||||
on_participant_disconnected_handler: Default::default(),
|
||||
}),
|
||||
events: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn events(&self) -> Arc<RoomEvents> {
|
||||
self.events.clone()
|
||||
}
|
||||
|
||||
pub async fn connect(&mut self, url: &str, token: &str) -> RoomResult<()> {
|
||||
let (rtc_engine, engine_events) =
|
||||
RTCEngine::connect(url, token, SignalOptions::default()).await?;
|
||||
@@ -109,28 +99,6 @@ impl Room {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn on_participant_connected<F, Fut>(&self, mut callback: F)
|
||||
where
|
||||
F: FnMut(RoomHandle, Arc<RemoteParticipant>) -> Fut + Send + Sync + 'static,
|
||||
Fut: Future<Output = ()> + Send + Sync + 'static,
|
||||
{
|
||||
*self.events.on_participant_connected_handler.lock() =
|
||||
Some(Box::new(move |handle, participant| {
|
||||
Box::pin(callback(handle, participant))
|
||||
}));
|
||||
}
|
||||
|
||||
pub fn on_participant_disconnected<F, Fut>(&self, mut callback: F)
|
||||
where
|
||||
F: FnMut(RoomHandle, Arc<RemoteParticipant>) -> Fut + Send + Sync + 'static,
|
||||
Fut: Future<Output = ()> + Send + Sync + 'static,
|
||||
{
|
||||
*self.events.on_participant_disconnected_handler.lock() =
|
||||
Some(Box::new(move |handle, participant| {
|
||||
Box::pin(callback(handle, participant))
|
||||
}));
|
||||
}
|
||||
|
||||
async fn room_task(
|
||||
room_inner: Arc<RoomInner>,
|
||||
room_events: Arc<RoomEvents>,
|
||||
@@ -178,7 +146,10 @@ impl Room {
|
||||
Self::get_participant(room_inner.clone(), &participant_sid.to_string().into());
|
||||
|
||||
if let Some(remote_participant) = remote_participant {
|
||||
|
||||
remote_participant.add_subscribed_media_track(
|
||||
track_sid.to_string().into(),
|
||||
rtp_receiver.track(),
|
||||
);
|
||||
} else {
|
||||
// The server should send participant updates before sending a new offer
|
||||
// So this should not happen.
|
||||
@@ -223,13 +194,14 @@ impl Room {
|
||||
}
|
||||
} else {
|
||||
// Create a new participant and call OnConnect event
|
||||
let remote_participant = Self::get_or_create_participant(room_inner.clone(), pi);
|
||||
let mut handler = room_events.on_participant_connected_handler.lock();
|
||||
if let Some(callback) = handler.as_mut() {
|
||||
callback(
|
||||
RoomHandle::from(room_inner.clone()),
|
||||
remote_participant.clone(),
|
||||
);
|
||||
let remote_participant =
|
||||
Self::get_or_create_participant(room_inner.clone(), room_events.clone(), pi);
|
||||
let mut handler = room_events.on_participant_connected.lock();
|
||||
if let Some(cb) = handler.as_mut() {
|
||||
cb(ParticipantConnectedEvent {
|
||||
room_handle: RoomHandle::from(room_inner.clone()),
|
||||
participant: remote_participant.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -247,12 +219,12 @@ impl Room {
|
||||
|
||||
// TODO(theomonnom): Unpublish all tracks
|
||||
|
||||
let mut handler = room_events.on_participant_disconnected_handler.lock();
|
||||
if let Some(callback) = handler.as_mut() {
|
||||
callback(
|
||||
RoomHandle::from(room_inner.clone()),
|
||||
remote_participant.clone(),
|
||||
);
|
||||
let mut handler = room_events.on_participant_disconnected.lock();
|
||||
if let Some(cb) = handler.as_mut() {
|
||||
cb(ParticipantDisconnectedEvent {
|
||||
room_handle: RoomHandle::from(room_inner.clone()),
|
||||
participant: remote_participant.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,6 +237,7 @@ impl Room {
|
||||
|
||||
fn get_or_create_participant(
|
||||
room_inner: Arc<RoomInner>,
|
||||
room_events: Arc<RoomEvents>,
|
||||
pi: proto::ParticipantInfo,
|
||||
) -> Arc<RemoteParticipant> {
|
||||
let participants = room_inner.participants.upgradable_read();
|
||||
@@ -275,6 +248,13 @@ impl Room {
|
||||
} else {
|
||||
let mut participants = RwLockUpgradableReadGuard::upgrade(participants);
|
||||
let p = Arc::new(RemoteParticipant::new(pi));
|
||||
|
||||
// Forward participantevents to room events
|
||||
p.internal_events().on_track_published({
|
||||
let room_events = room_events.clone();
|
||||
|event| async move {}
|
||||
});
|
||||
|
||||
participants.insert(sid, p.clone());
|
||||
p
|
||||
}
|
||||
|
||||
+1
-2
@@ -2,7 +2,6 @@ use crate::proto::{data_packet, DataPacket, UserPacket};
|
||||
use crate::room::participant::{impl_participant_trait, ParticipantShared};
|
||||
use crate::room::RoomError;
|
||||
use crate::rtc_engine::RTCEngine;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct LocalParticipant {
|
||||
shared: ParticipantShared,
|
||||
@@ -10,7 +9,7 @@ pub struct LocalParticipant {
|
||||
}
|
||||
|
||||
impl LocalParticipant {
|
||||
pub(super) fn new(rtc_engine: Arc<RTCEngine>, info: ParticipantInfo) -> Self {
|
||||
pub(crate) fn new(rtc_engine: Arc<RTCEngine>, info: ParticipantInfo) -> Self {
|
||||
Self {
|
||||
shared: ParticipantShared::new(
|
||||
info.sid.into(),
|
||||
+58
-30
@@ -1,11 +1,23 @@
|
||||
use crate::events::participant::ParticipantEvents;
|
||||
use crate::proto::ParticipantInfo;
|
||||
use crate::room::local_participant::LocalParticipant;
|
||||
use crate::room::remote_participant::RemoteParticipant;
|
||||
use crate::room::id::{ParticipantIdentity, ParticipantSid, TrackSid};
|
||||
use crate::room::participant::local_participant::LocalParticipant;
|
||||
use crate::room::participant::remote_participant::RemoteParticipant;
|
||||
use crate::room::publication::{TrackPublication, TrackPublicationTrait};
|
||||
use crate::utils::wrap_variants;
|
||||
use futures_util::future::BoxFuture;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub mod local_participant;
|
||||
pub mod remote_participant;
|
||||
|
||||
type OnTrackSubscribed = Box<dyn FnMut(ParticipantHandle) -> BoxFuture<'static, ()> + Send + Sync>;
|
||||
|
||||
pub(super) struct ParticipantShared {
|
||||
pub(super) events: Arc<ParticipantEvents>,
|
||||
pub(super) internal_events: Arc<ParticipantEvents>,
|
||||
pub(super) sid: Mutex<ParticipantSid>,
|
||||
pub(super) identity: Mutex<ParticipantIdentity>,
|
||||
pub(super) name: Mutex<String>,
|
||||
@@ -21,6 +33,8 @@ impl ParticipantShared {
|
||||
metadata: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
events: Default::default(),
|
||||
internal_events: Default::default(),
|
||||
sid: Mutex::new(sid),
|
||||
identity: Mutex::new(identity),
|
||||
name: Mutex::new(name),
|
||||
@@ -33,11 +47,20 @@ impl ParticipantShared {
|
||||
*self.sid.lock() = info.sid.into();
|
||||
*self.identity.lock() = info.identity.into();
|
||||
*self.name.lock() = info.name;
|
||||
*self.metadata.lock() = info.metadata; // TODO(theomonnom): callback
|
||||
*self.metadata.lock() = info.metadata; // TODO(theomonnom): callback MetadataChanged
|
||||
}
|
||||
|
||||
pub(crate) fn add_track_publication(&self, publication: TrackPublication) {
|
||||
self.tracks.write().insert(publication.sid(), publication);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait ParticipantInternalTrait {
|
||||
fn internal_events(&self) -> Arc<ParticipantEvents>;
|
||||
}
|
||||
|
||||
pub trait ParticipantTrait {
|
||||
fn events(&self) -> Arc<ParticipantEvents>;
|
||||
fn sid(&self) -> ParticipantSid;
|
||||
fn identity(&self) -> ParticipantIdentity;
|
||||
fn name(&self) -> String;
|
||||
@@ -45,42 +68,49 @@ pub trait ParticipantTrait {
|
||||
fn update_info(&self, info: ParticipantInfo);
|
||||
}
|
||||
|
||||
pub enum Participant {
|
||||
Local(LocalParticipant),
|
||||
Remote(RemoteParticipant),
|
||||
#[derive(Clone)]
|
||||
pub enum ParticipantHandle {
|
||||
Local(Arc<LocalParticipant>),
|
||||
Remote(Arc<RemoteParticipant>),
|
||||
}
|
||||
|
||||
macro_rules! shared_getter {
|
||||
($x:ident, $ret:ident) => {
|
||||
fn $x(&self) -> $ret {
|
||||
match self {
|
||||
Participant::Local(p) => p.$x(),
|
||||
Participant::Remote(p) => p.$x(),
|
||||
}
|
||||
}
|
||||
};
|
||||
impl ParticipantInternalTrait for ParticipantHandle {
|
||||
wrap_variants!(
|
||||
[Local, Remote]
|
||||
fnc!(internal_events, Arc<ParticipantEvents>, []);
|
||||
);
|
||||
}
|
||||
|
||||
impl ParticipantTrait for Participant {
|
||||
shared_getter!(sid, ParticipantSid);
|
||||
shared_getter!(identity, ParticipantIdentity);
|
||||
shared_getter!(name, String);
|
||||
shared_getter!(metadata, String);
|
||||
|
||||
fn update_info(&self, info: ParticipantInfo) {
|
||||
match self {
|
||||
Participant::Local(p) => p.update_info(info),
|
||||
Participant::Remote(p) => p.update_info(info),
|
||||
}
|
||||
}
|
||||
impl ParticipantTrait for ParticipantHandle {
|
||||
wrap_variants!(
|
||||
[Local, Remote]
|
||||
fnc!(events, Arc<ParticipantEvents>, []);
|
||||
fnc!(sid, ParticipantSid, []);
|
||||
fnc!(identity, ParticipantIdentity, []);
|
||||
fnc!(name, String, []);
|
||||
fnc!(metadata, String, []);
|
||||
fnc!(update_info, (), [info: ParticipantInfo]);
|
||||
);
|
||||
}
|
||||
|
||||
macro_rules! impl_participant_trait {
|
||||
($x:ident) => {
|
||||
($x:ty) => {
|
||||
use crate::events::participant::ParticipantEvents;
|
||||
use crate::proto::ParticipantInfo;
|
||||
use crate::room::id::{ParticipantIdentity, ParticipantSid};
|
||||
use std::sync::Arc;
|
||||
|
||||
impl crate::room::participant::ParticipantInternalTrait for $x {
|
||||
fn internal_events(&self) -> Arc<ParticipantEvents> {
|
||||
self.shared.internal_events.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::room::participant::ParticipantTrait for $x {
|
||||
fn events(&self) -> Arc<ParticipantEvents> {
|
||||
self.shared.events.clone()
|
||||
}
|
||||
|
||||
fn sid(&self) -> ParticipantSid {
|
||||
self.shared.sid.lock().clone()
|
||||
}
|
||||
@@ -104,6 +134,4 @@ macro_rules! impl_participant_trait {
|
||||
};
|
||||
}
|
||||
|
||||
use crate::room::id::{ParticipantIdentity, ParticipantSid, TrackSid};
|
||||
use crate::room::track_publication::TrackPublication;
|
||||
pub(super) use impl_participant_trait;
|
||||
@@ -0,0 +1,149 @@
|
||||
use crate::events::participant::{TrackSubscribedEvent, TrackSubscriptionFailedEvent};
|
||||
use crate::events::TrackError;
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::participant::{impl_participant_trait, ParticipantShared};
|
||||
use crate::room::publication::{RemoteTrackPublication, TrackPublication, TrackPublicationTrait};
|
||||
use crate::room::track::remote_audio_track::RemoteAudioTrack;
|
||||
use crate::room::track::remote_track::RemoteTrackHandle;
|
||||
use crate::room::track::remote_video_track::RemoteVideoTrack;
|
||||
use crate::room::track::{TrackTrait, TrackKind};
|
||||
use livekit_webrtc::media_stream::MediaStreamTrackHandle;
|
||||
use std::time::Duration;
|
||||
use tokio::time::{sleep, timeout};
|
||||
use tracing::error;
|
||||
|
||||
const ADD_TRACK_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
pub struct RemoteParticipant {
|
||||
shared: ParticipantShared,
|
||||
}
|
||||
|
||||
impl RemoteParticipant {
|
||||
pub(crate) fn new(info: ParticipantInfo) -> Self {
|
||||
Self {
|
||||
shared: ParticipantShared::new(
|
||||
info.sid.into(),
|
||||
info.identity.into(),
|
||||
info.name,
|
||||
info.metadata,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn add_subscribed_media_track(
|
||||
self: Arc<Self>,
|
||||
sid: TrackSid,
|
||||
media_track: MediaStreamTrackHandle,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
let wait_publication = {
|
||||
let participant = self.clone();
|
||||
let sid = sid.clone();
|
||||
async move {
|
||||
loop {
|
||||
let publication = participant.get_track_publication(&sid);
|
||||
if let Some(publication) = publication {
|
||||
return publication;
|
||||
}
|
||||
|
||||
sleep(Duration::from_millis(50)).await;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if let Ok(remote_publication) = timeout(ADD_TRACK_TIMEOUT, wait_publication).await {
|
||||
let track = match remote_publication.kind() {
|
||||
TrackKind::Audio => {
|
||||
if let MediaStreamTrackHandle::Audio(rtc_track) = media_track {
|
||||
let audio_track = RemoteAudioTrack::new(
|
||||
remote_publication.sid().into(),
|
||||
remote_publication.name(),
|
||||
rtc_track,
|
||||
);
|
||||
RemoteTrackHandle::Audio(Arc::new(audio_track))
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
TrackKind::Video => {
|
||||
if let MediaStreamTrackHandle::Video(rtc_track) = media_track {
|
||||
let video_track = RemoteVideoTrack::new(
|
||||
remote_publication.sid().into(),
|
||||
remote_publication.name(),
|
||||
rtc_track,
|
||||
);
|
||||
RemoteTrackHandle::Video(Arc::new(video_track))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
self.shared
|
||||
.add_track_publication(TrackPublication::Remote(remote_publication.clone()));
|
||||
track.start();
|
||||
|
||||
let event = TrackSubscribedEvent {
|
||||
track,
|
||||
publication: remote_publication,
|
||||
participant: self.clone(),
|
||||
};
|
||||
|
||||
if let Some(cb) = self
|
||||
.shared
|
||||
.internal_events
|
||||
.on_track_subscribed
|
||||
.lock()
|
||||
.as_mut()
|
||||
{
|
||||
cb(event.clone()).await;
|
||||
}
|
||||
|
||||
if let Some(cb) = self.shared.events.on_track_subscribed.lock().as_mut() {
|
||||
cb(event).await;
|
||||
}
|
||||
} else {
|
||||
error!("could not find published track with sid: {:?}", sid);
|
||||
|
||||
let event = TrackSubscriptionFailedEvent {
|
||||
sid: sid.clone(),
|
||||
error: TrackError::TrackNotFound(sid.clone().to_string()),
|
||||
participant: self.clone(),
|
||||
};
|
||||
|
||||
if let Some(cb) = self
|
||||
.shared
|
||||
.internal_events
|
||||
.on_track_subscription_failed
|
||||
.lock()
|
||||
.as_mut()
|
||||
{
|
||||
cb(event.clone()).await;
|
||||
}
|
||||
|
||||
if let Some(cb) = self
|
||||
.shared
|
||||
.events
|
||||
.on_track_subscription_failed
|
||||
.lock()
|
||||
.as_mut()
|
||||
{
|
||||
cb(event).await;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn get_track_publication(&self, sid: &TrackSid) -> Option<RemoteTrackPublication> {
|
||||
self.shared.tracks.read().get(sid).map(|track| {
|
||||
if let TrackPublication::Remote(remote) = track {
|
||||
remote.clone()
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl_participant_trait!(RemoteParticipant);
|
||||
+43
-13
@@ -1,8 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::track::local_track::LocalTrackHandle;
|
||||
use crate::room::track::remote_track::RemoteTrackHandle;
|
||||
use crate::room::track::{TrackHandle, TrackKind, TrackSource};
|
||||
use parking_lot::Mutex;
|
||||
use crate::room::id::{ParticipantIdentity, ParticipantSid, TrackSid};
|
||||
use crate::room::track::{TrackKind, TrackSource};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub trait TrackPublicationTrait {
|
||||
fn name(&self) -> String;
|
||||
@@ -13,21 +15,22 @@ pub trait TrackPublicationTrait {
|
||||
}
|
||||
|
||||
pub(super) struct TrackPublicationShared {
|
||||
pub(super) track: Mutex<Option<TrackHandle>>,
|
||||
pub(super) name: Mutex<String>,
|
||||
pub(super) sid: Mutex<TrackSid>,
|
||||
pub(super) kind: AtomicU8, // Casted to TrackKind
|
||||
pub(super) kind: AtomicU8, // Casted to TrackKind
|
||||
pub(super) source: AtomicU8, // Casted to TrackSource
|
||||
pub(super) simulcasted: AtomicBool
|
||||
pub(super) simulcasted: AtomicBool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum TrackPublication {
|
||||
Local(LocalTrackPublication),
|
||||
Remote(RemoteTrackPublication)
|
||||
Remote(RemoteTrackPublication),
|
||||
}
|
||||
|
||||
macro_rules! shared_getter {
|
||||
($x:ident, $ret:ident) => {
|
||||
($x:ident, $ret:ty) => {
|
||||
fn $x(&self) -> $ret {
|
||||
match self {
|
||||
TrackPublication::Local(p) => p.$x(),
|
||||
@@ -37,6 +40,15 @@ macro_rules! shared_getter {
|
||||
};
|
||||
}
|
||||
|
||||
impl TrackPublication {
|
||||
pub fn track(&self) -> Option<TrackHandle> {
|
||||
match self {
|
||||
TrackPublication::Local(p) => p.shared.track.lock().clone(),
|
||||
TrackPublication::Remote(p) => p.shared.track.lock().clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TrackPublicationTrait for TrackPublication {
|
||||
shared_getter!(name, String);
|
||||
shared_getter!(sid, TrackSid);
|
||||
@@ -68,19 +80,37 @@ macro_rules! impl_publication_trait {
|
||||
self.shared.simulcasted.load(Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LocalTrackPublication {
|
||||
shared: Arc<TrackPublicationShared>
|
||||
shared: Arc<TrackPublicationShared>,
|
||||
}
|
||||
|
||||
impl LocalTrackPublication {
|
||||
pub fn track(&self) -> Option<LocalTrackHandle> {
|
||||
self.shared
|
||||
.track
|
||||
.lock()
|
||||
.clone()
|
||||
.map(|local_track| local_track.try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RemoteTrackPublication {
|
||||
shared: Arc<TrackPublicationShared>
|
||||
shared: Arc<TrackPublicationShared>,
|
||||
}
|
||||
|
||||
impl RemoteTrackPublication {
|
||||
pub fn track(&self) -> Option<RemoteTrackHandle> {
|
||||
self.shared
|
||||
.track
|
||||
.lock()
|
||||
.clone()
|
||||
.map(|track| track.try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl_publication_trait!(LocalTrackPublication);
|
||||
@@ -1,83 +0,0 @@
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::participant::{impl_participant_trait, ParticipantShared};
|
||||
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,
|
||||
}
|
||||
|
||||
impl RemoteParticipant {
|
||||
pub(super) fn new(info: ParticipantInfo) -> Self {
|
||||
Self {
|
||||
shared: ParticipantShared::new(
|
||||
info.sid.into(),
|
||||
info.identity.into(),
|
||||
info.name,
|
||||
info.metadata,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
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: &TrackSid) -> Option<RemoteTrackPublication> {
|
||||
self.shared.tracks.read().get(sid).map(|track| {
|
||||
if let TrackPublication::Remote(remote) = track {
|
||||
remote.clone()
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl_participant_trait!(RemoteParticipant);
|
||||
@@ -0,0 +1,31 @@
|
||||
use crate::room::track::local_audio_track::LocalAudioTrack;
|
||||
use crate::room::track::remote_audio_track::RemoteAudioTrack;
|
||||
use crate::room::track::TrackHandle;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum AudioTrackHandle {
|
||||
Local(Arc<LocalAudioTrack>),
|
||||
Remote(Arc<RemoteAudioTrack>),
|
||||
}
|
||||
|
||||
impl From<AudioTrackHandle> for TrackHandle {
|
||||
fn from(audio_track: AudioTrackHandle) -> Self {
|
||||
match audio_track {
|
||||
AudioTrackHandle::Local(local_audio) => Self::LocalAudio(local_audio),
|
||||
AudioTrackHandle::Remote(remote_audio) => Self::RemoteAudio(remote_audio),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<TrackHandle> for AudioTrackHandle {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: TrackHandle) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
TrackHandle::LocalAudio(local_audio) => Ok(Self::Local(local_audio)),
|
||||
TrackHandle::RemoteAudio(remote_audio) => Ok(Self::Remote(remote_audio)),
|
||||
_ => Err("not a audio track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub struct TrackEvents {}
|
||||
@@ -0,0 +1,7 @@
|
||||
use crate::room::track::{impl_track_trait, TrackShared};
|
||||
|
||||
pub struct LocalAudioTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
impl_track_trait!(LocalAudioTrack);
|
||||
@@ -0,0 +1,31 @@
|
||||
use crate::room::track::local_audio_track::LocalAudioTrack;
|
||||
use crate::room::track::local_video_track::LocalVideoTrack;
|
||||
use crate::room::track::TrackHandle;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum LocalTrackHandle {
|
||||
Audio(Arc<LocalAudioTrack>),
|
||||
Video(Arc<LocalVideoTrack>),
|
||||
}
|
||||
|
||||
impl From<LocalTrackHandle> for TrackHandle {
|
||||
fn from(local_track: LocalTrackHandle) -> Self {
|
||||
match local_track {
|
||||
LocalTrackHandle::Audio(local_audio) => Self::LocalAudio(local_audio),
|
||||
LocalTrackHandle::Video(local_video) => Self::LocalVideo(local_video),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<TrackHandle> for LocalTrackHandle {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: TrackHandle) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
TrackHandle::LocalAudio(local_audio) => Ok(Self::Audio(local_audio)),
|
||||
TrackHandle::LocalVideo(local_video) => Ok(Self::Video(local_video)),
|
||||
_ => Err("not a local track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
use crate::room::track::{impl_track_trait, TrackShared};
|
||||
|
||||
pub struct LocalVideoTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
impl_track_trait!(LocalVideoTrack);
|
||||
@@ -1,3 +1,24 @@
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::track::local_audio_track::LocalAudioTrack;
|
||||
use crate::room::track::local_video_track::LocalVideoTrack;
|
||||
use crate::room::track::remote_audio_track::RemoteAudioTrack;
|
||||
use crate::room::track::remote_video_track::RemoteVideoTrack;
|
||||
use crate::utils::wrap_variants;
|
||||
use livekit_webrtc::media_stream::{MediaStreamTrackHandle, MediaStreamTrackTrait};
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::atomic::AtomicU8;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub mod audio_track;
|
||||
pub mod events;
|
||||
pub mod local_audio_track;
|
||||
pub mod local_track;
|
||||
pub mod local_video_track;
|
||||
pub mod remote_audio_track;
|
||||
pub mod remote_track;
|
||||
pub mod remote_video_track;
|
||||
pub mod video_track;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TrackKind {
|
||||
Unknown,
|
||||
@@ -22,6 +43,16 @@ pub enum StreamState {
|
||||
Paused,
|
||||
}
|
||||
|
||||
impl From<u8> for StreamState {
|
||||
fn from(val: u8) -> Self {
|
||||
match val {
|
||||
1 => Self::Active,
|
||||
2 => Self::Paused,
|
||||
_ => Self::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TrackSource {
|
||||
Unknown,
|
||||
@@ -43,135 +74,114 @@ impl From<u8> for TrackSource {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LocalVideoTrack {}
|
||||
pub struct RemoteVideoTrack {}
|
||||
pub struct LocalAudioTrack {}
|
||||
|
||||
|
||||
pub struct RemoteAudioTrack {
|
||||
|
||||
|
||||
pub trait TrackTrait {
|
||||
fn sid(&self) -> TrackSid;
|
||||
fn name(&self) -> String;
|
||||
fn kind(&self) -> TrackKind;
|
||||
fn stream_state(&self) -> StreamState;
|
||||
fn start(&self);
|
||||
fn stop(&self);
|
||||
}
|
||||
|
||||
impl RemoteVideoTrack {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {}
|
||||
pub(super) struct TrackShared {
|
||||
pub(super) sid: Mutex<TrackSid>,
|
||||
pub(super) name: Mutex<String>,
|
||||
pub(super) kind: AtomicU8, // TrackKind
|
||||
pub(super) stream_state: AtomicU8, // StreamState
|
||||
pub(super) rtc_track: MediaStreamTrackHandle,
|
||||
}
|
||||
|
||||
impl TrackShared {
|
||||
pub(crate) fn new(
|
||||
sid: TrackSid,
|
||||
name: String,
|
||||
kind: TrackKind,
|
||||
rtc_track: MediaStreamTrackHandle,
|
||||
) -> Self {
|
||||
Self {
|
||||
sid: Mutex::new(sid),
|
||||
name: Mutex::new(name),
|
||||
kind: AtomicU8::new(kind as u8),
|
||||
stream_state: AtomicU8::new(StreamState::Active as u8),
|
||||
rtc_track: rtc_track,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn start(&self) {
|
||||
self.rtc_track.set_enabled(true);
|
||||
}
|
||||
|
||||
pub(crate) fn stop(&self) {
|
||||
self.rtc_track.set_enabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
impl RemoteAudioTrack {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub enum TrackHandle {
|
||||
LocalVideo(Arc<LocalVideoTrack>),
|
||||
LocalAudio(Arc<LocalAudioTrack>),
|
||||
RemoteVideo(Arc<RemoteVideoTrack>),
|
||||
RemoteAudio(Arc<RemoteAudioTrack>),
|
||||
}
|
||||
|
||||
pub enum RemoteTrack {
|
||||
Audio(RemoteAudioTrack),
|
||||
Video(RemoteVideoTrack),
|
||||
impl TrackTrait for TrackHandle {
|
||||
wrap_variants!(
|
||||
[LocalVideo, LocalAudio, RemoteVideo, RemoteAudio]
|
||||
fnc!(sid, TrackSid, []);
|
||||
fnc!(name, String, []);
|
||||
fnc!(kind, TrackKind, []);
|
||||
fnc!(stream_state, StreamState, []);
|
||||
fnc!(start, (), []);
|
||||
fnc!(stop, (), []);
|
||||
);
|
||||
}
|
||||
|
||||
pub enum LocalTrack {
|
||||
Audio(LocalAudioTrack),
|
||||
Video(LocalVideoTrack),
|
||||
}
|
||||
|
||||
pub enum VideoTrack {
|
||||
Local(LocalVideoTrack),
|
||||
Remote(RemoteVideoTrack),
|
||||
}
|
||||
|
||||
pub enum AudioTrack {
|
||||
Local(LocalAudioTrack),
|
||||
Remote(RemoteAudioTrack),
|
||||
}
|
||||
|
||||
pub enum Track {
|
||||
LocalVideo(LocalVideoTrack),
|
||||
LocalAudio(LocalAudioTrack),
|
||||
RemoteVideo(RemoteVideoTrack),
|
||||
RemoteAudio(RemoteAudioTrack),
|
||||
}
|
||||
|
||||
impl From<VideoTrack> for Track {
|
||||
fn from(video_track: VideoTrack) -> Self {
|
||||
match video_track {
|
||||
VideoTrack::Local(local_video) => Self::LocalVideo(local_video),
|
||||
VideoTrack::Remote(remote_video) => Self::RemoteVideo(remote_video),
|
||||
impl TrackHandle {
|
||||
pub fn rtc_track(&self) -> MediaStreamTrackHandle {
|
||||
match self {
|
||||
Self::RemoteVideo(remote_video) => {
|
||||
MediaStreamTrackHandle::Video(remote_video.rtc_track())
|
||||
}
|
||||
Self::RemoteAudio(remote_audio) => {
|
||||
MediaStreamTrackHandle::Audio(remote_audio.rtc_track())
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AudioTrack> for Track {
|
||||
fn from(audio_track: AudioTrack) -> Self {
|
||||
match audio_track {
|
||||
AudioTrack::Local(local_audio) => Self::LocalAudio(local_audio),
|
||||
AudioTrack::Remote(remote_audio) => Self::RemoteAudio(remote_audio),
|
||||
macro_rules! impl_track_trait {
|
||||
($x:ident) => {
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::track::{StreamState, TrackKind, TrackTrait};
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
impl TrackTrait for $x {
|
||||
fn sid(&self) -> TrackSid {
|
||||
self.shared.sid.lock().clone()
|
||||
}
|
||||
|
||||
fn name(&self) -> String {
|
||||
self.shared.name.lock().clone()
|
||||
}
|
||||
|
||||
fn kind(&self) -> TrackKind {
|
||||
self.shared.kind.load(Ordering::SeqCst).into()
|
||||
}
|
||||
|
||||
fn stream_state(&self) -> StreamState {
|
||||
self.shared.stream_state.load(Ordering::SeqCst).into()
|
||||
}
|
||||
|
||||
fn start(&self) {
|
||||
self.shared.start();
|
||||
}
|
||||
|
||||
fn stop(&self) {
|
||||
self.shared.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl From<LocalTrack> for Track {
|
||||
fn from(local_track: LocalTrack) -> Self {
|
||||
match local_track {
|
||||
LocalTrack::Audio(local_audio) => Self::LocalAudio(local_audio),
|
||||
LocalTrack::Video(local_video) => Self::LocalVideo(local_video),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RemoteTrack> for Track {
|
||||
fn from(remote_track: RemoteTrack) -> Self {
|
||||
match remote_track {
|
||||
RemoteTrack::Audio(remote_audio) => Self::RemoteAudio(remote_audio),
|
||||
RemoteTrack::Video(remote_video) => Self::RemoteVideo(remote_video),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Track> for VideoTrack {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: Track) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
Track::LocalVideo(local_video) => Ok(Self::Local(local_video)),
|
||||
Track::RemoteVideo(remote_video) => Ok(Self::Remote(remote_video)),
|
||||
_ => Err("not a video track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Track> for AudioTrack {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: Track) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
Track::LocalAudio(local_audio) => Ok(Self::Local(local_audio)),
|
||||
Track::RemoteAudio(remote_audio) => Ok(Self::Remote(remote_audio)),
|
||||
_ => Err("not a audio track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Track> for LocalTrack {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: Track) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
Track::LocalAudio(local_audio) => Ok(Self::Audio(local_audio)),
|
||||
Track::LocalVideo(local_video) => Ok(Self::Video(local_video)),
|
||||
_ => Err("not a local track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Track> for RemoteTrack {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: Track) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
Track::RemoteAudio(remote_audio) => Ok(Self::Audio(remote_audio)),
|
||||
Track::RemoteVideo(remote_video) => Ok(Self::Video(remote_video)),
|
||||
_ => Err("not a remote track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) use impl_track_trait;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
use crate::room::track::{impl_track_trait, TrackShared};
|
||||
use livekit_webrtc::media_stream::{AudioTrack, MediaStreamTrackHandle};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct RemoteAudioTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
impl RemoteAudioTrack {
|
||||
pub(crate) fn new(sid: TrackSid, name: String, track: Arc<AudioTrack>) -> Self {
|
||||
Self {
|
||||
shared: TrackShared::new(
|
||||
sid,
|
||||
name,
|
||||
TrackKind::Audio,
|
||||
MediaStreamTrackHandle::Audio(track),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rtc_track(&self) -> Arc<AudioTrack> {
|
||||
if let MediaStreamTrackHandle::Audio(audio) = &self.shared.rtc_track {
|
||||
audio.clone()
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_track_trait!(RemoteAudioTrack);
|
||||
@@ -0,0 +1,49 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::{StreamState, TrackKind};
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::track::remote_audio_track::RemoteAudioTrack;
|
||||
use crate::room::track::remote_video_track::RemoteVideoTrack;
|
||||
use crate::room::track::TrackHandle;
|
||||
use crate::utils::wrap_variants;
|
||||
|
||||
use super::TrackTrait;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum RemoteTrackHandle {
|
||||
Audio(Arc<RemoteAudioTrack>),
|
||||
Video(Arc<RemoteVideoTrack>),
|
||||
}
|
||||
|
||||
impl TrackTrait for RemoteTrackHandle {
|
||||
wrap_variants!(
|
||||
[Audio, Video]
|
||||
fnc!(sid, TrackSid, []);
|
||||
fnc!(name, String, []);
|
||||
fnc!(kind, TrackKind, []);
|
||||
fnc!(stream_state, StreamState, []);
|
||||
fnc!(start, (), []);
|
||||
fnc!(stop, (), []);
|
||||
);
|
||||
}
|
||||
|
||||
impl From<RemoteTrackHandle> for TrackHandle {
|
||||
fn from(remote_track: RemoteTrackHandle) -> Self {
|
||||
match remote_track {
|
||||
RemoteTrackHandle::Audio(remote_audio) => Self::RemoteAudio(remote_audio),
|
||||
RemoteTrackHandle::Video(remote_video) => Self::RemoteVideo(remote_video),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<TrackHandle> for RemoteTrackHandle {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: TrackHandle) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
TrackHandle::RemoteAudio(remote_audio) => Ok(Self::Audio(remote_audio)),
|
||||
TrackHandle::RemoteVideo(remote_video) => Ok(Self::Video(remote_video)),
|
||||
_ => Err("not a remote track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use livekit_webrtc::media_stream::{MediaStreamTrackHandle, VideoTrack};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::room::track::{impl_track_trait, TrackShared};
|
||||
|
||||
pub struct RemoteVideoTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
impl RemoteVideoTrack {
|
||||
pub(crate) fn new(sid: TrackSid, name: String, track: Arc<VideoTrack>) -> Self {
|
||||
Self {
|
||||
shared: TrackShared::new(
|
||||
sid,
|
||||
name,
|
||||
TrackKind::Video,
|
||||
MediaStreamTrackHandle::Video(track),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rtc_track(&self) -> Arc<VideoTrack> {
|
||||
if let MediaStreamTrackHandle::Video(video) = &self.shared.rtc_track {
|
||||
video.clone()
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_track_trait!(RemoteVideoTrack);
|
||||
@@ -0,0 +1,31 @@
|
||||
use crate::room::track::local_video_track::LocalVideoTrack;
|
||||
use crate::room::track::remote_video_track::RemoteVideoTrack;
|
||||
use crate::room::track::TrackHandle;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum VideoTrackHandle {
|
||||
Local(Arc<LocalVideoTrack>),
|
||||
Remote(Arc<RemoteVideoTrack>),
|
||||
}
|
||||
|
||||
impl From<VideoTrackHandle> for TrackHandle {
|
||||
fn from(video_track: VideoTrackHandle) -> Self {
|
||||
match video_track {
|
||||
VideoTrackHandle::Local(local_video) => Self::LocalVideo(local_video),
|
||||
VideoTrackHandle::Remote(remote_video) => Self::RemoteVideo(remote_video),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<TrackHandle> for VideoTrackHandle {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(track: TrackHandle) -> Result<Self, Self::Error> {
|
||||
match track {
|
||||
TrackHandle::LocalVideo(local_video) => Ok(Self::Local(local_video)),
|
||||
TrackHandle::RemoteVideo(remote_video) => Ok(Self::Remote(remote_video)),
|
||||
_ => Err("not a video track"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
macro_rules! wrap_variants {
|
||||
// This arm is used to avoid nested loops with the arguments
|
||||
// The arguments are transformed to $combined_args TokenTree
|
||||
(@match $self:ident $fnc:ident $combined_args:tt [$($variant:ident),+]) => {
|
||||
match $self {
|
||||
$(
|
||||
Self::$variant(inner) => inner.$fnc$combined_args,
|
||||
)+
|
||||
}
|
||||
};
|
||||
|
||||
($fnc:ident, $ret:ty, [$($arg:ident: $t:ty),*], [$($variant:ident),+]) => {
|
||||
fn $fnc(&self, $($arg: $t),*) -> $ret {
|
||||
wrap_variants!(@match self $fnc ($($arg,)*) [$($variant),+])
|
||||
}
|
||||
};
|
||||
|
||||
($variants:tt $(fnc!($fnc:ident, $ret:ty, $args:tt);)+) => {
|
||||
$(
|
||||
wrap_variants!($fnc, $ret, $args, $variants);
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use wrap_variants;
|
||||
|
||||
Reference in New Issue
Block a user