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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::compartments::InCompartment;
use crate::dom::bindings::codegen::Bindings::MediaDevicesBinding::MediaStreamConstraints;
use crate::dom::bindings::codegen::Bindings::MediaDevicesBinding::{self, MediaDevicesMethods};
use crate::dom::bindings::codegen::UnionTypes::BooleanOrMediaTrackConstraints;
use crate::dom::bindings::codegen::UnionTypes::ClampedUnsignedLongOrConstrainULongRange as ConstrainULong;
use crate::dom::bindings::codegen::UnionTypes::DoubleOrConstrainDoubleRange as ConstrainDouble;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::DomRoot;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::mediastream::MediaStream;
use crate::dom::mediastreamtrack::MediaStreamTrack;
use crate::dom::promise::Promise;
use dom_struct::dom_struct;
use servo_media::streams::capture::{Constrain, ConstrainRange, MediaTrackConstraintSet};
use servo_media::streams::MediaStreamType;
use servo_media::ServoMedia;
use std::rc::Rc;
#[dom_struct]
pub struct MediaDevices {
eventtarget: EventTarget,
}
impl MediaDevices {
pub fn new_inherited() -> MediaDevices {
MediaDevices {
eventtarget: EventTarget::new_inherited(),
}
}
pub fn new(global: &GlobalScope) -> DomRoot<MediaDevices> {
reflect_dom_object(
Box::new(MediaDevices::new_inherited()),
global,
MediaDevicesBinding::Wrap,
)
}
}
impl MediaDevicesMethods for MediaDevices {
/// https://w3c.github.io/mediacapture-main/#dom-mediadevices-getusermedia
#[allow(unsafe_code)]
fn GetUserMedia(
&self,
constraints: &MediaStreamConstraints,
comp: InCompartment,
) -> Rc<Promise> {
let p = Promise::new_in_current_compartment(&self.global(), comp);
let media = ServoMedia::get().unwrap();
let stream = MediaStream::new(&self.global());
if let Some(constraints) = convert_constraints(&constraints.audio) {
if let Some(audio) = media.create_audioinput_stream(constraints) {
let track = MediaStreamTrack::new(&self.global(), audio, MediaStreamType::Audio);
stream.add_track(&track);
}
}
if let Some(constraints) = convert_constraints(&constraints.video) {
if let Some(video) = media.create_videoinput_stream(constraints) {
let track = MediaStreamTrack::new(&self.global(), video, MediaStreamType::Video);
stream.add_track(&track);
}
}
p.resolve_native(&stream);
p
}
}
fn convert_constraints(js: &BooleanOrMediaTrackConstraints) -> Option<MediaTrackConstraintSet> {
match js {
BooleanOrMediaTrackConstraints::Boolean(false) => None,
BooleanOrMediaTrackConstraints::Boolean(true) => Some(Default::default()),
BooleanOrMediaTrackConstraints::MediaTrackConstraints(ref c) => {
Some(MediaTrackConstraintSet {
height: c.parent.height.as_ref().and_then(convert_culong),
width: c.parent.width.as_ref().and_then(convert_culong),
aspect: c.parent.aspectRatio.as_ref().and_then(convert_cdouble),
frame_rate: c.parent.frameRate.as_ref().and_then(convert_cdouble),
sample_rate: c.parent.sampleRate.as_ref().and_then(convert_culong),
})
},
}
}
fn convert_culong(js: &ConstrainULong) -> Option<Constrain<u32>> {
match js {
ConstrainULong::ClampedUnsignedLong(val) => Some(Constrain::Value(*val)),
ConstrainULong::ConstrainULongRange(ref range) => {
if range.parent.min.is_some() || range.parent.max.is_some() {
Some(Constrain::Range(ConstrainRange {
min: range.parent.min,
max: range.parent.max,
ideal: range.ideal,
}))
} else if let Some(exact) = range.exact {
Some(Constrain::Value(exact))
} else {
// the unspecified case is treated as all three being none
None
}
},
}
}
fn convert_cdouble(js: &ConstrainDouble) -> Option<Constrain<f64>> {
match js {
ConstrainDouble::Double(val) => Some(Constrain::Value(**val)),
ConstrainDouble::ConstrainDoubleRange(ref range) => {
if range.parent.min.is_some() || range.parent.max.is_some() {
Some(Constrain::Range(ConstrainRange {
min: range.parent.min.map(|x| *x),
max: range.parent.max.map(|x| *x),
ideal: range.ideal.map(|x| *x),
}))
} else if let Some(exact) = range.exact {
Some(Constrain::Value(*exact))
} else {
// the unspecified case is treated as all three being none
None
}
},
}
}
|