Skip to main content

script/dom/audio/
offlineaudiocontext.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 std::cell::Cell;
6use std::rc::Rc;
7use std::sync::{Arc, Mutex, mpsc};
8use std::thread::Builder;
9
10use dom_struct::dom_struct;
11use js::context::JSContext;
12use js::realm::CurrentRealm;
13use js::rust::HandleObject;
14use script_bindings::cell::DomRefCell;
15use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
16use servo_base::id::PipelineId;
17use servo_media::audio::context::OfflineAudioContextOptions as ServoMediaOfflineAudioContextOptions;
18
19use crate::dom::audio::audiobuffer::{AudioBuffer, MAX_SAMPLE_RATE, MIN_SAMPLE_RATE};
20use crate::dom::audio::audionode::MAX_CHANNEL_COUNT;
21use crate::dom::audio::baseaudiocontext::{BaseAudioContext, BaseAudioContextOptions};
22use crate::dom::audio::offlineaudiocompletionevent::OfflineAudioCompletionEvent;
23use crate::dom::bindings::codegen::Bindings::BaseAudioContextBinding::BaseAudioContext_Binding::BaseAudioContextMethods;
24use crate::dom::bindings::codegen::Bindings::OfflineAudioContextBinding::{
25    OfflineAudioContextMethods, OfflineAudioContextOptions,
26};
27use crate::dom::bindings::error::{Error, Fallible};
28use crate::dom::bindings::inheritance::Castable;
29use crate::dom::bindings::num::Finite;
30use crate::dom::bindings::refcounted::Trusted;
31use crate::dom::bindings::reflector::DomGlobal;
32use crate::dom::bindings::root::DomRoot;
33use crate::dom::event::{Event, EventBubbles, EventCancelable};
34use crate::dom::promise::Promise;
35use crate::dom::window::Window;
36
37#[dom_struct]
38pub(crate) struct OfflineAudioContext {
39    context: BaseAudioContext,
40    channel_count: u32,
41    length: u32,
42    rendering_started: Cell<bool>,
43    #[conditional_malloc_size_of]
44    pending_rendering_promise: DomRefCell<Option<Rc<Promise>>>,
45}
46
47impl OfflineAudioContext {
48    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
49    fn new_inherited(
50        channel_count: u32,
51        length: u32,
52        sample_rate: f32,
53        pipeline_id: PipelineId,
54    ) -> Fallible<OfflineAudioContext> {
55        let options = ServoMediaOfflineAudioContextOptions {
56            channels: channel_count as u8,
57            length: length as usize,
58            sample_rate,
59        };
60        let context = BaseAudioContext::new_inherited(
61            BaseAudioContextOptions::OfflineAudioContext(options),
62            pipeline_id,
63        )?;
64        Ok(OfflineAudioContext {
65            context,
66            channel_count,
67            length,
68            rendering_started: Cell::new(false),
69            pending_rendering_promise: Default::default(),
70        })
71    }
72
73    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
74    fn new(
75        cx: &mut JSContext,
76        window: &Window,
77        proto: Option<HandleObject>,
78        channel_count: u32,
79        length: u32,
80        sample_rate: f32,
81    ) -> Fallible<DomRoot<OfflineAudioContext>> {
82        if channel_count > MAX_CHANNEL_COUNT ||
83            channel_count == 0 ||
84            length == 0 ||
85            !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sample_rate)
86        {
87            return Err(Error::NotSupported(None));
88        }
89        let pipeline_id = window.pipeline_id();
90        let context =
91            OfflineAudioContext::new_inherited(channel_count, length, sample_rate, pipeline_id)?;
92        Ok(reflect_dom_object_with_proto_and_cx(
93            Box::new(context),
94            window,
95            proto,
96            cx,
97        ))
98    }
99}
100
101impl OfflineAudioContextMethods<crate::DomTypeHolder> for OfflineAudioContext {
102    /// <https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-offlineaudiocontext>
103    fn Constructor(
104        cx: &mut JSContext,
105        window: &Window,
106        proto: Option<HandleObject>,
107        options: &OfflineAudioContextOptions,
108    ) -> Fallible<DomRoot<OfflineAudioContext>> {
109        OfflineAudioContext::new(
110            cx,
111            window,
112            proto,
113            options.numberOfChannels,
114            options.length,
115            *options.sampleRate,
116        )
117    }
118
119    /// <https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-offlineaudiocontext-numberofchannels-length-samplerate>
120    fn Constructor_(
121        cx: &mut JSContext,
122        window: &Window,
123        proto: Option<HandleObject>,
124        number_of_channels: u32,
125        length: u32,
126        sample_rate: Finite<f32>,
127    ) -> Fallible<DomRoot<OfflineAudioContext>> {
128        OfflineAudioContext::new(cx, window, proto, number_of_channels, length, *sample_rate)
129    }
130
131    // https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-oncomplete
132    event_handler!(complete, GetOncomplete, SetOncomplete);
133
134    /// <https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-length>
135    fn Length(&self) -> u32 {
136        self.length
137    }
138
139    /// <https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-startrendering>
140    fn StartRendering(&self, cx: &mut CurrentRealm) -> Rc<Promise> {
141        let promise = Promise::new_in_realm(cx);
142        if self.rendering_started.get() {
143            promise.reject_error_with_cx(cx, Error::InvalidState(None));
144            return promise;
145        }
146        self.rendering_started.set(true);
147
148        *self.pending_rendering_promise.borrow_mut() = Some(promise.clone());
149
150        let processed_audio = Arc::new(Mutex::new(Vec::new()));
151        let processed_audio_ = processed_audio.clone();
152        let (sender, receiver) = mpsc::channel();
153        let sender = Mutex::new(sender);
154        self.context
155            .audio_context_impl()
156            .lock()
157            .unwrap()
158            .set_eos_callback(Box::new(move |buffer| {
159                processed_audio_
160                    .lock()
161                    .unwrap()
162                    .extend_from_slice((*buffer).as_ref());
163                let _ = sender.lock().unwrap().send(());
164            }));
165
166        let this = Trusted::new(self);
167        let task_source = self
168            .global()
169            .task_manager()
170            .dom_manipulation_task_source()
171            .to_sendable();
172        Builder::new()
173            .name("OfflineACResolver".to_owned())
174            .spawn(move || {
175                let _ = receiver.recv();
176                task_source.queue(task!(resolve: move |cx| {
177                    let this = this.root();
178                    let processed_audio = processed_audio.lock().unwrap();
179                    let mut processed_audio: Vec<_> = processed_audio
180                        .chunks(this.length as usize)
181                        .map(|channel| channel.to_vec())
182                        .collect();
183                    // it can end up being empty if the task failed
184                    if processed_audio.len() != this.length as usize {
185                        processed_audio.resize(this.length as usize, Vec::new())
186                    }
187                    let buffer = AudioBuffer::new(
188                        cx,
189                        this.global().as_window(),
190                        this.channel_count,
191                        this.length,
192                        *this.context.SampleRate(),
193                        Some(processed_audio.as_slice()),
194                    );
195                    (*this.pending_rendering_promise.borrow_mut())
196                        .take()
197                        .unwrap()
198                        .resolve_native_with_cx(cx, &buffer);
199                    let global = &this.global();
200                    let window = global.as_window();
201                    let event = OfflineAudioCompletionEvent::new(cx, window,
202                                                                 atom!("complete"),
203                                                                 EventBubbles::DoesNotBubble,
204                                                                 EventCancelable::NotCancelable,
205                                                                 &buffer);
206                    event.upcast::<Event>().fire(cx, this.upcast());
207                }));
208            })
209            .unwrap();
210
211        if self
212            .context
213            .audio_context_impl()
214            .lock()
215            .unwrap()
216            .resume()
217            .is_none()
218        {
219            promise.reject_error_with_cx(
220                cx,
221                Error::Type(c"Could not start offline rendering".to_owned()),
222            );
223        }
224
225        promise
226    }
227}