Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(612)

Unified Diff: webrtc/test/fake_audio_device.cc

Issue 2694203002: Low-bandwidth audio testing (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/test/fake_audio_device.h ('k') | webrtc/test/fake_audio_device_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/test/fake_audio_device.cc
diff --git a/webrtc/test/fake_audio_device.cc b/webrtc/test/fake_audio_device.cc
index 7a3f0dd740e17e36128048a8625be3b5fed7b187..91d5978b3fccf0f08c116a7a7c796d0634d713e6 100644
--- a/webrtc/test/fake_audio_device.cc
+++ b/webrtc/test/fake_audio_device.cc
@@ -111,6 +111,69 @@ class WavFileWriter final : public test::FakeAudioDevice::Renderer {
WavWriter wav_writer_;
};
+class BoundedWavFileWriter : public test::FakeAudioDevice::Renderer {
+ public:
+ BoundedWavFileWriter(std::string filename, int sampling_frequency_in_hz)
+ : sampling_frequency_in_hz_(sampling_frequency_in_hz),
+ wav_writer_(filename, sampling_frequency_in_hz, 1),
+ silent_audio_(test::FakeAudioDevice::SamplesPerFrame(
+ sampling_frequency_in_hz), 0),
+ started_writing_(false),
+ trailing_zeros_(0) {}
+
+ int SamplingFrequency() const override {
+ return sampling_frequency_in_hz_;
+ }
+
+ bool Render(rtc::ArrayView<const int16_t> data) override {
+ const int16_t kAmplitudeThreshold = 5;
+
+ const int16_t* begin = data.begin();
+ const int16_t* end = data.end();
+ if (!started_writing_) {
+ // Cut off silence at the beginning.
+ while (begin < end) {
+ if (std::abs(*begin) > kAmplitudeThreshold) {
+ started_writing_ = true;
+ break;
+ }
+ ++begin;
+ }
+ }
+ if (started_writing_) {
+ // Cut off silence at the end.
+ while (begin < end) {
+ if (*(end - 1) != 0) {
+ break;
+ }
+ --end;
+ }
+ if (begin < end) {
+ // If it turns out that the silence was not final, need to write all the
+ // skipped zeros and continue writing audio.
+ while (trailing_zeros_ > 0) {
+ const size_t zeros_to_write = std::min(trailing_zeros_,
+ silent_audio_.size());
+ wav_writer_.WriteSamples(silent_audio_.data(), zeros_to_write);
+ trailing_zeros_ -= zeros_to_write;
+ }
+ wav_writer_.WriteSamples(begin, end - begin);
+ }
+ // Save the number of zeros we skipped in case this needs to be restored.
+ trailing_zeros_ += data.end() - end;
+ }
+ return true;
+ }
+
+ private:
+ int sampling_frequency_in_hz_;
+ WavWriter wav_writer_;
+ std::vector<int16_t> silent_audio_;
+ bool started_writing_;
+ size_t trailing_zeros_;
+};
+
+
class DiscardRenderer final : public test::FakeAudioDevice::Renderer {
public:
explicit DiscardRenderer(int sampling_frequency_in_hz)
@@ -162,6 +225,13 @@ std::unique_ptr<FakeAudioDevice::Renderer> FakeAudioDevice::CreateWavFileWriter(
}
std::unique_ptr<FakeAudioDevice::Renderer>
+ FakeAudioDevice::CreateBoundedWavFileWriter(
+ std::string filename, int sampling_frequency_in_hz) {
+ return std::unique_ptr<FakeAudioDevice::Renderer>(
+ new BoundedWavFileWriter(filename, sampling_frequency_in_hz));
+}
+
+std::unique_ptr<FakeAudioDevice::Renderer>
FakeAudioDevice::CreateDiscardRenderer(int sampling_frequency_in_hz) {
return std::unique_ptr<FakeAudioDevice::Renderer>(
new DiscardRenderer(sampling_frequency_in_hz));
« no previous file with comments | « webrtc/test/fake_audio_device.h ('k') | webrtc/test/fake_audio_device_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698