Skip to main content

script/dom/webrtc/
rtcrtptransceiver.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 std::cell::Cell;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
10
11use crate::dom::bindings::codegen::Bindings::RTCRtpTransceiverBinding::{
12    RTCRtpTransceiverDirection, RTCRtpTransceiverMethods,
13};
14use crate::dom::bindings::root::{Dom, DomRoot};
15use crate::dom::globalscope::GlobalScope;
16use crate::dom::rtcrtpsender::RTCRtpSender;
17
18#[dom_struct]
19pub(crate) struct RTCRtpTransceiver {
20    reflector_: Reflector,
21    sender: Dom<RTCRtpSender>,
22    direction: Cell<RTCRtpTransceiverDirection>,
23}
24
25impl RTCRtpTransceiver {
26    fn new_inherited(
27        cx: &mut JSContext,
28        global: &GlobalScope,
29        direction: RTCRtpTransceiverDirection,
30    ) -> Self {
31        let sender = RTCRtpSender::new(cx, global);
32        Self {
33            reflector_: Reflector::new(),
34            direction: Cell::new(direction),
35            sender: Dom::from_ref(&*sender),
36        }
37    }
38
39    pub(crate) fn new(
40        cx: &mut JSContext,
41        global: &GlobalScope,
42        direction: RTCRtpTransceiverDirection,
43    ) -> DomRoot<Self> {
44        reflect_dom_object_with_cx(
45            Box::new(Self::new_inherited(cx, global, direction)),
46            global,
47            cx,
48        )
49    }
50}
51
52impl RTCRtpTransceiverMethods<crate::DomTypeHolder> for RTCRtpTransceiver {
53    /// <https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction>
54    fn Direction(&self) -> RTCRtpTransceiverDirection {
55        self.direction.get()
56    }
57
58    /// <https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction>
59    fn SetDirection(&self, direction: RTCRtpTransceiverDirection) {
60        self.direction.set(direction);
61    }
62
63    /// <https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-sender>
64    fn Sender(&self) -> DomRoot<RTCRtpSender> {
65        DomRoot::from_ref(&*self.sender)
66    }
67}