room.rs

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