refactor: ♻️ play remote media stream

This commit is contained in:
Ahmet Kaan Gümüş 2025-05-03 18:26:17 +03:00
parent 52615c3d27
commit 7fcac42b7e
2 changed files with 27 additions and 32 deletions

View file

@ -1,13 +1,17 @@
use std::sync::Arc;
use leptos::logging::log;
use protocol::Error;
use wasm_bindgen_futures::JsFuture;
use web_sys::{
MediaStream, MediaStreamConstraints, MediaStreamTrack, MediaTrackConstraints,
HtmlAudioElement, MediaStream, MediaStreamConstraints, MediaStreamTrack, MediaTrackConstraints,
wasm_bindgen::{JsCast, JsValue},
window,
};
pub async fn audio() -> Result<MediaStream, Error> {
use crate::webrtc::WebRTC;
pub async fn get_audio_stream() -> Result<MediaStream, Error> {
let media_devices = window()
.ok_or(Error::MediaStream("Accessing Window".to_owned()))?
.navigator()
@ -57,3 +61,19 @@ pub async fn audio() -> Result<MediaStream, Error> {
Ok(audio_stream)
}
pub async fn get_remote_audio_stream_and_play(webrtc: Arc<WebRTC>) {
let audio_element = HtmlAudioElement::new().unwrap();
let audio_streams = webrtc.get_remote_streams().unwrap();
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();
}
None => todo!(),
}
}