Skip to main content

script/dom/audio/
mediastreamaudiodestinationnode.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 js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
9use servo_media::ServoMedia;
10use servo_media::audio::node::AudioNodeInit;
11use servo_media::streams::MediaStreamType;
12
13use crate::dom::audio::audiocontext::AudioContext;
14use crate::dom::audio::audionode::{AudioNode, AudioNodeOptionsHelper};
15use crate::dom::bindings::codegen::Bindings::AudioNodeBinding::{
16    AudioNodeOptions, ChannelCountMode, ChannelInterpretation,
17};
18use crate::dom::bindings::codegen::Bindings::MediaStreamAudioDestinationNodeBinding::MediaStreamAudioDestinationNodeMethods;
19use crate::dom::bindings::error::Fallible;
20use crate::dom::bindings::inheritance::Castable;
21use crate::dom::bindings::reflector::DomGlobal;
22use crate::dom::bindings::root::{Dom, DomRoot};
23use crate::dom::mediastream::MediaStream;
24use crate::dom::window::Window;
25
26#[dom_struct]
27pub(crate) struct MediaStreamAudioDestinationNode {
28    node: AudioNode,
29    stream: Dom<MediaStream>,
30}
31
32impl MediaStreamAudioDestinationNode {
33    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
34    pub(crate) fn new_inherited(
35        cx: &mut JSContext,
36        context: &AudioContext,
37        options: &AudioNodeOptions,
38    ) -> Fallible<MediaStreamAudioDestinationNode> {
39        let media = ServoMedia::get();
40        let (socket, id) = media.create_stream_and_socket(MediaStreamType::Audio);
41        let stream = MediaStream::new_single(cx, &context.global(), id, MediaStreamType::Audio);
42        let node_options = options.unwrap_or(
43            2,
44            ChannelCountMode::Explicit,
45            ChannelInterpretation::Speakers,
46        );
47        let node = AudioNode::new_inherited(
48            cx,
49            AudioNodeInit::MediaStreamDestinationNode(socket),
50            context.upcast(),
51            node_options,
52            1, // inputs
53            0, // outputs
54        )?;
55        Ok(MediaStreamAudioDestinationNode {
56            node,
57            stream: Dom::from_ref(&stream),
58        })
59    }
60
61    pub(crate) fn new(
62        cx: &mut JSContext,
63        window: &Window,
64        context: &AudioContext,
65        options: &AudioNodeOptions,
66    ) -> Fallible<DomRoot<MediaStreamAudioDestinationNode>> {
67        Self::new_with_proto(cx, window, None, context, options)
68    }
69
70    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
71    fn new_with_proto(
72        cx: &mut JSContext,
73        window: &Window,
74        proto: Option<HandleObject>,
75        context: &AudioContext,
76        options: &AudioNodeOptions,
77    ) -> Fallible<DomRoot<MediaStreamAudioDestinationNode>> {
78        let node = MediaStreamAudioDestinationNode::new_inherited(cx, context, options)?;
79        Ok(reflect_dom_object_with_proto_and_cx(
80            Box::new(node),
81            window,
82            proto,
83            cx,
84        ))
85    }
86}
87
88impl MediaStreamAudioDestinationNodeMethods<crate::DomTypeHolder>
89    for MediaStreamAudioDestinationNode
90{
91    /// <https://webaudio.github.io/web-audio-api/#dom-mediastreamaudiodestinationnode-mediastreamaudiodestinationnode>
92    fn Constructor(
93        cx: &mut JSContext,
94        window: &Window,
95        proto: Option<HandleObject>,
96        context: &AudioContext,
97        options: &AudioNodeOptions,
98    ) -> Fallible<DomRoot<MediaStreamAudioDestinationNode>> {
99        MediaStreamAudioDestinationNode::new_with_proto(cx, window, proto, context, options)
100    }
101
102    /// <https://webaudio.github.io/web-audio-api/#dom-mediastreamaudiodestinationnode-stream>
103    fn Stream(&self) -> DomRoot<MediaStream> {
104        DomRoot::from_ref(&self.stream)
105    }
106}