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 "media/audio/simple_sources.h" | 5 #include "media/audio/simple_sources.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 #include <memory> | 11 #include <memory> |
12 | 12 |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/time/time.h" |
15 #include "media/audio/sounds/test_data.h" | 16 #include "media/audio/sounds/test_data.h" |
16 #include "media/base/audio_bus.h" | 17 #include "media/base/audio_bus.h" |
17 #include "media/base/audio_parameters.h" | 18 #include "media/base/audio_parameters.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 // Validate that the SineWaveAudioSource writes the expected values. | 23 // Validate that the SineWaveAudioSource writes the expected values. |
23 TEST(SimpleSources, SineWaveAudioSource) { | 24 TEST(SimpleSources, SineWaveAudioSource) { |
24 static const uint32_t samples = 1024; | 25 static const uint32_t samples = 1024; |
25 static const uint32_t bytes_per_sample = 2; | 26 static const uint32_t bytes_per_sample = 2; |
26 static const int freq = 200; | 27 static const int freq = 200; |
27 | 28 |
28 AudioParameters params( | 29 AudioParameters params( |
29 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 30 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
30 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); | 31 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); |
31 | 32 |
32 SineWaveAudioSource source(1, freq, params.sample_rate()); | 33 SineWaveAudioSource source(1, freq, params.sample_rate()); |
33 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(params); | 34 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(params); |
34 source.OnMoreData(audio_bus.get(), 0, 0); | 35 source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 36 audio_bus.get()); |
35 EXPECT_EQ(1, source.callbacks()); | 37 EXPECT_EQ(1, source.callbacks()); |
36 EXPECT_EQ(0, source.errors()); | 38 EXPECT_EQ(0, source.errors()); |
37 | 39 |
38 uint32_t half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); | 40 uint32_t half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); |
39 | 41 |
40 // Spot test positive incursion of sine wave. | 42 // Spot test positive incursion of sine wave. |
41 EXPECT_NEAR(0, audio_bus->channel(0)[0], | 43 EXPECT_NEAR(0, audio_bus->channel(0)[0], |
42 std::numeric_limits<float>::epsilon()); | 44 std::numeric_limits<float>::epsilon()); |
43 EXPECT_FLOAT_EQ(0.15643446f, audio_bus->channel(0)[1]); | 45 EXPECT_FLOAT_EQ(0.15643446f, audio_bus->channel(0)[1]); |
44 EXPECT_LT(audio_bus->channel(0)[1], audio_bus->channel(0)[2]); | 46 EXPECT_LT(audio_bus->channel(0)[1], audio_bus->channel(0)[2]); |
45 EXPECT_LT(audio_bus->channel(0)[2], audio_bus->channel(0)[3]); | 47 EXPECT_LT(audio_bus->channel(0)[2], audio_bus->channel(0)[3]); |
46 // Spot test negative incursion of sine wave. | 48 // Spot test negative incursion of sine wave. |
47 EXPECT_NEAR(0, audio_bus->channel(0)[half_period], | 49 EXPECT_NEAR(0, audio_bus->channel(0)[half_period], |
48 std::numeric_limits<float>::epsilon()); | 50 std::numeric_limits<float>::epsilon()); |
49 EXPECT_FLOAT_EQ(-0.15643446f, audio_bus->channel(0)[half_period + 1]); | 51 EXPECT_FLOAT_EQ(-0.15643446f, audio_bus->channel(0)[half_period + 1]); |
50 EXPECT_GT(audio_bus->channel(0)[half_period + 1], | 52 EXPECT_GT(audio_bus->channel(0)[half_period + 1], |
51 audio_bus->channel(0)[half_period + 2]); | 53 audio_bus->channel(0)[half_period + 2]); |
52 EXPECT_GT(audio_bus->channel(0)[half_period + 2], | 54 EXPECT_GT(audio_bus->channel(0)[half_period + 2], |
53 audio_bus->channel(0)[half_period + 3]); | 55 audio_bus->channel(0)[half_period + 3]); |
54 } | 56 } |
55 | 57 |
56 TEST(SimpleSources, SineWaveAudioCapped) { | 58 TEST(SimpleSources, SineWaveAudioCapped) { |
57 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 59 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
58 | 60 |
59 static const int kSampleCap = 100; | 61 static const int kSampleCap = 100; |
60 source.CapSamples(kSampleCap); | 62 source.CapSamples(kSampleCap); |
61 | 63 |
62 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap); | 64 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap); |
63 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); | 65 EXPECT_EQ(source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 66 audio_bus.get()), |
| 67 kSampleCap); |
64 EXPECT_EQ(1, source.callbacks()); | 68 EXPECT_EQ(1, source.callbacks()); |
65 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), 0); | 69 EXPECT_EQ(source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 70 audio_bus.get()), |
| 71 0); |
66 EXPECT_EQ(2, source.callbacks()); | 72 EXPECT_EQ(2, source.callbacks()); |
67 source.Reset(); | 73 source.Reset(); |
68 EXPECT_EQ(source.OnMoreData(audio_bus.get(), 0, 0), kSampleCap); | 74 EXPECT_EQ(source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 75 audio_bus.get()), |
| 76 kSampleCap); |
69 EXPECT_EQ(3, source.callbacks()); | 77 EXPECT_EQ(3, source.callbacks()); |
70 EXPECT_EQ(0, source.errors()); | 78 EXPECT_EQ(0, source.errors()); |
71 } | 79 } |
72 | 80 |
73 TEST(SimpleSources, OnError) { | 81 TEST(SimpleSources, OnError) { |
74 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); | 82 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); |
75 source.OnError(NULL); | 83 source.OnError(NULL); |
76 EXPECT_EQ(1, source.errors()); | 84 EXPECT_EQ(1, source.errors()); |
77 source.OnError(NULL); | 85 source.OnError(NULL); |
78 EXPECT_EQ(2, source.errors()); | 86 EXPECT_EQ(2, source.errors()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 125 |
118 // Create AudioParameters which match those in the WAV data. | 126 // Create AudioParameters which match those in the WAV data. |
119 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 127 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
120 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 128 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
121 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 129 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
122 audio_bus->Zero(); | 130 audio_bus->Zero(); |
123 | 131 |
124 // Create a FileSource that reads this file. | 132 // Create a FileSource that reads this file. |
125 bool loop = false; | 133 bool loop = false; |
126 FileSource source(params, temp_path, loop); | 134 FileSource source(params, temp_path, loop); |
127 EXPECT_EQ(kNumFrames, source.OnMoreData(audio_bus.get(), 0, 0)); | 135 EXPECT_EQ(kNumFrames, |
| 136 source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 137 audio_bus.get())); |
128 | 138 |
129 VerifyContainsTestFile(audio_bus.get()); | 139 VerifyContainsTestFile(audio_bus.get()); |
130 | 140 |
131 // We should not play any more audio after the file reaches its end. | 141 // We should not play any more audio after the file reaches its end. |
132 audio_bus->Zero(); | 142 audio_bus->Zero(); |
133 source.OnMoreData(audio_bus.get(), 0, 0); | 143 source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 144 audio_bus.get()); |
134 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 145 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
135 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 146 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
136 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 147 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
137 } | 148 } |
138 } | 149 } |
139 } | 150 } |
140 | 151 |
141 TEST(SimpleSources, FileSourceTestDataWithLooping) { | 152 TEST(SimpleSources, FileSourceTestDataWithLooping) { |
142 const int kNumFrames = 10; | 153 const int kNumFrames = 10; |
143 | 154 |
144 // Create a temporary file filled with WAV data. | 155 // Create a temporary file filled with WAV data. |
145 base::FilePath temp_path; | 156 base::FilePath temp_path; |
146 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); | 157 ASSERT_TRUE(base::CreateTemporaryFile(&temp_path)); |
147 base::File temp(temp_path, | 158 base::File temp(temp_path, |
148 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); | 159 base::File::FLAG_WRITE | base::File::FLAG_OPEN_ALWAYS); |
149 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); | 160 temp.WriteAtCurrentPos(kTestAudioData, kTestAudioDataSize); |
150 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); | 161 ASSERT_EQ(kTestAudioDataSize, static_cast<size_t>(temp.GetLength())); |
151 temp.Close(); | 162 temp.Close(); |
152 | 163 |
153 // Create AudioParameters which match those in the WAV data. | 164 // Create AudioParameters which match those in the WAV data. |
154 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 165 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
155 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 166 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
156 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 167 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
157 audio_bus->Zero(); | 168 audio_bus->Zero(); |
158 | 169 |
159 bool loop = true; | 170 bool loop = true; |
160 FileSource source(params, temp_path, loop); | 171 FileSource source(params, temp_path, loop); |
161 | 172 |
162 // Verify that we keep reading in the file when looping. | 173 // Verify that we keep reading in the file when looping. |
163 source.OnMoreData(audio_bus.get(), 0, 0); | 174 source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 175 audio_bus.get()); |
164 audio_bus->Zero(); | 176 audio_bus->Zero(); |
165 source.OnMoreData(audio_bus.get(), 0, 0); | 177 source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 178 audio_bus.get()); |
166 | 179 |
167 VerifyContainsTestFile(audio_bus.get()); | 180 VerifyContainsTestFile(audio_bus.get()); |
168 } | 181 } |
169 | 182 |
170 TEST(SimpleSources, BadFilePathFails) { | 183 TEST(SimpleSources, BadFilePathFails) { |
171 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 184 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
172 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); | 185 CHANNEL_LAYOUT_STEREO, 48000, 16, 10); |
173 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); | 186 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, 10); |
174 audio_bus->Zero(); | 187 audio_bus->Zero(); |
175 | 188 |
176 // Create a FileSource that reads this file. | 189 // Create a FileSource that reads this file. |
177 base::FilePath path; | 190 base::FilePath path; |
178 path = path.Append(FILE_PATH_LITERAL("does")) | 191 path = path.Append(FILE_PATH_LITERAL("does")) |
179 .Append(FILE_PATH_LITERAL("not")) | 192 .Append(FILE_PATH_LITERAL("not")) |
180 .Append(FILE_PATH_LITERAL("exist")); | 193 .Append(FILE_PATH_LITERAL("exist")); |
181 bool loop = false; | 194 bool loop = false; |
182 FileSource source(params, path, loop); | 195 FileSource source(params, path, loop); |
183 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 196 EXPECT_EQ(0, source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 197 audio_bus.get())); |
184 | 198 |
185 // Confirm all frames are zero-padded. | 199 // Confirm all frames are zero-padded. |
186 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 200 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
187 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 201 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
188 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 202 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
189 } | 203 } |
190 } | 204 } |
191 } | 205 } |
192 | 206 |
193 TEST(SimpleSources, FileSourceCorruptTestDataFails) { | 207 TEST(SimpleSources, FileSourceCorruptTestDataFails) { |
(...skipping 14 matching lines...) Expand all Loading... |
208 | 222 |
209 // Create AudioParameters which match those in the WAV data. | 223 // Create AudioParameters which match those in the WAV data. |
210 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 224 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
211 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); | 225 CHANNEL_LAYOUT_STEREO, 48000, 16, kNumFrames); |
212 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); | 226 std::unique_ptr<AudioBus> audio_bus = AudioBus::Create(2, kNumFrames); |
213 audio_bus->Zero(); | 227 audio_bus->Zero(); |
214 | 228 |
215 // Create a FileSource that reads this file. | 229 // Create a FileSource that reads this file. |
216 bool loop = false; | 230 bool loop = false; |
217 FileSource source(params, temp_path, loop); | 231 FileSource source(params, temp_path, loop); |
218 EXPECT_EQ(0, source.OnMoreData(audio_bus.get(), 0, 0)); | 232 EXPECT_EQ(0, source.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, |
| 233 audio_bus.get())); |
219 | 234 |
220 // Confirm all frames are zero-padded. | 235 // Confirm all frames are zero-padded. |
221 for (int channel = 0; channel < audio_bus->channels(); ++channel) { | 236 for (int channel = 0; channel < audio_bus->channels(); ++channel) { |
222 for (int frame = 0; frame < audio_bus->frames(); ++frame) { | 237 for (int frame = 0; frame < audio_bus->frames(); ++frame) { |
223 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); | 238 EXPECT_FLOAT_EQ(0.0, audio_bus->channel(channel)[frame]); |
224 } | 239 } |
225 } | 240 } |
226 } | 241 } |
227 | 242 |
228 } // namespace media | 243 } // namespace media |
OLD | NEW |