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