diff --git a/client/src/gui.rs b/client/src/gui.rs index 34a79f4..10ca792 100644 --- a/client/src/gui.rs +++ b/client/src/gui.rs @@ -74,7 +74,11 @@ pub fn app() -> impl IntoView { let play_remote_audio_button = button() .on(leptos::ev::click, move |_| { let webrtc_audio = webrtc_audio.clone(); - spawn_local(get_remote_audio_stream_and_play(webrtc_audio)); + spawn_local(async { + get_remote_audio_stream_and_play(webrtc_audio) + .await + .unwrap() + }); }) .child("Play"); diff --git a/client/src/media.rs b/client/src/media.rs index 42ea3e5..ea48e01 100644 --- a/client/src/media.rs +++ b/client/src/media.rs @@ -62,17 +62,22 @@ pub async fn get_audio_stream() -> Result { Ok(audio_stream) } -pub async fn get_remote_audio_stream_and_play(webrtc: Arc) { +pub async fn get_remote_audio_stream_and_play(webrtc: Arc) -> Result<(), Error> { let audio_element = HtmlAudioElement::new().unwrap(); - let audio_streams = webrtc.get_remote_streams().unwrap(); + let audio_streams = webrtc.get_remote_streams()?; let audio_stream = audio_streams.get(0); match audio_stream { Some(audio_stream) => { audio_element.set_src_object(Some(audio_stream)); - let audio_element_play_promise = audio_element.play().unwrap(); - JsFuture::from(audio_element_play_promise).await.ok(); + let audio_element_play_promise = audio_element + .play() + .map_err(|err_val| Error::MediaPlay(format!("{:?}", err_val)))?; + JsFuture::from(audio_element_play_promise) + .await + .map_err(|err_val| Error::MediaStream(format!("{:?}", err_val)))?; + Ok(()) } None => todo!(), } diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index a8bd41c..57a3b47 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -10,6 +10,7 @@ pub struct User { #[derive(Debug, Clone, Serialize, Deserialize)] pub enum Error { + MediaPlay(String), MediaStream(String), InvalidSignalType(String), UnexpectedSignalType(SignalType), @@ -40,6 +41,7 @@ impl std::error::Error for Error { impl Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + Error::MediaPlay(err_val) => write!(f, "Media Play | {}", err_val), Error::MediaStream(err_val) => write!(f, "Media Stream | {}", err_val), Error::InvalidSignalType(invalid_signal_type) => { write!(f, "Invalid Signal Type: {}", invalid_signal_type)