Skip to content

Commit

Permalink
Implement MediaDevices.enumerateDevices()
Browse files Browse the repository at this point in the history
  • Loading branch information
ferjm committed Jul 7, 2020
1 parent 0a30516 commit 8c575ae
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
81 changes: 81 additions & 0 deletions components/script/dom/mediadeviceinfo.rs
@@ -0,0 +1,81 @@
/* 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::dom::bindings::codegen::Bindings::MediaDeviceInfoBinding::MediaDeviceInfoMethods;
use crate::dom::bindings::codegen::Bindings::MediaDeviceInfoBinding::MediaDeviceKind;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use servo_media::streams::device_monitor::MediaDeviceKind as ServoMediaDeviceKind;

#[dom_struct]
pub struct MediaDeviceInfo {
reflector_: Reflector,
device_id: DOMString,
kind: MediaDeviceKind,
label: DOMString,
group_id: DOMString,
}

impl MediaDeviceInfo {
fn new_inherited(
device_id: &str,
kind: MediaDeviceKind,
label: &str,
group_id: &str,
) -> MediaDeviceInfo {
MediaDeviceInfo {
reflector_: Reflector::new(),
device_id: DOMString::from(device_id),
kind,
label: DOMString::from(label),
group_id: DOMString::from(group_id),
}
}

pub fn new(
global: &GlobalScope,
device_id: &str,
kind: MediaDeviceKind,
label: &str,
group_id: &str,
) -> DomRoot<MediaDeviceInfo> {
reflect_dom_object(
Box::new(MediaDeviceInfo::new_inherited(
device_id, kind, label, group_id,
)),
global,
)
}
}

impl MediaDeviceInfoMethods for MediaDeviceInfo {
fn DeviceId(&self) -> DOMString {
self.device_id.clone()
}

fn Kind(&self) -> MediaDeviceKind {
self.kind
}

fn Label(&self) -> DOMString {
self.label.clone()
}

fn GroupId(&self) -> DOMString {
self.group_id.clone()
}
}

impl From<ServoMediaDeviceKind> for MediaDeviceKind {
fn from(kind: ServoMediaDeviceKind) -> MediaDeviceKind {
match kind {
ServoMediaDeviceKind::AudioInput => MediaDeviceKind::Audioinput,
ServoMediaDeviceKind::AudioOutput => MediaDeviceKind::Audiooutput,
ServoMediaDeviceKind::VideoInput => MediaDeviceKind::Videoinput,
}
}
}
40 changes: 39 additions & 1 deletion components/script/dom/mediadevices.rs
Expand Up @@ -12,10 +12,11 @@ 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::mediadeviceinfo::MediaDeviceInfo;
use crate::dom::mediastream::MediaStream;
use crate::dom::mediastreamtrack::MediaStreamTrack;
use crate::dom::promise::Promise;
use crate::realms::InRealm;
use crate::realms::{AlreadyInRealm, InRealm};
use dom_struct::dom_struct;
use servo_media::streams::capture::{Constrain, ConstrainRange, MediaTrackConstraintSet};
use servo_media::streams::MediaStreamType;
Expand Down Expand Up @@ -62,6 +63,43 @@ impl MediaDevicesMethods for MediaDevices {
p.resolve_native(&stream);
p
}

/// https://w3c.github.io/mediacapture-main/#dom-mediadevices-enumeratedevices
fn EnumerateDevices(&self) -> Rc<Promise> {
// Step 1.
let global = self.global();
let in_realm_proof = AlreadyInRealm::assert(&global);
let p = Promise::new_in_current_realm(&global, InRealm::Already(&in_realm_proof));

// Step 2.
// XXX These steps should be run in parallel.
// XXX Steps 2.1 - 2.4

// Step 2.5
let media = ServoMedia::get().unwrap();
let device_monitor = media.get_device_monitor();
let result_list = match device_monitor.enumerate_devices() {
Ok(devices) => devices
.iter()
.map(|device| {
// XXX The media backend has no way to group devices yet.
MediaDeviceInfo::new(
&self.global(),
&device.device_id,
device.kind.into(),
&device.label,
"",
)
})
.collect(),
Err(_) => Vec::new(),
};

p.resolve_native(&result_list);

// Step 3.
p
}
}

fn convert_constraints(js: &BooleanOrMediaTrackConstraints) -> Option<MediaTrackConstraintSet> {
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/mod.rs
Expand Up @@ -425,6 +425,7 @@ pub mod imagedata;
pub mod inputevent;
pub mod keyboardevent;
pub mod location;
pub mod mediadeviceinfo;
pub mod mediadevices;
pub mod mediaelementaudiosourcenode;
pub mod mediaerror;
Expand Down
21 changes: 21 additions & 0 deletions components/script/dom/webidls/MediaDeviceInfo.webidl
@@ -0,0 +1,21 @@
/* 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/. */

// https://w3c.github.io/mediacapture-main/#device-info

[Exposed=Window,
SecureContext, Pref="dom.webrtc.enabled"]
interface MediaDeviceInfo {
readonly attribute DOMString deviceId;
readonly attribute MediaDeviceKind kind;
readonly attribute DOMString label;
readonly attribute DOMString groupId;
[Default] object toJSON();
};

enum MediaDeviceKind {
"audioinput",
"audiooutput",
"videoinput"
};
2 changes: 1 addition & 1 deletion components/script/dom/webidls/MediaDevices.webidl
Expand Up @@ -8,7 +8,7 @@
SecureContext, Pref="dom.webrtc.enabled"]
interface MediaDevices : EventTarget {
// attribute EventHandler ondevicechange;
// Promise<sequence<MediaDeviceInfo>> enumerateDevices();
Promise<sequence<MediaDeviceInfo>> enumerateDevices();
};

partial interface Navigator {
Expand Down

0 comments on commit 8c575ae

Please sign in to comment.