room.rs

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