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

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

Issue 2736503002: Adds unit test for ADM on Linux (Closed)
Patch Set: Final tuning 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_ = TransportType::kInvalid;
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 {
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 if (requirements_satisfied_) {
224 EXPECT_EQ(0, audio_device_->SetPlayoutDevice(0));
225 EXPECT_EQ(0, audio_device_->InitSpeaker());
226 EXPECT_EQ(0, audio_device_->SetRecordingDevice(0));
227 EXPECT_EQ(0, audio_device_->InitMicrophone());
228 EXPECT_EQ(0, audio_device_->StereoPlayoutIsAvailable(&stereo_playout_));
229 EXPECT_EQ(0, audio_device_->SetStereoPlayout(stereo_playout_));
230 EXPECT_EQ(0,
231 audio_device_->StereoRecordingIsAvailable(&stereo_recording_));
232 EXPECT_EQ(0, audio_device_->SetStereoRecording(stereo_recording_));
233 EXPECT_EQ(0, audio_device_->SetAGC(false));
234 EXPECT_FALSE(audio_device_->AGC());
235 }
236 }
237
238 virtual ~AudioDeviceTest() {
239 if (audio_device_) {
240 EXPECT_EQ(0, audio_device_->Terminate());
241 }
242 }
243
244 bool requirements_satisfied() const { return requirements_satisfied_; }
245 rtc::Event* event() { return &event_; }
246
247 const rtc::scoped_refptr<AudioDeviceModule>& audio_device() const {
248 return audio_device_;
249 }
250
251 void StartPlayout() {
252 EXPECT_FALSE(audio_device()->Playing());
253 EXPECT_EQ(0, audio_device()->InitPlayout());
254 EXPECT_TRUE(audio_device()->PlayoutIsInitialized());
255 EXPECT_EQ(0, audio_device()->StartPlayout());
256 EXPECT_TRUE(audio_device()->Playing());
257 }
258
259 void StopPlayout() {
260 EXPECT_EQ(0, audio_device()->StopPlayout());
261 EXPECT_FALSE(audio_device()->Playing());
262 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
263 }
264
265 void StartRecording() {
266 EXPECT_FALSE(audio_device()->Recording());
267 EXPECT_EQ(0, audio_device()->InitRecording());
268 EXPECT_TRUE(audio_device()->RecordingIsInitialized());
269 EXPECT_EQ(0, audio_device()->StartRecording());
270 EXPECT_TRUE(audio_device()->Recording());
271 }
272
273 void StopRecording() {
274 EXPECT_EQ(0, audio_device()->StopRecording());
275 EXPECT_FALSE(audio_device()->Recording());
276 EXPECT_FALSE(audio_device()->RecordingIsInitialized());
277 }
278
279 private:
280 bool requirements_satisfied_ = true;
281 rtc::Event event_;
282 rtc::scoped_refptr<AudioDeviceModule> audio_device_;
283 bool stereo_playout_ = false;
284 bool stereo_recording_ = false;
285 };
286
287 // Uses the test fixture to create, initialize and destruct the ADM.
288 TEST_F(AudioDeviceTest, ConstructDestruct) {}
289
290 TEST_F(AudioDeviceTest, InitTerminate) {
291 SKIP_TEST_IF_NOT(requirements_satisfied());
292 // Initialization is part of the test fixture.
293 EXPECT_TRUE(audio_device()->Initialized());
294 EXPECT_EQ(0, audio_device()->Terminate());
295 EXPECT_FALSE(audio_device()->Initialized());
296 }
297
298 // Tests Start/Stop playout without any registered audio callback.
299 TEST_F(AudioDeviceTest, StartStopPlayout) {
300 SKIP_TEST_IF_NOT(requirements_satisfied());
301 StartPlayout();
302 StopPlayout();
303 StartPlayout();
304 StopPlayout();
305 }
306
307 // Tests Start/Stop recording without any registered audio callback.
308 TEST_F(AudioDeviceTest, StartStopRecording) {
309 SKIP_TEST_IF_NOT(requirements_satisfied());
310 StartRecording();
311 StopRecording();
312 StartRecording();
313 StopRecording();
314 }
315
316 // Start playout and verify that the native audio layer starts asking for real
317 // audio samples to play out using the NeedMorePlayData() callback.
318 // Note that we can't add expectations on audio parameters in EXPECT_CALL
319 // since parameter are not provided in the each callback. We therefore test and
320 // verify the parameters in the fake audio transport implementation instead.
321 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
322 SKIP_TEST_IF_NOT(requirements_satisfied());
323 MockAudioTransport mock(TransportType::kPlay);
324 mock.HandleCallbacks(event(), kNumCallbacks);
325 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _))
326 .Times(AtLeast(kNumCallbacks));
327 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
328 StartPlayout();
329 event()->Wait(kTestTimeOutInMilliseconds);
330 StopPlayout();
331 }
332
333 // Start recording and verify that the native audio layer starts providing real
334 // audio samples using the RecordedDataIsAvailable() callback.
335 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
336 SKIP_TEST_IF_NOT(requirements_satisfied());
337 MockAudioTransport mock(TransportType::kRecord);
338 mock.HandleCallbacks(event(), kNumCallbacks);
339 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _,
340 false, _))
341 .Times(AtLeast(kNumCallbacks));
342 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
343 StartRecording();
344 event()->Wait(kTestTimeOutInMilliseconds);
345 StopRecording();
346 }
347
348 // Start playout and recording (full-duplex audio) and verify that audio is
349 // active in both directions.
350 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
351 SKIP_TEST_IF_NOT(requirements_satisfied());
352 MockAudioTransport mock(TransportType::kPlayAndRecord);
353 mock.HandleCallbacks(event(), kNumCallbacks);
354 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _))
355 .Times(AtLeast(kNumCallbacks));
356 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _,
357 false, _))
358 .Times(AtLeast(kNumCallbacks));
359 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
360 StartPlayout();
361 StartRecording();
362 event()->Wait(kTestTimeOutInMilliseconds);
363 StopRecording();
364 StopPlayout();
365 }
366
367 } // 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