| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/copresence/mediums/audio/audio_recorder.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/aligned_memory.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "components/copresence/mediums/audio/audio_recorder_impl.h" | |
| 11 #include "components/copresence/public/copresence_constants.h" | |
| 12 #include "components/copresence/test/audio_test_support.h" | |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | |
| 14 #include "media/audio/audio_manager.h" | |
| 15 #include "media/audio/audio_manager_base.h" | |
| 16 #include "media/base/audio_bus.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 class TestAudioInputStream : public media::AudioInputStream { | |
| 22 public: | |
| 23 TestAudioInputStream(const media::AudioParameters& params, | |
| 24 const std::vector<float*> channel_data, | |
| 25 size_t samples) | |
| 26 : callback_(nullptr), params_(params) { | |
| 27 buffer_ = media::AudioBus::CreateWrapper(2); | |
| 28 for (size_t i = 0; i < channel_data.size(); ++i) | |
| 29 buffer_->SetChannelData(i, channel_data[i]); | |
| 30 buffer_->set_frames(samples); | |
| 31 } | |
| 32 | |
| 33 ~TestAudioInputStream() override {} | |
| 34 | |
| 35 bool Open() override { return true; } | |
| 36 void Start(AudioInputCallback* callback) override { | |
| 37 DCHECK(callback); | |
| 38 callback_ = callback; | |
| 39 media::AudioManager::Get()->GetTaskRunner()->PostTask( | |
| 40 FROM_HERE, | |
| 41 base::Bind(&TestAudioInputStream::SimulateRecording, | |
| 42 base::Unretained(this))); | |
| 43 } | |
| 44 void Stop() override {} | |
| 45 void Close() override {} | |
| 46 double GetMaxVolume() override { return 1.0; } | |
| 47 void SetVolume(double volume) override {} | |
| 48 double GetVolume() override { return 1.0; } | |
| 49 bool SetAutomaticGainControl(bool enabled) override { return false; } | |
| 50 bool GetAutomaticGainControl() override { return true; } | |
| 51 bool IsMuted() override { return false; } | |
| 52 | |
| 53 private: | |
| 54 void SimulateRecording() { | |
| 55 const int fpb = params_.frames_per_buffer(); | |
| 56 for (int i = 0; i < buffer_->frames() / fpb; ++i) { | |
| 57 scoped_ptr<media::AudioBus> source = media::AudioBus::Create(2, fpb); | |
| 58 buffer_->CopyPartialFramesTo(i * fpb, fpb, 0, source.get()); | |
| 59 callback_->OnData(this, source.get(), fpb, 1.0); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 AudioInputCallback* callback_; | |
| 64 media::AudioParameters params_; | |
| 65 scoped_ptr<media::AudioBus> buffer_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(TestAudioInputStream); | |
| 68 }; | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 namespace copresence { | |
| 73 | |
| 74 class AudioRecorderTest : public testing::Test { | |
| 75 public: | |
| 76 AudioRecorderTest() : total_samples_(0), recorder_(nullptr) { | |
| 77 if (!media::AudioManager::Get()) | |
| 78 media::AudioManager::CreateForTesting(); | |
| 79 } | |
| 80 | |
| 81 ~AudioRecorderTest() override { | |
| 82 DeleteRecorder(); | |
| 83 for (size_t i = 0; i < channel_data_.size(); ++i) | |
| 84 base::AlignedFree(channel_data_[i]); | |
| 85 } | |
| 86 | |
| 87 void CreateSimpleRecorder() { | |
| 88 DeleteRecorder(); | |
| 89 recorder_ = new AudioRecorderImpl(); | |
| 90 recorder_->Initialize( | |
| 91 base::Bind(&AudioRecorderTest::DecodeSamples, base::Unretained(this))); | |
| 92 } | |
| 93 | |
| 94 void CreateRecorder(size_t channels, | |
| 95 size_t sample_rate, | |
| 96 size_t bits_per_sample, | |
| 97 size_t samples) { | |
| 98 DeleteRecorder(); | |
| 99 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 100 kDefaultChannelLayout, | |
| 101 channels, | |
| 102 sample_rate, | |
| 103 bits_per_sample, | |
| 104 4096); | |
| 105 | |
| 106 channel_data_.clear(); | |
| 107 channel_data_.push_back(GenerateSamples(0x1337, samples)); | |
| 108 channel_data_.push_back(GenerateSamples(0x7331, samples)); | |
| 109 | |
| 110 total_samples_ = samples; | |
| 111 | |
| 112 recorder_ = new AudioRecorderImpl(); | |
| 113 recorder_->set_input_stream_for_testing( | |
| 114 new TestAudioInputStream(params_, channel_data_, samples)); | |
| 115 recorder_->set_params_for_testing(new media::AudioParameters(params_)); | |
| 116 recorder_->Initialize( | |
| 117 base::Bind(&AudioRecorderTest::DecodeSamples, base::Unretained(this))); | |
| 118 } | |
| 119 | |
| 120 void DeleteRecorder() { | |
| 121 if (!recorder_) | |
| 122 return; | |
| 123 recorder_->Finalize(); | |
| 124 recorder_ = nullptr; | |
| 125 } | |
| 126 | |
| 127 void RecordAndVerifySamples() { | |
| 128 received_samples_.clear(); | |
| 129 run_loop_.reset(new base::RunLoop()); | |
| 130 recorder_->Record(); | |
| 131 run_loop_->Run(); | |
| 132 } | |
| 133 | |
| 134 void DecodeSamples(const std::string& samples) { | |
| 135 received_samples_ += samples; | |
| 136 // We expect one less decode than our total samples would ideally have | |
| 137 // triggered since we process data in 4k chunks. So our sample processing | |
| 138 // will never rarely be perfectly aligned with 0.5s worth of samples, hence | |
| 139 // we will almost always run with a buffer of leftover samples that will | |
| 140 // not get sent to this callback since the recorder will be waiting for | |
| 141 // more data. | |
| 142 const size_t decode_buffer = params_.sample_rate() / 2; // 0.5s | |
| 143 const size_t expected_samples = | |
| 144 (total_samples_ / decode_buffer - 1) * decode_buffer; | |
| 145 const size_t expected_samples_size = | |
| 146 expected_samples * sizeof(float) * params_.channels(); | |
| 147 if (received_samples_.size() == expected_samples_size) { | |
| 148 VerifySamples(); | |
| 149 run_loop_->Quit(); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 void VerifySamples() { | |
| 154 int differences = 0; | |
| 155 | |
| 156 float* buffer_view = | |
| 157 reinterpret_cast<float*>(string_as_array(&received_samples_)); | |
| 158 const int channels = params_.channels(); | |
| 159 const int frames = | |
| 160 received_samples_.size() / sizeof(float) / params_.channels(); | |
| 161 for (int ch = 0; ch < channels; ++ch) { | |
| 162 for (int si = 0, di = ch; si < frames; ++si, di += channels) | |
| 163 differences += (buffer_view[di] != channel_data_[ch][si]); | |
| 164 } | |
| 165 | |
| 166 ASSERT_EQ(0, differences); | |
| 167 } | |
| 168 | |
| 169 protected: | |
| 170 float* GenerateSamples(int random_seed, size_t size) { | |
| 171 float* samples = static_cast<float*>(base::AlignedAlloc( | |
| 172 size * sizeof(float), media::AudioBus::kChannelAlignment)); | |
| 173 PopulateSamples(0x1337, size, samples); | |
| 174 return samples; | |
| 175 } | |
| 176 bool IsRecording() { | |
| 177 recorder_->FlushAudioLoopForTesting(); | |
| 178 return recorder_->is_recording_; | |
| 179 } | |
| 180 | |
| 181 std::vector<float*> channel_data_; | |
| 182 media::AudioParameters params_; | |
| 183 size_t total_samples_; | |
| 184 | |
| 185 // Deleted by calling Finalize() on the object. | |
| 186 AudioRecorderImpl* recorder_; | |
| 187 | |
| 188 std::string received_samples_; | |
| 189 | |
| 190 scoped_ptr<base::RunLoop> run_loop_; | |
| 191 content::TestBrowserThreadBundle thread_bundle_; | |
| 192 }; | |
| 193 | |
| 194 // TODO(rkc): These tests are broken on all platforms. | |
| 195 // On Windows and Mac, we cannot use non-OS params. The tests need to be | |
| 196 // rewritten to use the params provided to us by the audio manager | |
| 197 // rather than setting our own params. | |
| 198 // On Linux, there is a memory leak in the audio code during initialization. | |
| 199 #define MAYBE_BasicRecordAndStop DISABLED_BasicRecordAndStop | |
| 200 #define MAYBE_OutOfOrderRecordAndStopMultiple DISABLED_OutOfOrderRecordAndStopMu
ltiple | |
| 201 #define MAYBE_RecordingEndToEnd DISABLED_RecordingEndToEnd | |
| 202 | |
| 203 TEST_F(AudioRecorderTest, MAYBE_BasicRecordAndStop) { | |
| 204 CreateSimpleRecorder(); | |
| 205 | |
| 206 recorder_->Record(); | |
| 207 EXPECT_TRUE(IsRecording()); | |
| 208 recorder_->Stop(); | |
| 209 EXPECT_FALSE(IsRecording()); | |
| 210 recorder_->Record(); | |
| 211 | |
| 212 EXPECT_TRUE(IsRecording()); | |
| 213 recorder_->Stop(); | |
| 214 EXPECT_FALSE(IsRecording()); | |
| 215 recorder_->Record(); | |
| 216 | |
| 217 EXPECT_TRUE(IsRecording()); | |
| 218 recorder_->Stop(); | |
| 219 EXPECT_FALSE(IsRecording()); | |
| 220 | |
| 221 DeleteRecorder(); | |
| 222 } | |
| 223 | |
| 224 TEST_F(AudioRecorderTest, MAYBE_OutOfOrderRecordAndStopMultiple) { | |
| 225 CreateSimpleRecorder(); | |
| 226 | |
| 227 recorder_->Stop(); | |
| 228 recorder_->Stop(); | |
| 229 recorder_->Stop(); | |
| 230 EXPECT_FALSE(IsRecording()); | |
| 231 | |
| 232 recorder_->Record(); | |
| 233 recorder_->Record(); | |
| 234 EXPECT_TRUE(IsRecording()); | |
| 235 | |
| 236 recorder_->Stop(); | |
| 237 recorder_->Stop(); | |
| 238 EXPECT_FALSE(IsRecording()); | |
| 239 | |
| 240 DeleteRecorder(); | |
| 241 } | |
| 242 | |
| 243 TEST_F(AudioRecorderTest, MAYBE_RecordingEndToEnd) { | |
| 244 const int kNumSamples = 48000 * 3; | |
| 245 CreateRecorder( | |
| 246 kDefaultChannels, kDefaultSampleRate, kDefaultBitsPerSample, kNumSamples); | |
| 247 | |
| 248 RecordAndVerifySamples(); | |
| 249 | |
| 250 DeleteRecorder(); | |
| 251 } | |
| 252 | |
| 253 // TODO(rkc): Add tests with recording different sample rates. | |
| 254 | |
| 255 } // namespace copresence | |
| OLD | NEW |