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