refactor: ♻️ play remote media stream error handling
This commit is contained in:
parent
7fcac42b7e
commit
d54c138607
3 changed files with 16 additions and 5 deletions
|
@ -74,7 +74,11 @@ pub fn app() -> impl IntoView {
|
||||||
let play_remote_audio_button = button()
|
let play_remote_audio_button = button()
|
||||||
.on(leptos::ev::click, move |_| {
|
.on(leptos::ev::click, move |_| {
|
||||||
let webrtc_audio = webrtc_audio.clone();
|
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");
|
.child("Play");
|
||||||
|
|
||||||
|
|
|
@ -62,17 +62,22 @@ pub async fn get_audio_stream() -> Result<MediaStream, Error> {
|
||||||
Ok(audio_stream)
|
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_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);
|
let audio_stream = audio_streams.get(0);
|
||||||
match audio_stream {
|
match audio_stream {
|
||||||
Some(audio_stream) => {
|
Some(audio_stream) => {
|
||||||
audio_element.set_src_object(Some(audio_stream));
|
audio_element.set_src_object(Some(audio_stream));
|
||||||
|
|
||||||
let audio_element_play_promise = audio_element.play().unwrap();
|
let audio_element_play_promise = audio_element
|
||||||
JsFuture::from(audio_element_play_promise).await.ok();
|
.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!(),
|
None => todo!(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ pub struct User {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
MediaPlay(String),
|
||||||
MediaStream(String),
|
MediaStream(String),
|
||||||
InvalidSignalType(String),
|
InvalidSignalType(String),
|
||||||
UnexpectedSignalType(SignalType),
|
UnexpectedSignalType(SignalType),
|
||||||
|
@ -40,6 +41,7 @@ impl std::error::Error for Error {
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
Error::MediaPlay(err_val) => write!(f, "Media Play | {}", err_val),
|
||||||
Error::MediaStream(err_val) => write!(f, "Media Stream | {}", err_val),
|
Error::MediaStream(err_val) => write!(f, "Media Stream | {}", err_val),
|
||||||
Error::InvalidSignalType(invalid_signal_type) => {
|
Error::InvalidSignalType(invalid_signal_type) => {
|
||||||
write!(f, "Invalid Signal Type: {}", invalid_signal_type)
|
write!(f, "Invalid Signal Type: {}", invalid_signal_type)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue