Chromium Code Reviews| Index: media/audio/simple_sources_unittest.cc |
| diff --git a/media/audio/simple_sources_unittest.cc b/media/audio/simple_sources_unittest.cc |
| index 521b7864e7af587c7f7878489a85f8133e733600..7a346ab6f91a242faf04fd3f8567073937833595 100644 |
| --- a/media/audio/simple_sources_unittest.cc |
| +++ b/media/audio/simple_sources_unittest.cc |
| @@ -78,7 +78,31 @@ TEST(SimpleSources, OnError) { |
| EXPECT_EQ(2, source.errors()); |
| } |
| -TEST(SimpleSources, FileSourceTestData) { |
| +void VerifyContainsTestFile(const AudioBus* audio_bus) { |
| + // Convert the test data (little-endian) into floats and compare. |
|
tommi (sloooow) - chröme
2016/06/08 14:47:33
can you update the comment to explain the value th
phoglund_chromium
2016/06/09 12:08:31
Done.
|
| + const int kFirstSampleIndex = 12 + 8 + 16 + 8; |
| + int16_t data[2]; |
| + data[0] = kTestAudioData[kFirstSampleIndex]; |
| + data[0] |= (kTestAudioData[kFirstSampleIndex + 1] << 8); |
| + data[1] = kTestAudioData[kFirstSampleIndex + 2]; |
| + data[1] |= (kTestAudioData[kFirstSampleIndex + 3] << 8); |
| + |
| + // The first frame should hold the WAV data. |
| + EXPECT_FLOAT_EQ(static_cast<float>(data[0]) / ((1 << 15) - 1), |
| + audio_bus->channel(0)[0]); |
| + EXPECT_FLOAT_EQ(static_cast<float>(data[1]) / ((1 << 15) - 1), |
| + audio_bus->channel(1)[0]); |
| + |
| + // All other frames should be zero-padded. This applies even when looping, as |
| + // the looping will restart on the next call to OnMoreData. |
| + for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| + for (int frame = 1; frame < audio_bus->frames(); ++frame) { |
| + EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| + } |
| + } |
| +} |
| + |
| +TEST(SimpleSources, FileSourceTestDataWithoutLooping) { |
| const int kNumFrames = 10; |
| // Create a temporary file filled with WAV data. |
| @@ -97,31 +121,51 @@ TEST(SimpleSources, FileSourceTestData) { |
| audio_bus->Zero(); |
| // Create a FileSource that reads this file. |
| - FileSource source(params, temp_path); |
| + bool looping = false; |
|
tommi (sloooow) - chröme
2016/06/08 14:47:33
nit: should this variable/param name be 'loop' rat
phoglund_chromium
2016/06/09 12:08:31
Done.
|
| + FileSource source(params, temp_path, looping); |
| EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); |
| - // Convert the test data (little-endian) into floats and compare. |
| - const int kFirstSampleIndex = 12 + 8 + 16 + 8; |
| - int16_t data[2]; |
| - data[0] = kTestAudioData[kFirstSampleIndex]; |
| - data[0] |= (kTestAudioData[kFirstSampleIndex + 1] << 8); |
| - data[1] = kTestAudioData[kFirstSampleIndex + 2]; |
| - data[1] |= (kTestAudioData[kFirstSampleIndex + 3] << 8); |
| - |
| - // The first frame should hold the WAV data. |
| - EXPECT_FLOAT_EQ(static_cast<float>(data[0]) / ((1 << 15) - 1), |
| - audio_bus->channel(0)[0]); |
| - EXPECT_FLOAT_EQ(static_cast<float>(data[1]) / ((1 << 15) - 1), |
| - audio_bus->channel(1)[0]); |
| + VerifyContainsTestFile(audio_bus.get()); |
| - // All other frames should be zero-padded. |
| + // We should not play any more audio after the file reaches its end. |
| + audio_bus->Zero(); |
| + source.OnMoreData(audio_bus.get(), 0, 0); |
| for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| - for (int frame = 1; frame < audio_bus->frames(); ++frame) { |
| + for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| } |
| } |
| } |
| +TEST(SimpleSources, FileSourceTestDataWithLooping) { |
| + const int kNumFrames = 10; |
| + |
| + // Create a temporary file filled with WAV data. |
| + base::FilePath temp_path; |
| + ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
| + base::File temp(temp_path, |
| + base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
| + temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
| + ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| + temp.Close(); |
| + |
| + // Create AudioParameters which match those in the WAV data. |
| + AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| + CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| + std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| + audio_bus->Zero(); |
| + |
| + bool looping = true; |
| + FileSource source(params, temp_path, looping); |
| + |
| + // Verify that we keep reading in the file when looping. |
| + source.OnMoreData(audio_bus.get(), 0, 0); |
| + audio_bus->Zero(); |
| + source.OnMoreData(audio_bus.get(), 0, 0); |
| + |
| + VerifyContainsTestFile(audio_bus.get()); |
| +} |
| + |
| TEST(SimpleSources, BadFilePathFails) { |
| AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| CHANNEL_LAYOUT_STEREO, 48000, 16, 10); |
| @@ -133,7 +177,8 @@ TEST(SimpleSources, BadFilePathFails) { |
| path = path.Append(FILE_PATH_LITERAL("does")) |
| .Append(FILE_PATH_LITERAL("not")) |
| .Append(FILE_PATH_LITERAL("exist")); |
| - FileSource source(params, path); |
| + bool looping = false; |
| + FileSource source(params, path, looping); |
| EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
| // Confirm all frames are zero-padded. |
| @@ -167,7 +212,8 @@ TEST(SimpleSources, FileSourceCorruptTestDataFails) { |
| audio_bus->Zero(); |
| // Create a FileSource that reads this file. |
| - FileSource source(params, temp_path); |
| + bool looping = false; |
| + FileSource source(params, temp_path, looping); |
| EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); |
| // Confirm all frames are zero-padded. |