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

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

Issue 1477533002: Remove kint16max. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@kint8max
Patch Set: includes Created 5 years 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <stdint.h>
6
7 #include <limits>
8
5 #include "media/base/audio_buffer.h" 9 #include "media/base/audio_buffer.h"
6 #include "media/base/audio_bus.h" 10 #include "media/base/audio_bus.h"
7 #include "media/base/test_helpers.h" 11 #include "media/base/test_helpers.h"
8 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
9 13
10 namespace media { 14 namespace media {
11 15
12 static const int kSampleRate = 48000; 16 static const int kSampleRate = 48000;
13 17
14 static void VerifyBusWithOffset(AudioBus* bus, 18 static void VerifyBusWithOffset(AudioBus* bus,
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 VerifyBusWithOffset(bus.get(), 151 VerifyBusWithOffset(bus.get(),
148 trim_start, 152 trim_start,
149 buffer->frame_count() - trim_start, 153 buffer->frame_count() - trim_start,
150 trim_length * 2, 154 trim_length * 2,
151 trim_length * 2, 155 trim_length * 2,
152 1); 156 1);
153 } 157 }
154 158
155 TEST(AudioBufferTest, CopyFrom) { 159 TEST(AudioBufferTest, CopyFrom) {
156 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO; 160 const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_MONO;
157 scoped_refptr<AudioBuffer> original_buffer = 161 scoped_refptr<AudioBuffer> original_buffer = MakeAudioBuffer<uint8_t>(
158 MakeAudioBuffer<uint8>(kSampleFormatU8, 162 kSampleFormatU8, kChannelLayout,
159 kChannelLayout, 163 ChannelLayoutToChannelCount(kChannelLayout), kSampleRate, 1, 1,
160 ChannelLayoutToChannelCount(kChannelLayout), 164 kSampleRate / 100, base::TimeDelta());
161 kSampleRate,
162 1,
163 1,
164 kSampleRate / 100,
165 base::TimeDelta());
166 scoped_refptr<AudioBuffer> new_buffer = 165 scoped_refptr<AudioBuffer> new_buffer =
167 AudioBuffer::CopyFrom(kSampleFormatU8, 166 AudioBuffer::CopyFrom(kSampleFormatU8,
168 original_buffer->channel_layout(), 167 original_buffer->channel_layout(),
169 original_buffer->channel_count(), 168 original_buffer->channel_count(),
170 original_buffer->sample_rate(), 169 original_buffer->sample_rate(),
171 original_buffer->frame_count(), 170 original_buffer->frame_count(),
172 &original_buffer->channel_data()[0], 171 &original_buffer->channel_data()[0],
173 original_buffer->timestamp()); 172 original_buffer->timestamp());
174 EXPECT_EQ(original_buffer->frame_count(), new_buffer->frame_count()); 173 EXPECT_EQ(original_buffer->frame_count(), new_buffer->frame_count());
175 EXPECT_EQ(original_buffer->timestamp(), new_buffer->timestamp()); 174 EXPECT_EQ(original_buffer->timestamp(), new_buffer->timestamp());
176 EXPECT_EQ(original_buffer->duration(), new_buffer->duration()); 175 EXPECT_EQ(original_buffer->duration(), new_buffer->duration());
177 EXPECT_EQ(original_buffer->sample_rate(), new_buffer->sample_rate()); 176 EXPECT_EQ(original_buffer->sample_rate(), new_buffer->sample_rate());
178 EXPECT_EQ(original_buffer->channel_count(), new_buffer->channel_count()); 177 EXPECT_EQ(original_buffer->channel_count(), new_buffer->channel_count());
179 EXPECT_EQ(original_buffer->channel_layout(), new_buffer->channel_layout()); 178 EXPECT_EQ(original_buffer->channel_layout(), new_buffer->channel_layout());
180 EXPECT_FALSE(original_buffer->end_of_stream()); 179 EXPECT_FALSE(original_buffer->end_of_stream());
181 } 180 }
182 181
183 TEST(AudioBufferTest, CreateEOSBuffer) { 182 TEST(AudioBufferTest, CreateEOSBuffer) {
184 scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEOSBuffer(); 183 scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEOSBuffer();
185 EXPECT_TRUE(buffer->end_of_stream()); 184 EXPECT_TRUE(buffer->end_of_stream());
186 } 185 }
187 186
188 TEST(AudioBufferTest, FrameSize) { 187 TEST(AudioBufferTest, FrameSize) {
189 const uint8 kTestData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 188 const uint8_t kTestData[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
190 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 189 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
191 27, 28, 29, 30, 31 }; 190 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
192 const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1337); 191 const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1337);
193 192
194 const uint8* const data[] = { kTestData }; 193 const uint8_t* const data[] = {kTestData};
195 scoped_refptr<AudioBuffer> buffer = 194 scoped_refptr<AudioBuffer> buffer =
196 AudioBuffer::CopyFrom(kSampleFormatU8, 195 AudioBuffer::CopyFrom(kSampleFormatU8,
197 CHANNEL_LAYOUT_STEREO, 196 CHANNEL_LAYOUT_STEREO,
198 2, 197 2,
199 kSampleRate, 198 kSampleRate,
200 16, 199 16,
201 data, 200 data,
202 kTimestamp); 201 kTimestamp);
203 EXPECT_EQ(16, buffer->frame_count()); // 2 channels of 8-bit data 202 EXPECT_EQ(16, buffer->frame_count()); // 2 channels of 8-bit data
204 203
205 buffer = AudioBuffer::CopyFrom(kSampleFormatF32, CHANNEL_LAYOUT_4_0, 4, 204 buffer = AudioBuffer::CopyFrom(kSampleFormatF32, CHANNEL_LAYOUT_4_0, 4,
206 kSampleRate, 2, data, kTimestamp); 205 kSampleRate, 2, data, kTimestamp);
207 EXPECT_EQ(2, buffer->frame_count()); // now 4 channels of 32-bit data 206 EXPECT_EQ(2, buffer->frame_count()); // now 4 channels of 32-bit data
208 } 207 }
209 208
210 TEST(AudioBufferTest, ReadU8) { 209 TEST(AudioBufferTest, ReadU8) {
211 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; 210 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
212 const int channels = ChannelLayoutToChannelCount(channel_layout); 211 const int channels = ChannelLayoutToChannelCount(channel_layout);
213 const int frames = 10; 212 const int frames = 10;
214 const base::TimeDelta start_time; 213 const base::TimeDelta start_time;
215 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<uint8>(kSampleFormatU8, 214 scoped_refptr<AudioBuffer> buffer =
216 channel_layout, 215 MakeAudioBuffer<uint8_t>(kSampleFormatU8, channel_layout, channels,
217 channels, 216 kSampleRate, 128, 1, frames, start_time);
218 kSampleRate,
219 128,
220 1,
221 frames,
222 start_time);
223 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames); 217 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
224 buffer->ReadFrames(frames, 0, 0, bus.get()); 218 buffer->ReadFrames(frames, 0, 0, bus.get());
225 VerifyBus(bus.get(), frames, 0, 1.0f / 127.0f); 219 VerifyBus(bus.get(), frames, 0, 1.0f / 127.0f);
226 220
227 // Now read the same data one frame at a time. 221 // Now read the same data one frame at a time.
228 bus->Zero(); 222 bus->Zero();
229 for (int i = 0; i < frames; ++i) 223 for (int i = 0; i < frames; ++i)
230 buffer->ReadFrames(1, i, i, bus.get()); 224 buffer->ReadFrames(1, i, i, bus.get());
231 VerifyBus(bus.get(), frames, 0, 1.0f / 127.0f); 225 VerifyBus(bus.get(), frames, 0, 1.0f / 127.0f);
232 } 226 }
233 227
234 TEST(AudioBufferTest, ReadS16) { 228 TEST(AudioBufferTest, ReadS16) {
235 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 229 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
236 const int channels = ChannelLayoutToChannelCount(channel_layout); 230 const int channels = ChannelLayoutToChannelCount(channel_layout);
237 const int frames = 10; 231 const int frames = 10;
238 const base::TimeDelta start_time; 232 const base::TimeDelta start_time;
239 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<int16>(kSampleFormatS16, 233 scoped_refptr<AudioBuffer> buffer =
240 channel_layout, 234 MakeAudioBuffer<int16_t>(kSampleFormatS16, channel_layout, channels,
241 channels, 235 kSampleRate, 1, 1, frames, start_time);
242 kSampleRate,
243 1,
244 1,
245 frames,
246 start_time);
247 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames); 236 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
248 buffer->ReadFrames(frames, 0, 0, bus.get()); 237 buffer->ReadFrames(frames, 0, 0, bus.get());
249 VerifyBus(bus.get(), frames, 1.0f / kint16max, 1.0f / kint16max); 238 VerifyBus(bus.get(), frames, 1.0f / std::numeric_limits<int16_t>::max(),
239 1.0f / std::numeric_limits<int16_t>::max());
250 240
251 // Now read the same data one frame at a time. 241 // Now read the same data one frame at a time.
252 bus->Zero(); 242 bus->Zero();
253 for (int i = 0; i < frames; ++i) 243 for (int i = 0; i < frames; ++i)
254 buffer->ReadFrames(1, i, i, bus.get()); 244 buffer->ReadFrames(1, i, i, bus.get());
255 VerifyBus(bus.get(), frames, 1.0f / kint16max, 1.0f / kint16max); 245 VerifyBus(bus.get(), frames, 1.0f / std::numeric_limits<int16_t>::max(),
246 1.0f / std::numeric_limits<int16_t>::max());
256 } 247 }
257 248
258 TEST(AudioBufferTest, ReadS32) { 249 TEST(AudioBufferTest, ReadS32) {
259 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 250 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
260 const int channels = ChannelLayoutToChannelCount(channel_layout); 251 const int channels = ChannelLayoutToChannelCount(channel_layout);
261 const int frames = 20; 252 const int frames = 20;
262 const base::TimeDelta start_time; 253 const base::TimeDelta start_time;
263 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<int32>(kSampleFormatS32, 254 scoped_refptr<AudioBuffer> buffer =
264 channel_layout, 255 MakeAudioBuffer<int32_t>(kSampleFormatS32, channel_layout, channels,
265 channels, 256 kSampleRate, 1, 1, frames, start_time);
266 kSampleRate,
267 1,
268 1,
269 frames,
270 start_time);
271 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames); 257 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
272 buffer->ReadFrames(frames, 0, 0, bus.get()); 258 buffer->ReadFrames(frames, 0, 0, bus.get());
273 VerifyBus(bus.get(), frames, 1.0f / kint32max, 1.0f / kint32max); 259 VerifyBus(bus.get(), frames, 1.0f / std::numeric_limits<int32_t>::max(),
260 1.0f / std::numeric_limits<int32_t>::max());
274 261
275 // Read second 10 frames. 262 // Read second 10 frames.
276 bus->Zero(); 263 bus->Zero();
277 buffer->ReadFrames(10, 10, 0, bus.get()); 264 buffer->ReadFrames(10, 10, 0, bus.get());
278 VerifyBus(bus.get(), 10, 11.0f / kint32max, 1.0f / kint32max); 265 VerifyBus(bus.get(), 10, 11.0f / std::numeric_limits<int32_t>::max(),
266 1.0f / std::numeric_limits<int32_t>::max());
279 } 267 }
280 268
281 TEST(AudioBufferTest, ReadF32) { 269 TEST(AudioBufferTest, ReadF32) {
282 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 270 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
283 const int channels = ChannelLayoutToChannelCount(channel_layout); 271 const int channels = ChannelLayoutToChannelCount(channel_layout);
284 const int frames = 20; 272 const int frames = 20;
285 const base::TimeDelta start_time; 273 const base::TimeDelta start_time;
286 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(kSampleFormatF32, 274 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(kSampleFormatF32,
287 channel_layout, 275 channel_layout,
288 channels, 276 channels,
(...skipping 11 matching lines...) Expand all
300 buffer->ReadFrames(10, 10, 0, bus.get()); 288 buffer->ReadFrames(10, 10, 0, bus.get());
301 VerifyBus(bus.get(), 10, 11, 1); 289 VerifyBus(bus.get(), 10, 11, 1);
302 } 290 }
303 291
304 TEST(AudioBufferTest, ReadS16Planar) { 292 TEST(AudioBufferTest, ReadS16Planar) {
305 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 293 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
306 const int channels = ChannelLayoutToChannelCount(channel_layout); 294 const int channels = ChannelLayoutToChannelCount(channel_layout);
307 const int frames = 20; 295 const int frames = 20;
308 const base::TimeDelta start_time; 296 const base::TimeDelta start_time;
309 scoped_refptr<AudioBuffer> buffer = 297 scoped_refptr<AudioBuffer> buffer =
310 MakeAudioBuffer<int16>(kSampleFormatPlanarS16, 298 MakeAudioBuffer<int16_t>(kSampleFormatPlanarS16, channel_layout, channels,
311 channel_layout, 299 kSampleRate, 1, 1, frames, start_time);
312 channels,
313 kSampleRate,
314 1,
315 1,
316 frames,
317 start_time);
318 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames); 300 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
319 buffer->ReadFrames(10, 0, 0, bus.get()); 301 buffer->ReadFrames(10, 0, 0, bus.get());
320 VerifyBus(bus.get(), 10, 1.0f / kint16max, 1.0f / kint16max); 302 VerifyBus(bus.get(), 10, 1.0f / std::numeric_limits<int16_t>::max(),
303 1.0f / std::numeric_limits<int16_t>::max());
321 304
322 // Read all the frames backwards, one by one. ch[0] should be 20, 19, ... 305 // Read all the frames backwards, one by one. ch[0] should be 20, 19, ...
323 bus->Zero(); 306 bus->Zero();
324 for (int i = frames - 1; i >= 0; --i) 307 for (int i = frames - 1; i >= 0; --i)
325 buffer->ReadFrames(1, i, i, bus.get()); 308 buffer->ReadFrames(1, i, i, bus.get());
326 VerifyBus(bus.get(), frames, 1.0f / kint16max, 1.0f / kint16max); 309 VerifyBus(bus.get(), frames, 1.0f / std::numeric_limits<int16_t>::max(),
310 1.0f / std::numeric_limits<int16_t>::max());
327 311
328 // Read 0 frames with different offsets. Existing data in AudioBus should be 312 // Read 0 frames with different offsets. Existing data in AudioBus should be
329 // unchanged. 313 // unchanged.
330 buffer->ReadFrames(0, 0, 0, bus.get()); 314 buffer->ReadFrames(0, 0, 0, bus.get());
331 VerifyBus(bus.get(), frames, 1.0f / kint16max, 1.0f / kint16max); 315 VerifyBus(bus.get(), frames, 1.0f / std::numeric_limits<int16_t>::max(),
316 1.0f / std::numeric_limits<int16_t>::max());
332 buffer->ReadFrames(0, 0, 10, bus.get()); 317 buffer->ReadFrames(0, 0, 10, bus.get());
333 VerifyBus(bus.get(), frames, 1.0f / kint16max, 1.0f / kint16max); 318 VerifyBus(bus.get(), frames, 1.0f / std::numeric_limits<int16_t>::max(),
319 1.0f / std::numeric_limits<int16_t>::max());
334 buffer->ReadFrames(0, 10, 0, bus.get()); 320 buffer->ReadFrames(0, 10, 0, bus.get());
335 VerifyBus(bus.get(), frames, 1.0f / kint16max, 1.0f / kint16max); 321 VerifyBus(bus.get(), frames, 1.0f / std::numeric_limits<int16_t>::max(),
322 1.0f / std::numeric_limits<int16_t>::max());
336 } 323 }
337 324
338 TEST(AudioBufferTest, ReadF32Planar) { 325 TEST(AudioBufferTest, ReadF32Planar) {
339 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; 326 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
340 const int channels = ChannelLayoutToChannelCount(channel_layout); 327 const int channels = ChannelLayoutToChannelCount(channel_layout);
341 const int frames = 100; 328 const int frames = 100;
342 const base::TimeDelta start_time; 329 const base::TimeDelta start_time;
343 scoped_refptr<AudioBuffer> buffer = 330 scoped_refptr<AudioBuffer> buffer =
344 MakeAudioBuffer<float>(kSampleFormatPlanarF32, 331 MakeAudioBuffer<float>(kSampleFormatPlanarF32,
345 channel_layout, 332 channel_layout,
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 435
449 static scoped_refptr<AudioBuffer> MakeReadFramesInterleavedTestBuffer( 436 static scoped_refptr<AudioBuffer> MakeReadFramesInterleavedTestBuffer(
450 SampleFormat sample_format, 437 SampleFormat sample_format,
451 int sample_rate, 438 int sample_rate,
452 ChannelLayout channel_layout, 439 ChannelLayout channel_layout,
453 int channel_count, 440 int channel_count,
454 int frames) { 441 int frames) {
455 switch (sample_format) { 442 switch (sample_format) {
456 case kSampleFormatS16: 443 case kSampleFormatS16:
457 case kSampleFormatPlanarS16: 444 case kSampleFormatPlanarS16:
458 return MakeAudioBuffer<int16>(sample_format, 445 return MakeAudioBuffer<int16_t>(sample_format, channel_layout,
459 channel_layout, 446 channel_count, sample_rate, 0, 1, frames,
460 channel_count, 447 base::TimeDelta::FromSeconds(0));
461 sample_rate,
462 0,
463 1,
464 frames,
465 base::TimeDelta::FromSeconds(0));
466 case kSampleFormatS24: 448 case kSampleFormatS24:
467 case kSampleFormatS32: 449 case kSampleFormatS32:
468 return MakeAudioBuffer<int32>(kSampleFormatS32, 450 return MakeAudioBuffer<int32_t>(kSampleFormatS32, channel_layout,
469 channel_layout, 451 channel_count, sample_rate, 0, 65536,
470 channel_count, 452 frames, base::TimeDelta::FromSeconds(0));
471 sample_rate,
472 0,
473 65536,
474 frames,
475 base::TimeDelta::FromSeconds(0));
476 case kSampleFormatF32: 453 case kSampleFormatF32:
477 case kSampleFormatPlanarF32: 454 case kSampleFormatPlanarF32:
478 return MakeAudioBuffer<float>( 455 return MakeAudioBuffer<float>(
479 sample_format, 456 sample_format, channel_layout, channel_count, sample_rate, 0.0f,
480 channel_layout, 457 65536.0f / std::numeric_limits<int32_t>::max(), frames,
481 channel_count,
482 sample_rate,
483 0.0f,
484 65536.0f / std::numeric_limits<int32>::max(),
485 frames,
486 base::TimeDelta::FromSeconds(0)); 458 base::TimeDelta::FromSeconds(0));
487 case kSampleFormatPlanarS32: 459 case kSampleFormatPlanarS32:
488 return MakeAudioBuffer<int32>( 460 return MakeAudioBuffer<int32_t>(
489 sample_format, 461 sample_format, channel_layout, channel_count, sample_rate, 0.0f,
490 channel_layout, 462 65536.0f / std::numeric_limits<int32_t>::max(), frames,
491 channel_count,
492 sample_rate,
493 0.0f,
494 65536.0f / std::numeric_limits<int32>::max(),
495 frames,
496 base::TimeDelta::FromSeconds(0)); 463 base::TimeDelta::FromSeconds(0));
497 case kSampleFormatU8: 464 case kSampleFormatU8:
498 case kUnknownSampleFormat: 465 case kUnknownSampleFormat:
499 EXPECT_FALSE(true); 466 EXPECT_FALSE(true);
500 break; 467 break;
501 } 468 }
502 return AudioBuffer::CreateEOSBuffer(); 469 return AudioBuffer::CreateEOSBuffer();
503 } 470 }
504 471
505 static void ReadFramesInterleavedS32Test(SampleFormat sample_format) { 472 static void ReadFramesInterleavedS32Test(SampleFormat sample_format) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 547
581 TEST(AudioBufferTest, ReadFramesInterleavedS16FromPlanarS16) { 548 TEST(AudioBufferTest, ReadFramesInterleavedS16FromPlanarS16) {
582 ReadFramesInterleavedS16Test(kSampleFormatPlanarS16); 549 ReadFramesInterleavedS16Test(kSampleFormatPlanarS16);
583 } 550 }
584 551
585 TEST(AudioBufferTest, ReadFramesInterleavedS16FromPlanarF32) { 552 TEST(AudioBufferTest, ReadFramesInterleavedS16FromPlanarF32) {
586 ReadFramesInterleavedS16Test(kSampleFormatPlanarF32); 553 ReadFramesInterleavedS16Test(kSampleFormatPlanarF32);
587 } 554 }
588 555
589 } // namespace media 556 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_buffer_queue_unittest.cc ('k') | media/cast/test/utility/generate_timecode_audio.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698