#ifndef AudioContext_h
#define AudioContext_h
#include "bindings/v8/ScriptWrappable.h"
#include "core/dom/ActiveDOMObject.h"
#include "core/events/EventListener.h"
#include "core/events/EventTarget.h"
#include "platform/audio/AudioBus.h"
#include "modules/webaudio/AsyncAudioDecoder.h"
#include "modules/webaudio/AudioDestinationNode.h"
#include "wtf/HashSet.h"
#include "wtf/MainThread.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/RefPtr.h"
#include "wtf/ThreadSafeRefCounted.h"
#include "wtf/Threading.h"
#include "wtf/Vector.h"
#include "wtf/text/AtomicStringHash.h"
namespace WebCore {
class AnalyserNode;
class AudioBuffer;
class AudioBufferCallback;
class AudioBufferSourceNode;
class AudioListener;
class AudioSummingJunction;
class BiquadFilterNode;
class ChannelMergerNode;
class ChannelSplitterNode;
class ConvolverNode;
class DelayNode;
class Document;
class DynamicsCompressorNode;
class ExceptionState;
class GainNode;
class HTMLMediaElement;
class MediaElementAudioSourceNode;
class MediaStreamAudioDestinationNode;
class MediaStreamAudioSourceNode;
class OscillatorNode;
class PannerNode;
class PeriodicWave;
class ScriptProcessorNode;
class WaveShaperNode;
class AudioContext : public ActiveDOMObject, public ScriptWrappable, public ThreadSafeRefCounted<AudioContext>, public EventTargetWithInlineData {
DEFINE_EVENT_TARGET_REFCOUNTING(ThreadSafeRefCounted<AudioContext>);
public:
static PassRefPtr<AudioContext> create(Document&, ExceptionState&);
static PassRefPtr<AudioContext> create(Document&, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&);
virtual ~AudioContext();
bool isInitialized() const;
void lazyInitialize();
bool isOfflineContext() { return m_isOfflineContext; }
virtual void stop() OVERRIDE FINAL;
virtual bool hasPendingActivity() const OVERRIDE;
AudioDestinationNode* destination() { return m_destinationNode.get(); }
size_t currentSampleFrame() const { return m_destinationNode->currentSampleFrame(); }
double currentTime() const { return m_destinationNode->currentTime(); }
float sampleRate() const { return m_destinationNode->sampleRate(); }
unsigned long activeSourceCount() const { return static_cast<unsigned long>(m_activeSourceCount); }
void incrementActiveSourceCount();
void decrementActiveSourceCount();
PassRefPtr<AudioBuffer> createBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&);
PassRefPtr<AudioBuffer> createBuffer(ArrayBuffer*, bool mixToMono, ExceptionState&);
void decodeAudioData(ArrayBuffer*, PassOwnPtr<AudioBufferCallback>, PassOwnPtr<AudioBufferCallback>, ExceptionState&);
AudioListener* listener() { return m_listener.get(); }
PassRefPtr<AudioBufferSourceNode> createBufferSource();
PassRefPtr<MediaElementAudioSourceNode> createMediaElementSource(HTMLMediaElement*, ExceptionState&);
PassRefPtr<MediaStreamAudioSourceNode> createMediaStreamSource(MediaStream*, ExceptionState&);
PassRefPtr<MediaStreamAudioDestinationNode> createMediaStreamDestination();
PassRefPtr<GainNode> createGain();
PassRefPtr<BiquadFilterNode> createBiquadFilter();
PassRefPtr<WaveShaperNode> createWaveShaper();
PassRefPtr<DelayNode> createDelay(ExceptionState&);
PassRefPtr<DelayNode> createDelay(double maxDelayTime, ExceptionState&);
PassRefPtr<PannerNode> createPanner();
PassRefPtr<ConvolverNode> createConvolver();
PassRefPtr<DynamicsCompressorNode> createDynamicsCompressor();
PassRefPtr<AnalyserNode> createAnalyser();
PassRefPtr<ScriptProcessorNode> createScriptProcessor(ExceptionState&);
PassRefPtr<ScriptProcessorNode> createScriptProcessor(size_t bufferSize, ExceptionState&);
PassRefPtr<ScriptProcessorNode> createScriptProcessor(size_t bufferSize, size_t numberOfInputChannels, ExceptionState&);
PassRefPtr<ScriptProcessorNode> createScriptProcessor(size_t bufferSize, size_t numberOfInputChannels, size_t numberOfOutputChannels, ExceptionState&);
PassRefPtr<ChannelSplitterNode> createChannelSplitter(ExceptionState&);
PassRefPtr<ChannelSplitterNode> createChannelSplitter(size_t numberOfOutputs, ExceptionState&);
PassRefPtr<ChannelMergerNode> createChannelMerger(ExceptionState&);
PassRefPtr<ChannelMergerNode> createChannelMerger(size_t numberOfInputs, ExceptionState&);
PassRefPtr<OscillatorNode> createOscillator();
PassRefPtr<PeriodicWave> createPeriodicWave(Float32Array* real, Float32Array* imag, ExceptionState&);
void notifyNodeFinishedProcessing(AudioNode*);
void handlePreRenderTasks();
void handlePostRenderTasks();
void derefFinishedSourceNodes();
void markForDeletion(AudioNode*);
void deleteMarkedNodes();
void addAutomaticPullNode(AudioNode*);
void removeAutomaticPullNode(AudioNode*);
void processAutomaticPullNodes(size_t framesToProcess);
void incrementConnectionCount()
{
ASSERT(isMainThread());
m_connectionCount++;
}
unsigned connectionCount() const { return m_connectionCount; }
void setAudioThread(ThreadIdentifier thread) { m_audioThread = thread; }
ThreadIdentifier audioThread() const { return m_audioThread; }
bool isAudioThread() const;
bool isAudioThreadFinished() { return m_isAudioThreadFinished; }
void lock(bool& mustReleaseLock);
bool tryLock(bool& mustReleaseLock);
void unlock();
bool isGraphOwner() const;
static unsigned maxNumberOfChannels() { return MaxNumberOfChannels;}
class AutoLocker {
public:
AutoLocker(AudioContext* context)
: m_context(context)
{
ASSERT(context);
context->lock(m_mustReleaseLock);
}
~AutoLocker()
{
if (m_mustReleaseLock)
m_context->unlock();
}
private:
AudioContext* m_context;
bool m_mustReleaseLock;
};
void addDeferredFinishDeref(AudioNode*);
void handleDeferredFinishDerefs();
void markSummingJunctionDirty(AudioSummingJunction*);
void markAudioNodeOutputDirty(AudioNodeOutput*);
void removeMarkedSummingJunction(AudioSummingJunction*);
virtual const AtomicString& interfaceName() const OVERRIDE FINAL;
virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
DEFINE_ATTRIBUTE_EVENT_LISTENER(complete);
void startRendering();
void fireCompletionEvent();
static unsigned s_hardwareContextCount;
protected:
explicit AudioContext(Document*);
AudioContext(Document*, unsigned numberOfChannels, size_t numberOfFrames, float sampleRate);
static bool isSampleRateRangeGood(float sampleRate);
private:
void constructCommon();
void uninitialize();
bool m_isStopScheduled;
static void stopDispatch(void* userData);
bool m_isCleared;
void clear();
void scheduleNodeDeletion();
static void deleteMarkedNodesDispatch(void* userData);
bool m_isInitialized;
bool m_isAudioThreadFinished;
void refNode(AudioNode*);
void derefNode(AudioNode*);
void derefUnfinishedSourceNodes();
RefPtr<AudioDestinationNode> m_destinationNode;
RefPtr<AudioListener> m_listener;
Vector<AudioNode*> m_finishedNodes;
Vector<AudioNode*> m_referencedNodes;
Vector<AudioNode*> m_nodesMarkedForDeletion;
Vector<AudioNode*> m_nodesToDelete;
bool m_isDeletionScheduled;
HashSet<AudioSummingJunction*> m_dirtySummingJunctions;
HashSet<AudioNodeOutput*> m_dirtyAudioNodeOutputs;
void handleDirtyAudioSummingJunctions();
void handleDirtyAudioNodeOutputs();
HashSet<AudioNode*> m_automaticPullNodes;
Vector<AudioNode*> m_renderingAutomaticPullNodes;
bool m_automaticPullNodesNeedUpdating;
void updateAutomaticPullNodes();
unsigned m_connectionCount;
Mutex m_contextGraphMutex;
volatile ThreadIdentifier m_audioThread;
volatile ThreadIdentifier m_graphOwnerThread;
Vector<AudioNode*> m_deferredFinishDerefList;
RefPtr<AudioBuffer> m_renderTarget;
bool m_isOfflineContext;
AsyncAudioDecoder m_audioDecoder;
enum { MaxNumberOfChannels = 32 };
int m_activeSourceCount;
};
}
#endif