room.rs

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