content/media/AudioCompactor.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6 #if !defined(AudioCompactor_h)
michael@0 7 #define AudioCompactor_h
michael@0 8
michael@0 9 #include "MediaQueue.h"
michael@0 10 #include "MediaData.h"
michael@0 11 #include "VideoUtils.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14
michael@0 15 class AudioCompactor
michael@0 16 {
michael@0 17 public:
michael@0 18 AudioCompactor(MediaQueue<AudioData>& aQueue)
michael@0 19 : mQueue(aQueue)
michael@0 20 { }
michael@0 21
michael@0 22 // Push audio data into the underlying queue with minimal heap allocation
michael@0 23 // slop. This method is responsible for allocating AudioDataValue[] buffers.
michael@0 24 // The caller must provide a functor to copy the data into the buffers. The
michael@0 25 // functor must provide the following signature:
michael@0 26 //
michael@0 27 // uint32_t operator()(AudioDataValue *aBuffer, uint32_t aSamples);
michael@0 28 //
michael@0 29 // The functor must copy as many complete frames as possible to the provided
michael@0 30 // buffer given its length (in AudioDataValue elements). The number of frames
michael@0 31 // copied must be returned. This copy functor must support being called
michael@0 32 // multiple times in order to copy the audio data fully. The copy functor
michael@0 33 // must copy full frames as partial frames will be ignored.
michael@0 34 template<typename CopyFunc>
michael@0 35 bool Push(int64_t aOffset, int64_t aTime, int32_t aSampleRate,
michael@0 36 uint32_t aFrames, uint32_t aChannels, CopyFunc aCopyFunc)
michael@0 37 {
michael@0 38 // If we are losing more than a reasonable amount to padding, try to chunk
michael@0 39 // the data.
michael@0 40 size_t maxSlop = AudioDataSize(aFrames, aChannels) / MAX_SLOP_DIVISOR;
michael@0 41
michael@0 42 while (aFrames > 0) {
michael@0 43 uint32_t samples = GetChunkSamples(aFrames, aChannels, maxSlop);
michael@0 44 nsAutoArrayPtr<AudioDataValue> buffer(new AudioDataValue[samples]);
michael@0 45
michael@0 46 // Copy audio data to buffer using caller-provided functor.
michael@0 47 uint32_t framesCopied = aCopyFunc(buffer, samples);
michael@0 48
michael@0 49 NS_ASSERTION(framesCopied <= aFrames, "functor copied too many frames");
michael@0 50
michael@0 51 CheckedInt64 duration = FramesToUsecs(framesCopied, aSampleRate);
michael@0 52 if (!duration.isValid()) {
michael@0 53 return false;
michael@0 54 }
michael@0 55
michael@0 56 mQueue.Push(new AudioData(aOffset,
michael@0 57 aTime,
michael@0 58 duration.value(),
michael@0 59 framesCopied,
michael@0 60 buffer.forget(),
michael@0 61 aChannels));
michael@0 62
michael@0 63 // Remove the frames we just pushed into the queue and loop if there is
michael@0 64 // more to be done.
michael@0 65 aTime += duration.value();
michael@0 66 aFrames -= framesCopied;
michael@0 67
michael@0 68 // NOTE: No need to update aOffset as its only an approximation anyway.
michael@0 69 }
michael@0 70
michael@0 71 return true;
michael@0 72 }
michael@0 73
michael@0 74 // Copy functor suitable for copying audio samples already in the
michael@0 75 // AudioDataValue format/layout expected by AudioStream on this platform.
michael@0 76 class NativeCopy
michael@0 77 {
michael@0 78 public:
michael@0 79 NativeCopy(const uint8_t* aSource, size_t aSourceBytes,
michael@0 80 uint32_t aChannels)
michael@0 81 : mSource(aSource)
michael@0 82 , mSourceBytes(aSourceBytes)
michael@0 83 , mChannels(aChannels)
michael@0 84 , mNextByte(0)
michael@0 85 { }
michael@0 86
michael@0 87 uint32_t operator()(AudioDataValue *aBuffer, uint32_t aSamples);
michael@0 88
michael@0 89 private:
michael@0 90 const uint8_t* const mSource;
michael@0 91 const size_t mSourceBytes;
michael@0 92 const uint32_t mChannels;
michael@0 93 size_t mNextByte;
michael@0 94 };
michael@0 95
michael@0 96 // Allow 12.5% slop before chunking kicks in. Public so that the gtest can
michael@0 97 // access it.
michael@0 98 static const size_t MAX_SLOP_DIVISOR = 8;
michael@0 99
michael@0 100 private:
michael@0 101 // Compute the number of AudioDataValue samples that will be fit the most
michael@0 102 // frames while keeping heap allocation slop less than the given threshold.
michael@0 103 static uint32_t
michael@0 104 GetChunkSamples(uint32_t aFrames, uint32_t aChannels, size_t aMaxSlop);
michael@0 105
michael@0 106 static size_t BytesPerFrame(uint32_t aChannels)
michael@0 107 {
michael@0 108 return sizeof(AudioDataValue) * aChannels;
michael@0 109 }
michael@0 110
michael@0 111 static size_t AudioDataSize(uint32_t aFrames, uint32_t aChannels)
michael@0 112 {
michael@0 113 return aFrames * BytesPerFrame(aChannels);
michael@0 114 }
michael@0 115
michael@0 116 MediaQueue<AudioData> &mQueue;
michael@0 117 };
michael@0 118
michael@0 119 } // namespace mozilla
michael@0 120
michael@0 121 #endif // AudioCompactor_h

mercurial