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..57ba15852f548ce6ef7d00e1377bd8410273ff9c 100644 |
--- a/webrtc/test/fake_audio_device.cc |
+++ b/webrtc/test/fake_audio_device.cc |
@@ -11,50 +11,83 @@ |
#include "webrtc/test/fake_audio_device.h" |
#include <algorithm> |
+#include <cmath> |
+#include "webrtc/base/buffer.h" |
#include "webrtc/base/platform_thread.h" |
-#include "webrtc/modules/media_file/media_file_utility.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 { |
namespace test { |
-FakeAudioDevice::FakeAudioDevice(Clock* clock, |
- const std::string& filename, |
- float speed) |
+namespace { |
+ |
+const double kPi = std::acos(-1); |
+ |
+} // namespace |
+ |
+class FakeAudioDevice::SinusCapturer { |
+ public: |
+ SinusCapturer(AudioTransport* audio_callback, |
+ int frequency_in_hz, |
+ int sampling_frequency, |
+ 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.
|
+ : audio_callback_(audio_callback), |
+ normalized_frequency_((2 * kPi * frequency_in_hz) / sampling_frequency), |
+ sampling_frequency_(sampling_frequency), |
+ peak_to_peak_(peak_to_peak), |
+ sine_step_(0) {} |
+ |
+ // Capture |num_samples| of audio data, 2 bytes per sample. |
+ void Capture(size_t num_samples) { |
+ captured_audio_.SetSize(num_samples); |
+ // Get 10ms of audio. 2 bytes per sample. |
+ for (size_t i = 0; i < num_samples; ++i) { |
+ 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.
|
+ if (sine_step_ >= 2 * kPi) |
+ sine_step_ -= 2 * kPi; |
+ uint16_t sample = static_cast<uint16_t>((1.0 + std::sin(sine_step_)) / 2 * |
+ peak_to_peak_); |
+ captured_audio_[i] = sample; |
+ } |
+ |
+ uint32_t new_mic_level; |
+ RTC_CHECK_EQ(0, audio_callback_->RecordedDataIsAvailable( |
+ captured_audio_.data(), num_samples, 2, 1, |
+ sampling_frequency_, 0, 0, 0, false, new_mic_level)); |
+ } |
+ |
+ private: |
+ AudioTransport* const audio_callback_; |
+ const float normalized_frequency_; |
+ const uint32_t sampling_frequency_; |
+ const float peak_to_peak_; |
+ float sine_step_; |
+ rtc::BufferT<uint16_t> captured_audio_; |
+}; |
+ |
+FakeAudioDevice::FakeAudioDevice(Clock* clock, float speed) |
: audio_callback_(NULL), |
- capturing_(false), |
- captured_audio_(), |
+ rendering_(false), |
playout_buffer_(), |
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_)); |
+ thread_(FakeAudioDevice::Run, this, "FakeAudioDevice") { |
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; |
} |
FakeAudioDevice::~FakeAudioDevice() { |
- Stop(); |
- |
+ StopPlayout(); |
+ StopRecording(); |
thread_.Stop(); |
} |
int32_t FakeAudioDevice::Init() { |
- rtc::CritScope cs(&lock_); |
- if (file_utility_->InitPCMReading(*input_stream_.get()) != 0) |
- return -1; |
- |
- if (!tick_->StartTimer(true, 10 / speed_)) |
- return -1; |
+ RTC_CHECK(tick_->StartTimer(true, 10 / speed_)); |
thread_.Start(); |
thread_.SetPriority(rtc::kHighPriority); |
return 0; |
@@ -68,7 +101,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,36 +111,22 @@ int32_t FakeAudioDevice::PlayoutDelay(uint16_t* delay_ms) const { |
bool FakeAudioDevice::Recording() const { |
rtc::CritScope cs(&lock_); |
- return capturing_; |
+ 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.
|
} |
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); |
- uint32_t new_mic_level; |
- EXPECT_EQ(0, |
- audio_callback_->RecordedDataIsAvailable(captured_audio_, |
- num_samples, |
- 2, |
- 1, |
- kFrequencyHz, |
- 0, |
- 0, |
- 0, |
- false, |
- new_mic_level)); |
+ if (capturer_) { |
+ // Capture 10ms of audio. 2 bytes per sample. |
+ capturer_->Capture(kFrequencyHz / 100); |
+ } |
+ if (rendering_) { |
size_t samples_needed = kFrequencyHz / 100; |
int64_t now_ms = clock_.TimeInMilliseconds(); |
uint32_t time_since_last_playout_ms = now_ms - last_playout_ms_; |
@@ -119,28 +138,38 @@ void FakeAudioDevice::CaptureAudio() { |
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, kFrequencyHz, playout_buffer_, |
+ samples_out, &elapsed_time_ms, &ntp_time_ms)); |
} |
} |
tick_->Wait(WEBRTC_EVENT_INFINITE); |
} |
-void FakeAudioDevice::Start() { |
+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.
|
rtc::CritScope cs(&lock_); |
- capturing_ = true; |
+ rendering_ = true; |
+ return 0; |
} |
-void FakeAudioDevice::Stop() { |
+int32_t FakeAudioDevice::StopPlayout() { |
rtc::CritScope cs(&lock_); |
- capturing_ = false; |
+ rendering_ = false; |
+ return 0; |
} |
+ |
+void FakeAudioDevice::StartRecordingSine(int frequency_in_hz, |
+ uint16_t peak_to_peak) { |
+ rtc::CritScope cs(&lock_); |
+ capturer_.reset(new FakeAudioDevice::SinusCapturer( |
+ audio_callback_, frequency_in_hz, kFrequencyHz, peak_to_peak)); |
+} |
+ |
+int32_t FakeAudioDevice::StopRecording() { |
+ rtc::CritScope cs(&lock_); |
+ capturer_.reset(); |
+ return 0; |
+} |
+ |
} // namespace test |
} // namespace webrtc |