2012-09-18 16:07:33 -07: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 14:39:38 -07:00
|
|
|
#ifndef AudioNode_h_
|
|
|
|
#define AudioNode_h_
|
2012-09-18 16:07:33 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2013-04-27 15:44:50 -07:00
|
|
|
#include "mozilla/dom/AudioNodeBinding.h"
|
2012-09-18 16:07:33 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2012-09-24 20:31:58 -07:00
|
|
|
#include "nsTArray.h"
|
2012-09-18 16:07:33 -07:00
|
|
|
#include "AudioContext.h"
|
2013-02-04 15:07:25 -08:00
|
|
|
#include "MediaStreamGraph.h"
|
2013-05-29 04:36:37 -07:00
|
|
|
#include "WebAudioUtils.h"
|
2014-04-13 11:08:10 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-06-03 19:51:48 -07: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 07:12:29 -07:00
|
|
|
#include "SelfRef.h"
|
2012-09-18 16:07:33 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-09-24 20:31:58 -07:00
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
namespace dom {
|
|
|
|
|
2013-08-15 12:44:14 -07:00
|
|
|
class AudioContext;
|
|
|
|
class AudioBufferSourceNode;
|
2013-05-01 15:59:02 -07:00
|
|
|
class AudioParam;
|
2013-08-15 12:44:14 -07:00
|
|
|
class AudioParamTimeline;
|
2013-03-12 08:28:14 -07:00
|
|
|
struct ThreeDPoint;
|
|
|
|
|
2013-02-04 15:07:25 -08: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 16:53:40 -07: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 16:11:01 -07: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 15:07:25 -08:00
|
|
|
*/
|
2014-06-03 19:51:48 -07:00
|
|
|
class AudioNode : public DOMEventTargetHelper,
|
|
|
|
public nsSupportsWeakReference
|
2012-09-18 16:07:33 -07:00
|
|
|
{
|
2013-04-24 13:57:07 -07:00
|
|
|
protected:
|
|
|
|
// You can only use refcounting to delete this object
|
|
|
|
virtual ~AudioNode();
|
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
public:
|
2013-04-27 15:44:50 -07:00
|
|
|
AudioNode(AudioContext* aContext,
|
|
|
|
uint32_t aChannelCount,
|
|
|
|
ChannelCountMode aChannelCountMode,
|
|
|
|
ChannelInterpretation aChannelInterpretation);
|
2012-09-18 16:07:33 -07:00
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
// This should be idempotent (safe to call multiple times).
|
2013-04-22 19:45:37 -07:00
|
|
|
virtual void DestroyMediaStream();
|
2013-02-04 15:07:25 -08:00
|
|
|
|
2013-04-24 13:57:07 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2013-04-12 08:28:33 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioNode,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-02-04 15:07:25 -08:00
|
|
|
|
2013-04-11 05:47:57 -07:00
|
|
|
virtual AudioBufferSourceNode* AsAudioBufferSourceNode() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
AudioContext* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext* Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:23:54 -07:00
|
|
|
virtual void Connect(AudioNode& aDestination, uint32_t aOutput,
|
|
|
|
uint32_t aInput, ErrorResult& aRv);
|
2012-09-24 20:31:58 -07:00
|
|
|
|
2013-05-01 15:59:02 -07:00
|
|
|
virtual void Connect(AudioParam& aDestination, uint32_t aOutput,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-04-22 18:23:54 -07:00
|
|
|
virtual void Disconnect(uint32_t aOutput, ErrorResult& aRv);
|
2012-09-24 20:31:58 -07:00
|
|
|
|
|
|
|
// The following two virtual methods must be implemented by each node type
|
2013-01-23 16:50:18 -08: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 08:48:45 -07:00
|
|
|
virtual uint16_t NumberOfInputs() const { return 1; }
|
|
|
|
virtual uint16_t NumberOfOutputs() const { return 1; }
|
2013-01-23 16:50:18 -08:00
|
|
|
|
2014-06-03 19:51:48 -07:00
|
|
|
uint32_t Id() const { return mId; }
|
|
|
|
|
2014-08-18 17:12:50 -07:00
|
|
|
bool PassThrough() const;
|
|
|
|
void SetPassThrough(bool aPassThrough);
|
|
|
|
|
2013-04-27 15:44:50 -07:00
|
|
|
uint32_t ChannelCount() const { return mChannelCount; }
|
2013-06-10 10:32:28 -07:00
|
|
|
virtual void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv)
|
2013-04-27 15:44:50 -07:00
|
|
|
{
|
2013-05-29 04:36:37 -07:00
|
|
|
if (aChannelCount == 0 ||
|
|
|
|
aChannelCount > WebAudioUtils::MaxChannelCount) {
|
2013-05-23 04:46:20 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-27 15:44:50 -07:00
|
|
|
mChannelCount = aChannelCount;
|
2013-04-27 16:25:23 -07:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-27 15:44:50 -07:00
|
|
|
}
|
|
|
|
ChannelCountMode ChannelCountModeValue() const
|
|
|
|
{
|
|
|
|
return mChannelCountMode;
|
|
|
|
}
|
2013-09-04 12:44:35 -07:00
|
|
|
virtual void SetChannelCountModeValue(ChannelCountMode aMode, ErrorResult& aRv)
|
2013-04-27 15:44:50 -07:00
|
|
|
{
|
|
|
|
mChannelCountMode = aMode;
|
2013-04-27 16:25:23 -07:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-27 15:44:50 -07:00
|
|
|
}
|
|
|
|
ChannelInterpretation ChannelInterpretationValue() const
|
|
|
|
{
|
|
|
|
return mChannelInterpretation;
|
|
|
|
}
|
|
|
|
void SetChannelInterpretationValue(ChannelInterpretation aMode)
|
|
|
|
{
|
|
|
|
mChannelInterpretation = aMode;
|
2013-04-27 16:25:23 -07:00
|
|
|
SendChannelMixingParametersToStream();
|
2013-04-27 15:44:50 -07:00
|
|
|
}
|
|
|
|
|
2013-01-23 16:50:18 -08:00
|
|
|
struct InputNode {
|
2013-02-04 15:07:25 -08:00
|
|
|
~InputNode()
|
|
|
|
{
|
|
|
|
if (mStreamPort) {
|
|
|
|
mStreamPort->Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
if (mStreamPort) {
|
|
|
|
amount += mStreamPort->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2013-04-14 18:52:55 -07:00
|
|
|
// Weak reference.
|
|
|
|
AudioNode* mInputNode;
|
2013-02-04 15:07:25 -08:00
|
|
|
nsRefPtr<MediaInputPort> mStreamPort;
|
2013-01-23 16:50:18 -08:00
|
|
|
// The index of the input port this node feeds into.
|
2013-05-01 15:59:02 -07:00
|
|
|
// This is not used for connections to AudioParams.
|
2013-01-23 16:50:18 -08:00
|
|
|
uint32_t mInputPort;
|
|
|
|
// The index of the output port this node comes out of.
|
|
|
|
uint32_t mOutputPort;
|
2012-09-24 20:31:58 -07:00
|
|
|
};
|
2012-09-18 16:07:33 -07:00
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
MediaStream* Stream() { return mStream; }
|
|
|
|
|
2013-04-11 05:47:57 -07:00
|
|
|
const nsTArray<InputNode>& InputNodes() const
|
|
|
|
{
|
|
|
|
return mInputNodes;
|
|
|
|
}
|
2013-09-23 18:46:30 -07:00
|
|
|
const nsTArray<nsRefPtr<AudioNode> >& OutputNodes() const
|
|
|
|
{
|
|
|
|
return mOutputNodes;
|
|
|
|
}
|
|
|
|
const nsTArray<nsRefPtr<AudioParam> >& OutputParams() const
|
|
|
|
{
|
|
|
|
return mOutputParams;
|
|
|
|
}
|
2013-04-11 05:47:57 -07:00
|
|
|
|
2013-05-01 15:59:02 -07:00
|
|
|
void RemoveOutputParam(AudioParam* aParam);
|
|
|
|
|
2013-09-16 16:53:40 -07: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 11:08:10 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
|
|
|
virtual const char* NodeType() const = 0;
|
|
|
|
|
2013-04-14 18:52:55 -07:00
|
|
|
private:
|
2013-04-30 16:20:55 -07:00
|
|
|
friend class AudioBufferSourceNode;
|
2013-04-14 18:52:55 -07:00
|
|
|
// This could possibly delete 'this'.
|
|
|
|
void DisconnectFromGraph();
|
|
|
|
|
2013-01-28 15:59:29 -08:00
|
|
|
protected:
|
|
|
|
static void Callback(AudioNode* aNode) { /* not implemented */ }
|
|
|
|
|
2013-03-12 08:28:14 -07: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 16:25:23 -07:00
|
|
|
void SendChannelMixingParametersToStream();
|
2013-03-29 18:46:03 -07:00
|
|
|
static void SendTimelineParameterToStream(AudioNode* aNode, uint32_t aIndex,
|
|
|
|
const AudioParamTimeline& aValue);
|
2013-03-12 08:28:14 -07:00
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<AudioContext> mContext;
|
2013-01-23 16:50:18 -08:00
|
|
|
|
2013-02-04 15:07:25 -08: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-23 16:50:18 -08: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 15:59:02 -07: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 15:44:50 -07:00
|
|
|
uint32_t mChannelCount;
|
|
|
|
ChannelCountMode mChannelCountMode;
|
|
|
|
ChannelInterpretation mChannelInterpretation;
|
2014-06-03 19:51:48 -07:00
|
|
|
const uint32_t mId;
|
2014-08-18 17:12:50 -07: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-03 19:51:48 -07: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 16:07:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 14:39:38 -07:00
|
|
|
#endif
|