participant.rs

  1use super::*;
  2
  3#[derive(Clone, Debug)]
  4pub enum Participant {
  5    Local(LocalParticipant),
  6    Remote(RemoteParticipant),
  7}
  8
  9#[derive(Clone, Debug)]
 10pub struct LocalParticipant {
 11    #[cfg(not(all(target_os = "windows", target_env = "gnu")))]
 12    pub(super) identity: ParticipantIdentity,
 13    pub(super) room: Room,
 14}
 15
 16#[derive(Clone, Debug)]
 17pub struct RemoteParticipant {
 18    #[cfg(not(all(target_os = "windows", target_env = "gnu")))]
 19    pub(super) identity: ParticipantIdentity,
 20    pub(super) room: WeakRoom,
 21}
 22
 23#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
 24impl Participant {
 25    pub fn identity(&self) -> ParticipantIdentity {
 26        match self {
 27            Participant::Local(participant) => participant.identity.clone(),
 28            Participant::Remote(participant) => participant.identity.clone(),
 29        }
 30    }
 31}
 32
 33#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
 34impl LocalParticipant {
 35    pub async fn unpublish_track(&self, track: &TrackSid) -> Result<()> {
 36        self.room
 37            .test_server()
 38            .unpublish_track(self.room.token(), track)
 39            .await
 40    }
 41
 42    pub async fn publish_track(
 43        &self,
 44        track: LocalTrack,
 45        _options: TrackPublishOptions,
 46    ) -> Result<LocalTrackPublication> {
 47        let this = self.clone();
 48        let track = track.clone();
 49        let server = this.room.test_server();
 50        let sid = match track {
 51            LocalTrack::Video(track) => {
 52                server.publish_video_track(this.room.token(), track).await?
 53            }
 54            LocalTrack::Audio(track) => {
 55                server
 56                    .publish_audio_track(this.room.token(), &track)
 57                    .await?
 58            }
 59        };
 60        Ok(LocalTrackPublication {
 61            room: self.room.downgrade(),
 62            sid,
 63        })
 64    }
 65}
 66
 67#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
 68impl RemoteParticipant {
 69    pub fn track_publications(&self) -> HashMap<TrackSid, RemoteTrackPublication> {
 70        if let Some(room) = self.room.upgrade() {
 71            let server = room.test_server();
 72            let audio = server
 73                .audio_tracks(room.token())
 74                .unwrap()
 75                .into_iter()
 76                .filter(|track| track.publisher_id() == self.identity)
 77                .map(|track| {
 78                    (
 79                        track.sid(),
 80                        RemoteTrackPublication {
 81                            sid: track.sid(),
 82                            room: self.room.clone(),
 83                            track: RemoteTrack::Audio(track),
 84                        },
 85                    )
 86                });
 87            let video = server
 88                .video_tracks(room.token())
 89                .unwrap()
 90                .into_iter()
 91                .filter(|track| track.publisher_id() == self.identity)
 92                .map(|track| {
 93                    (
 94                        track.sid(),
 95                        RemoteTrackPublication {
 96                            sid: track.sid(),
 97                            room: self.room.clone(),
 98                            track: RemoteTrack::Video(track),
 99                        },
100                    )
101                });
102            audio.chain(video).collect()
103        } else {
104            HashMap::default()
105        }
106    }
107
108    pub fn identity(&self) -> ParticipantIdentity {
109        self.identity.clone()
110    }
111}