script/dom/media/
mediastreamtrack.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use servo_media::streams::MediaStreamType;
7use servo_media::streams::registry::MediaStreamId;
8
9use crate::dom::bindings::codegen::Bindings::MediaStreamTrackBinding::MediaStreamTrackMethods;
10use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_cx};
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::str::DOMString;
13use crate::dom::eventtarget::EventTarget;
14use crate::dom::globalscope::GlobalScope;
15
16#[dom_struct]
17pub(crate) struct MediaStreamTrack {
18    eventtarget: EventTarget,
19    #[ignore_malloc_size_of = "defined in servo-media"]
20    #[no_trace]
21    id: MediaStreamId,
22    #[ignore_malloc_size_of = "defined in servo-media"]
23    #[no_trace]
24    ty: MediaStreamType,
25}
26
27impl MediaStreamTrack {
28    pub(crate) fn new_inherited(id: MediaStreamId, ty: MediaStreamType) -> MediaStreamTrack {
29        MediaStreamTrack {
30            eventtarget: EventTarget::new_inherited(),
31            id,
32            ty,
33        }
34    }
35
36    pub(crate) fn new(
37        cx: &mut js::context::JSContext,
38        global: &GlobalScope,
39        id: MediaStreamId,
40        ty: MediaStreamType,
41    ) -> DomRoot<MediaStreamTrack> {
42        reflect_dom_object_with_cx(
43            Box::new(MediaStreamTrack::new_inherited(id, ty)),
44            global,
45            cx,
46        )
47    }
48
49    pub(crate) fn id(&self) -> MediaStreamId {
50        self.id
51    }
52
53    pub(crate) fn ty(&self) -> MediaStreamType {
54        self.ty
55    }
56}
57
58impl MediaStreamTrackMethods<crate::DomTypeHolder> for MediaStreamTrack {
59    /// <https://w3c.github.io/mediacapture-main/#dom-mediastreamtrack-kind>
60    fn Kind(&self) -> DOMString {
61        match self.ty {
62            MediaStreamType::Video => "video".into(),
63            MediaStreamType::Audio => "audio".into(),
64        }
65    }
66
67    /// <https://w3c.github.io/mediacapture-main/#dom-mediastreamtrack-id>
68    fn Id(&self) -> DOMString {
69        self.id.id().to_string().into()
70    }
71
72    /// <https://w3c.github.io/mediacapture-main/#dom-mediastreamtrack-clone>
73    fn Clone(&self, cx: &mut js::context::JSContext) -> DomRoot<MediaStreamTrack> {
74        MediaStreamTrack::new(cx, &self.global(), self.id, self.ty)
75    }
76}