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"
9 #include "nsIMemoryReporter.h"
11 using mozilla::AudioCompactor
;
12 using mozilla::AudioData
;
13 using mozilla::AudioDataValue
;
14 using mozilla::MediaQueue
;
16 class MemoryFunctor
: public nsDequeFunctor
<AudioData
> {
18 MemoryFunctor() : mSize(0) {}
19 MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf
);
21 void operator()(AudioData
* aObject
) override
{
22 mSize
+= aObject
->SizeOfIncludingThis(MallocSizeOf
);
30 TestCopy(uint32_t aFrames
, uint32_t aChannels
, uint32_t& aCallCount
,
31 uint32_t& aFrameCount
)
34 mCallCount(aCallCount
),
35 mFrameCount(aFrameCount
) {}
37 uint32_t operator()(AudioDataValue
* aBuffer
, uint32_t aSamples
) {
39 uint32_t frames
= std::min(mFrames
- mFrameCount
, aSamples
/ mChannels
);
40 mFrameCount
+= frames
;
45 const uint32_t mFrames
;
46 const uint32_t mChannels
;
48 uint32_t& mFrameCount
;
51 static void TestAudioCompactor(size_t aBytes
) {
52 MediaQueue
<AudioData
> queue
;
53 AudioCompactor
compactor(queue
);
57 uint32_t sampleRate
= 44000;
58 uint32_t channels
= 2;
59 uint32_t frames
= aBytes
/ (channels
* sizeof(AudioDataValue
));
60 size_t maxSlop
= aBytes
/ AudioCompactor::MAX_SLOP_DIVISOR
;
62 uint32_t callCount
= 0;
63 uint32_t frameCount
= 0;
65 compactor
.Push(offset
, time
, sampleRate
, frames
, channels
,
66 TestCopy(frames
, channels
, callCount
, frameCount
));
68 EXPECT_GT(callCount
, 0U) << "copy functor never called";
69 EXPECT_EQ(frames
, frameCount
) << "incorrect number of frames copied";
71 MemoryFunctor memoryFunc
;
72 queue
.LockedForEach(memoryFunc
);
73 size_t allocSize
= memoryFunc
.mSize
- (callCount
* sizeof(AudioData
));
74 size_t slop
= allocSize
- aBytes
;
75 EXPECT_LE(slop
, maxSlop
) << "allowed too much allocation slop";
78 TEST(Media
, AudioCompactor_4000
)
79 { TestAudioCompactor(4000); }
81 TEST(Media
, AudioCompactor_4096
)
82 { TestAudioCompactor(4096); }
84 TEST(Media
, AudioCompactor_5000
)
85 { TestAudioCompactor(5000); }
87 TEST(Media
, AudioCompactor_5256
)
88 { TestAudioCompactor(5256); }
90 TEST(Media
, AudioCompactor_NativeCopy
)
92 const uint32_t channels
= 2;
93 const size_t srcBytes
= 32;
94 const uint32_t srcSamples
= srcBytes
/ sizeof(AudioDataValue
);
95 const uint32_t srcFrames
= srcSamples
/ channels
;
96 uint8_t src
[srcBytes
];
98 for (uint32_t i
= 0; i
< srcBytes
; ++i
) {
102 AudioCompactor::NativeCopy
copy(src
, srcBytes
, channels
);
104 const uint32_t dstSamples
= srcSamples
* 2;
105 AudioDataValue dst
[dstSamples
];
107 const AudioDataValue notCopied
= 0xffff;
108 for (uint32_t i
= 0; i
< dstSamples
; ++i
) {
112 const uint32_t copyCount
= 8;
113 uint32_t copiedFrames
= 0;
114 uint32_t nextSample
= 0;
115 for (uint32_t i
= 0; i
< copyCount
; ++i
) {
116 uint32_t copySamples
= dstSamples
/ copyCount
;
117 copiedFrames
+= copy(dst
+ nextSample
, copySamples
);
118 nextSample
+= copySamples
;
121 EXPECT_EQ(srcFrames
, copiedFrames
) << "copy exact number of source frames";
123 // Verify that the only the correct bytes were copied.
124 for (uint32_t i
= 0; i
< dstSamples
; ++i
) {
125 if (i
< srcSamples
) {
126 EXPECT_NE(notCopied
, dst
[i
]) << "should have copied over these bytes";
128 EXPECT_EQ(notCopied
, dst
[i
]) << "should not have copied over these bytes";