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

@ -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!(),
}