2012-09-18 23:07:33 +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 AudioNode_h_
|
|
|
|
#define AudioNode_h_
|
2012-09-18 23:07:33 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2013-04-27 22:44:50 +00:00
|
|
|
#include "mozilla/dom/AudioNodeBinding.h"
|
2012-09-18 23:07:33 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2012-09-25 03:31:58 +00:00
|
|
|
#include "nsTArray.h"
|
2012-09-18 23:07:33 +00:00
|
|
|
#include "AudioContext.h"
|
2013-02-04 23:07:25 +00:00
|
|
|
#include "MediaStreamGraph.h"
|
2013-05-29 11:36:37 +00:00
|
|
|
#include "WebAudioUtils.h"
|
2014-04-13 18:08:10 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-06-04 02:51:48 +00:00
|
|
|
#include "nsWeakReference.h"
|
2012-09-18 23:07:33 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-09-25 03:31:58 +00:00
|
|
|
|
2012-09-18 23:07:33 +00:00
|
|
|
namespace dom {
|
|
|
|
|
2013-08-15 19:44:14 +00:00
|
|
|
class AudioContext;
|
|
|
|
class AudioBufferSourceNode;
|
2013-05-01 22:59:02 +00:00
|
|
|
class AudioParam;
|
2013-08-15 19:44:14 +00:00
|
|
|
class AudioParamTimeline;
|
2013-03-12 15:28:14 +00:00
|
|
|
struct ThreeDPoint;
|
|
|
|
|
2013-04-15 01:52:55 +00:00
|
|
|
template<class T>
|
|
|
|
class SelfReference {
|
|
|
|
public:
|
|
|
|
SelfReference() : mHeld(false) {}
|
|
|
|
~SelfReference()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mHeld, "Forgot to drop the self reference?");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Take(T* t)
|
|
|
|
{
|
|
|
|
if (!mHeld) {
|
|
|
|
mHeld = true;
|
|
|
|
t->AddRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Drop(T* t)
|
|
|
|
{
|
|
|
|
if (mHeld) {
|
|
|
|
mHeld = false;
|
|
|
|
t->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() const { return mHeld; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mHeld;
|
|
|
|
};
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
/**
|
|
|
|
* The DOM object representing a Web Audio AudioNode.
|
|
|
|
*
|
|
|
|
* Each AudioNode has a MediaStream representing the actual
|
|
|
|
* real-time processing and output of this AudioNode.
|
|
|
|
*
|
|
|
|
* We track the incoming and outgoing connections to other AudioNodes.
|
2013-09-16 23:53:40 +00:00
|
|
|
* Outgoing connections have strong ownership. Also, AudioNodes that will
|
|
|
|
* produce sound on their output even when they have silent or no input ask
|
2013-10-24 23:11:01 +00:00
|
|
|
* the AudioContext to keep playing or tail-time references to keep them alive
|
|
|
|
* until the context is finished.
|
|
|
|
*
|
|
|
|
* Explicit disconnections will only remove references from output nodes after
|
|
|
|
* the graph is notified and the main thread receives a reply. Similarly,
|
|
|
|
* nodes with playing or tail-time references release these references only
|
|
|
|
* after receiving notification from their engine on the graph thread that
|
|
|
|
* playing has stopped. Engines notifying the main thread that they have
|
|
|
|
* finished do so strictly *after* producing and returning their last block.
|
|
|
|
* In this way, an engine that receives non-null input knows that the input
|
|
|
|
* comes from nodes that are still alive and will keep their output nodes
|
|
|
|
* alive for at least as long as it takes to process messages from the graph
|
|
|
|
* thread. i.e. the engine receiving non-null input knows that its node is
|
|
|
|
* still alive, and will still be alive when it receives a message from the
|
|
|
|
* engine.
|
2013-02-04 23:07:25 +00:00
|
|
|
*/
|
2014-06-04 02:51:48 +00:00
|
|
|
class AudioNode : public DOMEventTargetHelper,
|
|
|
|
public nsSupportsWeakReference
|
2012-09-18 23:07:33 +00:00
|
|
|
{
|
2013-04-24 20:57:07 +00:00
|
|
|
protected:
|
|
|
|
// You can only use refcounting to delete this object
|
|
|
|
virtual ~AudioNode();
|
|
|
|
|
2012-09-18 23:07:33 +00:00
|
|
|
public:
|
2013-04-27 22:44:50 +00:00
|
|
|
AudioNode(AudioContext* aContext,
|
|
|
|
uint32_t aChannelCount,
|
|
|
|
ChannelCountMode aChannelCountMode,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
2012-09-18 23:07:33 +00:00
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
// This should be idempotent (safe to call multiple times).
|
2013-04-23 02:45:37 +00:00
|
|
|
virtual void DestroyMediaStream();
|
2013-02-04 23:07:25 +00:00
|
|
|
|
2013-04-24 20:57:07 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2013-04-12 15:28:33 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioNode,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2013-02-04 23:07:25 +00:00
|
|
|
|
2013-04-11 12:47:57 +00:00
|
|
|
virtual AudioBufferSourceNode* AsAudioBufferSourceNode() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:07:33 +00:00
|
|
|
AudioContext* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext* Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
2013-04-23 01:23:54 +00:00
|
|
|
virtual void Connect(AudioNode& aDestination, uint32_t aOutput,
|
|
|
|
uint32_t aInput, ErrorResult& aRv);
|
2012-09-25 03:31:58 +00:00
|
|
|
|
2013-05-01 22:59:02 +00:00
|
|
|
virtual void Connect(AudioParam& aDestination, uint32_t aOutput,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-04-23 01:23:54 +00:00
|
|
|
virtual void Disconnect(uint32_t aOutput, ErrorResult& aRv);
|
2012-09-25 03:31:58 +00:00
|
|
|
|
|
|
|
// The following two virtual methods must be implemented by each node type
|
2013-01-24 00:50:18 +00:00
|
|
|
// to provide their number of input and output ports. These numbers are
|
|
|
|
// constant for the lifetime of the node. Both default to 1.
|
2013-05-05 15:48:45 +00:00
|
|
|
virtual uint16_t NumberOfInputs() const { return 1; }
|
|
|
|
virtual uint16_t NumberOfOutputs() const { return 1; }
|
2013-01-24 00:50:18 +00:00
|
|
|
|
2014-06-04 02:51:48 +00:00
|
|
|
uint32_t Id() const { return mId; }
|
|
|
|
|
2014-08-19 00:12:50 +00:00
|
|
|
bool PassThrough() const;
|
|
|
|
void SetPassThrough(bool aPassThrough);
|
|
|
|
|
2013-04-27 22:44:50 +00:00
|
|
|
uint32_t ChannelCount() const { return mChannelCount; }
|
2013-06-10 17:32:28 +00:00
|
|
|
virtual void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv)
|
2013-04-27 22:44:50 +00:00
|
|
|
{
|
2013-05-29 11:36:37 +00:00
|
|
|
if (aChannelCount == 0 ||
|
|
|
|
aChannelCount > WebAudioUtils::MaxChannelCount) {
|
2013-05-23 11:46:20 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-27 22:44:50 +00:00
|
|
|
mChannelCount = aChannelCount;
|
2013-04-27 23:25:23 +00:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-27 22:44:50 +00:00
|
|
|
}
|
|
|
|
ChannelCountMode ChannelCountModeValue() const
|
|
|
|
{
|
|
|
|
return mChannelCountMode;
|
|
|
|
}
|
2013-09-04 19:44:35 +00:00
|
|
|
virtual void SetChannelCountModeValue(ChannelCountMode aMode, ErrorResult& aRv)
|
2013-04-27 22:44:50 +00:00
|
|
|
{
|
|
|
|
mChannelCountMode = aMode;
|
2013-04-27 23:25:23 +00:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-27 22:44:50 +00:00
|
|
|
}
|
|
|
|
ChannelInterpretation ChannelInterpretationValue() const
|
|
|
|
{
|
|
|
|
return mChannelInterpretation;
|
|
|
|
}
|
|
|
|
void SetChannelInterpretationValue(ChannelInterpretation aMode)
|
|
|
|
{
|
|
|
|
mChannelInterpretation = aMode;
|
2013-04-27 23:25:23 +00:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-27 22:44:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 00:50:18 +00:00
|
|
|
struct InputNode {
|
2013-02-04 23:07:25 +00:00
|
|
|
~InputNode()
|
|
|
|
{
|
|
|
|
if (mStreamPort) {
|
|
|
|
mStreamPort->Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-13 18:08:10 +00:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
if (mStreamPort) {
|
|
|
|
amount += mStreamPort->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2013-04-15 01:52:55 +00:00
|
|
|
// Weak reference.
|
|
|
|
AudioNode* mInputNode;
|
2013-02-04 23:07:25 +00:00
|
|
|
nsRefPtr<MediaInputPort> mStreamPort;
|
2013-01-24 00:50:18 +00:00
|
|
|
// The index of the input port this node feeds into.
|
2013-05-01 22:59:02 +00:00
|
|
|
// This is not used for connections to AudioParams.
|
2013-01-24 00:50:18 +00:00
|
|
|
uint32_t mInputPort;
|
|
|
|
// The index of the output port this node comes out of.
|
|
|
|
uint32_t mOutputPort;
|
2012-09-25 03:31:58 +00:00
|
|
|
};
|
2012-09-18 23:07:33 +00:00
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
MediaStream* Stream() { return mStream; }
|
|
|
|
|
2013-04-11 12:47:57 +00:00
|
|
|
const nsTArray<InputNode>& InputNodes() const
|
|
|
|
{
|
|
|
|
return mInputNodes;
|
|
|
|
}
|
2013-09-24 01:46:30 +00:00
|
|
|
const nsTArray<nsRefPtr<AudioNode> >& OutputNodes() const
|
|
|
|
{
|
|
|
|
return mOutputNodes;
|
|
|
|
}
|
|
|
|
const nsTArray<nsRefPtr<AudioParam> >& OutputParams() const
|
|
|
|
{
|
|
|
|
return mOutputParams;
|
|
|
|
}
|
2013-04-11 12:47:57 +00:00
|
|
|
|
2013-05-01 22:59:02 +00:00
|
|
|
void RemoveOutputParam(AudioParam* aParam);
|
|
|
|
|
2013-09-16 23:53:40 +00:00
|
|
|
// MarkActive() asks the context to keep the AudioNode alive until the
|
|
|
|
// context is finished. This takes care of "playing" references and
|
|
|
|
// "tail-time" references.
|
|
|
|
void MarkActive() { Context()->RegisterActiveNode(this); }
|
|
|
|
// Active nodes call MarkInactive() when they have finished producing sound
|
|
|
|
// for the foreseeable future.
|
|
|
|
// Do not call MarkInactive from a node destructor. If the destructor is
|
|
|
|
// called, then the node is already inactive.
|
|
|
|
// MarkInactive() may delete |this|.
|
|
|
|
void MarkInactive() { Context()->UnregisterActiveNode(this); }
|
|
|
|
|
2014-04-13 18:08:10 +00:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
|
|
|
virtual const char* NodeType() const = 0;
|
|
|
|
|
2013-04-15 01:52:55 +00:00
|
|
|
private:
|
2013-04-30 23:20:55 +00:00
|
|
|
friend class AudioBufferSourceNode;
|
2013-04-15 01:52:55 +00:00
|
|
|
// This could possibly delete 'this'.
|
|
|
|
void DisconnectFromGraph();
|
|
|
|
|
2013-01-28 23:59:29 +00:00
|
|
|
protected:
|
|
|
|
static void Callback(AudioNode* aNode) { /* not implemented */ }
|
|
|
|
|
2013-03-12 15:28:14 +00:00
|
|
|
// Helpers for sending different value types to streams
|
|
|
|
void SendDoubleParameterToStream(uint32_t aIndex, double aValue);
|
|
|
|
void SendInt32ParameterToStream(uint32_t aIndex, int32_t aValue);
|
|
|
|
void SendThreeDPointParameterToStream(uint32_t aIndex, const ThreeDPoint& aValue);
|
2013-04-27 23:25:23 +00:00
|
|
|
void SendChannelMixingParametersToStream();
|
2013-03-30 01:46:03 +00:00
|
|
|
static void SendTimelineParameterToStream(AudioNode* aNode, uint32_t aIndex,
|
|
|
|
const AudioParamTimeline& aValue);
|
2013-03-12 15:28:14 +00:00
|
|
|
|
2012-09-18 23:07:33 +00:00
|
|
|
private:
|
|
|
|
nsRefPtr<AudioContext> mContext;
|
2013-01-24 00:50:18 +00:00
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
protected:
|
|
|
|
// Must be set in the constructor. Must not be null.
|
|
|
|
// If MaxNumberOfInputs() is > 0, then mStream must be a ProcessedMediaStream.
|
|
|
|
nsRefPtr<MediaStream> mStream;
|
|
|
|
|
|
|
|
private:
|
2013-01-24 00:50:18 +00:00
|
|
|
// For every InputNode, there is a corresponding entry in mOutputNodes of the
|
|
|
|
// InputNode's mInputNode.
|
|
|
|
nsTArray<InputNode> mInputNodes;
|
|
|
|
// For every mOutputNode entry, there is a corresponding entry in mInputNodes
|
|
|
|
// of the mOutputNode entry. We won't necessarily be able to identify the
|
|
|
|
// exact matching entry, since mOutputNodes doesn't include the port
|
|
|
|
// identifiers and the same node could be connected on multiple ports.
|
|
|
|
nsTArray<nsRefPtr<AudioNode> > mOutputNodes;
|
2013-05-01 22:59:02 +00:00
|
|
|
// For every mOutputParams entry, there is a corresponding entry in
|
|
|
|
// AudioParam::mInputNodes of the mOutputParams entry. We won't necessarily be
|
|
|
|
// able to identify the exact matching entry, since mOutputParams doesn't
|
|
|
|
// include the port identifiers and the same node could be connected on
|
|
|
|
// multiple ports.
|
|
|
|
nsTArray<nsRefPtr<AudioParam> > mOutputParams;
|
2013-04-27 22:44:50 +00:00
|
|
|
uint32_t mChannelCount;
|
|
|
|
ChannelCountMode mChannelCountMode;
|
|
|
|
ChannelInterpretation mChannelInterpretation;
|
2014-06-04 02:51:48 +00:00
|
|
|
const uint32_t mId;
|
2014-08-19 00:12:50 +00:00
|
|
|
// Whether the node just passes through its input. This is a devtools API that
|
|
|
|
// only works for some node types.
|
|
|
|
bool mPassThrough;
|
2014-06-04 02:51:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// In debug builds, check to make sure that the node demise notification has
|
|
|
|
// been properly sent before the node is destroyed.
|
|
|
|
bool mDemiseNotified;
|
|
|
|
#endif
|
2012-09-18 23:07:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:39:38 +00:00
|
|
|
#endif
|