Initial Room

Switching computer
This commit is contained in:
Théo Monnom
2022-10-29 00:11:10 +02:00
parent 2ecae706a9
commit 3630e93054
47 changed files with 1783 additions and 980 deletions
+38
View File
@@ -0,0 +1,38 @@
macro_rules! id_str {
($($name:ident;)*) => {
$(
impl From<String> for $name {
fn from(str: String) -> $name {
$name(str)
}
}
impl PartialEq<$name> for String {
fn eq(&self, u: &$name) -> bool {
*self == *u.0
}
}
impl From<$name> for String {
fn from(id: $name) -> String {
id.0
}
}
)*
}
}
#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct ParticipantSid(pub String);
#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct ParticipantIdentity(pub String);
#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct TrackSid(pub String);
id_str! {
ParticipantSid;
ParticipantIdentity;
TrackSid;
}
@@ -0,0 +1,46 @@
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,
rtc_engine: Arc<RTCEngine>,
}
impl LocalParticipant {
pub(super) fn new(rtc_engine: Arc<RTCEngine>, info: ParticipantInfo) -> Self {
Self {
shared: ParticipantShared::new(
info.sid.into(),
info.identity.into(),
info.name,
info.metadata,
),
rtc_engine,
}
}
pub async fn publish_data(
&self,
data: &[u8],
kind: data_packet::Kind,
) -> Result<(), RoomError> {
let data = DataPacket {
kind: kind as i32,
value: Some(data_packet::Value::User(UserPacket {
participant_sid: "".to_string(), /*self.sid().to_owned()*/
payload: data.to_vec(),
destination_sids: vec![],
})),
};
self.rtc_engine
.publish_data(&data, kind)
.await
.map_err(Into::into)
}
}
impl_participant_trait!(LocalParticipant);
+316
View File
@@ -0,0 +1,316 @@
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;
use std::sync::Arc;
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 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;
#[derive(Error, Debug)]
pub enum RoomError {
#[error("internal RTCEngine failure")]
Engine(#[from] EngineError),
#[error("internal Room failure")]
Internal(String),
}
type RoomResult<T> = Result<T, RoomError>;
pub enum ConnectionState {
Disconnected,
Connecting,
Connected,
Reconnecting,
}
struct RoomInner {
state: AtomicU8, // ConnectionState
sid: Mutex<String>,
name: Mutex<String>,
participants: RwLock<HashMap<ParticipantSid, Arc<RemoteParticipant>>>,
rtc_engine: Arc<RTCEngine>,
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>,
}
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(),
}),
}
}
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 local_participant = Arc::new(LocalParticipant::new(
rtc_engine.clone(),
join_response.participant.unwrap().clone(),
));
let room_info = join_response.room.unwrap();
let inner = Arc::new(RoomInner {
state: AtomicU8::new(ConnectionState::Connecting as u8),
sid: Mutex::new(room_info.sid),
name: Mutex::new(room_info.name),
participants: Default::default(),
rtc_engine,
local_participant,
});
self.inner = Some(inner.clone());
tokio::spawn(Self::room_task(inner, self.events.clone(), engine_events));
Ok(())
}
pub fn get_handle(&self) -> Option<RoomHandle> {
self.inner.as_ref().map(|inner| RoomHandle {
inner: inner.clone(),
})
}
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>,
mut engine_events: EngineEvents,
) {
while let Some(event) = engine_events.recv().await {
if let Err(err) =
Self::handle_event(room_inner.clone(), room_events.clone(), event).await
{
error!("failed to handle engine event: {:?}", err);
}
}
}
async fn handle_event(
room_inner: Arc<RoomInner>,
room_events: Arc<RoomEvents>,
event: EngineEvent,
) -> RoomResult<()> {
match event {
EngineEvent::ParticipantUpdate(update) => {
Self::handle_participant_update(room_inner.clone(), room_events.clone(), update)
}
EngineEvent::AddTrack {
rtp_receiver,
streams,
} => {
if streams.is_empty() {
Err(RoomError::Internal(
"AddTrack event with empty streams".to_string(),
))?;
}
let first_stream_id = streams.first().unwrap().id();
let stream_id = unpack_stream_id(&first_stream_id);
if stream_id.is_none() {
Err(RoomError::Internal(format!(
"AddTrack event with invalid track_id: {:?}",
first_stream_id
)))?;
}
let (participant_sid, track_sid) = stream_id.unwrap();
let remote_participant =
Self::get_participant(room_inner.clone(), &participant_sid.to_string().into());
if let Some(remote_participant) = remote_participant {
} else {
// The server should send participant updates before sending a new offer
// So this should not happen.
Err(RoomError::Internal(format!(
"AddTrack event with invalid participant_sid: {:?}",
participant_sid
)))?;
}
}
}
Ok(())
}
fn handle_participant_update(
room_inner: Arc<RoomInner>,
room_events: Arc<RoomEvents>,
update: proto::ParticipantUpdate,
) {
for pi in update.participants {
if pi.sid == room_inner.local_participant.sid()
|| pi.identity == room_inner.local_participant.identity()
{
room_inner.local_participant.update_info(pi);
continue;
}
let remote_participant =
Self::get_participant(room_inner.clone(), &pi.sid.clone().into());
if let Some(remote_participant) = remote_participant {
if pi.state == participant_info::State::Disconnected as i32 {
// Participant disconencted
Self::handle_participant_disconnect(
room_inner.clone(),
room_events.clone(),
remote_participant,
)
} else {
// Participant is already connected, update the informations
remote_participant.update_info(pi);
}
} 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(),
);
}
}
}
}
fn handle_participant_disconnect(
room_inner: Arc<RoomInner>,
room_events: Arc<RoomEvents>,
remote_participant: Arc<RemoteParticipant>,
) {
room_inner
.participants
.write()
.remove(&remote_participant.sid());
// 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(),
);
}
}
fn get_participant(
room_inner: Arc<RoomInner>,
sid: &ParticipantSid,
) -> Option<Arc<RemoteParticipant>> {
room_inner.participants.read().get(sid).cloned()
}
fn get_or_create_participant(
room_inner: Arc<RoomInner>,
pi: proto::ParticipantInfo,
) -> Arc<RemoteParticipant> {
let participants = room_inner.participants.upgradable_read();
let sid = pi.sid.clone().into();
if let Some(p) = participants.get(&sid) {
p.update_info(pi);
p.clone()
} else {
let mut participants = RwLockUpgradableReadGuard::upgrade(participants);
let p = Arc::new(RemoteParticipant::new(pi));
participants.insert(sid, p.clone());
p
}
}
}
#[derive(Clone)]
pub struct RoomHandle {
inner: Arc<RoomInner>,
}
impl RoomHandle {
fn from(room_inner: Arc<RoomInner>) -> Self {
Self { inner: room_inner }
}
pub fn sid(&self) -> String {
self.inner.sid.lock().clone()
}
pub fn name(&self) -> String {
self.inner.name.lock().clone()
}
pub fn local_participant(&self) -> Arc<LocalParticipant> {
self.inner.local_participant.clone()
}
}
fn unpack_stream_id(stream_id: &str) -> Option<(&str, &str)> {
let split: Vec<&str> = stream_id.split('|').collect();
if split.len() == 2 {
let participant_sid = split.get(0).unwrap();
let track_sid = split.get(1).unwrap();
Some((participant_sid, track_sid))
} else {
None
}
}
+109
View File
@@ -0,0 +1,109 @@
use crate::proto::ParticipantInfo;
use crate::room::local_participant::LocalParticipant;
use crate::room::remote_participant::RemoteParticipant;
use parking_lot::{Mutex, RwLock};
use std::collections::HashMap;
use std::sync::Arc;
pub(super) struct ParticipantShared {
pub(super) sid: Mutex<ParticipantSid>,
pub(super) identity: Mutex<ParticipantIdentity>,
pub(super) name: Mutex<String>,
pub(super) metadata: Mutex<String>,
pub(super) tracks: RwLock<HashMap<TrackSid, Arc<TrackPublication>>>,
}
impl ParticipantShared {
pub(super) fn new(
sid: ParticipantSid,
identity: ParticipantIdentity,
name: String,
metadata: String,
) -> Self {
Self {
sid: Mutex::new(sid),
identity: Mutex::new(identity),
name: Mutex::new(name),
metadata: Mutex::new(metadata),
tracks: Default::default(),
}
}
pub(crate) fn update_info(&self, info: ParticipantInfo) {
*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
}
}
pub trait ParticipantTrait {
fn sid(&self) -> ParticipantSid;
fn identity(&self) -> ParticipantIdentity;
fn name(&self) -> String;
fn metadata(&self) -> String;
fn update_info(&self, info: ParticipantInfo);
}
pub enum Participant {
Local(LocalParticipant),
Remote(RemoteParticipant),
}
macro_rules! shared_method {
($x:ident, $ret:ident) => {
fn $x(&self) -> $ret {
match self {
Participant::Local(p) => p.$x(),
Participant::Remote(p) => p.$x(),
}
}
};
}
impl ParticipantTrait for Participant {
shared_method!(sid, ParticipantSid);
shared_method!(identity, ParticipantIdentity);
shared_method!(name, String);
shared_method!(metadata, String);
fn update_info(&self, info: ParticipantInfo) {
match self {
Participant::Local(p) => p.update_info(info),
Participant::Remote(p) => p.update_info(info),
}
}
}
macro_rules! impl_participant_trait {
($x:ident) => {
use crate::proto::ParticipantInfo;
use crate::room::id::{ParticipantIdentity, ParticipantSid};
impl crate::room::participant::ParticipantTrait for $x {
fn sid(&self) -> ParticipantSid {
self.shared.sid.lock().clone()
}
fn identity(&self) -> ParticipantIdentity {
self.shared.identity.lock().clone()
}
fn name(&self) -> String {
self.shared.name.lock().clone()
}
fn metadata(&self) -> String {
self.shared.metadata.lock().clone()
}
fn update_info(&self, info: ParticipantInfo) {
self.shared.update_info(info);
}
}
};
}
use crate::room::id::{ParticipantIdentity, ParticipantSid, TrackSid};
use crate::room::track_publication::TrackPublication;
pub(super) use impl_participant_trait;
@@ -0,0 +1,34 @@
use std::sync::Arc;
use crate::room::participant::{impl_participant_trait, ParticipantShared};
use crate::room::track_publication::RemoteTrackPublication;
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() {
}
fn get_track_publication(&self, sid: &str) -> Option<RemoteTrackPublication> {
let track = self.shared.tracks.read().get(&sid.to_string().into()).unwrap().clone();
None
}
}
impl_participant_trait!(RemoteParticipant);
+135
View File
@@ -0,0 +1,135 @@
pub enum TrackKind {
Audio,
Video
}
pub enum StreamState {
Active,
Paused,
Unknown
}
pub enum TrackSource {
Camera,
Microphone,
Screenshare,
ScreenshareAudio,
Unknown,
}
pub struct LocalVideoTrack {}
pub struct RemoteVideoTrack {}
pub struct LocalAudioTrack {}
pub struct RemoteAudioTrack {}
pub enum RemoteTrack {
Audio(RemoteAudioTrack),
Video(RemoteVideoTrack),
}
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 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),
}
}
}
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"),
}
}
}
@@ -0,0 +1,28 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU8};
use parking_lot::Mutex;
use crate::room::id::TrackSid;
pub(super) struct TrackPublicationShared {
pub(super) name: Mutex<String>,
pub(super) sid: Mutex<TrackSid>,
pub(super) kind: AtomicU8, // Casted to TrackKind
pub(super) source: AtomicU8, // Casted to TrackSource
pub(super) simulcasted: AtomicBool
}
#[derive(Clone)]
pub struct LocalTrackPublication {
shared: Arc<TrackPublicationShared>
}
#[derive(Clone)]
pub struct RemoteTrackPublication {
shared: Arc<TrackPublicationShared>
}
#[derive(Clone)]
pub enum TrackPublication {
Local(LocalTrackPublication),
Remote(RemoteTrackPublication)
}