Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <cstring> | 11 #include <cstring> |
| 12 | 12 |
| 13 #include "webrtc/base/array_view.h" | |
| 14 #include "webrtc/base/buffer.h" | |
| 15 #include "webrtc/base/criticalsection.h" | |
| 13 #include "webrtc/base/event.h" | 16 #include "webrtc/base/event.h" |
| 14 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/base/scoped_ref_ptr.h" | 18 #include "webrtc/base/scoped_ref_ptr.h" |
| 19 #include "webrtc/base/thread_annotations.h" | |
| 16 #include "webrtc/modules/audio_device/audio_device_impl.h" | 20 #include "webrtc/modules/audio_device/audio_device_impl.h" |
| 17 #include "webrtc/modules/audio_device/include/audio_device.h" | 21 #include "webrtc/modules/audio_device/include/audio_device.h" |
| 18 #include "webrtc/modules/audio_device/include/mock_audio_transport.h" | 22 #include "webrtc/modules/audio_device/include/mock_audio_transport.h" |
| 19 #include "webrtc/system_wrappers/include/sleep.h" | 23 #include "webrtc/system_wrappers/include/sleep.h" |
| 20 #include "webrtc/test/gmock.h" | 24 #include "webrtc/test/gmock.h" |
| 21 #include "webrtc/test/gtest.h" | 25 #include "webrtc/test/gtest.h" |
| 22 | 26 |
| 23 using ::testing::_; | 27 using ::testing::_; |
| 24 using ::testing::AtLeast; | 28 using ::testing::AtLeast; |
| 25 using ::testing::Ge; | 29 using ::testing::Ge; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 41 #else | 45 #else |
| 42 // Or if other audio-related requirements are not met. | 46 // Or if other audio-related requirements are not met. |
| 43 #define SKIP_TEST_IF_NOT(requirements_satisfied) \ | 47 #define SKIP_TEST_IF_NOT(requirements_satisfied) \ |
| 44 do { \ | 48 do { \ |
| 45 return; \ | 49 return; \ |
| 46 } while (false) | 50 } while (false) |
| 47 #endif | 51 #endif |
| 48 | 52 |
| 49 // Number of callbacks (input or output) the tests waits for before we set | 53 // Number of callbacks (input or output) the tests waits for before we set |
| 50 // an event indicating that the test was OK. | 54 // an event indicating that the test was OK. |
| 51 static const size_t kNumCallbacks = 10; | 55 static constexpr size_t kNumCallbacks = 10; |
| 52 // Max amount of time we wait for an event to be set while counting callbacks. | 56 // Max amount of time we wait for an event to be set while counting callbacks. |
| 53 static const int kTestTimeOutInMilliseconds = 10 * 1000; | 57 static constexpr int kTestTimeOutInMilliseconds = 10 * 1000; |
| 58 // Average number of audio callbacks per second assuming 10ms packet size. | |
| 59 static constexpr size_t kNumCallbacksPerSecond = 100; | |
| 60 // Run the full-duplex test during this time (unit is in seconds). | |
| 61 static constexpr int kFullDuplexTimeInSec = 5; | |
| 54 | 62 |
| 55 enum class TransportType { | 63 enum class TransportType { |
| 56 kInvalid, | 64 kInvalid, |
| 57 kPlay, | 65 kPlay, |
| 58 kRecord, | 66 kRecord, |
| 59 kPlayAndRecord, | 67 kPlayAndRecord, |
| 60 }; | 68 }; |
| 69 | |
| 70 // Interface for processing the audio stream. Real implementations can e.g. | |
| 71 // run audio in loopback, read audio from a file or perform latency | |
| 72 // measurements. | |
| 73 class AudioStream { | |
| 74 public: | |
| 75 virtual void Write(rtc::ArrayView<const int16_t> source, size_t channels) = 0; | |
| 76 virtual void Read(rtc::ArrayView<int16_t> destination, size_t channels) = 0; | |
| 77 | |
| 78 virtual ~AudioStream() = default; | |
| 79 }; | |
| 80 | |
| 61 } // namespace | 81 } // namespace |
| 62 | 82 |
| 83 // Simple first in first out (FIFO) class that wraps a list of 16-bit audio | |
| 84 // buffers of fixed size and allows Write and Read operations. The idea is to | |
| 85 // store recorded audio buffers (using Write) and then read (using Read) these | |
| 86 // stored buffers with as short delay as possible when the audio layer needs | |
| 87 // data to play out. The number of buffers in the FIFO will stabilize under | |
| 88 // normal conditions since there will be a balance between Write and Read calls. | |
| 89 // The container is a std::list container and access is protected with a lock | |
| 90 // since both sides (playout and recording) are driven by its own thread. | |
| 91 // Note that, we know by design that the size of the audio buffer will not | |
| 92 // change over time and that both sides will use the same size. | |
| 93 class FifoAudioStream : public AudioStream { | |
| 94 public: | |
| 95 void Write(rtc::ArrayView<const int16_t> source, size_t channels) override { | |
| 96 EXPECT_EQ(channels, 1u); | |
| 97 Buffer16* buffer = new Buffer16(source.data(), source.size()); | |
|
kwiberg-webrtc
2017/04/05 09:29:12
Here's a raw pointer that owns stuff. I would advi
henrika_webrtc
2017/04/05 14:20:59
Done.
| |
| 98 const size_t size = [&] { | |
| 99 rtc::CritScope lock(&lock_); | |
| 100 fifo_.push_back(buffer); | |
| 101 return fifo_.size(); | |
| 102 }(); | |
| 103 if (size > max_size_) { | |
| 104 max_size_ = size; | |
| 105 } | |
| 106 write_count_++; | |
| 107 written_elements_ += size; | |
| 108 } | |
| 109 | |
| 110 void Read(rtc::ArrayView<int16_t> destination, size_t channels) override { | |
| 111 EXPECT_EQ(channels, 1u); | |
| 112 const size_t bytes_per_buffer = sizeof(int16_t) * destination.size(); | |
| 113 rtc::CritScope lock(&lock_); | |
| 114 if (fifo_.empty()) { | |
| 115 memset(destination.data(), 0, bytes_per_buffer); | |
|
kwiberg-webrtc
2017/04/05 09:29:12
std::fill(destination.begin(), destination.end(),
henrika_webrtc
2017/04/05 14:21:00
Done.
| |
| 116 } else { | |
| 117 Buffer16* buffer = fifo_.front(); | |
| 118 memcpy(destination.data(), buffer->data(), bytes_per_buffer); | |
|
kwiberg-webrtc
2017/04/05 09:29:12
RTC_CHECK_EQ(buffer.size(), destination.size());
s
henrika_webrtc
2017/04/05 14:21:00
Done.
| |
| 119 fifo_.pop_front(); | |
|
kwiberg-webrtc
2017/04/05 09:29:12
Here you leak |buffer|. See the comment below on t
henrika_webrtc
2017/04/05 14:20:59
Done.
| |
| 120 } | |
| 121 } | |
| 122 | |
| 123 size_t size() const { | |
| 124 rtc::CritScope lock(&lock_); | |
| 125 return fifo_.size(); | |
| 126 } | |
| 127 | |
| 128 size_t max_size() const { return max_size_; } | |
| 129 | |
| 130 size_t average_size() const { | |
| 131 return 0.5 + static_cast<float>(written_elements_ / write_count_); | |
| 132 } | |
| 133 | |
| 134 using Buffer16 = rtc::BufferT<int16_t>; | |
|
kwiberg-webrtc
2017/04/05 09:29:12
You reduce the name from 21 to 8 characters, but r
henrika_webrtc
2017/04/05 14:20:59
Keeping Buffer16 ;-)
| |
| 135 rtc::CriticalSection lock_; | |
| 136 std::list<Buffer16*> fifo_ GUARDED_BY(lock_); | |
|
kwiberg-webrtc
2017/04/05 09:29:12
Who owns these buffers? (It would make more sense
henrika_webrtc
2017/04/05 14:20:59
Acknowledged.
| |
| 137 // These members are only modified on the playout thread (in | |
| 138 // FifoAudioStream::Write()), and then read by the test method after media | |
| 139 // has been stopped. Hence, no lock is needed. | |
|
kwiberg-webrtc
2017/04/05 09:29:12
OK. Using a race checker to actually verify this c
henrika_webrtc
2017/04/05 14:20:59
Done.
| |
| 140 size_t write_count_ = 0; | |
| 141 size_t max_size_ = 0; | |
| 142 size_t written_elements_ = 0; | |
| 143 }; | |
| 144 | |
| 63 // Mocks the AudioTransport object and proxies actions for the two callbacks | 145 // Mocks the AudioTransport object and proxies actions for the two callbacks |
| 64 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations | 146 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations |
| 65 // of AudioStreamInterface. | 147 // of AudioStreamInterface. |
| 66 class MockAudioTransport : public test::MockAudioTransport { | 148 class MockAudioTransport : public test::MockAudioTransport { |
| 67 public: | 149 public: |
| 68 explicit MockAudioTransport(TransportType type) : type_(type) {} | 150 explicit MockAudioTransport(TransportType type) : type_(type) {} |
| 69 ~MockAudioTransport() {} | 151 ~MockAudioTransport() {} |
| 70 | 152 |
| 71 // Set default actions of the mock object. We are delegating to fake | 153 // Set default actions of the mock object. We are delegating to fake |
| 72 // implementation where the number of callbacks is counted and an event | 154 // implementation where the number of callbacks is counted and an event |
| 73 // is set after a certain number of callbacks. Audio parameters are also | 155 // is set after a certain number of callbacks. Audio parameters are also |
| 74 // checked. | 156 // checked. |
| 75 void HandleCallbacks(rtc::Event* event, int num_callbacks) { | 157 void HandleCallbacks(rtc::Event* event, |
| 158 AudioStream* audio_stream, | |
| 159 int num_callbacks) { | |
| 76 event_ = event; | 160 event_ = event; |
| 161 audio_stream_ = audio_stream; | |
| 77 num_callbacks_ = num_callbacks; | 162 num_callbacks_ = num_callbacks; |
| 78 if (play_mode()) { | 163 if (play_mode()) { |
| 79 ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _)) | 164 ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _)) |
| 80 .WillByDefault( | 165 .WillByDefault( |
| 81 Invoke(this, &MockAudioTransport::RealNeedMorePlayData)); | 166 Invoke(this, &MockAudioTransport::RealNeedMorePlayData)); |
| 82 } | 167 } |
| 83 if (rec_mode()) { | 168 if (rec_mode()) { |
| 84 ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _)) | 169 ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _)) |
| 85 .WillByDefault( | 170 .WillByDefault( |
| 86 Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable)); | 171 Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable)); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 107 } else { | 192 } else { |
| 108 EXPECT_EQ(samples_per_channel, record_parameters_.frames_per_buffer()); | 193 EXPECT_EQ(samples_per_channel, record_parameters_.frames_per_buffer()); |
| 109 EXPECT_EQ(bytes_per_frame, record_parameters_.GetBytesPerFrame()); | 194 EXPECT_EQ(bytes_per_frame, record_parameters_.GetBytesPerFrame()); |
| 110 EXPECT_EQ(channels, record_parameters_.channels()); | 195 EXPECT_EQ(channels, record_parameters_.channels()); |
| 111 EXPECT_EQ(static_cast<int>(sample_rate), | 196 EXPECT_EQ(static_cast<int>(sample_rate), |
| 112 record_parameters_.sample_rate()); | 197 record_parameters_.sample_rate()); |
| 113 EXPECT_EQ(samples_per_channel, | 198 EXPECT_EQ(samples_per_channel, |
| 114 record_parameters_.frames_per_10ms_buffer()); | 199 record_parameters_.frames_per_10ms_buffer()); |
| 115 } | 200 } |
| 116 rec_count_++; | 201 rec_count_++; |
| 202 // Write audio data to audio stream object if one has been injected. | |
| 203 if (audio_stream_) { | |
| 204 audio_stream_->Write( | |
| 205 rtc::MakeArrayView(static_cast<const int16_t*>(audio_buffer), | |
| 206 samples_per_channel * channels), | |
| 207 channels); | |
| 208 } | |
| 117 // Signal the event after given amount of callbacks. | 209 // Signal the event after given amount of callbacks. |
| 118 if (ReceivedEnoughCallbacks()) { | 210 if (ReceivedEnoughCallbacks()) { |
| 119 event_->Set(); | 211 event_->Set(); |
| 120 } | 212 } |
| 121 return 0; | 213 return 0; |
| 122 } | 214 } |
| 123 | 215 |
| 124 int32_t RealNeedMorePlayData(const size_t samples_per_channel, | 216 int32_t RealNeedMorePlayData(const size_t samples_per_channel, |
| 125 const size_t bytes_per_frame, | 217 const size_t bytes_per_frame, |
| 126 const size_t channels, | 218 const size_t channels, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 140 EXPECT_EQ(samples_per_channel, playout_parameters_.frames_per_buffer()); | 232 EXPECT_EQ(samples_per_channel, playout_parameters_.frames_per_buffer()); |
| 141 EXPECT_EQ(bytes_per_frame, playout_parameters_.GetBytesPerFrame()); | 233 EXPECT_EQ(bytes_per_frame, playout_parameters_.GetBytesPerFrame()); |
| 142 EXPECT_EQ(channels, playout_parameters_.channels()); | 234 EXPECT_EQ(channels, playout_parameters_.channels()); |
| 143 EXPECT_EQ(static_cast<int>(sample_rate), | 235 EXPECT_EQ(static_cast<int>(sample_rate), |
| 144 playout_parameters_.sample_rate()); | 236 playout_parameters_.sample_rate()); |
| 145 EXPECT_EQ(samples_per_channel, | 237 EXPECT_EQ(samples_per_channel, |
| 146 playout_parameters_.frames_per_10ms_buffer()); | 238 playout_parameters_.frames_per_10ms_buffer()); |
| 147 } | 239 } |
| 148 play_count_++; | 240 play_count_++; |
| 149 samples_per_channel_out = samples_per_channel; | 241 samples_per_channel_out = samples_per_channel; |
| 150 // Fill the audio buffer with zeros to avoid disturbing audio. | 242 // Read audio data from audio stream object if one has been injected. |
| 151 const size_t num_bytes = samples_per_channel * bytes_per_frame; | 243 if (audio_stream_) { |
| 152 std::memset(audio_buffer, 0, num_bytes); | 244 audio_stream_->Read( |
| 245 rtc::MakeArrayView(static_cast<int16_t*>(audio_buffer), | |
| 246 samples_per_channel * channels), | |
| 247 channels); | |
| 248 } else { | |
| 249 // Fill the audio buffer with zeros to avoid disturbing audio. | |
| 250 const size_t num_bytes = samples_per_channel * bytes_per_frame; | |
| 251 std::memset(audio_buffer, 0, num_bytes); | |
| 252 } | |
| 153 // Signal the event after given amount of callbacks. | 253 // Signal the event after given amount of callbacks. |
| 154 if (ReceivedEnoughCallbacks()) { | 254 if (ReceivedEnoughCallbacks()) { |
| 155 event_->Set(); | 255 event_->Set(); |
| 156 } | 256 } |
| 157 return 0; | 257 return 0; |
| 158 } | 258 } |
| 159 | 259 |
| 160 bool ReceivedEnoughCallbacks() { | 260 bool ReceivedEnoughCallbacks() { |
| 161 bool recording_done = false; | 261 bool recording_done = false; |
| 162 if (rec_mode()) { | 262 if (rec_mode()) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 179 } | 279 } |
| 180 | 280 |
| 181 bool rec_mode() const { | 281 bool rec_mode() const { |
| 182 return type_ == TransportType::kRecord || | 282 return type_ == TransportType::kRecord || |
| 183 type_ == TransportType::kPlayAndRecord; | 283 type_ == TransportType::kPlayAndRecord; |
| 184 } | 284 } |
| 185 | 285 |
| 186 private: | 286 private: |
| 187 TransportType type_ = TransportType::kInvalid; | 287 TransportType type_ = TransportType::kInvalid; |
| 188 rtc::Event* event_ = nullptr; | 288 rtc::Event* event_ = nullptr; |
| 289 AudioStream* audio_stream_ = nullptr; | |
| 189 size_t num_callbacks_ = 0; | 290 size_t num_callbacks_ = 0; |
| 190 size_t play_count_ = 0; | 291 size_t play_count_ = 0; |
| 191 size_t rec_count_ = 0; | 292 size_t rec_count_ = 0; |
| 192 AudioParameters playout_parameters_; | 293 AudioParameters playout_parameters_; |
| 193 AudioParameters record_parameters_; | 294 AudioParameters record_parameters_; |
| 194 }; | 295 }; |
| 195 | 296 |
| 196 // AudioDeviceTest test fixture. | 297 // AudioDeviceTest test fixture. |
| 197 class AudioDeviceTest : public ::testing::Test { | 298 class AudioDeviceTest : public ::testing::Test { |
| 198 protected: | 299 protected: |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 } | 418 } |
| 318 | 419 |
| 319 // Start playout and verify that the native audio layer starts asking for real | 420 // Start playout and verify that the native audio layer starts asking for real |
| 320 // audio samples to play out using the NeedMorePlayData() callback. | 421 // audio samples to play out using the NeedMorePlayData() callback. |
| 321 // Note that we can't add expectations on audio parameters in EXPECT_CALL | 422 // Note that we can't add expectations on audio parameters in EXPECT_CALL |
| 322 // since parameter are not provided in the each callback. We therefore test and | 423 // since parameter are not provided in the each callback. We therefore test and |
| 323 // verify the parameters in the fake audio transport implementation instead. | 424 // verify the parameters in the fake audio transport implementation instead. |
| 324 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) { | 425 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) { |
| 325 SKIP_TEST_IF_NOT(requirements_satisfied()); | 426 SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 326 MockAudioTransport mock(TransportType::kPlay); | 427 MockAudioTransport mock(TransportType::kPlay); |
| 327 mock.HandleCallbacks(event(), kNumCallbacks); | 428 mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
| 328 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) | 429 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) |
| 329 .Times(AtLeast(kNumCallbacks)); | 430 .Times(AtLeast(kNumCallbacks)); |
| 330 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 431 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 331 StartPlayout(); | 432 StartPlayout(); |
| 332 event()->Wait(kTestTimeOutInMilliseconds); | 433 event()->Wait(kTestTimeOutInMilliseconds); |
| 333 StopPlayout(); | 434 StopPlayout(); |
| 334 } | 435 } |
| 335 | 436 |
| 336 // Start recording and verify that the native audio layer starts providing real | 437 // Start recording and verify that the native audio layer starts providing real |
| 337 // audio samples using the RecordedDataIsAvailable() callback. | 438 // audio samples using the RecordedDataIsAvailable() callback. |
| 338 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) { | 439 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) { |
| 339 SKIP_TEST_IF_NOT(requirements_satisfied()); | 440 SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 340 MockAudioTransport mock(TransportType::kRecord); | 441 MockAudioTransport mock(TransportType::kRecord); |
| 341 mock.HandleCallbacks(event(), kNumCallbacks); | 442 mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
| 342 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, | 443 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, |
| 343 false, _)) | 444 false, _)) |
| 344 .Times(AtLeast(kNumCallbacks)); | 445 .Times(AtLeast(kNumCallbacks)); |
| 345 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 446 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 346 StartRecording(); | 447 StartRecording(); |
| 347 event()->Wait(kTestTimeOutInMilliseconds); | 448 event()->Wait(kTestTimeOutInMilliseconds); |
| 348 StopRecording(); | 449 StopRecording(); |
| 349 } | 450 } |
| 350 | 451 |
| 351 // Start playout and recording (full-duplex audio) and verify that audio is | 452 // Start playout and recording (full-duplex audio) and verify that audio is |
| 352 // active in both directions. | 453 // active in both directions. |
| 353 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) { | 454 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) { |
| 354 SKIP_TEST_IF_NOT(requirements_satisfied()); | 455 SKIP_TEST_IF_NOT(requirements_satisfied()); |
| 355 MockAudioTransport mock(TransportType::kPlayAndRecord); | 456 MockAudioTransport mock(TransportType::kPlayAndRecord); |
| 356 mock.HandleCallbacks(event(), kNumCallbacks); | 457 mock.HandleCallbacks(event(), nullptr, kNumCallbacks); |
| 357 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) | 458 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) |
| 358 .Times(AtLeast(kNumCallbacks)); | 459 .Times(AtLeast(kNumCallbacks)); |
| 359 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, | 460 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, |
| 360 false, _)) | 461 false, _)) |
| 361 .Times(AtLeast(kNumCallbacks)); | 462 .Times(AtLeast(kNumCallbacks)); |
| 362 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | 463 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); |
| 363 StartPlayout(); | 464 StartPlayout(); |
| 364 StartRecording(); | 465 StartRecording(); |
| 365 event()->Wait(kTestTimeOutInMilliseconds); | 466 event()->Wait(kTestTimeOutInMilliseconds); |
| 366 StopRecording(); | 467 StopRecording(); |
| 367 StopPlayout(); | 468 StopPlayout(); |
| 368 } | 469 } |
| 369 | 470 |
| 471 // Start playout and recording and store recorded data in an intermediate FIFO | |
| 472 // buffer from which the playout side then reads its samples in the same order | |
| 473 // as they were stored. Under ideal circumstances, a callback sequence would | |
| 474 // look like: ...+-+-+-+-+-+-+-..., where '+' means 'packet recorded' and '-' | |
| 475 // means 'packet played'. Under such conditions, the FIFO would contain max 1, | |
| 476 // with an average somewhere in (0,1) depending on how long the packets are | |
| 477 // buffered. However, under more realistic conditions, the size | |
| 478 // of the FIFO will vary more due to an unbalance between the two sides. | |
| 479 // This test tries to verify that the device maintains a balanced callback- | |
| 480 // sequence by running in loopback for a few seconds while measuring the size | |
| 481 // (max and average) of the FIFO. The size of the FIFO is increased by the | |
| 482 // recording side and decreased by the playout side. | |
| 483 TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) { | |
| 484 SKIP_TEST_IF_NOT(requirements_satisfied()); | |
| 485 NiceMock<MockAudioTransport> mock(TransportType::kPlayAndRecord); | |
| 486 FifoAudioStream audio_stream; | |
| 487 mock.HandleCallbacks(event(), &audio_stream, | |
| 488 kFullDuplexTimeInSec * kNumCallbacksPerSecond); | |
| 489 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | |
| 490 // Run both sides in mono to make the loopback packet handling less complex. | |
| 491 // The test works for stereo as well; the only requirement is that both sides | |
| 492 // use the same configuration. | |
| 493 EXPECT_EQ(0, audio_device()->SetStereoPlayout(false)); | |
| 494 EXPECT_EQ(0, audio_device()->SetStereoRecording(false)); | |
| 495 StartPlayout(); | |
| 496 StartRecording(); | |
| 497 event()->Wait( | |
| 498 std::max(kTestTimeOutInMilliseconds, 1000 * kFullDuplexTimeInSec)); | |
| 499 StopRecording(); | |
| 500 StopPlayout(); | |
| 501 // This thresholds is set rather high to accomodate differences in hardware | |
| 502 // in several devices. The main idea is to capture cases where a very large | |
| 503 // latency is built up. | |
| 504 EXPECT_LE(audio_stream.average_size(), 5u); | |
| 505 } | |
| 506 | |
| 370 } // namespace webrtc | 507 } // namespace webrtc |
| OLD | NEW |