refactor: ♻️ play remote media stream error handling

This commit is contained in:
Ahmet Kaan Gümüş 2025-05-04 22:30:27 +03:00
parent 7fcac42b7e
commit d54c138607
3 changed files with 16 additions and 5 deletions

View file

@ -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");

View file

@ -62,17 +62,22 @@ pub async fn get_audio_stream() -> Result<MediaStream, Error> {
Ok(audio_stream)
}
pub async fn get_remote_audio_stream_and_play(webrtc: Arc<WebRTC>) {
pub async fn get_remote_audio_stream_and_play(webrtc: Arc<WebRTC>) -> 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!(),
}

View file

@ -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)