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