refactor: ♻️ play remote media stream
This commit is contained in:
parent
52615c3d27
commit
7fcac42b7e
2 changed files with 27 additions and 32 deletions
|
@ -5,18 +5,17 @@ use leptos::{
|
|||
prelude::{OnAttribute, Read, Show, ShowProps, ToChildren},
|
||||
server::LocalResource,
|
||||
};
|
||||
use wasm_bindgen_futures::{JsFuture, spawn_local};
|
||||
use web_sys::{HtmlAudioElement, MediaStreamTrack};
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
|
||||
use crate::{
|
||||
media,
|
||||
media::{self, get_remote_audio_stream_and_play},
|
||||
signal::{send_auth, wait_until_communication_is_ready},
|
||||
sleep,
|
||||
webrtc::WebRTC,
|
||||
};
|
||||
|
||||
pub fn app() -> impl IntoView {
|
||||
let audio_stream = LocalResource::new(|| media::audio());
|
||||
let audio_stream = LocalResource::new(|| media::get_audio_stream());
|
||||
let wait_until_communication_is_ready =
|
||||
LocalResource::new(|| wait_until_communication_is_ready());
|
||||
|
||||
|
@ -74,32 +73,8 @@ pub fn app() -> impl IntoView {
|
|||
let webrtc_audio = webrtc.clone();
|
||||
let play_remote_audio_button = button()
|
||||
.on(leptos::ev::click, move |_| {
|
||||
let audio_element = HtmlAudioElement::new().unwrap();
|
||||
|
||||
let audio_streams = webrtc_audio.get_remote_streams().unwrap();
|
||||
log!("Streams = {:#?}", audio_streams);
|
||||
let audio_stream = audio_streams.get(0);
|
||||
match audio_stream {
|
||||
Some(audio_stream) => {
|
||||
audio_element.set_src_object(Some(audio_stream));
|
||||
let audio_tracks = audio_stream
|
||||
.get_audio_tracks()
|
||||
.iter()
|
||||
.map(|audio_track| MediaStreamTrack::from(audio_track))
|
||||
.collect::<Vec<_>>();
|
||||
log!("How many tracks = {}", audio_tracks.len());
|
||||
log!(
|
||||
"Constraints = {:#?}",
|
||||
audio_tracks.first().unwrap().get_constraints()
|
||||
);
|
||||
|
||||
let audio_element_play_promise = audio_element.play().unwrap();
|
||||
spawn_local(async move {
|
||||
JsFuture::from(audio_element_play_promise).await.ok();
|
||||
});
|
||||
}
|
||||
None => todo!(),
|
||||
}
|
||||
let webrtc_audio = webrtc_audio.clone();
|
||||
spawn_local(get_remote_audio_stream_and_play(webrtc_audio));
|
||||
})
|
||||
.child("Play");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue