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

Side by Side Diff: webrtc/modules/audio_device/audio_device_unittest.cc

Issue 2736503002: Adds unit test for ADM on Linux (Closed)
Patch Set: Feedback from solenberg@ 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
« no previous file with comments | « webrtc/modules/audio_device/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <cstring>
12
13 #include "webrtc/base/event.h"
14 #include "webrtc/base/logging.h"
15 #include "webrtc/base/scoped_ref_ptr.h"
16 #include "webrtc/modules/audio_device/audio_device_impl.h"
17 #include "webrtc/modules/audio_device/include/audio_device.h"
18 #include "webrtc/modules/audio_device/include/mock_audio_transport.h"
19 #include "webrtc/system_wrappers/include/sleep.h"
20 #include "webrtc/test/gmock.h"
21 #include "webrtc/test/gtest.h"
22
23 using ::testing::_;
24 using ::testing::AtLeast;
25 using ::testing::Ge;
26 using ::testing::Invoke;
27 using ::testing::NiceMock;
28 using ::testing::NotNull;
29
30 namespace webrtc {
31 namespace {
32
33 // Don't run these tests in combination with sanitizers.
34 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
35 #define SKIP_TEST_IF_NOT(requirements_satisfied) \
36 do { \
37 if (!requirements_satisfied) { \
38 return; \
39 } \
40 } while (false)
41 #else
42 // Or if other audio-related requirements are not met.
43 #define SKIP_TEST_IF_NOT(requirements_satisfied) \
44 do { \
45 return; \
46 } while (false)
47 #endif
48
49 // Number of callbacks (input or output) the tests waits for before we set
50 // an event indicating that the test was OK.
51 static const size_t kNumCallbacks = 10;
52 // Max amount of time we wait for an event to be set while counting callbacks.
53 static const int kTestTimeOutInMilliseconds = 10 * 1000;
54
55 enum class TransportType {
56 kInvalid,
57 kPlay,
58 kRecord,
59 kPlayAndRecord,
60 };
61 } // namespace
62
63 // Mocks the AudioTransport object and proxies actions for the two callbacks
64 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations
65 // of AudioStreamInterface.
66 class MockAudioTransport : public test::MockAudioTransport {
67 public:
68 explicit MockAudioTransport(TransportType type) : type_(type) {}
69 ~MockAudioTransport() {}
70
71 // Set default actions of the mock object. We are delegating to fake
72 // implementation where the number of callbacks is counted and an event
73 // is set after a certain number of callbacks. Audio parameters are also
74 // checked.
75 void HandleCallbacks(rtc::Event* event, int num_callbacks) {
76 event_ = event;
77 num_callbacks_ = num_callbacks;
78 if (play_mode()) {
79 ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _))
80 .WillByDefault(
81 Invoke(this, &MockAudioTransport::RealNeedMorePlayData));
82 }
83 if (rec_mode()) {
84 ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _))
85 .WillByDefault(
86 Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable));
87 }
88 }
89
90 int32_t RealRecordedDataIsAvailable(const void* audio_buffer,
91 const size_t samples_per_channel,
92 const size_t bytes_per_frame,
93 const size_t channels,
94 const uint32_t sample_rate,
95 const uint32_t total_delay_ms,
96 const int32_t clock_drift,
97 const uint32_t current_mic_level,
98 const bool typing_status,
99 uint32_t& new_mic_level) {
100 EXPECT_TRUE(rec_mode()) << "No test is expecting these callbacks.";
101 LOG(INFO) << "+";
102 // Store audio parameters once in the first callback. For all other
103 // callbacks, verify that the provided audio parameters are maintained and
104 // that each callback corresponds to 10ms for any given sample rate.
105 if (!record_parameters_.is_complete()) {
106 record_parameters_.reset(sample_rate, channels, samples_per_channel);
107 } else {
108 EXPECT_EQ(samples_per_channel, record_parameters_.frames_per_buffer());
109 EXPECT_EQ(bytes_per_frame, record_parameters_.GetBytesPerFrame());
110 EXPECT_EQ(channels, record_parameters_.channels());
111 EXPECT_EQ(static_cast<int>(sample_rate),
112 record_parameters_.sample_rate());
113 EXPECT_EQ(samples_per_channel,
114 record_parameters_.frames_per_10ms_buffer());
115 }
116 rec_count_++;
117 // Signal the event after given amount of callbacks.
118 if (ReceivedEnoughCallbacks()) {
119 event_->Set();
120 }
121 return 0;
122 }
123
124 int32_t RealNeedMorePlayData(const size_t samples_per_channel,
125 const size_t bytes_per_frame,
126 const size_t channels,
127 const uint32_t sample_rate,
128 void* audio_buffer,
129 size_t& samples_per_channel_out,
130 int64_t* elapsed_time_ms,
131 int64_t* ntp_time_ms) {
132 EXPECT_TRUE(play_mode()) << "No test is expecting these callbacks.";
133 LOG(INFO) << "-";
134 // Store audio parameters once in the first callback. For all other
135 // callbacks, verify that the provided audio parameters are maintained and
136 // that each callback corresponds to 10ms for any given sample rate.
137 if (!playout_parameters_.is_complete()) {
138 playout_parameters_.reset(sample_rate, channels, samples_per_channel);
139 } else {
140 EXPECT_EQ(samples_per_channel, playout_parameters_.frames_per_buffer());
141 EXPECT_EQ(bytes_per_frame, playout_parameters_.GetBytesPerFrame());
142 EXPECT_EQ(channels, playout_parameters_.channels());
143 EXPECT_EQ(static_cast<int>(sample_rate),
144 playout_parameters_.sample_rate());
145 EXPECT_EQ(samples_per_channel,
146 playout_parameters_.frames_per_10ms_buffer());
147 }
148 play_count_++;
149 samples_per_channel_out = samples_per_channel;
150 // Fill the audio buffer with zeros to avoid disturbing audio.
151 const size_t num_bytes = samples_per_channel * bytes_per_frame;
152 std::memset(audio_buffer, 0, num_bytes);
153 // Signal the event after given amount of callbacks.
154 if (ReceivedEnoughCallbacks()) {
155 event_->Set();
156 }
157 return 0;
158 }
159
160 bool ReceivedEnoughCallbacks() {
161 bool recording_done = false;
162 if (rec_mode()) {
163 recording_done = rec_count_ >= num_callbacks_;
164 } else {
165 recording_done = true;
166 }
167 bool playout_done = false;
168 if (play_mode()) {
169 playout_done = play_count_ >= num_callbacks_;
170 } else {
171 playout_done = true;
172 }
173 return recording_done && playout_done;
174 }
175
176 bool play_mode() const {
177 return type_ == TransportType::kPlay ||
178 type_ == TransportType::kPlayAndRecord;
179 }
180
181 bool rec_mode() const {
182 return type_ == TransportType::kRecord ||
183 type_ == TransportType::kPlayAndRecord;
184 }
185
186 private:
187 TransportType type_;
the sun 2017/03/16 15:24:14 = kInvalid (or remove kInvalid from the enum)
henrika_webrtc 2017/03/17 09:54:03 Done.
188 rtc::Event* event_ = nullptr;
189 size_t num_callbacks_ = 0;
190 size_t play_count_ = 0;
191 size_t rec_count_ = 0;
192 AudioParameters playout_parameters_;
193 AudioParameters record_parameters_;
194 };
195
196 // AudioDeviceTest test fixture.
197 class AudioDeviceTest : public ::testing::Test {
the sun 2017/03/16 15:24:14 I had one more comment, which appears to have been
henrika_webrtc 2017/03/17 09:54:03 Thanks. I'll consider that next time. I have writt
198 protected:
199 AudioDeviceTest() : event_(false, false) {
200 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
201 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
202 // Add extra logging fields here if needed for debugging.
203 // rtc::LogMessage::LogTimestamps();
204 // rtc::LogMessage::LogThreads();
205 audio_device_ =
206 AudioDeviceModule::Create(0, AudioDeviceModule::kPlatformDefaultAudio);
207 EXPECT_NE(audio_device_.get(), nullptr);
208 AudioDeviceModule::AudioLayer audio_layer;
209 EXPECT_EQ(0, audio_device_->ActiveAudioLayer(&audio_layer));
210 if (audio_layer == AudioDeviceModule::kLinuxAlsaAudio) {
211 requirements_satisfied_ = false;
212 }
213 if (requirements_satisfied_) {
214 EXPECT_EQ(0, audio_device_->Init());
215 const int16_t num_playout_devices = audio_device_->PlayoutDevices();
216 const int16_t num_record_devices = audio_device_->RecordingDevices();
217 requirements_satisfied_ =
218 num_playout_devices > 0 && num_record_devices > 0;
219 }
220 #else
221 requirements_satisfied_ = false;
222 #endif
223 }
224 virtual ~AudioDeviceTest() {}
225
226 // Combine all required initialization and do all of it in the test setup.
227 // This test suite does not focus on testing all components of the ADM but
228 // instead focus on the "pig picture" and how it is used in VoiceEngine.
229 // TODO(henrika): perhaps use SetUpTestCase() to share initial setup between
the sun 2017/03/16 15:24:14 We're still sharding test cases in separate proces
henrika_webrtc 2017/03/17 09:54:02 I removed the use of SetUp/TearDown.
230 // all tests.
231 virtual void SetUp() {
232 if (!requirements_satisfied_)
233 return;
234 EXPECT_EQ(0, audio_device_->SetPlayoutDevice(0));
235 EXPECT_EQ(0, audio_device_->InitSpeaker());
236 EXPECT_EQ(0, audio_device_->SetRecordingDevice(0));
237 EXPECT_EQ(0, audio_device_->InitMicrophone());
238 EXPECT_EQ(0, audio_device_->StereoPlayoutIsAvailable(&stereo_playout_));
239 EXPECT_EQ(0, audio_device_->SetStereoPlayout(stereo_playout_));
240 EXPECT_EQ(0, audio_device_->StereoRecordingIsAvailable(&stereo_recording_));
241 EXPECT_EQ(0, audio_device_->SetStereoRecording(stereo_recording_));
242 EXPECT_EQ(0, audio_device_->SetAGC(false));
243 EXPECT_FALSE(audio_device_->AGC());
244 }
245
246 virtual void TearDown() {
247 if (audio_device_) {
248 EXPECT_EQ(0, audio_device_->Terminate());
249 }
250 }
251
252 bool requirements_satisfied() const { return requirements_satisfied_; }
253
254 const rtc::scoped_refptr<AudioDeviceModule>& audio_device() const {
255 return audio_device_;
256 }
257
258 void StartPlayout() {
259 EXPECT_FALSE(audio_device()->Playing());
260 EXPECT_EQ(0, audio_device()->InitPlayout());
261 EXPECT_TRUE(audio_device()->PlayoutIsInitialized());
262 EXPECT_EQ(0, audio_device()->StartPlayout());
263 EXPECT_TRUE(audio_device()->Playing());
264 }
265
266 void StopPlayout() {
267 EXPECT_EQ(0, audio_device()->StopPlayout());
268 EXPECT_FALSE(audio_device()->Playing());
269 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
270 }
271
272 void StartRecording() {
273 EXPECT_FALSE(audio_device()->Recording());
274 EXPECT_EQ(0, audio_device()->InitRecording());
275 EXPECT_TRUE(audio_device()->RecordingIsInitialized());
276 EXPECT_EQ(0, audio_device()->StartRecording());
277 EXPECT_TRUE(audio_device()->Recording());
278 }
279
280 void StopRecording() {
281 EXPECT_EQ(0, audio_device()->StopRecording());
282 EXPECT_FALSE(audio_device()->Recording());
283 EXPECT_FALSE(audio_device()->RecordingIsInitialized());
284 }
285
286 bool requirements_satisfied_ = true;
the sun 2017/03/16 15:24:14 Make these private: and add an accessor for event_
henrika_webrtc 2017/03/17 09:54:02 Used raw pointer as return value.
287 rtc::Event event_;
288 rtc::scoped_refptr<AudioDeviceModule> audio_device_;
289 bool stereo_playout_ = false;
290 bool stereo_recording_ = false;
291 };
292
293 // Uses the test fixture to create, initialize and destruct the ADM.
294 TEST_F(AudioDeviceTest, ConstructDestruct) {}
295
296 TEST_F(AudioDeviceTest, InitTerminate) {
297 SKIP_TEST_IF_NOT(requirements_satisfied());
298 // Initialization is part of the test fixture.
299 EXPECT_TRUE(audio_device()->Initialized());
300 EXPECT_EQ(0, audio_device()->Terminate());
301 EXPECT_FALSE(audio_device()->Initialized());
302 }
303
304 // Tests Start/Stop playout without any registered audio callback.
305 TEST_F(AudioDeviceTest, StartStopPlayout) {
306 SKIP_TEST_IF_NOT(requirements_satisfied());
307 StartPlayout();
308 StopPlayout();
309 StartPlayout();
310 StopPlayout();
311 }
312
313 // Tests Start/Stop recording without any registered audio callback.
314 TEST_F(AudioDeviceTest, StartStopRecording) {
315 SKIP_TEST_IF_NOT(requirements_satisfied());
316 StartRecording();
317 StopRecording();
318 StartRecording();
319 StopRecording();
320 }
321
322 // Start playout and verify that the native audio layer starts asking for real
323 // audio samples to play out using the NeedMorePlayData() callback.
324 // Note that we can't add expectations on audio parameters in EXPECT_CALL
325 // since parameter are not provided in the each callback. We therefore test and
326 // verify the parameters in the fake audio transport implementation instead.
327 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
328 SKIP_TEST_IF_NOT(requirements_satisfied());
329 MockAudioTransport mock(TransportType::kPlay);
330 mock.HandleCallbacks(&event_, kNumCallbacks);
331 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _))
332 .Times(AtLeast(kNumCallbacks));
333 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
334 StartPlayout();
335 event_.Wait(kTestTimeOutInMilliseconds);
336 StopPlayout();
337 }
338
339 // Start recording and verify that the native audio layer starts providing real
340 // audio samples using the RecordedDataIsAvailable() callback.
341 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
342 SKIP_TEST_IF_NOT(requirements_satisfied());
343 MockAudioTransport mock(TransportType::kRecord);
344 mock.HandleCallbacks(&event_, kNumCallbacks);
345 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _,
346 false, _))
347 .Times(AtLeast(kNumCallbacks));
348 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
349 StartRecording();
350 event_.Wait(kTestTimeOutInMilliseconds);
351 StopRecording();
352 }
353
354 // Start playout and recording (full-duplex audio) and verify that audio is
355 // active in both directions.
356 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
357 SKIP_TEST_IF_NOT(requirements_satisfied());
358 MockAudioTransport mock(TransportType::kPlayAndRecord);
359 mock.HandleCallbacks(&event_, kNumCallbacks);
360 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _))
361 .Times(AtLeast(kNumCallbacks));
362 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _,
363 false, _))
364 .Times(AtLeast(kNumCallbacks));
365 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
366 StartPlayout();
367 StartRecording();
368 event_.Wait(kTestTimeOutInMilliseconds);
369 StopRecording();
370 StopPlayout();
371 }
372
373 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_device/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698