1use 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::rust::HandleObject;
12use script_bindings::cell::DomRefCell;
13use script_bindings::reflector::reflect_dom_object_with_proto;
14use servo_base::id::PipelineId;
15use servo_media::audio::context::OfflineAudioContextOptions as ServoMediaOfflineAudioContextOptions;
16
17use crate::dom::audio::audiobuffer::{AudioBuffer, MAX_SAMPLE_RATE, MIN_SAMPLE_RATE};
18use crate::dom::audio::audionode::MAX_CHANNEL_COUNT;
19use crate::dom::audio::baseaudiocontext::{BaseAudioContext, BaseAudioContextOptions};
20use crate::dom::audio::offlineaudiocompletionevent::OfflineAudioCompletionEvent;
21use crate::dom::bindings::codegen::Bindings::BaseAudioContextBinding::BaseAudioContext_Binding::BaseAudioContextMethods;
22use crate::dom::bindings::codegen::Bindings::OfflineAudioContextBinding::{
23 OfflineAudioContextMethods, OfflineAudioContextOptions,
24};
25use crate::dom::bindings::error::{Error, Fallible};
26use crate::dom::bindings::inheritance::Castable;
27use crate::dom::bindings::num::Finite;
28use crate::dom::bindings::refcounted::Trusted;
29use crate::dom::bindings::reflector::DomGlobal;
30use crate::dom::bindings::root::DomRoot;
31use crate::dom::event::{Event, EventBubbles, EventCancelable};
32use crate::dom::promise::Promise;
33use crate::dom::window::Window;
34use crate::realms::InRealm;
35use crate::script_runtime::CanGc;
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 window: &Window,
76 proto: Option<HandleObject>,
77 channel_count: u32,
78 length: u32,
79 sample_rate: f32,
80 can_gc: CanGc,
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(
93 Box::new(context),
94 window,
95 proto,
96 can_gc,
97 ))
98 }
99}
100
101impl OfflineAudioContextMethods<crate::DomTypeHolder> for OfflineAudioContext {
102 fn Constructor(
104 window: &Window,
105 proto: Option<HandleObject>,
106 can_gc: CanGc,
107 options: &OfflineAudioContextOptions,
108 ) -> Fallible<DomRoot<OfflineAudioContext>> {
109 OfflineAudioContext::new(
110 window,
111 proto,
112 options.numberOfChannels,
113 options.length,
114 *options.sampleRate,
115 can_gc,
116 )
117 }
118
119 fn Constructor_(
121 window: &Window,
122 proto: Option<HandleObject>,
123 can_gc: CanGc,
124 number_of_channels: u32,
125 length: u32,
126 sample_rate: Finite<f32>,
127 ) -> Fallible<DomRoot<OfflineAudioContext>> {
128 OfflineAudioContext::new(
129 window,
130 proto,
131 number_of_channels,
132 length,
133 *sample_rate,
134 can_gc,
135 )
136 }
137
138 event_handler!(complete, GetOncomplete, SetOncomplete);
140
141 fn Length(&self) -> u32 {
143 self.length
144 }
145
146 fn StartRendering(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
148 let promise = Promise::new_in_current_realm(comp, can_gc);
149 if self.rendering_started.get() {
150 promise.reject_error(Error::InvalidState(None), can_gc);
151 return promise;
152 }
153 self.rendering_started.set(true);
154
155 *self.pending_rendering_promise.borrow_mut() = Some(promise.clone());
156
157 let processed_audio = Arc::new(Mutex::new(Vec::new()));
158 let processed_audio_ = processed_audio.clone();
159 let (sender, receiver) = mpsc::channel();
160 let sender = Mutex::new(sender);
161 self.context
162 .audio_context_impl()
163 .lock()
164 .unwrap()
165 .set_eos_callback(Box::new(move |buffer| {
166 processed_audio_
167 .lock()
168 .unwrap()
169 .extend_from_slice((*buffer).as_ref());
170 let _ = sender.lock().unwrap().send(());
171 }));
172
173 let this = Trusted::new(self);
174 let task_source = self
175 .global()
176 .task_manager()
177 .dom_manipulation_task_source()
178 .to_sendable();
179 Builder::new()
180 .name("OfflineACResolver".to_owned())
181 .spawn(move || {
182 let _ = receiver.recv();
183 task_source.queue(task!(resolve: move |cx| {
184 let this = this.root();
185 let processed_audio = processed_audio.lock().unwrap();
186 let mut processed_audio: Vec<_> = processed_audio
187 .chunks(this.length as usize)
188 .map(|channel| channel.to_vec())
189 .collect();
190 if processed_audio.len() != this.length as usize {
192 processed_audio.resize(this.length as usize, Vec::new())
193 }
194 let buffer = AudioBuffer::new(
195 cx,
196 this.global().as_window(),
197 this.channel_count,
198 this.length,
199 *this.context.SampleRate(),
200 Some(processed_audio.as_slice()),
201 );
202 (*this.pending_rendering_promise.borrow_mut())
203 .take()
204 .unwrap()
205 .resolve_native(&buffer, CanGc::from_cx(cx));
206 let global = &this.global();
207 let window = global.as_window();
208 let event = OfflineAudioCompletionEvent::new(window,
209 atom!("complete"),
210 EventBubbles::DoesNotBubble,
211 EventCancelable::NotCancelable,
212 &buffer, CanGc::from_cx(cx));
213 event.upcast::<Event>().fire(this.upcast(), CanGc::from_cx(cx));
214 }));
215 })
216 .unwrap();
217
218 if self
219 .context
220 .audio_context_impl()
221 .lock()
222 .unwrap()
223 .resume()
224 .is_none()
225 {
226 promise.reject_error(
227 Error::Type(c"Could not start offline rendering".to_owned()),
228 can_gc,
229 );
230 }
231
232 promise
233 }
234}