feat: webrtc gui interaction

This commit is contained in:
Ahmet Kaan Gümüş 2025-04-25 04:54:41 +03:00
parent cde03367c3
commit 8391ef31ba
2 changed files with 66 additions and 64 deletions

View file

@ -1,40 +1,41 @@
use leptos::{
IntoView, ev,
html::{ElementChild, button},
logging::log,
prelude::{OnAttribute, Read, Show, ShowProps, ToChildren},
server::LocalResource,
task::spawn_local,
};
use wasm_bindgen_futures::JsFuture;
use web_sys::HtmlAudioElement;
use crate::media::audio;
use crate::{media::audio, webrtc::WebRTC};
pub fn app() -> impl IntoView {
let audio_stream = LocalResource::new(|| audio());
let props = ShowProps::builder()
let offer_props = ShowProps::builder()
.when(move || audio_stream.read().is_some())
.children(ToChildren::to_children(move || {
let audio_element = HtmlAudioElement::new().unwrap();
let audio_stream = audio_stream.read();
let audio_stream = audio_stream.as_deref();
audio_element.set_src_object(audio_stream);
button()
.on(ev::click, move |_| match audio_element.play() {
Ok(audio_element_play_promise) => {
log!("{}", "Play will");
spawn_local(async move {
JsFuture::from(audio_element_play_promise).await.ok();
});
log!("{}", "Play must");
}
Err(err_val) => log!("{:#?}", err_val),
.on(ev::click, move |_| {
WebRTC::init(Some(audio_stream), None, None);
LocalResource::new(|| WebRTC::offer());
})
.child("Happy Button")
.child("Offer")
.into_view()
}))
.fallback(|| button().child("Sad Button"))
.fallback(|| button().child("Sad Offer Button"))
.build();
Show(props)
let answer_props = ShowProps::builder()
.when(move || audio_stream.read().is_some())
.children(ToChildren::to_children(move || {
button()
.on(ev::click, move |_| {
WebRTC::init(Some(audio_stream), None, None);
LocalResource::new(|| WebRTC::answer());
})
.child("Answer")
.into_view()
}))
.fallback(|| button().child("Sad Answer Button"))
.build();
(Show(offer_props), Show(answer_props))
}