Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/test/fake_audio_device.h" | 11 #include "webrtc/test/fake_audio_device.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <cmath> | |
| 14 | 15 |
| 16 #include "webrtc/base/buffer.h" | |
| 15 #include "webrtc/base/platform_thread.h" | 17 #include "webrtc/base/platform_thread.h" |
| 16 #include "webrtc/modules/media_file/media_file_utility.h" | |
| 17 #include "webrtc/system_wrappers/include/clock.h" | 18 #include "webrtc/system_wrappers/include/clock.h" |
| 18 #include "webrtc/system_wrappers/include/event_wrapper.h" | 19 #include "webrtc/system_wrappers/include/event_wrapper.h" |
| 19 #include "webrtc/system_wrappers/include/file_wrapper.h" | 20 #include "webrtc/system_wrappers/include/file_wrapper.h" |
| 20 #include "webrtc/test/gtest.h" | |
| 21 | 21 |
| 22 namespace webrtc { | 22 namespace webrtc { |
| 23 namespace test { | 23 namespace test { |
| 24 | 24 |
| 25 FakeAudioDevice::FakeAudioDevice(Clock* clock, | 25 namespace { |
| 26 const std::string& filename, | 26 |
| 27 float speed) | 27 const double kPi = std::acos(-1); |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 class FakeAudioDevice::SinusCapturer { | |
| 32 public: | |
| 33 SinusCapturer(AudioTransport* audio_callback, | |
| 34 int frequency_in_hz, | |
| 35 int sampling_frequency, | |
| 36 uint16_t peak_to_peak) | |
|
peah-webrtc
2017/01/25 10:45:27
Do you really need to specify frequency_in_hz and
perkj_webrtc
2017/01/25 13:43:25
yes, now use pulsed noise as you suggested.
| |
| 37 : audio_callback_(audio_callback), | |
| 38 normalized_frequency_((2 * kPi * frequency_in_hz) / sampling_frequency), | |
| 39 sampling_frequency_(sampling_frequency), | |
| 40 peak_to_peak_(peak_to_peak), | |
| 41 sine_step_(0) {} | |
| 42 | |
| 43 // Capture |num_samples| of audio data, 2 bytes per sample. | |
| 44 void Capture(size_t num_samples) { | |
| 45 captured_audio_.SetSize(num_samples); | |
| 46 // Get 10ms of audio. 2 bytes per sample. | |
| 47 for (size_t i = 0; i < num_samples; ++i) { | |
| 48 sine_step_ += normalized_frequency_; | |
|
peah-webrtc
2017/01/25 10:45:27
As we discussed offline you can use a pulsed noise
perkj_webrtc
2017/01/25 13:43:25
Done.
| |
| 49 if (sine_step_ >= 2 * kPi) | |
| 50 sine_step_ -= 2 * kPi; | |
| 51 uint16_t sample = static_cast<uint16_t>((1.0 + std::sin(sine_step_)) / 2 * | |
| 52 peak_to_peak_); | |
| 53 captured_audio_[i] = sample; | |
| 54 } | |
| 55 | |
| 56 uint32_t new_mic_level; | |
| 57 RTC_CHECK_EQ(0, audio_callback_->RecordedDataIsAvailable( | |
| 58 captured_audio_.data(), num_samples, 2, 1, | |
| 59 sampling_frequency_, 0, 0, 0, false, new_mic_level)); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 AudioTransport* const audio_callback_; | |
| 64 const float normalized_frequency_; | |
| 65 const uint32_t sampling_frequency_; | |
| 66 const float peak_to_peak_; | |
| 67 float sine_step_; | |
| 68 rtc::BufferT<uint16_t> captured_audio_; | |
| 69 }; | |
| 70 | |
| 71 FakeAudioDevice::FakeAudioDevice(Clock* clock, float speed) | |
| 28 : audio_callback_(NULL), | 72 : audio_callback_(NULL), |
| 29 capturing_(false), | 73 rendering_(false), |
| 30 captured_audio_(), | |
| 31 playout_buffer_(), | 74 playout_buffer_(), |
| 32 speed_(speed), | 75 speed_(speed), |
| 33 last_playout_ms_(-1), | 76 last_playout_ms_(-1), |
| 34 clock_(clock, speed), | 77 clock_(clock, speed), |
| 35 tick_(EventTimerWrapper::Create()), | 78 tick_(EventTimerWrapper::Create()), |
| 36 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice"), | 79 thread_(FakeAudioDevice::Run, this, "FakeAudioDevice") { |
| 37 file_utility_(new ModuleFileUtility(0)), | |
| 38 input_stream_(FileWrapper::Create()) { | |
| 39 memset(captured_audio_, 0, sizeof(captured_audio_)); | |
| 40 memset(playout_buffer_, 0, sizeof(playout_buffer_)); | 80 memset(playout_buffer_, 0, sizeof(playout_buffer_)); |
| 41 // Open audio input file as read-only and looping. | |
| 42 EXPECT_TRUE(input_stream_->OpenFile(filename.c_str(), true)) << filename; | |
| 43 } | 81 } |
| 44 | 82 |
| 45 FakeAudioDevice::~FakeAudioDevice() { | 83 FakeAudioDevice::~FakeAudioDevice() { |
| 46 Stop(); | 84 StopPlayout(); |
| 47 | 85 StopRecording(); |
| 48 thread_.Stop(); | 86 thread_.Stop(); |
| 49 } | 87 } |
| 50 | 88 |
| 51 int32_t FakeAudioDevice::Init() { | 89 int32_t FakeAudioDevice::Init() { |
| 52 rtc::CritScope cs(&lock_); | 90 RTC_CHECK(tick_->StartTimer(true, 10 / speed_)); |
| 53 if (file_utility_->InitPCMReading(*input_stream_.get()) != 0) | |
| 54 return -1; | |
| 55 | |
| 56 if (!tick_->StartTimer(true, 10 / speed_)) | |
| 57 return -1; | |
| 58 thread_.Start(); | 91 thread_.Start(); |
| 59 thread_.SetPriority(rtc::kHighPriority); | 92 thread_.SetPriority(rtc::kHighPriority); |
| 60 return 0; | 93 return 0; |
| 61 } | 94 } |
| 62 | 95 |
| 63 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) { | 96 int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) { |
| 64 rtc::CritScope cs(&lock_); | 97 rtc::CritScope cs(&lock_); |
| 65 audio_callback_ = callback; | 98 audio_callback_ = callback; |
| 66 return 0; | 99 return 0; |
| 67 } | 100 } |
| 68 | 101 |
| 69 bool FakeAudioDevice::Playing() const { | 102 bool FakeAudioDevice::Playing() const { |
| 70 rtc::CritScope cs(&lock_); | 103 rtc::CritScope cs(&lock_); |
| 71 return capturing_; | 104 return rendering_; |
| 72 } | 105 } |
| 73 | 106 |
| 74 int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const { | 107 int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const { |
| 75 *delay_ms = 0; | 108 *delay_ms = 0; |
| 76 return 0; | 109 return 0; |
| 77 } | 110 } |
| 78 | 111 |
| 79 bool FakeAudioDevice::Recording() const { | 112 bool FakeAudioDevice::Recording() const { |
| 80 rtc::CritScope cs(&lock_); | 113 rtc::CritScope cs(&lock_); |
| 81 return capturing_; | 114 return capturer_ != nullptr; |
|
peah-webrtc
2017/01/25 10:45:27
Since capturer_ is a unique_ptr, you can return wh
perkj_webrtc
2017/01/25 13:43:25
Done.
| |
| 82 } | 115 } |
| 83 | 116 |
| 84 bool FakeAudioDevice::Run(void* obj) { | 117 bool FakeAudioDevice::Run(void* obj) { |
| 85 static_cast<FakeAudioDevice*>(obj)->CaptureAudio(); | 118 static_cast<FakeAudioDevice*>(obj)->ProcessAudio(); |
| 86 return true; | 119 return true; |
| 87 } | 120 } |
| 88 | 121 |
| 89 void FakeAudioDevice::CaptureAudio() { | 122 void FakeAudioDevice::ProcessAudio() { |
| 90 { | 123 { |
| 91 rtc::CritScope cs(&lock_); | 124 rtc::CritScope cs(&lock_); |
| 92 if (capturing_) { | 125 if (capturer_) { |
| 93 int bytes_read = file_utility_->ReadPCMData( | 126 // Capture 10ms of audio. 2 bytes per sample. |
| 94 *input_stream_.get(), captured_audio_, kBufferSizeBytes); | 127 capturer_->Capture(kFrequencyHz / 100); |
| 95 if (bytes_read <= 0) | 128 } |
| 96 return; | 129 if (rendering_) { |
| 97 // 2 bytes per sample. | |
| 98 size_t num_samples = static_cast<size_t>(bytes_read / 2); | |
| 99 uint32_t new_mic_level; | |
| 100 EXPECT_EQ(0, | |
| 101 audio_callback_->RecordedDataIsAvailable(captured_audio_, | |
| 102 num_samples, | |
| 103 2, | |
| 104 1, | |
| 105 kFrequencyHz, | |
| 106 0, | |
| 107 0, | |
| 108 0, | |
| 109 false, | |
| 110 new_mic_level)); | |
| 111 size_t samples_needed = kFrequencyHz / 100; | 130 size_t samples_needed = kFrequencyHz / 100; |
| 112 int64_t now_ms = clock_.TimeInMilliseconds(); | 131 int64_t now_ms = clock_.TimeInMilliseconds(); |
| 113 uint32_t time_since_last_playout_ms = now_ms - last_playout_ms_; | 132 uint32_t time_since_last_playout_ms = now_ms - last_playout_ms_; |
| 114 if (last_playout_ms_ > 0 && time_since_last_playout_ms > 0) { | 133 if (last_playout_ms_ > 0 && time_since_last_playout_ms > 0) { |
| 115 samples_needed = std::min( | 134 samples_needed = std::min( |
| 116 static_cast<size_t>(kFrequencyHz / time_since_last_playout_ms), | 135 static_cast<size_t>(kFrequencyHz / time_since_last_playout_ms), |
| 117 kBufferSizeBytes / 2); | 136 kBufferSizeBytes / 2); |
| 118 } | 137 } |
| 119 size_t samples_out = 0; | 138 size_t samples_out = 0; |
| 120 int64_t elapsed_time_ms = -1; | 139 int64_t elapsed_time_ms = -1; |
| 121 int64_t ntp_time_ms = -1; | 140 int64_t ntp_time_ms = -1; |
| 122 EXPECT_EQ(0, | 141 RTC_CHECK_EQ(0, audio_callback_->NeedMorePlayData( |
| 123 audio_callback_->NeedMorePlayData(samples_needed, | 142 samples_needed, 2, 1, kFrequencyHz, playout_buffer_, |
| 124 2, | 143 samples_out, &elapsed_time_ms, &ntp_time_ms)); |
| 125 1, | |
| 126 kFrequencyHz, | |
| 127 playout_buffer_, | |
| 128 samples_out, | |
| 129 &elapsed_time_ms, | |
| 130 &ntp_time_ms)); | |
| 131 } | 144 } |
| 132 } | 145 } |
| 133 tick_->Wait(WEBRTC_EVENT_INFINITE); | 146 tick_->Wait(WEBRTC_EVENT_INFINITE); |
| 134 } | 147 } |
| 135 | 148 |
| 136 void FakeAudioDevice::Start() { | 149 int32_t FakeAudioDevice::StartPlayout() { |
|
peah-webrtc
2017/01/25 10:45:28
Please order the methods in the same order as they
perkj_webrtc
2017/01/25 13:43:25
Done.
| |
| 137 rtc::CritScope cs(&lock_); | 150 rtc::CritScope cs(&lock_); |
| 138 capturing_ = true; | 151 rendering_ = true; |
| 152 return 0; | |
| 139 } | 153 } |
| 140 | 154 |
| 141 void FakeAudioDevice::Stop() { | 155 int32_t FakeAudioDevice::StopPlayout() { |
| 142 rtc::CritScope cs(&lock_); | 156 rtc::CritScope cs(&lock_); |
| 143 capturing_ = false; | 157 rendering_ = false; |
| 158 return 0; | |
| 144 } | 159 } |
| 160 | |
| 161 void FakeAudioDevice::StartRecordingSine(int frequency_in_hz, | |
| 162 uint16_t peak_to_peak) { | |
| 163 rtc::CritScope cs(&lock_); | |
| 164 capturer_.reset(new FakeAudioDevice::SinusCapturer( | |
| 165 audio_callback_, frequency_in_hz, kFrequencyHz, peak_to_peak)); | |
| 166 } | |
| 167 | |
| 168 int32_t FakeAudioDevice::StopRecording() { | |
| 169 rtc::CritScope cs(&lock_); | |
| 170 capturer_.reset(); | |
| 171 return 0; | |
| 172 } | |
| 173 | |
| 145 } // namespace test | 174 } // namespace test |
| 146 } // namespace webrtc | 175 } // namespace webrtc |
| OLD | NEW |