content/media/gtest/TestAudioCompactor.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 #include "gtest/gtest.h"
michael@0 7 #include "AudioCompactor.h"
michael@0 8 #include "MediaDecoderReader.h"
michael@0 9
michael@0 10 using mozilla::AudioCompactor;
michael@0 11 using mozilla::AudioData;
michael@0 12 using mozilla::AudioDataValue;
michael@0 13 using mozilla::MediaDecoderReader;
michael@0 14 using mozilla::MediaQueue;
michael@0 15
michael@0 16 class MemoryFunctor : public nsDequeFunctor {
michael@0 17 public:
michael@0 18 MemoryFunctor() : mSize(0) {}
michael@0 19 MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf);
michael@0 20
michael@0 21 virtual void* operator()(void* anObject) {
michael@0 22 const AudioData* audioData = static_cast<const AudioData*>(anObject);
michael@0 23 mSize += audioData->SizeOfIncludingThis(MallocSizeOf);
michael@0 24 return nullptr;
michael@0 25 }
michael@0 26
michael@0 27 size_t mSize;
michael@0 28 };
michael@0 29
michael@0 30 class TestCopy
michael@0 31 {
michael@0 32 public:
michael@0 33 TestCopy(uint32_t aFrames, uint32_t aChannels,
michael@0 34 uint32_t &aCallCount, uint32_t &aFrameCount)
michael@0 35 : mFrames(aFrames)
michael@0 36 , mChannels(aChannels)
michael@0 37 , mCallCount(aCallCount)
michael@0 38 , mFrameCount(aFrameCount)
michael@0 39 { }
michael@0 40
michael@0 41 uint32_t operator()(AudioDataValue *aBuffer, uint32_t aSamples)
michael@0 42 {
michael@0 43 mCallCount += 1;
michael@0 44 uint32_t frames = std::min(mFrames - mFrameCount, aSamples / mChannels);
michael@0 45 mFrameCount += frames;
michael@0 46 return frames;
michael@0 47 }
michael@0 48
michael@0 49 private:
michael@0 50 const uint32_t mFrames;
michael@0 51 const uint32_t mChannels;
michael@0 52 uint32_t &mCallCount;
michael@0 53 uint32_t &mFrameCount;
michael@0 54 };
michael@0 55
michael@0 56 static void TestAudioCompactor(size_t aBytes)
michael@0 57 {
michael@0 58 MediaQueue<AudioData> queue;
michael@0 59 AudioCompactor compactor(queue);
michael@0 60
michael@0 61 uint64_t offset = 0;
michael@0 62 uint64_t time = 0;
michael@0 63 uint32_t sampleRate = 44000;
michael@0 64 uint32_t channels = 2;
michael@0 65 uint32_t frames = aBytes / (channels * sizeof(AudioDataValue));
michael@0 66 size_t maxSlop = aBytes / AudioCompactor::MAX_SLOP_DIVISOR;
michael@0 67
michael@0 68 uint32_t callCount = 0;
michael@0 69 uint32_t frameCount = 0;
michael@0 70
michael@0 71 compactor.Push(offset, time, sampleRate, frames, channels,
michael@0 72 TestCopy(frames, channels, callCount, frameCount));
michael@0 73
michael@0 74 EXPECT_GT(callCount, 0U) << "copy functor never called";
michael@0 75 EXPECT_EQ(frames, frameCount) << "incorrect number of frames copied";
michael@0 76
michael@0 77 MemoryFunctor memoryFunc;
michael@0 78 queue.LockedForEach(memoryFunc);
michael@0 79 size_t allocSize = memoryFunc.mSize - (callCount * sizeof(AudioData));
michael@0 80 size_t slop = allocSize - aBytes;
michael@0 81 EXPECT_LE(slop, maxSlop) << "allowed too much allocation slop";
michael@0 82 }
michael@0 83
michael@0 84 TEST(Media, AudioCompactor_4000)
michael@0 85 {
michael@0 86 TestAudioCompactor(4000);
michael@0 87 }
michael@0 88
michael@0 89 TEST(Media, AudioCompactor_4096)
michael@0 90 {
michael@0 91 TestAudioCompactor(4096);
michael@0 92 }
michael@0 93
michael@0 94 TEST(Media, AudioCompactor_5000)
michael@0 95 {
michael@0 96 TestAudioCompactor(5000);
michael@0 97 }
michael@0 98
michael@0 99 TEST(Media, AudioCompactor_5256)
michael@0 100 {
michael@0 101 TestAudioCompactor(5256);
michael@0 102 }
michael@0 103
michael@0 104 TEST(Media, AudioCompactor_NativeCopy)
michael@0 105 {
michael@0 106 const uint32_t channels = 2;
michael@0 107 const size_t srcBytes = 32;
michael@0 108 const uint32_t srcSamples = srcBytes / sizeof(AudioDataValue);
michael@0 109 const uint32_t srcFrames = srcSamples / channels;
michael@0 110 uint8_t src[srcBytes];
michael@0 111
michael@0 112 for (uint32_t i = 0; i < srcBytes; ++i) {
michael@0 113 src[i] = i;
michael@0 114 }
michael@0 115
michael@0 116 AudioCompactor::NativeCopy copy(src, srcBytes, channels);
michael@0 117
michael@0 118 const uint32_t dstSamples = srcSamples * 2;
michael@0 119 AudioDataValue dst[dstSamples];
michael@0 120
michael@0 121 const AudioDataValue notCopied = 0xffff;
michael@0 122 for (uint32_t i = 0; i < dstSamples; ++i) {
michael@0 123 dst[i] = notCopied;
michael@0 124 }
michael@0 125
michael@0 126 const uint32_t copyCount = 8;
michael@0 127 uint32_t copiedFrames = 0;
michael@0 128 uint32_t nextSample = 0;
michael@0 129 for (uint32_t i = 0; i < copyCount; ++i) {
michael@0 130 uint32_t copySamples = dstSamples / copyCount;
michael@0 131 copiedFrames += copy(dst + nextSample, copySamples);
michael@0 132 nextSample += copySamples;
michael@0 133 }
michael@0 134
michael@0 135 EXPECT_EQ(srcFrames, copiedFrames) << "copy exact number of source frames";
michael@0 136
michael@0 137 // Verify that the only the correct bytes were copied.
michael@0 138 for (uint32_t i = 0; i < dstSamples; ++i) {
michael@0 139 if (i < srcSamples) {
michael@0 140 EXPECT_NE(notCopied, dst[i]) << "should have copied over these bytes";
michael@0 141 } else {
michael@0 142 EXPECT_EQ(notCopied, dst[i]) << "should not have copied over these bytes";
michael@0 143 }
michael@0 144 }
michael@0 145 }

mercurial