Skip to main content

script/dom/audio/
channelsplitternode.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::audio::node::AudioNodeInit;
10
11use crate::dom::audio::audionode::{AudioNode, AudioNodeOptionsHelper, MAX_CHANNEL_COUNT};
12use crate::dom::audio::baseaudiocontext::BaseAudioContext;
13use crate::dom::bindings::codegen::Bindings::AudioNodeBinding::{
14    ChannelCountMode, ChannelInterpretation,
15};
16use crate::dom::bindings::codegen::Bindings::ChannelSplitterNodeBinding::{
17    ChannelSplitterNodeMethods, ChannelSplitterOptions,
18};
19use crate::dom::bindings::error::{Error, Fallible};
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::window::Window;
22
23#[dom_struct]
24pub(crate) struct ChannelSplitterNode {
25    node: AudioNode,
26}
27
28impl ChannelSplitterNode {
29    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
30    pub(crate) fn new_inherited(
31        cx: &mut JSContext,
32        _: &Window,
33        context: &BaseAudioContext,
34        options: &ChannelSplitterOptions,
35    ) -> Fallible<ChannelSplitterNode> {
36        if options.numberOfOutputs < 1 || options.numberOfOutputs > MAX_CHANNEL_COUNT {
37            return Err(Error::IndexSize(None));
38        }
39
40        let node_options = options.parent.unwrap_or(
41            options.numberOfOutputs,
42            ChannelCountMode::Explicit,
43            ChannelInterpretation::Discrete,
44        );
45
46        if node_options.count != options.numberOfOutputs ||
47            node_options.mode != ChannelCountMode::Explicit ||
48            node_options.interpretation != ChannelInterpretation::Discrete
49        {
50            return Err(Error::InvalidState(None));
51        }
52
53        let node = AudioNode::new_inherited(
54            cx,
55            AudioNodeInit::ChannelSplitterNode,
56            context,
57            node_options,
58            1,                       // inputs
59            options.numberOfOutputs, // outputs
60        )?;
61        Ok(ChannelSplitterNode { node })
62    }
63
64    pub(crate) fn new(
65        cx: &mut JSContext,
66        window: &Window,
67        context: &BaseAudioContext,
68        options: &ChannelSplitterOptions,
69    ) -> Fallible<DomRoot<ChannelSplitterNode>> {
70        Self::new_with_proto(cx, window, None, context, options)
71    }
72
73    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
74    fn new_with_proto(
75        cx: &mut JSContext,
76        window: &Window,
77        proto: Option<HandleObject>,
78        context: &BaseAudioContext,
79        options: &ChannelSplitterOptions,
80    ) -> Fallible<DomRoot<ChannelSplitterNode>> {
81        let node = ChannelSplitterNode::new_inherited(cx, window, context, options)?;
82        Ok(reflect_dom_object_with_proto_and_cx(
83            Box::new(node),
84            window,
85            proto,
86            cx,
87        ))
88    }
89}
90
91impl ChannelSplitterNodeMethods<crate::DomTypeHolder> for ChannelSplitterNode {
92    /// <https://webaudio.github.io/web-audio-api/#dom-channelsplitternode-channelsplitternode>
93    fn Constructor(
94        cx: &mut JSContext,
95        window: &Window,
96        proto: Option<HandleObject>,
97        context: &BaseAudioContext,
98        options: &ChannelSplitterOptions,
99    ) -> Fallible<DomRoot<ChannelSplitterNode>> {
100        ChannelSplitterNode::new_with_proto(cx, window, proto, context, options)
101    }
102}