2013-07-24 14:15:11 +04: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 "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeExternalInputStream.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "AudioChannelFormat.h"
|
2014-04-25 18:32:00 +04:00
|
|
|
#include "mozilla/dom/MediaStreamAudioSourceNode.h"
|
2013-07-24 14:15:11 +04:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-03-30 16:14:09 +03:00
|
|
|
AudioNodeExternalInputStream::AudioNodeExternalInputStream(AudioNodeEngine* aEngine, TrackRate aSampleRate)
|
|
|
|
: AudioNodeStream(aEngine, MediaStreamGraph::INTERNAL_STREAM, aSampleRate)
|
2013-07-24 14:15:11 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(AudioNodeExternalInputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioNodeExternalInputStream::~AudioNodeExternalInputStream()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(AudioNodeExternalInputStream);
|
|
|
|
}
|
|
|
|
|
2014-09-18 03:51:21 +04:00
|
|
|
/**
|
|
|
|
* Copies the data in aInput to aOffsetInBlock within aBlock.
|
|
|
|
* aBlock must have been allocated with AllocateInputBlock and have a channel
|
|
|
|
* count that's a superset of the channels in aInput.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CopyChunkToBlock(const AudioChunk& aInput, AudioChunk *aBlock,
|
|
|
|
uint32_t aOffsetInBlock)
|
2013-07-24 14:15:11 +04:00
|
|
|
{
|
2014-09-18 03:51:21 +04:00
|
|
|
uint32_t blockChannels = aBlock->ChannelCount();
|
|
|
|
nsAutoTArray<const void*,2> channels;
|
|
|
|
if (aInput.IsNull()) {
|
|
|
|
channels.SetLength(blockChannels);
|
|
|
|
PodZero(channels.Elements(), blockChannels);
|
|
|
|
} else {
|
|
|
|
channels.SetLength(aInput.ChannelCount());
|
|
|
|
PodCopy(channels.Elements(), aInput.mChannelData.Elements(), channels.Length());
|
|
|
|
if (channels.Length() != blockChannels) {
|
|
|
|
// We only need to upmix here because aBlock's channel count has been
|
|
|
|
// chosen to be a superset of the channel count of every chunk.
|
|
|
|
AudioChannelsUpMix(&channels, blockChannels, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t duration = aInput.GetDuration();
|
|
|
|
for (uint32_t c = 0; c < blockChannels; ++c) {
|
|
|
|
float* outputData =
|
|
|
|
static_cast<float*>(const_cast<void*>(aBlock->mChannelData[c])) + aOffsetInBlock;
|
|
|
|
if (channels[c]) {
|
|
|
|
switch (aInput.mBufferFormat) {
|
2013-07-24 14:15:11 +04:00
|
|
|
case AUDIO_FORMAT_FLOAT32:
|
2014-09-18 03:51:21 +04:00
|
|
|
ConvertAudioSamplesWithScale(
|
|
|
|
static_cast<const float*>(channels[c]), outputData, duration,
|
|
|
|
aInput.mVolume);
|
2013-07-24 14:15:11 +04:00
|
|
|
break;
|
|
|
|
case AUDIO_FORMAT_S16:
|
2014-09-18 03:51:21 +04:00
|
|
|
ConvertAudioSamplesWithScale(
|
|
|
|
static_cast<const int16_t*>(channels[c]), outputData, duration,
|
|
|
|
aInput.mVolume);
|
2013-07-24 14:15:11 +04:00
|
|
|
break;
|
|
|
|
default:
|
2014-09-18 03:51:21 +04:00
|
|
|
NS_ERROR("Unhandled format");
|
2013-07-24 14:15:11 +04:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-18 03:51:21 +04:00
|
|
|
PodZero(outputData, duration);
|
2013-07-24 14:15:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-18 03:51:21 +04:00
|
|
|
* Converts the data in aSegment to a single chunk aBlock. aSegment must have
|
|
|
|
* duration WEBAUDIO_BLOCK_SIZE. aFallbackChannelCount is a superset of the
|
|
|
|
* channels in every chunk of aSegment. aBlock must be float format or null.
|
2013-07-24 14:15:11 +04:00
|
|
|
*/
|
2014-09-18 03:51:21 +04:00
|
|
|
static void ConvertSegmentToAudioBlock(AudioSegment* aSegment,
|
|
|
|
AudioChunk* aBlock,
|
|
|
|
int32_t aFallbackChannelCount)
|
2013-07-24 14:15:11 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aSegment->GetDuration() == WEBAUDIO_BLOCK_SIZE, "Bad segment duration");
|
|
|
|
|
|
|
|
{
|
|
|
|
AudioSegment::ChunkIterator ci(*aSegment);
|
2014-09-18 03:51:21 +04:00
|
|
|
NS_ASSERTION(!ci.IsEnded(), "Should be at least one chunk!");
|
|
|
|
if (ci->GetDuration() == WEBAUDIO_BLOCK_SIZE &&
|
|
|
|
(ci->IsNull() || ci->mBufferFormat == AUDIO_FORMAT_FLOAT32)) {
|
|
|
|
// Return this chunk directly to avoid copying data.
|
|
|
|
*aBlock = *ci;
|
2013-07-24 14:15:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 03:51:21 +04:00
|
|
|
AllocateAudioBlock(aFallbackChannelCount, aBlock);
|
|
|
|
|
2013-07-24 14:15:11 +04:00
|
|
|
uint32_t duration = 0;
|
2014-09-18 03:51:21 +04:00
|
|
|
for (AudioSegment::ChunkIterator ci(*aSegment); !ci.IsEnded(); ci.Next()) {
|
2013-07-24 14:15:11 +04:00
|
|
|
CopyChunkToBlock(*ci, aBlock, duration);
|
|
|
|
duration += ci->GetDuration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-05 01:53:55 +04:00
|
|
|
AudioNodeExternalInputStream::ProcessInput(GraphTime aFrom, GraphTime aTo,
|
|
|
|
uint32_t aFlags)
|
2013-07-24 14:15:11 +04:00
|
|
|
{
|
|
|
|
// According to spec, number of outputs is always 1.
|
2014-11-18 06:22:45 +03:00
|
|
|
MOZ_ASSERT(mLastChunks.Length() == 1);
|
2013-07-24 14:15:11 +04:00
|
|
|
|
|
|
|
// GC stuff can result in our input stream being destroyed before this stream.
|
|
|
|
// Handle that.
|
2014-08-19 16:12:43 +04:00
|
|
|
if (!IsEnabled() || mInputs.IsEmpty() || mPassThrough) {
|
2013-07-24 14:15:11 +04:00
|
|
|
mLastChunks[0].SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
AdvanceOutputSegment();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mInputs.Length() == 1);
|
|
|
|
|
|
|
|
MediaStream* source = mInputs[0]->GetSource();
|
|
|
|
nsAutoTArray<AudioSegment,1> audioSegments;
|
|
|
|
uint32_t inputChannels = 0;
|
|
|
|
for (StreamBuffer::TrackIter tracks(source->mBuffer, MediaSegment::AUDIO);
|
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
|
|
|
const StreamBuffer::Track& inputTrack = *tracks;
|
2014-09-18 03:51:21 +04:00
|
|
|
const AudioSegment& inputSegment =
|
|
|
|
*static_cast<AudioSegment*>(inputTrack.GetSegment());
|
|
|
|
if (inputSegment.IsNull()) {
|
2013-07-24 14:15:11 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-18 03:51:21 +04:00
|
|
|
AudioSegment& segment = *audioSegments.AppendElement();
|
2013-07-24 14:15:11 +04:00
|
|
|
GraphTime next;
|
|
|
|
for (GraphTime t = aFrom; t < aTo; t = next) {
|
|
|
|
MediaInputPort::InputInterval interval = mInputs[0]->GetNextInputInterval(t);
|
|
|
|
interval.mEnd = std::min(interval.mEnd, aTo);
|
|
|
|
if (interval.mStart >= interval.mEnd)
|
|
|
|
break;
|
|
|
|
next = interval.mEnd;
|
|
|
|
|
|
|
|
StreamTime outputStart = GraphTimeToStreamTime(interval.mStart);
|
2014-09-18 03:51:21 +04:00
|
|
|
StreamTime outputEnd = GraphTimeToStreamTime(interval.mEnd);
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime ticks = outputEnd - outputStart;
|
2013-07-24 14:15:11 +04:00
|
|
|
|
|
|
|
if (interval.mInputIsBlocked) {
|
|
|
|
segment.AppendNullData(ticks);
|
|
|
|
} else {
|
2014-09-18 03:51:21 +04:00
|
|
|
StreamTime inputStart =
|
|
|
|
std::min(inputSegment.GetDuration(),
|
|
|
|
source->GraphTimeToStreamTime(interval.mStart));
|
|
|
|
StreamTime inputEnd =
|
|
|
|
std::min(inputSegment.GetDuration(),
|
|
|
|
source->GraphTimeToStreamTime(interval.mEnd));
|
|
|
|
|
|
|
|
segment.AppendSlice(inputSegment, inputStart, inputEnd);
|
2013-07-24 14:15:11 +04:00
|
|
|
// Pad if we're looking past the end of the track
|
2014-09-18 03:51:21 +04:00
|
|
|
segment.AppendNullData(ticks - (inputEnd - inputStart));
|
2013-07-24 14:15:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 03:51:21 +04:00
|
|
|
for (AudioSegment::ChunkIterator iter(segment); !iter.IsEnded(); iter.Next()) {
|
|
|
|
inputChannels = GetAudioChannelsSuperset(inputChannels, iter->ChannelCount());
|
2013-07-24 14:15:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 04:49:20 +04:00
|
|
|
uint32_t accumulateIndex = 0;
|
2014-04-01 01:25:45 +04:00
|
|
|
if (inputChannels) {
|
2013-07-24 14:15:11 +04:00
|
|
|
nsAutoTArray<float,GUESS_AUDIO_CHANNELS*WEBAUDIO_BLOCK_SIZE> downmixBuffer;
|
|
|
|
for (uint32_t i = 0; i < audioSegments.Length(); ++i) {
|
|
|
|
AudioChunk tmpChunk;
|
2014-09-18 03:51:21 +04:00
|
|
|
ConvertSegmentToAudioBlock(&audioSegments[i], &tmpChunk, inputChannels);
|
2013-07-24 14:15:11 +04:00
|
|
|
if (!tmpChunk.IsNull()) {
|
2014-04-01 04:49:20 +04:00
|
|
|
if (accumulateIndex == 0) {
|
|
|
|
AllocateAudioBlock(inputChannels, &mLastChunks[0]);
|
|
|
|
}
|
|
|
|
AccumulateInputChunk(accumulateIndex, tmpChunk, &mLastChunks[0], &downmixBuffer);
|
|
|
|
accumulateIndex++;
|
2013-07-24 14:15:11 +04:00
|
|
|
}
|
|
|
|
}
|
2014-04-01 04:49:20 +04:00
|
|
|
}
|
|
|
|
if (accumulateIndex == 0) {
|
2013-07-24 14:15:11 +04:00
|
|
|
mLastChunks[0].SetNull(WEBAUDIO_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using AudioNodeStream's AdvanceOutputSegment to push the media stream graph along with null data.
|
|
|
|
AdvanceOutputSegment();
|
|
|
|
}
|
|
|
|
|
2014-04-25 18:32:00 +04:00
|
|
|
bool
|
|
|
|
AudioNodeExternalInputStream::IsEnabled()
|
|
|
|
{
|
|
|
|
return ((MediaStreamAudioSourceNodeEngine*)Engine())->IsEnabled();
|
|
|
|
}
|
|
|
|
|
2013-07-24 14:15:11 +04:00
|
|
|
}
|