mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-24 19:10:24 +00:00
Lazily initialize and destroy the audio handle state on call initiation and end
This commit is contained in:
parent
706227701e
commit
0524abf114
2 changed files with 29 additions and 13 deletions
|
@ -39,29 +39,43 @@ pub struct Audio {
|
||||||
|
|
||||||
impl Audio {
|
impl Audio {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let (_output_stream, output_handle) = OutputStream::try_default().log_err().unzip();
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
_output_stream,
|
_output_stream: None,
|
||||||
output_handle,
|
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::<Self>() {
|
if !cx.has_global::<Self>() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let this = cx.global::<Self>();
|
cx.update_global::<Self, _, _>(|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::<Self>() {
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
let Some(source) = SoundRegistry::global(cx).get(sound.file()).log_err() else {
|
cx.update_global::<Self, _, _>(|this, _| {
|
||||||
return;
|
this._output_stream.take();
|
||||||
};
|
this.output_handle.take();
|
||||||
|
});
|
||||||
output_handle.play_raw(source).log_err();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ pub mod room;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
use audio::Audio;
|
||||||
use call_settings::CallSettings;
|
use call_settings::CallSettings;
|
||||||
use client::{
|
use client::{
|
||||||
proto, ChannelId, ClickhouseEvent, Client, TelemetrySettings, TypedEnvelope, User, UserStore,
|
proto, ChannelId, ClickhouseEvent, Client, TelemetrySettings, TypedEnvelope, User, UserStore,
|
||||||
|
@ -309,6 +310,7 @@ impl ActiveCall {
|
||||||
pub fn hang_up(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
|
pub fn hang_up(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
|
||||||
cx.notify();
|
cx.notify();
|
||||||
self.report_call_event("hang up", cx);
|
self.report_call_event("hang up", cx);
|
||||||
|
Audio::end_call(cx);
|
||||||
if let Some((room, _)) = self.room.take() {
|
if let Some((room, _)) = self.room.take() {
|
||||||
room.update(cx, |room, cx| room.leave(cx))
|
room.update(cx, |room, cx| room.leave(cx))
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue