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"
|
|
|
|
#include "nsAutoPtr.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
|
|
|
|
2013-04-11 16:47:57 +04:00
|
|
|
virtual AudioBufferSourceNode* AsAudioBufferSourceNode() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-19 03:07:33 +04:00
|
|
|
AudioContext* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext* Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
2013-04-23 05:23:54 +04:00
|
|
|
virtual void 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);
|
|
|
|
|
2013-04-23 05:23:54 +04:00
|
|
|
virtual void Disconnect(uint32_t aOutput, ErrorResult& aRv);
|
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
|
|
|
}
|
|
|
|
|
2013-01-24 04:50:18 +04:00
|
|
|
struct InputNode {
|
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;
|
2013-02-05 03:07:25 +04:00
|
|
|
nsRefPtr<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
|
|
|
|
2013-02-05 03:07:25 +04:00
|
|
|
MediaStream* Stream() { return mStream; }
|
|
|
|
|
2013-04-11 16:47:57 +04:00
|
|
|
const nsTArray<InputNode>& InputNodes() const
|
|
|
|
{
|
|
|
|
return mInputNodes;
|
|
|
|
}
|
2013-09-24 05:46:30 +04:00
|
|
|
const nsTArray<nsRefPtr<AudioNode> >& OutputNodes() const
|
|
|
|
{
|
|
|
|
return mOutputNodes;
|
|
|
|
}
|
|
|
|
const nsTArray<nsRefPtr<AudioParam> >& OutputParams() const
|
|
|
|
{
|
|
|
|
return mOutputParams;
|
|
|
|
}
|
2013-04-11 16:47:57 +04:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
virtual const char* NodeType() const = 0;
|
|
|
|
|
2013-04-15 05:52:55 +04:00
|
|
|
private:
|
2013-05-01 03:20:55 +04:00
|
|
|
friend class AudioBufferSourceNode;
|
2013-04-15 05:52:55 +04:00
|
|
|
// This could possibly delete 'this'.
|
|
|
|
void DisconnectFromGraph();
|
|
|
|
|
2013-01-29 03:59:29 +04:00
|
|
|
protected:
|
|
|
|
static void Callback(AudioNode* aNode) { /* not implemented */ }
|
|
|
|
|
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-30 05:46:03 +04:00
|
|
|
static void SendTimelineParameterToStream(AudioNode* aNode, uint32_t aIndex,
|
|
|
|
const AudioParamTimeline& aValue);
|
2013-03-12 19:28:14 +04:00
|
|
|
|
2012-09-19 03:07:33 +04:00
|
|
|
private:
|
|
|
|
nsRefPtr<AudioContext> mContext;
|
2013-01-24 04:50:18 +04:00
|
|
|
|
2013-02-05 03:07:25 +04: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 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.
|
|
|
|
nsTArray<nsRefPtr<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.
|
|
|
|
nsTArray<nsRefPtr<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;
|
2014-06-04 06:51:48 +04: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-19 03:07:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 01:39:38 +04:00
|
|
|
#endif
|