Skip to content
Permalink
Browse files

Add support for basic MediaTrackConstraints

  • Loading branch information...
Manishearth committed Feb 12, 2019
1 parent 28c6dec commit dd7b69b6d8876ca3073c8ea6aeca007073ca3425
Showing with 75 additions and 38 deletions.
  1. +41 −4 components/script/dom/mediadevices.rs
  2. +34 −34 components/script/dom/webidls/MediaDevices.webidl
@@ -4,6 +4,8 @@

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::reflector::reflect_dom_object;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::DomRoot;
@@ -12,6 +14,7 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::mediastream::MediaStream;
use crate::dom::promise::Promise;
use dom_struct::dom_struct;
use servo_media::streams::capture::{Constrain, ConstrainRange, MediaTrackConstraintSet};
use servo_media::ServoMedia;
use std::rc::Rc;

@@ -42,13 +45,13 @@ impl MediaDevicesMethods for MediaDevices {
let p = Promise::new(&self.global());
let media = ServoMedia::get().unwrap();
let mut tracks = vec![];
if constraints.audio {
if let Some(audio) = media.create_audioinput_stream(Default::default()) {
if let Some(constraints) = convert_constraints(&constraints.audio) {
if let Some(audio) = media.create_audioinput_stream(constraints) {
tracks.push(audio)
}
}
if constraints.video {
if let Some(video) = media.create_videoinput_stream(Default::default()) {
if let Some(constraints) = convert_constraints(&constraints.video) {
if let Some(video) = media.create_videoinput_stream(constraints) {
tracks.push(video)
}
}
@@ -57,3 +60,37 @@ impl MediaDevicesMethods for MediaDevices {
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: convert_culong(&c.parent.height),
width: convert_culong(&c.parent.width),
..Default::default()
})
},
}
}

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
}
},
}
}
@@ -23,10 +23,10 @@ partial interface MediaDevices {


dictionary MediaStreamConstraints {
// (boolean or MediaTrackConstraints) video = false;
// (boolean or MediaTrackConstraints) audio = false;
boolean video = false;
boolean audio = false;
(boolean or MediaTrackConstraints) video;
// (boolean or MediaTrackConstraints) video = false;
(boolean or MediaTrackConstraints) audio;
// (boolean or MediaTrackConstraints) audio = false;
};

// dictionary DoubleRange {
@@ -39,15 +39,15 @@ dictionary MediaStreamConstraints {
// double ideal;
// };

// dictionary ULongRange {
// [Clamp] unsigned long max;
// [Clamp] unsigned long min;
// };
dictionary ULongRange {
[Clamp] unsigned long max;
[Clamp] unsigned long min;
};

// dictionary ConstrainULongRange : ULongRange {
// [Clamp] unsigned long exact;
// [Clamp] unsigned long ideal;
// };
dictionary ConstrainULongRange : ULongRange {
[Clamp] unsigned long exact;
[Clamp] unsigned long ideal;
};

// dictionary ConstrainBooleanParameters {
// boolean exact;
@@ -59,30 +59,30 @@ dictionary MediaStreamConstraints {
// (DOMString or sequence<DOMString>) ideal;
// };

// dictionary MediaTrackConstraints : MediaTrackConstraintSet {
// sequence<MediaTrackConstraintSet> advanced;
// };
dictionary MediaTrackConstraints : MediaTrackConstraintSet {
sequence<MediaTrackConstraintSet> advanced;
};

// typedef ([Clamp] unsigned long or ConstrainULongRange) ConstrainULong;
typedef ([Clamp] unsigned long or ConstrainULongRange) ConstrainULong;
// typedef (double or ConstrainDoubleRange) ConstrainDouble;
// typedef (boolean or ConstrainBooleanParameters) ConstrainBoolean;
// typedef (DOMString or sequence<DOMString> or ConstrainDOMStringParameters) ConstrainDOMString;

// dictionary MediaTrackConstraintSet {
// ConstrainULong width;
// ConstrainULong height;
// ConstrainDouble aspectRatio;
// ConstrainDouble frameRate;
// ConstrainDOMString facingMode;
// ConstrainDOMString resizeMode;
// ConstrainDouble volume;
// ConstrainULong sampleRate;
// ConstrainULong sampleSize;
// ConstrainBoolean echoCancellation;
// ConstrainBoolean autoGainControl;
// ConstrainBoolean noiseSuppression;
// ConstrainDouble latency;
// ConstrainULong channelCount;
// ConstrainDOMString deviceId;
// ConstrainDOMString groupId;
// };
dictionary MediaTrackConstraintSet {
ConstrainULong width;
ConstrainULong height;
// ConstrainDouble aspectRatio;
// ConstrainDouble frameRate;
// ConstrainDOMString facingMode;
// ConstrainDOMString resizeMode;
// ConstrainDouble volume;
// ConstrainULong sampleRate;
// ConstrainULong sampleSize;
// ConstrainBoolean echoCancellation;
// ConstrainBoolean autoGainControl;
// ConstrainBoolean noiseSuppression;
// ConstrainDouble latency;
// ConstrainULong channelCount;
// ConstrainDOMString deviceId;
// ConstrainDOMString groupId;
};

0 comments on commit dd7b69b

Please sign in to comment.
You can’t perform that action at this time.