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