1 /*
2 * Copyright (c) 2015 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 <algorithm>
12 #include <limits>
13 #include <list>
14 #include <memory>
15 #include <numeric>
16 #include <string>
17 #include <vector>
18
19 #include "modules/audio_device/android/audio_common.h"
20 #include "modules/audio_device/android/audio_manager.h"
21 #include "modules/audio_device/android/build_info.h"
22 #include "modules/audio_device/android/ensure_initialized.h"
23 #include "modules/audio_device/audio_device_impl.h"
24 #include "modules/audio_device/include/audio_device.h"
25 #include "modules/audio_device/include/mock_audio_transport.h"
26 #include "rtc_base/arraysize.h"
27 #include "rtc_base/criticalsection.h"
28 #include "rtc_base/format_macros.h"
29 #include "rtc_base/scoped_ref_ptr.h"
30 #include "rtc_base/timeutils.h"
31 #include "system_wrappers/include/event_wrapper.h"
32 #include "test/gmock.h"
33 #include "test/gtest.h"
34 #include "test/testsupport/fileutils.h"
35
36 using std::cout;
37 using std::endl;
38 using ::testing::_;
39 using ::testing::AtLeast;
40 using ::testing::Gt;
41 using ::testing::Invoke;
42 using ::testing::NiceMock;
43 using ::testing::NotNull;
44 using ::testing::Return;
45
46 // #define ENABLE_DEBUG_PRINTF
47 #ifdef ENABLE_DEBUG_PRINTF
48 #define PRINTD(...) fprintf(stderr, __VA_ARGS__);
49 #else
50 #define PRINTD(...) ((void)0)
51 #endif
52 #define PRINT(...) fprintf(stderr, __VA_ARGS__);
53
54 namespace webrtc {
55
56 // Number of callbacks (input or output) the tests waits for before we set
57 // an event indicating that the test was OK.
58 static const size_t kNumCallbacks = 10;
59 // Max amount of time we wait for an event to be set while counting callbacks.
60 static const int kTestTimeOutInMilliseconds = 10 * 1000;
61 // Average number of audio callbacks per second assuming 10ms packet size.
62 static const size_t kNumCallbacksPerSecond = 100;
63 // Play out a test file during this time (unit is in seconds).
64 static const int kFilePlayTimeInSec = 5;
65 static const size_t kBitsPerSample = 16;
66 static const size_t kBytesPerSample = kBitsPerSample / 8;
67 // Run the full-duplex test during this time (unit is in seconds).
68 // Note that first |kNumIgnoreFirstCallbacks| are ignored.
69 static const int kFullDuplexTimeInSec = 5;
70 // Wait for the callback sequence to stabilize by ignoring this amount of the
71 // initial callbacks (avoids initial FIFO access).
72 // Only used in the RunPlayoutAndRecordingInFullDuplex test.
73 static const size_t kNumIgnoreFirstCallbacks = 50;
74 // Sets the number of impulses per second in the latency test.
75 static const int kImpulseFrequencyInHz = 1;
76 // Length of round-trip latency measurements. Number of transmitted impulses
77 // is kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1.
78 static const int kMeasureLatencyTimeInSec = 11;
79 // Utilized in round-trip latency measurements to avoid capturing noise samples.
80 static const int kImpulseThreshold = 1000;
81 static const char kTag[] = "[..........] ";
82
83 enum TransportType {
84 kPlayout = 0x1,
85 kRecording = 0x2,
86 };
87
88 // Interface for processing the audio stream. Real implementations can e.g.
89 // run audio in loopback, read audio from a file or perform latency
90 // measurements.
91 class AudioStreamInterface {
92 public:
93 virtual void Write(const void* source, size_t num_frames) = 0;
94 virtual void Read(void* destination, size_t num_frames) = 0;
95 protected:
~AudioStreamInterface()96 virtual ~AudioStreamInterface() {}
97 };
98
99 // Reads audio samples from a PCM file where the file is stored in memory at
100 // construction.
101 class FileAudioStream : public AudioStreamInterface {
102 public:
FileAudioStream(size_t num_callbacks,const std::string & file_name,int sample_rate)103 FileAudioStream(
104 size_t num_callbacks, const std::string& file_name, int sample_rate)
105 : file_size_in_bytes_(0),
106 sample_rate_(sample_rate),
107 file_pos_(0) {
108 file_size_in_bytes_ = test::GetFileSize(file_name);
109 sample_rate_ = sample_rate;
110 EXPECT_GE(file_size_in_callbacks(), num_callbacks)
111 << "Size of test file is not large enough to last during the test.";
112 const size_t num_16bit_samples =
113 test::GetFileSize(file_name) / kBytesPerSample;
114 file_.reset(new int16_t[num_16bit_samples]);
115 FILE* audio_file = fopen(file_name.c_str(), "rb");
116 EXPECT_NE(audio_file, nullptr);
117 size_t num_samples_read = fread(
118 file_.get(), sizeof(int16_t), num_16bit_samples, audio_file);
119 EXPECT_EQ(num_samples_read, num_16bit_samples);
120 fclose(audio_file);
121 }
122
123 // AudioStreamInterface::Write() is not implemented.
Write(const void * source,size_t num_frames)124 void Write(const void* source, size_t num_frames) override {}
125
126 // Read samples from file stored in memory (at construction) and copy
127 // |num_frames| (<=> 10ms) to the |destination| byte buffer.
Read(void * destination,size_t num_frames)128 void Read(void* destination, size_t num_frames) override {
129 memcpy(destination,
130 static_cast<int16_t*> (&file_[file_pos_]),
131 num_frames * sizeof(int16_t));
132 file_pos_ += num_frames;
133 }
134
file_size_in_seconds() const135 int file_size_in_seconds() const {
136 return static_cast<int>(
137 file_size_in_bytes_ / (kBytesPerSample * sample_rate_));
138 }
file_size_in_callbacks() const139 size_t file_size_in_callbacks() const {
140 return file_size_in_seconds() * kNumCallbacksPerSecond;
141 }
142
143 private:
144 size_t file_size_in_bytes_;
145 int sample_rate_;
146 std::unique_ptr<int16_t[]> file_;
147 size_t file_pos_;
148 };
149
150 // Simple first in first out (FIFO) class that wraps a list of 16-bit audio
151 // buffers of fixed size and allows Write and Read operations. The idea is to
152 // store recorded audio buffers (using Write) and then read (using Read) these
153 // stored buffers with as short delay as possible when the audio layer needs
154 // data to play out. The number of buffers in the FIFO will stabilize under
155 // normal conditions since there will be a balance between Write and Read calls.
156 // The container is a std::list container and access is protected with a lock
157 // since both sides (playout and recording) are driven by its own thread.
158 class FifoAudioStream : public AudioStreamInterface {
159 public:
FifoAudioStream(size_t frames_per_buffer)160 explicit FifoAudioStream(size_t frames_per_buffer)
161 : frames_per_buffer_(frames_per_buffer),
162 bytes_per_buffer_(frames_per_buffer_ * sizeof(int16_t)),
163 fifo_(new AudioBufferList),
164 largest_size_(0),
165 total_written_elements_(0),
166 write_count_(0) {
167 EXPECT_NE(fifo_.get(), nullptr);
168 }
169
~FifoAudioStream()170 ~FifoAudioStream() {
171 Flush();
172 }
173
174 // Allocate new memory, copy |num_frames| samples from |source| into memory
175 // and add pointer to the memory location to end of the list.
176 // Increases the size of the FIFO by one element.
Write(const void * source,size_t num_frames)177 void Write(const void* source, size_t num_frames) override {
178 ASSERT_EQ(num_frames, frames_per_buffer_);
179 PRINTD("+");
180 if (write_count_++ < kNumIgnoreFirstCallbacks) {
181 return;
182 }
183 int16_t* memory = new int16_t[frames_per_buffer_];
184 memcpy(static_cast<int16_t*> (&memory[0]),
185 source,
186 bytes_per_buffer_);
187 rtc::CritScope lock(&lock_);
188 fifo_->push_back(memory);
189 const size_t size = fifo_->size();
190 if (size > largest_size_) {
191 largest_size_ = size;
192 PRINTD("(%" PRIuS ")", largest_size_);
193 }
194 total_written_elements_ += size;
195 }
196
197 // Read pointer to data buffer from front of list, copy |num_frames| of stored
198 // data into |destination| and delete the utilized memory allocation.
199 // Decreases the size of the FIFO by one element.
Read(void * destination,size_t num_frames)200 void Read(void* destination, size_t num_frames) override {
201 ASSERT_EQ(num_frames, frames_per_buffer_);
202 PRINTD("-");
203 rtc::CritScope lock(&lock_);
204 if (fifo_->empty()) {
205 memset(destination, 0, bytes_per_buffer_);
206 } else {
207 int16_t* memory = fifo_->front();
208 fifo_->pop_front();
209 memcpy(destination,
210 static_cast<int16_t*> (&memory[0]),
211 bytes_per_buffer_);
212 delete memory;
213 }
214 }
215
size() const216 size_t size() const {
217 return fifo_->size();
218 }
219
largest_size() const220 size_t largest_size() const {
221 return largest_size_;
222 }
223
average_size() const224 size_t average_size() const {
225 return (total_written_elements_ == 0) ? 0.0 : 0.5 + static_cast<float> (
226 total_written_elements_) / (write_count_ - kNumIgnoreFirstCallbacks);
227 }
228
229 private:
Flush()230 void Flush() {
231 for (auto it = fifo_->begin(); it != fifo_->end(); ++it) {
232 delete *it;
233 }
234 fifo_->clear();
235 }
236
237 using AudioBufferList = std::list<int16_t*>;
238 rtc::CriticalSection lock_;
239 const size_t frames_per_buffer_;
240 const size_t bytes_per_buffer_;
241 std::unique_ptr<AudioBufferList> fifo_;
242 size_t largest_size_;
243 size_t total_written_elements_;
244 size_t write_count_;
245 };
246
247 // Inserts periodic impulses and measures the latency between the time of
248 // transmission and time of receiving the same impulse.
249 // Usage requires a special hardware called Audio Loopback Dongle.
250 // See http://source.android.com/devices/audio/loopback.html for details.
251 class LatencyMeasuringAudioStream : public AudioStreamInterface {
252 public:
LatencyMeasuringAudioStream(size_t frames_per_buffer)253 explicit LatencyMeasuringAudioStream(size_t frames_per_buffer)
254 : frames_per_buffer_(frames_per_buffer),
255 bytes_per_buffer_(frames_per_buffer_ * sizeof(int16_t)),
256 play_count_(0),
257 rec_count_(0),
258 pulse_time_(0) {
259 }
260
261 // Insert periodic impulses in first two samples of |destination|.
Read(void * destination,size_t num_frames)262 void Read(void* destination, size_t num_frames) override {
263 ASSERT_EQ(num_frames, frames_per_buffer_);
264 if (play_count_ == 0) {
265 PRINT("[");
266 }
267 play_count_++;
268 memset(destination, 0, bytes_per_buffer_);
269 if (play_count_ % (kNumCallbacksPerSecond / kImpulseFrequencyInHz) == 0) {
270 if (pulse_time_ == 0) {
271 pulse_time_ = rtc::TimeMillis();
272 }
273 PRINT(".");
274 const int16_t impulse = std::numeric_limits<int16_t>::max();
275 int16_t* ptr16 = static_cast<int16_t*> (destination);
276 for (size_t i = 0; i < 2; ++i) {
277 ptr16[i] = impulse;
278 }
279 }
280 }
281
282 // Detect received impulses in |source|, derive time between transmission and
283 // detection and add the calculated delay to list of latencies.
Write(const void * source,size_t num_frames)284 void Write(const void* source, size_t num_frames) override {
285 ASSERT_EQ(num_frames, frames_per_buffer_);
286 rec_count_++;
287 if (pulse_time_ == 0) {
288 // Avoid detection of new impulse response until a new impulse has
289 // been transmitted (sets |pulse_time_| to value larger than zero).
290 return;
291 }
292 const int16_t* ptr16 = static_cast<const int16_t*> (source);
293 std::vector<int16_t> vec(ptr16, ptr16 + num_frames);
294 // Find max value in the audio buffer.
295 int max = *std::max_element(vec.begin(), vec.end());
296 // Find index (element position in vector) of the max element.
297 int index_of_max = std::distance(vec.begin(),
298 std::find(vec.begin(), vec.end(),
299 max));
300 if (max > kImpulseThreshold) {
301 PRINTD("(%d,%d)", max, index_of_max);
302 int64_t now_time = rtc::TimeMillis();
303 int extra_delay = IndexToMilliseconds(static_cast<double> (index_of_max));
304 PRINTD("[%d]", static_cast<int> (now_time - pulse_time_));
305 PRINTD("[%d]", extra_delay);
306 // Total latency is the difference between transmit time and detection
307 // tome plus the extra delay within the buffer in which we detected the
308 // received impulse. It is transmitted at sample 0 but can be received
309 // at sample N where N > 0. The term |extra_delay| accounts for N and it
310 // is a value between 0 and 10ms.
311 latencies_.push_back(now_time - pulse_time_ + extra_delay);
312 pulse_time_ = 0;
313 } else {
314 PRINTD("-");
315 }
316 }
317
num_latency_values() const318 size_t num_latency_values() const {
319 return latencies_.size();
320 }
321
min_latency() const322 int min_latency() const {
323 if (latencies_.empty())
324 return 0;
325 return *std::min_element(latencies_.begin(), latencies_.end());
326 }
327
max_latency() const328 int max_latency() const {
329 if (latencies_.empty())
330 return 0;
331 return *std::max_element(latencies_.begin(), latencies_.end());
332 }
333
average_latency() const334 int average_latency() const {
335 if (latencies_.empty())
336 return 0;
337 return 0.5 + static_cast<double> (
338 std::accumulate(latencies_.begin(), latencies_.end(), 0)) /
339 latencies_.size();
340 }
341
PrintResults() const342 void PrintResults() const {
343 PRINT("] ");
344 for (auto it = latencies_.begin(); it != latencies_.end(); ++it) {
345 PRINT("%d ", *it);
346 }
347 PRINT("\n");
348 PRINT("%s[min, max, avg]=[%d, %d, %d] ms\n", kTag,
349 min_latency(), max_latency(), average_latency());
350 }
351
IndexToMilliseconds(double index) const352 int IndexToMilliseconds(double index) const {
353 return static_cast<int>(10.0 * (index / frames_per_buffer_) + 0.5);
354 }
355
356 private:
357 const size_t frames_per_buffer_;
358 const size_t bytes_per_buffer_;
359 size_t play_count_;
360 size_t rec_count_;
361 int64_t pulse_time_;
362 std::vector<int> latencies_;
363 };
364
365 // Mocks the AudioTransport object and proxies actions for the two callbacks
366 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations
367 // of AudioStreamInterface.
368 class MockAudioTransportAndroid : public test::MockAudioTransport {
369 public:
MockAudioTransportAndroid(int type)370 explicit MockAudioTransportAndroid(int type)
371 : num_callbacks_(0),
372 type_(type),
373 play_count_(0),
374 rec_count_(0),
375 audio_stream_(nullptr) {}
376
~MockAudioTransportAndroid()377 virtual ~MockAudioTransportAndroid() {}
378
379 // Set default actions of the mock object. We are delegating to fake
380 // implementations (of AudioStreamInterface) here.
HandleCallbacks(EventWrapper * test_is_done,AudioStreamInterface * audio_stream,int num_callbacks)381 void HandleCallbacks(EventWrapper* test_is_done,
382 AudioStreamInterface* audio_stream,
383 int num_callbacks) {
384 test_is_done_ = test_is_done;
385 audio_stream_ = audio_stream;
386 num_callbacks_ = num_callbacks;
387 if (play_mode()) {
388 ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _))
389 .WillByDefault(
390 Invoke(this, &MockAudioTransportAndroid::RealNeedMorePlayData));
391 }
392 if (rec_mode()) {
393 ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _))
394 .WillByDefault(Invoke(
395 this, &MockAudioTransportAndroid::RealRecordedDataIsAvailable));
396 }
397 }
398
RealRecordedDataIsAvailable(const void * audioSamples,const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samplesPerSec,const uint32_t totalDelayMS,const int32_t clockDrift,const uint32_t currentMicLevel,const bool keyPressed,uint32_t & newMicLevel)399 int32_t RealRecordedDataIsAvailable(const void* audioSamples,
400 const size_t nSamples,
401 const size_t nBytesPerSample,
402 const size_t nChannels,
403 const uint32_t samplesPerSec,
404 const uint32_t totalDelayMS,
405 const int32_t clockDrift,
406 const uint32_t currentMicLevel,
407 const bool keyPressed,
408 uint32_t& newMicLevel) {
409 EXPECT_TRUE(rec_mode()) << "No test is expecting these callbacks.";
410 rec_count_++;
411 // Process the recorded audio stream if an AudioStreamInterface
412 // implementation exists.
413 if (audio_stream_) {
414 audio_stream_->Write(audioSamples, nSamples);
415 }
416 if (ReceivedEnoughCallbacks()) {
417 test_is_done_->Set();
418 }
419 return 0;
420 }
421
RealNeedMorePlayData(const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samplesPerSec,void * audioSamples,size_t & nSamplesOut,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)422 int32_t RealNeedMorePlayData(const size_t nSamples,
423 const size_t nBytesPerSample,
424 const size_t nChannels,
425 const uint32_t samplesPerSec,
426 void* audioSamples,
427 size_t& nSamplesOut,
428 int64_t* elapsed_time_ms,
429 int64_t* ntp_time_ms) {
430 EXPECT_TRUE(play_mode()) << "No test is expecting these callbacks.";
431 play_count_++;
432 nSamplesOut = nSamples;
433 // Read (possibly processed) audio stream samples to be played out if an
434 // AudioStreamInterface implementation exists.
435 if (audio_stream_) {
436 audio_stream_->Read(audioSamples, nSamples);
437 }
438 if (ReceivedEnoughCallbacks()) {
439 test_is_done_->Set();
440 }
441 return 0;
442 }
443
ReceivedEnoughCallbacks()444 bool ReceivedEnoughCallbacks() {
445 bool recording_done = false;
446 if (rec_mode())
447 recording_done = rec_count_ >= num_callbacks_;
448 else
449 recording_done = true;
450
451 bool playout_done = false;
452 if (play_mode())
453 playout_done = play_count_ >= num_callbacks_;
454 else
455 playout_done = true;
456
457 return recording_done && playout_done;
458 }
459
play_mode() const460 bool play_mode() const { return type_ & kPlayout; }
rec_mode() const461 bool rec_mode() const { return type_ & kRecording; }
462
463 private:
464 EventWrapper* test_is_done_;
465 size_t num_callbacks_;
466 int type_;
467 size_t play_count_;
468 size_t rec_count_;
469 AudioStreamInterface* audio_stream_;
470 std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream_;
471 };
472
473 // AudioDeviceTest test fixture.
474 class AudioDeviceTest : public ::testing::Test {
475 protected:
AudioDeviceTest()476 AudioDeviceTest()
477 : test_is_done_(EventWrapper::Create()) {
478 // One-time initialization of JVM and application context. Ensures that we
479 // can do calls between C++ and Java. Initializes both Java and OpenSL ES
480 // implementations.
481 webrtc::audiodevicemodule::EnsureInitialized();
482 // Creates an audio device using a default audio layer.
483 audio_device_ = CreateAudioDevice(AudioDeviceModule::kPlatformDefaultAudio);
484 EXPECT_NE(audio_device_.get(), nullptr);
485 EXPECT_EQ(0, audio_device_->Init());
486 playout_parameters_ = audio_manager()->GetPlayoutAudioParameters();
487 record_parameters_ = audio_manager()->GetRecordAudioParameters();
488 build_info_.reset(new BuildInfo());
489 }
~AudioDeviceTest()490 virtual ~AudioDeviceTest() {
491 EXPECT_EQ(0, audio_device_->Terminate());
492 }
493
playout_sample_rate() const494 int playout_sample_rate() const {
495 return playout_parameters_.sample_rate();
496 }
record_sample_rate() const497 int record_sample_rate() const {
498 return record_parameters_.sample_rate();
499 }
playout_channels() const500 size_t playout_channels() const {
501 return playout_parameters_.channels();
502 }
record_channels() const503 size_t record_channels() const {
504 return record_parameters_.channels();
505 }
playout_frames_per_10ms_buffer() const506 size_t playout_frames_per_10ms_buffer() const {
507 return playout_parameters_.frames_per_10ms_buffer();
508 }
record_frames_per_10ms_buffer() const509 size_t record_frames_per_10ms_buffer() const {
510 return record_parameters_.frames_per_10ms_buffer();
511 }
512
total_delay_ms() const513 int total_delay_ms() const {
514 return audio_manager()->GetDelayEstimateInMilliseconds();
515 }
516
audio_device() const517 rtc::scoped_refptr<AudioDeviceModule> audio_device() const {
518 return audio_device_;
519 }
520
audio_device_impl() const521 AudioDeviceModuleImpl* audio_device_impl() const {
522 return static_cast<AudioDeviceModuleImpl*>(audio_device_.get());
523 }
524
audio_manager() const525 AudioManager* audio_manager() const {
526 return audio_device_impl()->GetAndroidAudioManagerForTest();
527 }
528
GetAudioManager(AudioDeviceModule * adm) const529 AudioManager* GetAudioManager(AudioDeviceModule* adm) const {
530 return static_cast<AudioDeviceModuleImpl*>(adm)->
531 GetAndroidAudioManagerForTest();
532 }
533
audio_device_buffer() const534 AudioDeviceBuffer* audio_device_buffer() const {
535 return audio_device_impl()->GetAudioDeviceBuffer();
536 }
537
CreateAudioDevice(AudioDeviceModule::AudioLayer audio_layer)538 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDevice(
539 AudioDeviceModule::AudioLayer audio_layer) {
540 rtc::scoped_refptr<AudioDeviceModule> module(
541 AudioDeviceModule::Create(0, audio_layer));
542 return module;
543 }
544
545 // Returns file name relative to the resource root given a sample rate.
GetFileName(int sample_rate)546 std::string GetFileName(int sample_rate) {
547 EXPECT_TRUE(sample_rate == 48000 || sample_rate == 44100);
548 char fname[64];
549 snprintf(fname,
550 sizeof(fname),
551 "audio_device/audio_short%d",
552 sample_rate / 1000);
553 std::string file_name(webrtc::test::ResourcePath(fname, "pcm"));
554 EXPECT_TRUE(test::FileExists(file_name));
555 #ifdef ENABLE_PRINTF
556 PRINT("file name: %s\n", file_name.c_str());
557 const size_t bytes = test::GetFileSize(file_name);
558 PRINT("file size: %" PRIuS " [bytes]\n", bytes);
559 PRINT("file size: %" PRIuS " [samples]\n", bytes / kBytesPerSample);
560 const int seconds =
561 static_cast<int>(bytes / (sample_rate * kBytesPerSample));
562 PRINT("file size: %d [secs]\n", seconds);
563 PRINT("file size: %" PRIuS " [callbacks]\n",
564 seconds * kNumCallbacksPerSecond);
565 #endif
566 return file_name;
567 }
568
GetActiveAudioLayer() const569 AudioDeviceModule::AudioLayer GetActiveAudioLayer() const {
570 AudioDeviceModule::AudioLayer audio_layer;
571 EXPECT_EQ(0, audio_device()->ActiveAudioLayer(&audio_layer));
572 return audio_layer;
573 }
574
TestDelayOnAudioLayer(const AudioDeviceModule::AudioLayer & layer_to_test)575 int TestDelayOnAudioLayer(
576 const AudioDeviceModule::AudioLayer& layer_to_test) {
577 rtc::scoped_refptr<AudioDeviceModule> audio_device;
578 audio_device = CreateAudioDevice(layer_to_test);
579 EXPECT_NE(audio_device.get(), nullptr);
580 AudioManager* audio_manager = GetAudioManager(audio_device.get());
581 EXPECT_NE(audio_manager, nullptr);
582 return audio_manager->GetDelayEstimateInMilliseconds();
583 }
584
TestActiveAudioLayer(const AudioDeviceModule::AudioLayer & layer_to_test)585 AudioDeviceModule::AudioLayer TestActiveAudioLayer(
586 const AudioDeviceModule::AudioLayer& layer_to_test) {
587 rtc::scoped_refptr<AudioDeviceModule> audio_device;
588 audio_device = CreateAudioDevice(layer_to_test);
589 EXPECT_NE(audio_device.get(), nullptr);
590 AudioDeviceModule::AudioLayer active;
591 EXPECT_EQ(0, audio_device->ActiveAudioLayer(&active));
592 return active;
593 }
594
DisableTestForThisDevice(const std::string & model)595 bool DisableTestForThisDevice(const std::string& model) {
596 return (build_info_->GetDeviceModel() == model);
597 }
598
599 // Volume control is currently only supported for the Java output audio layer.
600 // For OpenSL ES, the internal stream volume is always on max level and there
601 // is no need for this test to set it to max.
AudioLayerSupportsVolumeControl() const602 bool AudioLayerSupportsVolumeControl() const {
603 return GetActiveAudioLayer() == AudioDeviceModule::kAndroidJavaAudio;
604 }
605
SetMaxPlayoutVolume()606 void SetMaxPlayoutVolume() {
607 if (!AudioLayerSupportsVolumeControl())
608 return;
609 uint32_t max_volume;
610 EXPECT_EQ(0, audio_device()->MaxSpeakerVolume(&max_volume));
611 EXPECT_EQ(0, audio_device()->SetSpeakerVolume(max_volume));
612 }
613
DisableBuiltInAECIfAvailable()614 void DisableBuiltInAECIfAvailable() {
615 if (audio_device()->BuiltInAECIsAvailable()) {
616 EXPECT_EQ(0, audio_device()->EnableBuiltInAEC(false));
617 }
618 }
619
StartPlayout()620 void StartPlayout() {
621 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
622 EXPECT_FALSE(audio_device()->Playing());
623 EXPECT_EQ(0, audio_device()->InitPlayout());
624 EXPECT_TRUE(audio_device()->PlayoutIsInitialized());
625 EXPECT_EQ(0, audio_device()->StartPlayout());
626 EXPECT_TRUE(audio_device()->Playing());
627 }
628
StopPlayout()629 void StopPlayout() {
630 EXPECT_EQ(0, audio_device()->StopPlayout());
631 EXPECT_FALSE(audio_device()->Playing());
632 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
633 }
634
StartRecording()635 void StartRecording() {
636 EXPECT_FALSE(audio_device()->RecordingIsInitialized());
637 EXPECT_FALSE(audio_device()->Recording());
638 EXPECT_EQ(0, audio_device()->InitRecording());
639 EXPECT_TRUE(audio_device()->RecordingIsInitialized());
640 EXPECT_EQ(0, audio_device()->StartRecording());
641 EXPECT_TRUE(audio_device()->Recording());
642 }
643
StopRecording()644 void StopRecording() {
645 EXPECT_EQ(0, audio_device()->StopRecording());
646 EXPECT_FALSE(audio_device()->Recording());
647 }
648
GetMaxSpeakerVolume() const649 int GetMaxSpeakerVolume() const {
650 uint32_t max_volume(0);
651 EXPECT_EQ(0, audio_device()->MaxSpeakerVolume(&max_volume));
652 return max_volume;
653 }
654
GetMinSpeakerVolume() const655 int GetMinSpeakerVolume() const {
656 uint32_t min_volume(0);
657 EXPECT_EQ(0, audio_device()->MinSpeakerVolume(&min_volume));
658 return min_volume;
659 }
660
GetSpeakerVolume() const661 int GetSpeakerVolume() const {
662 uint32_t volume(0);
663 EXPECT_EQ(0, audio_device()->SpeakerVolume(&volume));
664 return volume;
665 }
666
667 std::unique_ptr<EventWrapper> test_is_done_;
668 rtc::scoped_refptr<AudioDeviceModule> audio_device_;
669 AudioParameters playout_parameters_;
670 AudioParameters record_parameters_;
671 std::unique_ptr<BuildInfo> build_info_;
672 };
673
TEST_F(AudioDeviceTest,ConstructDestruct)674 TEST_F(AudioDeviceTest, ConstructDestruct) {
675 // Using the test fixture to create and destruct the audio device module.
676 }
677
678 // We always ask for a default audio layer when the ADM is constructed. But the
679 // ADM will then internally set the best suitable combination of audio layers,
680 // for input and output based on if low-latency output and/or input audio in
681 // combination with OpenSL ES is supported or not. This test ensures that the
682 // correct selection is done.
TEST_F(AudioDeviceTest,VerifyDefaultAudioLayer)683 TEST_F(AudioDeviceTest, VerifyDefaultAudioLayer) {
684 const AudioDeviceModule::AudioLayer audio_layer = GetActiveAudioLayer();
685 bool low_latency_output = audio_manager()->IsLowLatencyPlayoutSupported();
686 bool low_latency_input = audio_manager()->IsLowLatencyRecordSupported();
687 AudioDeviceModule::AudioLayer expected_audio_layer;
688 if (low_latency_output && low_latency_input) {
689 expected_audio_layer = AudioDeviceModule::kAndroidOpenSLESAudio;
690 } else if (low_latency_output && !low_latency_input) {
691 expected_audio_layer =
692 AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio;
693 } else {
694 expected_audio_layer = AudioDeviceModule::kAndroidJavaAudio;
695 }
696 EXPECT_EQ(expected_audio_layer, audio_layer);
697 }
698
699 // Verify that it is possible to explicitly create the two types of supported
700 // ADMs. These two tests overrides the default selection of native audio layer
701 // by ignoring if the device supports low-latency output or not.
TEST_F(AudioDeviceTest,CorrectAudioLayerIsUsedForCombinedJavaOpenSLCombo)702 TEST_F(AudioDeviceTest, CorrectAudioLayerIsUsedForCombinedJavaOpenSLCombo) {
703 AudioDeviceModule::AudioLayer expected_layer =
704 AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio;
705 AudioDeviceModule::AudioLayer active_layer = TestActiveAudioLayer(
706 expected_layer);
707 EXPECT_EQ(expected_layer, active_layer);
708 }
709
TEST_F(AudioDeviceTest,CorrectAudioLayerIsUsedForJavaInBothDirections)710 TEST_F(AudioDeviceTest, CorrectAudioLayerIsUsedForJavaInBothDirections) {
711 AudioDeviceModule::AudioLayer expected_layer =
712 AudioDeviceModule::kAndroidJavaAudio;
713 AudioDeviceModule::AudioLayer active_layer = TestActiveAudioLayer(
714 expected_layer);
715 EXPECT_EQ(expected_layer, active_layer);
716 }
717
TEST_F(AudioDeviceTest,CorrectAudioLayerIsUsedForOpenSLInBothDirections)718 TEST_F(AudioDeviceTest, CorrectAudioLayerIsUsedForOpenSLInBothDirections) {
719 AudioDeviceModule::AudioLayer expected_layer =
720 AudioDeviceModule::kAndroidOpenSLESAudio;
721 AudioDeviceModule::AudioLayer active_layer =
722 TestActiveAudioLayer(expected_layer);
723 EXPECT_EQ(expected_layer, active_layer);
724 }
725
726 // The Android ADM supports two different delay reporting modes. One for the
727 // low-latency output path (in combination with OpenSL ES), and one for the
728 // high-latency output path (Java backends in both directions). These two tests
729 // verifies that the audio manager reports correct delay estimate given the
730 // selected audio layer. Note that, this delay estimate will only be utilized
731 // if the HW AEC is disabled.
TEST_F(AudioDeviceTest,UsesCorrectDelayEstimateForHighLatencyOutputPath)732 TEST_F(AudioDeviceTest, UsesCorrectDelayEstimateForHighLatencyOutputPath) {
733 EXPECT_EQ(kHighLatencyModeDelayEstimateInMilliseconds,
734 TestDelayOnAudioLayer(AudioDeviceModule::kAndroidJavaAudio));
735 }
736
TEST_F(AudioDeviceTest,UsesCorrectDelayEstimateForLowLatencyOutputPath)737 TEST_F(AudioDeviceTest, UsesCorrectDelayEstimateForLowLatencyOutputPath) {
738 EXPECT_EQ(kLowLatencyModeDelayEstimateInMilliseconds,
739 TestDelayOnAudioLayer(
740 AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio));
741 }
742
743 // Ensure that the ADM internal audio device buffer is configured to use the
744 // correct set of parameters.
TEST_F(AudioDeviceTest,VerifyAudioDeviceBufferParameters)745 TEST_F(AudioDeviceTest, VerifyAudioDeviceBufferParameters) {
746 EXPECT_EQ(playout_parameters_.sample_rate(),
747 audio_device_buffer()->PlayoutSampleRate());
748 EXPECT_EQ(record_parameters_.sample_rate(),
749 audio_device_buffer()->RecordingSampleRate());
750 EXPECT_EQ(playout_parameters_.channels(),
751 audio_device_buffer()->PlayoutChannels());
752 EXPECT_EQ(record_parameters_.channels(),
753 audio_device_buffer()->RecordingChannels());
754 }
755
756
TEST_F(AudioDeviceTest,InitTerminate)757 TEST_F(AudioDeviceTest, InitTerminate) {
758 // Initialization is part of the test fixture.
759 EXPECT_TRUE(audio_device()->Initialized());
760 EXPECT_EQ(0, audio_device()->Terminate());
761 EXPECT_FALSE(audio_device()->Initialized());
762 }
763
TEST_F(AudioDeviceTest,Devices)764 TEST_F(AudioDeviceTest, Devices) {
765 // Device enumeration is not supported. Verify fixed values only.
766 EXPECT_EQ(1, audio_device()->PlayoutDevices());
767 EXPECT_EQ(1, audio_device()->RecordingDevices());
768 }
769
TEST_F(AudioDeviceTest,SpeakerVolumeShouldBeAvailable)770 TEST_F(AudioDeviceTest, SpeakerVolumeShouldBeAvailable) {
771 // The OpenSL ES output audio path does not support volume control.
772 if (!AudioLayerSupportsVolumeControl())
773 return;
774 bool available;
775 EXPECT_EQ(0, audio_device()->SpeakerVolumeIsAvailable(&available));
776 EXPECT_TRUE(available);
777 }
778
TEST_F(AudioDeviceTest,MaxSpeakerVolumeIsPositive)779 TEST_F(AudioDeviceTest, MaxSpeakerVolumeIsPositive) {
780 // The OpenSL ES output audio path does not support volume control.
781 if (!AudioLayerSupportsVolumeControl())
782 return;
783 StartPlayout();
784 EXPECT_GT(GetMaxSpeakerVolume(), 0);
785 StopPlayout();
786 }
787
TEST_F(AudioDeviceTest,MinSpeakerVolumeIsZero)788 TEST_F(AudioDeviceTest, MinSpeakerVolumeIsZero) {
789 // The OpenSL ES output audio path does not support volume control.
790 if (!AudioLayerSupportsVolumeControl())
791 return;
792 EXPECT_EQ(GetMinSpeakerVolume(), 0);
793 }
794
TEST_F(AudioDeviceTest,DefaultSpeakerVolumeIsWithinMinMax)795 TEST_F(AudioDeviceTest, DefaultSpeakerVolumeIsWithinMinMax) {
796 // The OpenSL ES output audio path does not support volume control.
797 if (!AudioLayerSupportsVolumeControl())
798 return;
799 const int default_volume = GetSpeakerVolume();
800 EXPECT_GE(default_volume, GetMinSpeakerVolume());
801 EXPECT_LE(default_volume, GetMaxSpeakerVolume());
802 }
803
TEST_F(AudioDeviceTest,SetSpeakerVolumeActuallySetsVolume)804 TEST_F(AudioDeviceTest, SetSpeakerVolumeActuallySetsVolume) {
805 // The OpenSL ES output audio path does not support volume control.
806 if (!AudioLayerSupportsVolumeControl())
807 return;
808 const int default_volume = GetSpeakerVolume();
809 const int max_volume = GetMaxSpeakerVolume();
810 EXPECT_EQ(0, audio_device()->SetSpeakerVolume(max_volume));
811 int new_volume = GetSpeakerVolume();
812 EXPECT_EQ(new_volume, max_volume);
813 EXPECT_EQ(0, audio_device()->SetSpeakerVolume(default_volume));
814 }
815
816 // Tests that playout can be initiated, started and stopped. No audio callback
817 // is registered in this test.
TEST_F(AudioDeviceTest,StartStopPlayout)818 TEST_F(AudioDeviceTest, StartStopPlayout) {
819 StartPlayout();
820 StopPlayout();
821 StartPlayout();
822 StopPlayout();
823 }
824
825 // Tests that recording can be initiated, started and stopped. No audio callback
826 // is registered in this test.
TEST_F(AudioDeviceTest,StartStopRecording)827 TEST_F(AudioDeviceTest, StartStopRecording) {
828 StartRecording();
829 StopRecording();
830 StartRecording();
831 StopRecording();
832 }
833
834 // Verify that calling StopPlayout() will leave us in an uninitialized state
835 // which will require a new call to InitPlayout(). This test does not call
836 // StartPlayout() while being uninitialized since doing so will hit a
837 // RTC_DCHECK and death tests are not supported on Android.
TEST_F(AudioDeviceTest,StopPlayoutRequiresInitToRestart)838 TEST_F(AudioDeviceTest, StopPlayoutRequiresInitToRestart) {
839 EXPECT_EQ(0, audio_device()->InitPlayout());
840 EXPECT_EQ(0, audio_device()->StartPlayout());
841 EXPECT_EQ(0, audio_device()->StopPlayout());
842 EXPECT_FALSE(audio_device()->PlayoutIsInitialized());
843 }
844
845 // Verify that calling StopRecording() will leave us in an uninitialized state
846 // which will require a new call to InitRecording(). This test does not call
847 // StartRecording() while being uninitialized since doing so will hit a
848 // RTC_DCHECK and death tests are not supported on Android.
TEST_F(AudioDeviceTest,StopRecordingRequiresInitToRestart)849 TEST_F(AudioDeviceTest, StopRecordingRequiresInitToRestart) {
850 EXPECT_EQ(0, audio_device()->InitRecording());
851 EXPECT_EQ(0, audio_device()->StartRecording());
852 EXPECT_EQ(0, audio_device()->StopRecording());
853 EXPECT_FALSE(audio_device()->RecordingIsInitialized());
854 }
855
856 // Start playout and verify that the native audio layer starts asking for real
857 // audio samples to play out using the NeedMorePlayData callback.
TEST_F(AudioDeviceTest,StartPlayoutVerifyCallbacks)858 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
859 MockAudioTransportAndroid mock(kPlayout);
860 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
861 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
862 kBytesPerSample,
863 playout_channels(),
864 playout_sample_rate(),
865 NotNull(),
866 _, _, _))
867 .Times(AtLeast(kNumCallbacks));
868 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
869 StartPlayout();
870 test_is_done_->Wait(kTestTimeOutInMilliseconds);
871 StopPlayout();
872 }
873
874 // Start recording and verify that the native audio layer starts feeding real
875 // audio samples via the RecordedDataIsAvailable callback.
TEST_F(AudioDeviceTest,StartRecordingVerifyCallbacks)876 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
877 MockAudioTransportAndroid mock(kRecording);
878 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
879 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(),
880 record_frames_per_10ms_buffer(),
881 kBytesPerSample,
882 record_channels(),
883 record_sample_rate(),
884 total_delay_ms(),
885 0,
886 0,
887 false,
888 _))
889 .Times(AtLeast(kNumCallbacks));
890
891 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
892 StartRecording();
893 test_is_done_->Wait(kTestTimeOutInMilliseconds);
894 StopRecording();
895 }
896
897
898 // Start playout and recording (full-duplex audio) and verify that audio is
899 // active in both directions.
TEST_F(AudioDeviceTest,StartPlayoutAndRecordingVerifyCallbacks)900 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
901 MockAudioTransportAndroid mock(kPlayout | kRecording);
902 mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
903 EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
904 kBytesPerSample,
905 playout_channels(),
906 playout_sample_rate(),
907 NotNull(),
908 _, _, _))
909 .Times(AtLeast(kNumCallbacks));
910 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(),
911 record_frames_per_10ms_buffer(),
912 kBytesPerSample,
913 record_channels(),
914 record_sample_rate(),
915 total_delay_ms(),
916 0,
917 0,
918 false,
919 _))
920 .Times(AtLeast(kNumCallbacks));
921 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
922 StartPlayout();
923 StartRecording();
924 test_is_done_->Wait(kTestTimeOutInMilliseconds);
925 StopRecording();
926 StopPlayout();
927 }
928
929 // Start playout and read audio from an external PCM file when the audio layer
930 // asks for data to play out. Real audio is played out in this test but it does
931 // not contain any explicit verification that the audio quality is perfect.
TEST_F(AudioDeviceTest,RunPlayoutWithFileAsSource)932 TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) {
933 // TODO(henrika): extend test when mono output is supported.
934 EXPECT_EQ(1u, playout_channels());
935 NiceMock<MockAudioTransportAndroid> mock(kPlayout);
936 const int num_callbacks = kFilePlayTimeInSec * kNumCallbacksPerSecond;
937 std::string file_name = GetFileName(playout_sample_rate());
938 std::unique_ptr<FileAudioStream> file_audio_stream(
939 new FileAudioStream(num_callbacks, file_name, playout_sample_rate()));
940 mock.HandleCallbacks(test_is_done_.get(),
941 file_audio_stream.get(),
942 num_callbacks);
943 // SetMaxPlayoutVolume();
944 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
945 StartPlayout();
946 test_is_done_->Wait(kTestTimeOutInMilliseconds);
947 StopPlayout();
948 }
949
950 // Start playout and recording and store recorded data in an intermediate FIFO
951 // buffer from which the playout side then reads its samples in the same order
952 // as they were stored. Under ideal circumstances, a callback sequence would
953 // look like: ...+-+-+-+-+-+-+-..., where '+' means 'packet recorded' and '-'
954 // means 'packet played'. Under such conditions, the FIFO would only contain
955 // one packet on average. However, under more realistic conditions, the size
956 // of the FIFO will vary more due to an unbalance between the two sides.
957 // This test tries to verify that the device maintains a balanced callback-
958 // sequence by running in loopback for ten seconds while measuring the size
959 // (max and average) of the FIFO. The size of the FIFO is increased by the
960 // recording side and decreased by the playout side.
961 // TODO(henrika): tune the final test parameters after running tests on several
962 // different devices.
963 // Disabling this test on bots since it is difficult to come up with a robust
964 // test condition that all worked as intended. The main issue is that, when
965 // swarming is used, an initial latency can be built up when the both sides
966 // starts at different times. Hence, the test can fail even if audio works
967 // as intended. Keeping the test so it can be enabled manually.
968 // http://bugs.webrtc.org/7744
TEST_F(AudioDeviceTest,DISABLED_RunPlayoutAndRecordingInFullDuplex)969 TEST_F(AudioDeviceTest, DISABLED_RunPlayoutAndRecordingInFullDuplex) {
970 EXPECT_EQ(record_channels(), playout_channels());
971 EXPECT_EQ(record_sample_rate(), playout_sample_rate());
972 NiceMock<MockAudioTransportAndroid> mock(kPlayout | kRecording);
973 std::unique_ptr<FifoAudioStream> fifo_audio_stream(
974 new FifoAudioStream(playout_frames_per_10ms_buffer()));
975 mock.HandleCallbacks(test_is_done_.get(),
976 fifo_audio_stream.get(),
977 kFullDuplexTimeInSec * kNumCallbacksPerSecond);
978 SetMaxPlayoutVolume();
979 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
980 StartRecording();
981 StartPlayout();
982 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds,
983 1000 * kFullDuplexTimeInSec));
984 StopPlayout();
985 StopRecording();
986
987 // These thresholds are set rather high to accomodate differences in hardware
988 // in several devices, so this test can be used in swarming.
989 // See http://bugs.webrtc.org/6464
990 EXPECT_LE(fifo_audio_stream->average_size(), 60u);
991 EXPECT_LE(fifo_audio_stream->largest_size(), 70u);
992 }
993
994 // Measures loopback latency and reports the min, max and average values for
995 // a full duplex audio session.
996 // The latency is measured like so:
997 // - Insert impulses periodically on the output side.
998 // - Detect the impulses on the input side.
999 // - Measure the time difference between the transmit time and receive time.
1000 // - Store time differences in a vector and calculate min, max and average.
1001 // This test requires a special hardware called Audio Loopback Dongle.
1002 // See http://source.android.com/devices/audio/loopback.html for details.
TEST_F(AudioDeviceTest,DISABLED_MeasureLoopbackLatency)1003 TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) {
1004 EXPECT_EQ(record_channels(), playout_channels());
1005 EXPECT_EQ(record_sample_rate(), playout_sample_rate());
1006 NiceMock<MockAudioTransportAndroid> mock(kPlayout | kRecording);
1007 std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream(
1008 new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer()));
1009 mock.HandleCallbacks(test_is_done_.get(),
1010 latency_audio_stream.get(),
1011 kMeasureLatencyTimeInSec * kNumCallbacksPerSecond);
1012 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
1013 SetMaxPlayoutVolume();
1014 DisableBuiltInAECIfAvailable();
1015 StartRecording();
1016 StartPlayout();
1017 test_is_done_->Wait(std::max(kTestTimeOutInMilliseconds,
1018 1000 * kMeasureLatencyTimeInSec));
1019 StopPlayout();
1020 StopRecording();
1021 // Verify that the correct number of transmitted impulses are detected.
1022 EXPECT_EQ(latency_audio_stream->num_latency_values(),
1023 static_cast<size_t>(
1024 kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1));
1025 latency_audio_stream->PrintResults();
1026 }
1027
1028 } // namespace webrtc
1029