Index: webrtc/test/fake_audio_device.cc |
diff --git a/webrtc/test/fake_audio_device.cc b/webrtc/test/fake_audio_device.cc |
index 9e5e95fb0dbdc229b61dceb7dd182b404c4edc91..90592f91aa2d8623933c7aff903a9b7e0fa9fa98 100644 |
--- a/webrtc/test/fake_audio_device.cc |
+++ b/webrtc/test/fake_audio_device.cc |
@@ -12,49 +12,90 @@ |
#include <algorithm> |
-#include "webrtc/base/platform_thread.h" |
-#include "webrtc/modules/media_file/media_file_utility.h" |
+#include "webrtc/base/array_view.h" |
+#include "webrtc/base/random.h" |
#include "webrtc/system_wrappers/include/clock.h" |
#include "webrtc/system_wrappers/include/event_wrapper.h" |
-#include "webrtc/system_wrappers/include/file_wrapper.h" |
-#include "webrtc/test/gtest.h" |
namespace webrtc { |
peah-webrtc
2017/01/26 13:20:11
If you here add
namespace {
constexpr size_t kFra
perkj_webrtc
2017/01/29 13:25:19
ok, but will use int https://google.github.io/styl
|
namespace test { |
+class FakeAudioDevice::PulsedNoiseCapturer { |
+ public: |
+ PulsedNoiseCapturer(size_t num_samples, int16_t max_amplitude) |
+ : fill_with_zero_(false), |
+ random_generator_(1), |
+ max_amplitude_(max_amplitude), |
+ random_audio_(num_samples), |
+ null_audio_(num_samples, 0) { |
+ RTC_DCHECK_GT(max_amplitude, 0); |
+ } |
+ |
+ rtc::ArrayView<const int16_t> Capture() { |
+ fill_with_zero_ = !fill_with_zero_; |
+ if (!fill_with_zero_) { |
+ std::generate(random_audio_.begin(), random_audio_.end(), [&]() { |
+ return random_generator_.Rand(-max_amplitude_, max_amplitude_); |
+ }); |
+ } |
+ return fill_with_zero_ ? null_audio_ : random_audio_; |
+ } |
+ |
+ private: |
+ bool fill_with_zero_; |
+ webrtc::Random random_generator_; |
+ const int16_t max_amplitude_; |
+ std::vector<int16_t> random_audio_; |
+ std::vector<int16_t> null_audio_; |
peah-webrtc
2017/01/26 13:20:11
You probably should rename this to silent_audio_,
perkj_webrtc
2017/01/29 13:25:19
Done.
|
+}; |
+ |
FakeAudioDevice::FakeAudioDevice(Clock* clock, |
- const std::string& filename, |
- float speed) |
- : audio_callback_(NULL), |
- capturing_(false), |
- captured_audio_(), |
- playout_buffer_(), |
+ float speed, |
+ int sampling_frequency_in_hz) |
+ : sampling_frequency_in_hz_(sampling_frequency_in_hz), |
+ audio_callback_(NULL), |
+ rendering_(false), |
+ // Assuming 10ms audio packets. |
peah-webrtc
2017/01/26 13:20:11
This should be documented in the header file as it
perkj_webrtc
2017/01/29 13:25:20
Done.
|
+ playout_buffer_(sampling_frequency_in_hz_ / 100, 0), |
peah-webrtc
2017/01/26 13:20:11
playout_buffer_(rtc::CheckedDivExact(sampling_freq
perkj_webrtc
2017/01/29 13:25:20
Done.
|
speed_(speed), |
last_playout_ms_(-1), |
clock_(clock, speed), |
tick_(EventTimerWrapper::Create()), |
- thread_(FakeAudioDevice::Run, this, "FakeAudioDevice"), |
- file_utility_(new ModuleFileUtility(0)), |
- input_stream_(FileWrapper::Create()) { |
- memset(captured_audio_, 0, sizeof(captured_audio_)); |
- memset(playout_buffer_, 0, sizeof(playout_buffer_)); |
- // Open audio input file as read-only and looping. |
- EXPECT_TRUE(input_stream_->OpenFile(filename.c_str(), true)) << filename; |
-} |
+ thread_(FakeAudioDevice::Run, this, "FakeAudioDevice") {} |
peah-webrtc
2017/01/26 13:20:11
Please add
RTC_DCHECK(clock);
(because clock sho
perkj_webrtc
2017/01/29 13:25:19
now only used by the drifting clock and that check
|
FakeAudioDevice::~FakeAudioDevice() { |
- Stop(); |
- |
+ StopPlayout(); |
+ StopRecording(); |
thread_.Stop(); |
} |
-int32_t FakeAudioDevice::Init() { |
+int32_t FakeAudioDevice::StartPlayout() { |
+ rtc::CritScope cs(&lock_); |
+ rendering_ = true; |
+ return 0; |
+} |
+ |
+int32_t FakeAudioDevice::StopPlayout() { |
+ rtc::CritScope cs(&lock_); |
+ rendering_ = false; |
+ return 0; |
+} |
+ |
+void FakeAudioDevice::StartRecordingPulsedNoise(int16_t max_amplitude) { |
rtc::CritScope cs(&lock_); |
- if (file_utility_->InitPCMReading(*input_stream_.get()) != 0) |
- return -1; |
+ // Assuming 10ms audio packets. |
+ capturer_.reset(new FakeAudioDevice::PulsedNoiseCapturer( |
+ sampling_frequency_in_hz_ / 100, max_amplitude)); |
peah-webrtc
2017/01/26 13:20:11
rtc::CheckedDivExact(sampling_frequency_in_hz_,
kF
perkj_webrtc
2017/01/29 13:25:20
Done.
|
+} |
+ |
+int32_t FakeAudioDevice::StopRecording() { |
+ rtc::CritScope cs(&lock_); |
+ capturer_.reset(); |
+ return 0; |
+} |
- if (!tick_->StartTimer(true, 10 / speed_)) |
- return -1; |
+int32_t FakeAudioDevice::Init() { |
+ RTC_CHECK(tick_->StartTimer(true, 10 / speed_)); |
peah-webrtc
2017/01/26 13:20:11
RTC_CHECK(tick_->StartTimer(true, kFrameLengthMs /
perkj_webrtc
2017/01/29 13:25:19
Done.
|
thread_.Start(); |
thread_.SetPriority(rtc::kHighPriority); |
return 0; |
@@ -68,7 +109,7 @@ int32_t FakeAudioDevice::RegisterAudioCallback(AudioTransport* callback) { |
bool FakeAudioDevice::Playing() const { |
rtc::CritScope cs(&lock_); |
- return capturing_; |
+ return rendering_; |
} |
int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const { |
@@ -78,69 +119,49 @@ int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const { |
bool FakeAudioDevice::Recording() const { |
rtc::CritScope cs(&lock_); |
- return capturing_; |
+ return !!capturer_; |
} |
bool FakeAudioDevice::Run(void* obj) { |
- static_cast<FakeAudioDevice*>(obj)->CaptureAudio(); |
+ static_cast<FakeAudioDevice*>(obj)->ProcessAudio(); |
return true; |
} |
-void FakeAudioDevice::CaptureAudio() { |
+void FakeAudioDevice::ProcessAudio() { |
{ |
rtc::CritScope cs(&lock_); |
- if (capturing_) { |
- int bytes_read = file_utility_->ReadPCMData( |
- *input_stream_.get(), captured_audio_, kBufferSizeBytes); |
- if (bytes_read <= 0) |
- return; |
- // 2 bytes per sample. |
- size_t num_samples = static_cast<size_t>(bytes_read / 2); |
+ if (capturer_) { |
+ // Capture 10ms of audio. 2 bytes per sample. |
+ rtc::ArrayView<const int16_t> audio_data = capturer_->Capture(); |
uint32_t new_mic_level; |
peah-webrtc
2017/01/26 13:25:18
Please initialize new_mic_level.
perkj_webrtc
2017/01/29 13:25:20
Done.
|
- EXPECT_EQ(0, |
- audio_callback_->RecordedDataIsAvailable(captured_audio_, |
- num_samples, |
- 2, |
- 1, |
- kFrequencyHz, |
- 0, |
- 0, |
- 0, |
- false, |
- new_mic_level)); |
- size_t samples_needed = kFrequencyHz / 100; |
+ RTC_CHECK_EQ( |
+ 0, audio_callback_->RecordedDataIsAvailable( |
+ audio_data.data(), audio_data.size(), 2, 1, |
+ sampling_frequency_in_hz_, 0, 0, 0, false, new_mic_level)); |
+ } |
+ if (rendering_) { |
+ // Assuming 10ms audio packet size. |
+ size_t samples_needed = sampling_frequency_in_hz_ / 100; |
peah-webrtc
2017/01/26 13:20:12
With the code construct below, you can skip the co
perkj_webrtc
2017/01/29 13:25:20
I removed all this weird stuff since the clock has
|
int64_t now_ms = clock_.TimeInMilliseconds(); |
uint32_t time_since_last_playout_ms = now_ms - last_playout_ms_; |
peah-webrtc
2017/01/26 13:20:12
I don't see you ever updating last_playout_ms_. Ho
perkj_webrtc
2017/01/29 13:25:19
Acknowledged.
|
if (last_playout_ms_ > 0 && time_since_last_playout_ms > 0) { |
peah-webrtc
2017/01/26 13:20:11
It would actually be fine to initialize last_play
perkj_webrtc
2017/01/29 13:25:20
Acknowledged.
|
- samples_needed = std::min( |
- static_cast<size_t>(kFrequencyHz / time_since_last_playout_ms), |
- kBufferSizeBytes / 2); |
+ samples_needed = |
+ std::min(static_cast<size_t>(sampling_frequency_in_hz_ / |
peah-webrtc
2017/01/26 13:20:11
If you change sampling_frequency_in_hz_ to be size
perkj_webrtc
2017/01/29 13:25:19
NeedMorePlayData actually use size_t but style gui
peah-webrtc
2017/01/30 06:45:08
I think the style guide leaves some room for inter
|
+ time_since_last_playout_ms), |
+ playout_buffer_.size()); |
} |
size_t samples_out = 0; |
int64_t elapsed_time_ms = -1; |
int64_t ntp_time_ms = -1; |
- EXPECT_EQ(0, |
- audio_callback_->NeedMorePlayData(samples_needed, |
- 2, |
- 1, |
- kFrequencyHz, |
- playout_buffer_, |
- samples_out, |
- &elapsed_time_ms, |
- &ntp_time_ms)); |
+ RTC_CHECK_EQ(0, audio_callback_->NeedMorePlayData( |
+ samples_needed, 2, 1, sampling_frequency_in_hz_, |
+ playout_buffer_.data(), samples_out, &elapsed_time_ms, |
+ &ntp_time_ms)); |
} |
} |
tick_->Wait(WEBRTC_EVENT_INFINITE); |
} |
-void FakeAudioDevice::Start() { |
- rtc::CritScope cs(&lock_); |
- capturing_ = true; |
-} |
-void FakeAudioDevice::Stop() { |
- rtc::CritScope cs(&lock_); |
- capturing_ = false; |
-} |
} // namespace test |
} // namespace webrtc |