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/Task.h" | |
37 #include "platform/ThreadSafeFunctional.h" | |
34 #include "platform/audio/AudioUtilities.h" | 38 #include "platform/audio/AudioUtilities.h" |
39 #include "public/platform/Platform.h" | |
40 | |
35 | 41 |
36 namespace blink { | 42 namespace blink { |
37 | 43 |
38 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 44 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
39 { | 45 { |
40 // FIXME: add support for workers. | 46 // FIXME: add support for workers. |
41 if (!context || !context->isDocument()) { | 47 if (!context || !context->isDocument()) { |
42 exceptionState.throwDOMException( | 48 exceptionState.throwDOMException( |
43 NotSupportedError, | 49 NotSupportedError, |
44 "Workers are not supported."); | 50 "Workers are not supported."); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 + ", " + String::number(sampleRate) | 91 + ", " + String::number(sampleRate) |
86 + ")"); | 92 + ")"); |
87 } | 93 } |
88 | 94 |
89 audioContext->suspendIfNeeded(); | 95 audioContext->suspendIfNeeded(); |
90 return audioContext; | 96 return audioContext; |
91 } | 97 } |
92 | 98 |
93 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 99 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) |
94 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) | 100 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) |
101 , m_isRenderingStarted(false) | |
102 , m_totalRenderFrames(numberOfFrames) | |
95 { | 103 { |
96 } | 104 } |
97 | 105 |
98 OfflineAudioContext::~OfflineAudioContext() | 106 OfflineAudioContext::~OfflineAudioContext() |
99 { | 107 { |
100 } | 108 } |
101 | 109 |
110 DEFINE_TRACE(OfflineAudioContext) | |
111 { | |
112 visitor->trace(m_completeResolver); | |
113 AudioContext::trace(visitor); | |
114 } | |
115 | |
116 bool OfflineAudioContext::shouldSuspendNow() | |
117 { | |
118 ASSERT(!isMainThread()); | |
119 | |
120 // If there is any scheduled suspend at |currentSampleFrame|, the context | |
121 // should be suspended. | |
122 if (m_scheduledSuspends.isEmpty() || !m_scheduledSuspends.contains(currentSa mpleFrame())) | |
123 return false; | |
124 | |
125 return true; | |
126 } | |
127 | |
128 void OfflineAudioContext::resolvePendingSuspendPromises() | |
129 { | |
130 ASSERT(!isMainThread()); | |
131 | |
132 // Find a pending suspend at |currentSampleFrame| and resolve the associated | |
133 // promise on the main thread. | |
134 SuspendContainerMap::iterator it = m_scheduledSuspends.find(currentSampleFra me()); | |
135 if (m_scheduledSuspends.isEmpty() || it == m_scheduledSuspends.end()) | |
136 return; | |
137 | |
138 RefPtr<ScheduledSuspendContainer> suspendContainer = it->value; | |
139 m_scheduledSuspends.remove(it); | |
140 Platform::current()->mainThread()->postTask(FROM_HERE, | |
141 threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOnMain Thread, this, suspendContainer.release())); | |
142 } | |
143 | |
144 void OfflineAudioContext::fireCompletionEvent() | |
145 { | |
146 ASSERT(isMainThread()); | |
147 | |
148 // We set the state to closed here so that the oncomplete event handler sees | |
149 // that the context has been closed. | |
150 setContextState(Closed); | |
151 | |
152 AudioBuffer* renderedBuffer = renderTarget(); | |
153 | |
154 ASSERT(renderedBuffer); | |
155 if (!renderedBuffer) | |
156 return; | |
157 | |
158 // Avoid firing the event if the document has already gone away. | |
159 if (executionContext()) { | |
160 // Call the offline rendering completion event listener and resolve the | |
161 // promise too. | |
162 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
163 m_completeResolver->resolve(renderedBuffer); | |
164 } | |
165 } | |
166 | |
102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 167 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
103 { | 168 { |
169 ASSERT(isMainThread()); | |
170 | |
104 // Calling close() on an OfflineAudioContext is not supported/allowed, | 171 // Calling close() on an OfflineAudioContext is not supported/allowed, |
105 // but it might well have been stopped by its execution context. | 172 // but it might well have been stopped by its execution context. |
106 if (isContextClosed()) { | 173 if (isContextClosed()) { |
107 return ScriptPromise::rejectWithDOMException( | 174 return ScriptPromise::rejectWithDOMException( |
108 scriptState, | 175 scriptState, |
109 DOMException::create( | 176 DOMException::create( |
110 InvalidStateError, | 177 InvalidStateError, |
111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 178 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); |
112 } | 179 } |
113 | 180 |
114 if (m_offlineResolver) { | 181 if (m_completeResolver) { |
115 // Can't call startRendering more than once. Return a rejected promise now. | 182 // Can't call startRendering more than once. Return a rejected promise now. |
116 return ScriptPromise::rejectWithDOMException( | 183 return ScriptPromise::rejectWithDOMException( |
117 scriptState, | 184 scriptState, |
118 DOMException::create( | 185 DOMException::create( |
119 InvalidStateError, | 186 InvalidStateError, |
120 "cannot call startRendering more than once")); | 187 "cannot call startRendering more than once")); |
121 } | 188 } |
122 | 189 |
123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 190 // If the context is not in the suspended state, reject the promise. |
124 startRendering(); | 191 if (contextState() != AudioContextState::Suspended) { |
125 return m_offlineResolver->promise(); | 192 return ScriptPromise::rejectWithDOMException( |
193 scriptState, | |
194 DOMException::create( | |
195 InvalidStateError, | |
196 "cannot startRendering when an OfflineAudioContext is not in a s uspended state")); | |
197 } | |
198 | |
199 m_completeResolver = ScriptPromiseResolver::create(scriptState); | |
200 | |
201 // Start rendering and return the promise. | |
202 m_isRenderingStarted = true; | |
203 setContextState(Running); | |
204 destination()->audioDestinationHandler().startRendering(); | |
205 | |
206 return m_completeResolver->promise(); | |
207 } | |
208 | |
209 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when) | |
210 { | |
211 ASSERT(isMainThread()); | |
212 ASSERT(destination()->audioDestinationHandler().offlineRenderThread()); | |
213 | |
214 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
215 ScriptPromise promise = resolver->promise(); | |
216 | |
217 // The specified suspend time is negative, reject the promise. | |
218 if (when < 0) { | |
219 resolver->reject(DOMException::create(InvalidStateError, | |
220 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
221 return promise; | |
222 } | |
223 | |
224 // Quantize the suspend time to the render quantum boundary. | |
225 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when); | |
226 | |
227 // The specified suspend time is in the past, reject the promise. | |
228 if (quantizedFrame < currentSampleFrame()) { | |
229 resolver->reject(DOMException::create(InvalidStateError, | |
230 "cannot schedule a suspend at frame " + String::number(quantizedFram e) + | |
231 " (" + String::number(when) + " seconds) because it is earlier than the current frame of " + | |
232 String::number(currentSampleFrame()))); | |
233 return promise; | |
234 } | |
235 | |
236 // The suspend time should be earlier than the total render frame. If the | |
237 // requested suspension time is equal to the total render frame, the promise | |
238 // will be rejected. | |
239 if (m_totalRenderFrames <= quantizedFrame) { | |
240 resolver->reject(DOMException::create(InvalidStateError, | |
241 "cannot schedule a suspend at frame " + String::number(quantizedFram e) + | |
242 " (" + String::number(when) + " seconds) because it is greater than or equal to the total render duration of " + | |
243 String::number(m_totalRenderFrames) + " frames")); | |
244 return promise; | |
245 } | |
246 | |
247 RefPtr<ScheduledSuspendContainer> suspendContainer = ScheduledSuspendContain er::create(when, quantizedFrame, resolver); | |
248 | |
249 // Validate the suspend and append if necessary on the render thread. | |
250 destination()->audioDestinationHandler().offlineRenderThread()->postTask(FRO M_HERE, | |
251 threadSafeBind(&OfflineAudioContext::checkDuplicateSuspend, this, suspen dContainer)); | |
252 | |
253 return promise; | |
254 } | |
255 | |
256 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te) | |
257 { | |
258 ASSERT(isMainThread()); | |
259 | |
260 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
261 ScriptPromise promise = resolver->promise(); | |
262 | |
263 // If the context is not in a suspended state, reject the promise. | |
264 if (contextState() != AudioContextState::Suspended) { | |
265 resolver->reject(DOMException::create(InvalidStateError, | |
266 "cannot resume a context that is not suspended")); | |
267 return promise; | |
268 } | |
269 | |
270 // If the rendering has not started, reject the promise. | |
271 if (!m_isRenderingStarted) { | |
272 resolver->reject(DOMException::create(InvalidStateError, | |
273 "cannot resume a context that has not started")); | |
274 return promise; | |
275 } | |
276 | |
277 // If the context is suspended, resume rendering by setting the state to | |
278 // "Running." and calling startRendering(). Note that resuming is possible | |
279 // only after the rendering started. | |
280 setContextState(Running); | |
281 destination()->audioDestinationHandler().startRendering(); | |
282 | |
283 // Resolve the promise immediately. | |
284 resolver->resolve(); | |
285 | |
286 return promise; | |
287 } | |
288 | |
289 void OfflineAudioContext::checkDuplicateSuspend(PassRefPtr<ScheduledSuspendConta iner> suspendContainer) | |
290 { | |
291 ASSERT(!isMainThread()); | |
292 | |
293 // If there is a duplicate suspension at the same quantize frame, reject the | |
294 // promise. The rejection of promise should happen in the main thread, so we | |
295 // post a task to it. | |
296 if (m_scheduledSuspends.contains(suspendContainer->suspendFrame())) { | |
297 Platform::current()->mainThread()->postTask(FROM_HERE, | |
298 threadSafeBind(&OfflineAudioContext::rejectSuspendPromiseOnMainThrea d, this, suspendContainer)); | |
299 return; | |
300 } | |
301 | |
302 // If the duplicate check passes, we can add the container safely here in | |
303 // the render thread. | |
304 m_scheduledSuspends.add(suspendContainer->suspendFrame(), suspendContainer); | |
305 } | |
306 | |
307 void OfflineAudioContext::rejectSuspendPromiseOnMainThread(PassRefPtr<ScheduledS uspendContainer> suspendContainer) | |
308 { | |
309 ASSERT(isMainThread()); | |
310 | |
311 suspendContainer->resolver()->reject(DOMException::create(InvalidStateError, | |
312 "cannot schedule more than one suspend at frame " + | |
313 String::number(suspendContainer->suspendFrame()) + " (" + | |
314 String::number(suspendContainer->suspendTime()) + " seconds)")); | |
315 } | |
316 | |
317 void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread(PassRefPtr<S cheduledSuspendContainer> suspendContainer) | |
318 { | |
319 ASSERT(isMainThread()); | |
320 AutoLocker locker(this); | |
yhirano
2015/06/26 02:29:09
Can you tell me why this lock is needed here? Ther
| |
321 | |
322 // Suspend the context first. This will fire onstatechange event. | |
323 setContextState(Suspended); | |
324 | |
325 suspendContainer->resolver()->resolve(); | |
326 } | |
327 | |
328 ScheduledSuspendContainer::ScheduledSuspendContainer(double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
329 : m_suspendTime(suspendTime) | |
330 , m_suspendFrame(suspendFrame) | |
331 , m_resolver(resolver) | |
332 { | |
333 ASSERT(isMainThread()); | |
334 } | |
335 | |
336 ScheduledSuspendContainer::~ScheduledSuspendContainer() | |
337 { | |
338 ASSERT(isMainThread()); | |
339 } | |
340 | |
341 PassRefPtr<ScheduledSuspendContainer> ScheduledSuspendContainer::create(double s uspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> r esolver) | |
342 { | |
343 return new ScheduledSuspendContainer(suspendTime, suspendFrame, resolver); | |
yhirano
2015/06/26 02:29:09
+adoptRef
| |
344 } | |
345 | |
346 bool ScheduledSuspendContainer::shouldSuspendAtFrame(size_t whenFrame) const | |
347 { | |
348 if (m_suspendFrame != whenFrame) | |
349 return false; | |
350 | |
351 return true; | |
126 } | 352 } |
127 | 353 |
128 } // namespace blink | 354 } // namespace blink |
129 | 355 |
130 #endif // ENABLE(WEB_AUDIO) | 356 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |