participant.rs

  1use crate::{
  2    AudioStream, LocalAudioTrack, LocalTrackPublication, LocalVideoTrack, Participant,
  3    ParticipantIdentity, RemoteTrack, RemoteTrackPublication, TrackSid,
  4    test::{Room, WeakRoom},
  5};
  6use anyhow::Result;
  7use collections::HashMap;
  8use gpui::{AsyncApp, ScreenCaptureSource, ScreenCaptureStream, TestScreenCaptureStream};
  9
 10#[derive(Clone, Debug)]
 11pub struct LocalParticipant {
 12    pub(crate) identity: ParticipantIdentity,
 13    pub(crate) room: Room,
 14}
 15
 16#[derive(Clone, Debug)]
 17pub struct RemoteParticipant {
 18    pub(crate) identity: ParticipantIdentity,
 19    pub(crate) room: WeakRoom,
 20}
 21
 22impl Participant {
 23    pub fn identity(&self) -> ParticipantIdentity {
 24        match self {
 25            Participant::Local(participant) => participant.identity.clone(),
 26            Participant::Remote(participant) => participant.identity.clone(),
 27        }
 28    }
 29}
 30
 31impl LocalParticipant {
 32    pub async fn unpublish_track(&self, track: TrackSid, _cx: &AsyncApp) -> Result<()> {
 33        self.room
 34            .test_server()
 35            .unpublish_track(self.room.token(), &track)
 36            .await
 37    }
 38
 39    pub(crate) async fn publish_microphone_track(
 40        &self,
 41        _cx: &AsyncApp,
 42    ) -> Result<(LocalTrackPublication, AudioStream)> {
 43        let this = self.clone();
 44        let server = this.room.test_server();
 45        let sid = server
 46            .publish_audio_track(this.room.token(), &LocalAudioTrack {})
 47            .await?;
 48
 49        Ok((
 50            LocalTrackPublication {
 51                room: self.room.downgrade(),
 52                sid,
 53            },
 54            AudioStream {},
 55        ))
 56    }
 57
 58    pub async fn publish_screenshare_track(
 59        &self,
 60        _source: &dyn ScreenCaptureSource,
 61        _cx: &mut AsyncApp,
 62    ) -> Result<(LocalTrackPublication, Box<dyn ScreenCaptureStream>)> {
 63        let this = self.clone();
 64        let server = this.room.test_server();
 65        let sid = server
 66            .publish_video_track(this.room.token(), LocalVideoTrack {})
 67            .await?;
 68        Ok((
 69            LocalTrackPublication {
 70                room: self.room.downgrade(),
 71                sid,
 72            },
 73            Box::new(TestScreenCaptureStream {}),
 74        ))
 75    }
 76}
 77
 78impl RemoteParticipant {
 79    pub fn track_publications(&self) -> HashMap<TrackSid, RemoteTrackPublication> {
 80        if let Some(room) = self.room.upgrade() {
 81            let server = room.test_server();
 82            let audio = server
 83                .audio_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::Audio(track),
 94                        },
 95                    )
 96                });
 97            let video = server
 98                .video_tracks(room.token())
 99                .unwrap()
100                .into_iter()
101                .filter(|track| track.publisher_id() == self.identity)
102                .map(|track| {
103                    (
104                        track.sid(),
105                        RemoteTrackPublication {
106                            sid: track.sid(),
107                            room: self.room.clone(),
108                            track: RemoteTrack::Video(track),
109                        },
110                    )
111                });
112            audio.chain(video).collect()
113        } else {
114            HashMap::default()
115        }
116    }
117
118    pub fn identity(&self) -> ParticipantIdentity {
119        self.identity.clone()
120    }
121}