Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: media/base/audio_bus_unittest.cc

Issue 1906423005: Replace scoped_ptr with std::unique_ptr in //media/base. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: scopedptr-media-base: android Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/base/audio_bus_perftest.cc ('k') | media/base/audio_converter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <limits> 8 #include <limits>
9 #include <memory>
9 10
10 #include "base/macros.h" 11 #include "base/macros.h"
11 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
13 #include "build/build_config.h" 14 #include "build/build_config.h"
14 #include "media/base/audio_bus.h" 15 #include "media/base/audio_bus.h"
15 #include "media/base/audio_parameters.h" 16 #include "media/base/audio_parameters.h"
16 #include "media/base/channel_layout.h" 17 #include "media/base/channel_layout.h"
17 #include "media/base/fake_audio_render_callback.h" 18 #include "media/base/fake_audio_render_callback.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 } 105 }
105 106
106 protected: 107 protected:
107 std::vector<float*> data_; 108 std::vector<float*> data_;
108 109
109 DISALLOW_COPY_AND_ASSIGN(AudioBusTest); 110 DISALLOW_COPY_AND_ASSIGN(AudioBusTest);
110 }; 111 };
111 112
112 // Verify basic Create(...) method works as advertised. 113 // Verify basic Create(...) method works as advertised.
113 TEST_F(AudioBusTest, Create) { 114 TEST_F(AudioBusTest, Create) {
114 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); 115 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
115 VerifyParams(bus.get()); 116 VerifyParams(bus.get());
116 VerifyChannelData(bus.get()); 117 VerifyChannelData(bus.get());
117 } 118 }
118 119
119 // Verify Create(...) using AudioParameters works as advertised. 120 // Verify Create(...) using AudioParameters works as advertised.
120 TEST_F(AudioBusTest, CreateUsingAudioParameters) { 121 TEST_F(AudioBusTest, CreateUsingAudioParameters) {
121 scoped_ptr<AudioBus> bus = AudioBus::Create(AudioParameters( 122 std::unique_ptr<AudioBus> bus = AudioBus::Create(
122 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, 32, 123 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
123 kFrameCount)); 124 kSampleRate, 32, kFrameCount));
124 VerifyParams(bus.get()); 125 VerifyParams(bus.get());
125 VerifyChannelData(bus.get()); 126 VerifyChannelData(bus.get());
126 } 127 }
127 128
128 // Verify an AudioBus created via wrapping a vector works as advertised. 129 // Verify an AudioBus created via wrapping a vector works as advertised.
129 TEST_F(AudioBusTest, WrapVector) { 130 TEST_F(AudioBusTest, WrapVector) {
130 data_.reserve(kChannels); 131 data_.reserve(kChannels);
131 for (int i = 0; i < kChannels; ++i) { 132 for (int i = 0; i < kChannels; ++i) {
132 data_.push_back(static_cast<float*>(base::AlignedAlloc( 133 data_.push_back(static_cast<float*>(base::AlignedAlloc(
133 sizeof(*data_[i]) * kFrameCount, AudioBus::kChannelAlignment))); 134 sizeof(*data_[i]) * kFrameCount, AudioBus::kChannelAlignment)));
134 } 135 }
135 136
136 scoped_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_); 137 std::unique_ptr<AudioBus> bus = AudioBus::WrapVector(kFrameCount, data_);
137 VerifyParams(bus.get()); 138 VerifyParams(bus.get());
138 VerifyChannelData(bus.get()); 139 VerifyChannelData(bus.get());
139 } 140 }
140 141
141 // Verify an AudioBus created via wrapping a memory block works as advertised. 142 // Verify an AudioBus created via wrapping a memory block works as advertised.
142 TEST_F(AudioBusTest, WrapMemory) { 143 TEST_F(AudioBusTest, WrapMemory) {
143 AudioParameters params( 144 AudioParameters params(
144 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, 32, 145 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, 32,
145 kFrameCount); 146 kFrameCount);
146 int data_size = AudioBus::CalculateMemorySize(params); 147 int data_size = AudioBus::CalculateMemorySize(params);
147 scoped_ptr<float, base::AlignedFreeDeleter> data(static_cast<float*>( 148 std::unique_ptr<float, base::AlignedFreeDeleter> data(static_cast<float*>(
148 base::AlignedAlloc(data_size, AudioBus::kChannelAlignment))); 149 base::AlignedAlloc(data_size, AudioBus::kChannelAlignment)));
149 150
150 // Fill the memory with a test value we can check for after wrapping. 151 // Fill the memory with a test value we can check for after wrapping.
151 static const float kTestValue = 3; 152 static const float kTestValue = 3;
152 std::fill( 153 std::fill(
153 data.get(), data.get() + data_size / sizeof(*data.get()), kTestValue); 154 data.get(), data.get() + data_size / sizeof(*data.get()), kTestValue);
154 155
155 scoped_ptr<AudioBus> bus = AudioBus::WrapMemory(params, data.get()); 156 std::unique_ptr<AudioBus> bus = AudioBus::WrapMemory(params, data.get());
156 // Verify the test value we filled prior to wrapping. 157 // Verify the test value we filled prior to wrapping.
157 for (int i = 0; i < bus->channels(); ++i) 158 for (int i = 0; i < bus->channels(); ++i)
158 VerifyValue(bus->channel(i), bus->frames(), kTestValue); 159 VerifyValue(bus->channel(i), bus->frames(), kTestValue);
159 VerifyParams(bus.get()); 160 VerifyParams(bus.get());
160 VerifyChannelData(bus.get()); 161 VerifyChannelData(bus.get());
161 162
162 // Verify the channel vectors lie within the provided memory block. 163 // Verify the channel vectors lie within the provided memory block.
163 EXPECT_GE(bus->channel(0), data.get()); 164 EXPECT_GE(bus->channel(0), data.get());
164 EXPECT_LT(bus->channel(bus->channels() - 1) + bus->frames(), 165 EXPECT_LT(bus->channel(bus->channels() - 1) + bus->frames(),
165 data.get() + data_size / sizeof(*data.get())); 166 data.get() + data_size / sizeof(*data.get()));
166 } 167 }
167 168
168 // Simulate a shared memory transfer and verify results. 169 // Simulate a shared memory transfer and verify results.
169 TEST_F(AudioBusTest, CopyTo) { 170 TEST_F(AudioBusTest, CopyTo) {
170 // Create one bus with AudioParameters and the other through direct values to 171 // Create one bus with AudioParameters and the other through direct values to
171 // test for parity between the Create() functions. 172 // test for parity between the Create() functions.
172 AudioParameters params( 173 AudioParameters params(
173 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, 32, 174 AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate, 32,
174 kFrameCount); 175 kFrameCount);
175 scoped_ptr<AudioBus> bus1 = AudioBus::Create(kChannels, kFrameCount); 176 std::unique_ptr<AudioBus> bus1 = AudioBus::Create(kChannels, kFrameCount);
176 scoped_ptr<AudioBus> bus2 = AudioBus::Create(params); 177 std::unique_ptr<AudioBus> bus2 = AudioBus::Create(params);
177 178
178 { 179 {
179 SCOPED_TRACE("Created"); 180 SCOPED_TRACE("Created");
180 CopyTest(bus1.get(), bus2.get()); 181 CopyTest(bus1.get(), bus2.get());
181 } 182 }
182 { 183 {
183 SCOPED_TRACE("Wrapped Vector"); 184 SCOPED_TRACE("Wrapped Vector");
184 // Try a copy to an AudioBus wrapping a vector. 185 // Try a copy to an AudioBus wrapping a vector.
185 data_.reserve(kChannels); 186 data_.reserve(kChannels);
186 for (int i = 0; i < kChannels; ++i) { 187 for (int i = 0; i < kChannels; ++i) {
187 data_.push_back(static_cast<float*>(base::AlignedAlloc( 188 data_.push_back(static_cast<float*>(base::AlignedAlloc(
188 sizeof(*data_[i]) * kFrameCount, AudioBus::kChannelAlignment))); 189 sizeof(*data_[i]) * kFrameCount, AudioBus::kChannelAlignment)));
189 } 190 }
190 191
191 bus2 = AudioBus::WrapVector(kFrameCount, data_); 192 bus2 = AudioBus::WrapVector(kFrameCount, data_);
192 CopyTest(bus1.get(), bus2.get()); 193 CopyTest(bus1.get(), bus2.get());
193 } 194 }
194 { 195 {
195 SCOPED_TRACE("Wrapped Memory"); 196 SCOPED_TRACE("Wrapped Memory");
196 // Try a copy to an AudioBus wrapping a memory block. 197 // Try a copy to an AudioBus wrapping a memory block.
197 scoped_ptr<float, base::AlignedFreeDeleter> data( 198 std::unique_ptr<float, base::AlignedFreeDeleter> data(static_cast<float*>(
198 static_cast<float*>(base::AlignedAlloc( 199 base::AlignedAlloc(AudioBus::CalculateMemorySize(params),
199 AudioBus::CalculateMemorySize(params), 200 AudioBus::kChannelAlignment)));
200 AudioBus::kChannelAlignment)));
201 201
202 bus2 = AudioBus::WrapMemory(params, data.get()); 202 bus2 = AudioBus::WrapMemory(params, data.get());
203 CopyTest(bus1.get(), bus2.get()); 203 CopyTest(bus1.get(), bus2.get());
204 } 204 }
205 } 205 }
206 206
207 // Verify Zero() and ZeroFrames(...) utility methods work as advertised. 207 // Verify Zero() and ZeroFrames(...) utility methods work as advertised.
208 TEST_F(AudioBusTest, Zero) { 208 TEST_F(AudioBusTest, Zero) {
209 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); 209 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
210 210
211 // Fill the bus with dummy data. 211 // Fill the bus with dummy data.
212 for (int i = 0; i < bus->channels(); ++i) 212 for (int i = 0; i < bus->channels(); ++i)
213 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1); 213 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), i + 1);
214 EXPECT_FALSE(bus->AreFramesZero()); 214 EXPECT_FALSE(bus->AreFramesZero());
215 215
216 // Zero first half the frames of each channel. 216 // Zero first half the frames of each channel.
217 bus->ZeroFrames(kFrameCount / 2); 217 bus->ZeroFrames(kFrameCount / 2);
218 for (int i = 0; i < bus->channels(); ++i) { 218 for (int i = 0; i < bus->channels(); ++i) {
219 SCOPED_TRACE("First Half Zero"); 219 SCOPED_TRACE("First Half Zero");
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 INT32_MIN / 2, 0, INT32_MAX, 0, 0}; 266 INT32_MIN / 2, 0, INT32_MAX, 0, 0};
267 267
268 // Expected results. 268 // Expected results.
269 static const int kTestVectorFrames = kTestVectorSize / 2; 269 static const int kTestVectorFrames = kTestVectorSize / 2;
270 static const float kTestVectorResult[][kTestVectorFrames] = { 270 static const float kTestVectorResult[][kTestVectorFrames] = {
271 { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }}; 271 { -1, 1, 0.5, 0, 0 }, { 0, -1, -0.5, 1, 0 }};
272 static const int kTestVectorChannels = arraysize(kTestVectorResult); 272 static const int kTestVectorChannels = arraysize(kTestVectorResult);
273 273
274 // Verify FromInterleaved() deinterleaves audio in supported formats correctly. 274 // Verify FromInterleaved() deinterleaves audio in supported formats correctly.
275 TEST_F(AudioBusTest, FromInterleaved) { 275 TEST_F(AudioBusTest, FromInterleaved) {
276 scoped_ptr<AudioBus> bus = AudioBus::Create( 276 std::unique_ptr<AudioBus> bus =
277 kTestVectorChannels, kTestVectorFrames); 277 AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
278 scoped_ptr<AudioBus> expected = AudioBus::Create( 278 std::unique_ptr<AudioBus> expected =
279 kTestVectorChannels, kTestVectorFrames); 279 AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
280 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 280 for (int ch = 0; ch < kTestVectorChannels; ++ch) {
281 memcpy(expected->channel(ch), kTestVectorResult[ch], 281 memcpy(expected->channel(ch), kTestVectorResult[ch],
282 kTestVectorFrames * sizeof(*expected->channel(ch))); 282 kTestVectorFrames * sizeof(*expected->channel(ch)));
283 } 283 }
284 { 284 {
285 SCOPED_TRACE("uint8_t"); 285 SCOPED_TRACE("uint8_t");
286 bus->Zero(); 286 bus->Zero();
287 bus->FromInterleaved( 287 bus->FromInterleaved(
288 kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8)); 288 kTestVectorUint8, kTestVectorFrames, sizeof(*kTestVectorUint8));
289 // Biased uint8_t calculations have poor precision, so the epsilon here is 289 // Biased uint8_t calculations have poor precision, so the epsilon here is
(...skipping 19 matching lines...) Expand all
309 } 309 }
310 } 310 }
311 311
312 // Verify FromInterleavedPartial() deinterleaves audio correctly. 312 // Verify FromInterleavedPartial() deinterleaves audio correctly.
313 TEST_F(AudioBusTest, FromInterleavedPartial) { 313 TEST_F(AudioBusTest, FromInterleavedPartial) {
314 // Only deinterleave the middle two frames in each channel. 314 // Only deinterleave the middle two frames in each channel.
315 static const int kPartialStart = 1; 315 static const int kPartialStart = 1;
316 static const int kPartialFrames = 2; 316 static const int kPartialFrames = 2;
317 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); 317 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames);
318 318
319 scoped_ptr<AudioBus> bus = AudioBus::Create( 319 std::unique_ptr<AudioBus> bus =
320 kTestVectorChannels, kTestVectorFrames); 320 AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
321 scoped_ptr<AudioBus> expected = AudioBus::Create( 321 std::unique_ptr<AudioBus> expected =
322 kTestVectorChannels, kTestVectorFrames); 322 AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
323 expected->Zero(); 323 expected->Zero();
324 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 324 for (int ch = 0; ch < kTestVectorChannels; ++ch) {
325 memcpy(expected->channel(ch) + kPartialStart, 325 memcpy(expected->channel(ch) + kPartialStart,
326 kTestVectorResult[ch] + kPartialStart, 326 kTestVectorResult[ch] + kPartialStart,
327 kPartialFrames * sizeof(*expected->channel(ch))); 327 kPartialFrames * sizeof(*expected->channel(ch)));
328 } 328 }
329 329
330 bus->Zero(); 330 bus->Zero();
331 bus->FromInterleavedPartial( 331 bus->FromInterleavedPartial(
332 kTestVectorInt32 + kPartialStart * bus->channels(), kPartialStart, 332 kTestVectorInt32 + kPartialStart * bus->channels(), kPartialStart,
333 kPartialFrames, sizeof(*kTestVectorInt32)); 333 kPartialFrames, sizeof(*kTestVectorInt32));
334 VerifyBus(bus.get(), expected.get()); 334 VerifyBus(bus.get(), expected.get());
335 } 335 }
336 336
337 // Verify ToInterleaved() interleaves audio in suported formats correctly. 337 // Verify ToInterleaved() interleaves audio in suported formats correctly.
338 TEST_F(AudioBusTest, ToInterleaved) { 338 TEST_F(AudioBusTest, ToInterleaved) {
339 scoped_ptr<AudioBus> bus = AudioBus::Create( 339 std::unique_ptr<AudioBus> bus =
340 kTestVectorChannels, kTestVectorFrames); 340 AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
341 // Fill the bus with our test vector. 341 // Fill the bus with our test vector.
342 for (int ch = 0; ch < bus->channels(); ++ch) { 342 for (int ch = 0; ch < bus->channels(); ++ch) {
343 memcpy(bus->channel(ch), kTestVectorResult[ch], 343 memcpy(bus->channel(ch), kTestVectorResult[ch],
344 kTestVectorFrames * sizeof(*bus->channel(ch))); 344 kTestVectorFrames * sizeof(*bus->channel(ch)));
345 } 345 }
346 { 346 {
347 SCOPED_TRACE("uint8_t"); 347 SCOPED_TRACE("uint8_t");
348 uint8_t test_array[arraysize(kTestVectorUint8)]; 348 uint8_t test_array[arraysize(kTestVectorUint8)];
349 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorUint8), test_array); 349 bus->ToInterleaved(bus->frames(), sizeof(*kTestVectorUint8), test_array);
350 ASSERT_EQ(memcmp( 350 ASSERT_EQ(memcmp(
(...skipping 24 matching lines...) Expand all
375 } 375 }
376 } 376 }
377 377
378 // Verify ToInterleavedPartial() interleaves audio correctly. 378 // Verify ToInterleavedPartial() interleaves audio correctly.
379 TEST_F(AudioBusTest, ToInterleavedPartial) { 379 TEST_F(AudioBusTest, ToInterleavedPartial) {
380 // Only interleave the middle two frames in each channel. 380 // Only interleave the middle two frames in each channel.
381 static const int kPartialStart = 1; 381 static const int kPartialStart = 1;
382 static const int kPartialFrames = 2; 382 static const int kPartialFrames = 2;
383 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames); 383 ASSERT_LE(kPartialStart + kPartialFrames, kTestVectorFrames);
384 384
385 scoped_ptr<AudioBus> expected = AudioBus::Create( 385 std::unique_ptr<AudioBus> expected =
386 kTestVectorChannels, kTestVectorFrames); 386 AudioBus::Create(kTestVectorChannels, kTestVectorFrames);
387 for (int ch = 0; ch < kTestVectorChannels; ++ch) { 387 for (int ch = 0; ch < kTestVectorChannels; ++ch) {
388 memcpy(expected->channel(ch), kTestVectorResult[ch], 388 memcpy(expected->channel(ch), kTestVectorResult[ch],
389 kTestVectorFrames * sizeof(*expected->channel(ch))); 389 kTestVectorFrames * sizeof(*expected->channel(ch)));
390 } 390 }
391 391
392 int16_t test_array[arraysize(kTestVectorInt16)]; 392 int16_t test_array[arraysize(kTestVectorInt16)];
393 expected->ToInterleavedPartial( 393 expected->ToInterleavedPartial(
394 kPartialStart, kPartialFrames, sizeof(*kTestVectorInt16), test_array); 394 kPartialStart, kPartialFrames, sizeof(*kTestVectorInt16), test_array);
395 ASSERT_EQ(memcmp( 395 ASSERT_EQ(memcmp(
396 test_array, kTestVectorInt16 + kPartialStart * kTestVectorChannels, 396 test_array, kTestVectorInt16 + kPartialStart * kTestVectorChannels,
397 kPartialFrames * sizeof(*kTestVectorInt16) * kTestVectorChannels), 0); 397 kPartialFrames * sizeof(*kTestVectorInt16) * kTestVectorChannels), 0);
398 } 398 }
399 399
400 TEST_F(AudioBusTest, Scale) { 400 TEST_F(AudioBusTest, Scale) {
401 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); 401 std::unique_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
402 402
403 // Fill the bus with dummy data. 403 // Fill the bus with dummy data.
404 static const float kFillValue = 1; 404 static const float kFillValue = 1;
405 for (int i = 0; i < bus->channels(); ++i) 405 for (int i = 0; i < bus->channels(); ++i)
406 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), kFillValue); 406 std::fill(bus->channel(i), bus->channel(i) + bus->frames(), kFillValue);
407 407
408 // Adjust by an invalid volume and ensure volume is unchanged. 408 // Adjust by an invalid volume and ensure volume is unchanged.
409 bus->Scale(-1); 409 bus->Scale(-1);
410 for (int i = 0; i < bus->channels(); ++i) { 410 for (int i = 0; i < bus->channels(); ++i) {
411 SCOPED_TRACE("Invalid Scale"); 411 SCOPED_TRACE("Invalid Scale");
(...skipping 10 matching lines...) Expand all
422 422
423 // Verify zero volume case. 423 // Verify zero volume case.
424 bus->Scale(0); 424 bus->Scale(0);
425 for (int i = 0; i < bus->channels(); ++i) { 425 for (int i = 0; i < bus->channels(); ++i) {
426 SCOPED_TRACE("Zero Scale"); 426 SCOPED_TRACE("Zero Scale");
427 VerifyValue(bus->channel(i), bus->frames(), 0); 427 VerifyValue(bus->channel(i), bus->frames(), 0);
428 } 428 }
429 } 429 }
430 430
431 } // namespace media 431 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_bus_perftest.cc ('k') | media/base/audio_converter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698