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
/* 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 dom_struct::dom_struct;
use servo_media::streams::device_monitor::MediaDeviceKind as ServoMediaDeviceKind;

use crate::dom::bindings::codegen::Bindings::MediaDeviceInfoBinding::{
    MediaDeviceInfoMethods, 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;

#[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 {
    /// <https://w3c.github.io/mediacapture-main/#dom-mediadeviceinfo-deviceid>
    fn DeviceId(&self) -> DOMString {
        self.device_id.clone()
    }

    /// <https://w3c.github.io/mediacapture-main/#dom-mediadeviceinfo-kind>
    fn Kind(&self) -> MediaDeviceKind {
        self.kind
    }

    /// <https://w3c.github.io/mediacapture-main/#dom-mediadeviceinfo-label>
    fn Label(&self) -> DOMString {
        self.label.clone()
    }

    /// <https://w3c.github.io/mediacapture-main/#dom-mediadeviceinfo-groupid>
    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,
        }
    }
}