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