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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-05-07 20:31:15 -07:00
|
|
|
TrackTicks ticks =
|
|
|
|
WebAudioUtils::ConvertDestinationStreamTimeToSourceStreamTime(
|
|
|
|
aStreamTime, this, aRelativeToStream);
|
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
|
|
|
|
AudioNodeStream::SetBuffer(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(AudioNodeStream* aStream,
|
|
|
|
already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer)
|
|
|
|
: 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
|
|
|
|
AudioNodeStream::ComputeFinalOuputChannelCount(uint32_t aInputChannelCount)
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2013-07-24 03:11:35 -07:00
|
|
|
outputChannelCount = ComputeFinalOuputChannelCount(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
|
2013-12-05 12:23:57 -08:00
|
|
|
AudioNodeStream::ProduceOutput(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;
|
2013-12-12 04:33:01 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
for (uint16_t i = 0; i < outputCount; ++i) {
|
2014-01-28 15:55:10 -08:00
|
|
|
// Alter mDuration so we can detect if ProduceAudioBlock fails to set
|
|
|
|
// chunks.
|
|
|
|
mLastChunks[i].mDuration--;
|
2013-12-12 04:33:01 -08:00
|
|
|
}
|
|
|
|
#endif
|
2013-05-07 04:59:16 -07:00
|
|
|
if (maxInputs <= 1 && mEngine->OutputCount() <= 1) {
|
|
|
|
mEngine->ProduceAudioBlock(this, inputChunks[0], &mLastChunks[0], &finished);
|
|
|
|
} else {
|
|
|
|
mEngine->ProduceAudioBlocksOnPorts(this, inputChunks, mLastChunks, &finished);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|