2014-02-06 03:11:25 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
#if !defined(AudioCompactor_h)
|
|
|
|
# define AudioCompactor_h
|
|
|
|
|
|
|
|
# include "MediaQueue.h"
|
|
|
|
# include "MediaData.h"
|
|
|
|
# include "VideoUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class AudioCompactor {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit AudioCompactor(MediaQueue<AudioData>& aQueue) : mQueue(aQueue) {
|
2016-04-09 14:11:16 +03:00
|
|
|
// Determine padding size used by AlignedBuffer.
|
|
|
|
size_t paddedSize = AlignedAudioBuffer::AlignmentPaddingSize();
|
|
|
|
mSamplesPadding = paddedSize / sizeof(AudioDataValue);
|
|
|
|
if (mSamplesPadding * sizeof(AudioDataValue) < paddedSize) {
|
|
|
|
// Round up.
|
|
|
|
mSamplesPadding++;
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 03:11:25 +04:00
|
|
|
|
|
|
|
// Push audio data into the underlying queue with minimal heap allocation
|
|
|
|
// slop. This method is responsible for allocating AudioDataValue[] buffers.
|
|
|
|
// The caller must provide a functor to copy the data into the buffers. The
|
|
|
|
// functor must provide the following signature:
|
|
|
|
//
|
|
|
|
// uint32_t operator()(AudioDataValue *aBuffer, uint32_t aSamples);
|
|
|
|
//
|
|
|
|
// The functor must copy as many complete frames as possible to the provided
|
|
|
|
// buffer given its length (in AudioDataValue elements). The number of frames
|
|
|
|
// copied must be returned. This copy functor must support being called
|
|
|
|
// multiple times in order to copy the audio data fully. The copy functor
|
|
|
|
// must copy full frames as partial frames will be ignored.
|
|
|
|
template <typename CopyFunc>
|
|
|
|
bool Push(int64_t aOffset, int64_t aTime, int32_t aSampleRate,
|
|
|
|
uint32_t aFrames, uint32_t aChannels, CopyFunc aCopyFunc) {
|
2017-04-24 12:33:05 +03:00
|
|
|
auto time = media::TimeUnit::FromMicroseconds(aTime);
|
|
|
|
|
2014-02-06 03:11:25 +04:00
|
|
|
// If we are losing more than a reasonable amount to padding, try to chunk
|
|
|
|
// the data.
|
|
|
|
size_t maxSlop = AudioDataSize(aFrames, aChannels) / MAX_SLOP_DIVISOR;
|
|
|
|
|
|
|
|
while (aFrames > 0) {
|
|
|
|
uint32_t samples = GetChunkSamples(aFrames, aChannels, maxSlop);
|
2016-04-20 12:56:59 +03:00
|
|
|
if (samples / aChannels > mSamplesPadding / aChannels + 1) {
|
2016-04-09 14:11:16 +03:00
|
|
|
samples -= mSamplesPadding;
|
|
|
|
}
|
2016-04-03 16:09:45 +03:00
|
|
|
AlignedAudioBuffer buffer(samples);
|
|
|
|
if (!buffer) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-06 03:11:25 +04:00
|
|
|
|
|
|
|
// Copy audio data to buffer using caller-provided functor.
|
2015-11-02 01:34:26 +03:00
|
|
|
uint32_t framesCopied = aCopyFunc(buffer.get(), samples);
|
2014-02-06 03:11:25 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(framesCopied <= aFrames, "functor copied too many frames");
|
2016-10-01 11:56:21 +03:00
|
|
|
buffer.SetLength(size_t(framesCopied) * aChannels);
|
2014-02-06 03:11:25 +04:00
|
|
|
|
2017-04-24 12:33:05 +03:00
|
|
|
auto duration = FramesToTimeUnit(framesCopied, aSampleRate);
|
|
|
|
if (!duration.IsValid()) {
|
2014-02-06 03:11:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:19:47 +03:00
|
|
|
RefPtr<AudioData> data = new AudioData(aOffset, time, std::move(buffer),
|
|
|
|
aChannels, aSampleRate);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(duration == data->mDuration, "must be equal");
|
|
|
|
mQueue.Push(data);
|
2014-02-06 03:11:25 +04:00
|
|
|
|
|
|
|
// Remove the frames we just pushed into the queue and loop if there is
|
|
|
|
// more to be done.
|
2017-04-24 12:33:05 +03:00
|
|
|
time += duration;
|
2014-02-06 03:11:25 +04:00
|
|
|
aFrames -= framesCopied;
|
|
|
|
|
|
|
|
// NOTE: No need to update aOffset as its only an approximation anyway.
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy functor suitable for copying audio samples already in the
|
|
|
|
// AudioDataValue format/layout expected by AudioStream on this platform.
|
|
|
|
class NativeCopy {
|
|
|
|
public:
|
|
|
|
NativeCopy(const uint8_t* aSource, size_t aSourceBytes, uint32_t aChannels)
|
|
|
|
: mSource(aSource),
|
|
|
|
mSourceBytes(aSourceBytes),
|
|
|
|
mChannels(aChannels),
|
|
|
|
mNextByte(0) {}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2014-02-06 03:11:25 +04:00
|
|
|
uint32_t operator()(AudioDataValue* aBuffer, uint32_t aSamples);
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2014-02-06 03:11:25 +04:00
|
|
|
private:
|
|
|
|
const uint8_t* const mSource;
|
|
|
|
const size_t mSourceBytes;
|
|
|
|
const uint32_t mChannels;
|
|
|
|
size_t mNextByte;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Allow 12.5% slop before chunking kicks in. Public so that the gtest can
|
|
|
|
// access it.
|
|
|
|
static const size_t MAX_SLOP_DIVISOR = 8;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Compute the number of AudioDataValue samples that will be fit the most
|
|
|
|
// frames while keeping heap allocation slop less than the given threshold.
|
|
|
|
static uint32_t GetChunkSamples(uint32_t aFrames, uint32_t aChannels,
|
|
|
|
size_t aMaxSlop);
|
|
|
|
|
|
|
|
static size_t BytesPerFrame(uint32_t aChannels) {
|
|
|
|
return sizeof(AudioDataValue) * aChannels;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t AudioDataSize(uint32_t aFrames, uint32_t aChannels) {
|
|
|
|
return aFrames * BytesPerFrame(aChannels);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaQueue<AudioData>& mQueue;
|
2016-04-09 14:11:16 +03:00
|
|
|
size_t mSamplesPadding;
|
2014-02-06 03:11:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // AudioCompactor_h
|