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