OLD | NEW |
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 <limits> | 5 #include <limits> |
6 | 6 |
| 7 #include "base/files/file_util.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "media/audio/audio_parameters.h" | 11 #include "media/audio/audio_parameters.h" |
11 #include "media/audio/simple_sources.h" | 12 #include "media/audio/simple_sources.h" |
| 13 #include "media/audio/sounds/test_data.h" |
12 #include "media/base/audio_bus.h" | 14 #include "media/base/audio_bus.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 namespace media { | 17 namespace media { |
16 | 18 |
17 // Validate that the SineWaveAudioSource writes the expected values. | 19 // Validate that the SineWaveAudioSource writes the expected values. |
18 TEST(SimpleSources, SineWaveAudioSource) { | 20 TEST(SimpleSources, SineWaveAudioSource) { |
19 static const uint32 samples = 1024; | 21 static const uint32 samples = 1024; |
20 static const uint32 bytes_per_sample = 2; | 22 static const uint32 bytes_per_sample = 2; |
21 static const int freq = 200; | 23 static const int freq = 200; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 } | 70 } |
69 | 71 |
70 TEST(SimpleSources, OnError) { | 72 TEST(SimpleSources, OnError) { |
71 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 73 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
72 source.OnError(NULL); | 74 source.OnError(NULL); |
73 EXPECT_EQ(1, source.errors()); | 75 EXPECT_EQ(1, source.errors()); |
74 source.OnError(NULL); | 76 source.OnError(NULL); |
75 EXPECT_EQ(2, source.errors()); | 77 EXPECT_EQ(2, source.errors()); |
76 } | 78 } |
77 | 79 |
| 80 TEST(SimpleSources, FileSourceTestData) { |
| 81 const int kNumFrames = 10; |
| 82 |
| 83 // Create a temporary file filled with WAV data. |
| 84 base::FilePath temp_path; |
| 85 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
| 86 base::File temp(temp_path, |
| 87 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
| 88 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
| 89 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 90 temp.Close(); |
| 91 |
| 92 // Create AudioParameters which match those in the WAV data. |
| 93 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 94 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 95 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 96 audio_bus->Zero(); |
| 97 |
| 98 // Create a FileSource that reads this file. |
| 99 FileSource source(params, temp_path); |
| 100 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0)); |
| 101 |
| 102 // Convert the test data (little-endian) into floats and compare. |
| 103 const int kFirstSampleIndex = 12 + 8 + 16 + 8; |
| 104 int16_t data[2]; |
| 105 data[0] = kTestAudioData[kFirstSampleIndex]; |
| 106 data[0] |= (kTestAudioData[kFirstSampleIndex + 1] << 8); |
| 107 data[1] = kTestAudioData[kFirstSampleIndex + 2]; |
| 108 data[1] |= (kTestAudioData[kFirstSampleIndex + 3] << 8); |
| 109 |
| 110 // The first frame should hold the WAV data. |
| 111 EXPECT_FLOAT_EQ(static_cast<float>(data[0]) / ((1 << 15) - 1), |
| 112 audio_bus->channel(0)[0]); |
| 113 EXPECT_FLOAT_EQ(static_cast<float>(data[1]) / ((1 << 15) - 1), |
| 114 audio_bus->channel(1)[0]); |
| 115 |
| 116 // All other frames should be zero-padded. |
| 117 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 118 for (int frame = 1; frame < audio_bus->frames(); ++frame) { |
| 119 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 120 } |
| 121 } |
| 122 } |
| 123 |
| 124 TEST(SimpleSources, BadFilePathFails) { |
| 125 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 126 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); |
| 127 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); |
| 128 audio_bus->Zero(); |
| 129 |
| 130 // Create a FileSource that reads this file. |
| 131 base::FilePath path; |
| 132 path = path.Append(FILE_PATH_LITERAL("does")) |
| 133 .Append(FILE_PATH_LITERAL("not")) |
| 134 .Append(FILE_PATH_LITERAL("exist")); |
| 135 FileSource source(params, path); |
| 136 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0)); |
| 137 |
| 138 // Confirm all frames are zero-padded. |
| 139 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 140 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 141 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 142 } |
| 143 } |
| 144 } |
| 145 |
| 146 TEST(SimpleSources, FileSourceCorruptTestDataFails) { |
| 147 const int kNumFrames = 10; |
| 148 |
| 149 // Create a temporary file filled with WAV data. |
| 150 base::FilePath temp_path; |
| 151 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
| 152 base::File temp(temp_path, |
| 153 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
| 154 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
| 155 |
| 156 // Corrupt the header. |
| 157 temp.Write(3, "0x00", 1); |
| 158 |
| 159 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
| 160 temp.Close(); |
| 161 |
| 162 // Create AudioParameters which match those in the WAV data. |
| 163 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 164 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
| 165 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
| 166 audio_bus->Zero(); |
| 167 |
| 168 // Create a FileSource that reads this file. |
| 169 FileSource source(params, temp_path); |
| 170 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0)); |
| 171 |
| 172 // Confirm all frames are zero-padded. |
| 173 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
| 174 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
| 175 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
| 176 } |
| 177 } |
| 178 } |
| 179 |
78 } // namespace media | 180 } // namespace media |
OLD | NEW |