2012-09-19 03:07:33 +04: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-31 01:39:38 +04:00
|
|
|
#ifndef AudioNode_h_
|
|
|
|
#define AudioNode_h_
|
2012-09-19 03:07:33 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2013-04-28 02:44:50 +04:00
|
|
|
#include "mozilla/dom/AudioNodeBinding.h"
|
2012-09-19 03:07:33 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2012-09-25 07:31:58 +04:00
|
|
|
#include "nsTArray.h"
|
2012-09-19 03:07:33 +04:00
|
|
|
#include "AudioContext.h"
|
2013-02-05 03:07:25 +04:00
|
|
|
#include "MediaStreamGraph.h"
|
2013-05-29 15:36:37 +04:00
|
|
|
#include "WebAudioUtils.h"
|
2014-04-13 22:08:10 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-06-04 06:51:48 +04:00
|
|
|
#include "nsWeakReference.h"
|
Bug 1085356 - Better handling of OSX audio output devices switching when SourceMediaStream are present in the MSG. r=jesup
On OSX, when the audio output device changes, the OS will call the audio
callbacks in weird patterns, if at all, during a period of ~1s. If
real-time SourceMediaStreams are present in the MediaStreamGraph, this means
buffering will occur, and the overall latency between the MediaStreamGraph
insertion time, and the actual output time will grow.
To fix this, we detect when the output device changes, and we switch temporarily
to a SystemClockDriver, that will pull from the SourceMediaStream, and simply
discard all input data. Then, when we get audio callbacks called reliably
(basically, when OSX is done switching to the other output), we switch back to
the previous AudioCallbackDriver.
We keep the previous AudioCallbackDriver alive using a self-reference. If an
AudioCallbackDriver has a self-reference, that means it's in a state when a
device is switching, so it's not linked to an MSG per se.
2014-10-22 18:12:29 +04:00
|
|
|
#include "SelfRef.h"
|
2012-09-19 03:07:33 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-09-25 07:31:58 +04:00
|
|
|
|
2012-09-19 03:07:33 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2013-08-15 23:44:14 +04:00
|
|
|
class AudioContext;
|
|
|
|
class AudioBufferSourceNode;
|
2013-05-02 02:59:02 +04:00
|
|
|
class AudioParam;
|
2013-08-15 23:44:14 +04:00
|
|
|
class AudioParamTimeline;
|
2013-03-12 19:28:14 +04:00
|
|
|
struct ThreeDPoint;
|
|
|
|
|
2013-02-05 03:07:25 +04: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-17 03:53:40 +04: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-25 03:11:01 +04: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-05 03:07:25 +04:00
|
|
|
*/
|
2014-06-04 06:51:48 +04:00
|
|
|
class AudioNode : public DOMEventTargetHelper,
|
|
|
|
public nsSupportsWeakReference
|
2012-09-19 03:07:33 +04:00
|
|
|
{
|
2013-04-25 00:57:07 +04:00
|
|
|
protected:
|
|
|
|
// You can only use refcounting to delete this object
|
|
|
|
virtual ~AudioNode();
|
|
|
|
|
2012-09-19 03:07:33 +04:00
|
|
|
public:
|
2013-04-28 02:44:50 +04:00
|
|
|
AudioNode(AudioContext* aContext,
|
|
|
|
uint32_t aChannelCount,
|
|
|
|
ChannelCountMode aChannelCountMode,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
2012-09-19 03:07:33 +04:00
|
|
|
|
2013-02-05 03:07:25 +04:00
|
|
|
// This should be idempotent (safe to call multiple times).
|
2013-04-23 06:45:37 +04:00
|
|
|
virtual void DestroyMediaStream();
|
2013-02-05 03:07:25 +04:00
|
|
|
|
2013-04-25 00:57:07 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2013-04-12 19:28:33 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioNode,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-02-05 03:07:25 +04:00
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
virtual AudioBufferSourceNode* AsAudioBufferSourceNode()
|
|
|
|
{
|
2013-04-11 16:47:57 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-19 03:07:33 +04:00
|
|
|
AudioContext* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext* Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
2015-10-28 07:54:35 +03:00
|
|
|
virtual AudioNode* Connect(AudioNode& aDestination, uint32_t aOutput,
|
|
|
|
uint32_t aInput, ErrorResult& aRv);
|
2012-09-25 07:31:58 +04:00
|
|
|
|
2013-05-02 02:59:02 +04:00
|
|
|
virtual void Connect(AudioParam& aDestination, uint32_t aOutput,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-06-05 12:17:14 +03:00
|
|
|
virtual void Disconnect(ErrorResult& aRv);
|
2013-04-23 05:23:54 +04:00
|
|
|
virtual void Disconnect(uint32_t aOutput, ErrorResult& aRv);
|
2015-06-05 12:17:14 +03:00
|
|
|
virtual void Disconnect(AudioNode& aDestination, ErrorResult& aRv);
|
|
|
|
virtual void Disconnect(AudioNode& aDestination, uint32_t aOutput,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
virtual void Disconnect(AudioNode& aDestination,
|
|
|
|
uint32_t aOutput, uint32_t aInput,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
virtual void Disconnect(AudioParam& aDestination, ErrorResult& aRv);
|
|
|
|
virtual void Disconnect(AudioParam& aDestination, uint32_t aOutput,
|
|
|
|
ErrorResult& aRv);
|
2012-09-25 07:31:58 +04:00
|
|
|
|
2015-08-06 10:25:53 +03:00
|
|
|
// Called after input nodes have been explicitly added or removed through
|
|
|
|
// the Connect() or Disconnect() methods.
|
|
|
|
virtual void NotifyInputsChanged() {}
|
2015-08-06 05:15:40 +03:00
|
|
|
// Indicate that the node should continue indefinitely to behave as if an
|
|
|
|
// input is connected, even though there is no longer a corresponding entry
|
|
|
|
// in mInputNodes. Called after an input node has been removed because it
|
|
|
|
// is being garbage collected.
|
|
|
|
virtual void NotifyHasPhantomInput() {}
|
2015-08-06 10:25:53 +03:00
|
|
|
|
2012-09-25 07:31:58 +04:00
|
|
|
// The following two virtual methods must be implemented by each node type
|
2013-01-24 04:50:18 +04: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 19:48:45 +04:00
|
|
|
virtual uint16_t NumberOfInputs() const { return 1; }
|
|
|
|
virtual uint16_t NumberOfOutputs() const { return 1; }
|
2013-01-24 04:50:18 +04:00
|
|
|
|
2014-06-04 06:51:48 +04:00
|
|
|
uint32_t Id() const { return mId; }
|
|
|
|
|
2014-08-19 04:12:50 +04:00
|
|
|
bool PassThrough() const;
|
|
|
|
void SetPassThrough(bool aPassThrough);
|
|
|
|
|
2013-04-28 02:44:50 +04:00
|
|
|
uint32_t ChannelCount() const { return mChannelCount; }
|
2013-06-10 21:32:28 +04:00
|
|
|
virtual void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv)
|
2013-04-28 02:44:50 +04:00
|
|
|
{
|
2013-05-29 15:36:37 +04:00
|
|
|
if (aChannelCount == 0 ||
|
|
|
|
aChannelCount > WebAudioUtils::MaxChannelCount) {
|
2013-05-23 15:46:20 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-28 02:44:50 +04:00
|
|
|
mChannelCount = aChannelCount;
|
2013-04-28 03:25:23 +04:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-28 02:44:50 +04:00
|
|
|
}
|
|
|
|
ChannelCountMode ChannelCountModeValue() const
|
|
|
|
{
|
|
|
|
return mChannelCountMode;
|
|
|
|
}
|
2013-09-04 23:44:35 +04:00
|
|
|
virtual void SetChannelCountModeValue(ChannelCountMode aMode, ErrorResult& aRv)
|
2013-04-28 02:44:50 +04:00
|
|
|
{
|
|
|
|
mChannelCountMode = aMode;
|
2013-04-28 03:25:23 +04:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-28 02:44:50 +04:00
|
|
|
}
|
|
|
|
ChannelInterpretation ChannelInterpretationValue() const
|
|
|
|
{
|
|
|
|
return mChannelInterpretation;
|
|
|
|
}
|
|
|
|
void SetChannelInterpretationValue(ChannelInterpretation aMode)
|
|
|
|
{
|
|
|
|
mChannelInterpretation = aMode;
|
2013-04-28 03:25:23 +04:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-28 02:44:50 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
struct InputNode final
|
|
|
|
{
|
2013-02-05 03:07:25 +04:00
|
|
|
~InputNode()
|
|
|
|
{
|
|
|
|
if (mStreamPort) {
|
|
|
|
mStreamPort->Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
if (mStreamPort) {
|
|
|
|
amount += mStreamPort->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2013-04-15 05:52:55 +04:00
|
|
|
// Weak reference.
|
|
|
|
AudioNode* mInputNode;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaInputPort> mStreamPort;
|
2013-01-24 04:50:18 +04:00
|
|
|
// The index of the input port this node feeds into.
|
2013-05-02 02:59:02 +04:00
|
|
|
// This is not used for connections to AudioParams.
|
2013-01-24 04:50:18 +04:00
|
|
|
uint32_t mInputPort;
|
|
|
|
// The index of the output port this node comes out of.
|
|
|
|
uint32_t mOutputPort;
|
2012-09-25 07:31:58 +04:00
|
|
|
};
|
2012-09-19 03:07:33 +04:00
|
|
|
|
2015-07-02 08:30:36 +03:00
|
|
|
// Returns the stream, if any.
|
2015-09-16 07:15:21 +03:00
|
|
|
AudioNodeStream* GetStream() const { return mStream; }
|
2013-02-05 03:07:25 +04:00
|
|
|
|
2013-04-11 16:47:57 +04:00
|
|
|
const nsTArray<InputNode>& InputNodes() const
|
|
|
|
{
|
|
|
|
return mInputNodes;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
const nsTArray<RefPtr<AudioNode> >& OutputNodes() const
|
2013-09-24 05:46:30 +04:00
|
|
|
{
|
|
|
|
return mOutputNodes;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
const nsTArray<RefPtr<AudioParam> >& OutputParams() const
|
2013-09-24 05:46:30 +04:00
|
|
|
{
|
|
|
|
return mOutputParams;
|
|
|
|
}
|
2013-04-11 16:47:57 +04:00
|
|
|
|
2016-10-20 15:34:27 +03:00
|
|
|
template<typename T>
|
|
|
|
const nsTArray<InputNode>&
|
|
|
|
InputsForDestination(uint32_t aOutputIndex) const;
|
|
|
|
|
2013-05-02 02:59:02 +04:00
|
|
|
void RemoveOutputParam(AudioParam* aParam);
|
|
|
|
|
2013-09-17 03:53:40 +04: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 22:08:10 +04:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2016-07-04 07:24:47 +03:00
|
|
|
// Returns a string from constant static storage identifying the dom node
|
|
|
|
// type.
|
2014-04-13 22:08:10 +04:00
|
|
|
virtual const char* NodeType() const = 0;
|
|
|
|
|
2013-04-15 05:52:55 +04:00
|
|
|
private:
|
2016-09-09 14:41:01 +03:00
|
|
|
// Given:
|
|
|
|
//
|
|
|
|
// - a DestinationType, that can be an AudioNode or an AudioParam ;
|
|
|
|
// - a Predicate, a function that takes an InputNode& and returns a bool ;
|
|
|
|
//
|
2016-10-20 15:34:27 +03:00
|
|
|
// This method iterates on the InputNodes() of the node at the index
|
|
|
|
// aDestinationIndex, and calls `DisconnectFromOutputIfConnected` with this
|
|
|
|
// input node, if aPredicate returns true.
|
2016-09-09 14:41:01 +03:00
|
|
|
template<typename DestinationType, typename Predicate>
|
2016-10-20 15:34:27 +03:00
|
|
|
bool DisconnectMatchingDestinationInputs(uint32_t aDestinationIndex,
|
2016-09-09 14:41:01 +03:00
|
|
|
Predicate aPredicate);
|
|
|
|
|
2015-09-30 14:14:26 +03:00
|
|
|
virtual void LastRelease() override
|
|
|
|
{
|
|
|
|
// We are about to be deleted, disconnect the object from the graph before
|
|
|
|
// the derived type is destroyed.
|
|
|
|
DisconnectFromGraph();
|
|
|
|
}
|
|
|
|
// Callers must hold a reference to 'this'.
|
2013-04-15 05:52:55 +04:00
|
|
|
void DisconnectFromGraph();
|
2016-10-20 15:34:27 +03:00
|
|
|
|
|
|
|
template<typename DestinationType>
|
|
|
|
bool DisconnectFromOutputIfConnected(uint32_t aOutputIndex, uint32_t aInputIndex);
|
2013-04-15 05:52:55 +04:00
|
|
|
|
2013-01-29 03:59:29 +04:00
|
|
|
protected:
|
2016-12-15 21:24:41 +03:00
|
|
|
// Helper for the Constructors for nodes.
|
|
|
|
void Initialize(const AudioNodeOptions& aOptions, ErrorResult& aRv);
|
|
|
|
|
2013-03-12 19:28:14 +04: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-28 03:25:23 +04:00
|
|
|
void SendChannelMixingParametersToStream();
|
2013-03-12 19:28:14 +04:00
|
|
|
|
2012-09-19 03:07:33 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioContext> mContext;
|
2013-01-24 04:50:18 +04:00
|
|
|
|
2013-02-05 03:07:25 +04:00
|
|
|
protected:
|
2015-06-10 14:31:29 +03:00
|
|
|
// Must be set in the constructor. Must not be null unless finished.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioNodeStream> mStream;
|
2013-02-05 03:07:25 +04:00
|
|
|
|
|
|
|
private:
|
2013-01-24 04:50:18 +04: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.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<AudioNode> > mOutputNodes;
|
2013-05-02 02:59:02 +04: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.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<AudioParam> > mOutputParams;
|
2013-04-28 02:44:50 +04:00
|
|
|
uint32_t mChannelCount;
|
|
|
|
ChannelCountMode mChannelCountMode;
|
|
|
|
ChannelInterpretation mChannelInterpretation;
|
2014-06-04 06:51:48 +04:00
|
|
|
const uint32_t mId;
|
2014-08-19 04:12:50 +04:00
|
|
|
// Whether the node just passes through its input. This is a devtools API that
|
|
|
|
// only works for some node types.
|
|
|
|
bool mPassThrough;
|
2012-09-19 03:07:33 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-09-19 03:07:33 +04:00
|
|
|
|
2012-10-31 01:39:38 +04:00
|
|
|
#endif
|