2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
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 ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
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 ON
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
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #ifndef AudioContext_h
26 #define AudioContext_h
28 #include "ActiveDOMObject.h"
29 #include "AsyncAudioDecoder.h"
31 #include "AudioDestinationNode.h"
32 #include "EventListener.h"
33 #include "EventTarget.h"
34 #include "HRTFDatabaseLoader.h"
35 #include <wtf/HashSet.h>
36 #include <wtf/MainThread.h>
37 #include <wtf/OwnPtr.h>
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RefCounted.h>
40 #include <wtf/RefPtr.h>
41 #include <wtf/ThreadSafeRefCounted.h>
42 #include <wtf/Threading.h>
43 #include <wtf/Vector.h>
44 #include <wtf/text/AtomicStringHash.h>
49 class AudioBufferCallback;
50 class AudioBufferSourceNode;
51 class MediaElementAudioSourceNode;
52 class MediaStreamAudioDestinationNode;
53 class MediaStreamAudioSourceNode;
54 class HTMLMediaElement;
55 class ChannelMergerNode;
56 class ChannelSplitterNode;
60 class AudioSummingJunction;
61 class BiquadFilterNode;
65 class DynamicsCompressorNode;
68 class ScriptProcessorNode;
72 // AudioContext is the cornerstone of the web audio API and all AudioNodes are created from it.
73 // For thread safety between the audio thread and the main thread, it has a rendering graph locking mechanism.
75 class AudioContext : public ActiveDOMObject, public ThreadSafeRefCounted<AudioContext>, public EventTarget {
77 // Create an AudioContext for rendering to the audio hardware.
78 static PassRefPtr<AudioContext> create(Document*, ExceptionCode&);
80 // Create an AudioContext for offline (non-realtime) rendering.
81 static PassRefPtr<AudioContext> createOfflineContext(Document*, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionCode&);
83 virtual ~AudioContext();
85 bool isInitialized() const;
87 bool isOfflineContext() { return m_isOfflineContext; }
89 // Returns true when initialize() was called AND all asynchronous initialization has completed.
90 bool isRunnable() const;
92 // Document notification
94 #if ENABLE(TIZEN_WEB_AUDIO)
97 Document* document() const; // ASSERTs if document no longer exists.
100 AudioDestinationNode* destination() { return m_destinationNode.get(); }
101 size_t currentSampleFrame() const { return m_destinationNode->currentSampleFrame(); }
102 double currentTime() const { return m_destinationNode->currentTime(); }
103 float sampleRate() const { return m_destinationNode->sampleRate(); }
104 unsigned long activeSourceCount() const { return static_cast<unsigned long>(m_activeSourceCount); }
105 #if ENABLE(TIZEN_WEB_AUDIO)
106 unsigned long activeScriptProcessorCount() const { return static_cast<unsigned long>(m_activeScriptProcessorCount); }
108 void incrementActiveSourceCount();
109 void decrementActiveSourceCount();
110 #if ENABLE(TIZEN_WEB_AUDIO)
111 void incrementActiveScriptProcessorCount();
112 void decrementActiveScriptProcessorCount();
114 PassRefPtr<AudioBuffer> createBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionCode&);
115 PassRefPtr<AudioBuffer> createBuffer(ArrayBuffer*, bool mixToMono, ExceptionCode&);
117 // Asynchronous audio file data decoding.
118 void decodeAudioData(ArrayBuffer*, PassRefPtr<AudioBufferCallback>, PassRefPtr<AudioBufferCallback>, ExceptionCode& ec);
120 AudioListener* listener() { return m_listener.get(); }
122 // The AudioNode create methods are called on the main thread (from JavaScript).
123 PassRefPtr<AudioBufferSourceNode> createBufferSource();
125 PassRefPtr<MediaElementAudioSourceNode> createMediaElementSource(HTMLMediaElement*, ExceptionCode&);
127 #if ENABLE(MEDIA_STREAM)
128 PassRefPtr<MediaStreamAudioSourceNode> createMediaStreamSource(MediaStream*, ExceptionCode&);
129 PassRefPtr<MediaStreamAudioDestinationNode> createMediaStreamDestination();
131 PassRefPtr<GainNode> createGain();
132 PassRefPtr<BiquadFilterNode> createBiquadFilter();
133 PassRefPtr<WaveShaperNode> createWaveShaper();
134 PassRefPtr<DelayNode> createDelay(ExceptionCode&);
135 PassRefPtr<DelayNode> createDelay(double maxDelayTime, ExceptionCode&);
136 PassRefPtr<PannerNode> createPanner();
137 PassRefPtr<ConvolverNode> createConvolver();
138 PassRefPtr<DynamicsCompressorNode> createDynamicsCompressor();
139 PassRefPtr<AnalyserNode> createAnalyser();
140 PassRefPtr<ScriptProcessorNode> createScriptProcessor(size_t bufferSize, ExceptionCode&);
141 PassRefPtr<ScriptProcessorNode> createScriptProcessor(size_t bufferSize, size_t numberOfInputChannels, ExceptionCode&);
142 PassRefPtr<ScriptProcessorNode> createScriptProcessor(size_t bufferSize, size_t numberOfInputChannels, size_t numberOfOutputChannels, ExceptionCode&);
143 PassRefPtr<ChannelSplitterNode> createChannelSplitter(ExceptionCode&);
144 PassRefPtr<ChannelSplitterNode> createChannelSplitter(size_t numberOfOutputs, ExceptionCode&);
145 PassRefPtr<ChannelMergerNode> createChannelMerger(ExceptionCode&);
146 PassRefPtr<ChannelMergerNode> createChannelMerger(size_t numberOfInputs, ExceptionCode&);
147 PassRefPtr<OscillatorNode> createOscillator();
148 PassRefPtr<WaveTable> createWaveTable(Float32Array* real, Float32Array* imag, ExceptionCode&);
150 // When a source node has no more processing to do (has finished playing), then it tells the context to dereference it.
151 void notifyNodeFinishedProcessing(AudioNode*);
153 // Called at the start of each render quantum.
154 void handlePreRenderTasks();
156 // Called at the end of each render quantum.
157 void handlePostRenderTasks();
159 // Called periodically at the end of each render quantum to dereference finished source nodes.
160 void derefFinishedSourceNodes();
162 // We schedule deletion of all marked nodes at the end of each realtime render quantum.
163 void markForDeletion(AudioNode*);
164 void deleteMarkedNodes();
166 // AudioContext can pull node(s) at the end of each render quantum even when they are not connected to any downstream nodes.
167 // These two methods are called by the nodes who want to add/remove themselves into/from the automatic pull lists.
168 void addAutomaticPullNode(AudioNode*);
169 void removeAutomaticPullNode(AudioNode*);
171 // Called right before handlePostRenderTasks() to handle nodes which need to be pulled even when they are not connected to anything.
172 void processAutomaticPullNodes(size_t framesToProcess);
174 // Keeps track of the number of connections made.
175 void incrementConnectionCount()
177 ASSERT(isMainThread());
181 unsigned connectionCount() const { return m_connectionCount; }
184 // Thread Safety and Graph Locking:
187 void setAudioThread(ThreadIdentifier thread) { m_audioThread = thread; } // FIXME: check either not initialized or the same
188 ThreadIdentifier audioThread() const { return m_audioThread; }
189 bool isAudioThread() const;
191 // Returns true only after the audio thread has been started and then shutdown.
192 bool isAudioThreadFinished() { return m_isAudioThreadFinished; }
194 // mustReleaseLock is set to true if we acquired the lock in this method call and caller must unlock(), false if it was previously acquired.
195 void lock(bool& mustReleaseLock);
197 // Returns true if we own the lock.
198 // mustReleaseLock is set to true if we acquired the lock in this method call and caller must unlock(), false if it was previously acquired.
199 bool tryLock(bool& mustReleaseLock);
203 // Returns true if this thread owns the context's lock.
204 bool isGraphOwner() const;
206 // Returns the maximum numuber of channels we can support.
207 static unsigned maxNumberOfChannels() { return MaxNumberOfChannels;}
211 AutoLocker(AudioContext* context)
215 context->lock(m_mustReleaseLock);
220 if (m_mustReleaseLock)
224 AudioContext* m_context;
225 bool m_mustReleaseLock;
228 // In AudioNode::deref() a tryLock() is used for calling finishDeref(), but if it fails keep track here.
229 void addDeferredFinishDeref(AudioNode*);
231 // In the audio thread at the start of each render cycle, we'll call handleDeferredFinishDerefs().
232 void handleDeferredFinishDerefs();
234 // Only accessed when the graph lock is held.
235 void markSummingJunctionDirty(AudioSummingJunction*);
236 void markAudioNodeOutputDirty(AudioNodeOutput*);
238 // Must be called on main thread.
239 void removeMarkedSummingJunction(AudioSummingJunction*);
242 virtual const AtomicString& interfaceName() const;
243 virtual ScriptExecutionContext* scriptExecutionContext() const;
244 virtual EventTargetData* eventTargetData() { return &m_eventTargetData; }
245 virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; }
247 DEFINE_ATTRIBUTE_EVENT_LISTENER(complete);
249 // Reconcile ref/deref which are defined both in ThreadSafeRefCounted and EventTarget.
250 using ThreadSafeRefCounted<AudioContext>::ref;
251 using ThreadSafeRefCounted<AudioContext>::deref;
253 void startRendering();
254 void fireCompletionEvent();
256 static unsigned s_hardwareContextCount;
259 explicit AudioContext(Document*);
260 AudioContext(Document*, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate);
262 static bool isSampleRateRangeGood(float sampleRate);
265 void constructCommon();
267 void lazyInitialize();
270 // ScriptExecutionContext calls stop twice.
271 // We'd like to schedule only one stop action for them.
272 bool m_isStopScheduled;
273 static void uninitializeDispatch(void* userData);
274 #if ENABLE(TIZEN_WEB_AUDIO)
275 static void pauseDispatch(void* userData);
279 void scheduleNodeDeletion();
280 static void deleteMarkedNodesDispatch(void* userData);
282 bool m_isInitialized;
283 bool m_isAudioThreadFinished;
285 // The context itself keeps a reference to all source nodes. The source nodes, then reference all nodes they're connected to.
286 // In turn, these nodes reference all nodes they're connected to. All nodes are ultimately connected to the AudioDestinationNode.
287 // When the context dereferences a source node, it will be deactivated from the rendering graph along with all other nodes it is
288 // uniquely connected to. See the AudioNode::ref() and AudioNode::deref() methods for more details.
289 void refNode(AudioNode*);
290 void derefNode(AudioNode*);
292 // When the context goes away, there might still be some sources which haven't finished playing.
293 // Make sure to dereference them here.
294 void derefUnfinishedSourceNodes();
296 RefPtr<AudioDestinationNode> m_destinationNode;
297 RefPtr<AudioListener> m_listener;
299 // Only accessed in the audio thread.
300 Vector<AudioNode*> m_finishedNodes;
302 // We don't use RefPtr<AudioNode> here because AudioNode has a more complex ref() / deref() implementation
303 // with an optional argument for refType. We need to use the special refType: RefTypeConnection
304 // Either accessed when the graph lock is held, or on the main thread when the audio thread has finished.
305 Vector<AudioNode*> m_referencedNodes;
307 // Accumulate nodes which need to be deleted here.
308 // This is copied to m_nodesToDelete at the end of a render cycle in handlePostRenderTasks(), where we're assured of a stable graph
309 // state which will have no references to any of the nodes in m_nodesToDelete once the context lock is released
310 // (when handlePostRenderTasks() has completed).
311 Vector<AudioNode*> m_nodesMarkedForDeletion;
313 // They will be scheduled for deletion (on the main thread) at the end of a render cycle (in realtime thread).
314 Vector<AudioNode*> m_nodesToDelete;
315 bool m_isDeletionScheduled;
317 // Only accessed when the graph lock is held.
318 HashSet<AudioSummingJunction*> m_dirtySummingJunctions;
319 HashSet<AudioNodeOutput*> m_dirtyAudioNodeOutputs;
320 void handleDirtyAudioSummingJunctions();
321 void handleDirtyAudioNodeOutputs();
323 // For the sake of thread safety, we maintain a seperate Vector of automatic pull nodes for rendering in m_renderingAutomaticPullNodes.
324 // It will be copied from m_automaticPullNodes by updateAutomaticPullNodes() at the very start or end of the rendering quantum.
325 HashSet<AudioNode*> m_automaticPullNodes;
326 Vector<AudioNode*> m_renderingAutomaticPullNodes;
327 // m_automaticPullNodesNeedUpdating keeps track if m_automaticPullNodes is modified.
328 bool m_automaticPullNodesNeedUpdating;
329 void updateAutomaticPullNodes();
331 unsigned m_connectionCount;
334 Mutex m_contextGraphMutex;
335 volatile ThreadIdentifier m_audioThread;
336 volatile ThreadIdentifier m_graphOwnerThread; // if the lock is held then this is the thread which owns it, otherwise == UndefinedThreadIdentifier
338 // Only accessed in the audio thread.
339 Vector<AudioNode*> m_deferredFinishDerefList;
341 // HRTF Database loader
342 RefPtr<HRTFDatabaseLoader> m_hrtfDatabaseLoader;
345 virtual void refEventTarget() { ref(); }
346 virtual void derefEventTarget() { deref(); }
347 EventTargetData m_eventTargetData;
349 RefPtr<AudioBuffer> m_renderTarget;
351 bool m_isOfflineContext;
353 AsyncAudioDecoder m_audioDecoder;
355 // This is considering 32 is large enough for multiple channels audio.
356 // It is somewhat arbitrary and could be increased if necessary.
357 enum { MaxNumberOfChannels = 32 };
359 // Number of AudioBufferSourceNodes that are active (playing).
360 int m_activeSourceCount;
361 #if ENABLE(TIZEN_WEB_AUDIO)
362 int m_activeScriptProcessorCount;
368 #endif // AudioContext_h