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

Side by Side Diff: webrtc/test/fake_audio_device.cc

Issue 2694203002: Low-bandwidth audio testing (Closed)
Patch Set: Move BoundedWavFileWriter and add unittests for it 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 unified diff | Download patch
OLDNEW
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
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 bool Render(rtc::ArrayView<const int16_t> data) override { 104 bool Render(rtc::ArrayView<const int16_t> data) override {
105 wav_writer_.WriteSamples(data.data(), data.size()); 105 wav_writer_.WriteSamples(data.data(), data.size());
106 return true; 106 return true;
107 } 107 }
108 108
109 private: 109 private:
110 int sampling_frequency_in_hz_; 110 int sampling_frequency_in_hz_;
111 WavWriter wav_writer_; 111 WavWriter wav_writer_;
112 }; 112 };
113 113
114 class BoundedWavFileWriter : public test::FakeAudioDevice::Renderer {
115 public:
116 BoundedWavFileWriter(std::string filename, int sampling_frequency_in_hz)
117 : sampling_frequency_in_hz_(sampling_frequency_in_hz),
118 wav_writer_(filename, sampling_frequency_in_hz, 1),
119 silent_audio_(test::FakeAudioDevice::SamplesPerFrame(
120 sampling_frequency_in_hz), 0),
121 started_writing_(false),
122 trailing_zeros_(0) {}
123
124 int SamplingFrequency() const override {
125 return sampling_frequency_in_hz_;
126 }
127
128 bool Render(rtc::ArrayView<const int16_t> data) override {
129 const int16_t kAmplitudeThreshold = 5;
130
131 const int16_t* begin = data.begin();
132 const int16_t* end = data.end();
133 if (!started_writing_) {
134 // Cut off silence at the beginning.
135 while (begin < end) {
136 if (std::abs(*begin) > kAmplitudeThreshold) {
137 started_writing_ = true;
138 break;
139 }
140 ++begin;
141 }
142 }
143 if (started_writing_) {
144 // Cut off silence at the end.
145 while (begin < end) {
146 if (*(end - 1) != 0) {
147 break;
148 }
149 --end;
150 }
151 if (begin < end) {
152 // If it turns out that the silence was not final, need to write all the
153 // skipped zeros and continue writing audio.
154 while (trailing_zeros_ > 0) {
155 const size_t zeros_to_write = std::min(trailing_zeros_,
156 silent_audio_.size());
157 wav_writer_.WriteSamples(silent_audio_.data(), zeros_to_write);
158 trailing_zeros_ -= zeros_to_write;
159 }
160 wav_writer_.WriteSamples(begin, end - begin);
161 }
162 // Save the number of zeros we skipped in case this needs to be restored.
163 trailing_zeros_ += data.end() - end;
164 }
165 return true;
166 }
167
168 private:
169 int sampling_frequency_in_hz_;
170 WavWriter wav_writer_;
171 std::vector<int16_t> silent_audio_;
172 bool started_writing_;
173 size_t trailing_zeros_;
174 };
175
176
114 class DiscardRenderer final : public test::FakeAudioDevice::Renderer { 177 class DiscardRenderer final : public test::FakeAudioDevice::Renderer {
115 public: 178 public:
116 explicit DiscardRenderer(int sampling_frequency_in_hz) 179 explicit DiscardRenderer(int sampling_frequency_in_hz)
117 : sampling_frequency_in_hz_(sampling_frequency_in_hz) {} 180 : sampling_frequency_in_hz_(sampling_frequency_in_hz) {}
118 181
119 int SamplingFrequency() const override { 182 int SamplingFrequency() const override {
120 return sampling_frequency_in_hz_; 183 return sampling_frequency_in_hz_;
121 } 184 }
122 185
123 bool Render(rtc::ArrayView<const int16_t> data) override { 186 bool Render(rtc::ArrayView<const int16_t> data) override {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 new WavFileReader(filename, sampling_frequency_in_hz)); 218 new WavFileReader(filename, sampling_frequency_in_hz));
156 } 219 }
157 220
158 std::unique_ptr<FakeAudioDevice::Renderer> FakeAudioDevice::CreateWavFileWriter( 221 std::unique_ptr<FakeAudioDevice::Renderer> FakeAudioDevice::CreateWavFileWriter(
159 std::string filename, int sampling_frequency_in_hz) { 222 std::string filename, int sampling_frequency_in_hz) {
160 return std::unique_ptr<FakeAudioDevice::Renderer>( 223 return std::unique_ptr<FakeAudioDevice::Renderer>(
161 new WavFileWriter(filename, sampling_frequency_in_hz)); 224 new WavFileWriter(filename, sampling_frequency_in_hz));
162 } 225 }
163 226
164 std::unique_ptr<FakeAudioDevice::Renderer> 227 std::unique_ptr<FakeAudioDevice::Renderer>
228 FakeAudioDevice::CreateBoundedWavFileWriter(
229 std::string filename, int sampling_frequency_in_hz) {
230 return std::unique_ptr<FakeAudioDevice::Renderer>(
231 new BoundedWavFileWriter(filename, sampling_frequency_in_hz));
232 }
233
234 std::unique_ptr<FakeAudioDevice::Renderer>
165 FakeAudioDevice::CreateDiscardRenderer(int sampling_frequency_in_hz) { 235 FakeAudioDevice::CreateDiscardRenderer(int sampling_frequency_in_hz) {
166 return std::unique_ptr<FakeAudioDevice::Renderer>( 236 return std::unique_ptr<FakeAudioDevice::Renderer>(
167 new DiscardRenderer(sampling_frequency_in_hz)); 237 new DiscardRenderer(sampling_frequency_in_hz));
168 } 238 }
169 239
170 240
171 FakeAudioDevice::FakeAudioDevice(std::unique_ptr<Capturer> capturer, 241 FakeAudioDevice::FakeAudioDevice(std::unique_ptr<Capturer> capturer,
172 std::unique_ptr<Renderer> renderer, 242 std::unique_ptr<Renderer> renderer,
173 float speed) 243 float speed)
174 : capturer_(std::move(capturer)), 244 : capturer_(std::move(capturer)),
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 done_rendering_.Set(); 371 done_rendering_.Set();
302 } 372 }
303 } 373 }
304 } 374 }
305 tick_->Wait(WEBRTC_EVENT_INFINITE); 375 tick_->Wait(WEBRTC_EVENT_INFINITE);
306 } 376 }
307 377
308 378
309 } // namespace test 379 } // namespace test
310 } // namespace webrtc 380 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698