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