OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/audio_modem/audio_recorder.h" | 5 #include "components/audio_modem/audio_recorder.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/aligned_memory.h" | 10 #include "base/memory/aligned_memory.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "components/audio_modem/audio_recorder_impl.h" | 12 #include "components/audio_modem/audio_recorder_impl.h" |
13 #include "components/audio_modem/public/audio_modem_types.h" | 13 #include "components/audio_modem/public/audio_modem_types.h" |
14 #include "components/audio_modem/test/random_samples.h" | 14 #include "components/audio_modem/test/random_samples.h" |
15 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "media/audio/audio_manager.h" | 16 #include "media/audio/audio_manager.h" |
17 #include "media/audio/audio_manager_base.h" | 17 #include "media/audio/audio_manager_base.h" |
18 #include "media/base/audio_bus.h" | 18 #include "media/base/audio_bus.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
| 23 const size_t kSomeNumber = 0x9999; |
| 24 |
23 class TestAudioInputStream : public media::AudioInputStream { | 25 class TestAudioInputStream : public media::AudioInputStream { |
24 public: | 26 public: |
25 TestAudioInputStream(const media::AudioParameters& params, | 27 TestAudioInputStream(const media::AudioParameters& params, |
26 const std::vector<float*> channel_data, | 28 const std::vector<float*> channel_data, |
27 size_t samples) | 29 size_t samples) |
28 : callback_(nullptr), params_(params) { | 30 : callback_(nullptr), params_(params) { |
29 buffer_ = media::AudioBus::CreateWrapper(2); | 31 buffer_ = media::AudioBus::CreateWrapper(2); |
30 for (size_t i = 0; i < channel_data.size(); ++i) | 32 for (size_t i = 0; i < channel_data.size(); ++i) |
31 buffer_->SetChannelData(i, channel_data[i]); | 33 buffer_->SetChannelData(i, channel_data[i]); |
32 buffer_->set_frames(samples); | 34 buffer_->set_frames(samples); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 media::AudioManager::CreateForTesting(); | 82 media::AudioManager::CreateForTesting(); |
81 } | 83 } |
82 | 84 |
83 ~AudioRecorderTest() override { | 85 ~AudioRecorderTest() override { |
84 DeleteRecorder(); | 86 DeleteRecorder(); |
85 for (size_t i = 0; i < channel_data_.size(); ++i) | 87 for (size_t i = 0; i < channel_data_.size(); ++i) |
86 base::AlignedFree(channel_data_[i]); | 88 base::AlignedFree(channel_data_[i]); |
87 } | 89 } |
88 | 90 |
89 void CreateSimpleRecorder() { | 91 void CreateSimpleRecorder() { |
90 DeleteRecorder(); | 92 // If we have input devices, we'll create a recorder which uses a real |
91 recorder_ = new AudioRecorderImpl(); | 93 // input stream, if not, we'll create a recorder which uses our mock input |
92 recorder_->Initialize( | 94 // stream. |
93 base::Bind(&AudioRecorderTest::DecodeSamples, base::Unretained(this))); | 95 if (media::AudioManager::Get()->HasAudioInputDevices()) { |
| 96 DeleteRecorder(); |
| 97 recorder_ = new AudioRecorderImpl(); |
| 98 recorder_->Initialize(base::Bind(&AudioRecorderTest::DecodeSamples, |
| 99 base::Unretained(this))); |
| 100 } else { |
| 101 CreateRecorder(kSomeNumber); |
| 102 } |
94 } | 103 } |
95 | 104 |
96 void CreateRecorder(size_t channels, | 105 void CreateRecorder(size_t samples) { |
97 size_t sample_rate, | |
98 size_t bits_per_sample, | |
99 size_t samples) { | |
100 DeleteRecorder(); | 106 DeleteRecorder(); |
101 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 107 |
102 kDefaultChannelLayout, | 108 params_ = media::AudioManager::Get()->GetInputStreamParameters( |
103 channels, | 109 media::AudioManagerBase::kDefaultDeviceId); |
104 sample_rate, | |
105 bits_per_sample, | |
106 4096); | |
107 | 110 |
108 channel_data_.clear(); | 111 channel_data_.clear(); |
109 channel_data_.push_back(GenerateSamples(0x1337, samples)); | 112 channel_data_.push_back(GenerateSamples(0x1337, samples)); |
110 channel_data_.push_back(GenerateSamples(0x7331, samples)); | 113 channel_data_.push_back(GenerateSamples(0x7331, samples)); |
111 | 114 |
112 total_samples_ = samples; | 115 total_samples_ = samples; |
113 | 116 |
114 recorder_ = new AudioRecorderImpl(); | 117 recorder_ = new AudioRecorderImpl(); |
115 recorder_->set_input_stream_for_testing( | 118 recorder_->set_input_stream_for_testing( |
116 new TestAudioInputStream(params_, channel_data_, samples)); | 119 new TestAudioInputStream(params_, channel_data_, samples)); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 | 189 |
187 // Deleted by calling Finalize() on the object. | 190 // Deleted by calling Finalize() on the object. |
188 AudioRecorderImpl* recorder_; | 191 AudioRecorderImpl* recorder_; |
189 | 192 |
190 std::string received_samples_; | 193 std::string received_samples_; |
191 | 194 |
192 scoped_ptr<base::RunLoop> run_loop_; | 195 scoped_ptr<base::RunLoop> run_loop_; |
193 content::TestBrowserThreadBundle thread_bundle_; | 196 content::TestBrowserThreadBundle thread_bundle_; |
194 }; | 197 }; |
195 | 198 |
196 // TODO(rkc): These tests are broken on all platforms. | 199 TEST_F(AudioRecorderTest, BasicRecordAndStop) { |
197 // On Windows and Mac, we cannot use non-OS params. The tests need to be | |
198 // rewritten to use the params provided to us by the audio manager | |
199 // rather than setting our own params. | |
200 // On Linux, there is a memory leak in the audio code during initialization. | |
201 #define MAYBE_BasicRecordAndStop DISABLED_BasicRecordAndStop | |
202 #define MAYBE_OutOfOrderRecordAndStopMultiple \ | |
203 DISABLED_OutOfOrderRecordAndStopMultiple | |
204 #define MAYBE_RecordingEndToEnd DISABLED_RecordingEndToEnd | |
205 | |
206 TEST_F(AudioRecorderTest, MAYBE_BasicRecordAndStop) { | |
207 CreateSimpleRecorder(); | 200 CreateSimpleRecorder(); |
208 | 201 |
209 recorder_->Record(); | 202 recorder_->Record(); |
210 EXPECT_TRUE(IsRecording()); | 203 EXPECT_TRUE(IsRecording()); |
211 recorder_->Stop(); | 204 recorder_->Stop(); |
212 EXPECT_FALSE(IsRecording()); | 205 EXPECT_FALSE(IsRecording()); |
213 recorder_->Record(); | 206 recorder_->Record(); |
214 | 207 |
215 EXPECT_TRUE(IsRecording()); | 208 EXPECT_TRUE(IsRecording()); |
216 recorder_->Stop(); | 209 recorder_->Stop(); |
217 EXPECT_FALSE(IsRecording()); | 210 EXPECT_FALSE(IsRecording()); |
218 recorder_->Record(); | 211 recorder_->Record(); |
219 | 212 |
220 EXPECT_TRUE(IsRecording()); | 213 EXPECT_TRUE(IsRecording()); |
221 recorder_->Stop(); | 214 recorder_->Stop(); |
222 EXPECT_FALSE(IsRecording()); | 215 EXPECT_FALSE(IsRecording()); |
223 | 216 |
224 DeleteRecorder(); | 217 DeleteRecorder(); |
225 } | 218 } |
226 | 219 |
227 TEST_F(AudioRecorderTest, MAYBE_OutOfOrderRecordAndStopMultiple) { | 220 TEST_F(AudioRecorderTest, OutOfOrderRecordAndStopMultiple) { |
228 CreateSimpleRecorder(); | 221 CreateSimpleRecorder(); |
229 | 222 |
230 recorder_->Stop(); | 223 recorder_->Stop(); |
231 recorder_->Stop(); | 224 recorder_->Stop(); |
232 recorder_->Stop(); | 225 recorder_->Stop(); |
233 EXPECT_FALSE(IsRecording()); | 226 EXPECT_FALSE(IsRecording()); |
234 | 227 |
235 recorder_->Record(); | 228 recorder_->Record(); |
236 recorder_->Record(); | 229 recorder_->Record(); |
237 EXPECT_TRUE(IsRecording()); | 230 EXPECT_TRUE(IsRecording()); |
238 | 231 |
239 recorder_->Stop(); | 232 recorder_->Stop(); |
240 recorder_->Stop(); | 233 recorder_->Stop(); |
241 EXPECT_FALSE(IsRecording()); | 234 EXPECT_FALSE(IsRecording()); |
242 | 235 |
243 DeleteRecorder(); | 236 DeleteRecorder(); |
244 } | 237 } |
245 | 238 |
246 TEST_F(AudioRecorderTest, MAYBE_RecordingEndToEnd) { | 239 TEST_F(AudioRecorderTest, RecordingEndToEnd) { |
247 const int kNumSamples = 48000 * 3; | 240 const int kNumSamples = 48000 * 3; |
248 CreateRecorder( | 241 CreateRecorder(kNumSamples); |
249 kDefaultChannels, kDefaultSampleRate, kDefaultBitsPerSample, kNumSamples); | |
250 | 242 |
251 RecordAndVerifySamples(); | 243 RecordAndVerifySamples(); |
252 | 244 |
253 DeleteRecorder(); | 245 DeleteRecorder(); |
254 } | 246 } |
255 | 247 |
256 // TODO(rkc): Add tests with recording different sample rates. | 248 // TODO(rkc): Add tests with recording different sample rates. |
257 | 249 |
258 } // namespace audio_modem | 250 } // namespace audio_modem |
OLD | NEW |