Skip to content

Commit

Permalink
feat(blink): improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwoodbury committed Nov 2, 2023
1 parent 32ae7bb commit b83dec4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
22 changes: 17 additions & 5 deletions extensions/warp-blink-wrtc/src/host_media/audio/opus/sink/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cpal::{
traits::{DeviceTrait, StreamTrait},
SampleRate,
BuildStreamError, SampleRate,
};
use ringbuf::HeapRb;
use std::{cmp::Ordering, sync::Arc};
Expand Down Expand Up @@ -132,13 +132,25 @@ impl OpusSink {
output_data_fn,
move |err| {
log::error!("an error occurred on stream: {}", err);
if matches!(err, cpal::StreamError::DeviceNotAvailable) {
let _ = event_ch3.send(BlinkEventKind::AudioOutputDeviceNoLongerAvailable);
}
let evt = match err {
cpal::StreamError::DeviceNotAvailable => {
BlinkEventKind::AudioOutputDeviceNoLongerAvailable
}
_ => BlinkEventKind::AudioStreamError,
};
let _ = event_ch3.send(evt);
},
None,
)
.map_err(|x| Error::OtherWithContext(x.to_string()))?;
.map_err(|e| match e {
BuildStreamError::StreamConfigNotSupported => Error::InvalidAudioConfig,
BuildStreamError::DeviceNotAvailable => Error::AudioDeviceNotFound,
e => Error::OtherWithContext(format!(
"failed to build output stream: {e}, {}, {}",
file!(),
line!()
)),
})?;

Ok(Self {
peer_id,
Expand Down
20 changes: 13 additions & 7 deletions extensions/warp-blink-wrtc/src/host_media/audio/opus/source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cpal::{
traits::{DeviceTrait, StreamTrait},
SampleRate,
BuildStreamError, SampleRate,
};
use rand::Rng;
use ringbuf::HeapRb;
Expand Down Expand Up @@ -196,18 +196,24 @@ fn create_source_track(
input_data_fn,
move |err| {
log::error!("an error occurred on stream: {}", err);
if matches!(err, cpal::StreamError::DeviceNotAvailable) {
let _ = event_ch2.send(BlinkEventKind::AudioInputDeviceNoLongerAvailable);
}
let evt = match err {
cpal::StreamError::DeviceNotAvailable => {
BlinkEventKind::AudioInputDeviceNoLongerAvailable
}
_ => BlinkEventKind::AudioStreamError,
};
let _ = event_ch2.send(evt);
},
None,
)
.map_err(|e| {
anyhow::anyhow!(
.map_err(|e| match e {
BuildStreamError::StreamConfigNotSupported => Error::InvalidAudioConfig,
BuildStreamError::DeviceNotAvailable => Error::AudioDeviceNotFound,
e => Error::OtherWithContext(format!(
"failed to build input stream: {e}, {}, {}",
file!(),
line!()
)
)),
})?;

let join_handle = tokio::spawn(async move {
Expand Down
2 changes: 2 additions & 0 deletions warp/src/blink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ pub enum BlinkEventKind {
AudioOutputDeviceNoLongerAvailable,
#[display(fmt = "AudioInputDeviceNoLongerAvailable")]
AudioInputDeviceNoLongerAvailable,
#[display(fmt = "AudioStreamError")]
AudioStreamError,
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down
14 changes: 8 additions & 6 deletions warp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,23 @@ pub enum Error {
InvalidDataType,

//Blink Errors
#[error("Invalid MIME type: {_0}")]
InvalidMimeType(String),
#[error("Device not found")]
AudioDeviceNotFound,
// indicates a problem enumerating audio I/O devices
#[error("AudioHostError: {_0}")]
AudioHostError(String),
#[error("BlinkNotInitialized")]
BlinkNotInitialized,
#[error("CallNotInProgress")]
CallNotInProgress,
#[error("Invalid MIME type: {_0}")]
InvalidMimeType(String),
#[error("InvalidAudioConfig")]
InvalidAudioConfig,
#[error("MicrophoneMissing")]
MicrophoneMissing,
#[error("SpeakerMissing")]
SpeakerMissing,
#[error("CallNotInProgress")]
CallNotInProgress,
#[error("BlinkNotInitialized")]
BlinkNotInitialized,

//Misc
#[error("Length for '{context}' is invalid. Current length: {current}. Minimum Length: {minimum:?}, Maximum: {maximum:?}")]
Expand Down

0 comments on commit b83dec4

Please sign in to comment.