| 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()); |
| 87 } | 88 } |
| 88 | 89 |
| 89 ~AudioRecorderTest() override { | 90 ~AudioRecorderTest() override { |
| 90 DeleteRecorder(); | 91 DeleteRecorder(); |
| 91 for (size_t i = 0; i < channel_data_.size(); ++i) | 92 for (size_t i = 0; i < channel_data_.size(); ++i) |
| 92 base::AlignedFree(channel_data_[i]); | 93 base::AlignedFree(channel_data_[i]); |
| 93 } | 94 } |
| 94 | 95 |
| 95 void CreateSimpleRecorder() { | 96 void CreateSimpleRecorder() { |
| 96 // If we have input devices, we'll create a recorder which uses a real | 97 // If we have input devices, we'll create a recorder which uses a real |
| (...skipping 27 matching lines...) Expand all Loading... |
| 124 recorder_->set_params_for_testing(new media::AudioParameters(params_)); | 125 recorder_->set_params_for_testing(new media::AudioParameters(params_)); |
| 125 recorder_->Initialize( | 126 recorder_->Initialize( |
| 126 base::Bind(&AudioRecorderTest::DecodeSamples, base::Unretained(this))); | 127 base::Bind(&AudioRecorderTest::DecodeSamples, base::Unretained(this))); |
| 127 } | 128 } |
| 128 | 129 |
| 129 void DeleteRecorder() { | 130 void DeleteRecorder() { |
| 130 if (!recorder_) | 131 if (!recorder_) |
| 131 return; | 132 return; |
| 132 recorder_->Finalize(); | 133 recorder_->Finalize(); |
| 133 recorder_ = nullptr; | 134 recorder_ = nullptr; |
| 135 base::RunLoop().RunUntilIdle(); |
| 134 } | 136 } |
| 135 | 137 |
| 136 void RecordAndVerifySamples() { | 138 void RecordAndVerifySamples() { |
| 137 received_samples_.clear(); | 139 received_samples_.clear(); |
| 138 run_loop_.reset(new base::RunLoop()); | 140 run_loop_.reset(new base::RunLoop()); |
| 139 recorder_->Record(); | 141 recorder_->Record(); |
| 140 run_loop_->Run(); | 142 run_loop_->Run(); |
| 141 } | 143 } |
| 142 | 144 |
| 143 void DecodeSamples(const std::string& samples) { | 145 void DecodeSamples(const std::string& samples) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 178 } |
| 177 | 179 |
| 178 protected: | 180 protected: |
| 179 float* GenerateSamples(int random_seed, size_t size) { | 181 float* GenerateSamples(int random_seed, size_t size) { |
| 180 float* samples = static_cast<float*>(base::AlignedAlloc( | 182 float* samples = static_cast<float*>(base::AlignedAlloc( |
| 181 size * sizeof(float), media::AudioBus::kChannelAlignment)); | 183 size * sizeof(float), media::AudioBus::kChannelAlignment)); |
| 182 PopulateSamples(0x1337, size, samples); | 184 PopulateSamples(0x1337, size, samples); |
| 183 return samples; | 185 return samples; |
| 184 } | 186 } |
| 185 bool IsRecording() { | 187 bool IsRecording() { |
| 186 recorder_->FlushAudioLoopForTesting(); | 188 base::RunLoop().RunUntilIdle(); |
| 187 return recorder_->is_recording_; | 189 return recorder_->is_recording_; |
| 188 } | 190 } |
| 189 | 191 |
| 192 content::TestBrowserThreadBundle thread_bundle_; |
| 193 media::ScopedAudioManagerPtr audio_manager_; |
| 194 |
| 190 std::vector<float*> channel_data_; | 195 std::vector<float*> channel_data_; |
| 191 media::AudioParameters params_; | 196 media::AudioParameters params_; |
| 192 size_t total_samples_; | 197 size_t total_samples_; |
| 193 | 198 |
| 194 // Deleted by calling Finalize() on the object. | 199 // Deleted by calling Finalize() on the object. |
| 195 AudioRecorderImpl* recorder_; | 200 AudioRecorderImpl* recorder_; |
| 196 | 201 |
| 197 std::string received_samples_; | 202 std::string received_samples_; |
| 198 | 203 |
| 199 scoped_ptr<base::RunLoop> run_loop_; | 204 scoped_ptr<base::RunLoop> run_loop_; |
| 200 content::TestBrowserThreadBundle thread_bundle_; | |
| 201 }; | 205 }; |
| 202 | 206 |
| 203 | 207 |
| 204 // http://crbug.com/463854 | 208 // http://crbug.com/463854 |
| 205 #if defined(OS_MACOSX) | 209 #if defined(OS_MACOSX) |
| 206 #define MAYBE_BasicRecordAndStop DISABLED_BasicRecordAndStop | 210 #define MAYBE_BasicRecordAndStop DISABLED_BasicRecordAndStop |
| 207 #else | 211 #else |
| 208 #define MAYBE_BasicRecordAndStop BasicRecordAndStop | 212 #define MAYBE_BasicRecordAndStop BasicRecordAndStop |
| 209 #endif | 213 #endif |
| 210 | 214 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 CreateRecorder(kNumSamples); | 266 CreateRecorder(kNumSamples); |
| 263 | 267 |
| 264 RecordAndVerifySamples(); | 268 RecordAndVerifySamples(); |
| 265 | 269 |
| 266 DeleteRecorder(); | 270 DeleteRecorder(); |
| 267 } | 271 } |
| 268 | 272 |
| 269 // TODO(rkc): Add tests with recording different sample rates. | 273 // TODO(rkc): Add tests with recording different sample rates. |
| 270 | 274 |
| 271 } // namespace audio_modem | 275 } // namespace audio_modem |
| OLD | NEW |