call2.rs

  1pub mod call_settings;
  2pub mod participant;
  3pub mod room;
  4mod shared_screen;
  5
  6use anyhow::{anyhow, Result};
  7use async_trait::async_trait;
  8use audio::Audio;
  9use call_settings::CallSettings;
 10use client::{
 11    proto::{self, PeerId},
 12    Client, TelemetrySettings, TypedEnvelope, User, UserStore, ZED_ALWAYS_ACTIVE,
 13};
 14use collections::HashSet;
 15use futures::{channel::oneshot, future::Shared, Future, FutureExt};
 16use gpui::{
 17    AppContext, AsyncAppContext, Context, EventEmitter, Model, ModelContext, Subscription, Task,
 18    View, ViewContext, VisualContext, WeakModel, WeakView,
 19};
 20pub use participant::ParticipantLocation;
 21use postage::watch;
 22use project::Project;
 23use room::Event;
 24pub use room::Room;
 25use settings::Settings;
 26use shared_screen::SharedScreen;
 27use std::sync::Arc;
 28use util::ResultExt;
 29use workspace::{item::ItemHandle, CallHandler, Pane, Workspace};
 30
 31pub fn init(client: Arc<Client>, user_store: Model<UserStore>, cx: &mut AppContext) {
 32    CallSettings::register(cx);
 33
 34    let active_call = cx.build_model(|cx| ActiveCall::new(client, user_store, cx));
 35    cx.set_global(active_call);
 36}
 37
 38pub struct OneAtATime {
 39    cancel: Option<oneshot::Sender<()>>,
 40}
 41
 42impl OneAtATime {
 43    /// spawn a task in the given context.
 44    /// if another task is spawned before that resolves, or if the OneAtATime itself is dropped, the first task will be cancelled and return Ok(None)
 45    /// otherwise you'll see the result of the task.
 46    fn spawn<F, Fut, R>(&mut self, cx: &mut AppContext, f: F) -> Task<Result<Option<R>>>
 47    where
 48        F: 'static + FnOnce(AsyncAppContext) -> Fut,
 49        Fut: Future<Output = Result<R>>,
 50        R: 'static,
 51    {
 52        let (tx, rx) = oneshot::channel();
 53        self.cancel.replace(tx);
 54        cx.spawn(|cx| async move {
 55            futures::select_biased! {
 56                _ = rx.fuse() => Ok(None),
 57                result = f(cx).fuse() => result.map(Some),
 58            }
 59        })
 60    }
 61
 62    fn running(&self) -> bool {
 63        self.cancel
 64            .as_ref()
 65            .is_some_and(|cancel| !cancel.is_canceled())
 66    }
 67}
 68
 69#[derive(Clone)]
 70pub struct IncomingCall {
 71    pub room_id: u64,
 72    pub calling_user: Arc<User>,
 73    pub participants: Vec<Arc<User>>,
 74    pub initial_project: Option<proto::ParticipantProject>,
 75}
 76
 77/// Singleton global maintaining the user's participation in a room across workspaces.
 78pub struct ActiveCall {
 79    room: Option<(Model<Room>, Vec<Subscription>)>,
 80    pending_room_creation: Option<Shared<Task<Result<Model<Room>, Arc<anyhow::Error>>>>>,
 81    location: Option<WeakModel<Project>>,
 82    _join_debouncer: OneAtATime,
 83    pending_invites: HashSet<u64>,
 84    incoming_call: (
 85        watch::Sender<Option<IncomingCall>>,
 86        watch::Receiver<Option<IncomingCall>>,
 87    ),
 88    client: Arc<Client>,
 89    user_store: Model<UserStore>,
 90    _subscriptions: Vec<client::Subscription>,
 91}
 92
 93impl EventEmitter<Event> for ActiveCall {}
 94
 95impl ActiveCall {
 96    fn new(client: Arc<Client>, user_store: Model<UserStore>, cx: &mut ModelContext<Self>) -> Self {
 97        Self {
 98            room: None,
 99            pending_room_creation: None,
100            location: None,
101            pending_invites: Default::default(),
102            incoming_call: watch::channel(),
103            _join_debouncer: OneAtATime { cancel: None },
104            _subscriptions: vec![
105                client.add_request_handler(cx.weak_model(), Self::handle_incoming_call),
106                client.add_message_handler(cx.weak_model(), Self::handle_call_canceled),
107            ],
108            client,
109            user_store,
110        }
111    }
112
113    pub fn channel_id(&self, cx: &AppContext) -> Option<u64> {
114        self.room()?.read(cx).channel_id()
115    }
116
117    async fn handle_incoming_call(
118        this: Model<Self>,
119        envelope: TypedEnvelope<proto::IncomingCall>,
120        _: Arc<Client>,
121        mut cx: AsyncAppContext,
122    ) -> Result<proto::Ack> {
123        let user_store = this.update(&mut cx, |this, _| this.user_store.clone())?;
124        let call = IncomingCall {
125            room_id: envelope.payload.room_id,
126            participants: user_store
127                .update(&mut cx, |user_store, cx| {
128                    user_store.get_users(envelope.payload.participant_user_ids, cx)
129                })?
130                .await?,
131            calling_user: user_store
132                .update(&mut cx, |user_store, cx| {
133                    user_store.get_user(envelope.payload.calling_user_id, cx)
134                })?
135                .await?,
136            initial_project: envelope.payload.initial_project,
137        };
138        this.update(&mut cx, |this, _| {
139            *this.incoming_call.0.borrow_mut() = Some(call);
140        })?;
141
142        Ok(proto::Ack {})
143    }
144
145    async fn handle_call_canceled(
146        this: Model<Self>,
147        envelope: TypedEnvelope<proto::CallCanceled>,
148        _: Arc<Client>,
149        mut cx: AsyncAppContext,
150    ) -> Result<()> {
151        this.update(&mut cx, |this, _| {
152            let mut incoming_call = this.incoming_call.0.borrow_mut();
153            if incoming_call
154                .as_ref()
155                .map_or(false, |call| call.room_id == envelope.payload.room_id)
156            {
157                incoming_call.take();
158            }
159        })?;
160        Ok(())
161    }
162
163    pub fn global(cx: &AppContext) -> Model<Self> {
164        cx.global::<Model<Self>>().clone()
165    }
166
167    pub fn invite(
168        &mut self,
169        called_user_id: u64,
170        initial_project: Option<Model<Project>>,
171        cx: &mut ModelContext<Self>,
172    ) -> Task<Result<()>> {
173        if !self.pending_invites.insert(called_user_id) {
174            return Task::ready(Err(anyhow!("user was already invited")));
175        }
176        cx.notify();
177
178        if self._join_debouncer.running() {
179            return Task::ready(Ok(()));
180        }
181
182        let room = if let Some(room) = self.room().cloned() {
183            Some(Task::ready(Ok(room)).shared())
184        } else {
185            self.pending_room_creation.clone()
186        };
187
188        let invite = if let Some(room) = room {
189            cx.spawn(move |_, mut cx| async move {
190                let room = room.await.map_err(|err| anyhow!("{:?}", err))?;
191
192                let initial_project_id = if let Some(initial_project) = initial_project {
193                    Some(
194                        room.update(&mut cx, |room, cx| room.share_project(initial_project, cx))?
195                            .await?,
196                    )
197                } else {
198                    None
199                };
200
201                room.update(&mut cx, move |room, cx| {
202                    room.call(called_user_id, initial_project_id, cx)
203                })?
204                .await?;
205
206                anyhow::Ok(())
207            })
208        } else {
209            let client = self.client.clone();
210            let user_store = self.user_store.clone();
211            let room = cx
212                .spawn(move |this, mut cx| async move {
213                    let create_room = async {
214                        let room = cx
215                            .update(|cx| {
216                                Room::create(
217                                    called_user_id,
218                                    initial_project,
219                                    client,
220                                    user_store,
221                                    cx,
222                                )
223                            })?
224                            .await?;
225
226                        this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx))?
227                            .await?;
228
229                        anyhow::Ok(room)
230                    };
231
232                    let room = create_room.await;
233                    this.update(&mut cx, |this, _| this.pending_room_creation = None)?;
234                    room.map_err(Arc::new)
235                })
236                .shared();
237            self.pending_room_creation = Some(room.clone());
238            cx.background_executor().spawn(async move {
239                room.await.map_err(|err| anyhow!("{:?}", err))?;
240                anyhow::Ok(())
241            })
242        };
243
244        cx.spawn(move |this, mut cx| async move {
245            let result = invite.await;
246            if result.is_ok() {
247                this.update(&mut cx, |this, cx| this.report_call_event("invite", cx))?;
248            } else {
249                // TODO: Resport collaboration error
250            }
251
252            this.update(&mut cx, |this, cx| {
253                this.pending_invites.remove(&called_user_id);
254                cx.notify();
255            })?;
256            result
257        })
258    }
259
260    pub fn cancel_invite(
261        &mut self,
262        called_user_id: u64,
263        cx: &mut ModelContext<Self>,
264    ) -> Task<Result<()>> {
265        let room_id = if let Some(room) = self.room() {
266            room.read(cx).id()
267        } else {
268            return Task::ready(Err(anyhow!("no active call")));
269        };
270
271        let client = self.client.clone();
272        cx.background_executor().spawn(async move {
273            client
274                .request(proto::CancelCall {
275                    room_id,
276                    called_user_id,
277                })
278                .await?;
279            anyhow::Ok(())
280        })
281    }
282
283    pub fn incoming(&self) -> watch::Receiver<Option<IncomingCall>> {
284        self.incoming_call.1.clone()
285    }
286
287    pub fn accept_incoming(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
288        if self.room.is_some() {
289            return Task::ready(Err(anyhow!("cannot join while on another call")));
290        }
291
292        let call = if let Some(call) = self.incoming_call.1.borrow().clone() {
293            call
294        } else {
295            return Task::ready(Err(anyhow!("no incoming call")));
296        };
297
298        if self.pending_room_creation.is_some() {
299            return Task::ready(Ok(()));
300        }
301
302        let room_id = call.room_id.clone();
303        let client = self.client.clone();
304        let user_store = self.user_store.clone();
305        let join = self
306            ._join_debouncer
307            .spawn(cx, move |cx| Room::join(room_id, client, user_store, cx));
308
309        cx.spawn(|this, mut cx| async move {
310            let room = join.await?;
311            this.update(&mut cx, |this, cx| this.set_room(room.clone(), cx))?
312                .await?;
313            this.update(&mut cx, |this, cx| {
314                this.report_call_event("accept incoming", cx)
315            })?;
316            Ok(())
317        })
318    }
319
320    pub fn decline_incoming(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
321        let call = self
322            .incoming_call
323            .0
324            .borrow_mut()
325            .take()
326            .ok_or_else(|| anyhow!("no incoming call"))?;
327        report_call_event_for_room("decline incoming", call.room_id, None, &self.client, cx);
328        self.client.send(proto::DeclineCall {
329            room_id: call.room_id,
330        })?;
331        Ok(())
332    }
333
334    pub fn join_channel(
335        &mut self,
336        channel_id: u64,
337        cx: &mut ModelContext<Self>,
338    ) -> Task<Result<Option<Model<Room>>>> {
339        if let Some(room) = self.room().cloned() {
340            if room.read(cx).channel_id() == Some(channel_id) {
341                return Task::ready(Ok(Some(room)));
342            } else {
343                room.update(cx, |room, cx| room.clear_state(cx));
344            }
345        }
346
347        if self.pending_room_creation.is_some() {
348            return Task::ready(Ok(None));
349        }
350
351        let client = self.client.clone();
352        let user_store = self.user_store.clone();
353        let join = self._join_debouncer.spawn(cx, move |cx| async move {
354            Room::join_channel(channel_id, client, user_store, cx).await
355        });
356
357        cx.spawn(|this, mut cx| async move {
358            let room = join.await?;
359            this.update(&mut cx, |this, cx| this.set_room(room.clone(), cx))?
360                .await?;
361            this.update(&mut cx, |this, cx| {
362                this.report_call_event("join channel", cx)
363            })?;
364            Ok(room)
365        })
366    }
367
368    pub fn hang_up(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
369        cx.notify();
370        self.report_call_event("hang up", cx);
371
372        Audio::end_call(cx);
373        if let Some((room, _)) = self.room.take() {
374            room.update(cx, |room, cx| room.leave(cx))
375        } else {
376            Task::ready(Ok(()))
377        }
378    }
379
380    pub fn share_project(
381        &mut self,
382        project: Model<Project>,
383        cx: &mut ModelContext<Self>,
384    ) -> Task<Result<u64>> {
385        if let Some((room, _)) = self.room.as_ref() {
386            self.report_call_event("share project", cx);
387            room.update(cx, |room, cx| room.share_project(project, cx))
388        } else {
389            Task::ready(Err(anyhow!("no active call")))
390        }
391    }
392
393    pub fn unshare_project(
394        &mut self,
395        project: Model<Project>,
396        cx: &mut ModelContext<Self>,
397    ) -> Result<()> {
398        if let Some((room, _)) = self.room.as_ref() {
399            self.report_call_event("unshare project", cx);
400            room.update(cx, |room, cx| room.unshare_project(project, cx))
401        } else {
402            Err(anyhow!("no active call"))
403        }
404    }
405
406    pub fn location(&self) -> Option<&WeakModel<Project>> {
407        self.location.as_ref()
408    }
409
410    pub fn set_location(
411        &mut self,
412        project: Option<&Model<Project>>,
413        cx: &mut ModelContext<Self>,
414    ) -> Task<Result<()>> {
415        if project.is_some() || !*ZED_ALWAYS_ACTIVE {
416            self.location = project.map(|project| project.downgrade());
417            if let Some((room, _)) = self.room.as_ref() {
418                return room.update(cx, |room, cx| room.set_location(project, cx));
419            }
420        }
421        Task::ready(Ok(()))
422    }
423
424    fn set_room(
425        &mut self,
426        room: Option<Model<Room>>,
427        cx: &mut ModelContext<Self>,
428    ) -> Task<Result<()>> {
429        if room.as_ref() != self.room.as_ref().map(|room| &room.0) {
430            cx.notify();
431            if let Some(room) = room {
432                if room.read(cx).status().is_offline() {
433                    self.room = None;
434                    Task::ready(Ok(()))
435                } else {
436                    let subscriptions = vec![
437                        cx.observe(&room, |this, room, cx| {
438                            if room.read(cx).status().is_offline() {
439                                this.set_room(None, cx).detach_and_log_err(cx);
440                            }
441
442                            cx.notify();
443                        }),
444                        cx.subscribe(&room, |_, _, event, cx| cx.emit(event.clone())),
445                    ];
446                    self.room = Some((room.clone(), subscriptions));
447                    let location = self
448                        .location
449                        .as_ref()
450                        .and_then(|location| location.upgrade());
451                    room.update(cx, |room, cx| room.set_location(location.as_ref(), cx))
452                }
453            } else {
454                self.room = None;
455                Task::ready(Ok(()))
456            }
457        } else {
458            Task::ready(Ok(()))
459        }
460    }
461
462    pub fn room(&self) -> Option<&Model<Room>> {
463        self.room.as_ref().map(|(room, _)| room)
464    }
465
466    pub fn client(&self) -> Arc<Client> {
467        self.client.clone()
468    }
469
470    pub fn pending_invites(&self) -> &HashSet<u64> {
471        &self.pending_invites
472    }
473
474    pub fn report_call_event(&self, operation: &'static str, cx: &mut AppContext) {
475        if let Some(room) = self.room() {
476            let room = room.read(cx);
477            report_call_event_for_room(operation, room.id(), room.channel_id(), &self.client, cx);
478        }
479    }
480}
481
482pub fn report_call_event_for_room(
483    operation: &'static str,
484    room_id: u64,
485    channel_id: Option<u64>,
486    client: &Arc<Client>,
487    cx: &mut AppContext,
488) {
489    let telemetry = client.telemetry();
490    let telemetry_settings = *TelemetrySettings::get_global(cx);
491
492    telemetry.report_call_event(telemetry_settings, operation, Some(room_id), channel_id)
493}
494
495pub fn report_call_event_for_channel(
496    operation: &'static str,
497    channel_id: u64,
498    client: &Arc<Client>,
499    cx: &AppContext,
500) {
501    let room = ActiveCall::global(cx).read(cx).room();
502
503    let telemetry = client.telemetry();
504
505    let telemetry_settings = *TelemetrySettings::get_global(cx);
506
507    telemetry.report_call_event(
508        telemetry_settings,
509        operation,
510        room.map(|r| r.read(cx).id()),
511        Some(channel_id),
512    )
513}
514
515pub struct Call {
516    active_call: Option<(Model<ActiveCall>, Vec<Subscription>)>,
517    parent_workspace: WeakView<Workspace>,
518}
519
520impl Call {
521    pub fn new(
522        parent_workspace: WeakView<Workspace>,
523        cx: &mut ViewContext<'_, Workspace>,
524    ) -> Box<dyn CallHandler> {
525        let mut active_call = None;
526        if cx.has_global::<Model<ActiveCall>>() {
527            let call = cx.global::<Model<ActiveCall>>().clone();
528            let subscriptions = vec![cx.subscribe(&call, Self::on_active_call_event)];
529            active_call = Some((call, subscriptions));
530        }
531        Box::new(Self {
532            active_call,
533            parent_workspace,
534        })
535    }
536    fn on_active_call_event(
537        workspace: &mut Workspace,
538        _: Model<ActiveCall>,
539        event: &room::Event,
540        cx: &mut ViewContext<Workspace>,
541    ) {
542        match event {
543            room::Event::ParticipantLocationChanged { participant_id }
544            | room::Event::RemoteVideoTracksChanged { participant_id } => {
545                workspace.leader_updated(*participant_id, cx);
546            }
547            _ => {}
548        }
549    }
550}
551
552#[async_trait(?Send)]
553impl CallHandler for Call {
554    fn peer_state(
555        &mut self,
556        leader_id: PeerId,
557        cx: &mut ViewContext<Workspace>,
558    ) -> Option<(bool, bool)> {
559        let (call, _) = self.active_call.as_ref()?;
560        let room = call.read(cx).room()?.read(cx);
561        let participant = room.remote_participant_for_peer_id(leader_id)?;
562
563        let leader_in_this_app;
564        let leader_in_this_project;
565        match participant.location {
566            ParticipantLocation::SharedProject { project_id } => {
567                leader_in_this_app = true;
568                leader_in_this_project = Some(project_id)
569                    == self
570                        .parent_workspace
571                        .update(cx, |this, cx| this.project().read(cx).remote_id())
572                        .log_err()
573                        .flatten();
574            }
575            ParticipantLocation::UnsharedProject => {
576                leader_in_this_app = true;
577                leader_in_this_project = false;
578            }
579            ParticipantLocation::External => {
580                leader_in_this_app = false;
581                leader_in_this_project = false;
582            }
583        };
584
585        Some((leader_in_this_project, leader_in_this_app))
586    }
587
588    fn shared_screen_for_peer(
589        &self,
590        peer_id: PeerId,
591        pane: &View<Pane>,
592        cx: &mut ViewContext<Workspace>,
593    ) -> Option<Box<dyn ItemHandle>> {
594        let (call, _) = self.active_call.as_ref()?;
595        let room = call.read(cx).room()?.read(cx);
596        let participant = room.remote_participant_for_peer_id(peer_id)?;
597        let track = participant.video_tracks.values().next()?.clone();
598        let user = participant.user.clone();
599        for item in pane.read(cx).items_of_type::<SharedScreen>() {
600            if item.read(cx).peer_id == peer_id {
601                return Some(Box::new(item));
602            }
603        }
604
605        Some(Box::new(cx.build_view(|cx| {
606            SharedScreen::new(&track, peer_id, user.clone(), cx)
607        })))
608    }
609    fn room_id(&self, cx: &AppContext) -> Option<u64> {
610        Some(self.active_call.as_ref()?.0.read(cx).room()?.read(cx).id())
611    }
612    fn hang_up(&self, cx: &mut AppContext) -> Task<Result<()>> {
613        let Some((call, _)) = self.active_call.as_ref() else {
614            return Task::ready(Err(anyhow!("Cannot exit a call; not in a call")));
615        };
616
617        call.update(cx, |this, cx| this.hang_up(cx))
618    }
619    fn active_project(&self, cx: &AppContext) -> Option<WeakModel<Project>> {
620        ActiveCall::global(cx).read(cx).location().cloned()
621    }
622    fn invite(
623        &mut self,
624        called_user_id: u64,
625        initial_project: Option<Model<Project>>,
626        cx: &mut AppContext,
627    ) -> Task<Result<()>> {
628        ActiveCall::global(cx).update(cx, |this, cx| {
629            this.invite(called_user_id, initial_project, cx)
630        })
631    }
632    fn remote_participants(&self, cx: &AppContext) -> Option<Vec<(Arc<User>, PeerId)>> {
633        self.active_call
634            .as_ref()
635            .map(|call| {
636                call.0.read(cx).room().map(|room| {
637                    room.read(cx)
638                        .remote_participants()
639                        .iter()
640                        .map(|participant| {
641                            (participant.1.user.clone(), participant.1.peer_id.clone())
642                        })
643                        .collect()
644                })
645            })
646            .flatten()
647    }
648    fn is_muted(&self, cx: &AppContext) -> Option<bool> {
649        self.active_call
650            .as_ref()
651            .map(|call| {
652                call.0
653                    .read(cx)
654                    .room()
655                    .map(|room| room.read(cx).is_muted(cx))
656            })
657            .flatten()
658    }
659    fn toggle_mute(&self, cx: &mut AppContext) {
660        self.active_call.as_ref().map(|call| {
661            call.0.update(cx, |this, cx| {
662                this.room().map(|room| {
663                    let room = room.clone();
664                    cx.spawn(|_, mut cx| async move {
665                        room.update(&mut cx, |this, cx| this.toggle_mute(cx))??
666                            .await
667                    })
668                    .detach_and_log_err(cx);
669                })
670            })
671        });
672    }
673    fn toggle_screen_share(&self, cx: &mut AppContext) {
674        self.active_call.as_ref().map(|call| {
675            call.0.update(cx, |this, cx| {
676                this.room().map(|room| {
677                    room.update(cx, |this, cx| {
678                        if this.is_screen_sharing() {
679                            this.unshare_screen(cx).log_err();
680                        } else {
681                            let t = this.share_screen(cx);
682                            cx.spawn(move |_, _| async move {
683                                t.await.log_err();
684                            })
685                            .detach();
686                        }
687                    })
688                })
689            })
690        });
691    }
692    fn toggle_deafen(&self, cx: &mut AppContext) {
693        self.active_call.as_ref().map(|call| {
694            call.0.update(cx, |this, cx| {
695                this.room().map(|room| {
696                    room.update(cx, |this, cx| {
697                        this.toggle_deafen(cx).log_err();
698                    })
699                })
700            })
701        });
702    }
703    fn is_deafened(&self, cx: &AppContext) -> Option<bool> {
704        self.active_call
705            .as_ref()
706            .map(|call| {
707                call.0
708                    .read(cx)
709                    .room()
710                    .map(|room| room.read(cx).is_deafened())
711            })
712            .flatten()
713            .flatten()
714    }
715}
716
717#[cfg(test)]
718mod test {
719    use gpui::TestAppContext;
720
721    use crate::OneAtATime;
722
723    #[gpui::test]
724    async fn test_one_at_a_time(cx: &mut TestAppContext) {
725        let mut one_at_a_time = OneAtATime { cancel: None };
726
727        assert_eq!(
728            cx.update(|cx| one_at_a_time.spawn(cx, |_| async { Ok(1) }))
729                .await
730                .unwrap(),
731            Some(1)
732        );
733
734        let (a, b) = cx.update(|cx| {
735            (
736                one_at_a_time.spawn(cx, |_| async {
737                    assert!(false);
738                    Ok(2)
739                }),
740                one_at_a_time.spawn(cx, |_| async { Ok(3) }),
741            )
742        });
743
744        assert_eq!(a.await.unwrap(), None);
745        assert_eq!(b.await.unwrap(), Some(3));
746
747        let promise = cx.update(|cx| one_at_a_time.spawn(cx, |_| async { Ok(4) }));
748        drop(one_at_a_time);
749
750        assert_eq!(promise.await.unwrap(), None);
751    }
752}