1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! [MediaDevices][1] functionality.
//!
//! [1]: https://w3.org/TR/mediacapture-streams#mediadevices
#![allow(clippy::unwrap_used)]
use std::{cell::RefCell, rc::Rc};
use wasm_bindgen_futures::JsFuture;
use tracerr::Traced;
use web_sys::{Event, MediaDevices as SysMediaDevices};
use crate::{
media::{InvalidOutputAudioDeviceIdError, MediaSourceKind, MicVolumeError},
platform::{
utils::EventListener, DisplayMediaStreamConstraints, Error,
GetUserMediaError, MediaDeviceInfo, MediaDisplayInfo,
MediaStreamConstraints, MediaStreamTrack,
},
};
use super::window;
impl From<Error> for GetUserMediaError {
fn from(err: Error) -> Self {
let message = err.message().to_lowercase();
if message.contains("audio") {
Self::Audio(err)
} else if message.contains("video") {
Self::Video(err)
} else {
Self::Unknown(err)
}
}
}
/// Media devices controller.
#[derive(Debug)]
pub struct MediaDevices {
/// Underlying [`SysMediaDevices`], used for media devices management.
devices: Rc<SysMediaDevices>,
/// [`EventListener`] for the `devicechange` event of the underlying
/// [`SysMediaDevices`].
on_device_change_listener:
RefCell<Option<EventListener<SysMediaDevices, Event>>>,
}
impl Default for MediaDevices {
fn default() -> Self {
Self::new()
}
}
impl MediaDevices {
/// Returns new [`MediaDevices`].
///
/// # Panics
///
/// If failed to get [`SysMediaDevices`] from the browser.
#[must_use]
pub fn new() -> Self {
let devices = window()
.navigator()
.media_devices()
.map_err(Error::from)
.map_err(tracerr::wrap!())
.unwrap();
Self {
devices: Rc::new(devices),
on_device_change_listener: RefCell::new(None),
}
}
/// Collects information about the User Agent's available media input
/// devices.
///
/// Adapter for the [MediaDevices.enumerateDevices()][1] function.
///
/// # Errors
///
/// With [`Error`] if [MediaDevices.enumerateDevices()][1] returns error or
/// cannot get [MediaDevices][2].
///
/// # Panics
///
/// If [`js_sys::Array`] returned from [MediaDevices.enumerateDevices()][1]
/// contains something that is not [`web_sys::MediaDeviceInfo`].
///
/// [1]: https://tinyurl.com/w3-streams#dom-mediadevices-enumeratedevices
/// [2]: https://w3.org/TR/mediacapture-streams#mediadevices
pub async fn enumerate_devices(
&self,
) -> Result<Vec<MediaDeviceInfo>, Traced<Error>> {
let devices = JsFuture::from(
self.devices
.enumerate_devices()
.map_err(Error::from)
.map_err(tracerr::wrap!())?,
)
.await
.map_err(Error::from)
.map_err(tracerr::wrap!())?;
Ok(js_sys::Array::from(&devices)
.values()
.into_iter()
.map(|info| {
let info = web_sys::MediaDeviceInfo::from(info.unwrap());
MediaDeviceInfo::from(info)
})
.collect())
}
/// Unimplemented on WASM targets.
#[allow(clippy::missing_errors_doc)]
#[allow(clippy::unused_async)] // for platform code uniformity
pub async fn enumerate_displays(
&self,
) -> Result<Vec<MediaDisplayInfo>, Traced<Error>> {
unimplemented!()
}
/// Prompts a user for a permission to use a media input which produces
/// [`MediaStreamTrack`]s containing the requested types of media.
///
/// Adapter for the [MediaDevices.getUserMedia()][1] function.
///
/// # Errors
///
/// With [`Error`] if [MediaDevices.getUserMedia()][1] returns error or
/// cannot get [MediaDevices][2].
///
/// # Panics
///
/// If [`js_sys::Array`] returned from [MediaDevices.getUserMedia()][1]
/// contains something that is not [`web_sys::MediaStreamTrack`].
///
/// [1]: https://tinyurl.com/w3-streams#dom-mediadevices-getusermedia
/// [2]: https://w3.org/TR/mediacapture-streams#mediadevices
pub async fn get_user_media(
&self,
caps: MediaStreamConstraints,
) -> Result<Vec<MediaStreamTrack>, Traced<GetUserMediaError>> {
let stream = JsFuture::from(
self.devices
.get_user_media_with_constraints(&caps.into())
.map_err(Error::from)
.map_err(tracerr::from_and_wrap!())?,
)
.await
.map(web_sys::MediaStream::from)
.map_err(Error::from)
.map_err(tracerr::from_and_wrap!())?;
Ok(js_sys::try_iter(&stream.get_tracks())
.unwrap()
.unwrap()
.map(|tr| {
MediaStreamTrack::new(
tr.unwrap(),
Some(MediaSourceKind::Device),
)
})
.collect())
}
/// Prompts a user to select and grant a permission to capture contents of a
/// display or portion thereof (such as a single window) as vector of
/// [`MediaStreamTrack`]s.
///
/// Adapter for the [MediaDevices.getDisplayMedia()][1] function.
///
/// # Errors
///
/// With [`Error`] if [MediaDevices.getDisplayMedia()][1] returns error or
/// cannot get [MediaDevices][2].
///
/// # Panics
///
/// If [`js_sys::Array`] returned from [MediaDevices.getDisplayMedia()][1]
/// contains something that is not [`web_sys::MediaStreamTrack`].
///
/// [1]: https://w3.org/TR/screen-capture#dom-mediadevices-getdisplaymedia
/// [2]: https://w3.org/TR/mediacapture-streams#mediadevices
pub async fn get_display_media(
&self,
caps: DisplayMediaStreamConstraints,
) -> Result<Vec<MediaStreamTrack>, Traced<Error>> {
let media_devices = window()
.navigator()
.media_devices()
.map_err(Error::from)
.map_err(tracerr::wrap!())?;
let stream = JsFuture::from(
media_devices
.get_display_media_with_constraints(&caps.into())
.map_err(Error::from)
.map_err(tracerr::wrap!())?,
)
.await
.map(web_sys::MediaStream::from)
.map_err(Error::from)
.map_err(tracerr::wrap!())?;
Ok(js_sys::try_iter(&stream.get_tracks())
.unwrap()
.unwrap()
.map(|tr| {
MediaStreamTrack::new(
tr.unwrap(),
Some(MediaSourceKind::Display),
)
})
.collect())
}
/// This method should be unreachable, because this functional is
/// implemented on the Dart side of Jason only.
///
/// # Errors
///
/// Never.
///
/// # Panics
///
/// Always.
#[allow(clippy::unused_async)] // for platform code uniformity
pub async fn set_output_audio_id(
&self,
_: String,
) -> Result<(), Traced<InvalidOutputAudioDeviceIdError>> {
unreachable!(
"`set_output_audio_id()` is implemented on the Dart side, \
so this method call is unreachable",
)
}
/// Subscribes onto the [`MediaDevices`]'s `devicechange` event.
///
/// # Panics
///
/// If `devicechange` event listener binding fails.
pub fn on_device_change<F>(&self, f: Option<F>)
where
F: 'static + FnMut(),
{
if let Some(mut f) = f {
drop(
self.on_device_change_listener.borrow_mut().replace(
EventListener::new_mut(
Rc::clone(&self.devices),
"devicechange",
move |_| f(),
)
.unwrap(),
),
);
}
}
/// Always returns `false` since accessing microphone cannot be implemented
/// on web platform.
#[allow(clippy::unused_async)] // for platform code uniformity
pub async fn microphone_volume_is_available(&self) -> bool {
false
}
/// This method should be unreachable, because cannot be implemented on web
/// platform.
///
/// # Errors
///
/// Never.
///
/// # Panics
///
/// Always.
#[allow(clippy::unused_async)] // for platform code uniformity
pub async fn microphone_volume(
&self,
) -> Result<i64, Traced<MicVolumeError>> {
unreachable!(
"`microphone_volume()` cannot be implemented on web platform",
)
}
/// This method should be unreachable, because cannot be implemented on web
/// platform.
///
/// # Errors
///
/// Never.
///
/// # Panics
///
/// Always.
#[allow(clippy::unused_async)] // for platform code uniformity
pub async fn set_microphone_volume(
&self,
_: i64,
) -> Result<(), Traced<MicVolumeError>> {
unreachable!(
"`set_microphone_volume()` cannot be implemented on web platform",
)
}
}