Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: Source/modules/webaudio/OfflineAudioContext.cpp

Issue 1140723003: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Making Oilpan-compatible changes Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 DEFINE_TRACE(OfflineAudioContext)
109 {
110 visitor->trace(m_scheduledSuspends);
111 visitor->trace(m_completeResolver);
112 }
113
114 bool OfflineAudioContext::shouldSuspendNow()
yhirano 2015/06/18 13:40:52 Is m_scheduledSuspends protected by lock? I'm not
hongchan 2015/06/18 18:22:31 I understand your concern, but adding a lock in he
yhirano 2015/06/19 02:31:59 No, I don't think so. The following functions touc
hongchan 2015/06/19 22:15:39 @yhirano Although I agree with your idea of clear
115 {
116 ASSERT(!isMainThread());
117
118 // Suspend if necessary and mark the associated promise as pending. Marked
119 // promises will be resolved later. Note that duplicate entries in the
120 // suspend list are prohibited so it returns immediately when a valid
121 // suspend is found. This duplicate check is done by
122 // |suspendOfflineRendering|.
123 size_t nowFrame = currentSampleFrame();
124 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
125 if (m_scheduledSuspends.at(index)->shouldSuspendAt(nowFrame)) {
126 m_scheduledSuspends.at(index)->markAsPending();
127 return true;
128 }
129 }
130
131 return false;
132 }
133
134 void OfflineAudioContext::resolvePendingSuspendPromises()
135 {
136 ASSERT(!isMainThread());
137
138 // Resolve promises marked as 'pending'.
139 if (m_scheduledSuspends.size() > 0) {
yhirano 2015/06/18 13:40:52 ditto
hongchan 2015/06/18 18:22:31 Acknowledged.
140 Platform::current()->mainThread()->postTask(FROM_HERE,
141 threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOn MainThread, this));
142 }
143 }
144
145 void OfflineAudioContext::fireCompletionEvent()
146 {
147 ASSERT(isMainThread());
148
149 // We set the state to closed here so that the oncomplete event handler sees
150 // that the context has been closed.
151 setContextState(Closed);
152
153 AudioBuffer* renderedBuffer = renderTarget();
154
155 ASSERT(renderedBuffer);
156 if (!renderedBuffer)
157 return;
158
159 // Avoid firing the event if the document has already gone away.
160 if (executionContext()) {
161 // Call the offline rendering completion event listener and resolve the
162 // promise too.
163 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
164 m_completeResolver->resolve(renderedBuffer);
165 }
166 }
167
102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) 168 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e)
103 { 169 {
170 ASSERT(isMainThread());
171
104 // Calling close() on an OfflineAudioContext is not supported/allowed, 172 // Calling close() on an OfflineAudioContext is not supported/allowed,
105 // but it might well have been stopped by its execution context. 173 // but it might well have been stopped by its execution context.
106 if (isContextClosed()) { 174 if (isContextClosed()) {
107 return ScriptPromise::rejectWithDOMException( 175 return ScriptPromise::rejectWithDOMException(
108 scriptState, 176 scriptState,
109 DOMException::create( 177 DOMException::create(
110 InvalidStateError, 178 InvalidStateError,
111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); 179 "cannot call startRendering on an OfflineAudioContext in a stopp ed state."));
112 } 180 }
113 181
114 if (m_offlineResolver) { 182 if (m_completeResolver) {
115 // Can't call startRendering more than once. Return a rejected promise now. 183 // Can't call startRendering more than once. Return a rejected promise now.
116 return ScriptPromise::rejectWithDOMException( 184 return ScriptPromise::rejectWithDOMException(
117 scriptState, 185 scriptState,
118 DOMException::create( 186 DOMException::create(
119 InvalidStateError, 187 InvalidStateError,
120 "cannot call startRendering more than once")); 188 "cannot call startRendering more than once"));
121 } 189 }
122 190
123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); 191 // If the context is not in the suspended state, reject the promise.
124 startRendering(); 192 if (contextState() != AudioContextState::Suspended) {
125 return m_offlineResolver->promise(); 193 return ScriptPromise::rejectWithDOMException(
194 scriptState,
195 DOMException::create(
196 InvalidStateError,
197 "cannot startRendering when an OfflineAudioContext is not in a s uspended state"));
198 }
199
200 m_completeResolver = ScriptPromiseResolver::create(scriptState);
201
202 // Start rendering and return the promise.
203 m_isRenderingStarted = true;
204 setContextState(Running);
205 destination()->audioDestinationHandler().startRendering();
206
207 return m_completeResolver->promise();
208 }
209
210 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when)
211 {
212 ASSERT(isMainThread());
213
214 // The specified suspend time is negative, reject the promise.
215 if (when < 0) {
216 return ScriptPromise::rejectWithDOMException(
217 scriptState,
218 DOMException::create(
219 InvalidStateError,
220 "negative suspend time (" + String::number(when) + ") is not allowed "));
221 }
222
223 // Quantize the suspend time to the rendering block boundary.
224 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when);
225
226 // The specified suspend time is in the past, reject the promise.
227 if (quantizedFrame < currentSampleFrame()) {
228 return ScriptPromise::rejectWithDOMException(
229 scriptState,
230 DOMException::create(
231 InvalidStateError,
232 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
233 " (" + String::number(when) + " seconds) because it is earlier than the current frame of " +
234 String::number(currentSampleFrame())));
235 }
236
237 // The suspend time should be earlier than the total render frame. If the
238 // requested suspension time is equal to the total render frame, the promise
239 // will be rejected.
240 if (m_totalRenderFrames <= quantizedFrame) {
241 return ScriptPromise::rejectWithDOMException(
242 scriptState,
243 DOMException::create(
244 InvalidStateError,
245 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
246 " (" + String::number(when) + " seconds) because it is greater than or equal to the total render duration of " +
247 String::number(m_totalRenderFrames) + " frames"));
248 }
249
250 // If there is a duplicate suspension at the same quantize frame, reject the
251 // promise.
252 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
253 if (m_scheduledSuspends.at(index)->shouldSuspendAt(quantizedFrame)) {
254 return ScriptPromise::rejectWithDOMException(
255 scriptState,
256 DOMException::create(
257 InvalidStateError,
258 "cannot schedule more than one suspend at frame " + String::numb er(quantizedFrame) +
259 " (" + String::number(when) + " seconds)"));
260 }
261 }
262
263 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
264 ScriptPromise promise = resolver->promise();
265 m_scheduledSuspends.append(ScheduledSuspendContainer::create(quantizedFrame, resolver));
266
267 return promise;
268 }
269
270 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te)
271 {
272 ASSERT(isMainThread());
273
274 // If the context is not in a suspended state, reject the promise.
275 if (contextState() != AudioContextState::Suspended) {
276 return ScriptPromise::rejectWithDOMException(
277 scriptState,
278 DOMException::create(
279 InvalidStateError,
280 "cannot resume a context that is not suspended"));
281 }
282
283 // If the rendering has not started, reject the promise.
284 if (!m_isRenderingStarted) {
285 return ScriptPromise::rejectWithDOMException(
286 scriptState,
287 DOMException::create(
288 InvalidStateError,
289 "cannot resume a context that has not started"));
290 }
291
292 // If the context is suspended, resume rendering by calling startRendering()
293 // and set the state to "Running." Note that resuming is possible only after
294 // the rendering started.
295 setContextState(Running);
296
297 destination()->audioDestinationHandler().startRendering();
298
299 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
300 ScriptPromise promise = resolver->promise();
301
302 // Resolve the promise immediately.
303 resolver->resolve();
304
305 return promise;
306 }
307
308 void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread()
309 {
310 ASSERT(isMainThread());
311 AutoLocker locker(this);
312
313 // Suspend the context first. This will fire onstatechange event.
314 setContextState(Suspended);
315
316 // TODO: is removing elements efficient? What if there are 10K suspends?
317 // Resolve any pending suspend and remove it from the list.
318 bool pendingPromiseResolved = false;
319 for (unsigned index = 0; index < m_scheduledSuspends.size();) {
320 if (m_scheduledSuspends.at(index)->isPending()) {
321
322 // We should never have more than one pending suspend at any time.
323 ASSERT(!pendingPromiseResolved);
324
325 m_scheduledSuspends.at(index)->resolver()->resolve();
326 m_scheduledSuspends.remove(index);
327
328 pendingPromiseResolved = true;
329 } else {
330 ++index;
331 }
332 }
333 }
334
335 ScheduledSuspendContainer::ScheduledSuspendContainer(
336 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
337 : m_suspendFrame(suspendFrame)
338 , m_resolver(resolver)
339 , m_isPending(false)
340 {
341 }
342
343 ScheduledSuspendContainer::~ScheduledSuspendContainer()
344 {
345 }
346
347 DEFINE_TRACE(ScheduledSuspendContainer)
348 {
349 visitor->trace(m_resolver);
350 }
351
352 ScheduledSuspendContainer* ScheduledSuspendContainer::create(
353 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
354 {
355 return new ScheduledSuspendContainer(suspendFrame, resolver);
356 }
357
358 bool ScheduledSuspendContainer::shouldSuspendAt(size_t whenFrame) const
359 {
360 if (m_suspendFrame != whenFrame)
361 return false;
362
363 return true;
364 }
365
366 bool ScheduledSuspendContainer::isPending() const
367 {
368 return m_isPending;
369 }
370
371 void ScheduledSuspendContainer::markAsPending()
372 {
373 m_isPending = true;
126 } 374 }
127 375
128 } // namespace blink 376 } // namespace blink
129 377
130 #endif // ENABLE(WEB_AUDIO) 378 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698