1use crate::{
2 call_settings::CallSettings,
3 participant::{LocalParticipant, ParticipantLocation, RemoteParticipant, RemoteVideoTrack},
4 IncomingCall,
5};
6use anyhow::{anyhow, Result};
7use audio::{Audio, Sound};
8use client::{
9 proto::{self, PeerId},
10 Client, TypedEnvelope, User, UserStore,
11};
12use collections::{BTreeMap, HashMap, HashSet};
13use fs::Fs;
14use futures::{FutureExt, StreamExt};
15use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, WeakModelHandle};
16use language::LanguageRegistry;
17use live_kit_client::{
18 LocalAudioTrack, LocalTrackPublication, LocalVideoTrack, RemoteAudioTrackUpdate,
19 RemoteVideoTrackUpdate,
20};
21use postage::stream::Stream;
22use project::Project;
23use std::{future::Future, mem, pin::Pin, sync::Arc, time::Duration};
24use util::{post_inc, ResultExt, TryFutureExt};
25
26pub const RECONNECT_TIMEOUT: Duration = Duration::from_secs(30);
27
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub enum Event {
30 ParticipantLocationChanged {
31 participant_id: proto::PeerId,
32 },
33 RemoteVideoTracksChanged {
34 participant_id: proto::PeerId,
35 },
36 RemoteAudioTracksChanged {
37 participant_id: proto::PeerId,
38 },
39 RemoteProjectShared {
40 owner: Arc<User>,
41 project_id: u64,
42 worktree_root_names: Vec<String>,
43 },
44 RemoteProjectUnshared {
45 project_id: u64,
46 },
47 Left,
48}
49
50pub struct Room {
51 id: u64,
52 channel_id: Option<u64>,
53 live_kit: Option<LiveKitRoom>,
54 status: RoomStatus,
55 shared_projects: HashSet<WeakModelHandle<Project>>,
56 joined_projects: HashSet<WeakModelHandle<Project>>,
57 local_participant: LocalParticipant,
58 remote_participants: BTreeMap<u64, RemoteParticipant>,
59 pending_participants: Vec<Arc<User>>,
60 participant_user_ids: HashSet<u64>,
61 pending_call_count: usize,
62 leave_when_empty: bool,
63 client: Arc<Client>,
64 user_store: ModelHandle<UserStore>,
65 follows_by_leader_id_project_id: HashMap<(PeerId, u64), Vec<PeerId>>,
66 subscriptions: Vec<client::Subscription>,
67 pending_room_update: Option<Task<()>>,
68 maintain_connection: Option<Task<Option<()>>>,
69}
70
71impl Entity for Room {
72 type Event = Event;
73
74 fn release(&mut self, cx: &mut AppContext) {
75 if self.status.is_online() {
76 self.leave_internal(cx).detach_and_log_err(cx);
77 }
78 }
79
80 fn app_will_quit(&mut self, cx: &mut AppContext) -> Option<Pin<Box<dyn Future<Output = ()>>>> {
81 if self.status.is_online() {
82 let leave = self.leave_internal(cx);
83 Some(
84 cx.background()
85 .spawn(async move {
86 leave.await.log_err();
87 })
88 .boxed(),
89 )
90 } else {
91 None
92 }
93 }
94}
95
96impl Room {
97 pub fn channel_id(&self) -> Option<u64> {
98 self.channel_id
99 }
100
101 #[cfg(any(test, feature = "test-support"))]
102 pub fn is_connected(&self) -> bool {
103 if let Some(live_kit) = self.live_kit.as_ref() {
104 matches!(
105 *live_kit.room.status().borrow(),
106 live_kit_client::ConnectionState::Connected { .. }
107 )
108 } else {
109 false
110 }
111 }
112
113 fn new(
114 id: u64,
115 channel_id: Option<u64>,
116 live_kit_connection_info: Option<proto::LiveKitConnectionInfo>,
117 client: Arc<Client>,
118 user_store: ModelHandle<UserStore>,
119 cx: &mut ModelContext<Self>,
120 ) -> Self {
121 let live_kit_room = if let Some(connection_info) = live_kit_connection_info {
122 let room = live_kit_client::Room::new();
123 let mut status = room.status();
124 // Consume the initial status of the room.
125 let _ = status.try_recv();
126 let _maintain_room = cx.spawn_weak(|this, mut cx| async move {
127 while let Some(status) = status.next().await {
128 let this = if let Some(this) = this.upgrade(&cx) {
129 this
130 } else {
131 break;
132 };
133
134 if status == live_kit_client::ConnectionState::Disconnected {
135 this.update(&mut cx, |this, cx| this.leave(cx).log_err());
136 break;
137 }
138 }
139 });
140
141 let mut track_video_changes = room.remote_video_track_updates();
142 let _maintain_video_tracks = cx.spawn_weak(|this, mut cx| async move {
143 while let Some(track_change) = track_video_changes.next().await {
144 let this = if let Some(this) = this.upgrade(&cx) {
145 this
146 } else {
147 break;
148 };
149
150 this.update(&mut cx, |this, cx| {
151 this.remote_video_track_updated(track_change, cx).log_err()
152 });
153 }
154 });
155
156 let mut track_audio_changes = room.remote_audio_track_updates();
157 let _maintain_audio_tracks = cx.spawn_weak(|this, mut cx| async move {
158 while let Some(track_change) = track_audio_changes.next().await {
159 let this = if let Some(this) = this.upgrade(&cx) {
160 this
161 } else {
162 break;
163 };
164
165 this.update(&mut cx, |this, cx| {
166 this.remote_audio_track_updated(track_change, cx).log_err()
167 });
168 }
169 });
170
171 let connect = room.connect(&connection_info.server_url, &connection_info.token);
172 cx.spawn(|this, mut cx| async move {
173 connect.await?;
174
175 if !cx.read(Self::mute_on_join) {
176 this.update(&mut cx, |this, cx| this.share_microphone(cx))
177 .await?;
178 }
179
180 anyhow::Ok(())
181 })
182 .detach_and_log_err(cx);
183
184 Some(LiveKitRoom {
185 room,
186 screen_track: LocalTrack::None,
187 microphone_track: LocalTrack::None,
188 next_publish_id: 0,
189 muted_by_user: false,
190 deafened: false,
191 speaking: false,
192 _maintain_room,
193 _maintain_tracks: [_maintain_video_tracks, _maintain_audio_tracks],
194 })
195 } else {
196 None
197 };
198
199 let maintain_connection =
200 cx.spawn_weak(|this, cx| Self::maintain_connection(this, client.clone(), cx).log_err());
201
202 Audio::play_sound(Sound::Joined, cx);
203
204 Self {
205 id,
206 channel_id,
207 live_kit: live_kit_room,
208 status: RoomStatus::Online,
209 shared_projects: Default::default(),
210 joined_projects: Default::default(),
211 participant_user_ids: Default::default(),
212 local_participant: Default::default(),
213 remote_participants: Default::default(),
214 pending_participants: Default::default(),
215 pending_call_count: 0,
216 subscriptions: vec![client.add_message_handler(cx.handle(), Self::handle_room_updated)],
217 leave_when_empty: false,
218 pending_room_update: None,
219 client,
220 user_store,
221 follows_by_leader_id_project_id: Default::default(),
222 maintain_connection: Some(maintain_connection),
223 }
224 }
225
226 pub(crate) fn create(
227 called_user_id: u64,
228 initial_project: Option<ModelHandle<Project>>,
229 client: Arc<Client>,
230 user_store: ModelHandle<UserStore>,
231 cx: &mut AppContext,
232 ) -> Task<Result<ModelHandle<Self>>> {
233 cx.spawn(|mut cx| async move {
234 let response = client.request(proto::CreateRoom {}).await?;
235 let room_proto = response.room.ok_or_else(|| anyhow!("invalid room"))?;
236 let room = cx.add_model(|cx| {
237 Self::new(
238 room_proto.id,
239 None,
240 response.live_kit_connection_info,
241 client,
242 user_store,
243 cx,
244 )
245 });
246
247 let initial_project_id = if let Some(initial_project) = initial_project {
248 let initial_project_id = room
249 .update(&mut cx, |room, cx| {
250 room.share_project(initial_project.clone(), cx)
251 })
252 .await?;
253 Some(initial_project_id)
254 } else {
255 None
256 };
257
258 match room
259 .update(&mut cx, |room, cx| {
260 room.leave_when_empty = true;
261 room.call(called_user_id, initial_project_id, cx)
262 })
263 .await
264 {
265 Ok(()) => Ok(room),
266 Err(error) => Err(anyhow!("room creation failed: {:?}", error)),
267 }
268 })
269 }
270
271 pub(crate) fn join_channel(
272 channel_id: u64,
273 client: Arc<Client>,
274 user_store: ModelHandle<UserStore>,
275 cx: &mut AppContext,
276 ) -> Task<Result<ModelHandle<Self>>> {
277 cx.spawn(|cx| async move {
278 Self::from_join_response(
279 client.request(proto::JoinChannel { channel_id }).await?,
280 client,
281 user_store,
282 cx,
283 )
284 })
285 }
286
287 pub(crate) fn join(
288 call: &IncomingCall,
289 client: Arc<Client>,
290 user_store: ModelHandle<UserStore>,
291 cx: &mut AppContext,
292 ) -> Task<Result<ModelHandle<Self>>> {
293 let id = call.room_id;
294 cx.spawn(|cx| async move {
295 Self::from_join_response(
296 client.request(proto::JoinRoom { id }).await?,
297 client,
298 user_store,
299 cx,
300 )
301 })
302 }
303
304 pub fn mute_on_join(cx: &AppContext) -> bool {
305 settings::get::<CallSettings>(cx).mute_on_join || client::IMPERSONATE_LOGIN.is_some()
306 }
307
308 fn from_join_response(
309 response: proto::JoinRoomResponse,
310 client: Arc<Client>,
311 user_store: ModelHandle<UserStore>,
312 mut cx: AsyncAppContext,
313 ) -> Result<ModelHandle<Self>> {
314 let room_proto = response.room.ok_or_else(|| anyhow!("invalid room"))?;
315 let room = cx.add_model(|cx| {
316 Self::new(
317 room_proto.id,
318 response.channel_id,
319 response.live_kit_connection_info,
320 client,
321 user_store,
322 cx,
323 )
324 });
325 room.update(&mut cx, |room, cx| {
326 room.leave_when_empty = room.channel_id.is_none();
327 room.apply_room_update(room_proto, cx)?;
328 anyhow::Ok(())
329 })?;
330 Ok(room)
331 }
332
333 fn should_leave(&self) -> bool {
334 self.leave_when_empty
335 && self.pending_room_update.is_none()
336 && self.pending_participants.is_empty()
337 && self.remote_participants.is_empty()
338 && self.pending_call_count == 0
339 }
340
341 pub(crate) fn leave(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
342 cx.notify();
343 cx.emit(Event::Left);
344 self.leave_internal(cx)
345 }
346
347 fn leave_internal(&mut self, cx: &mut AppContext) -> Task<Result<()>> {
348 if self.status.is_offline() {
349 return Task::ready(Err(anyhow!("room is offline")));
350 }
351
352 log::info!("leaving room");
353 Audio::play_sound(Sound::Leave, cx);
354
355 self.clear_state(cx);
356
357 let leave_room = self.client.request(proto::LeaveRoom {});
358 cx.background().spawn(async move {
359 leave_room.await?;
360 anyhow::Ok(())
361 })
362 }
363
364 pub(crate) fn clear_state(&mut self, cx: &mut AppContext) {
365 for project in self.shared_projects.drain() {
366 if let Some(project) = project.upgrade(cx) {
367 project.update(cx, |project, cx| {
368 project.unshare(cx).log_err();
369 });
370 }
371 }
372 for project in self.joined_projects.drain() {
373 if let Some(project) = project.upgrade(cx) {
374 project.update(cx, |project, cx| {
375 project.disconnected_from_host(cx);
376 project.close(cx);
377 });
378 }
379 }
380
381 self.status = RoomStatus::Offline;
382 self.remote_participants.clear();
383 self.pending_participants.clear();
384 self.participant_user_ids.clear();
385 self.subscriptions.clear();
386 self.live_kit.take();
387 self.pending_room_update.take();
388 self.maintain_connection.take();
389 }
390
391 async fn maintain_connection(
392 this: WeakModelHandle<Self>,
393 client: Arc<Client>,
394 mut cx: AsyncAppContext,
395 ) -> Result<()> {
396 let mut client_status = client.status();
397 loop {
398 let _ = client_status.try_recv();
399 let is_connected = client_status.borrow().is_connected();
400 // Even if we're initially connected, any future change of the status means we momentarily disconnected.
401 if !is_connected || client_status.next().await.is_some() {
402 log::info!("detected client disconnection");
403
404 this.upgrade(&cx)
405 .ok_or_else(|| anyhow!("room was dropped"))?
406 .update(&mut cx, |this, cx| {
407 this.status = RoomStatus::Rejoining;
408 cx.notify();
409 });
410
411 // Wait for client to re-establish a connection to the server.
412 {
413 let mut reconnection_timeout = cx.background().timer(RECONNECT_TIMEOUT).fuse();
414 let client_reconnection = async {
415 let mut remaining_attempts = 3;
416 while remaining_attempts > 0 {
417 if client_status.borrow().is_connected() {
418 log::info!("client reconnected, attempting to rejoin room");
419
420 let Some(this) = this.upgrade(&cx) else { break };
421 if this
422 .update(&mut cx, |this, cx| this.rejoin(cx))
423 .await
424 .log_err()
425 .is_some()
426 {
427 return true;
428 } else {
429 remaining_attempts -= 1;
430 }
431 } else if client_status.borrow().is_signed_out() {
432 return false;
433 }
434
435 log::info!(
436 "waiting for client status change, remaining attempts {}",
437 remaining_attempts
438 );
439 client_status.next().await;
440 }
441 false
442 }
443 .fuse();
444 futures::pin_mut!(client_reconnection);
445
446 futures::select_biased! {
447 reconnected = client_reconnection => {
448 if reconnected {
449 log::info!("successfully reconnected to room");
450 // If we successfully joined the room, go back around the loop
451 // waiting for future connection status changes.
452 continue;
453 }
454 }
455 _ = reconnection_timeout => {
456 log::info!("room reconnection timeout expired");
457 }
458 }
459 }
460
461 break;
462 }
463 }
464
465 // The client failed to re-establish a connection to the server
466 // or an error occurred while trying to re-join the room. Either way
467 // we leave the room and return an error.
468 if let Some(this) = this.upgrade(&cx) {
469 log::info!("reconnection failed, leaving room");
470 let _ = this.update(&mut cx, |this, cx| this.leave(cx));
471 }
472 Err(anyhow!(
473 "can't reconnect to room: client failed to re-establish connection"
474 ))
475 }
476
477 fn rejoin(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
478 let mut projects = HashMap::default();
479 let mut reshared_projects = Vec::new();
480 let mut rejoined_projects = Vec::new();
481 self.shared_projects.retain(|project| {
482 if let Some(handle) = project.upgrade(cx) {
483 let project = handle.read(cx);
484 if let Some(project_id) = project.remote_id() {
485 projects.insert(project_id, handle.clone());
486 reshared_projects.push(proto::UpdateProject {
487 project_id,
488 worktrees: project.worktree_metadata_protos(cx),
489 });
490 return true;
491 }
492 }
493 false
494 });
495 self.joined_projects.retain(|project| {
496 if let Some(handle) = project.upgrade(cx) {
497 let project = handle.read(cx);
498 if let Some(project_id) = project.remote_id() {
499 projects.insert(project_id, handle.clone());
500 rejoined_projects.push(proto::RejoinProject {
501 id: project_id,
502 worktrees: project
503 .worktrees(cx)
504 .map(|worktree| {
505 let worktree = worktree.read(cx);
506 proto::RejoinWorktree {
507 id: worktree.id().to_proto(),
508 scan_id: worktree.completed_scan_id() as u64,
509 }
510 })
511 .collect(),
512 });
513 }
514 return true;
515 }
516 false
517 });
518
519 let response = self.client.request_envelope(proto::RejoinRoom {
520 id: self.id,
521 reshared_projects,
522 rejoined_projects,
523 });
524
525 cx.spawn(|this, mut cx| async move {
526 let response = response.await?;
527 let message_id = response.message_id;
528 let response = response.payload;
529 let room_proto = response.room.ok_or_else(|| anyhow!("invalid room"))?;
530 this.update(&mut cx, |this, cx| {
531 this.status = RoomStatus::Online;
532 this.apply_room_update(room_proto, cx)?;
533
534 for reshared_project in response.reshared_projects {
535 if let Some(project) = projects.get(&reshared_project.id) {
536 project.update(cx, |project, cx| {
537 project.reshared(reshared_project, cx).log_err();
538 });
539 }
540 }
541
542 for rejoined_project in response.rejoined_projects {
543 if let Some(project) = projects.get(&rejoined_project.id) {
544 project.update(cx, |project, cx| {
545 project.rejoined(rejoined_project, message_id, cx).log_err();
546 });
547 }
548 }
549
550 anyhow::Ok(())
551 })
552 })
553 }
554
555 pub fn id(&self) -> u64 {
556 self.id
557 }
558
559 pub fn status(&self) -> RoomStatus {
560 self.status
561 }
562
563 pub fn local_participant(&self) -> &LocalParticipant {
564 &self.local_participant
565 }
566
567 pub fn remote_participants(&self) -> &BTreeMap<u64, RemoteParticipant> {
568 &self.remote_participants
569 }
570
571 pub fn remote_participant_for_peer_id(&self, peer_id: PeerId) -> Option<&RemoteParticipant> {
572 self.remote_participants
573 .values()
574 .find(|p| p.peer_id == peer_id)
575 }
576
577 pub fn pending_participants(&self) -> &[Arc<User>] {
578 &self.pending_participants
579 }
580
581 pub fn contains_participant(&self, user_id: u64) -> bool {
582 self.participant_user_ids.contains(&user_id)
583 }
584
585 pub fn followers_for(&self, leader_id: PeerId, project_id: u64) -> &[PeerId] {
586 self.follows_by_leader_id_project_id
587 .get(&(leader_id, project_id))
588 .map_or(&[], |v| v.as_slice())
589 }
590
591 async fn handle_room_updated(
592 this: ModelHandle<Self>,
593 envelope: TypedEnvelope<proto::RoomUpdated>,
594 _: Arc<Client>,
595 mut cx: AsyncAppContext,
596 ) -> Result<()> {
597 let room = envelope
598 .payload
599 .room
600 .ok_or_else(|| anyhow!("invalid room"))?;
601 this.update(&mut cx, |this, cx| this.apply_room_update(room, cx))
602 }
603
604 fn apply_room_update(
605 &mut self,
606 mut room: proto::Room,
607 cx: &mut ModelContext<Self>,
608 ) -> Result<()> {
609 // Filter ourselves out from the room's participants.
610 let local_participant_ix = room
611 .participants
612 .iter()
613 .position(|participant| Some(participant.user_id) == self.client.user_id());
614 let local_participant = local_participant_ix.map(|ix| room.participants.swap_remove(ix));
615
616 let pending_participant_user_ids = room
617 .pending_participants
618 .iter()
619 .map(|p| p.user_id)
620 .collect::<Vec<_>>();
621
622 let remote_participant_user_ids = room
623 .participants
624 .iter()
625 .map(|p| p.user_id)
626 .collect::<Vec<_>>();
627
628 let (remote_participants, pending_participants) =
629 self.user_store.update(cx, move |user_store, cx| {
630 (
631 user_store.get_users(remote_participant_user_ids, cx),
632 user_store.get_users(pending_participant_user_ids, cx),
633 )
634 });
635
636 self.pending_room_update = Some(cx.spawn(|this, mut cx| async move {
637 let (remote_participants, pending_participants) =
638 futures::join!(remote_participants, pending_participants);
639
640 this.update(&mut cx, |this, cx| {
641 this.participant_user_ids.clear();
642
643 if let Some(participant) = local_participant {
644 this.local_participant.projects = participant.projects;
645 } else {
646 this.local_participant.projects.clear();
647 }
648
649 if let Some(participants) = remote_participants.log_err() {
650 for (participant, user) in room.participants.into_iter().zip(participants) {
651 let Some(peer_id) = participant.peer_id else {
652 continue;
653 };
654 this.participant_user_ids.insert(participant.user_id);
655
656 let old_projects = this
657 .remote_participants
658 .get(&participant.user_id)
659 .into_iter()
660 .flat_map(|existing| &existing.projects)
661 .map(|project| project.id)
662 .collect::<HashSet<_>>();
663 let new_projects = participant
664 .projects
665 .iter()
666 .map(|project| project.id)
667 .collect::<HashSet<_>>();
668
669 for project in &participant.projects {
670 if !old_projects.contains(&project.id) {
671 cx.emit(Event::RemoteProjectShared {
672 owner: user.clone(),
673 project_id: project.id,
674 worktree_root_names: project.worktree_root_names.clone(),
675 });
676 }
677 }
678
679 for unshared_project_id in old_projects.difference(&new_projects) {
680 this.joined_projects.retain(|project| {
681 if let Some(project) = project.upgrade(cx) {
682 project.update(cx, |project, cx| {
683 if project.remote_id() == Some(*unshared_project_id) {
684 project.disconnected_from_host(cx);
685 false
686 } else {
687 true
688 }
689 })
690 } else {
691 false
692 }
693 });
694 cx.emit(Event::RemoteProjectUnshared {
695 project_id: *unshared_project_id,
696 });
697 }
698
699 let location = ParticipantLocation::from_proto(participant.location)
700 .unwrap_or(ParticipantLocation::External);
701 if let Some(remote_participant) =
702 this.remote_participants.get_mut(&participant.user_id)
703 {
704 remote_participant.projects = participant.projects;
705 remote_participant.peer_id = peer_id;
706 if location != remote_participant.location {
707 remote_participant.location = location;
708 cx.emit(Event::ParticipantLocationChanged {
709 participant_id: peer_id,
710 });
711 }
712 } else {
713 this.remote_participants.insert(
714 participant.user_id,
715 RemoteParticipant {
716 user: user.clone(),
717 peer_id,
718 projects: participant.projects,
719 location,
720 muted: true,
721 speaking: false,
722 video_tracks: Default::default(),
723 audio_tracks: Default::default(),
724 },
725 );
726
727 Audio::play_sound(Sound::Joined, cx);
728
729 if let Some(live_kit) = this.live_kit.as_ref() {
730 let video_tracks =
731 live_kit.room.remote_video_tracks(&user.id.to_string());
732 let audio_tracks =
733 live_kit.room.remote_audio_tracks(&user.id.to_string());
734 let publications = live_kit
735 .room
736 .remote_audio_track_publications(&user.id.to_string());
737
738 for track in video_tracks {
739 this.remote_video_track_updated(
740 RemoteVideoTrackUpdate::Subscribed(track),
741 cx,
742 )
743 .log_err();
744 }
745
746 for (track, publication) in
747 audio_tracks.iter().zip(publications.iter())
748 {
749 this.remote_audio_track_updated(
750 RemoteAudioTrackUpdate::Subscribed(
751 track.clone(),
752 publication.clone(),
753 ),
754 cx,
755 )
756 .log_err();
757 }
758 }
759 }
760 }
761
762 this.remote_participants.retain(|user_id, participant| {
763 if this.participant_user_ids.contains(user_id) {
764 true
765 } else {
766 for project in &participant.projects {
767 cx.emit(Event::RemoteProjectUnshared {
768 project_id: project.id,
769 });
770 }
771 false
772 }
773 });
774 }
775
776 if let Some(pending_participants) = pending_participants.log_err() {
777 this.pending_participants = pending_participants;
778 for participant in &this.pending_participants {
779 this.participant_user_ids.insert(participant.id);
780 }
781 }
782
783 this.follows_by_leader_id_project_id.clear();
784 for follower in room.followers {
785 let project_id = follower.project_id;
786 let (leader, follower) = match (follower.leader_id, follower.follower_id) {
787 (Some(leader), Some(follower)) => (leader, follower),
788
789 _ => {
790 log::error!("Follower message {follower:?} missing some state");
791 continue;
792 }
793 };
794
795 let list = this
796 .follows_by_leader_id_project_id
797 .entry((leader, project_id))
798 .or_insert(Vec::new());
799 if !list.contains(&follower) {
800 list.push(follower);
801 }
802 }
803
804 this.pending_room_update.take();
805 if this.should_leave() {
806 log::info!("room is empty, leaving");
807 let _ = this.leave(cx);
808 }
809
810 this.check_invariants();
811 cx.notify();
812 });
813 }));
814
815 cx.notify();
816 Ok(())
817 }
818
819 fn remote_video_track_updated(
820 &mut self,
821 change: RemoteVideoTrackUpdate,
822 cx: &mut ModelContext<Self>,
823 ) -> Result<()> {
824 match change {
825 RemoteVideoTrackUpdate::Subscribed(track) => {
826 let user_id = track.publisher_id().parse()?;
827 let track_id = track.sid().to_string();
828 let participant = self
829 .remote_participants
830 .get_mut(&user_id)
831 .ok_or_else(|| anyhow!("subscribed to track by unknown participant"))?;
832 participant.video_tracks.insert(
833 track_id.clone(),
834 Arc::new(RemoteVideoTrack {
835 live_kit_track: track,
836 }),
837 );
838 cx.emit(Event::RemoteVideoTracksChanged {
839 participant_id: participant.peer_id,
840 });
841 }
842 RemoteVideoTrackUpdate::Unsubscribed {
843 publisher_id,
844 track_id,
845 } => {
846 let user_id = publisher_id.parse()?;
847 let participant = self
848 .remote_participants
849 .get_mut(&user_id)
850 .ok_or_else(|| anyhow!("unsubscribed from track by unknown participant"))?;
851 participant.video_tracks.remove(&track_id);
852 cx.emit(Event::RemoteVideoTracksChanged {
853 participant_id: participant.peer_id,
854 });
855 }
856 }
857
858 cx.notify();
859 Ok(())
860 }
861
862 fn remote_audio_track_updated(
863 &mut self,
864 change: RemoteAudioTrackUpdate,
865 cx: &mut ModelContext<Self>,
866 ) -> Result<()> {
867 match change {
868 RemoteAudioTrackUpdate::ActiveSpeakersChanged { speakers } => {
869 let mut speaker_ids = speakers
870 .into_iter()
871 .filter_map(|speaker_sid| speaker_sid.parse().ok())
872 .collect::<Vec<u64>>();
873 speaker_ids.sort_unstable();
874 for (sid, participant) in &mut self.remote_participants {
875 if let Ok(_) = speaker_ids.binary_search(sid) {
876 participant.speaking = true;
877 } else {
878 participant.speaking = false;
879 }
880 }
881 if let Some(id) = self.client.user_id() {
882 if let Some(room) = &mut self.live_kit {
883 if let Ok(_) = speaker_ids.binary_search(&id) {
884 room.speaking = true;
885 } else {
886 room.speaking = false;
887 }
888 }
889 }
890 cx.notify();
891 }
892 RemoteAudioTrackUpdate::MuteChanged { track_id, muted } => {
893 let mut found = false;
894 for participant in &mut self.remote_participants.values_mut() {
895 for track in participant.audio_tracks.values() {
896 if track.sid() == track_id {
897 found = true;
898 break;
899 }
900 }
901 if found {
902 participant.muted = muted;
903 break;
904 }
905 }
906
907 cx.notify();
908 }
909 RemoteAudioTrackUpdate::Subscribed(track, publication) => {
910 let user_id = track.publisher_id().parse()?;
911 let track_id = track.sid().to_string();
912 let participant = self
913 .remote_participants
914 .get_mut(&user_id)
915 .ok_or_else(|| anyhow!("subscribed to track by unknown participant"))?;
916
917 participant.audio_tracks.insert(track_id.clone(), track);
918 participant.muted = publication.is_muted();
919
920 cx.emit(Event::RemoteAudioTracksChanged {
921 participant_id: participant.peer_id,
922 });
923 }
924 RemoteAudioTrackUpdate::Unsubscribed {
925 publisher_id,
926 track_id,
927 } => {
928 let user_id = publisher_id.parse()?;
929 let participant = self
930 .remote_participants
931 .get_mut(&user_id)
932 .ok_or_else(|| anyhow!("unsubscribed from track by unknown participant"))?;
933 participant.audio_tracks.remove(&track_id);
934 cx.emit(Event::RemoteAudioTracksChanged {
935 participant_id: participant.peer_id,
936 });
937 }
938 }
939
940 cx.notify();
941 Ok(())
942 }
943
944 fn check_invariants(&self) {
945 #[cfg(any(test, feature = "test-support"))]
946 {
947 for participant in self.remote_participants.values() {
948 assert!(self.participant_user_ids.contains(&participant.user.id));
949 assert_ne!(participant.user.id, self.client.user_id().unwrap());
950 }
951
952 for participant in &self.pending_participants {
953 assert!(self.participant_user_ids.contains(&participant.id));
954 assert_ne!(participant.id, self.client.user_id().unwrap());
955 }
956
957 assert_eq!(
958 self.participant_user_ids.len(),
959 self.remote_participants.len() + self.pending_participants.len()
960 );
961 }
962 }
963
964 pub(crate) fn call(
965 &mut self,
966 called_user_id: u64,
967 initial_project_id: Option<u64>,
968 cx: &mut ModelContext<Self>,
969 ) -> Task<Result<()>> {
970 if self.status.is_offline() {
971 return Task::ready(Err(anyhow!("room is offline")));
972 }
973
974 cx.notify();
975 let client = self.client.clone();
976 let room_id = self.id;
977 self.pending_call_count += 1;
978 cx.spawn(|this, mut cx| async move {
979 let result = client
980 .request(proto::Call {
981 room_id,
982 called_user_id,
983 initial_project_id,
984 })
985 .await;
986 this.update(&mut cx, |this, cx| {
987 this.pending_call_count -= 1;
988 if this.should_leave() {
989 this.leave(cx).detach_and_log_err(cx);
990 }
991 });
992 result?;
993 Ok(())
994 })
995 }
996
997 pub fn join_project(
998 &mut self,
999 id: u64,
1000 language_registry: Arc<LanguageRegistry>,
1001 fs: Arc<dyn Fs>,
1002 cx: &mut ModelContext<Self>,
1003 ) -> Task<Result<ModelHandle<Project>>> {
1004 let client = self.client.clone();
1005 let user_store = self.user_store.clone();
1006 cx.spawn(|this, mut cx| async move {
1007 let project =
1008 Project::remote(id, client, user_store, language_registry, fs, cx.clone()).await?;
1009
1010 this.update(&mut cx, |this, cx| {
1011 this.joined_projects.retain(|project| {
1012 if let Some(project) = project.upgrade(cx) {
1013 !project.read(cx).is_read_only()
1014 } else {
1015 false
1016 }
1017 });
1018 this.joined_projects.insert(project.downgrade());
1019 });
1020 Ok(project)
1021 })
1022 }
1023
1024 pub(crate) fn share_project(
1025 &mut self,
1026 project: ModelHandle<Project>,
1027 cx: &mut ModelContext<Self>,
1028 ) -> Task<Result<u64>> {
1029 if let Some(project_id) = project.read(cx).remote_id() {
1030 return Task::ready(Ok(project_id));
1031 }
1032
1033 let request = self.client.request(proto::ShareProject {
1034 room_id: self.id(),
1035 worktrees: project.read(cx).worktree_metadata_protos(cx),
1036 });
1037 cx.spawn(|this, mut cx| async move {
1038 let response = request.await?;
1039
1040 project.update(&mut cx, |project, cx| {
1041 project.shared(response.project_id, cx)
1042 })?;
1043
1044 // If the user's location is in this project, it changes from UnsharedProject to SharedProject.
1045 this.update(&mut cx, |this, cx| {
1046 this.shared_projects.insert(project.downgrade());
1047 let active_project = this.local_participant.active_project.as_ref();
1048 if active_project.map_or(false, |location| *location == project) {
1049 this.set_location(Some(&project), cx)
1050 } else {
1051 Task::ready(Ok(()))
1052 }
1053 })
1054 .await?;
1055
1056 Ok(response.project_id)
1057 })
1058 }
1059
1060 pub(crate) fn unshare_project(
1061 &mut self,
1062 project: ModelHandle<Project>,
1063 cx: &mut ModelContext<Self>,
1064 ) -> Result<()> {
1065 let project_id = match project.read(cx).remote_id() {
1066 Some(project_id) => project_id,
1067 None => return Ok(()),
1068 };
1069
1070 self.client.send(proto::UnshareProject { project_id })?;
1071 project.update(cx, |this, cx| this.unshare(cx))
1072 }
1073
1074 pub(crate) fn set_location(
1075 &mut self,
1076 project: Option<&ModelHandle<Project>>,
1077 cx: &mut ModelContext<Self>,
1078 ) -> Task<Result<()>> {
1079 if self.status.is_offline() {
1080 return Task::ready(Err(anyhow!("room is offline")));
1081 }
1082
1083 let client = self.client.clone();
1084 let room_id = self.id;
1085 let location = if let Some(project) = project {
1086 self.local_participant.active_project = Some(project.downgrade());
1087 if let Some(project_id) = project.read(cx).remote_id() {
1088 proto::participant_location::Variant::SharedProject(
1089 proto::participant_location::SharedProject { id: project_id },
1090 )
1091 } else {
1092 proto::participant_location::Variant::UnsharedProject(
1093 proto::participant_location::UnsharedProject {},
1094 )
1095 }
1096 } else {
1097 self.local_participant.active_project = None;
1098 proto::participant_location::Variant::External(proto::participant_location::External {})
1099 };
1100
1101 cx.notify();
1102 cx.foreground().spawn(async move {
1103 client
1104 .request(proto::UpdateParticipantLocation {
1105 room_id,
1106 location: Some(proto::ParticipantLocation {
1107 variant: Some(location),
1108 }),
1109 })
1110 .await?;
1111 Ok(())
1112 })
1113 }
1114
1115 pub fn is_screen_sharing(&self) -> bool {
1116 self.live_kit.as_ref().map_or(false, |live_kit| {
1117 !matches!(live_kit.screen_track, LocalTrack::None)
1118 })
1119 }
1120
1121 pub fn is_sharing_mic(&self) -> bool {
1122 self.live_kit.as_ref().map_or(false, |live_kit| {
1123 !matches!(live_kit.microphone_track, LocalTrack::None)
1124 })
1125 }
1126
1127 pub fn is_muted(&self, cx: &AppContext) -> bool {
1128 self.live_kit
1129 .as_ref()
1130 .and_then(|live_kit| match &live_kit.microphone_track {
1131 LocalTrack::None => Some(Self::mute_on_join(cx)),
1132 LocalTrack::Pending { muted, .. } => Some(*muted),
1133 LocalTrack::Published { muted, .. } => Some(*muted),
1134 })
1135 .unwrap_or(false)
1136 }
1137
1138 pub fn is_speaking(&self) -> bool {
1139 self.live_kit
1140 .as_ref()
1141 .map_or(false, |live_kit| live_kit.speaking)
1142 }
1143
1144 pub fn is_deafened(&self) -> Option<bool> {
1145 self.live_kit.as_ref().map(|live_kit| live_kit.deafened)
1146 }
1147
1148 #[track_caller]
1149 pub fn share_microphone(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
1150 if self.status.is_offline() {
1151 return Task::ready(Err(anyhow!("room is offline")));
1152 } else if self.is_sharing_mic() {
1153 return Task::ready(Err(anyhow!("microphone was already shared")));
1154 }
1155
1156 let publish_id = if let Some(live_kit) = self.live_kit.as_mut() {
1157 let publish_id = post_inc(&mut live_kit.next_publish_id);
1158 live_kit.microphone_track = LocalTrack::Pending {
1159 publish_id,
1160 muted: false,
1161 };
1162 cx.notify();
1163 publish_id
1164 } else {
1165 return Task::ready(Err(anyhow!("live-kit was not initialized")));
1166 };
1167
1168 cx.spawn_weak(|this, mut cx| async move {
1169 let publish_track = async {
1170 let track = LocalAudioTrack::create();
1171 this.upgrade(&cx)
1172 .ok_or_else(|| anyhow!("room was dropped"))?
1173 .read_with(&cx, |this, _| {
1174 this.live_kit
1175 .as_ref()
1176 .map(|live_kit| live_kit.room.publish_audio_track(&track))
1177 })
1178 .ok_or_else(|| anyhow!("live-kit was not initialized"))?
1179 .await
1180 };
1181
1182 let publication = publish_track.await;
1183 this.upgrade(&cx)
1184 .ok_or_else(|| anyhow!("room was dropped"))?
1185 .update(&mut cx, |this, cx| {
1186 let live_kit = this
1187 .live_kit
1188 .as_mut()
1189 .ok_or_else(|| anyhow!("live-kit was not initialized"))?;
1190
1191 let (canceled, muted) = if let LocalTrack::Pending {
1192 publish_id: cur_publish_id,
1193 muted,
1194 } = &live_kit.microphone_track
1195 {
1196 (*cur_publish_id != publish_id, *muted)
1197 } else {
1198 (true, false)
1199 };
1200
1201 match publication {
1202 Ok(publication) => {
1203 if canceled {
1204 live_kit.room.unpublish_track(publication);
1205 } else {
1206 if muted {
1207 cx.background().spawn(publication.set_mute(muted)).detach();
1208 }
1209 live_kit.microphone_track = LocalTrack::Published {
1210 track_publication: publication,
1211 muted,
1212 };
1213 cx.notify();
1214 }
1215 Ok(())
1216 }
1217 Err(error) => {
1218 if canceled {
1219 Ok(())
1220 } else {
1221 live_kit.microphone_track = LocalTrack::None;
1222 cx.notify();
1223 Err(error)
1224 }
1225 }
1226 }
1227 })
1228 })
1229 }
1230
1231 pub fn share_screen(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
1232 if self.status.is_offline() {
1233 return Task::ready(Err(anyhow!("room is offline")));
1234 } else if self.is_screen_sharing() {
1235 return Task::ready(Err(anyhow!("screen was already shared")));
1236 }
1237
1238 let (displays, publish_id) = if let Some(live_kit) = self.live_kit.as_mut() {
1239 let publish_id = post_inc(&mut live_kit.next_publish_id);
1240 live_kit.screen_track = LocalTrack::Pending {
1241 publish_id,
1242 muted: false,
1243 };
1244 cx.notify();
1245 (live_kit.room.display_sources(), publish_id)
1246 } else {
1247 return Task::ready(Err(anyhow!("live-kit was not initialized")));
1248 };
1249
1250 cx.spawn_weak(|this, mut cx| async move {
1251 let publish_track = async {
1252 let displays = displays.await?;
1253 let display = displays
1254 .first()
1255 .ok_or_else(|| anyhow!("no display found"))?;
1256 let track = LocalVideoTrack::screen_share_for_display(&display);
1257 this.upgrade(&cx)
1258 .ok_or_else(|| anyhow!("room was dropped"))?
1259 .read_with(&cx, |this, _| {
1260 this.live_kit
1261 .as_ref()
1262 .map(|live_kit| live_kit.room.publish_video_track(&track))
1263 })
1264 .ok_or_else(|| anyhow!("live-kit was not initialized"))?
1265 .await
1266 };
1267
1268 let publication = publish_track.await;
1269 this.upgrade(&cx)
1270 .ok_or_else(|| anyhow!("room was dropped"))?
1271 .update(&mut cx, |this, cx| {
1272 let live_kit = this
1273 .live_kit
1274 .as_mut()
1275 .ok_or_else(|| anyhow!("live-kit was not initialized"))?;
1276
1277 let (canceled, muted) = if let LocalTrack::Pending {
1278 publish_id: cur_publish_id,
1279 muted,
1280 } = &live_kit.screen_track
1281 {
1282 (*cur_publish_id != publish_id, *muted)
1283 } else {
1284 (true, false)
1285 };
1286
1287 match publication {
1288 Ok(publication) => {
1289 if canceled {
1290 live_kit.room.unpublish_track(publication);
1291 } else {
1292 if muted {
1293 cx.background().spawn(publication.set_mute(muted)).detach();
1294 }
1295 live_kit.screen_track = LocalTrack::Published {
1296 track_publication: publication,
1297 muted,
1298 };
1299 cx.notify();
1300 }
1301
1302 Audio::play_sound(Sound::StartScreenshare, cx);
1303
1304 Ok(())
1305 }
1306 Err(error) => {
1307 if canceled {
1308 Ok(())
1309 } else {
1310 live_kit.screen_track = LocalTrack::None;
1311 cx.notify();
1312 Err(error)
1313 }
1314 }
1315 }
1316 })
1317 })
1318 }
1319
1320 pub fn toggle_mute(&mut self, cx: &mut ModelContext<Self>) -> Result<Task<Result<()>>> {
1321 let should_mute = !self.is_muted(cx);
1322 if let Some(live_kit) = self.live_kit.as_mut() {
1323 if matches!(live_kit.microphone_track, LocalTrack::None) {
1324 return Ok(self.share_microphone(cx));
1325 }
1326
1327 let (ret_task, old_muted) = live_kit.set_mute(should_mute, cx)?;
1328 live_kit.muted_by_user = should_mute;
1329
1330 if old_muted == true && live_kit.deafened == true {
1331 if let Some(task) = self.toggle_deafen(cx).ok() {
1332 task.detach();
1333 }
1334 }
1335
1336 Ok(ret_task)
1337 } else {
1338 Err(anyhow!("LiveKit not started"))
1339 }
1340 }
1341
1342 pub fn toggle_deafen(&mut self, cx: &mut ModelContext<Self>) -> Result<Task<Result<()>>> {
1343 if let Some(live_kit) = self.live_kit.as_mut() {
1344 (*live_kit).deafened = !live_kit.deafened;
1345
1346 let mut tasks = Vec::with_capacity(self.remote_participants.len());
1347 // Context notification is sent within set_mute itself.
1348 let mut mute_task = None;
1349 // When deafening, mute user's mic as well.
1350 // When undeafening, unmute user's mic unless it was manually muted prior to deafening.
1351 if live_kit.deafened || !live_kit.muted_by_user {
1352 mute_task = Some(live_kit.set_mute(live_kit.deafened, cx)?.0);
1353 };
1354 for participant in self.remote_participants.values() {
1355 for track in live_kit
1356 .room
1357 .remote_audio_track_publications(&participant.user.id.to_string())
1358 {
1359 tasks.push(cx.foreground().spawn(track.set_enabled(!live_kit.deafened)));
1360 }
1361 }
1362
1363 Ok(cx.foreground().spawn(async move {
1364 if let Some(mute_task) = mute_task {
1365 mute_task.await?;
1366 }
1367 for task in tasks {
1368 task.await?;
1369 }
1370 Ok(())
1371 }))
1372 } else {
1373 Err(anyhow!("LiveKit not started"))
1374 }
1375 }
1376
1377 pub fn unshare_screen(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
1378 if self.status.is_offline() {
1379 return Err(anyhow!("room is offline"));
1380 }
1381
1382 let live_kit = self
1383 .live_kit
1384 .as_mut()
1385 .ok_or_else(|| anyhow!("live-kit was not initialized"))?;
1386 match mem::take(&mut live_kit.screen_track) {
1387 LocalTrack::None => Err(anyhow!("screen was not shared")),
1388 LocalTrack::Pending { .. } => {
1389 cx.notify();
1390 Ok(())
1391 }
1392 LocalTrack::Published {
1393 track_publication, ..
1394 } => {
1395 live_kit.room.unpublish_track(track_publication);
1396 cx.notify();
1397
1398 Audio::play_sound(Sound::StopScreenshare, cx);
1399 Ok(())
1400 }
1401 }
1402 }
1403
1404 #[cfg(any(test, feature = "test-support"))]
1405 pub fn set_display_sources(&self, sources: Vec<live_kit_client::MacOSDisplay>) {
1406 self.live_kit
1407 .as_ref()
1408 .unwrap()
1409 .room
1410 .set_display_sources(sources);
1411 }
1412}
1413
1414struct LiveKitRoom {
1415 room: Arc<live_kit_client::Room>,
1416 screen_track: LocalTrack,
1417 microphone_track: LocalTrack,
1418 /// Tracks whether we're currently in a muted state due to auto-mute from deafening or manual mute performed by user.
1419 muted_by_user: bool,
1420 deafened: bool,
1421 speaking: bool,
1422 next_publish_id: usize,
1423 _maintain_room: Task<()>,
1424 _maintain_tracks: [Task<()>; 2],
1425}
1426
1427impl LiveKitRoom {
1428 fn set_mute(
1429 self: &mut LiveKitRoom,
1430 should_mute: bool,
1431 cx: &mut ModelContext<Room>,
1432 ) -> Result<(Task<Result<()>>, bool)> {
1433 if !should_mute {
1434 // clear user muting state.
1435 self.muted_by_user = false;
1436 }
1437
1438 let (result, old_muted) = match &mut self.microphone_track {
1439 LocalTrack::None => Err(anyhow!("microphone was not shared")),
1440 LocalTrack::Pending { muted, .. } => {
1441 let old_muted = *muted;
1442 *muted = should_mute;
1443 cx.notify();
1444 Ok((Task::Ready(Some(Ok(()))), old_muted))
1445 }
1446 LocalTrack::Published {
1447 track_publication,
1448 muted,
1449 } => {
1450 let old_muted = *muted;
1451 *muted = should_mute;
1452 cx.notify();
1453 Ok((
1454 cx.background().spawn(track_publication.set_mute(*muted)),
1455 old_muted,
1456 ))
1457 }
1458 }?;
1459
1460 if old_muted != should_mute {
1461 if should_mute {
1462 Audio::play_sound(Sound::Mute, cx);
1463 } else {
1464 Audio::play_sound(Sound::Unmute, cx);
1465 }
1466 }
1467
1468 Ok((result, old_muted))
1469 }
1470}
1471
1472enum LocalTrack {
1473 None,
1474 Pending {
1475 publish_id: usize,
1476 muted: bool,
1477 },
1478 Published {
1479 track_publication: LocalTrackPublication,
1480 muted: bool,
1481 },
1482}
1483
1484impl Default for LocalTrack {
1485 fn default() -> Self {
1486 Self::None
1487 }
1488}
1489
1490#[derive(Copy, Clone, PartialEq, Eq)]
1491pub enum RoomStatus {
1492 Online,
1493 Rejoining,
1494 Offline,
1495}
1496
1497impl RoomStatus {
1498 pub fn is_offline(&self) -> bool {
1499 matches!(self, RoomStatus::Offline)
1500 }
1501
1502 pub fn is_online(&self) -> bool {
1503 matches!(self, RoomStatus::Online)
1504 }
1505}