2013-05-07 04:59:16 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
|
|
|
|
#include "MediaStreamGraphImpl.h"
|
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "ThreeDPoint.h"
|
2013-09-05 13:25:17 -07:00
|
|
|
#include "AudioChannelFormat.h"
|
|
|
|
#include "AudioParamTimeline.h"
|
2014-01-15 03:08:20 -08:00
|
|
|
#include "AudioContext.h"
|
2013-05-07 04:59:16 -07:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An AudioNodeStream produces a single audio track with ID
|
2013-10-08 11:20:33 -07:00
|
|
|
* AUDIO_TRACK. This track has rate AudioContext::sIdealAudioRate
|
2013-05-24 10:09:29 -07:00
|
|
|
* for regular audio contexts, and the rate requested by the web content
|
|
|
|
* for offline audio contexts.
|
2013-05-07 04:59:16 -07:00
|
|
|
* Each chunk in the track is a single block of WEBAUDIO_BLOCK_SIZE samples.
|
2013-05-21 12:17:47 -07:00
|
|
|
* Note: This must be a different value than MEDIA_STREAM_DEST_TRACK_ID
|
2013-05-07 04:59:16 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
AudioNodeStream::~AudioNodeStream()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(AudioNodeStream);
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
size_t
|
|
|
|
AudioNodeStream::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
|
|
|
|
// Not reported:
|
|
|
|
// - mEngine
|
|
|
|
|
|
|
|
amount += ProcessedMediaStream::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mLastChunks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (size_t i = 0; i < mLastChunks.Length(); i++) {
|
|
|
|
// NB: This is currently unshared only as there are instances of
|
|
|
|
// double reporting in DMD otherwise.
|
|
|
|
amount += mLastChunks[i].SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
AudioNodeStream::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::SizeOfAudioNodesIncludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
AudioNodeSizes& aUsage) const
|
|
|
|
{
|
|
|
|
// Explicitly separate out the stream memory.
|
|
|
|
aUsage.mStream = SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
if (mEngine) {
|
|
|
|
// This will fill out the rest of |aUsage|.
|
|
|
|
mEngine->SizeOfIncludingThis(aMallocSizeOf, aUsage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 04:59:16 -07:00
|
|
|
void
|
2014-01-15 03:08:20 -08:00
|
|
|
AudioNodeStream::SetStreamTimeParameter(uint32_t aIndex, AudioContext* aContext,
|
2013-05-07 04:59:16 -07:00
|
|
|
double aStreamTime)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream, uint32_t aIndex, MediaStream* aRelativeToStream,
|
|
|
|
double aStreamTime)
|
|
|
|
: ControlMessage(aStream), mStreamTime(aStreamTime),
|
|
|
|
mRelativeToStream(aRelativeToStream), mIndex(aIndex) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->
|
|
|
|
SetStreamTimeParameterImpl(mIndex, mRelativeToStream, mStreamTime);
|
|
|
|
}
|
|
|
|
double mStreamTime;
|
|
|
|
MediaStream* mRelativeToStream;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOZ_ASSERT(this);
|
2014-01-15 03:08:20 -08:00
|
|
|
GraphImpl()->AppendMessage(new Message(this, aIndex,
|
|
|
|
aContext->DestinationStream(),
|
|
|
|
aContext->DOMTimeToStreamTime(aStreamTime)));
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::SetStreamTimeParameterImpl(uint32_t aIndex, MediaStream* aRelativeToStream,
|
|
|
|
double aStreamTime)
|
|
|
|
{
|
2014-02-16 12:46:56 -08:00
|
|
|
TrackTicks ticks = TicksFromDestinationTime(aRelativeToStream, aStreamTime);
|
2013-05-07 04:59:16 -07:00
|
|
|
mEngine->SetStreamTimeParameter(aIndex, ticks);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::SetDoubleParameter(uint32_t aIndex, double aValue)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream, uint32_t aIndex, double aValue)
|
|
|
|
: ControlMessage(aStream), mValue(aValue), mIndex(aIndex) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->Engine()->
|
|
|
|
SetDoubleParameter(mIndex, mValue);
|
|
|
|
}
|
|
|
|
double mValue;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOZ_ASSERT(this);
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aIndex, aValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::SetInt32Parameter(uint32_t aIndex, int32_t aValue)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream, uint32_t aIndex, int32_t aValue)
|
|
|
|
: ControlMessage(aStream), mValue(aValue), mIndex(aIndex) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->Engine()->
|
|
|
|
SetInt32Parameter(mIndex, mValue);
|
|
|
|
}
|
|
|
|
int32_t mValue;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOZ_ASSERT(this);
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aIndex, aValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::SetTimelineParameter(uint32_t aIndex,
|
|
|
|
const AudioParamTimeline& aValue)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream, uint32_t aIndex,
|
|
|
|
const AudioParamTimeline& aValue)
|
2013-05-24 10:10:08 -07:00
|
|
|
: ControlMessage(aStream),
|
|
|
|
mValue(aValue),
|
|
|
|
mSampleRate(aStream->SampleRate()),
|
|
|
|
mIndex(aIndex) {}
|
2013-05-07 04:59:16 -07:00
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->Engine()->
|
2013-05-24 10:10:08 -07:00
|
|
|
SetTimelineParameter(mIndex, mValue, mSampleRate);
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
|
|
|
AudioParamTimeline mValue;
|
2013-05-24 10:10:08 -07:00
|
|
|
TrackRate mSampleRate;
|
2013-05-07 04:59:16 -07:00
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aIndex, aValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::SetThreeDPointParameter(uint32_t aIndex, const ThreeDPoint& aValue)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream, uint32_t aIndex, const ThreeDPoint& aValue)
|
|
|
|
: ControlMessage(aStream), mValue(aValue), mIndex(aIndex) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->Engine()->
|
|
|
|
SetThreeDPointParameter(mIndex, mValue);
|
|
|
|
}
|
|
|
|
ThreeDPoint mValue;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOZ_ASSERT(this);
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aIndex, aValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-15 12:00:17 -07:00
|
|
|
AudioNodeStream::SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList>&& aBuffer)
|
2013-05-07 04:59:16 -07:00
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream,
|
2014-03-15 12:00:17 -07:00
|
|
|
already_AddRefed<ThreadSharedFloatArrayBufferList>& aBuffer)
|
2013-05-07 04:59:16 -07:00
|
|
|
: ControlMessage(aStream), mBuffer(aBuffer) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->Engine()->
|
|
|
|
SetBuffer(mBuffer.forget());
|
|
|
|
}
|
|
|
|
nsRefPtr<ThreadSharedFloatArrayBufferList> mBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOZ_ASSERT(this);
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aBuffer));
|
|
|
|
}
|
|
|
|
|
2013-05-13 21:12:30 -07:00
|
|
|
void
|
|
|
|
AudioNodeStream::SetRawArrayData(nsTArray<float>& aData)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream,
|
|
|
|
nsTArray<float>& aData)
|
|
|
|
: ControlMessage(aStream)
|
|
|
|
{
|
|
|
|
mData.SwapElements(aData);
|
|
|
|
}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->Engine()->SetRawArrayData(mData);
|
|
|
|
}
|
|
|
|
nsTArray<float> mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOZ_ASSERT(this);
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aData));
|
|
|
|
}
|
|
|
|
|
2013-05-07 04:59:16 -07:00
|
|
|
void
|
|
|
|
AudioNodeStream::SetChannelMixingParameters(uint32_t aNumberOfChannels,
|
|
|
|
ChannelCountMode aChannelCountMode,
|
|
|
|
ChannelInterpretation aChannelInterpretation)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream,
|
|
|
|
uint32_t aNumberOfChannels,
|
|
|
|
ChannelCountMode aChannelCountMode,
|
|
|
|
ChannelInterpretation aChannelInterpretation)
|
|
|
|
: ControlMessage(aStream),
|
|
|
|
mNumberOfChannels(aNumberOfChannels),
|
|
|
|
mChannelCountMode(aChannelCountMode),
|
|
|
|
mChannelInterpretation(aChannelInterpretation)
|
|
|
|
{}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
static_cast<AudioNodeStream*>(mStream)->
|
|
|
|
SetChannelMixingParametersImpl(mNumberOfChannels, mChannelCountMode,
|
|
|
|
mChannelInterpretation);
|
|
|
|
}
|
|
|
|
uint32_t mNumberOfChannels;
|
|
|
|
ChannelCountMode mChannelCountMode;
|
|
|
|
ChannelInterpretation mChannelInterpretation;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOZ_ASSERT(this);
|
|
|
|
GraphImpl()->AppendMessage(new Message(this, aNumberOfChannels,
|
|
|
|
aChannelCountMode,
|
|
|
|
aChannelInterpretation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::SetChannelMixingParametersImpl(uint32_t aNumberOfChannels,
|
|
|
|
ChannelCountMode aChannelCountMode,
|
|
|
|
ChannelInterpretation aChannelInterpretation)
|
|
|
|
{
|
|
|
|
// Make sure that we're not clobbering any significant bits by fitting these
|
|
|
|
// values in 16 bits.
|
|
|
|
MOZ_ASSERT(int(aChannelCountMode) < INT16_MAX);
|
|
|
|
MOZ_ASSERT(int(aChannelInterpretation) < INT16_MAX);
|
|
|
|
|
|
|
|
mNumberOfInputChannels = aNumberOfChannels;
|
2013-05-06 12:28:13 -07:00
|
|
|
mChannelCountMode = aChannelCountMode;
|
|
|
|
mChannelInterpretation = aChannelInterpretation;
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
|
|
|
|
2013-07-24 03:11:35 -07:00
|
|
|
uint32_t
|
2014-03-31 14:26:02 -07:00
|
|
|
AudioNodeStream::ComputedNumberOfChannels(uint32_t aInputChannelCount)
|
2013-07-24 03:11:35 -07:00
|
|
|
{
|
|
|
|
switch (mChannelCountMode) {
|
|
|
|
case ChannelCountMode::Explicit:
|
|
|
|
// Disregard the channel count we've calculated from inputs, and just use
|
|
|
|
// mNumberOfInputChannels.
|
|
|
|
return mNumberOfInputChannels;
|
|
|
|
case ChannelCountMode::Clamped_max:
|
|
|
|
// Clamp the computed output channel count to mNumberOfInputChannels.
|
|
|
|
return std::min(aInputChannelCount, mNumberOfInputChannels);
|
|
|
|
default:
|
|
|
|
case ChannelCountMode::Max:
|
|
|
|
// Nothing to do here, just shut up the compiler warning.
|
|
|
|
return aInputChannelCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 04:59:16 -07:00
|
|
|
void
|
|
|
|
AudioNodeStream::ObtainInputBlock(AudioChunk& aTmpChunk, uint32_t aPortIndex)
|
|
|
|
{
|
|
|
|
uint32_t inputCount = mInputs.Length();
|
|
|
|
uint32_t outputChannelCount = 1;
|
|
|
|
nsAutoTArray<AudioChunk*,250> inputChunks;
|
|
|
|
for (uint32_t i = 0; i < inputCount; ++i) {
|
|
|
|
if (aPortIndex != mInputs[i]->InputNumber()) {
|
|
|
|
// This input is connected to a different port
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MediaStream* s = mInputs[i]->GetSource();
|
|
|
|
AudioNodeStream* a = static_cast<AudioNodeStream*>(s);
|
|
|
|
MOZ_ASSERT(a == s->AsAudioNodeStream());
|
2013-12-09 16:49:03 -08:00
|
|
|
if (a->IsAudioParamStream()) {
|
2013-05-07 04:59:16 -07:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-26 10:19:36 -07:00
|
|
|
|
|
|
|
// It is possible for mLastChunks to be empty here, because `a` might be a
|
|
|
|
// AudioNodeStream that has not been scheduled yet, because it is further
|
|
|
|
// down the graph _but_ as a connection to this node. Because we enforce the
|
|
|
|
// presence of at least one DelayNode, with at least one block of delay, and
|
|
|
|
// because the output of a DelayNode when it has been fed less that
|
|
|
|
// `delayTime` amount of audio is silence, we can simply continue here,
|
|
|
|
// because this input would not influence the output of this node. Next
|
|
|
|
// iteration, a->mLastChunks.IsEmpty() will be false, and everthing will
|
|
|
|
// work as usual.
|
|
|
|
if (a->mLastChunks.IsEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-05-07 04:59:16 -07:00
|
|
|
AudioChunk* chunk = &a->mLastChunks[mInputs[i]->OutputNumber()];
|
|
|
|
MOZ_ASSERT(chunk);
|
2013-08-16 07:42:50 -07:00
|
|
|
if (chunk->IsNull() || chunk->mChannelData.IsEmpty()) {
|
2013-05-07 04:59:16 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputChunks.AppendElement(chunk);
|
|
|
|
outputChannelCount =
|
|
|
|
GetAudioChannelsSuperset(outputChannelCount, chunk->mChannelData.Length());
|
|
|
|
}
|
|
|
|
|
2014-03-31 14:26:02 -07:00
|
|
|
outputChannelCount = ComputedNumberOfChannels(outputChannelCount);
|
2013-05-07 04:59:16 -07:00
|
|
|
|
|
|
|
uint32_t inputChunkCount = inputChunks.Length();
|
2013-05-07 20:31:55 -07:00
|
|
|
if (inputChunkCount == 0 ||
|
|
|
|
(inputChunkCount == 1 && inputChunks[0]->mChannelData.Length() == 0)) {
|
2013-05-07 04:59:16 -07:00
|
|
|
aTmpChunk.SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputChunkCount == 1 &&
|
|
|
|
inputChunks[0]->mChannelData.Length() == outputChannelCount) {
|
|
|
|
aTmpChunk = *inputChunks[0];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-29 04:40:14 -07:00
|
|
|
if (outputChannelCount == 0) {
|
|
|
|
aTmpChunk.SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-23 04:46:20 -07:00
|
|
|
|
2013-05-07 04:59:16 -07:00
|
|
|
AllocateAudioBlock(outputChannelCount, &aTmpChunk);
|
|
|
|
// The static storage here should be 1KB, so it's fine
|
|
|
|
nsAutoTArray<float, GUESS_AUDIO_CHANNELS*WEBAUDIO_BLOCK_SIZE> downmixBuffer;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < inputChunkCount; ++i) {
|
2013-07-24 03:11:35 -07:00
|
|
|
AccumulateInputChunk(i, *inputChunks[i], &aTmpChunk, &downmixBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::AccumulateInputChunk(uint32_t aInputIndex, const AudioChunk& aChunk,
|
|
|
|
AudioChunk* aBlock,
|
|
|
|
nsTArray<float>* aDownmixBuffer)
|
|
|
|
{
|
|
|
|
nsAutoTArray<const void*,GUESS_AUDIO_CHANNELS> channels;
|
|
|
|
UpMixDownMixChunk(&aChunk, aBlock->mChannelData.Length(), channels, *aDownmixBuffer);
|
|
|
|
|
|
|
|
for (uint32_t c = 0; c < channels.Length(); ++c) {
|
|
|
|
const float* inputData = static_cast<const float*>(channels[c]);
|
|
|
|
float* outputData = static_cast<float*>(const_cast<void*>(aBlock->mChannelData[c]));
|
|
|
|
if (inputData) {
|
|
|
|
if (aInputIndex == 0) {
|
|
|
|
AudioBlockCopyChannelWithScale(inputData, aChunk.mVolume, outputData);
|
2013-05-07 04:59:16 -07:00
|
|
|
} else {
|
2013-07-24 03:11:35 -07:00
|
|
|
AudioBlockAddChannelWithScale(inputData, aChunk.mVolume, outputData);
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
2013-07-24 03:11:35 -07:00
|
|
|
} else {
|
|
|
|
if (aInputIndex == 0) {
|
|
|
|
PodZero(outputData, WEBAUDIO_BLOCK_SIZE);
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
|
|
|
}
|
2013-07-24 03:11:35 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-07 04:59:16 -07:00
|
|
|
|
2013-07-24 03:11:35 -07:00
|
|
|
void
|
|
|
|
AudioNodeStream::UpMixDownMixChunk(const AudioChunk* aChunk,
|
|
|
|
uint32_t aOutputChannelCount,
|
|
|
|
nsTArray<const void*>& aOutputChannels,
|
|
|
|
nsTArray<float>& aDownmixBuffer)
|
|
|
|
{
|
|
|
|
static const float silenceChannel[WEBAUDIO_BLOCK_SIZE] = {0.f};
|
|
|
|
|
|
|
|
aOutputChannels.AppendElements(aChunk->mChannelData);
|
|
|
|
if (aOutputChannels.Length() < aOutputChannelCount) {
|
|
|
|
if (mChannelInterpretation == ChannelInterpretation::Speakers) {
|
|
|
|
AudioChannelsUpMix(&aOutputChannels, aOutputChannelCount, nullptr);
|
|
|
|
NS_ASSERTION(aOutputChannelCount == aOutputChannels.Length(),
|
|
|
|
"We called GetAudioChannelsSuperset to avoid this");
|
|
|
|
} else {
|
|
|
|
// Fill up the remaining aOutputChannels by zeros
|
|
|
|
for (uint32_t j = aOutputChannels.Length(); j < aOutputChannelCount; ++j) {
|
|
|
|
aOutputChannels.AppendElement(silenceChannel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (aOutputChannels.Length() > aOutputChannelCount) {
|
|
|
|
if (mChannelInterpretation == ChannelInterpretation::Speakers) {
|
|
|
|
nsAutoTArray<float*,GUESS_AUDIO_CHANNELS> outputChannels;
|
|
|
|
outputChannels.SetLength(aOutputChannelCount);
|
|
|
|
aDownmixBuffer.SetLength(aOutputChannelCount * WEBAUDIO_BLOCK_SIZE);
|
|
|
|
for (uint32_t j = 0; j < aOutputChannelCount; ++j) {
|
|
|
|
outputChannels[j] = &aDownmixBuffer[j * WEBAUDIO_BLOCK_SIZE];
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelsDownMix(aOutputChannels, outputChannels.Elements(),
|
|
|
|
aOutputChannelCount, WEBAUDIO_BLOCK_SIZE);
|
|
|
|
|
|
|
|
aOutputChannels.SetLength(aOutputChannelCount);
|
|
|
|
for (uint32_t j = 0; j < aOutputChannels.Length(); ++j) {
|
|
|
|
aOutputChannels[j] = outputChannels[j];
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
2013-07-24 03:11:35 -07:00
|
|
|
} else {
|
|
|
|
// Drop the remaining aOutputChannels
|
|
|
|
aOutputChannels.RemoveElementsAt(aOutputChannelCount,
|
|
|
|
aOutputChannels.Length() - aOutputChannelCount);
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The MediaStreamGraph guarantees that this is actually one block, for
|
|
|
|
// AudioNodeStreams.
|
|
|
|
void
|
2014-03-04 13:53:55 -08:00
|
|
|
AudioNodeStream::ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags)
|
2013-05-07 04:59:16 -07:00
|
|
|
{
|
2013-10-08 11:20:33 -07:00
|
|
|
EnsureTrack(AUDIO_TRACK, mSampleRate);
|
2013-12-05 12:23:57 -08:00
|
|
|
// No more tracks will be coming
|
|
|
|
mBuffer.AdvanceKnownTracksTime(STREAM_TIME_MAX);
|
2013-05-07 04:59:16 -07:00
|
|
|
|
2013-05-23 19:36:20 -07:00
|
|
|
uint16_t outputCount = std::max(uint16_t(1), mEngine->OutputCount());
|
|
|
|
mLastChunks.SetLength(outputCount);
|
2013-05-07 04:59:16 -07:00
|
|
|
|
2014-01-15 02:07:30 -08:00
|
|
|
// Consider this stream blocked if it has already finished output. Normally
|
|
|
|
// mBlocked would reflect this, but due to rounding errors our audio track may
|
|
|
|
// appear to extend slightly beyond aFrom, so we might not be blocked yet.
|
|
|
|
bool blocked = mFinished || mBlocked.GetAt(aFrom);
|
|
|
|
// If the stream has finished at this time, it will be blocked.
|
|
|
|
if (mMuted || blocked) {
|
2013-05-23 19:36:20 -07:00
|
|
|
for (uint16_t i = 0; i < outputCount; ++i) {
|
|
|
|
mLastChunks[i].SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
}
|
2013-05-07 04:59:16 -07:00
|
|
|
} else {
|
|
|
|
// We need to generate at least one input
|
|
|
|
uint16_t maxInputs = std::max(uint16_t(1), mEngine->InputCount());
|
|
|
|
OutputChunks inputChunks;
|
|
|
|
inputChunks.SetLength(maxInputs);
|
|
|
|
for (uint16_t i = 0; i < maxInputs; ++i) {
|
|
|
|
ObtainInputBlock(inputChunks[i], i);
|
|
|
|
}
|
|
|
|
bool finished = false;
|
|
|
|
if (maxInputs <= 1 && mEngine->OutputCount() <= 1) {
|
2014-03-04 13:09:49 -08:00
|
|
|
mEngine->ProcessBlock(this, inputChunks[0], &mLastChunks[0], &finished);
|
2013-05-07 04:59:16 -07:00
|
|
|
} else {
|
2014-03-04 13:09:49 -08:00
|
|
|
mEngine->ProcessBlocksOnPorts(this, inputChunks, mLastChunks, &finished);
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
2013-12-12 04:33:01 -08:00
|
|
|
for (uint16_t i = 0; i < outputCount; ++i) {
|
|
|
|
NS_ASSERTION(mLastChunks[i].GetDuration() == WEBAUDIO_BLOCK_SIZE,
|
|
|
|
"Invalid WebAudio chunk size");
|
|
|
|
}
|
2013-05-07 04:59:16 -07:00
|
|
|
if (finished) {
|
|
|
|
mMarkAsFinishedAfterThisBlock = true;
|
|
|
|
}
|
|
|
|
|
2014-01-15 02:07:30 -08:00
|
|
|
if (mDisabledTrackIDs.Contains(static_cast<TrackID>(AUDIO_TRACK))) {
|
|
|
|
for (uint32_t i = 0; i < outputCount; ++i) {
|
|
|
|
mLastChunks[i].SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
}
|
2013-05-29 21:44:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 02:07:30 -08:00
|
|
|
if (!blocked) {
|
|
|
|
// Don't output anything while blocked
|
2013-12-05 12:23:57 -08:00
|
|
|
AdvanceOutputSegment();
|
|
|
|
if (mMarkAsFinishedAfterThisBlock && (aFlags & ALLOW_FINISH)) {
|
|
|
|
// This stream was finished the last time that we looked at it, and all
|
|
|
|
// of the depending streams have finished their output as well, so now
|
|
|
|
// it's time to mark this stream as finished.
|
|
|
|
FinishOutput();
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 03:11:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::AdvanceOutputSegment()
|
|
|
|
{
|
2013-10-08 11:20:33 -07:00
|
|
|
StreamBuffer::Track* track = EnsureTrack(AUDIO_TRACK, mSampleRate);
|
2013-07-24 03:11:35 -07:00
|
|
|
AudioSegment* segment = track->Get<AudioSegment>();
|
|
|
|
|
2013-05-07 04:59:16 -07:00
|
|
|
if (mKind == MediaStreamGraph::EXTERNAL_STREAM) {
|
|
|
|
segment->AppendAndConsumeChunk(&mLastChunks[0]);
|
|
|
|
} else {
|
|
|
|
segment->AppendNullData(mLastChunks[0].GetDuration());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
|
|
|
AudioChunk copyChunk = mLastChunks[0];
|
|
|
|
AudioSegment tmpSegment;
|
|
|
|
tmpSegment.AppendAndConsumeChunk(©Chunk);
|
2013-10-08 11:20:33 -07:00
|
|
|
l->NotifyQueuedTrackChanges(Graph(), AUDIO_TRACK,
|
2013-05-24 10:09:29 -07:00
|
|
|
mSampleRate, segment->GetDuration(), 0,
|
2013-05-07 04:59:16 -07:00
|
|
|
tmpSegment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackTicks
|
|
|
|
AudioNodeStream::GetCurrentPosition()
|
|
|
|
{
|
2013-10-08 11:20:33 -07:00
|
|
|
return EnsureTrack(AUDIO_TRACK, mSampleRate)->Get<AudioSegment>()->GetDuration();
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioNodeStream::FinishOutput()
|
|
|
|
{
|
|
|
|
if (IsFinishedOnGraphThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-08 11:20:33 -07:00
|
|
|
StreamBuffer::Track* track = EnsureTrack(AUDIO_TRACK, mSampleRate);
|
2013-05-07 04:59:16 -07:00
|
|
|
track->SetEnded();
|
|
|
|
FinishOnGraphThread();
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
|
|
|
AudioSegment emptySegment;
|
2013-10-08 11:20:33 -07:00
|
|
|
l->NotifyQueuedTrackChanges(Graph(), AUDIO_TRACK,
|
2013-05-24 10:09:29 -07:00
|
|
|
mSampleRate,
|
2013-05-07 04:59:16 -07:00
|
|
|
track->GetSegment()->GetDuration(),
|
|
|
|
MediaStreamListener::TRACK_EVENT_ENDED, emptySegment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 14:45:04 -08:00
|
|
|
double
|
|
|
|
AudioNodeStream::TimeFromDestinationTime(AudioNodeStream* aDestination,
|
|
|
|
double aSeconds)
|
2014-02-16 12:46:56 -08:00
|
|
|
{
|
2014-02-26 14:45:04 -08:00
|
|
|
MOZ_ASSERT(aDestination->SampleRate() == SampleRate());
|
2014-02-16 12:47:19 -08:00
|
|
|
|
|
|
|
double destinationSeconds = std::max(0.0, aSeconds);
|
|
|
|
StreamTime streamTime = SecondsToMediaTime(destinationSeconds);
|
|
|
|
// MediaTime does not have the resolution of double
|
|
|
|
double offset = destinationSeconds - MediaTimeToSeconds(streamTime);
|
|
|
|
|
2014-02-16 12:46:56 -08:00
|
|
|
GraphTime graphTime = aDestination->StreamTimeToGraphTime(streamTime);
|
|
|
|
StreamTime thisStreamTime = GraphTimeToStreamTimeOptimistic(graphTime);
|
2014-02-16 12:47:19 -08:00
|
|
|
double thisSeconds = MediaTimeToSeconds(thisStreamTime) + offset;
|
|
|
|
MOZ_ASSERT(thisSeconds >= 0.0);
|
2014-02-26 14:45:04 -08:00
|
|
|
return thisSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackTicks
|
|
|
|
AudioNodeStream::TicksFromDestinationTime(MediaStream* aDestination,
|
|
|
|
double aSeconds)
|
|
|
|
{
|
|
|
|
AudioNodeStream* destination = aDestination->AsAudioNodeStream();
|
|
|
|
MOZ_ASSERT(destination);
|
|
|
|
|
|
|
|
double thisSeconds = TimeFromDestinationTime(destination, aSeconds);
|
2014-02-16 12:47:19 -08:00
|
|
|
// Round to nearest
|
|
|
|
TrackTicks ticks = thisSeconds * SampleRate() + 0.5;
|
2014-02-16 12:46:56 -08:00
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
AudioNodeStream::DestinationTimeFromTicks(AudioNodeStream* aDestination,
|
|
|
|
TrackTicks aPosition)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(SampleRate() == aDestination->SampleRate());
|
|
|
|
StreamTime sourceTime = TicksToTimeRoundDown(SampleRate(), aPosition);
|
|
|
|
GraphTime graphTime = StreamTimeToGraphTime(sourceTime);
|
|
|
|
StreamTime destinationTime = aDestination->GraphTimeToStreamTimeOptimistic(graphTime);
|
|
|
|
return MediaTimeToSeconds(destinationTime);
|
|
|
|
}
|
|
|
|
|
2013-05-07 04:59:16 -07:00
|
|
|
}
|