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};
20use crate::dom::bindings::root::{Dom, DomRoot};
21use crate::dom::mediastream::MediaStream;
22use crate::dom::window::Window;
23use crate::script_runtime::CanGc;
24
25#[dom_struct]
26pub(crate) struct MediaStreamAudioDestinationNode {
27    node: AudioNode,
28    stream: Dom<MediaStream>,
29}
30
31impl MediaStreamAudioDestinationNode {
32    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
33    pub(crate) fn new_inherited(
34        context: &AudioContext,
35        options: &AudioNodeOptions,
36        can_gc: CanGc,
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(&context.global(), id, MediaStreamType::Audio, can_gc);
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        window: &Window,
61        context: &AudioContext,
62        options: &AudioNodeOptions,
63        can_gc: CanGc,
64    ) -> Fallible<DomRoot<MediaStreamAudioDestinationNode>> {
65        Self::new_with_proto(window, None, context, options, can_gc)
66    }
67
68    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
69    fn new_with_proto(
70        window: &Window,
71        proto: Option<HandleObject>,
72        context: &AudioContext,
73        options: &AudioNodeOptions,
74        can_gc: CanGc,
75    ) -> Fallible<DomRoot<MediaStreamAudioDestinationNode>> {
76        let node = MediaStreamAudioDestinationNode::new_inherited(context, options, can_gc)?;
77        Ok(reflect_dom_object_with_proto(
78            Box::new(node),
79            window,
80            proto,
81            can_gc,
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        window: &Window,
92        proto: Option<HandleObject>,
93        can_gc: CanGc,
94        context: &AudioContext,
95        options: &AudioNodeOptions,
96    ) -> Fallible<DomRoot<MediaStreamAudioDestinationNode>> {
97        MediaStreamAudioDestinationNode::new_with_proto(window, proto, context, options, can_gc)
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}