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

Side by Side Diff: Source/modules/webaudio/AudioContext.h

Issue 625363004: Implement suspend/resume for AudioContext (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Update according to new webaudio spec Created 6 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, 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
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */ 23 */
24 24
25 #ifndef AudioContext_h 25 #ifndef AudioContext_h
26 #define AudioContext_h 26 #define AudioContext_h
27 27
28 #include "bindings/core/v8/ScriptPromise.h"
29 #include "bindings/core/v8/ScriptPromiseResolver.h"
28 #include "core/dom/ActiveDOMObject.h" 30 #include "core/dom/ActiveDOMObject.h"
29 #include "core/dom/DOMTypedArray.h" 31 #include "core/dom/DOMTypedArray.h"
30 #include "core/events/EventListener.h" 32 #include "core/events/EventListener.h"
31 #include "modules/EventTargetModules.h" 33 #include "modules/EventTargetModules.h"
32 #include "modules/webaudio/AsyncAudioDecoder.h" 34 #include "modules/webaudio/AsyncAudioDecoder.h"
33 #include "modules/webaudio/AudioDestinationNode.h" 35 #include "modules/webaudio/AudioDestinationNode.h"
34 #include "platform/audio/AudioBus.h" 36 #include "platform/audio/AudioBus.h"
35 #include "platform/heap/Handle.h" 37 #include "platform/heap/Handle.h"
36 #include "wtf/HashSet.h" 38 #include "wtf/HashSet.h"
37 #include "wtf/MainThread.h" 39 #include "wtf/MainThread.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 class WaveShaperNode; 73 class WaveShaperNode;
72 74
73 // AudioContext is the cornerstone of the web audio API and all AudioNodes are c reated from it. 75 // AudioContext is the cornerstone of the web audio API and all AudioNodes are c reated from it.
74 // For thread safety between the audio thread and the main thread, it has a rend ering graph locking mechanism. 76 // For thread safety between the audio thread and the main thread, it has a rend ering graph locking mechanism.
75 77
76 class AudioContext : public RefCountedGarbageCollectedWillBeGarbageCollectedFina lized<AudioContext>, public ActiveDOMObject, public EventTargetWithInlineData { 78 class AudioContext : public RefCountedGarbageCollectedWillBeGarbageCollectedFina lized<AudioContext>, public ActiveDOMObject, public EventTargetWithInlineData {
77 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<A udioContext>); 79 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<A udioContext>);
78 DEFINE_WRAPPERTYPEINFO(); 80 DEFINE_WRAPPERTYPEINFO();
79 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(AudioContext); 81 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(AudioContext);
80 public: 82 public:
83 // The state of an audio context. On creation, the state is Suspended. The state is Running if
84 // audio is being processed (audio graph is being pulled for data). The stat e is Closed if the
85 // audio context has been closed. The valid transitions are from Suspended to either Running or
86 // Closed; Running to Suspended or Closed. Once Closed, there are no valid t ransitions.
87 enum AudioContextState {
88 Suspended,
89 Running,
90 Closed
91 };
92
81 // Create an AudioContext for rendering to the audio hardware. 93 // Create an AudioContext for rendering to the audio hardware.
82 static AudioContext* create(Document&, ExceptionState&); 94 static AudioContext* create(Document&, ExceptionState&);
83 95
84 virtual ~AudioContext(); 96 virtual ~AudioContext();
85 97
86 virtual void trace(Visitor*) override; 98 virtual void trace(Visitor*) override;
87 99
88 bool isInitialized() const { return m_isInitialized; } 100 bool isInitialized() const { return m_isInitialized; }
89 bool isOfflineContext() { return m_isOfflineContext; } 101 bool isOfflineContext() { return m_isOfflineContext; }
90 102
91 // Document notification 103 // Document notification
92 virtual void stop() override final; 104 virtual void stop() override final;
93 virtual bool hasPendingActivity() const override; 105 virtual bool hasPendingActivity() const override;
94 106
95 AudioDestinationNode* destination() { return m_destinationNode.get(); } 107 AudioDestinationNode* destination() { return m_destinationNode.get(); }
96 size_t currentSampleFrame() const { return m_destinationNode->currentSampleF rame(); } 108 size_t currentSampleFrame() const { return m_destinationNode->currentSampleF rame(); }
97 double currentTime() const { return m_destinationNode->currentTime(); } 109 double currentTime() const { return m_destinationNode->currentTime(); }
98 float sampleRate() const { return m_destinationNode->sampleRate(); } 110 float sampleRate() const { return m_destinationNode->sampleRate(); }
111 String state() const;
99 112
100 AudioBuffer* createBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&); 113 AudioBuffer* createBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&);
101 114
102 // Asynchronous audio file data decoding. 115 // Asynchronous audio file data decoding.
103 void decodeAudioData(DOMArrayBuffer*, AudioBufferCallback*, AudioBufferCallb ack*, ExceptionState&); 116 void decodeAudioData(DOMArrayBuffer*, AudioBufferCallback*, AudioBufferCallb ack*, ExceptionState&);
104 117
105 AudioListener* listener() { return m_listener.get(); } 118 AudioListener* listener() { return m_listener.get(); }
106 119
107 // The AudioNode create methods are called on the main thread (from JavaScri pt). 120 // The AudioNode create methods are called on the main thread (from JavaScri pt).
108 AudioBufferSourceNode* createBufferSource(); 121 AudioBufferSourceNode* createBufferSource();
(...skipping 13 matching lines...) Expand all
122 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, ExceptionState &); 135 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, ExceptionState &);
123 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, ExceptionState&); 136 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, ExceptionState&);
124 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, size_t numberOfOutputChannels, ExceptionState&); 137 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, size_t numberOfOutputChannels, ExceptionState&);
125 ChannelSplitterNode* createChannelSplitter(ExceptionState&); 138 ChannelSplitterNode* createChannelSplitter(ExceptionState&);
126 ChannelSplitterNode* createChannelSplitter(size_t numberOfOutputs, Exception State&); 139 ChannelSplitterNode* createChannelSplitter(size_t numberOfOutputs, Exception State&);
127 ChannelMergerNode* createChannelMerger(ExceptionState&); 140 ChannelMergerNode* createChannelMerger(ExceptionState&);
128 ChannelMergerNode* createChannelMerger(size_t numberOfInputs, ExceptionState &); 141 ChannelMergerNode* createChannelMerger(size_t numberOfInputs, ExceptionState &);
129 OscillatorNode* createOscillator(); 142 OscillatorNode* createOscillator();
130 PeriodicWave* createPeriodicWave(DOMFloat32Array* real, DOMFloat32Array* ima g, ExceptionState&); 143 PeriodicWave* createPeriodicWave(DOMFloat32Array* real, DOMFloat32Array* ima g, ExceptionState&);
131 144
145 // Suspend/Resume
146 ScriptPromise suspendContext(ScriptState*);
147 ScriptPromise resumeContext(ScriptState*);
148
132 // When a source node has no more processing to do (has finished playing), t hen it tells the context to dereference it. 149 // When a source node has no more processing to do (has finished playing), t hen it tells the context to dereference it.
133 void notifyNodeFinishedProcessing(AudioNode*); 150 void notifyNodeFinishedProcessing(AudioNode*);
134 151
135 // Called at the start of each render quantum. 152 // Called at the start of each render quantum.
136 void handlePreRenderTasks(); 153 void handlePreRenderTasks();
137 154
138 // Called at the end of each render quantum. 155 // Called at the end of each render quantum.
139 void handlePostRenderTasks(); 156 void handlePostRenderTasks();
140 157
141 // Called periodically at the end of each render quantum to dereference fini shed source nodes. 158 // Called periodically at the end of each render quantum to dereference fini shed source nodes.
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 GC_PLUGIN_IGNORE("http://crbug.com/404527") 285 GC_PLUGIN_IGNORE("http://crbug.com/404527")
269 Vector<AudioNode*> m_finishedNodes; 286 Vector<AudioNode*> m_finishedNodes;
270 287
271 // List of source nodes. This is either accessed when the graph lock is 288 // List of source nodes. This is either accessed when the graph lock is
272 // held, or on the main thread when the audio thread has finished. 289 // held, or on the main thread when the audio thread has finished.
273 // Oilpan: This Vector holds connection references. We must call 290 // Oilpan: This Vector holds connection references. We must call
274 // AudioNode::makeConnection when we add an AudioNode to this, and must call 291 // AudioNode::makeConnection when we add an AudioNode to this, and must call
275 // AudioNode::breakConnection() when we remove an AudioNode from this. 292 // AudioNode::breakConnection() when we remove an AudioNode from this.
276 HeapVector<Member<AudioNode> > m_referencedNodes; 293 HeapVector<Member<AudioNode> > m_referencedNodes;
277 294
295 // Stop rendering the audio graph.
296 void stopRendering();
297
298 // Handle Promises for resume() and suspend()
299 void resolvePromisesForResume();
300 void resolvePromisesForResumeOnMainThread();
301
302 void resolvePromisesForSuspend();
303 void resolvePromisesForSuspendOnMainThread();
304
305 // Vector of promises created by resume(). It takes time to handle them, so we collect all of
306 // the promises here until they can be resolved or rejected.
307 Vector<RefPtr<ScriptPromiseResolver> > m_resumePromises;
308 // Like m_resumePromises but for suspend().
309 Vector<RefPtr<ScriptPromiseResolver> > m_suspendPromises;
310
311 // True if we're in the process of resolving promises for resume(). Resolvi ng can take some
312 // time and the audio context process loop is very fast, so we don't want to call resolve an
313 // excessive number of times.
314 bool m_isResolvingResumePromises;
315
278 class AudioNodeDisposer { 316 class AudioNodeDisposer {
279 public: 317 public:
280 explicit AudioNodeDisposer(AudioNode& node) : m_node(node) { } 318 explicit AudioNodeDisposer(AudioNode& node) : m_node(node) { }
281 ~AudioNodeDisposer(); 319 ~AudioNodeDisposer();
282 320
283 private: 321 private:
284 AudioNode& m_node; 322 AudioNode& m_node;
285 }; 323 };
286 HeapHashMap<WeakMember<AudioNode>, OwnPtr<AudioNodeDisposer> > m_liveNodes; 324 HeapHashMap<WeakMember<AudioNode>, OwnPtr<AudioNodeDisposer> > m_liveNodes;
287 325
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 // Only accessed in the audio thread. 370 // Only accessed in the audio thread.
333 // Oilpan: Since items are added to these vectors by the audio thread (not r egistered to Oilpan), 371 // Oilpan: Since items are added to these vectors by the audio thread (not r egistered to Oilpan),
334 // we cannot use HeapVectors. 372 // we cannot use HeapVectors.
335 GC_PLUGIN_IGNORE("http://crbug.com/404527") 373 GC_PLUGIN_IGNORE("http://crbug.com/404527")
336 Vector<AudioNode*> m_deferredBreakConnectionList; 374 Vector<AudioNode*> m_deferredBreakConnectionList;
337 375
338 Member<AudioBuffer> m_renderTarget; 376 Member<AudioBuffer> m_renderTarget;
339 377
340 bool m_isOfflineContext; 378 bool m_isOfflineContext;
341 379
380 AudioContextState m_contextState;
381 void setContextState(AudioContextState);
382
342 AsyncAudioDecoder m_audioDecoder; 383 AsyncAudioDecoder m_audioDecoder;
343 384
344 // Collection of nodes where the channel count mode has changed. We want the channel count mode 385 // Collection of nodes where the channel count mode has changed. We want the channel count mode
345 // to change in the pre- or post-rendering phase so as not to disturb the ru nning audio thread. 386 // to change in the pre- or post-rendering phase so as not to disturb the ru nning audio thread.
346 GC_PLUGIN_IGNORE("http://crbug.com/404527") 387 GC_PLUGIN_IGNORE("http://crbug.com/404527")
347 HashSet<AudioNode*> m_deferredCountModeChange; 388 HashSet<AudioNode*> m_deferredCountModeChange;
348 389
349 // This is considering 32 is large enough for multiple channels audio. 390 // This is considering 32 is large enough for multiple channels audio.
350 // It is somewhat arbitrary and could be increased if necessary. 391 // It is somewhat arbitrary and could be increased if necessary.
351 enum { MaxNumberOfChannels = 32 }; 392 enum { MaxNumberOfChannels = 32 };
352 }; 393 };
353 394
354 } // namespace blink 395 } // namespace blink
355 396
356 #endif // AudioContext_h 397 #endif // AudioContext_h
OLDNEW
« no previous file with comments | « LayoutTests/webaudio/audiocontext-suspend-resume-expected.txt ('k') | Source/modules/webaudio/AudioContext.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698