henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 11 | #include <algorithm> |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 12 | #include <cstring> |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 13 | #include <memory> |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 14 | #include <numeric> |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 15 | |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/array_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_device/audio_device_impl.h" |
| 19 | #include "modules/audio_device/include/audio_device.h" |
| 20 | #include "modules/audio_device/include/mock_audio_transport.h" |
| 21 | #include "rtc_base/buffer.h" |
| 22 | #include "rtc_base/criticalsection.h" |
| 23 | #include "rtc_base/event.h" |
| 24 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 25 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/race_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/scoped_ref_ptr.h" |
| 28 | #include "rtc_base/thread_annotations.h" |
| 29 | #include "rtc_base/thread_checker.h" |
| 30 | #include "rtc_base/timeutils.h" |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 31 | #include "system_wrappers/include/sleep.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "test/gmock.h" |
| 33 | #include "test/gtest.h" |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 34 | #ifdef WEBRTC_WIN |
| 35 | #include "modules/audio_device/include/audio_device_factory.h" |
| 36 | #include "modules/audio_device/win/core_audio_utility_win.h" |
| 37 | #endif |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 38 | |
| 39 | using ::testing::_; |
| 40 | using ::testing::AtLeast; |
| 41 | using ::testing::Ge; |
| 42 | using ::testing::Invoke; |
| 43 | using ::testing::NiceMock; |
| 44 | using ::testing::NotNull; |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 45 | using ::testing::Mock; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 46 | |
| 47 | namespace webrtc { |
| 48 | namespace { |
| 49 | |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 50 | // #define ENABLE_DEBUG_PRINTF |
| 51 | #ifdef ENABLE_DEBUG_PRINTF |
| 52 | #define PRINTD(...) fprintf(stderr, __VA_ARGS__); |
| 53 | #else |
| 54 | #define PRINTD(...) ((void)0) |
| 55 | #endif |
| 56 | #define PRINT(...) fprintf(stderr, __VA_ARGS__); |
| 57 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 58 | // Don't run these tests in combination with sanitizers. |
| 59 | #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) |
| 60 | #define SKIP_TEST_IF_NOT(requirements_satisfied) \ |
| 61 | do { \ |
| 62 | if (!requirements_satisfied) { \ |
| 63 | return; \ |
| 64 | } \ |
| 65 | } while (false) |
| 66 | #else |
| 67 | // Or if other audio-related requirements are not met. |
| 68 | #define SKIP_TEST_IF_NOT(requirements_satisfied) \ |
| 69 | do { \ |
| 70 | return; \ |
| 71 | } while (false) |
| 72 | #endif |
| 73 | |
| 74 | // Number of callbacks (input or output) the tests waits for before we set |
| 75 | // an event indicating that the test was OK. |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 76 | static constexpr size_t kNumCallbacks = 10; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 77 | // Max amount of time we wait for an event to be set while counting callbacks. |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 78 | static constexpr size_t kTestTimeOutInMilliseconds = 10 * 1000; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 79 | // Average number of audio callbacks per second assuming 10ms packet size. |
| 80 | static constexpr size_t kNumCallbacksPerSecond = 100; |
| 81 | // Run the full-duplex test during this time (unit is in seconds). |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 82 | static constexpr size_t kFullDuplexTimeInSec = 5; |
| 83 | // Length of round-trip latency measurements. Number of deteced impulses |
| 84 | // shall be kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1 since the |
| 85 | // last transmitted pulse is not used. |
| 86 | static constexpr size_t kMeasureLatencyTimeInSec = 10; |
| 87 | // Sets the number of impulses per second in the latency test. |
| 88 | static constexpr size_t kImpulseFrequencyInHz = 1; |
| 89 | // Utilized in round-trip latency measurements to avoid capturing noise samples. |
| 90 | static constexpr int kImpulseThreshold = 1000; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 91 | |
| 92 | enum class TransportType { |
| 93 | kInvalid, |
| 94 | kPlay, |
| 95 | kRecord, |
| 96 | kPlayAndRecord, |
| 97 | }; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 98 | |
| 99 | // Interface for processing the audio stream. Real implementations can e.g. |
| 100 | // run audio in loopback, read audio from a file or perform latency |
| 101 | // measurements. |
| 102 | class AudioStream { |
| 103 | public: |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 104 | virtual void Write(rtc::ArrayView<const int16_t> source) = 0; |
| 105 | virtual void Read(rtc::ArrayView<int16_t> destination) = 0; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 106 | |
| 107 | virtual ~AudioStream() = default; |
| 108 | }; |
| 109 | |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 110 | // Converts index corresponding to position within a 10ms buffer into a |
| 111 | // delay value in milliseconds. |
| 112 | // Example: index=240, frames_per_10ms_buffer=480 => 5ms as output. |
| 113 | int IndexToMilliseconds(size_t index, size_t frames_per_10ms_buffer) { |
| 114 | return rtc::checked_cast<int>( |
| 115 | 10.0 * (static_cast<double>(index) / frames_per_10ms_buffer) + 0.5); |
| 116 | } |
| 117 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 118 | } // namespace |
| 119 | |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 120 | // Simple first in first out (FIFO) class that wraps a list of 16-bit audio |
| 121 | // buffers of fixed size and allows Write and Read operations. The idea is to |
| 122 | // store recorded audio buffers (using Write) and then read (using Read) these |
| 123 | // stored buffers with as short delay as possible when the audio layer needs |
| 124 | // data to play out. The number of buffers in the FIFO will stabilize under |
| 125 | // normal conditions since there will be a balance between Write and Read calls. |
| 126 | // The container is a std::list container and access is protected with a lock |
| 127 | // since both sides (playout and recording) are driven by its own thread. |
| 128 | // Note that, we know by design that the size of the audio buffer will not |
| 129 | // change over time and that both sides will use the same size. |
| 130 | class FifoAudioStream : public AudioStream { |
| 131 | public: |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 132 | void Write(rtc::ArrayView<const int16_t> source) override { |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 133 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 134 | const size_t size = [&] { |
| 135 | rtc::CritScope lock(&lock_); |
| 136 | fifo_.push_back(Buffer16(source.data(), source.size())); |
| 137 | return fifo_.size(); |
| 138 | }(); |
| 139 | if (size > max_size_) { |
| 140 | max_size_ = size; |
| 141 | } |
| 142 | // Add marker once per second to signal that audio is active. |
| 143 | if (write_count_++ % 100 == 0) { |
| 144 | PRINT("."); |
| 145 | } |
| 146 | written_elements_ += size; |
| 147 | } |
| 148 | |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 149 | void Read(rtc::ArrayView<int16_t> destination) override { |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 150 | rtc::CritScope lock(&lock_); |
| 151 | if (fifo_.empty()) { |
| 152 | std::fill(destination.begin(), destination.end(), 0); |
| 153 | } else { |
| 154 | const Buffer16& buffer = fifo_.front(); |
| 155 | RTC_CHECK_EQ(buffer.size(), destination.size()); |
| 156 | std::copy(buffer.begin(), buffer.end(), destination.begin()); |
| 157 | fifo_.pop_front(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | size_t size() const { |
| 162 | rtc::CritScope lock(&lock_); |
| 163 | return fifo_.size(); |
| 164 | } |
| 165 | |
| 166 | size_t max_size() const { |
| 167 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 168 | return max_size_; |
| 169 | } |
| 170 | |
| 171 | size_t average_size() const { |
| 172 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 173 | return 0.5 + static_cast<float>(written_elements_ / write_count_); |
| 174 | } |
| 175 | |
| 176 | using Buffer16 = rtc::BufferT<int16_t>; |
| 177 | |
| 178 | rtc::CriticalSection lock_; |
| 179 | rtc::RaceChecker race_checker_; |
| 180 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 181 | std::list<Buffer16> fifo_ RTC_GUARDED_BY(lock_); |
| 182 | size_t write_count_ RTC_GUARDED_BY(race_checker_) = 0; |
| 183 | size_t max_size_ RTC_GUARDED_BY(race_checker_) = 0; |
| 184 | size_t written_elements_ RTC_GUARDED_BY(race_checker_) = 0; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 187 | // Inserts periodic impulses and measures the latency between the time of |
| 188 | // transmission and time of receiving the same impulse. |
| 189 | class LatencyAudioStream : public AudioStream { |
| 190 | public: |
| 191 | LatencyAudioStream() { |
| 192 | // Delay thread checkers from being initialized until first callback from |
| 193 | // respective thread. |
| 194 | read_thread_checker_.DetachFromThread(); |
| 195 | write_thread_checker_.DetachFromThread(); |
| 196 | } |
| 197 | |
| 198 | // Insert periodic impulses in first two samples of |destination|. |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 199 | void Read(rtc::ArrayView<int16_t> destination) override { |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 200 | RTC_DCHECK_RUN_ON(&read_thread_checker_); |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 201 | if (read_count_ == 0) { |
| 202 | PRINT("["); |
| 203 | } |
| 204 | read_count_++; |
| 205 | std::fill(destination.begin(), destination.end(), 0); |
| 206 | if (read_count_ % (kNumCallbacksPerSecond / kImpulseFrequencyInHz) == 0) { |
| 207 | PRINT("."); |
| 208 | { |
| 209 | rtc::CritScope lock(&lock_); |
| 210 | if (!pulse_time_) { |
Oskar Sundbom | 6ad9f26 | 2017-11-16 10:53:39 +0100 | [diff] [blame] | 211 | pulse_time_ = rtc::TimeMillis(); |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | constexpr int16_t impulse = std::numeric_limits<int16_t>::max(); |
| 215 | std::fill_n(destination.begin(), 2, impulse); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Detect received impulses in |source|, derive time between transmission and |
| 220 | // detection and add the calculated delay to list of latencies. |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 221 | void Write(rtc::ArrayView<const int16_t> source) override { |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 222 | RTC_DCHECK_RUN_ON(&write_thread_checker_); |
| 223 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 224 | rtc::CritScope lock(&lock_); |
| 225 | write_count_++; |
| 226 | if (!pulse_time_) { |
| 227 | // Avoid detection of new impulse response until a new impulse has |
| 228 | // been transmitted (sets |pulse_time_| to value larger than zero). |
| 229 | return; |
| 230 | } |
| 231 | // Find index (element position in vector) of the max element. |
| 232 | const size_t index_of_max = |
| 233 | std::max_element(source.begin(), source.end()) - source.begin(); |
| 234 | // Derive time between transmitted pulse and received pulse if the level |
| 235 | // is high enough (removes noise). |
| 236 | const size_t max = source[index_of_max]; |
| 237 | if (max > kImpulseThreshold) { |
| 238 | PRINTD("(%zu, %zu)", max, index_of_max); |
| 239 | int64_t now_time = rtc::TimeMillis(); |
| 240 | int extra_delay = IndexToMilliseconds(index_of_max, source.size()); |
| 241 | PRINTD("[%d]", rtc::checked_cast<int>(now_time - pulse_time_)); |
| 242 | PRINTD("[%d]", extra_delay); |
| 243 | // Total latency is the difference between transmit time and detection |
| 244 | // tome plus the extra delay within the buffer in which we detected the |
| 245 | // received impulse. It is transmitted at sample 0 but can be received |
| 246 | // at sample N where N > 0. The term |extra_delay| accounts for N and it |
| 247 | // is a value between 0 and 10ms. |
| 248 | latencies_.push_back(now_time - *pulse_time_ + extra_delay); |
| 249 | pulse_time_.reset(); |
| 250 | } else { |
| 251 | PRINTD("-"); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | size_t num_latency_values() const { |
| 256 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 257 | return latencies_.size(); |
| 258 | } |
| 259 | |
| 260 | int min_latency() const { |
| 261 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 262 | if (latencies_.empty()) |
| 263 | return 0; |
| 264 | return *std::min_element(latencies_.begin(), latencies_.end()); |
| 265 | } |
| 266 | |
| 267 | int max_latency() const { |
| 268 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 269 | if (latencies_.empty()) |
| 270 | return 0; |
| 271 | return *std::max_element(latencies_.begin(), latencies_.end()); |
| 272 | } |
| 273 | |
| 274 | int average_latency() const { |
| 275 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 276 | if (latencies_.empty()) |
| 277 | return 0; |
| 278 | return 0.5 + static_cast<double>( |
| 279 | std::accumulate(latencies_.begin(), latencies_.end(), 0)) / |
| 280 | latencies_.size(); |
| 281 | } |
| 282 | |
| 283 | void PrintResults() const { |
| 284 | RTC_DCHECK_RUNS_SERIALIZED(&race_checker_); |
| 285 | PRINT("] "); |
| 286 | for (auto it = latencies_.begin(); it != latencies_.end(); ++it) { |
| 287 | PRINTD("%d ", *it); |
| 288 | } |
| 289 | PRINT("\n"); |
| 290 | PRINT("[..........] [min, max, avg]=[%d, %d, %d] ms\n", min_latency(), |
| 291 | max_latency(), average_latency()); |
| 292 | } |
| 293 | |
| 294 | rtc::CriticalSection lock_; |
| 295 | rtc::RaceChecker race_checker_; |
| 296 | rtc::ThreadChecker read_thread_checker_; |
| 297 | rtc::ThreadChecker write_thread_checker_; |
| 298 | |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 299 | absl::optional<int64_t> pulse_time_ RTC_GUARDED_BY(lock_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 300 | std::vector<int> latencies_ RTC_GUARDED_BY(race_checker_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 301 | size_t read_count_ RTC_GUARDED_BY(read_thread_checker_) = 0; |
| 302 | size_t write_count_ RTC_GUARDED_BY(write_thread_checker_) = 0; |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 305 | // Mocks the AudioTransport object and proxies actions for the two callbacks |
| 306 | // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations |
| 307 | // of AudioStreamInterface. |
| 308 | class MockAudioTransport : public test::MockAudioTransport { |
| 309 | public: |
| 310 | explicit MockAudioTransport(TransportType type) : type_(type) {} |
| 311 | ~MockAudioTransport() {} |
| 312 | |
| 313 | // Set default actions of the mock object. We are delegating to fake |
| 314 | // implementation where the number of callbacks is counted and an event |
| 315 | // is set after a certain number of callbacks. Audio parameters are also |
| 316 | // checked. |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 317 | void HandleCallbacks(rtc::Event* event, |
| 318 | AudioStream* audio_stream, |
| 319 | int num_callbacks) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 320 | event_ = event; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 321 | audio_stream_ = audio_stream; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 322 | num_callbacks_ = num_callbacks; |
| 323 | if (play_mode()) { |
| 324 | ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _)) |
| 325 | .WillByDefault( |
| 326 | Invoke(this, &MockAudioTransport::RealNeedMorePlayData)); |
| 327 | } |
| 328 | if (rec_mode()) { |
| 329 | ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _)) |
| 330 | .WillByDefault( |
| 331 | Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable)); |
| 332 | } |
| 333 | } |
| 334 | |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 335 | // Special constructor used in manual tests where the user wants to run audio |
| 336 | // until e.g. a keyboard key is pressed. The event flag is set to nullptr by |
| 337 | // default since it is up to the user to stop the test. See e.g. |
| 338 | // DISABLED_RunPlayoutAndRecordingInFullDuplexAndWaitForEnterKey(). |
| 339 | void HandleCallbacks(AudioStream* audio_stream) { |
| 340 | HandleCallbacks(nullptr, audio_stream, 0); |
| 341 | } |
| 342 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 343 | int32_t RealRecordedDataIsAvailable(const void* audio_buffer, |
| 344 | const size_t samples_per_channel, |
| 345 | const size_t bytes_per_frame, |
| 346 | const size_t channels, |
| 347 | const uint32_t sample_rate, |
| 348 | const uint32_t total_delay_ms, |
| 349 | const int32_t clock_drift, |
| 350 | const uint32_t current_mic_level, |
| 351 | const bool typing_status, |
| 352 | uint32_t& new_mic_level) { |
| 353 | EXPECT_TRUE(rec_mode()) << "No test is expecting these callbacks."; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 354 | // Store audio parameters once in the first callback. For all other |
| 355 | // callbacks, verify that the provided audio parameters are maintained and |
| 356 | // that each callback corresponds to 10ms for any given sample rate. |
| 357 | if (!record_parameters_.is_complete()) { |
| 358 | record_parameters_.reset(sample_rate, channels, samples_per_channel); |
| 359 | } else { |
| 360 | EXPECT_EQ(samples_per_channel, record_parameters_.frames_per_buffer()); |
| 361 | EXPECT_EQ(bytes_per_frame, record_parameters_.GetBytesPerFrame()); |
| 362 | EXPECT_EQ(channels, record_parameters_.channels()); |
| 363 | EXPECT_EQ(static_cast<int>(sample_rate), |
| 364 | record_parameters_.sample_rate()); |
| 365 | EXPECT_EQ(samples_per_channel, |
| 366 | record_parameters_.frames_per_10ms_buffer()); |
| 367 | } |
| 368 | rec_count_++; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 369 | // Write audio data to audio stream object if one has been injected. |
| 370 | if (audio_stream_) { |
| 371 | audio_stream_->Write( |
| 372 | rtc::MakeArrayView(static_cast<const int16_t*>(audio_buffer), |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 373 | samples_per_channel * channels)); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 374 | } |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 375 | // Signal the event after given amount of callbacks. |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 376 | if (event_ && ReceivedEnoughCallbacks()) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 377 | event_->Set(); |
| 378 | } |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | int32_t RealNeedMorePlayData(const size_t samples_per_channel, |
| 383 | const size_t bytes_per_frame, |
| 384 | const size_t channels, |
| 385 | const uint32_t sample_rate, |
| 386 | void* audio_buffer, |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 387 | size_t& samples_out, |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 388 | int64_t* elapsed_time_ms, |
| 389 | int64_t* ntp_time_ms) { |
| 390 | EXPECT_TRUE(play_mode()) << "No test is expecting these callbacks."; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 391 | // Store audio parameters once in the first callback. For all other |
| 392 | // callbacks, verify that the provided audio parameters are maintained and |
| 393 | // that each callback corresponds to 10ms for any given sample rate. |
| 394 | if (!playout_parameters_.is_complete()) { |
| 395 | playout_parameters_.reset(sample_rate, channels, samples_per_channel); |
| 396 | } else { |
| 397 | EXPECT_EQ(samples_per_channel, playout_parameters_.frames_per_buffer()); |
| 398 | EXPECT_EQ(bytes_per_frame, playout_parameters_.GetBytesPerFrame()); |
| 399 | EXPECT_EQ(channels, playout_parameters_.channels()); |
| 400 | EXPECT_EQ(static_cast<int>(sample_rate), |
| 401 | playout_parameters_.sample_rate()); |
| 402 | EXPECT_EQ(samples_per_channel, |
| 403 | playout_parameters_.frames_per_10ms_buffer()); |
| 404 | } |
| 405 | play_count_++; |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 406 | samples_out = samples_per_channel * channels; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 407 | // Read audio data from audio stream object if one has been injected. |
| 408 | if (audio_stream_) { |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 409 | audio_stream_->Read(rtc::MakeArrayView( |
| 410 | static_cast<int16_t*>(audio_buffer), samples_per_channel * channels)); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 411 | } else { |
| 412 | // Fill the audio buffer with zeros to avoid disturbing audio. |
| 413 | const size_t num_bytes = samples_per_channel * bytes_per_frame; |
| 414 | std::memset(audio_buffer, 0, num_bytes); |
| 415 | } |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 416 | // Signal the event after given amount of callbacks. |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 417 | if (event_ && ReceivedEnoughCallbacks()) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 418 | event_->Set(); |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | bool ReceivedEnoughCallbacks() { |
| 424 | bool recording_done = false; |
| 425 | if (rec_mode()) { |
| 426 | recording_done = rec_count_ >= num_callbacks_; |
| 427 | } else { |
| 428 | recording_done = true; |
| 429 | } |
| 430 | bool playout_done = false; |
| 431 | if (play_mode()) { |
| 432 | playout_done = play_count_ >= num_callbacks_; |
| 433 | } else { |
| 434 | playout_done = true; |
| 435 | } |
| 436 | return recording_done && playout_done; |
| 437 | } |
| 438 | |
| 439 | bool play_mode() const { |
| 440 | return type_ == TransportType::kPlay || |
| 441 | type_ == TransportType::kPlayAndRecord; |
| 442 | } |
| 443 | |
| 444 | bool rec_mode() const { |
| 445 | return type_ == TransportType::kRecord || |
| 446 | type_ == TransportType::kPlayAndRecord; |
| 447 | } |
| 448 | |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 449 | void ResetCallbackCounters() { |
| 450 | if (play_mode()) { |
| 451 | play_count_ = 0; |
| 452 | } |
| 453 | if (rec_mode()) { |
| 454 | rec_count_ = 0; |
| 455 | } |
| 456 | } |
| 457 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 458 | private: |
| 459 | TransportType type_ = TransportType::kInvalid; |
| 460 | rtc::Event* event_ = nullptr; |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 461 | AudioStream* audio_stream_ = nullptr; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 462 | size_t num_callbacks_ = 0; |
| 463 | size_t play_count_ = 0; |
| 464 | size_t rec_count_ = 0; |
| 465 | AudioParameters playout_parameters_; |
| 466 | AudioParameters record_parameters_; |
| 467 | }; |
| 468 | |
| 469 | // AudioDeviceTest test fixture. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 470 | class AudioDeviceTest |
| 471 | : public ::testing::TestWithParam<webrtc::AudioDeviceModule::AudioLayer> { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 472 | protected: |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 473 | AudioDeviceTest() : audio_layer_(GetParam()), event_(false, false) { |
Joachim Bauch | 5d2bb36 | 2017-12-20 21:19:49 +0100 | [diff] [blame] | 474 | #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \ |
| 475 | !defined(WEBRTC_DUMMY_AUDIO_BUILD) |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 476 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 477 | // Add extra logging fields here if needed for debugging. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 478 | rtc::LogMessage::LogTimestamps(); |
| 479 | rtc::LogMessage::LogThreads(); |
| 480 | audio_device_ = CreateAudioDevice(); |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 481 | EXPECT_NE(audio_device_.get(), nullptr); |
| 482 | AudioDeviceModule::AudioLayer audio_layer; |
maxmorin | 33bf69a | 2017-03-23 04:06:53 -0700 | [diff] [blame] | 483 | int got_platform_audio_layer = |
| 484 | audio_device_->ActiveAudioLayer(&audio_layer); |
henrika | 919dc2e | 2017-10-12 14:24:55 +0200 | [diff] [blame] | 485 | // First, ensure that a valid audio layer can be activated. |
| 486 | if (got_platform_audio_layer != 0) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 487 | requirements_satisfied_ = false; |
| 488 | } |
henrika | 919dc2e | 2017-10-12 14:24:55 +0200 | [diff] [blame] | 489 | // Next, verify that the ADM can be initialized. |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 490 | if (requirements_satisfied_) { |
henrika | 919dc2e | 2017-10-12 14:24:55 +0200 | [diff] [blame] | 491 | requirements_satisfied_ = (audio_device_->Init() == 0); |
| 492 | } |
| 493 | // Finally, ensure that at least one valid device exists in each direction. |
| 494 | if (requirements_satisfied_) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 495 | const int16_t num_playout_devices = audio_device_->PlayoutDevices(); |
| 496 | const int16_t num_record_devices = audio_device_->RecordingDevices(); |
| 497 | requirements_satisfied_ = |
| 498 | num_playout_devices > 0 && num_record_devices > 0; |
| 499 | } |
| 500 | #else |
| 501 | requirements_satisfied_ = false; |
| 502 | #endif |
| 503 | if (requirements_satisfied_) { |
| 504 | EXPECT_EQ(0, audio_device_->SetPlayoutDevice(0)); |
| 505 | EXPECT_EQ(0, audio_device_->InitSpeaker()); |
| 506 | EXPECT_EQ(0, audio_device_->SetRecordingDevice(0)); |
| 507 | EXPECT_EQ(0, audio_device_->InitMicrophone()); |
| 508 | EXPECT_EQ(0, audio_device_->StereoPlayoutIsAvailable(&stereo_playout_)); |
| 509 | EXPECT_EQ(0, audio_device_->SetStereoPlayout(stereo_playout_)); |
henrika | 0238ba8 | 2017-03-28 04:38:29 -0700 | [diff] [blame] | 510 | // Avoid asking for input stereo support and always record in mono |
| 511 | // since asking can cause issues in combination with remote desktop. |
| 512 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7397 for |
| 513 | // details. |
| 514 | EXPECT_EQ(0, audio_device_->SetStereoRecording(false)); |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | virtual ~AudioDeviceTest() { |
| 519 | if (audio_device_) { |
| 520 | EXPECT_EQ(0, audio_device_->Terminate()); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | bool requirements_satisfied() const { return requirements_satisfied_; } |
| 525 | rtc::Event* event() { return &event_; } |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 526 | AudioDeviceModule::AudioLayer audio_layer() const { return audio_layer_; } |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 527 | |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 528 | // AudioDeviceModuleForTest extends the default ADM interface with some extra |
| 529 | // test methods. Intended for usage in tests only and requires a unique |
| 530 | // factory method. See CreateAudioDevice() for details. |
| 531 | const rtc::scoped_refptr<AudioDeviceModuleForTest>& audio_device() const { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 532 | return audio_device_; |
| 533 | } |
| 534 | |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 535 | rtc::scoped_refptr<AudioDeviceModuleForTest> CreateAudioDevice() { |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 536 | // Use the default factory for kPlatformDefaultAudio and a special factory |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 537 | // CreateWindowsCoreAudioAudioDeviceModuleForTest() for kWindowsCoreAudio2. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 538 | // The value of |audio_layer_| is set at construction by GetParam() and two |
| 539 | // different layers are tested on Windows only. |
| 540 | if (audio_layer_ == AudioDeviceModule::kPlatformDefaultAudio) { |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 541 | return AudioDeviceModule::CreateForTest(audio_layer_); |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 542 | } else if (audio_layer_ == AudioDeviceModule::kWindowsCoreAudio2) { |
| 543 | #ifdef WEBRTC_WIN |
| 544 | // We must initialize the COM library on a thread before we calling any of |
| 545 | // the library functions. All COM functions in the ADM will return |
| 546 | // CO_E_NOTINITIALIZED otherwise. |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 547 | com_initializer_ = absl::make_unique<webrtc_win::ScopedCOMInitializer>( |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 548 | webrtc_win::ScopedCOMInitializer::kMTA); |
| 549 | EXPECT_TRUE(com_initializer_->Succeeded()); |
| 550 | EXPECT_TRUE(webrtc_win::core_audio_utility::IsSupported()); |
| 551 | EXPECT_TRUE(webrtc_win::core_audio_utility::IsMMCSSSupported()); |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 552 | return CreateWindowsCoreAudioAudioDeviceModuleForTest(); |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 553 | #else |
| 554 | return nullptr; |
| 555 | #endif |
| 556 | } else { |
| 557 | return nullptr; |
| 558 | } |
| 559 | } |
| 560 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 561 | void StartPlayout() { |
| 562 | EXPECT_FALSE(audio_device()->Playing()); |
| 563 | EXPECT_EQ(0, audio_device()->InitPlayout()); |
| 564 | EXPECT_TRUE(audio_device()->PlayoutIsInitialized()); |
| 565 | EXPECT_EQ(0, audio_device()->StartPlayout()); |
| 566 | EXPECT_TRUE(audio_device()->Playing()); |
| 567 | } |
| 568 | |
| 569 | void StopPlayout() { |
| 570 | EXPECT_EQ(0, audio_device()->StopPlayout()); |
| 571 | EXPECT_FALSE(audio_device()->Playing()); |
| 572 | EXPECT_FALSE(audio_device()->PlayoutIsInitialized()); |
| 573 | } |
| 574 | |
| 575 | void StartRecording() { |
| 576 | EXPECT_FALSE(audio_device()->Recording()); |
| 577 | EXPECT_EQ(0, audio_device()->InitRecording()); |
| 578 | EXPECT_TRUE(audio_device()->RecordingIsInitialized()); |
| 579 | EXPECT_EQ(0, audio_device()->StartRecording()); |
| 580 | EXPECT_TRUE(audio_device()->Recording()); |
| 581 | } |
| 582 | |
| 583 | void StopRecording() { |
| 584 | EXPECT_EQ(0, audio_device()->StopRecording()); |
| 585 | EXPECT_FALSE(audio_device()->Recording()); |
| 586 | EXPECT_FALSE(audio_device()->RecordingIsInitialized()); |
| 587 | } |
| 588 | |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 589 | bool NewWindowsAudioDeviceModuleIsUsed() { |
| 590 | #ifdef WEBRTC_WIN |
| 591 | AudioDeviceModule::AudioLayer audio_layer; |
| 592 | EXPECT_EQ(0, audio_device()->ActiveAudioLayer(&audio_layer)); |
| 593 | if (audio_layer == AudioDeviceModule::kWindowsCoreAudio2) { |
| 594 | // Default device is always added as first element in the list and the |
| 595 | // default communication device as the second element. Hence, the list |
| 596 | // contains two extra elements in this case. |
| 597 | return true; |
| 598 | } |
| 599 | #endif |
| 600 | return false; |
| 601 | } |
| 602 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 603 | private: |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 604 | #ifdef WEBRTC_WIN |
| 605 | // Windows Core Audio based ADM needs to run on a COM initialized thread. |
| 606 | std::unique_ptr<webrtc_win::ScopedCOMInitializer> com_initializer_; |
| 607 | #endif |
| 608 | AudioDeviceModule::AudioLayer audio_layer_; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 609 | bool requirements_satisfied_ = true; |
| 610 | rtc::Event event_; |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 611 | rtc::scoped_refptr<AudioDeviceModuleForTest> audio_device_; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 612 | bool stereo_playout_ = false; |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 613 | }; |
| 614 | |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 615 | // Instead of using the test fixture, verify that the different factory methods |
| 616 | // work as intended. |
| 617 | TEST(AudioDeviceTestWin, ConstructDestructWithFactory) { |
| 618 | rtc::scoped_refptr<AudioDeviceModule> audio_device; |
| 619 | // The default factory should work for all platforms when a default ADM is |
| 620 | // requested. |
| 621 | audio_device = |
| 622 | AudioDeviceModule::Create(AudioDeviceModule::kPlatformDefaultAudio); |
| 623 | EXPECT_TRUE(audio_device); |
| 624 | audio_device = nullptr; |
| 625 | #ifdef WEBRTC_WIN |
| 626 | // For Windows, the old factory method creates an ADM where the platform- |
| 627 | // specific parts are implemented by an AudioDeviceGeneric object. Verify |
| 628 | // that the old factory can't be used in combination with the latest audio |
| 629 | // layer AudioDeviceModule::kWindowsCoreAudio2. |
| 630 | audio_device = |
| 631 | AudioDeviceModule::Create(AudioDeviceModule::kWindowsCoreAudio2); |
| 632 | EXPECT_FALSE(audio_device); |
| 633 | audio_device = nullptr; |
| 634 | // Instead, ensure that the new dedicated factory method called |
| 635 | // CreateWindowsCoreAudioAudioDeviceModule() can be used on Windows and that |
| 636 | // it sets the audio layer to kWindowsCoreAudio2 implicitly. Note that, the |
| 637 | // new ADM for Windows must be created on a COM thread. |
| 638 | webrtc_win::ScopedCOMInitializer com_initializer( |
| 639 | webrtc_win::ScopedCOMInitializer::kMTA); |
| 640 | EXPECT_TRUE(com_initializer.Succeeded()); |
| 641 | audio_device = CreateWindowsCoreAudioAudioDeviceModule(); |
| 642 | EXPECT_TRUE(audio_device); |
| 643 | AudioDeviceModule::AudioLayer audio_layer; |
| 644 | EXPECT_EQ(0, audio_device->ActiveAudioLayer(&audio_layer)); |
| 645 | EXPECT_EQ(audio_layer, AudioDeviceModule::kWindowsCoreAudio2); |
| 646 | #endif |
| 647 | } |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 648 | |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 649 | // Uses the test fixture to create, initialize and destruct the ADM. |
| 650 | TEST_P(AudioDeviceTest, ConstructDestructDefault) {} |
| 651 | |
| 652 | TEST_P(AudioDeviceTest, InitTerminate) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 653 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 654 | // Initialization is part of the test fixture. |
| 655 | EXPECT_TRUE(audio_device()->Initialized()); |
| 656 | EXPECT_EQ(0, audio_device()->Terminate()); |
| 657 | EXPECT_FALSE(audio_device()->Initialized()); |
| 658 | } |
| 659 | |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 660 | // Enumerate all available and active output devices. |
| 661 | TEST_P(AudioDeviceTest, PlayoutDeviceNames) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 662 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 663 | char device_name[kAdmMaxDeviceNameSize]; |
| 664 | char unique_id[kAdmMaxGuidSize]; |
| 665 | int num_devices = audio_device()->PlayoutDevices(); |
| 666 | if (NewWindowsAudioDeviceModuleIsUsed()) { |
| 667 | num_devices += 2; |
| 668 | } |
| 669 | EXPECT_GT(num_devices, 0); |
| 670 | for (int i = 0; i < num_devices; ++i) { |
| 671 | EXPECT_EQ(0, audio_device()->PlayoutDeviceName(i, device_name, unique_id)); |
| 672 | } |
| 673 | EXPECT_EQ(-1, audio_device()->PlayoutDeviceName(num_devices, device_name, |
| 674 | unique_id)); |
| 675 | } |
| 676 | |
| 677 | // Enumerate all available and active input devices. |
| 678 | TEST_P(AudioDeviceTest, RecordingDeviceNames) { |
| 679 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 680 | char device_name[kAdmMaxDeviceNameSize]; |
| 681 | char unique_id[kAdmMaxGuidSize]; |
| 682 | int num_devices = audio_device()->RecordingDevices(); |
| 683 | if (NewWindowsAudioDeviceModuleIsUsed()) { |
| 684 | num_devices += 2; |
| 685 | } |
| 686 | EXPECT_GT(num_devices, 0); |
| 687 | for (int i = 0; i < num_devices; ++i) { |
| 688 | EXPECT_EQ(0, |
| 689 | audio_device()->RecordingDeviceName(i, device_name, unique_id)); |
| 690 | } |
| 691 | EXPECT_EQ(-1, audio_device()->RecordingDeviceName(num_devices, device_name, |
| 692 | unique_id)); |
| 693 | } |
| 694 | |
| 695 | // Counts number of active output devices and ensure that all can be selected. |
| 696 | TEST_P(AudioDeviceTest, SetPlayoutDevice) { |
| 697 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 698 | int num_devices = audio_device()->PlayoutDevices(); |
| 699 | if (NewWindowsAudioDeviceModuleIsUsed()) { |
| 700 | num_devices += 2; |
| 701 | } |
| 702 | EXPECT_GT(num_devices, 0); |
| 703 | // Verify that all available playout devices can be set (not enabled yet). |
| 704 | for (int i = 0; i < num_devices; ++i) { |
| 705 | EXPECT_EQ(0, audio_device()->SetPlayoutDevice(i)); |
| 706 | } |
| 707 | EXPECT_EQ(-1, audio_device()->SetPlayoutDevice(num_devices)); |
| 708 | #ifdef WEBRTC_WIN |
| 709 | // On Windows, verify the alternative method where the user can select device |
| 710 | // by role. |
| 711 | EXPECT_EQ( |
| 712 | 0, audio_device()->SetPlayoutDevice(AudioDeviceModule::kDefaultDevice)); |
| 713 | EXPECT_EQ(0, audio_device()->SetPlayoutDevice( |
| 714 | AudioDeviceModule::kDefaultCommunicationDevice)); |
| 715 | #endif |
| 716 | } |
| 717 | |
| 718 | // Counts number of active input devices and ensure that all can be selected. |
| 719 | TEST_P(AudioDeviceTest, SetRecordingDevice) { |
| 720 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 721 | int num_devices = audio_device()->RecordingDevices(); |
| 722 | if (NewWindowsAudioDeviceModuleIsUsed()) { |
| 723 | num_devices += 2; |
| 724 | } |
| 725 | EXPECT_GT(num_devices, 0); |
| 726 | // Verify that all available recording devices can be set (not enabled yet). |
| 727 | for (int i = 0; i < num_devices; ++i) { |
| 728 | EXPECT_EQ(0, audio_device()->SetRecordingDevice(i)); |
| 729 | } |
| 730 | EXPECT_EQ(-1, audio_device()->SetRecordingDevice(num_devices)); |
| 731 | #ifdef WEBRTC_WIN |
| 732 | // On Windows, verify the alternative method where the user can select device |
| 733 | // by role. |
| 734 | EXPECT_EQ( |
| 735 | 0, audio_device()->SetRecordingDevice(AudioDeviceModule::kDefaultDevice)); |
| 736 | EXPECT_EQ(0, audio_device()->SetRecordingDevice( |
| 737 | AudioDeviceModule::kDefaultCommunicationDevice)); |
| 738 | #endif |
| 739 | } |
| 740 | |
| 741 | // Tests Start/Stop playout without any registered audio callback. |
| 742 | TEST_P(AudioDeviceTest, StartStopPlayout) { |
| 743 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 744 | StartPlayout(); |
| 745 | StopPlayout(); |
| 746 | } |
| 747 | |
| 748 | // Tests Start/Stop recording without any registered audio callback. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 749 | TEST_P(AudioDeviceTest, StartStopRecording) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 750 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 751 | StartRecording(); |
| 752 | StopRecording(); |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 753 | } |
| 754 | |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 755 | // Tests Init/Stop/Init recording without any registered audio callback. |
| 756 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=8041 for details |
| 757 | // on why this test is useful. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 758 | TEST_P(AudioDeviceTest, InitStopInitRecording) { |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 759 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 760 | EXPECT_EQ(0, audio_device()->InitRecording()); |
| 761 | EXPECT_TRUE(audio_device()->RecordingIsInitialized()); |
| 762 | StopRecording(); |
| 763 | EXPECT_EQ(0, audio_device()->InitRecording()); |
| 764 | StopRecording(); |
| 765 | } |
| 766 | |
| 767 | // Tests Init/Stop/Init recording while playout is active. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 768 | TEST_P(AudioDeviceTest, InitStopInitRecordingWhilePlaying) { |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 769 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 770 | StartPlayout(); |
| 771 | EXPECT_EQ(0, audio_device()->InitRecording()); |
| 772 | EXPECT_TRUE(audio_device()->RecordingIsInitialized()); |
| 773 | StopRecording(); |
| 774 | EXPECT_EQ(0, audio_device()->InitRecording()); |
| 775 | StopRecording(); |
| 776 | StopPlayout(); |
| 777 | } |
| 778 | |
| 779 | // Tests Init/Stop/Init playout without any registered audio callback. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 780 | TEST_P(AudioDeviceTest, InitStopInitPlayout) { |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 781 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 782 | EXPECT_EQ(0, audio_device()->InitPlayout()); |
| 783 | EXPECT_TRUE(audio_device()->PlayoutIsInitialized()); |
| 784 | StopPlayout(); |
| 785 | EXPECT_EQ(0, audio_device()->InitPlayout()); |
| 786 | StopPlayout(); |
| 787 | } |
| 788 | |
| 789 | // Tests Init/Stop/Init playout while recording is active. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 790 | TEST_P(AudioDeviceTest, InitStopInitPlayoutWhileRecording) { |
henrika | 6b3e1a2 | 2017-09-25 16:34:30 +0200 | [diff] [blame] | 791 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 792 | StartRecording(); |
| 793 | EXPECT_EQ(0, audio_device()->InitPlayout()); |
| 794 | EXPECT_TRUE(audio_device()->PlayoutIsInitialized()); |
| 795 | StopPlayout(); |
| 796 | EXPECT_EQ(0, audio_device()->InitPlayout()); |
| 797 | StopPlayout(); |
| 798 | StopRecording(); |
| 799 | } |
| 800 | |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 801 | // TODO(henrika): restart without intermediate destruction is currently only |
| 802 | // supported on Windows. |
| 803 | #ifdef WEBRTC_WIN |
| 804 | // Tests Start/Stop playout followed by a second session (emulates a restart |
| 805 | // triggered by a user using public APIs). |
| 806 | TEST_P(AudioDeviceTest, StartStopPlayoutWithExternalRestart) { |
| 807 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 808 | StartPlayout(); |
| 809 | StopPlayout(); |
| 810 | // Restart playout without destroying the ADM in between. Ensures that we |
| 811 | // support: Init(), Start(), Stop(), Init(), Start(), Stop(). |
| 812 | StartPlayout(); |
| 813 | StopPlayout(); |
| 814 | } |
| 815 | |
| 816 | // Tests Start/Stop recording followed by a second session (emulates a restart |
| 817 | // triggered by a user using public APIs). |
| 818 | TEST_P(AudioDeviceTest, StartStopRecordingWithExternalRestart) { |
| 819 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 820 | StartRecording(); |
| 821 | StopRecording(); |
| 822 | // Restart recording without destroying the ADM in between. Ensures that we |
| 823 | // support: Init(), Start(), Stop(), Init(), Start(), Stop(). |
| 824 | StartRecording(); |
| 825 | StopRecording(); |
| 826 | } |
| 827 | |
| 828 | // Tests Start/Stop playout followed by a second session (emulates a restart |
| 829 | // triggered by an internal callback e.g. corresponding to a device switch). |
| 830 | // Note that, internal restart is only supported in combination with the latest |
| 831 | // Windows ADM. |
| 832 | TEST_P(AudioDeviceTest, StartStopPlayoutWithInternalRestart) { |
| 833 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 834 | if (audio_layer() != AudioDeviceModule::kWindowsCoreAudio2) { |
| 835 | return; |
| 836 | } |
| 837 | MockAudioTransport mock(TransportType::kPlay); |
| 838 | mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
| 839 | EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) |
| 840 | .Times(AtLeast(kNumCallbacks)); |
| 841 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 842 | StartPlayout(); |
| 843 | event()->Wait(kTestTimeOutInMilliseconds); |
| 844 | EXPECT_TRUE(audio_device()->Playing()); |
| 845 | // Restart playout but without stopping the internal audio thread. |
| 846 | // This procedure uses a non-public test API and it emulates what happens |
| 847 | // inside the ADM when e.g. a device is removed. |
| 848 | EXPECT_EQ(0, audio_device()->RestartPlayoutInternally()); |
| 849 | |
| 850 | // Run basic tests of public APIs while a restart attempt is active. |
| 851 | // These calls should now be very thin and not trigger any new actions. |
| 852 | EXPECT_EQ(-1, audio_device()->StopPlayout()); |
| 853 | EXPECT_TRUE(audio_device()->Playing()); |
| 854 | EXPECT_TRUE(audio_device()->PlayoutIsInitialized()); |
| 855 | EXPECT_EQ(0, audio_device()->InitPlayout()); |
| 856 | EXPECT_EQ(0, audio_device()->StartPlayout()); |
| 857 | |
| 858 | // Wait until audio has restarted and a new sequence of audio callbacks |
| 859 | // becomes active. |
| 860 | // TODO(henrika): is it possible to verify that the internal state transition |
| 861 | // is Stop->Init->Start? |
| 862 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&mock)); |
| 863 | mock.ResetCallbackCounters(); |
| 864 | EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) |
| 865 | .Times(AtLeast(kNumCallbacks)); |
| 866 | event()->Wait(kTestTimeOutInMilliseconds); |
| 867 | EXPECT_TRUE(audio_device()->Playing()); |
| 868 | // Stop playout and the audio thread after successful internal restart. |
| 869 | StopPlayout(); |
| 870 | } |
| 871 | |
| 872 | // Tests Start/Stop recording followed by a second session (emulates a restart |
| 873 | // triggered by an internal callback e.g. corresponding to a device switch). |
| 874 | // Note that, internal restart is only supported in combination with the latest |
| 875 | // Windows ADM. |
| 876 | TEST_P(AudioDeviceTest, StartStopRecordingWithInternalRestart) { |
| 877 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 878 | if (audio_layer() != AudioDeviceModule::kWindowsCoreAudio2) { |
| 879 | return; |
| 880 | } |
| 881 | MockAudioTransport mock(TransportType::kRecord); |
| 882 | mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
| 883 | EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, |
| 884 | false, _)) |
| 885 | .Times(AtLeast(kNumCallbacks)); |
| 886 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 887 | StartRecording(); |
| 888 | event()->Wait(kTestTimeOutInMilliseconds); |
| 889 | EXPECT_TRUE(audio_device()->Recording()); |
| 890 | // Restart recording but without stopping the internal audio thread. |
| 891 | // This procedure uses a non-public test API and it emulates what happens |
| 892 | // inside the ADM when e.g. a device is removed. |
| 893 | EXPECT_EQ(0, audio_device()->RestartRecordingInternally()); |
| 894 | |
| 895 | // Run basic tests of public APIs while a restart attempt is active. |
| 896 | // These calls should now be very thin and not trigger any new actions. |
| 897 | EXPECT_EQ(-1, audio_device()->StopRecording()); |
| 898 | EXPECT_TRUE(audio_device()->Recording()); |
| 899 | EXPECT_TRUE(audio_device()->RecordingIsInitialized()); |
| 900 | EXPECT_EQ(0, audio_device()->InitRecording()); |
| 901 | EXPECT_EQ(0, audio_device()->StartRecording()); |
| 902 | |
| 903 | // Wait until audio has restarted and a new sequence of audio callbacks |
| 904 | // becomes active. |
| 905 | // TODO(henrika): is it possible to verify that the internal state transition |
| 906 | // is Stop->Init->Start? |
| 907 | ASSERT_TRUE(Mock::VerifyAndClearExpectations(&mock)); |
| 908 | mock.ResetCallbackCounters(); |
| 909 | EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, |
| 910 | false, _)) |
| 911 | .Times(AtLeast(kNumCallbacks)); |
| 912 | event()->Wait(kTestTimeOutInMilliseconds); |
| 913 | EXPECT_TRUE(audio_device()->Recording()); |
| 914 | // Stop recording and the audio thread after successful internal restart. |
| 915 | StopRecording(); |
| 916 | } |
| 917 | #endif // #ifdef WEBRTC_WIN |
| 918 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 919 | // Start playout and verify that the native audio layer starts asking for real |
| 920 | // audio samples to play out using the NeedMorePlayData() callback. |
| 921 | // Note that we can't add expectations on audio parameters in EXPECT_CALL |
| 922 | // since parameter are not provided in the each callback. We therefore test and |
| 923 | // verify the parameters in the fake audio transport implementation instead. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 924 | TEST_P(AudioDeviceTest, StartPlayoutVerifyCallbacks) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 925 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 926 | MockAudioTransport mock(TransportType::kPlay); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 927 | mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 928 | EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) |
| 929 | .Times(AtLeast(kNumCallbacks)); |
| 930 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 931 | StartPlayout(); |
| 932 | event()->Wait(kTestTimeOutInMilliseconds); |
| 933 | StopPlayout(); |
| 934 | } |
| 935 | |
| 936 | // Start recording and verify that the native audio layer starts providing real |
| 937 | // audio samples using the RecordedDataIsAvailable() callback. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 938 | TEST_P(AudioDeviceTest, StartRecordingVerifyCallbacks) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 939 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 940 | MockAudioTransport mock(TransportType::kRecord); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 941 | mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 942 | EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, |
| 943 | false, _)) |
| 944 | .Times(AtLeast(kNumCallbacks)); |
| 945 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 946 | StartRecording(); |
| 947 | event()->Wait(kTestTimeOutInMilliseconds); |
| 948 | StopRecording(); |
| 949 | } |
| 950 | |
| 951 | // Start playout and recording (full-duplex audio) and verify that audio is |
| 952 | // active in both directions. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 953 | TEST_P(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) { |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 954 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 955 | MockAudioTransport mock(TransportType::kPlayAndRecord); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 956 | mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 957 | EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) |
| 958 | .Times(AtLeast(kNumCallbacks)); |
| 959 | EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, |
| 960 | false, _)) |
| 961 | .Times(AtLeast(kNumCallbacks)); |
| 962 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 963 | StartPlayout(); |
| 964 | StartRecording(); |
| 965 | event()->Wait(kTestTimeOutInMilliseconds); |
| 966 | StopRecording(); |
| 967 | StopPlayout(); |
| 968 | } |
| 969 | |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 970 | // Start playout and recording and store recorded data in an intermediate FIFO |
| 971 | // buffer from which the playout side then reads its samples in the same order |
| 972 | // as they were stored. Under ideal circumstances, a callback sequence would |
| 973 | // look like: ...+-+-+-+-+-+-+-..., where '+' means 'packet recorded' and '-' |
| 974 | // means 'packet played'. Under such conditions, the FIFO would contain max 1, |
| 975 | // with an average somewhere in (0,1) depending on how long the packets are |
| 976 | // buffered. However, under more realistic conditions, the size |
| 977 | // of the FIFO will vary more due to an unbalance between the two sides. |
| 978 | // This test tries to verify that the device maintains a balanced callback- |
| 979 | // sequence by running in loopback for a few seconds while measuring the size |
| 980 | // (max and average) of the FIFO. The size of the FIFO is increased by the |
| 981 | // recording side and decreased by the playout side. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 982 | TEST_P(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) { |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 983 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 984 | NiceMock<MockAudioTransport> mock(TransportType::kPlayAndRecord); |
| 985 | FifoAudioStream audio_stream; |
| 986 | mock.HandleCallbacks(event(), &audio_stream, |
| 987 | kFullDuplexTimeInSec * kNumCallbacksPerSecond); |
| 988 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 989 | // Run both sides using the same channel configuration to avoid conversions |
| 990 | // between mono/stereo while running in full duplex mode. Also, some devices |
| 991 | // (mainly on Windows) do not support mono. |
| 992 | EXPECT_EQ(0, audio_device()->SetStereoPlayout(true)); |
| 993 | EXPECT_EQ(0, audio_device()->SetStereoRecording(true)); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 994 | StartPlayout(); |
| 995 | StartRecording(); |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 996 | event()->Wait(static_cast<int>( |
| 997 | std::max(kTestTimeOutInMilliseconds, 1000 * kFullDuplexTimeInSec))); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 998 | StopRecording(); |
| 999 | StopPlayout(); |
| 1000 | // This thresholds is set rather high to accommodate differences in hardware |
| 1001 | // in several devices. The main idea is to capture cases where a very large |
henrika | b6ca721 | 2017-10-06 12:47:52 +0200 | [diff] [blame] | 1002 | // latency is built up. See http://bugs.webrtc.org/7744 for examples on |
| 1003 | // bots where relatively large average latencies can happen. |
| 1004 | EXPECT_LE(audio_stream.average_size(), 25u); |
henrika | e24991d | 2017-04-06 01:14:23 -0700 | [diff] [blame] | 1005 | PRINT("\n"); |
| 1006 | } |
| 1007 | |
henrika | 5b6afc0 | 2018-09-05 14:34:40 +0200 | [diff] [blame] | 1008 | // Runs audio in full duplex until user hits Enter. Intended as a manual test |
| 1009 | // to ensure that the audio quality is good and that real device switches works |
| 1010 | // as intended. |
| 1011 | TEST_P(AudioDeviceTest, |
| 1012 | DISABLED_RunPlayoutAndRecordingInFullDuplexAndWaitForEnterKey) { |
| 1013 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 1014 | if (audio_layer() != AudioDeviceModule::kWindowsCoreAudio2) { |
| 1015 | return; |
| 1016 | } |
| 1017 | NiceMock<MockAudioTransport> mock(TransportType::kPlayAndRecord); |
| 1018 | FifoAudioStream audio_stream; |
| 1019 | mock.HandleCallbacks(&audio_stream); |
| 1020 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 1021 | EXPECT_EQ(0, audio_device()->SetStereoPlayout(true)); |
| 1022 | EXPECT_EQ(0, audio_device()->SetStereoRecording(true)); |
| 1023 | // Ensure that the sample rate for both directions are identical so that we |
| 1024 | // always can listen to our own voice. Will lead to rate conversion (and |
| 1025 | // higher latency) if the native sample rate is not 48kHz. |
| 1026 | EXPECT_EQ(0, audio_device()->SetPlayoutSampleRate(48000)); |
| 1027 | EXPECT_EQ(0, audio_device()->SetRecordingSampleRate(48000)); |
| 1028 | StartPlayout(); |
| 1029 | StartRecording(); |
| 1030 | do { |
| 1031 | PRINT("Loopback audio is active at 48kHz. Press Enter to stop.\n"); |
| 1032 | } while (getchar() != '\n'); |
| 1033 | StopRecording(); |
| 1034 | StopPlayout(); |
| 1035 | } |
| 1036 | |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 1037 | // Measures loopback latency and reports the min, max and average values for |
| 1038 | // a full duplex audio session. |
| 1039 | // The latency is measured like so: |
| 1040 | // - Insert impulses periodically on the output side. |
| 1041 | // - Detect the impulses on the input side. |
| 1042 | // - Measure the time difference between the transmit time and receive time. |
| 1043 | // - Store time differences in a vector and calculate min, max and average. |
| 1044 | // This test needs the '--gtest_also_run_disabled_tests' flag to run and also |
| 1045 | // some sort of audio feedback loop. E.g. a headset where the mic is placed |
| 1046 | // close to the speaker to ensure highest possible echo. It is also recommended |
| 1047 | // to run the test at highest possible output volume. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 1048 | TEST_P(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) { |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 1049 | SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 1050 | NiceMock<MockAudioTransport> mock(TransportType::kPlayAndRecord); |
| 1051 | LatencyAudioStream audio_stream; |
| 1052 | mock.HandleCallbacks(event(), &audio_stream, |
| 1053 | kMeasureLatencyTimeInSec * kNumCallbacksPerSecond); |
| 1054 | EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
henrika | eb98c72 | 2018-03-20 12:54:07 +0100 | [diff] [blame] | 1055 | EXPECT_EQ(0, audio_device()->SetStereoPlayout(true)); |
| 1056 | EXPECT_EQ(0, audio_device()->SetStereoRecording(true)); |
henrika | 714e5cd | 2017-04-20 08:03:11 -0700 | [diff] [blame] | 1057 | StartPlayout(); |
| 1058 | StartRecording(); |
| 1059 | event()->Wait(static_cast<int>( |
| 1060 | std::max(kTestTimeOutInMilliseconds, 1000 * kMeasureLatencyTimeInSec))); |
| 1061 | StopRecording(); |
| 1062 | StopPlayout(); |
| 1063 | // Verify that the correct number of transmitted impulses are detected. |
| 1064 | EXPECT_EQ(audio_stream.num_latency_values(), |
| 1065 | static_cast<size_t>( |
| 1066 | kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1)); |
| 1067 | // Print out min, max and average delay values for debugging purposes. |
| 1068 | audio_stream.PrintResults(); |
| 1069 | } |
| 1070 | |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 1071 | #ifdef WEBRTC_WIN |
| 1072 | // Test two different audio layers (or rather two different Core Audio |
| 1073 | // implementations) for Windows. |
| 1074 | INSTANTIATE_TEST_CASE_P( |
| 1075 | AudioLayerWin, |
| 1076 | AudioDeviceTest, |
| 1077 | ::testing::Values(AudioDeviceModule::kPlatformDefaultAudio, |
| 1078 | AudioDeviceModule::kWindowsCoreAudio2)); |
| 1079 | #else |
| 1080 | // For all platforms but Windows, only test the default audio layer. |
| 1081 | INSTANTIATE_TEST_CASE_P( |
| 1082 | AudioLayer, |
| 1083 | AudioDeviceTest, |
| 1084 | ::testing::Values(AudioDeviceModule::kPlatformDefaultAudio)); |
| 1085 | #endif |
| 1086 | |
henrika | f2f91fa | 2017-03-17 04:26:22 -0700 | [diff] [blame] | 1087 | } // namespace webrtc |