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

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: Fixed a LO test and minor nits 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 // FIXME: What should be done to trace members in OfflineAudioContext?
haraken 2015/06/17 06:46:08 This trace method looks correct. You can just trac
hongchan 2015/06/17 20:10:39 It produces error during the compilation. Probably
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. Marked
162 // promises will be resolved later. Note that duplicate entries in the
163 // suspend list are prohibited so it returns immediately when a valid
164 // suspend is found. This duplicate check is done by
165 // |suspendOfflineRendering|.
166 size_t nowFrame = currentSampleFrame();
167 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
168 if (m_scheduledSuspends.at(index)->shouldSuspendAt(nowFrame)) {
169 m_scheduledSuspends.at(index)->markAsPending();
170 return true;
171 }
172 }
173
174 return false;
175 }
176
177 void OfflineAudioContext::resolvePendingSuspendPromises()
178 {
179 ASSERT(!isMainThread());
180
181 // Resolve promises marked as 'pending'.
182 if (m_scheduledSuspends.size() > 0) {
183 Platform::current()->mainThread()->postTask(FROM_HERE,
184 threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOn MainThread, this));
185 }
186 }
187
188 void OfflineAudioContext::fireCompletionEvent()
189 {
190 ASSERT(isMainThread());
191 if (!isMainThread())
haraken 2015/06/17 06:46:08 Basically it is not a good idea to add both ASSERT
hongchan 2015/06/17 20:10:39 Done.
192 return;
193
194 // We set the state to closed here so that the oncomplete event handler sees
195 // that the context has been closed.
196 setContextState(Closed);
197
198 AudioBuffer* renderedBuffer = renderTarget().get();
199
200 ASSERT(renderedBuffer);
201 if (!renderedBuffer)
202 return;
203
204 // Avoid firing the event if the document has already gone away.
205 if (executionContext()) {
206 // Call the offline rendering completion event listener and resolve the
207 // promise too.
208 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
209 m_completeResolver->resolve(renderedBuffer);
210 }
211 }
212
102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) 213 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e)
103 { 214 {
215 ASSERT(isMainThread());
216
104 // Calling close() on an OfflineAudioContext is not supported/allowed, 217 // Calling close() on an OfflineAudioContext is not supported/allowed,
105 // but it might well have been stopped by its execution context. 218 // but it might well have been stopped by its execution context.
106 if (isContextClosed()) { 219 if (isContextClosed()) {
107 return ScriptPromise::rejectWithDOMException( 220 return ScriptPromise::rejectWithDOMException(
108 scriptState, 221 scriptState,
109 DOMException::create( 222 DOMException::create(
110 InvalidStateError, 223 InvalidStateError,
111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); 224 "cannot call startRendering on an OfflineAudioContext in a stopp ed state."));
112 } 225 }
113 226
114 if (m_offlineResolver) { 227 if (m_completeResolver) {
115 // Can't call startRendering more than once. Return a rejected promise now. 228 // Can't call startRendering more than once. Return a rejected promise now.
116 return ScriptPromise::rejectWithDOMException( 229 return ScriptPromise::rejectWithDOMException(
117 scriptState, 230 scriptState,
118 DOMException::create( 231 DOMException::create(
119 InvalidStateError, 232 InvalidStateError,
120 "cannot call startRendering more than once")); 233 "cannot call startRendering more than once"));
121 } 234 }
122 235
123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); 236 m_completeResolver = ScriptPromiseResolver::create(scriptState);
yhirano 2015/06/17 01:07:50 Shouldn't this statement be placed after the L239-
hongchan 2015/06/17 20:10:39 Done.
124 startRendering(); 237
125 return m_offlineResolver->promise(); 238 // If the context is not in the suspended state, reject the promise.
239 if (contextState() != AudioContextState::Suspended) {
240 return ScriptPromise::rejectWithDOMException(
241 scriptState,
242 DOMException::create(
243 InvalidStateError,
244 "cannot startRendering when an OfflineAudioContext is not in a s uspended state"));
245 }
246
247 // Start rendering and return the promise.
248 m_isRenderingStarted = true;
249 setContextState(Running);
250 destination()->audioDestinationHandler().startRendering();
251 return m_completeResolver->promise();
252 }
253
254 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when)
255 {
256 ASSERT(isMainThread());
yhirano 2015/06/17 01:07:50 if (state == closed) { return Promise.reject(...)
hongchan 2015/06/17 20:10:39 1) OfflineAudioContext does not have an explicit c
yhirano 2015/06/18 13:40:52 I see, thanks!
257
258 // The specified suspend time is negative, reject the promise.
259 if (when < 0) {
260 return ScriptPromise::rejectWithDOMException(
261 scriptState,
262 DOMException::create(
263 InvalidStateError,
264 "negative suspend time (" + String::number(when) + ") is not allowed "));
265 }
266
267 // Quantize the suspend time to the rendering block boundary.
268 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when);
269
270 // The specified suspend time is in the past, reject the promise.
271 if (quantizedFrame < currentSampleFrame()) {
272 return ScriptPromise::rejectWithDOMException(
273 scriptState,
274 DOMException::create(
275 InvalidStateError,
276 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
277 " (" + String::number(when) + " seconds) because it is earlier than the current frame of " +
278 String::number(currentSampleFrame())));
279 }
280
281 // The suspend time should be earlier than the total render frame. If the
282 // requested suspension time is equal to the total render frame, the promise
283 // will be rejected.
284 if (m_totalRenderFrames <= quantizedFrame) {
285 return ScriptPromise::rejectWithDOMException(
286 scriptState,
287 DOMException::create(
288 InvalidStateError,
289 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
290 " (" + String::number(when) + " seconds) because it is greater than or equal to the total render duration of " +
291 String::number(m_totalRenderFrames) + " frames"));
292 }
293
294 // If there is a duplicate suspension at the same quantize frame, reject the
295 // promise.
296 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
297 if (m_scheduledSuspends.at(index)->shouldSuspendAt(quantizedFrame)) {
298 return ScriptPromise::rejectWithDOMException(
299 scriptState,
300 DOMException::create(
301 InvalidStateError,
302 "cannot schedule more than one suspend at frame " + String::numb er(quantizedFrame) +
303 " (" + String::number(when) + " seconds)"));
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);
Raymond Toy 2015/06/17 15:56:10 destination()->audioDestinationHandler().startRend
hongchan 2015/06/17 20:10:39 Yes. I agree. However, it didn't matter because st
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 a context that is not suspended"));
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 a 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 setContextState(Running);
342
343 destination()->audioDestinationHandler().startRendering();
344
345 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
346 ScriptPromise promise = resolver->promise();
347
348 // Resolve the promise immediately.
349 resolver->resolve();
350
351 return promise;
352 }
353
354 void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread()
355 {
356 ASSERT(isMainThread());
357 AutoLocker locker(this);
haraken 2015/06/17 06:46:09 Why do you need this lock?
hongchan 2015/06/17 20:10:39 When we resolve the suspend promise, the script ca
358
359 // Suspend the context first. This will fire onstatechange event.
360 setContextState(Suspended);
361
362 // FIXME: is removing elements efficient? What if there are 10K suspends?
haraken 2015/06/17 06:46:08 FIXME => TODO
hongchan 2015/06/17 20:10:39 Done.
363 // Resolve any pending suspend and remove it from the list.
364 bool pendingPromiseResolved = false;
365 for (unsigned index = 0; index < m_scheduledSuspends.size();) {
366 if (m_scheduledSuspends.at(index)->isPending()) {
367
368 // We should never have more than one pending suspend at any time.
369 ASSERT(!pendingPromiseResolved);
370
371 m_scheduledSuspends.at(index)->resolver()->resolve();
372 m_scheduledSuspends.remove(index);
373
374 pendingPromiseResolved = true;
375 } else {
376 ++index;
377 }
378 }
126 } 379 }
127 380
128 } // namespace blink 381 } // namespace blink
129 382
130 #endif // ENABLE(WEB_AUDIO) 383 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698