From d5ba221ac7171d9e688e137fecdf3bd0ead2801b Mon Sep 17 00:00:00 2001 From: Stuart Woodbury Date: Thu, 2 Nov 2023 10:08:54 -0400 Subject: [PATCH] chore(blink): use new errors for play and pause --- .../src/host_media/audio/opus/sink/mod.rs | 24 ++++++++++--------- .../src/host_media/audio/opus/source/mod.rs | 21 +++++++++------- warp/src/error.rs | 4 +++- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/extensions/warp-blink-wrtc/src/host_media/audio/opus/sink/mod.rs b/extensions/warp-blink-wrtc/src/host_media/audio/opus/sink/mod.rs index c9bc83da2..66f046baf 100644 --- a/extensions/warp-blink-wrtc/src/host_media/audio/opus/sink/mod.rs +++ b/extensions/warp-blink-wrtc/src/host_media/audio/opus/sink/mod.rs @@ -188,17 +188,18 @@ impl SinkTrack for OpusSink { fn play(&self) -> Result<(), Error> { *self.muted.write() = false; - self.stream - .play() - .map_err(|x| Error::OtherWithContext(x.to_string()))?; - Ok(()) + self.stream.play().map_err(|err| match err { + cpal::PlayStreamError::DeviceNotAvailable => Error::AudioDeviceDisconnected, + _ => Error::OtherWithContext(err.to_string()), + }) } + fn pause(&self) -> Result<(), Error> { *self.muted.write() = true; - self.stream - .pause() - .map_err(|x| Error::OtherWithContext(x.to_string()))?; - Ok(()) + self.stream.pause().map_err(|err| match err { + cpal::PauseStreamError::DeviceNotAvailable => Error::AudioDeviceDisconnected, + _ => Error::OtherWithContext(err.to_string()), + }) } fn change_output_device( &mut self, @@ -219,9 +220,10 @@ impl SinkTrack for OpusSink { )?; *self = new_sink; if !*self.muted.read() { - self.stream - .play() - .map_err(|x| Error::OtherWithContext(x.to_string()))?; + self.stream.play().map_err(|err| match err { + cpal::PlayStreamError::DeviceNotAvailable => Error::AudioDeviceDisconnected, + _ => Error::OtherWithContext(err.to_string()), + })?; } Ok(()) diff --git a/extensions/warp-blink-wrtc/src/host_media/audio/opus/source/mod.rs b/extensions/warp-blink-wrtc/src/host_media/audio/opus/source/mod.rs index 9c006f0fe..00f65b6b0 100644 --- a/extensions/warp-blink-wrtc/src/host_media/audio/opus/source/mod.rs +++ b/extensions/warp-blink-wrtc/src/host_media/audio/opus/source/mod.rs @@ -83,16 +83,18 @@ impl SourceTrack for OpusSource { } fn play(&self) -> Result<(), Error> { - self.stream - .play() - .map_err(|x| Error::OtherWithContext(x.to_string()))?; + self.stream.play().map_err(|err| match err { + cpal::PlayStreamError::DeviceNotAvailable => Error::AudioDeviceDisconnected, + _ => Error::OtherWithContext(err.to_string()), + })?; *self.muted.write() = false; Ok(()) } fn pause(&self) -> Result<(), Error> { - self.stream - .pause() - .map_err(|x| Error::OtherWithContext(x.to_string()))?; + self.stream.pause().map_err(|err| match err { + cpal::PauseStreamError::DeviceNotAvailable => Error::AudioDeviceDisconnected, + _ => Error::OtherWithContext(err.to_string()), + })?; *self.muted.write() = true; Ok(()) } @@ -118,9 +120,10 @@ impl SourceTrack for OpusSource { self.stream = stream; self.packetizer_handle = handle; if !*self.muted.read() { - self.stream - .play() - .map_err(|x| Error::OtherWithContext(x.to_string()))?; + self.stream.play().map_err(|err| match err { + cpal::PlayStreamError::DeviceNotAvailable => Error::AudioDeviceDisconnected, + _ => Error::OtherWithContext(err.to_string()), + })?; } Ok(()) } diff --git a/warp/src/error.rs b/warp/src/error.rs index 9259322e0..0158aafad 100644 --- a/warp/src/error.rs +++ b/warp/src/error.rs @@ -215,8 +215,10 @@ pub enum Error { InvalidDataType, //Blink Errors - #[error("Device not found")] + #[error("Audio device not found")] AudioDeviceNotFound, + #[error("AudioDeviceDisconnected")] + AudioDeviceDisconnected, // indicates a problem enumerating audio I/O devices #[error("AudioHostError: {_0}")] AudioHostError(String),