use derive_more::From;
use js_sys::Promise;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::future_to_promise;
use crate::{
api::{LocalMediaTrack, MediaDeviceDetails, MediaStreamSettings},
media,
};
use super::Error;
#[wasm_bindgen]
#[derive(Debug, From)]
pub struct MediaManagerHandle(media::MediaManagerHandle);
#[wasm_bindgen]
impl MediaManagerHandle {
pub fn enumerate_devices(&self) -> Promise {
let this = self.0.clone();
future_to_promise(async move {
this.enumerate_devices()
.await
.map(|devices| {
devices
.into_iter()
.fold(js_sys::Array::new(), |devices_info, info| {
_ = devices_info.push(&JsValue::from(
MediaDeviceDetails::from(info),
));
devices_info
})
.into()
})
.map_err(Error::from)
.map_err(Into::into)
})
}
pub fn init_local_tracks(&self, caps: &MediaStreamSettings) -> Promise {
let this = self.0.clone();
let caps = caps.clone();
future_to_promise(async move {
this.init_local_tracks(caps.into())
.await
.map(|tracks| {
tracks
.into_iter()
.map(|t| JsValue::from(LocalMediaTrack::from(t)))
.collect::<js_sys::Array>()
.into()
})
.map_err(Error::from)
.map_err(Into::into)
})
}
pub fn on_device_change(
&self,
cb: js_sys::Function,
) -> Result<(), JsValue> {
let this = self.0.clone();
this.on_device_change(cb.into())
.map_err(Error::from)
.map_err(Into::into)
}
}