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