/src/mozilla-central/dom/media/gtest/TestAudioCompactor.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | #include "gtest/gtest.h" |
7 | | #include "AudioCompactor.h" |
8 | | #include "nsIMemoryReporter.h" |
9 | | |
10 | | using mozilla::AudioCompactor; |
11 | | using mozilla::AudioData; |
12 | | using mozilla::AudioDataValue; |
13 | | using mozilla::MediaQueue; |
14 | | |
15 | | class MemoryFunctor : public nsDequeFunctor { |
16 | | public: |
17 | 0 | MemoryFunctor() : mSize(0) {} |
18 | | MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf); |
19 | | |
20 | | void operator()(void* aObject) override |
21 | 0 | { |
22 | 0 | const AudioData* audioData = static_cast<const AudioData*>(aObject); |
23 | 0 | mSize += audioData->SizeOfIncludingThis(MallocSizeOf); |
24 | 0 | } |
25 | | |
26 | | size_t mSize; |
27 | | }; |
28 | | |
29 | | class TestCopy |
30 | | { |
31 | | public: |
32 | | TestCopy(uint32_t aFrames, uint32_t aChannels, |
33 | | uint32_t &aCallCount, uint32_t &aFrameCount) |
34 | | : mFrames(aFrames) |
35 | | , mChannels(aChannels) |
36 | | , mCallCount(aCallCount) |
37 | | , mFrameCount(aFrameCount) |
38 | 0 | { } |
39 | | |
40 | | uint32_t operator()(AudioDataValue *aBuffer, uint32_t aSamples) |
41 | 0 | { |
42 | 0 | mCallCount += 1; |
43 | 0 | uint32_t frames = std::min(mFrames - mFrameCount, aSamples / mChannels); |
44 | 0 | mFrameCount += frames; |
45 | 0 | return frames; |
46 | 0 | } |
47 | | |
48 | | private: |
49 | | const uint32_t mFrames; |
50 | | const uint32_t mChannels; |
51 | | uint32_t &mCallCount; |
52 | | uint32_t &mFrameCount; |
53 | | }; |
54 | | |
55 | | static void TestAudioCompactor(size_t aBytes) |
56 | 0 | { |
57 | 0 | MediaQueue<AudioData> queue; |
58 | 0 | AudioCompactor compactor(queue); |
59 | 0 |
|
60 | 0 | uint64_t offset = 0; |
61 | 0 | uint64_t time = 0; |
62 | 0 | uint32_t sampleRate = 44000; |
63 | 0 | uint32_t channels = 2; |
64 | 0 | uint32_t frames = aBytes / (channels * sizeof(AudioDataValue)); |
65 | 0 | size_t maxSlop = aBytes / AudioCompactor::MAX_SLOP_DIVISOR; |
66 | 0 |
|
67 | 0 | uint32_t callCount = 0; |
68 | 0 | uint32_t frameCount = 0; |
69 | 0 |
|
70 | 0 | compactor.Push(offset, time, sampleRate, frames, channels, |
71 | 0 | TestCopy(frames, channels, callCount, frameCount)); |
72 | 0 |
|
73 | 0 | EXPECT_GT(callCount, 0U) << "copy functor never called"; |
74 | 0 | EXPECT_EQ(frames, frameCount) << "incorrect number of frames copied"; |
75 | 0 |
|
76 | 0 | MemoryFunctor memoryFunc; |
77 | 0 | queue.LockedForEach(memoryFunc); |
78 | 0 | size_t allocSize = memoryFunc.mSize - (callCount * sizeof(AudioData)); |
79 | 0 | size_t slop = allocSize - aBytes; |
80 | 0 | EXPECT_LE(slop, maxSlop) << "allowed too much allocation slop"; |
81 | 0 | } |
82 | | |
83 | | TEST(Media, AudioCompactor_4000) |
84 | 0 | { |
85 | 0 | TestAudioCompactor(4000); |
86 | 0 | } |
87 | | |
88 | | TEST(Media, AudioCompactor_4096) |
89 | 0 | { |
90 | 0 | TestAudioCompactor(4096); |
91 | 0 | } |
92 | | |
93 | | TEST(Media, AudioCompactor_5000) |
94 | 0 | { |
95 | 0 | TestAudioCompactor(5000); |
96 | 0 | } |
97 | | |
98 | | TEST(Media, AudioCompactor_5256) |
99 | 0 | { |
100 | 0 | TestAudioCompactor(5256); |
101 | 0 | } |
102 | | |
103 | | TEST(Media, AudioCompactor_NativeCopy) |
104 | 0 | { |
105 | 0 | const uint32_t channels = 2; |
106 | 0 | const size_t srcBytes = 32; |
107 | 0 | const uint32_t srcSamples = srcBytes / sizeof(AudioDataValue); |
108 | 0 | const uint32_t srcFrames = srcSamples / channels; |
109 | 0 | uint8_t src[srcBytes]; |
110 | 0 |
|
111 | 0 | for (uint32_t i = 0; i < srcBytes; ++i) { |
112 | 0 | src[i] = i; |
113 | 0 | } |
114 | 0 |
|
115 | 0 | AudioCompactor::NativeCopy copy(src, srcBytes, channels); |
116 | 0 |
|
117 | 0 | const uint32_t dstSamples = srcSamples * 2; |
118 | 0 | AudioDataValue dst[dstSamples]; |
119 | 0 |
|
120 | 0 | const AudioDataValue notCopied = 0xffff; |
121 | 0 | for (uint32_t i = 0; i < dstSamples; ++i) { |
122 | 0 | dst[i] = notCopied; |
123 | 0 | } |
124 | 0 |
|
125 | 0 | const uint32_t copyCount = 8; |
126 | 0 | uint32_t copiedFrames = 0; |
127 | 0 | uint32_t nextSample = 0; |
128 | 0 | for (uint32_t i = 0; i < copyCount; ++i) { |
129 | 0 | uint32_t copySamples = dstSamples / copyCount; |
130 | 0 | copiedFrames += copy(dst + nextSample, copySamples); |
131 | 0 | nextSample += copySamples; |
132 | 0 | } |
133 | 0 |
|
134 | 0 | EXPECT_EQ(srcFrames, copiedFrames) << "copy exact number of source frames"; |
135 | 0 |
|
136 | 0 | // Verify that the only the correct bytes were copied. |
137 | 0 | for (uint32_t i = 0; i < dstSamples; ++i) { |
138 | 0 | if (i < srcSamples) { |
139 | 0 | EXPECT_NE(notCopied, dst[i]) << "should have copied over these bytes"; |
140 | 0 | } else { |
141 | 0 | EXPECT_EQ(notCopied, dst[i]) << "should not have copied over these bytes"; |
142 | 0 | } |
143 | 0 | } |
144 | 0 | } |