1use crate::{ConstraintRangeBuilder, IntoDeviceIds, MaybeRwSignal};
2use default_struct_builder::DefaultBuilder;
3use js_sys::{Object, Reflect};
4use wasm_bindgen::JsValue;
5
6#[derive(DefaultBuilder, Clone, Debug)]
12pub struct UseUserMediaOptions {
13 pub(crate) enabled: MaybeRwSignal<bool>,
15 #[builder(into)]
18 pub(crate) video: VideoConstraints,
19 #[builder(into)]
22 pub(crate) audio: AudioConstraints,
23}
24
25impl Default for UseUserMediaOptions {
26 fn default() -> Self {
27 Self {
28 enabled: false.into(),
29 video: true.into(),
30 audio: false.into(),
31 }
32 }
33}
34
35#[derive(Clone, Debug)]
36pub enum ConstraintExactIdeal<T> {
37 Single(Option<T>),
38 ExactIdeal { exact: Option<T>, ideal: Option<T> },
39}
40
41impl<T> Default for ConstraintExactIdeal<T>
42where
43 T: Default,
44{
45 fn default() -> Self {
46 ConstraintExactIdeal::Single(Some(T::default()))
47 }
48}
49
50impl<T> ConstraintExactIdeal<T> {
51 pub fn exact(mut self, value: T) -> Self {
52 if let ConstraintExactIdeal::ExactIdeal { exact: e, .. } = &mut self {
53 *e = Some(value);
54 }
55
56 self
57 }
58
59 pub fn ideal(mut self, value: T) -> Self {
60 if let ConstraintExactIdeal::ExactIdeal { ideal: i, .. } = &mut self {
61 *i = Some(value);
62 }
63
64 self
65 }
66}
67
68impl<T> ConstraintExactIdeal<T>
69where
70 T: Into<JsValue> + Clone,
71{
72 pub fn to_jsvalue(&self) -> JsValue {
73 match self {
74 ConstraintExactIdeal::Single(value) => value.clone().unwrap().into(),
75 ConstraintExactIdeal::ExactIdeal { exact, ideal } => {
76 let obj = Object::new();
77
78 if let Some(value) = exact {
79 Reflect::set(&obj, &JsValue::from_str("exact"), &value.clone().into()).unwrap();
80 }
81 if let Some(value) = ideal {
82 Reflect::set(&obj, &JsValue::from_str("ideal"), &value.clone().into()).unwrap();
83 }
84
85 JsValue::from(obj)
86 }
87 }
88 }
89}
90
91impl From<&'static str> for ConstraintExactIdeal<&'static str> {
92 fn from(value: &'static str) -> Self {
93 ConstraintExactIdeal::Single(Some(value))
94 }
95}
96
97#[derive(Clone, Debug)]
98pub enum ConstraintBoolOrRange<T> {
99 Bool(bool),
100 Single(Option<T>),
101 Range {
102 min: Option<T>,
103 max: Option<T>,
104 exact: Option<T>,
105 ideal: Option<T>,
106 },
107}
108
109impl<T> ConstraintBoolOrRange<T>
110where
111 T: Into<JsValue> + Clone,
112{
113 pub fn to_jsvalue(&self) -> JsValue {
114 match self {
115 Self::Bool(value) => JsValue::from_bool(*value),
116 Self::Single(value) => value.clone().unwrap().into(),
117 Self::Range {
118 min,
119 max,
120 exact,
121 ideal,
122 } => {
123 let obj = Object::new();
124
125 if let Some(min_value) = min {
126 Reflect::set(&obj, &JsValue::from_str("min"), &min_value.clone().into())
127 .unwrap();
128 }
129 if let Some(max_value) = max {
130 Reflect::set(&obj, &JsValue::from_str("max"), &max_value.clone().into())
131 .unwrap();
132 }
133 if let Some(value) = exact {
134 Reflect::set(&obj, &JsValue::from_str("exact"), &value.clone().into()).unwrap();
135 }
136 if let Some(value) = ideal {
137 Reflect::set(&obj, &JsValue::from_str("ideal"), &value.clone().into()).unwrap();
138 }
139
140 JsValue::from(obj)
141 }
142 }
143 }
144}
145
146impl<T: Default> Default for ConstraintBoolOrRange<T> {
147 fn default() -> Self {
148 ConstraintBoolOrRange::Single(Some(T::default()))
149 }
150}
151
152impl<T> From<bool> for ConstraintBoolOrRange<T> {
153 fn from(value: bool) -> Self {
154 ConstraintBoolOrRange::Bool(value)
155 }
156}
157
158impl<T> From<ConstraintRange<T>> for ConstraintBoolOrRange<T> {
159 fn from(value: ConstraintRange<T>) -> Self {
160 match value {
161 ConstraintRange::Single(value) => ConstraintBoolOrRange::Single(value),
162 ConstraintRange::Range {
163 min,
164 max,
165 exact,
166 ideal,
167 } => ConstraintBoolOrRange::Range {
168 min,
169 max,
170 exact,
171 ideal,
172 },
173 }
174 }
175}
176
177impl From<f64> for ConstraintBoolOrRange<f64> {
178 fn from(value: f64) -> Self {
179 Self::Single(Some(value))
180 }
181}
182
183impl From<u32> for ConstraintBoolOrRange<u32> {
184 fn from(value: u32) -> Self {
185 Self::Single(Some(value))
186 }
187}
188
189#[derive(Clone, Debug)]
190pub enum ConstraintRange<T> {
191 Single(Option<T>),
192 Range {
193 min: Option<T>,
194 max: Option<T>,
195 exact: Option<T>,
196 ideal: Option<T>,
197 },
198}
199
200impl<T> Default for ConstraintRange<T>
201where
202 T: Default,
203{
204 fn default() -> Self {
205 ConstraintRange::Single(Some(T::default()))
206 }
207}
208
209impl<T> ConstraintRange<T>
210where
211 T: Clone + std::fmt::Debug,
212{
213 pub fn new(value: Option<T>) -> Self {
214 ConstraintRange::Single(value)
215 }
216}
217
218macro_rules! impl_constraint_range_builder {
219 ($ty:ty) => {
220 impl<T> ConstraintRangeBuilder<T> for $ty {
221 fn min(mut self, value: T) -> Self {
222 if let Self::Range { ref mut min, .. } = self {
223 *min = Some(value);
224 }
225 self
226 }
227
228 fn max(mut self, value: T) -> Self {
229 if let Self::Range { ref mut max, .. } = self {
230 *max = Some(value);
231 }
232 self
233 }
234
235 fn exact(mut self, value: T) -> Self {
236 if let Self::Range { exact, .. } = &mut self {
237 *exact = Some(value);
238 }
239
240 self
241 }
242
243 fn ideal(mut self, value: T) -> Self {
244 if let Self::Range { ideal, .. } = &mut self {
245 *ideal = Some(value);
246 }
247
248 self
249 }
250 }
251 };
252}
253
254impl_constraint_range_builder!(ConstraintRange<T>);
255
256impl_constraint_range_builder!(ConstraintBoolOrRange<T>);
257
258impl<T> ConstraintRange<T>
259where
260 T: Into<JsValue> + Clone,
261{
262 pub fn to_jsvalue(&self) -> JsValue {
263 match self {
264 ConstraintRange::Single(value) => value.clone().unwrap().into(),
265 ConstraintRange::Range {
266 min,
267 max,
268 exact,
269 ideal,
270 } => {
271 let obj = Object::new();
272
273 if let Some(min_value) = min {
274 Reflect::set(&obj, &JsValue::from_str("min"), &min_value.clone().into())
275 .unwrap();
276 }
277 if let Some(max_value) = max {
278 Reflect::set(&obj, &JsValue::from_str("max"), &max_value.clone().into())
279 .unwrap();
280 }
281 if let Some(value) = exact {
282 Reflect::set(&obj, &JsValue::from_str("exact"), &value.clone().into()).unwrap();
283 }
284 if let Some(value) = ideal {
285 Reflect::set(&obj, &JsValue::from_str("ideal"), &value.clone().into()).unwrap();
286 }
287
288 JsValue::from(obj)
289 }
290 }
291 }
292}
293
294impl From<f64> for ConstraintDouble {
295 fn from(value: f64) -> Self {
296 ConstraintRange::Single(Some(value))
297 }
298}
299
300impl From<u32> for ConstraintULong {
301 fn from(value: u32) -> Self {
302 ConstraintRange::Single(Some(value))
303 }
304}
305
306pub type ConstraintBool = ConstraintExactIdeal<bool>;
307
308impl From<bool> for ConstraintBool {
309 fn from(value: bool) -> Self {
310 ConstraintExactIdeal::Single(Some(value))
311 }
312}
313
314pub type ConstraintDouble = ConstraintRange<f64>;
315
316pub type ConstraintULong = ConstraintRange<u32>;
317
318#[derive(Clone, Copy, Debug)]
319pub enum FacingMode {
320 User,
321 Environment,
322 Left,
323 Right,
324}
325
326impl FacingMode {
327 pub fn as_str(self) -> &'static str {
328 match self {
329 FacingMode::User => "user",
330 FacingMode::Environment => "environment",
331 FacingMode::Left => "left",
332 FacingMode::Right => "right",
333 }
334 }
335}
336
337pub type ConstraintFacingMode = ConstraintExactIdeal<FacingMode>;
338
339impl From<FacingMode> for ConstraintFacingMode {
340 fn from(value: FacingMode) -> Self {
341 ConstraintFacingMode::Single(Some(value))
342 }
343}
344
345impl ConstraintFacingMode {
346 pub fn to_jsvalue(&self) -> JsValue {
347 match self {
348 ConstraintExactIdeal::Single(value) => JsValue::from_str((*value).unwrap().as_str()),
349 ConstraintExactIdeal::ExactIdeal { exact, ideal } => {
350 let obj = Object::new();
351
352 if let Some(value) = exact {
353 Reflect::set(
354 &obj,
355 &JsValue::from_str("exact"),
356 &JsValue::from_str(value.as_str()),
357 )
358 .unwrap();
359 }
360 if let Some(value) = ideal {
361 Reflect::set(
362 &obj,
363 &JsValue::from_str("ideal"),
364 &JsValue::from_str(value.as_str()),
365 )
366 .unwrap();
367 }
368
369 JsValue::from(obj)
370 }
371 }
372 }
373}
374
375#[derive(Clone, Debug)]
376pub enum AudioConstraints {
377 Bool(bool),
378 Constraints(Box<AudioTrackConstraints>),
379}
380
381impl From<bool> for AudioConstraints {
382 fn from(value: bool) -> Self {
383 AudioConstraints::Bool(value)
384 }
385}
386
387impl From<AudioTrackConstraints> for AudioConstraints {
388 fn from(value: AudioTrackConstraints) -> Self {
389 AudioConstraints::Constraints(Box::new(value))
390 }
391}
392
393#[derive(Clone, Debug)]
394pub enum VideoConstraints {
395 Bool(bool),
396 Constraints(Box<VideoTrackConstraints>),
397}
398
399impl From<bool> for VideoConstraints {
400 fn from(value: bool) -> Self {
401 VideoConstraints::Bool(value)
402 }
403}
404
405impl From<VideoTrackConstraints> for VideoConstraints {
406 fn from(value: VideoTrackConstraints) -> Self {
407 VideoConstraints::Constraints(Box::new(value))
408 }
409}
410
411#[derive(DefaultBuilder, Default, Clone, Debug)]
412#[allow(dead_code)]
413pub struct AudioTrackConstraints {
414 #[builder(skip)]
415 pub(crate) device_id: Vec<String>,
416
417 #[builder(into)]
418 pub(crate) auto_gain_control: Option<ConstraintBool>,
419 #[builder(into)]
420 pub(crate) channel_count: Option<ConstraintULong>,
421 #[builder(into)]
422 pub(crate) echo_cancellation: Option<ConstraintBool>,
423 #[builder(into)]
424 pub(crate) noise_suppression: Option<ConstraintBool>,
425}
426
427impl AudioTrackConstraints {
428 pub fn new() -> Self {
429 AudioTrackConstraints::default()
430 }
431
432 pub fn device_id<M>(mut self, value: impl IntoDeviceIds<M>) -> Self {
433 self.device_id = value.into_device_ids();
434 self
435 }
436}
437
438#[derive(DefaultBuilder, Default, Clone, Debug)]
439pub struct VideoTrackConstraints {
440 #[builder(skip)]
441 pub device_id: Vec<String>,
442
443 #[builder(into)]
444 pub facing_mode: Option<ConstraintFacingMode>,
445 #[builder(into)]
446 pub frame_rate: Option<ConstraintDouble>,
447 #[builder(into)]
448 pub height: Option<ConstraintULong>,
449 #[builder(into)]
450 pub width: Option<ConstraintULong>,
451 #[builder(into)]
452 pub viewport_offset_x: Option<ConstraintULong>,
453 #[builder(into)]
454 pub viewport_offset_y: Option<ConstraintULong>,
455 #[builder(into)]
456 pub viewport_height: Option<ConstraintULong>,
457 #[builder(into)]
458 pub viewport_width: Option<ConstraintULong>,
459 #[builder(into)]
460 pub zoom: Option<ConstraintBoolOrRange<f64>>,
461}
462
463impl VideoTrackConstraints {
464 pub fn new() -> Self {
465 VideoTrackConstraints::default()
466 }
467
468 pub fn device_id<M>(mut self, value: impl IntoDeviceIds<M>) -> Self {
469 self.device_id = value.into_device_ids();
470 self
471 }
472}