michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef AudioContext_h_ michael@0: #define AudioContext_h_ michael@0: michael@0: #include "mozilla/dom/AudioChannelBinding.h" michael@0: #include "MediaBufferDecoder.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/DOMEventTargetHelper.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/dom/TypedArray.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "nsHashKeys.h" michael@0: #include "nsTHashtable.h" michael@0: #include "js/TypeDecls.h" michael@0: #include "nsIMemoryReporter.h" michael@0: michael@0: // X11 has a #define for CurrentTime. Unbelievable :-(. michael@0: // See content/media/DOMMediaStream.h for more fun! michael@0: #ifdef CurrentTime michael@0: #undef CurrentTime michael@0: #endif michael@0: michael@0: class nsPIDOMWindow; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class DOMMediaStream; michael@0: class ErrorResult; michael@0: class MediaStream; michael@0: class MediaStreamGraph; michael@0: michael@0: namespace dom { michael@0: michael@0: class AnalyserNode; michael@0: class AudioBuffer; michael@0: class AudioBufferSourceNode; michael@0: class AudioDestinationNode; michael@0: class AudioListener; michael@0: class AudioNode; michael@0: class BiquadFilterNode; michael@0: class ChannelMergerNode; michael@0: class ChannelSplitterNode; michael@0: class ConvolverNode; michael@0: class DelayNode; michael@0: class DynamicsCompressorNode; michael@0: class GainNode; michael@0: class HTMLMediaElement; michael@0: class MediaElementAudioSourceNode; michael@0: class GlobalObject; michael@0: class MediaStreamAudioDestinationNode; michael@0: class MediaStreamAudioSourceNode; michael@0: class OscillatorNode; michael@0: class PannerNode; michael@0: class ScriptProcessorNode; michael@0: class WaveShaperNode; michael@0: class PeriodicWave; michael@0: michael@0: class AudioContext MOZ_FINAL : public DOMEventTargetHelper, michael@0: public nsIMemoryReporter michael@0: { michael@0: AudioContext(nsPIDOMWindow* aParentWindow, michael@0: bool aIsOffline, michael@0: AudioChannel aChannel = AudioChannel::Normal, michael@0: uint32_t aNumberOfChannels = 0, michael@0: uint32_t aLength = 0, michael@0: float aSampleRate = 0.0f); michael@0: ~AudioContext(); michael@0: michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioContext, michael@0: DOMEventTargetHelper) michael@0: MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf) michael@0: michael@0: nsPIDOMWindow* GetParentObject() const michael@0: { michael@0: return GetOwner(); michael@0: } michael@0: michael@0: void Shutdown(); // idempotent michael@0: void Suspend(); michael@0: void Resume(); michael@0: michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: using DOMEventTargetHelper::DispatchTrustedEvent; michael@0: michael@0: // Constructor for regular AudioContext michael@0: static already_AddRefed michael@0: Constructor(const GlobalObject& aGlobal, ErrorResult& aRv); michael@0: michael@0: // Constructor for regular AudioContext. A default audio channel is needed. michael@0: static already_AddRefed michael@0: Constructor(const GlobalObject& aGlobal, michael@0: AudioChannel aChannel, michael@0: ErrorResult& aRv); michael@0: michael@0: // Constructor for offline AudioContext michael@0: static already_AddRefed michael@0: Constructor(const GlobalObject& aGlobal, michael@0: uint32_t aNumberOfChannels, michael@0: uint32_t aLength, michael@0: float aSampleRate, michael@0: ErrorResult& aRv); michael@0: michael@0: // AudioContext methods michael@0: michael@0: AudioDestinationNode* Destination() const michael@0: { michael@0: return mDestination; michael@0: } michael@0: michael@0: float SampleRate() const michael@0: { michael@0: return mSampleRate; michael@0: } michael@0: michael@0: double CurrentTime() const; michael@0: michael@0: AudioListener* Listener(); michael@0: michael@0: already_AddRefed CreateBufferSource(); michael@0: michael@0: already_AddRefed michael@0: CreateBuffer(JSContext* aJSContext, uint32_t aNumberOfChannels, michael@0: uint32_t aLength, float aSampleRate, michael@0: ErrorResult& aRv); michael@0: michael@0: already_AddRefed michael@0: CreateMediaStreamDestination(ErrorResult& aRv); michael@0: michael@0: already_AddRefed michael@0: CreateScriptProcessor(uint32_t aBufferSize, michael@0: uint32_t aNumberOfInputChannels, michael@0: uint32_t aNumberOfOutputChannels, michael@0: ErrorResult& aRv); michael@0: michael@0: already_AddRefed michael@0: CreateAnalyser(); michael@0: michael@0: already_AddRefed michael@0: CreateGain(); michael@0: michael@0: already_AddRefed michael@0: CreateWaveShaper(); michael@0: michael@0: already_AddRefed michael@0: CreateMediaElementSource(HTMLMediaElement& aMediaElement, ErrorResult& aRv); michael@0: already_AddRefed michael@0: CreateMediaStreamSource(DOMMediaStream& aMediaStream, ErrorResult& aRv); michael@0: michael@0: already_AddRefed michael@0: CreateDelay(double aMaxDelayTime, ErrorResult& aRv); michael@0: michael@0: already_AddRefed michael@0: CreatePanner(); michael@0: michael@0: already_AddRefed michael@0: CreateConvolver(); michael@0: michael@0: already_AddRefed michael@0: CreateChannelSplitter(uint32_t aNumberOfOutputs, ErrorResult& aRv); michael@0: michael@0: already_AddRefed michael@0: CreateChannelMerger(uint32_t aNumberOfInputs, ErrorResult& aRv); michael@0: michael@0: already_AddRefed michael@0: CreateDynamicsCompressor(); michael@0: michael@0: already_AddRefed michael@0: CreateBiquadFilter(); michael@0: michael@0: already_AddRefed michael@0: CreateOscillator(); michael@0: michael@0: already_AddRefed michael@0: CreatePeriodicWave(const Float32Array& aRealData, const Float32Array& aImagData, michael@0: ErrorResult& aRv); michael@0: michael@0: void DecodeAudioData(const ArrayBuffer& aBuffer, michael@0: DecodeSuccessCallback& aSuccessCallback, michael@0: const Optional >& aFailureCallback); michael@0: michael@0: // OfflineAudioContext methods michael@0: void StartRendering(ErrorResult& aRv); michael@0: IMPL_EVENT_HANDLER(complete) michael@0: michael@0: bool IsOffline() const { return mIsOffline; } michael@0: michael@0: MediaStreamGraph* Graph() const; michael@0: MediaStream* DestinationStream() const; michael@0: michael@0: // Nodes register here if they will produce sound even if they have silent michael@0: // or no input connections. The AudioContext will keep registered nodes michael@0: // alive until the context is collected. This takes care of "playing" michael@0: // references and "tail-time" references. michael@0: void RegisterActiveNode(AudioNode* aNode); michael@0: // Nodes unregister when they have finished producing sound for the michael@0: // foreseeable future. michael@0: // Do NOT call UnregisterActiveNode from an AudioNode destructor. michael@0: // If the destructor is called, then the Node has already been unregistered. michael@0: // The destructor may be called during hashtable enumeration, during which michael@0: // unregistering would not be safe. michael@0: void UnregisterActiveNode(AudioNode* aNode); michael@0: michael@0: void UnregisterAudioBufferSourceNode(AudioBufferSourceNode* aNode); michael@0: void UnregisterPannerNode(PannerNode* aNode); michael@0: void UpdatePannerSource(); michael@0: michael@0: uint32_t MaxChannelCount() const; michael@0: michael@0: void Mute() const; michael@0: void Unmute() const; michael@0: michael@0: JSContext* GetJSContext() const; michael@0: michael@0: AudioChannel MozAudioChannelType() const; michael@0: void SetMozAudioChannelType(AudioChannel aValue, ErrorResult& aRv); michael@0: michael@0: void UpdateNodeCount(int32_t aDelta); michael@0: michael@0: double DOMTimeToStreamTime(double aTime) const michael@0: { michael@0: return aTime - ExtraCurrentTime(); michael@0: } michael@0: michael@0: private: michael@0: /** michael@0: * Returns the amount of extra time added to the current time of the michael@0: * AudioDestinationNode's MediaStream to get this AudioContext's currentTime. michael@0: * Must be subtracted from all DOM API parameter times that are on the same michael@0: * timeline as AudioContext's currentTime to get times we can pass to the michael@0: * MediaStreamGraph. michael@0: */ michael@0: double ExtraCurrentTime() const; michael@0: michael@0: void RemoveFromDecodeQueue(WebAudioDecodeJob* aDecodeJob); michael@0: void ShutdownDecoder(); michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport, michael@0: nsISupports* aData); michael@0: michael@0: friend struct ::mozilla::WebAudioDecodeJob; michael@0: michael@0: private: michael@0: // Note that it's important for mSampleRate to be initialized before michael@0: // mDestination, as mDestination's constructor needs to access it! michael@0: const float mSampleRate; michael@0: nsRefPtr mDestination; michael@0: nsRefPtr mListener; michael@0: MediaBufferDecoder mDecoder; michael@0: nsTArray > mDecodeJobs; michael@0: // See RegisterActiveNode. These will keep the AudioContext alive while it michael@0: // is rendering and the window remains alive. michael@0: nsTHashtable > mActiveNodes; michael@0: // Hashsets containing all the PannerNodes, to compute the doppler shift. michael@0: // These are weak pointers. michael@0: nsTHashtable > mPannerNodes; michael@0: // Number of channels passed in the OfflineAudioContext ctor. michael@0: uint32_t mNumberOfChannels; michael@0: // Number of nodes that currently exist for this AudioContext michael@0: int32_t mNodeCount; michael@0: bool mIsOffline; michael@0: bool mIsStarted; michael@0: bool mIsShutDown; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: