reconnect WIP
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
use parking_lot::lock_api::RwLockUpgradableReadGuard;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::AtomicU8;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use self::id::ParticipantSid;
|
||||
use self::id::{ParticipantIdentity, 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, TrackSubscribedEvent,
|
||||
use crate::events::{
|
||||
ParticipantConnectedEvent, ParticipantDisconnectedEvent, RoomEvents, TrackPublishedEvent,
|
||||
TrackSubscribedEvent,
|
||||
};
|
||||
use crate::proto;
|
||||
use crate::proto::participant_info;
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, error};
|
||||
use tracing::{debug, error, instrument, trace_span, Level};
|
||||
|
||||
use crate::rtc_engine::{EngineError, EngineEvent, EngineEvents, RTCEngine};
|
||||
use crate::signal_client::SignalOptions;
|
||||
@@ -27,15 +27,15 @@ pub mod track;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RoomError {
|
||||
#[error("internal RTCEngine failure")]
|
||||
#[error("engine : {0}")]
|
||||
Engine(#[from] EngineError),
|
||||
#[error("internal Room failure")]
|
||||
#[error("room failure: {0}")]
|
||||
Internal(String),
|
||||
}
|
||||
|
||||
type RoomResult<T> = Result<T, RoomError>;
|
||||
pub type RoomResult<T> = Result<T, RoomError>;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||
pub enum ConnectionState {
|
||||
Disconnected,
|
||||
Connecting,
|
||||
@@ -43,6 +43,7 @@ pub enum ConnectionState {
|
||||
Reconnecting,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RoomInner {
|
||||
state: AtomicU8, // ConnectionState
|
||||
sid: Mutex<String>,
|
||||
@@ -52,6 +53,7 @@ struct RoomInner {
|
||||
local_participant: Arc<LocalParticipant>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Room {
|
||||
inner: Option<Arc<RoomInner>>,
|
||||
events: Arc<RoomEvents>,
|
||||
@@ -65,14 +67,19 @@ impl Room {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = Level::DEBUG)]
|
||||
pub async fn connect(&mut self, url: &str, token: &str) -> RoomResult<()> {
|
||||
let (rtc_engine, engine_events) =
|
||||
RTCEngine::connect(url, token, SignalOptions::default()).await?;
|
||||
let rtc_engine = Arc::new(rtc_engine);
|
||||
let join_response = rtc_engine.join_response();
|
||||
let pi = join_response.participant.unwrap().clone();
|
||||
let local_participant = Arc::new(LocalParticipant::new(
|
||||
rtc_engine.clone(),
|
||||
join_response.participant.unwrap().clone(),
|
||||
pi.sid.into(),
|
||||
pi.identity.into(),
|
||||
pi.name,
|
||||
pi.metadata,
|
||||
));
|
||||
let room_info = join_response.room.unwrap();
|
||||
let inner = Arc::new(RoomInner {
|
||||
@@ -84,14 +91,25 @@ impl Room {
|
||||
local_participant,
|
||||
});
|
||||
|
||||
self.inner = Some(inner.clone());
|
||||
|
||||
// Add already connected participants
|
||||
for pi in join_response.other_participants {
|
||||
let p = Self::create_participant(inner.clone(), self.events.clone(), pi.clone());
|
||||
p.update_info(pi).await;
|
||||
let participant = {
|
||||
let pi = pi.clone();
|
||||
Self::create_participant(
|
||||
inner.clone(),
|
||||
self.events.clone(),
|
||||
pi.sid.into(),
|
||||
pi.identity.into(),
|
||||
pi.name,
|
||||
pi.metadata,
|
||||
)
|
||||
};
|
||||
participant.update_info(pi.clone());
|
||||
participant
|
||||
.update_tracks(RoomHandle::from(inner.clone()), pi.tracks)
|
||||
.await;
|
||||
}
|
||||
|
||||
self.inner = Some(inner.clone());
|
||||
tokio::spawn(Self::room_task(inner, self.events.clone(), engine_events));
|
||||
|
||||
Ok(())
|
||||
@@ -121,6 +139,7 @@ impl Room {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = Level::DEBUG, skip(room_inner, room_events))]
|
||||
async fn handle_event(
|
||||
room_inner: Arc<RoomInner>,
|
||||
room_events: Arc<RoomEvents>,
|
||||
@@ -155,10 +174,18 @@ 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(),
|
||||
);
|
||||
tokio::spawn({
|
||||
let track_sid = track_sid.to_owned().into();
|
||||
async move {
|
||||
remote_participant
|
||||
.add_subscribed_media_track(
|
||||
RoomHandle::from(room_inner),
|
||||
track_sid,
|
||||
rtp_receiver.track(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// The server should send participant updates before sending a new offer
|
||||
// So this should not happen.
|
||||
@@ -173,6 +200,7 @@ impl Room {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(level = Level::DEBUG, skip(room_inner, room_events))]
|
||||
async fn handle_participant_update(
|
||||
room_inner: Arc<RoomInner>,
|
||||
room_events: Arc<RoomEvents>,
|
||||
@@ -182,7 +210,7 @@ impl Room {
|
||||
if pi.sid == room_inner.local_participant.sid()
|
||||
|| pi.identity == room_inner.local_participant.identity()
|
||||
{
|
||||
room_inner.local_participant.clone().update_info(pi).await;
|
||||
room_inner.local_participant.clone().update_info(pi);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -199,12 +227,24 @@ impl Room {
|
||||
)
|
||||
} else {
|
||||
// Participant is already connected, update the informations
|
||||
remote_participant.update_info(pi).await;
|
||||
remote_participant.update_info(pi.clone());
|
||||
remote_participant
|
||||
.update_tracks(RoomHandle::from(room_inner.clone()), pi.tracks)
|
||||
.await;
|
||||
}
|
||||
} else {
|
||||
// Create a new participant and call OnConnect event
|
||||
let remote_participant =
|
||||
Self::create_participant(room_inner.clone(), room_events.clone(), pi);
|
||||
let remote_participant = {
|
||||
let pi = pi.clone();
|
||||
Self::create_participant(
|
||||
room_inner.clone(),
|
||||
room_events.clone(),
|
||||
pi.sid.into(),
|
||||
pi.identity.into(),
|
||||
pi.name,
|
||||
pi.metadata,
|
||||
)
|
||||
};
|
||||
let mut handler = room_events.on_participant_connected.lock();
|
||||
if let Some(cb) = handler.as_mut() {
|
||||
cb(ParticipantConnectedEvent {
|
||||
@@ -212,10 +252,16 @@ impl Room {
|
||||
participant: remote_participant.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
remote_participant.update_info(pi.clone());
|
||||
remote_participant
|
||||
.update_tracks(RoomHandle::from(room_inner.clone()), pi.tracks)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = Level::DEBUG, skip(room_inner, room_events))]
|
||||
fn handle_participant_disconnect(
|
||||
room_inner: Arc<RoomInner>,
|
||||
room_events: Arc<RoomEvents>,
|
||||
@@ -247,42 +293,64 @@ impl Room {
|
||||
fn create_participant(
|
||||
room_inner: Arc<RoomInner>,
|
||||
room_events: Arc<RoomEvents>,
|
||||
pi: proto::ParticipantInfo,
|
||||
sid: ParticipantSid,
|
||||
identity: ParticipantIdentity,
|
||||
name: String,
|
||||
metadata: String,
|
||||
) -> Arc<RemoteParticipant> {
|
||||
let p = Arc::new(RemoteParticipant::new(pi.clone()));
|
||||
let p = Arc::new(RemoteParticipant::new(
|
||||
sid.clone(),
|
||||
identity,
|
||||
name,
|
||||
metadata,
|
||||
));
|
||||
|
||||
macro_rules! forward_event {
|
||||
($type:ident, when_connected) => {
|
||||
p.internal_events().$type({
|
||||
let room_events = room_events.clone();
|
||||
let room_inner = room_inner.clone();
|
||||
move |event| {
|
||||
let room_events = room_events.clone();
|
||||
let room_inner = room_inner.clone();
|
||||
async move {
|
||||
if room_inner.state.load(Ordering::SeqCst)
|
||||
== ConnectionState::Connected as u8
|
||||
{
|
||||
if let Some(cb) = room_events.$type.lock().as_mut() {
|
||||
cb(event).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
($type:ident) => {
|
||||
p.internal_events().$type({
|
||||
let room_events = room_events.clone();
|
||||
move |event| {
|
||||
let room_events = room_events.clone();
|
||||
async move {
|
||||
if let Some(cb) = room_events.$type.lock().as_mut() {
|
||||
cb(event).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
// Forward participantevents to room events
|
||||
p.internal_events().on_track_subscribed({
|
||||
let room_events = room_events.clone();
|
||||
let room_inner = room_inner.clone();
|
||||
forward_event!(on_track_published, when_connected);
|
||||
forward_event!(on_track_subscribed);
|
||||
forward_event!(on_track_subscription_failed);
|
||||
|
||||
move |event| {
|
||||
let room_events = room_events.clone();
|
||||
let room_inner = room_inner.clone();
|
||||
|
||||
async move {
|
||||
if let Some(cb) = room_events.clone().on_track_subscribed.lock().as_mut() {
|
||||
cb(TrackSubscribedEvent {
|
||||
room_handle: RoomHandle::from(room_inner.clone()),
|
||||
track: event.track,
|
||||
participant: event.participant,
|
||||
publication: event.publication,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
room_inner
|
||||
.participants
|
||||
.write()
|
||||
.insert(pi.sid.into(), p.clone());
|
||||
room_inner.participants.write().insert(sid, p.clone());
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RoomHandle {
|
||||
inner: Arc<RoomInner>,
|
||||
}
|
||||
|
||||
@@ -1,22 +1,28 @@
|
||||
use std::sync::Weak;
|
||||
|
||||
use crate::proto::{data_packet, DataPacket, UserPacket};
|
||||
use crate::room::participant::{impl_participant_trait, ParticipantShared, ParticipantInternalTrait};
|
||||
use crate::room::RoomError;
|
||||
use crate::room::participant::{
|
||||
impl_participant_trait, ParticipantInternalTrait, ParticipantShared,
|
||||
};
|
||||
use crate::room::{RoomError, RoomInner};
|
||||
use crate::rtc_engine::RTCEngine;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LocalParticipant {
|
||||
shared: ParticipantShared,
|
||||
rtc_engine: Arc<RTCEngine>,
|
||||
}
|
||||
|
||||
impl LocalParticipant {
|
||||
pub(crate) fn new(rtc_engine: Arc<RTCEngine>, info: ParticipantInfo) -> Self {
|
||||
pub(crate) fn new(
|
||||
rtc_engine: Arc<RTCEngine>,
|
||||
sid: ParticipantSid,
|
||||
identity: ParticipantIdentity,
|
||||
name: String,
|
||||
metadata: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
shared: ParticipantShared::new(
|
||||
info.sid.into(),
|
||||
info.identity.into(),
|
||||
info.name,
|
||||
info.metadata,
|
||||
),
|
||||
shared: ParticipantShared::new(sid, identity, name, metadata),
|
||||
rtc_engine,
|
||||
}
|
||||
}
|
||||
@@ -40,16 +46,16 @@ impl LocalParticipant {
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) async fn update_info(self: Arc<Self>, info: ParticipantInfo) {
|
||||
self.shared.update_info(info);
|
||||
}
|
||||
}
|
||||
|
||||
impl ParticipantInternalTrait for LocalParticipant {
|
||||
fn internal_events(&self) -> Arc<ParticipantEvents> {
|
||||
self.shared.internal_events.clone()
|
||||
}
|
||||
|
||||
fn update_info(&self, info: ParticipantInfo) {
|
||||
self.shared.update_info(info);
|
||||
}
|
||||
}
|
||||
|
||||
impl_participant_trait!(LocalParticipant);
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use crate::events::participant::ParticipantEvents;
|
||||
use crate::events::ParticipantEvents;
|
||||
use crate::proto::ParticipantInfo;
|
||||
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 futures_util::future::BoxFuture;
|
||||
use livekit_utils::enum_dispatch;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use std::collections::HashMap;
|
||||
@@ -13,8 +12,7 @@ use std::sync::Arc;
|
||||
pub mod local_participant;
|
||||
pub mod remote_participant;
|
||||
|
||||
type OnTrackSubscribed = Box<dyn FnMut(ParticipantHandle) -> BoxFuture<'static, ()> + Send + Sync>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct ParticipantShared {
|
||||
pub(super) events: Arc<ParticipantEvents>,
|
||||
pub(super) internal_events: Arc<ParticipantEvents>,
|
||||
@@ -57,6 +55,7 @@ impl ParticipantShared {
|
||||
|
||||
pub(crate) trait ParticipantInternalTrait {
|
||||
fn internal_events(&self) -> Arc<ParticipantEvents>;
|
||||
fn update_info(&self, info: ParticipantInfo);
|
||||
}
|
||||
|
||||
pub trait ParticipantTrait {
|
||||
@@ -73,20 +72,11 @@ pub enum ParticipantHandle {
|
||||
Remote(Arc<RemoteParticipant>),
|
||||
}
|
||||
|
||||
impl ParticipantHandle {
|
||||
// TODO(theomonnom): Add async support to wrap_variants ...
|
||||
pub(crate) async fn update_info(&self, info: ParticipantInfo) {
|
||||
match self {
|
||||
Self::Local(inner) => inner.clone().update_info(info).await,
|
||||
Self::Remote(inner) => inner.clone().update_info(info).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ParticipantInternalTrait for ParticipantHandle {
|
||||
enum_dispatch!(
|
||||
[Local, Remote]
|
||||
fnc!(internal_events, &Self, [], Arc<ParticipantEvents>);
|
||||
fnc!(update_info, &Self, [info: ParticipantInfo], ());
|
||||
);
|
||||
}
|
||||
|
||||
@@ -103,7 +93,7 @@ impl ParticipantTrait for ParticipantHandle {
|
||||
|
||||
macro_rules! impl_participant_trait {
|
||||
($x:ty) => {
|
||||
use crate::events::participant::ParticipantEvents;
|
||||
use crate::events::ParticipantEvents;
|
||||
use crate::proto::ParticipantInfo;
|
||||
use crate::room::id::{ParticipantIdentity, ParticipantSid};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::events::participant::{
|
||||
TrackPublishedEvent, TrackSubscribedEvent, TrackSubscriptionFailedEvent,
|
||||
use crate::events::{
|
||||
TrackError, TrackPublishedEvent, TrackSubscribedEvent, TrackSubscriptionFailedEvent,
|
||||
};
|
||||
use crate::events::TrackError;
|
||||
use crate::proto::TrackInfo;
|
||||
use crate::room::id::TrackSid;
|
||||
use crate::room::participant::{
|
||||
impl_participant_trait, ParticipantInternalTrait, ParticipantShared,
|
||||
@@ -12,141 +12,35 @@ use crate::room::publication::{
|
||||
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::{TrackKind, TrackTrait, TrackHandle};
|
||||
use crate::room::track::{TrackKind, TrackTrait};
|
||||
use crate::room::{RoomHandle, RoomInner};
|
||||
use livekit_webrtc::media_stream::MediaStreamTrackHandle;
|
||||
use std::collections::HashSet;
|
||||
use std::time::Duration;
|
||||
use tokio::time::{sleep, timeout};
|
||||
use tracing::{info, error};
|
||||
use tracing::{debug, debug_span, error, instrument, Instrument, Level};
|
||||
|
||||
use super::ParticipantTrait;
|
||||
|
||||
const ADD_TRACK_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RemoteParticipant {
|
||||
shared: ParticipantShared,
|
||||
}
|
||||
|
||||
impl RemoteParticipant {
|
||||
pub(crate) fn new(info: ParticipantInfo) -> Self {
|
||||
pub(crate) fn new(
|
||||
sid: ParticipantSid,
|
||||
identity: ParticipantIdentity,
|
||||
name: String,
|
||||
metadata: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
shared: ParticipantShared::new(
|
||||
info.sid.into(),
|
||||
info.identity.into(),
|
||||
info.name,
|
||||
info.metadata,
|
||||
),
|
||||
shared: ParticipantShared::new(sid, identity, name, 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!(),
|
||||
};
|
||||
|
||||
info!("starting track: {:?}", sid);
|
||||
|
||||
remote_publication.update_track(Some(track.clone().into()));
|
||||
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 {
|
||||
@@ -157,12 +51,125 @@ impl RemoteParticipant {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn update_info(self: Arc<Self>, info: ParticipantInfo) {
|
||||
self.shared.update_info(info.clone());
|
||||
#[instrument(level = Level::DEBUG, skip(room_handle))]
|
||||
pub(crate) async fn add_subscribed_media_track(
|
||||
self: Arc<Self>,
|
||||
room_handle: RoomHandle,
|
||||
sid: TrackSid,
|
||||
media_track: MediaStreamTrackHandle,
|
||||
) {
|
||||
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!(),
|
||||
};
|
||||
|
||||
debug!("starting track: {:?}", sid);
|
||||
|
||||
remote_publication.update_track(Some(track.clone().into()));
|
||||
self.shared
|
||||
.add_track_publication(TrackPublication::Remote(remote_publication.clone()));
|
||||
track.start();
|
||||
|
||||
let event = TrackSubscribedEvent {
|
||||
room_handle,
|
||||
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 {
|
||||
room_handle,
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = Level::DEBUG, skip(room_handle))]
|
||||
pub(crate) async fn update_tracks(
|
||||
self: Arc<Self>,
|
||||
room_handle: RoomHandle,
|
||||
tracks: Vec<TrackInfo>,
|
||||
) {
|
||||
let mut valid_tracks = HashSet::<TrackSid>::new();
|
||||
|
||||
for track in info.tracks {
|
||||
for track in tracks {
|
||||
if let Some(publication) = self.get_track_publication(&track.sid.clone().into()) {
|
||||
publication.update_info(track.clone());
|
||||
} else {
|
||||
@@ -172,6 +179,7 @@ impl RemoteParticipant {
|
||||
|
||||
// This is a new track, fire publish events
|
||||
let event = TrackPublishedEvent {
|
||||
room_handle: room_handle.clone(),
|
||||
participant: self.clone(),
|
||||
publication: publication.clone(),
|
||||
};
|
||||
@@ -200,6 +208,10 @@ impl ParticipantInternalTrait for RemoteParticipant {
|
||||
fn internal_events(&self) -> Arc<ParticipantEvents> {
|
||||
self.shared.internal_events.clone()
|
||||
}
|
||||
|
||||
fn update_info(&self, info: ParticipantInfo) {
|
||||
self.shared.update_info(info)
|
||||
}
|
||||
}
|
||||
|
||||
impl_participant_trait!(RemoteParticipant);
|
||||
|
||||
@@ -25,6 +25,7 @@ pub trait TrackPublicationTrait {
|
||||
fn simulcasted(&self) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct TrackPublicationShared {
|
||||
pub(super) track: Mutex<Option<TrackHandle>>,
|
||||
pub(super) name: Mutex<String>,
|
||||
@@ -75,7 +76,7 @@ impl TrackPublicationShared {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TrackPublication {
|
||||
Local(LocalTrackPublication),
|
||||
Remote(RemoteTrackPublication),
|
||||
@@ -146,7 +147,7 @@ macro_rules! impl_publication_trait {
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct LocalTrackPublication {
|
||||
shared: Arc<TrackPublicationShared>,
|
||||
}
|
||||
@@ -161,7 +162,7 @@ impl LocalTrackPublication {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RemoteTrackPublication {
|
||||
shared: Arc<TrackPublicationShared>,
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
pub struct TrackEvents {}
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::room::track::{impl_track_trait, TrackShared};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LocalAudioTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::room::track::{impl_track_trait, TrackShared};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LocalVideoTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ 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;
|
||||
@@ -97,6 +96,7 @@ impl From<ProtoTrackSource> for TrackSource {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct TrackDimension(pub u32, pub u32);
|
||||
|
||||
pub trait TrackTrait {
|
||||
@@ -108,6 +108,7 @@ pub trait TrackTrait {
|
||||
fn stop(&self);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct TrackShared {
|
||||
pub(super) sid: Mutex<TrackSid>,
|
||||
pub(super) name: Mutex<String>,
|
||||
@@ -141,7 +142,7 @@ impl TrackShared {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TrackHandle {
|
||||
LocalVideo(Arc<LocalVideoTrack>),
|
||||
LocalAudio(Arc<LocalAudioTrack>),
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::room::track::{impl_track_trait, TrackShared};
|
||||
use livekit_webrtc::media_stream::{AudioTrack, MediaStreamTrackHandle};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RemoteAudioTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use livekit_utils::enum_dispatch;
|
||||
|
||||
use super::TrackTrait;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum RemoteTrackHandle {
|
||||
Audio(Arc<RemoteAudioTrack>),
|
||||
Video(Arc<RemoteVideoTrack>),
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::sync::Arc;
|
||||
|
||||
use crate::room::track::{impl_track_trait, TrackShared};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RemoteVideoTrack {
|
||||
shared: TrackShared,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user