OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2012, Google Inc. All rights reserved. | 2 * Copyright (C) 2012, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 13 matching lines...) Expand all Loading... | |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 #if ENABLE(WEB_AUDIO) | 26 #if ENABLE(WEB_AUDIO) |
27 #include "modules/webaudio/OfflineAudioContext.h" | 27 #include "modules/webaudio/OfflineAudioContext.h" |
28 | 28 |
29 #include "bindings/core/v8/ExceptionMessages.h" | 29 #include "bindings/core/v8/ExceptionMessages.h" |
30 #include "bindings/core/v8/ExceptionState.h" | 30 #include "bindings/core/v8/ExceptionState.h" |
31 #include "core/dom/Document.h" | 31 #include "core/dom/Document.h" |
32 #include "core/dom/ExceptionCode.h" | 32 #include "core/dom/ExceptionCode.h" |
33 #include "core/dom/ExecutionContext.h" | 33 #include "core/dom/ExecutionContext.h" |
34 #include "modules/webaudio/OfflineAudioCompletionEvent.h" | |
35 #include "modules/webaudio/OfflineAudioDestinationNode.h" | |
36 #include "platform/ThreadSafeFunctional.h" | |
34 #include "platform/audio/AudioUtilities.h" | 37 #include "platform/audio/AudioUtilities.h" |
38 #include "public/platform/Platform.h" | |
35 | 39 |
36 namespace blink { | 40 namespace blink { |
37 | 41 |
38 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 42 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
39 { | 43 { |
40 // FIXME: add support for workers. | 44 // FIXME: add support for workers. |
41 if (!context || !context->isDocument()) { | 45 if (!context || !context->isDocument()) { |
42 exceptionState.throwDOMException( | 46 exceptionState.throwDOMException( |
43 NotSupportedError, | 47 NotSupportedError, |
44 "Workers are not supported."); | 48 "Workers are not supported."); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 + ", " + String::number(sampleRate) | 89 + ", " + String::number(sampleRate) |
86 + ")"); | 90 + ")"); |
87 } | 91 } |
88 | 92 |
89 audioContext->suspendIfNeeded(); | 93 audioContext->suspendIfNeeded(); |
90 return audioContext; | 94 return audioContext; |
91 } | 95 } |
92 | 96 |
93 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 97 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) |
94 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) | 98 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) |
99 , m_isRenderingStarted(false) | |
100 , m_totalRenderFrames(numberOfFrames) | |
95 { | 101 { |
96 } | 102 } |
97 | 103 |
98 OfflineAudioContext::~OfflineAudioContext() | 104 OfflineAudioContext::~OfflineAudioContext() |
99 { | 105 { |
100 } | 106 } |
101 | 107 |
108 // FIXME: What should be done to trace members in OfflineAudioContext? | |
hongchan
2015/06/12 17:51:44
Oilpan team's opinion is needed here.
| |
109 // DEFINE_TRACE(OfflineAudioContext) | |
110 // { | |
111 // visitor->trace(m_scheduledSuspends); | |
112 // visitor->trace(m_completeResolver); | |
113 // } | |
114 | |
115 OfflineAudioContext::ScheduledSuspendContainer::ScheduledSuspendContainer( | |
116 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
117 : m_suspendFrame(suspendFrame) | |
118 , m_resolver(resolver) | |
119 , m_isPending(false) | |
120 { | |
121 } | |
122 | |
123 OfflineAudioContext::ScheduledSuspendContainer::~ScheduledSuspendContainer() | |
124 { | |
125 } | |
126 | |
127 DEFINE_TRACE(OfflineAudioContext::ScheduledSuspendContainer) | |
128 { | |
129 visitor->trace(m_resolver); | |
130 } | |
131 | |
132 PassOwnPtr<OfflineAudioContext::ScheduledSuspendContainer> | |
133 OfflineAudioContext::ScheduledSuspendContainer::create( | |
134 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
135 { | |
136 return adoptPtr(new ScheduledSuspendContainer(suspendFrame, resolver)); | |
137 } | |
138 | |
139 bool OfflineAudioContext::ScheduledSuspendContainer::shouldSuspendAt(size_t when Frame) const | |
140 { | |
141 if (m_suspendFrame != whenFrame) | |
142 return false; | |
143 | |
144 return true; | |
145 } | |
146 | |
147 bool OfflineAudioContext::ScheduledSuspendContainer::isPending() const | |
148 { | |
149 return m_isPending; | |
150 } | |
151 | |
152 void OfflineAudioContext::ScheduledSuspendContainer::markAsPending() | |
153 { | |
154 m_isPending = true; | |
155 } | |
156 | |
157 bool OfflineAudioContext::shouldSuspendNow() | |
158 { | |
159 ASSERT(!isMainThread()); | |
160 | |
161 // Suspend if necessary and mark the associated promise as pending. Note | |
162 // that duplicate entries in the suspend list are prohibited so it returns | |
Raymond Toy
2015/06/12 21:11:37
Comment on who prohibits duplicate entries.
hongchan
2015/06/15 18:40:46
Done.
| |
163 // immediately when a valid suspend is found. | |
164 size_t nowFrame = currentSampleFrame(); | |
165 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) { | |
166 if (m_scheduledSuspends.at(index)->shouldSuspendAt(nowFrame)) { | |
167 m_scheduledSuspends.at(index)->markAsPending(); | |
168 return true; | |
169 } | |
170 } | |
171 | |
172 return false; | |
173 } | |
174 | |
175 void OfflineAudioContext::resolvePendingSuspendPromises() | |
176 { | |
177 ASSERT(!isMainThread()); | |
178 | |
179 // Resolve promises marked as 'pending'. | |
180 if (m_scheduledSuspends.size() > 0) { | |
181 Platform::current()->mainThread()->postTask(FROM_HERE, | |
182 threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOn MainThread, this)); | |
183 } | |
184 } | |
185 | |
186 void OfflineAudioContext::fireCompletionEvent() | |
187 { | |
188 ASSERT(isMainThread()); | |
189 if (!isMainThread()) | |
190 return; | |
191 | |
192 // We set the state to closed here so that the oncomplete event handler sees | |
193 // that the context has been closed. | |
194 setContextState(Closed); | |
195 | |
196 AudioBuffer* renderedBuffer = renderTarget().get(); | |
197 | |
198 ASSERT(renderedBuffer); | |
199 if (!renderedBuffer) | |
200 return; | |
201 | |
202 // Avoid firing the event if the document has already gone away. | |
203 if (executionContext()) { | |
204 // Call the offline rendering completion event listener and resolve the | |
205 // promise too. | |
206 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
207 m_completeResolver->resolve(renderedBuffer); | |
208 } | |
209 } | |
210 | |
102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 211 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
103 { | 212 { |
213 ASSERT(isMainThread()); | |
214 | |
215 // FIXME: This check causes crash on oac-detached-no-crash.html | |
216 // ASSERT(m_destinationNode); | |
217 | |
218 // AutoLocker locker(this); | |
Raymond Toy
2015/06/12 21:11:37
Fix these. Does the crash still happen?
hongchan
2015/06/15 18:40:46
Done.
| |
219 | |
104 // Calling close() on an OfflineAudioContext is not supported/allowed, | 220 // Calling close() on an OfflineAudioContext is not supported/allowed, |
105 // but it might well have been stopped by its execution context. | 221 // but it might well have been stopped by its execution context. |
106 if (isContextClosed()) { | 222 if (isContextClosed()) { |
107 return ScriptPromise::rejectWithDOMException( | 223 return ScriptPromise::rejectWithDOMException( |
108 scriptState, | 224 scriptState, |
109 DOMException::create( | 225 DOMException::create( |
110 InvalidStateError, | 226 InvalidStateError, |
111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 227 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); |
112 } | 228 } |
113 | 229 |
114 if (m_offlineResolver) { | 230 if (m_completeResolver) { |
115 // Can't call startRendering more than once. Return a rejected promise now. | 231 // Can't call startRendering more than once. Return a rejected promise now. |
116 return ScriptPromise::rejectWithDOMException( | 232 return ScriptPromise::rejectWithDOMException( |
117 scriptState, | 233 scriptState, |
118 DOMException::create( | 234 DOMException::create( |
119 InvalidStateError, | 235 InvalidStateError, |
120 "cannot call startRendering more than once")); | 236 "cannot call startRendering more than once")); |
121 } | 237 } |
122 | 238 |
123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 239 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
124 startRendering(); | 240 |
125 return m_offlineResolver->promise(); | 241 // If the context is not in the suspended state, reject the promise. |
242 if (contextState() != AudioContextState::Suspended) { | |
243 return ScriptPromise::rejectWithDOMException( | |
244 scriptState, | |
245 DOMException::create( | |
246 InvalidStateError, | |
247 "cannot startRendering when an OfflineAudioContext is not in a s uspended state")); | |
248 } | |
249 | |
250 // Start rendering and return the promise. | |
251 m_isRenderingStarted = true; | |
252 setContextState(Running); | |
253 destination()->audioDestinationHandler().startRendering(); | |
254 return m_completeResolver->promise(); | |
255 } | |
256 | |
257 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when) | |
258 { | |
259 ASSERT(isMainThread()); | |
260 | |
261 // AutoLocker locker(this); | |
Raymond Toy
2015/06/12 21:11:37
Remove this.
hongchan
2015/06/15 18:40:46
Done.
| |
262 | |
263 // The specified suspend time is negative, reject the promise. | |
264 if (when < 0) { | |
265 return ScriptPromise::rejectWithDOMException( | |
266 scriptState, | |
267 DOMException::create( | |
268 InvalidStateError, | |
269 "negative suspend time is not allowed")); | |
270 } | |
271 | |
272 // Quantize the suspend time to the rendering block boundary. | |
273 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when); | |
274 | |
275 // The specified suspend time is in the past, reject the promise. | |
276 if (quantizedFrame < currentSampleFrame()) { | |
277 return ScriptPromise::rejectWithDOMException( | |
278 scriptState, | |
279 DOMException::create( | |
280 InvalidStateError, | |
281 "cannot schedule a suspend in the past")); | |
Raymond Toy
2015/06/12 21:11:37
Is there a test for this?
hongchan
2015/06/15 18:40:46
I will add one in |oac-suspend-resume-basic.html|
| |
282 } | |
283 | |
284 // The suspend time should be earlier than the total render frame. If the | |
285 // requested suspension time is equal to the total render frame, the promise | |
286 // will be rejected. | |
287 if (m_totalRenderFrames <= quantizedFrame) { | |
288 return ScriptPromise::rejectWithDOMException( | |
289 scriptState, | |
290 DOMException::create( | |
291 InvalidStateError, | |
292 "cannot schedule a suspend beyond the total render duration")); | |
293 } | |
294 | |
295 // If there is a duplicate suspension at the same quantize frame, reject the | |
296 // promise. | |
297 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) { | |
298 if (m_scheduledSuspends.at(index)->shouldSuspendAt(quantizedFrame)) { | |
299 return ScriptPromise::rejectWithDOMException( | |
300 scriptState, | |
301 DOMException::create( | |
302 InvalidStateError, | |
303 "cannot schedule a suspend at the duplicate time")); | |
Raymond Toy
2015/06/12 21:11:37
Include the time in the message.
hongchan
2015/06/15 18:40:46
Done.
| |
304 } | |
305 } | |
306 | |
307 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
308 ScriptPromise promise = resolver->promise(); | |
309 m_scheduledSuspends.append(ScheduledSuspendContainer::create(quantizedFrame, resolver)); | |
310 | |
311 return promise; | |
312 } | |
313 | |
314 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te) | |
315 { | |
316 ASSERT(isMainThread()); | |
317 | |
318 AutoLocker locker(this); | |
319 | |
320 // If the context is not in a suspended state, reject the promise. | |
321 if (contextState() != AudioContextState::Suspended) { | |
322 return ScriptPromise::rejectWithDOMException( | |
323 scriptState, | |
324 DOMException::create( | |
325 InvalidStateError, | |
326 "cannot resume the context that is not suspended")); | |
Raymond Toy
2015/06/12 21:11:36
the -> a
hongchan
2015/06/15 18:40:46
Done.
| |
327 } | |
328 | |
329 // If the rendering has not started, reject the promise. | |
330 if (!m_isRenderingStarted) { | |
331 return ScriptPromise::rejectWithDOMException( | |
332 scriptState, | |
333 DOMException::create( | |
334 InvalidStateError, | |
335 "cannot resume the context that has not started")); | |
336 } | |
337 | |
338 // If the context is suspended, resume rendering by calling startRendering() | |
339 // and set the state to "Running." Note that resuming is possible only after | |
340 // the rendering started. | |
341 destination()->audioDestinationHandler().startRendering(); | |
342 setContextState(Running); | |
Raymond Toy
2015/06/12 21:11:37
Should the state be set for calling startRendering
hongchan
2015/06/15 18:40:46
Can you elaborate? I can swap the order of the sta
Raymond Toy
2015/06/15 20:42:13
Pretty much. I think setting the Running state bef
| |
343 | |
344 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
345 ScriptPromise promise = resolver->promise(); | |
346 | |
347 // Resolve the promise immediately. | |
348 resolver->resolve(); | |
349 | |
350 return promise; | |
351 } | |
352 | |
353 void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread() | |
354 { | |
355 ASSERT(isMainThread()); | |
356 AutoLocker locker(this); | |
357 | |
358 // Suspend the context first. This will fire onstatechange event. | |
359 setContextState(Suspended); | |
360 | |
361 // FIXME: is removing elements efficient? What if there are 10K suspends? | |
362 // Resolve any pending suspend and remove it from the list. | |
363 for (unsigned index = 0; index < m_scheduledSuspends.size();) { | |
364 if (m_scheduledSuspends.at(index)->isPending()) { | |
365 m_scheduledSuspends.at(index)->resolver()->resolve(); | |
366 m_scheduledSuspends.remove(index); | |
367 } else { | |
368 ++index; | |
369 } | |
370 } | |
126 } | 371 } |
127 | 372 |
128 } // namespace blink | 373 } // namespace blink |
129 | 374 |
130 #endif // ENABLE(WEB_AUDIO) | 375 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |