fire_timer moved due timers into an unrooted Vec; a GC inside one callback swept the functions of the timers still waiting. Heavy-web reruns and cargo test still pending. Co-Authored-By: Claude Opus 5.5 <[email protected]>
727 lines
30 KiB
Rust
727 lines
30 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
use std::cell::Cell;
|
|
use std::collections::hash_map::Entry;
|
|
use std::collections::{HashMap, VecDeque};
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use dom_struct::dom_struct;
|
|
use js::context::JSContext;
|
|
use js::realm::CurrentRealm;
|
|
use js::rust::CustomAutoRooterGuard;
|
|
use js::typedarray::{ArrayBuffer, ArrayBufferU8};
|
|
use script_bindings::cell::DomRefCell;
|
|
use script_bindings::cformat;
|
|
use script_bindings::codegen::GenericBindings::PeriodicWaveBinding::PeriodicWaveMethods;
|
|
use servo_base::id::PipelineId;
|
|
use servo_media::audio::context::{
|
|
AudioContext, AudioContextOptions, OfflineAudioContextOptions, ProcessingState,
|
|
RealTimeAudioContextOptions,
|
|
};
|
|
use servo_media::audio::decoder::AudioDecoderCallbacksBuilder;
|
|
use servo_media::audio::graph::NodeId;
|
|
use servo_media::{ClientContextId, ServoMedia};
|
|
use uuid::Uuid;
|
|
|
|
use crate::conversions::Convert;
|
|
use crate::dom::audio::analysernode::AnalyserNode;
|
|
use crate::dom::audio::audiobuffer::AudioBuffer;
|
|
use crate::dom::audio::audiobuffersourcenode::AudioBufferSourceNode;
|
|
use crate::dom::audio::audiodestinationnode::AudioDestinationNode;
|
|
use crate::dom::audio::audiolistener::AudioListener;
|
|
use crate::dom::audio::audionode::MAX_CHANNEL_COUNT;
|
|
use crate::dom::audio::biquadfilternode::BiquadFilterNode;
|
|
use crate::dom::audio::channelmergernode::ChannelMergerNode;
|
|
use crate::dom::audio::channelsplitternode::ChannelSplitterNode;
|
|
use crate::dom::audio::constantsourcenode::ConstantSourceNode;
|
|
use crate::dom::audio::gainnode::GainNode;
|
|
use crate::dom::audio::iirfilternode::IIRFilterNode;
|
|
use crate::dom::audio::oscillatornode::OscillatorNode;
|
|
use crate::dom::audio::pannernode::PannerNode;
|
|
use crate::dom::audio::stereopannernode::StereoPannerNode;
|
|
use crate::dom::bindings::buffer_source::HeapBufferSource;
|
|
use crate::dom::bindings::callback::{ExceptionHandling, RootedCallback, TracedCallback};
|
|
use crate::dom::bindings::codegen::Bindings::AnalyserNodeBinding::AnalyserOptions;
|
|
use crate::dom::bindings::codegen::Bindings::AudioBufferSourceNodeBinding::AudioBufferSourceOptions;
|
|
use crate::dom::bindings::codegen::Bindings::AudioNodeBinding::{
|
|
AudioNodeOptions, ChannelCountMode, ChannelInterpretation,
|
|
};
|
|
use crate::dom::bindings::codegen::Bindings::BaseAudioContextBinding::{
|
|
AudioContextState, BaseAudioContextMethods, DecodeErrorCallback, DecodeSuccessCallback,
|
|
};
|
|
use crate::dom::bindings::codegen::Bindings::BiquadFilterNodeBinding::BiquadFilterOptions;
|
|
use crate::dom::bindings::codegen::Bindings::ChannelMergerNodeBinding::ChannelMergerOptions;
|
|
use crate::dom::bindings::codegen::Bindings::ChannelSplitterNodeBinding::ChannelSplitterOptions;
|
|
use crate::dom::bindings::codegen::Bindings::ConstantSourceNodeBinding::ConstantSourceOptions;
|
|
use crate::dom::bindings::codegen::Bindings::GainNodeBinding::GainOptions;
|
|
use crate::dom::bindings::codegen::Bindings::IIRFilterNodeBinding::IIRFilterOptions;
|
|
use crate::dom::bindings::codegen::Bindings::OscillatorNodeBinding::OscillatorOptions;
|
|
use crate::dom::bindings::codegen::Bindings::PannerNodeBinding::PannerOptions;
|
|
use crate::dom::bindings::codegen::Bindings::PeriodicWaveBinding::{
|
|
PeriodicWaveConstraints, PeriodicWaveOptions,
|
|
};
|
|
use crate::dom::bindings::codegen::Bindings::StereoPannerNodeBinding::StereoPannerOptions;
|
|
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
|
|
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
|
|
use crate::dom::bindings::inheritance::Castable;
|
|
use crate::dom::bindings::num::Finite;
|
|
use crate::dom::bindings::refcounted::Trusted;
|
|
use crate::dom::bindings::reflector::DomGlobal;
|
|
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
|
|
use crate::dom::domexception::{DOMErrorName, DOMException};
|
|
use crate::dom::eventtarget::EventTarget;
|
|
use crate::dom::promise::{Promise, RootedPromise, TracedPromise};
|
|
use crate::dom::types::PeriodicWave;
|
|
|
|
pub(crate) enum BaseAudioContextOptions {
|
|
AudioContext(RealTimeAudioContextOptions),
|
|
OfflineAudioContext(OfflineAudioContextOptions),
|
|
}
|
|
|
|
#[derive(JSTraceable, MallocSizeOf)]
|
|
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
|
struct DecodeResolver {
|
|
pub(crate) promise: TracedPromise,
|
|
pub(crate) success_callback: Option<TracedCallback<DecodeSuccessCallback>>,
|
|
pub(crate) error_callback: Option<TracedCallback<DecodeErrorCallback>>,
|
|
}
|
|
|
|
impl js::gc::Rootable for DecodeResolver {}
|
|
|
|
type BoxedSliceOfPromises = Box<[TracedPromise]>;
|
|
|
|
#[dom_struct]
|
|
pub(crate) struct BaseAudioContext {
|
|
eventtarget: EventTarget,
|
|
#[ignore_malloc_size_of = "servo_media"]
|
|
#[no_trace]
|
|
audio_context_impl: Arc<Mutex<AudioContext>>,
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-destination>
|
|
destination: MutNullableDom<AudioDestinationNode>,
|
|
listener: MutNullableDom<AudioListener>,
|
|
/// Resume promises which are soon to be fulfilled by a queued task.
|
|
in_flight_resume_promises_queue: DomRefCell<VecDeque<(BoxedSliceOfPromises, ErrorResult)>>,
|
|
/// <https://webaudio.github.io/web-audio-api/#pendingresumepromises>
|
|
pending_resume_promises: DomRefCell<Vec<TracedPromise>>,
|
|
decode_resolvers: DomRefCell<HashMap<String, DecodeResolver>>,
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-samplerate>
|
|
sample_rate: f32,
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-state>
|
|
/// Although servo-media already keeps track of the control thread state,
|
|
/// we keep a state flag here as well. This is so that we can synchronously
|
|
/// throw when trying to do things on the context when the context has just
|
|
/// been "closed()".
|
|
state: Cell<AudioContextState>,
|
|
channel_count: u32,
|
|
}
|
|
|
|
impl BaseAudioContext {
|
|
pub(crate) fn new_inherited(
|
|
options: BaseAudioContextOptions,
|
|
pipeline_id: PipelineId,
|
|
) -> Fallible<BaseAudioContext> {
|
|
let (sample_rate, channel_count) = match options {
|
|
BaseAudioContextOptions::AudioContext(ref opt) => (opt.sample_rate, 2),
|
|
BaseAudioContextOptions::OfflineAudioContext(ref opt) => {
|
|
(opt.sample_rate, opt.channels)
|
|
},
|
|
};
|
|
|
|
let client_context_id =
|
|
ClientContextId::build(pipeline_id.namespace_id.0, pipeline_id.index.0.get());
|
|
let audio_context_impl = ServoMedia::get()
|
|
.create_audio_context(&client_context_id, options.convert())
|
|
.map_err(|_| Error::NotSupported(None))?;
|
|
|
|
Ok(BaseAudioContext {
|
|
eventtarget: EventTarget::new_inherited(),
|
|
audio_context_impl,
|
|
destination: Default::default(),
|
|
listener: Default::default(),
|
|
in_flight_resume_promises_queue: Default::default(),
|
|
pending_resume_promises: Default::default(),
|
|
decode_resolvers: Default::default(),
|
|
sample_rate,
|
|
state: Cell::new(AudioContextState::Suspended),
|
|
channel_count: channel_count.into(),
|
|
})
|
|
}
|
|
|
|
/// Tells whether this is an OfflineAudioContext or not.
|
|
pub(crate) fn is_offline(&self) -> bool {
|
|
false
|
|
}
|
|
|
|
pub(crate) fn audio_context_impl(&self) -> Arc<Mutex<AudioContext>> {
|
|
self.audio_context_impl.clone()
|
|
}
|
|
|
|
pub(crate) fn destination_node(&self) -> NodeId {
|
|
self.audio_context_impl.lock().unwrap().dest_node()
|
|
}
|
|
|
|
pub(crate) fn listener(&self) -> NodeId {
|
|
self.audio_context_impl.lock().unwrap().listener()
|
|
}
|
|
|
|
// https://webaudio.github.io/web-audio-api/#allowed-to-start
|
|
pub(crate) fn is_allowed_to_start(&self) -> bool {
|
|
self.state.get() == AudioContextState::Suspended
|
|
}
|
|
|
|
fn push_pending_resume_promise(&self, promise: &RootedPromise) {
|
|
self.pending_resume_promises
|
|
.borrow_mut()
|
|
.push(promise.to_traced());
|
|
}
|
|
|
|
/// Takes the pending resume promises.
|
|
///
|
|
/// The result with which these promises will be fulfilled is passed here
|
|
/// and this method returns nothing because we actually just move the
|
|
/// current list of pending resume promises to the
|
|
/// `in_flight_resume_promises_queue` field.
|
|
///
|
|
/// Each call to this method must be followed by a call to
|
|
/// `fulfill_in_flight_resume_promises`, to actually fulfill the promises
|
|
/// which were taken and moved to the in-flight queue.
|
|
fn take_pending_resume_promises(&self, result: ErrorResult) {
|
|
self.in_flight_resume_promises_queue
|
|
.borrow_mut()
|
|
.push_back((
|
|
std::mem::take(&mut *self.pending_resume_promises.borrow_mut()).into(),
|
|
result,
|
|
));
|
|
}
|
|
|
|
/// Fulfills the next in-flight resume promises queue after running a closure.
|
|
///
|
|
/// See the comment on `take_pending_resume_promises` for why this method
|
|
/// does not take a list of promises to fulfill. Callers cannot just pop
|
|
/// the front list off of `in_flight_resume_promises_queue` and later fulfill
|
|
/// the promises because that would mean putting
|
|
/// `#[cfg_attr(crown, expect(crown::unrooted_must_root))]` on even more functions, potentially
|
|
/// hiding actual safety bugs.
|
|
fn fulfill_in_flight_resume_promises<F>(&self, cx: &mut JSContext, f: F)
|
|
where
|
|
F: FnOnce(),
|
|
{
|
|
let (promises, result) = self
|
|
.in_flight_resume_promises_queue
|
|
.borrow_mut()
|
|
.pop_front()
|
|
.map(|(promises, result)| {
|
|
let promises: Vec<RootedPromise> =
|
|
promises.iter().map(|promise| promise.root(cx)).collect();
|
|
(promises, result)
|
|
})
|
|
.expect("there should be at least one list of in flight resume promises");
|
|
f();
|
|
for promise in &promises {
|
|
match result {
|
|
Ok(ref value) => promise.resolve_native(cx, value),
|
|
Err(ref error) => promise.reject_error(cx, error.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Control thread processing state
|
|
pub(crate) fn control_thread_state(&self) -> ProcessingState {
|
|
self.audio_context_impl.lock().unwrap().state()
|
|
}
|
|
|
|
/// Set audio context state
|
|
pub(crate) fn set_state_attribute(&self, state: AudioContextState) {
|
|
self.state.set(state);
|
|
}
|
|
|
|
pub(crate) fn resume(&self) {
|
|
let this = Trusted::new(self);
|
|
// Set the rendering thread state to 'running' and start
|
|
// rendering the audio graph.
|
|
match self.audio_context_impl.lock().unwrap().resume() {
|
|
Some(()) => {
|
|
self.take_pending_resume_promises(Ok(()));
|
|
self.global().task_manager().dom_manipulation_task_source().queue(
|
|
task!(resume_success: move |cx| {
|
|
let this = this.root();
|
|
this.fulfill_in_flight_resume_promises(cx, || {
|
|
if this.state.get() != AudioContextState::Running {
|
|
this.state.set(AudioContextState::Running);
|
|
this.global().task_manager().dom_manipulation_task_source().queue_simple_event(
|
|
this.upcast(),
|
|
atom!("statechange"),
|
|
);
|
|
}
|
|
});
|
|
})
|
|
);
|
|
},
|
|
None => {
|
|
self.take_pending_resume_promises(Err(Error::Type(
|
|
c"Something went wrong".to_owned(),
|
|
)));
|
|
self.global()
|
|
.task_manager()
|
|
.dom_manipulation_task_source()
|
|
.queue(task!(resume_error: move |cx| {
|
|
this.root().fulfill_in_flight_resume_promises(cx, || {})
|
|
}));
|
|
},
|
|
}
|
|
}
|
|
|
|
pub(crate) fn channel_count(&self) -> u32 {
|
|
self.channel_count
|
|
}
|
|
}
|
|
|
|
impl BaseAudioContextMethods<crate::DomTypeHolder> for BaseAudioContext {
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-samplerate>
|
|
fn SampleRate(&self) -> Finite<f32> {
|
|
Finite::wrap(self.sample_rate)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-currenttime>
|
|
fn CurrentTime(&self) -> Finite<f64> {
|
|
let current_time = self.audio_context_impl.lock().unwrap().current_time();
|
|
Finite::wrap(current_time)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-state>
|
|
fn State(&self) -> AudioContextState {
|
|
self.state.get()
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-resume>
|
|
fn Resume(&self, cx: &mut CurrentRealm) -> RootedPromise {
|
|
// Step 1.
|
|
let promise = Promise::new_in_realm(cx);
|
|
|
|
// Step 2.
|
|
if self.audio_context_impl.lock().unwrap().state() == ProcessingState::Closed {
|
|
promise.reject_error(cx, Error::InvalidState(None));
|
|
return promise;
|
|
}
|
|
|
|
// Step 3.
|
|
if self.state.get() == AudioContextState::Running {
|
|
promise.resolve_native(cx, &());
|
|
return promise;
|
|
}
|
|
|
|
self.push_pending_resume_promise(&promise);
|
|
|
|
// Step 4.
|
|
if !self.is_allowed_to_start() {
|
|
return promise;
|
|
}
|
|
|
|
// Steps 5 and 6.
|
|
self.resume();
|
|
|
|
// Step 7.
|
|
promise
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-destination>
|
|
fn Destination(&self, cx: &mut JSContext) -> DomRoot<AudioDestinationNode> {
|
|
let global = self.global();
|
|
self.destination.or_init(|| {
|
|
let mut options = AudioNodeOptions::empty();
|
|
options.channelCount = Some(self.channel_count);
|
|
options.channelCountMode = Some(ChannelCountMode::Explicit);
|
|
options.channelInterpretation = Some(ChannelInterpretation::Speakers);
|
|
AudioDestinationNode::new(cx, &global, self, &options)
|
|
})
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-listener>
|
|
fn Listener(&self, cx: &mut JSContext) -> DomRoot<AudioListener> {
|
|
let global = self.global();
|
|
let window = global.as_window();
|
|
self.listener
|
|
.or_init(|| AudioListener::new(cx, window, self))
|
|
}
|
|
|
|
// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-onstatechange
|
|
event_handler!(statechange, GetOnstatechange, SetOnstatechange);
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createoscillator>
|
|
fn CreateOscillator(&self, cx: &mut JSContext) -> Fallible<DomRoot<OscillatorNode>> {
|
|
OscillatorNode::new(
|
|
cx,
|
|
self.global().as_window(),
|
|
self,
|
|
&OscillatorOptions::empty(),
|
|
)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-creategain>
|
|
fn CreateGain(&self, cx: &mut JSContext) -> Fallible<DomRoot<GainNode>> {
|
|
GainNode::new(cx, self.global().as_window(), self, &GainOptions::empty())
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createpanner>
|
|
fn CreatePanner(&self, cx: &mut JSContext) -> Fallible<DomRoot<PannerNode>> {
|
|
PannerNode::new(cx, self.global().as_window(), self, &PannerOptions::empty())
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createperiodicwave>
|
|
fn CreatePeriodicWave(
|
|
&self,
|
|
cx: &mut JSContext,
|
|
real: Vec<Finite<f32>>,
|
|
imag: Vec<Finite<f32>>,
|
|
constraints: &PeriodicWaveConstraints,
|
|
) -> Fallible<DomRoot<PeriodicWave>> {
|
|
// options is a new object of type PeriodicWaveOptions.
|
|
let mut options = PeriodicWaveOptions::empty();
|
|
let mut constraints_copy = PeriodicWaveConstraints::empty();
|
|
// Set the disableNormalization attribute on options to the value of the
|
|
// disableNormalization attribute of the constraints attribute passed to the factory method.
|
|
constraints_copy.disableNormalization = constraints.disableNormalization;
|
|
// Respectively set the real and imag parameters passed to this factory method to the attributes
|
|
// of the same name on options.
|
|
options.real = Some(real);
|
|
options.imag = Some(imag);
|
|
options.parent = constraints_copy;
|
|
PeriodicWave::Constructor(cx, self.global().as_window(), None, self, &options)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createanalyser>
|
|
fn CreateAnalyser(&self, cx: &mut JSContext) -> Fallible<DomRoot<AnalyserNode>> {
|
|
AnalyserNode::new(
|
|
cx,
|
|
self.global().as_window(),
|
|
self,
|
|
&AnalyserOptions::empty(),
|
|
)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbiquadfilter>
|
|
fn CreateBiquadFilter(&self, cx: &mut JSContext) -> Fallible<DomRoot<BiquadFilterNode>> {
|
|
BiquadFilterNode::new(
|
|
cx,
|
|
self.global().as_window(),
|
|
self,
|
|
&BiquadFilterOptions::empty(),
|
|
)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createstereopanner>
|
|
fn CreateStereoPanner(&self, cx: &mut JSContext) -> Fallible<DomRoot<StereoPannerNode>> {
|
|
StereoPannerNode::new(
|
|
cx,
|
|
self.global().as_window(),
|
|
self,
|
|
&StereoPannerOptions::empty(),
|
|
)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createconstantsource>
|
|
fn CreateConstantSource(&self, cx: &mut JSContext) -> Fallible<DomRoot<ConstantSourceNode>> {
|
|
ConstantSourceNode::new(
|
|
cx,
|
|
self.global().as_window(),
|
|
self,
|
|
&ConstantSourceOptions::empty(),
|
|
)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createchannelmerger>
|
|
fn CreateChannelMerger(
|
|
&self,
|
|
cx: &mut JSContext,
|
|
count: u32,
|
|
) -> Fallible<DomRoot<ChannelMergerNode>> {
|
|
let mut opts = ChannelMergerOptions::empty();
|
|
opts.numberOfInputs = count;
|
|
ChannelMergerNode::new(cx, self.global().as_window(), self, &opts)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createchannelsplitter>
|
|
fn CreateChannelSplitter(
|
|
&self,
|
|
cx: &mut JSContext,
|
|
count: u32,
|
|
) -> Fallible<DomRoot<ChannelSplitterNode>> {
|
|
let mut opts = ChannelSplitterOptions::empty();
|
|
opts.numberOfOutputs = count;
|
|
ChannelSplitterNode::new(cx, self.global().as_window(), self, &opts)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer>
|
|
fn CreateBuffer(
|
|
&self,
|
|
cx: &mut JSContext,
|
|
number_of_channels: u32,
|
|
length: u32,
|
|
sample_rate: Finite<f32>,
|
|
) -> Fallible<DomRoot<AudioBuffer>> {
|
|
if number_of_channels == 0 ||
|
|
number_of_channels > MAX_CHANNEL_COUNT ||
|
|
length == 0 ||
|
|
*sample_rate <= 0.
|
|
{
|
|
return Err(Error::NotSupported(None));
|
|
}
|
|
Ok(AudioBuffer::new(
|
|
cx,
|
|
self.global().as_window(),
|
|
number_of_channels,
|
|
length,
|
|
*sample_rate,
|
|
None,
|
|
))
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffersource>
|
|
fn CreateBufferSource(&self, cx: &mut JSContext) -> Fallible<DomRoot<AudioBufferSourceNode>> {
|
|
AudioBufferSourceNode::new(
|
|
cx,
|
|
self.global().as_window(),
|
|
self,
|
|
&AudioBufferSourceOptions::empty(),
|
|
)
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-decodeaudiodata>
|
|
fn DecodeAudioData(
|
|
&self,
|
|
cx: &mut CurrentRealm,
|
|
audio_data: CustomAutoRooterGuard<ArrayBuffer>,
|
|
decode_success_callback: Option<Option<RootedCallback<DecodeSuccessCallback>>>,
|
|
decode_error_callback: Option<Option<RootedCallback<DecodeErrorCallback>>>,
|
|
) -> RootedPromise {
|
|
// Step 1. If this's relevant global object's associated Document is NOT fully active,
|
|
// return a promise rejected with "InvalidStateError".
|
|
if !self.global().as_window().Document().is_fully_active() {
|
|
let promise = Promise::new_in_realm(cx);
|
|
promise.reject_error(
|
|
cx,
|
|
Error::InvalidState(Some("Audio context's document is not fully active.".into())),
|
|
);
|
|
return promise;
|
|
}
|
|
|
|
// Step 2. Let promise be a new promise.
|
|
let promise = Promise::new_in_realm(cx);
|
|
|
|
// flatten the optionally nullable callbacks
|
|
let decode_success_callback = decode_success_callback.flatten();
|
|
let decode_error_callback = decode_error_callback.flatten();
|
|
|
|
let audio_data = HeapBufferSource::<ArrayBufferU8>::from_view(cx, audio_data);
|
|
|
|
// Step 3. If audio_data is NOT detached, execute the following steps:
|
|
// - Append promise to [[pending promises]].
|
|
// - Detach the audio_data ArrayBuffer. If this operation throws, jump to step 4.1.
|
|
// - Queue a decoding operation to be performed on another thread.
|
|
let audio_data = if !audio_data.is_detached_buffer(cx) {
|
|
audio_data
|
|
.get_typed_array()
|
|
.ok()
|
|
.and_then(|buffer| buffer.to_vec())
|
|
.filter(|_| audio_data.detach_buffer(cx))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
if let Some(audio_data) = audio_data {
|
|
let uuid = Uuid::new_v4().simple().to_string();
|
|
let uuid_ = uuid.clone();
|
|
self.decode_resolvers.safe_borrow_mut(cx.no_gc()).insert(
|
|
uuid.clone(),
|
|
DecodeResolver {
|
|
promise: promise.to_traced(),
|
|
success_callback: decode_success_callback.map(|callback| callback.to_traced()),
|
|
error_callback: decode_error_callback.map(|callback| callback.to_traced()),
|
|
},
|
|
);
|
|
let decoded_audio = Arc::new(Mutex::new(Vec::new()));
|
|
let decoded_audio_ = decoded_audio.clone();
|
|
let decoded_audio__ = decoded_audio.clone();
|
|
// servo-media returns an audio channel position along
|
|
// with the AudioDecoderCallback progress callback, which
|
|
// may not be the same as the index of the decoded_audio
|
|
// Vec.
|
|
let channels = Arc::new(Mutex::new(HashMap::new()));
|
|
let this = Trusted::new(self);
|
|
let this_ = this.clone();
|
|
let task_source = self
|
|
.global()
|
|
.task_manager()
|
|
.dom_manipulation_task_source()
|
|
.to_sendable();
|
|
let task_source_clone = task_source.clone();
|
|
let callbacks = AudioDecoderCallbacksBuilder::default()
|
|
.ready(move |channel_count| {
|
|
decoded_audio
|
|
.lock()
|
|
.unwrap()
|
|
.resize(channel_count as usize, Vec::new());
|
|
})
|
|
.progress(move |buffer, channel_pos_mask| {
|
|
let mut decoded_audio = decoded_audio_.lock().unwrap();
|
|
let mut channels = channels.lock().unwrap();
|
|
let channel = match channels.entry(channel_pos_mask) {
|
|
Entry::Occupied(entry) => *entry.get(),
|
|
Entry::Vacant(entry) => {
|
|
let x = (channel_pos_mask as f32).log2() as usize;
|
|
*entry.insert(x)
|
|
},
|
|
};
|
|
decoded_audio[channel].extend_from_slice((*buffer).as_ref());
|
|
})
|
|
.eos(move || {
|
|
task_source.queue(task!(audio_decode_eos: move |cx| {
|
|
let this = this.root();
|
|
let decoded_audio = decoded_audio__.lock().unwrap();
|
|
let length = if !decoded_audio.is_empty() {
|
|
decoded_audio[0].len()
|
|
} else {
|
|
0
|
|
};
|
|
let buffer = AudioBuffer::new(
|
|
cx,
|
|
this.global().as_window(),
|
|
decoded_audio.len() as u32 /* number of channels */,
|
|
length as u32,
|
|
this.sample_rate,
|
|
Some(decoded_audio.as_slice()),
|
|
);
|
|
rooted!(&in(cx) let resolver = this
|
|
.decode_resolvers
|
|
.safe_borrow_mut(cx.no_gc())
|
|
.remove(&uuid_)
|
|
.expect("resolver should exist"));
|
|
let promise = resolver.promise.root(cx);
|
|
|
|
if let Some(callback) = &resolver.success_callback {
|
|
let _ = callback.Call__(cx, &buffer, ExceptionHandling::Report);
|
|
}
|
|
promise.resolve_native(cx, &buffer);
|
|
}));
|
|
})
|
|
.error(move |error| {
|
|
task_source_clone.queue(task!(audio_decode_eos: move |cx| {
|
|
let this = this_.root();
|
|
rooted!(&in(cx) let resolver = this
|
|
.decode_resolvers
|
|
.safe_borrow_mut(cx.no_gc())
|
|
.remove(&uuid)
|
|
.expect("resolver should exist"));
|
|
let promise = resolver.promise.root(cx);
|
|
|
|
if let Some(callback) = &resolver.error_callback {
|
|
let exception = DOMException::new(
|
|
cx,
|
|
&this.global(),
|
|
DOMErrorName::DataCloneError,
|
|
);
|
|
let _ =
|
|
callback.Call__(cx, &exception, ExceptionHandling::Report);
|
|
}
|
|
let error = cformat!("Audio decode error {:?}", error);
|
|
promise.reject_error(cx, Error::Type(error));
|
|
}));
|
|
})
|
|
.build();
|
|
self.audio_context_impl
|
|
.lock()
|
|
.unwrap()
|
|
.decode_audio_data(audio_data, callbacks);
|
|
} else {
|
|
// Step 4.
|
|
// Else, execute the following error steps:
|
|
// - Let error be a DataCloneError.
|
|
// - Reject promise with error, and remove it from [[pending promises]].
|
|
// - Queue a media element task to invoke errorCallback with error.
|
|
let exception = DOMException::new(cx, &self.global(), DOMErrorName::DataCloneError);
|
|
promise.reject_native(cx, &exception);
|
|
|
|
if let Some(callback) = decode_error_callback {
|
|
// Build the Task object using a unique uuid as a key to remove the callback resolver entry.
|
|
// Stash the callback with clone of uuid in the decode_resolvers map.
|
|
// Enqueue the task after the callback is stashed.
|
|
let uuid = Uuid::new_v4().simple().to_string();
|
|
let uuid_ = uuid.clone();
|
|
let this = Trusted::new(self);
|
|
let exception = Trusted::new(&*exception);
|
|
let task = task!(decode_audio_data_detached_buffer: move |cx| {
|
|
let this = this.root();
|
|
let exception = exception.root();
|
|
rooted!(&in(cx) let resolver = this
|
|
.decode_resolvers
|
|
.safe_borrow_mut(cx.no_gc())
|
|
.remove(&uuid)
|
|
.expect("resolver should exist"));
|
|
|
|
if let Some(callback) = &resolver.error_callback {
|
|
let _ = callback.Call__(
|
|
cx,
|
|
&exception,
|
|
ExceptionHandling::Report
|
|
);
|
|
}
|
|
});
|
|
self.decode_resolvers.safe_borrow_mut(cx.no_gc()).insert(
|
|
uuid_,
|
|
DecodeResolver {
|
|
promise: promise.to_traced(),
|
|
success_callback: None,
|
|
error_callback: Some(callback.to_traced()),
|
|
},
|
|
);
|
|
self.global()
|
|
.task_manager()
|
|
.media_element_task_source()
|
|
.queue(task);
|
|
}
|
|
}
|
|
|
|
// Step 5. Return promise.
|
|
promise
|
|
}
|
|
|
|
/// <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createiirfilter>
|
|
fn CreateIIRFilter(
|
|
&self,
|
|
cx: &mut JSContext,
|
|
feedforward: Vec<Finite<f64>>,
|
|
feedback: Vec<Finite<f64>>,
|
|
) -> Fallible<DomRoot<IIRFilterNode>> {
|
|
let opts = IIRFilterOptions {
|
|
parent: AudioNodeOptions::empty(),
|
|
feedback,
|
|
feedforward,
|
|
};
|
|
IIRFilterNode::new(cx, self.global().as_window(), self, &opts)
|
|
}
|
|
}
|
|
|
|
impl Convert<AudioContextOptions> for BaseAudioContextOptions {
|
|
fn convert(self) -> AudioContextOptions {
|
|
match self {
|
|
BaseAudioContextOptions::AudioContext(options) => {
|
|
AudioContextOptions::RealTimeAudioContext(options)
|
|
},
|
|
BaseAudioContextOptions::OfflineAudioContext(options) => {
|
|
AudioContextOptions::OfflineAudioContext(options)
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Convert<AudioContextState> for ProcessingState {
|
|
fn convert(self) -> AudioContextState {
|
|
match self {
|
|
ProcessingState::Suspended => AudioContextState::Suspended,
|
|
ProcessingState::Running => AudioContextState::Running,
|
|
ProcessingState::Closed => AudioContextState::Closed,
|
|
}
|
|
}
|
|
}
|