From 0524abf11478b0a86610fd6c3b9a77eab1f99a50 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Tue, 15 Aug 2023 23:19:11 -0700 Subject: [PATCH] Lazily initialize and destroy the audio handle state on call initiation and end --- crates/audio/src/audio.rs | 40 ++++++++++++++++++++++++++------------- crates/call/src/call.rs | 2 ++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/crates/audio/src/audio.rs b/crates/audio/src/audio.rs index 233b0f62aa..d80fb6738f 100644 --- a/crates/audio/src/audio.rs +++ b/crates/audio/src/audio.rs @@ -39,29 +39,43 @@ pub struct Audio { impl Audio { pub fn new() -> Self { - let (_output_stream, output_handle) = OutputStream::try_default().log_err().unzip(); - Self { - _output_stream, - output_handle, + _output_stream: None, + output_handle: None, } } - pub fn play_sound(sound: Sound, cx: &AppContext) { + fn ensure_output_exists(&mut self) -> Option<&OutputStreamHandle> { + if self.output_handle.is_none() { + let (_output_stream, output_handle) = OutputStream::try_default().log_err().unzip(); + self.output_handle = output_handle; + self._output_stream = _output_stream; + } + + self.output_handle.as_ref() + } + + pub fn play_sound(sound: Sound, cx: &mut AppContext) { if !cx.has_global::() { return; } - let this = cx.global::(); + cx.update_global::(|this, cx| { + let output_handle = this.ensure_output_exists()?; + let source = SoundRegistry::global(cx).get(sound.file()).log_err()?; + output_handle.play_raw(source).log_err()?; + Some(()) + }); + } - let Some(output_handle) = this.output_handle.as_ref() else { + pub fn end_call(cx: &mut AppContext) { + if !cx.has_global::() { return; - }; + } - let Some(source) = SoundRegistry::global(cx).get(sound.file()).log_err() else { - return; - }; - - output_handle.play_raw(source).log_err(); + cx.update_global::(|this, _| { + this._output_stream.take(); + this.output_handle.take(); + }); } } diff --git a/crates/call/src/call.rs b/crates/call/src/call.rs index 33ba7a2ab9..3ac29bfc85 100644 --- a/crates/call/src/call.rs +++ b/crates/call/src/call.rs @@ -5,6 +5,7 @@ pub mod room; use std::sync::Arc; use anyhow::{anyhow, Result}; +use audio::Audio; use call_settings::CallSettings; use client::{ proto, ChannelId, ClickhouseEvent, Client, TelemetrySettings, TypedEnvelope, User, UserStore, @@ -309,6 +310,7 @@ impl ActiveCall { pub fn hang_up(&mut self, cx: &mut ModelContext) -> Task> { cx.notify(); self.report_call_event("hang up", cx); + Audio::end_call(cx); if let Some((room, _)) = self.room.take() { room.update(cx, |room, cx| room.leave(cx)) } else {