From 758ae94aa13b18a8e6014a6c771ffd3ee49f9817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Wed, 11 Jul 2018 15:02:10 +0200 Subject: [PATCH] AudioNode channel related properties getter and setter fixes --- components/script/dom/audionode.rs | 68 +++++++++++++++++++++++ components/script/dom/baseaudiocontext.rs | 5 ++ 2 files changed, 73 insertions(+) diff --git a/components/script/dom/audionode.rs b/components/script/dom/audionode.rs index 43e0ee736e7..3b9d6ce8ac3 100644 --- a/components/script/dom/audionode.rs +++ b/components/script/dom/audionode.rs @@ -5,13 +5,17 @@ use dom::baseaudiocontext::BaseAudioContext; use dom::bindings::codegen::Bindings::AudioNodeBinding::{AudioNodeMethods, AudioNodeOptions}; use dom::bindings::codegen::Bindings::AudioNodeBinding::{ChannelCountMode, ChannelInterpretation}; +use dom::bindings::codegen::InheritTypes::{AudioNodeTypeId, EventTargetTypeId}; use dom::bindings::error::{Error, ErrorResult, Fallible}; +use dom::bindings::inheritance::Castable; use dom::bindings::root::{Dom, DomRoot}; use dom::audioparam::AudioParam; use dom::eventtarget::EventTarget; use dom_struct::dom_struct; use servo_media::audio::graph::NodeId; use servo_media::audio::node::{AudioNodeMessage, AudioNodeInit}; +use servo_media::audio::node::ChannelCountMode as ServoMediaChannelCountMode; +use servo_media::audio::node::ChannelInterpretation as ServoMediaChannelInterpretation; use std::cell::Cell; // 32 is the minimum required by the spec for createBuffer() and the deprecated @@ -169,28 +173,92 @@ impl AudioNodeMethods for AudioNode { self.channel_count.get() } + // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcount fn SetChannelCount(&self, value: u32) -> ErrorResult { + match self.upcast::().type_id() { + EventTargetTypeId::AudioNode(AudioNodeTypeId::AudioDestinationNode) => { + if self.context.is_offline() { + return Err(Error::InvalidState); + } else if value < 1 || value > MAX_CHANNEL_COUNT { + return Err(Error::IndexSize); + } + }, + // XXX We do not support any of the other AudioNodes with + // constraints yet. Add more cases here as we add support + // for new AudioNodes. + _ => (), + }; + if value == 0 || value > MAX_CHANNEL_COUNT { return Err(Error::NotSupported); } + self.channel_count.set(value); + self.message(AudioNodeMessage::SetChannelCount(value as u8)); Ok(()) } + // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcountmode fn ChannelCountMode(&self) -> ChannelCountMode { self.channel_count_mode.get() } + // https://webaudio.github.io/web-audio-api/#dom-audionode-channelcountmode fn SetChannelCountMode(&self, value: ChannelCountMode) -> ErrorResult { + // Channel count mode has no effect for nodes with no inputs. + if self.number_of_inputs == 0 { + return Ok(()); + } + + match self.upcast::().type_id() { + EventTargetTypeId::AudioNode(AudioNodeTypeId::AudioDestinationNode) => { + if self.context.is_offline() { + return Err(Error::InvalidState); + } + }, + // XXX We do not support any of the other AudioNodes with + // constraints yet. Add more cases here as we add support + // for new AudioNodes. + _ => (), + }; + self.channel_count_mode.set(value); + self.message(AudioNodeMessage::SetChannelMode(value.into())); Ok(()) } + // https://webaudio.github.io/web-audio-api/#dom-audionode-channelinterpretation fn ChannelInterpretation(&self) -> ChannelInterpretation { self.channel_interpretation.get() } + // https://webaudio.github.io/web-audio-api/#dom-audionode-channelinterpretation fn SetChannelInterpretation(&self, value: ChannelInterpretation) { + // Channel interpretation mode has no effect for nodes with no inputs. + if self.number_of_inputs == 0 { + return Ok(()); + } + self.channel_interpretation.set(value); + self.message(AudioNodeMessage::SetChannelInterpretation(value.into())); + } +} + +impl From for ServoMediaChannelCountMode { + fn from(mode: ChannelCountMode) -> Self { + match mode { + ChannelCountMode::Max => ServoMediaChannelCountMode::Max, + ChannelCountMode::Clamped_max => ServoMediaChannelCountMode::ClampedMax, + ChannelCountMode::Explicit => ServoMediaChannelCountMode::Explicit, + } + } +} + +impl From for ServoMediaChannelInterpretation { + fn from(interpretation: ChannelInterpretation) -> Self { + match interpretation { + ChannelInterpretation::Discrete => ServoMediaChannelInterpretation::Discrete, + ChannelInterpretation::Speakers => ServoMediaChannelInterpretation::Speakers, + } } } diff --git a/components/script/dom/baseaudiocontext.rs b/components/script/dom/baseaudiocontext.rs index 1dbf8bbaf7a..7010cd76661 100644 --- a/components/script/dom/baseaudiocontext.rs +++ b/components/script/dom/baseaudiocontext.rs @@ -112,6 +112,11 @@ impl BaseAudioContext { context } + /// Tells whether this is an OfflineAudioContext or not. + pub fn is_offline(&self) -> bool { + false + } + pub fn audio_context_impl(&self) -> Rc { self.audio_context_impl.clone() }