| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/aligned_memory.h" | 13 #include "base/memory/aligned_memory.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "components/audio_modem/audio_recorder_impl.h" | 17 #include "components/audio_modem/audio_recorder_impl.h" |
| 17 #include "components/audio_modem/public/audio_modem_types.h" | 18 #include "components/audio_modem/public/audio_modem_types.h" |
| 18 #include "components/audio_modem/test/random_samples.h" | 19 #include "components/audio_modem/test/random_samples.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "media/audio/audio_manager.h" | 21 #include "media/audio/audio_manager.h" |
| 21 #include "media/audio/audio_manager_base.h" | 22 #include "media/audio/audio_manager_base.h" |
| 22 #include "media/base/audio_bus.h" | 23 #include "media/base/audio_bus.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 DISALLOW_COPY_AND_ASSIGN(TestAudioInputStream); | 76 DISALLOW_COPY_AND_ASSIGN(TestAudioInputStream); |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 } // namespace | 79 } // namespace |
| 79 | 80 |
| 80 namespace audio_modem { | 81 namespace audio_modem { |
| 81 | 82 |
| 82 class AudioRecorderTest : public testing::Test { | 83 class AudioRecorderTest : public testing::Test { |
| 83 public: | 84 public: |
| 84 AudioRecorderTest() : total_samples_(0), recorder_(nullptr) { | 85 AudioRecorderTest() : total_samples_(0), recorder_(nullptr) { |
| 85 if (!media::AudioManager::Get()) | 86 audio_manager_ = media::AudioManager::CreateForTesting( |
| 86 media::AudioManager::CreateForTesting(); | 87 base::ThreadTaskRunnerHandle::Get()); |
| 88 base::RunLoop().RunUntilIdle(); |
| 87 } | 89 } |
| 88 | 90 |
| 89 ~AudioRecorderTest() override { | 91 ~AudioRecorderTest() override { |
| 90 DeleteRecorder(); | 92 DeleteRecorder(); |
| 91 for (size_t i = 0; i < channel_data_.size(); ++i) | 93 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 92 base::AlignedFree(channel_data_[i]); | 94 base::AlignedFree(channel_data_[i]); |
| 93 } | 95 } |
| 94 | 96 |
| 95 void CreateSimpleRecorder() { | 97 void CreateSimpleRecorder() { |
| 96 // If we have input devices, we'll create a recorder which uses a real | 98 // If we have input devices, we'll create a recorder which uses a real |
| 97 // input stream, if not, we'll create a recorder which uses our mock input | 99 // input stream, if not, we'll create a recorder which uses our mock input |
| 98 // stream. | 100 // stream. |
| 99 if (media::AudioManager::Get()->HasAudioInputDevices()) { | 101 if (media::AudioManager::Get()->HasAudioInputDevices()) { |
| 100 DeleteRecorder(); | 102 DeleteRecorder(); |
| 101 recorder_ = new AudioRecorderImpl(); | 103 recorder_ = new AudioRecorderImpl(); |
| 102 recorder_->Initialize(base::Bind(&AudioRecorderTest::DecodeSamples, | 104 recorder_->Initialize(base::Bind(&AudioRecorderTest::DecodeSamples, |
| 103 base::Unretained(this))); | 105 base::Unretained(this))); |
| 106 base::RunLoop().RunUntilIdle(); |
| 104 } else { | 107 } else { |
| 105 CreateRecorder(kSomeNumber); | 108 CreateRecorder(kSomeNumber); |
| 106 } | 109 } |
| 107 } | 110 } |
| 108 | 111 |
| 109 void CreateRecorder(size_t samples) { | 112 void CreateRecorder(size_t samples) { |
| 110 DeleteRecorder(); | 113 DeleteRecorder(); |
| 111 | 114 |
| 112 params_ = media::AudioManager::Get()->GetInputStreamParameters( | 115 params_ = media::AudioManager::Get()->GetInputStreamParameters( |
| 113 media::AudioManagerBase::kDefaultDeviceId); | 116 media::AudioManagerBase::kDefaultDeviceId); |
| 114 | 117 |
| 115 channel_data_.clear(); | 118 channel_data_.clear(); |
| 116 channel_data_.push_back(GenerateSamples(0x1337, samples)); | 119 channel_data_.push_back(GenerateSamples(0x1337, samples)); |
| 117 channel_data_.push_back(GenerateSamples(0x7331, samples)); | 120 channel_data_.push_back(GenerateSamples(0x7331, samples)); |
| 118 | 121 |
| 119 total_samples_ = samples; | 122 total_samples_ = samples; |
| 120 | 123 |
| 121 recorder_ = new AudioRecorderImpl(); | 124 recorder_ = new AudioRecorderImpl(); |
| 122 recorder_->set_input_stream_for_testing( | 125 recorder_->set_input_stream_for_testing( |
| 123 new TestAudioInputStream(params_, channel_data_, samples)); | 126 new TestAudioInputStream(params_, channel_data_, samples)); |
| 124 recorder_->set_params_for_testing(new media::AudioParameters(params_)); | 127 recorder_->set_params_for_testing(new media::AudioParameters(params_)); |
| 125 recorder_->Initialize( | 128 recorder_->Initialize( |
| 126 base::Bind(&AudioRecorderTest::DecodeSamples, base::Unretained(this))); | 129 base::Bind(&AudioRecorderTest::DecodeSamples, base::Unretained(this))); |
| 130 base::RunLoop().RunUntilIdle(); |
| 127 } | 131 } |
| 128 | 132 |
| 129 void DeleteRecorder() { | 133 void DeleteRecorder() { |
| 130 if (!recorder_) | 134 if (!recorder_) |
| 131 return; | 135 return; |
| 132 recorder_->Finalize(); | 136 recorder_->Finalize(); |
| 133 recorder_ = nullptr; | 137 recorder_ = nullptr; |
| 138 base::RunLoop().RunUntilIdle(); |
| 134 } | 139 } |
| 135 | 140 |
| 136 void RecordAndVerifySamples() { | 141 void RecordAndVerifySamples() { |
| 137 received_samples_.clear(); | 142 received_samples_.clear(); |
| 138 run_loop_.reset(new base::RunLoop()); | 143 run_loop_.reset(new base::RunLoop()); |
| 139 recorder_->Record(); | 144 recorder_->Record(); |
| 140 run_loop_->Run(); | 145 run_loop_->Run(); |
| 141 } | 146 } |
| 142 | 147 |
| 143 void DecodeSamples(const std::string& samples) { | 148 void DecodeSamples(const std::string& samples) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 181 } |
| 177 | 182 |
| 178 protected: | 183 protected: |
| 179 float* GenerateSamples(int random_seed, size_t size) { | 184 float* GenerateSamples(int random_seed, size_t size) { |
| 180 float* samples = static_cast<float*>(base::AlignedAlloc( | 185 float* samples = static_cast<float*>(base::AlignedAlloc( |
| 181 size * sizeof(float), media::AudioBus::kChannelAlignment)); | 186 size * sizeof(float), media::AudioBus::kChannelAlignment)); |
| 182 PopulateSamples(0x1337, size, samples); | 187 PopulateSamples(0x1337, size, samples); |
| 183 return samples; | 188 return samples; |
| 184 } | 189 } |
| 185 bool IsRecording() { | 190 bool IsRecording() { |
| 186 recorder_->FlushAudioLoopForTesting(); | 191 base::RunLoop().RunUntilIdle(); |
| 187 return recorder_->is_recording_; | 192 return recorder_->is_recording_; |
| 188 } | 193 } |
| 189 | 194 |
| 195 content::TestBrowserThreadBundle thread_bundle_; |
| 196 media::ScopedAudioManagerPtr audio_manager_; |
| 197 |
| 190 std::vector<float*> channel_data_; | 198 std::vector<float*> channel_data_; |
| 191 media::AudioParameters params_; | 199 media::AudioParameters params_; |
| 192 size_t total_samples_; | 200 size_t total_samples_; |
| 193 | 201 |
| 194 // Deleted by calling Finalize() on the object. | 202 // Deleted by calling Finalize() on the object. |
| 195 AudioRecorderImpl* recorder_; | 203 AudioRecorderImpl* recorder_; |
| 196 | 204 |
| 197 std::string received_samples_; | 205 std::string received_samples_; |
| 198 | 206 |
| 199 scoped_ptr<base::RunLoop> run_loop_; | 207 scoped_ptr<base::RunLoop> run_loop_; |
| 200 content::TestBrowserThreadBundle thread_bundle_; | |
| 201 }; | 208 }; |
| 202 | 209 |
| 203 | 210 |
| 204 // http://crbug.com/463854 | 211 // http://crbug.com/463854 |
| 205 #if defined(OS_MACOSX) | 212 #if defined(OS_MACOSX) |
| 206 #define MAYBE_BasicRecordAndStop DISABLED_BasicRecordAndStop | 213 #define MAYBE_BasicRecordAndStop DISABLED_BasicRecordAndStop |
| 207 #else | 214 #else |
| 208 #define MAYBE_BasicRecordAndStop BasicRecordAndStop | 215 #define MAYBE_BasicRecordAndStop BasicRecordAndStop |
| 209 #endif | 216 #endif |
| 210 | 217 |
| 211 TEST_F(AudioRecorderTest, MAYBE_BasicRecordAndStop) { | 218 TEST_F(AudioRecorderTest, MAYBE_BasicRecordAndStop) { |
| 212 CreateSimpleRecorder(); | 219 CreateSimpleRecorder(); |
| 213 | 220 |
| 214 recorder_->Record(); | 221 recorder_->Record(); |
| 215 EXPECT_TRUE(IsRecording()); | 222 EXPECT_TRUE(IsRecording()); |
| 216 recorder_->Stop(); | |
| 217 EXPECT_FALSE(IsRecording()); | |
| 218 recorder_->Record(); | |
| 219 | 223 |
| 220 EXPECT_TRUE(IsRecording()); | |
| 221 recorder_->Stop(); | |
| 222 EXPECT_FALSE(IsRecording()); | |
| 223 recorder_->Record(); | |
| 224 | |
| 225 EXPECT_TRUE(IsRecording()); | |
| 226 recorder_->Stop(); | 224 recorder_->Stop(); |
| 227 EXPECT_FALSE(IsRecording()); | 225 EXPECT_FALSE(IsRecording()); |
| 228 | 226 |
| 227 recorder_->Record(); |
| 228 EXPECT_TRUE(IsRecording()); |
| 229 |
| 230 recorder_->Stop(); |
| 231 EXPECT_FALSE(IsRecording()); |
| 232 |
| 233 recorder_->Record(); |
| 234 EXPECT_TRUE(IsRecording()); |
| 235 |
| 236 recorder_->Stop(); |
| 237 EXPECT_FALSE(IsRecording()); |
| 238 |
| 229 DeleteRecorder(); | 239 DeleteRecorder(); |
| 230 } | 240 } |
| 231 | 241 |
| 232 // http://crbug.com/460685 | 242 // http://crbug.com/460685 |
| 233 #if defined(OS_MACOSX) | 243 #if defined(OS_MACOSX) |
| 234 #define MAYBE_OutOfOrderRecordAndStopMultiple \ | 244 #define MAYBE_OutOfOrderRecordAndStopMultiple \ |
| 235 DISABLED_OutOfOrderRecordAndStopMultiple | 245 DISABLED_OutOfOrderRecordAndStopMultiple |
| 236 #else | 246 #else |
| 237 #define MAYBE_OutOfOrderRecordAndStopMultiple \ | 247 #define MAYBE_OutOfOrderRecordAndStopMultiple \ |
| 238 OutOfOrderRecordAndStopMultiple | 248 OutOfOrderRecordAndStopMultiple |
| (...skipping 23 matching lines...) Expand all Loading... |
| 262 CreateRecorder(kNumSamples); | 272 CreateRecorder(kNumSamples); |
| 263 | 273 |
| 264 RecordAndVerifySamples(); | 274 RecordAndVerifySamples(); |
| 265 | 275 |
| 266 DeleteRecorder(); | 276 DeleteRecorder(); |
| 267 } | 277 } |
| 268 | 278 |
| 269 // TODO(rkc): Add tests with recording different sample rates. | 279 // TODO(rkc): Add tests with recording different sample rates. |
| 270 | 280 |
| 271 } // namespace audio_modem | 281 } // namespace audio_modem |
| OLD | NEW |