Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 11 #include <cstring> | |
| 12 | |
| 13 #include "webrtc/base/event.h" | |
| 14 #include "webrtc/base/logging.h" | |
| 15 #include "webrtc/base/scoped_ref_ptr.h" | |
| 16 #include "webrtc/modules/audio_device/audio_device_impl.h" | |
| 17 #include "webrtc/modules/audio_device/include/audio_device.h" | |
| 18 #include "webrtc/modules/audio_device/include/mock_audio_transport.h" | |
| 19 #include "webrtc/system_wrappers/include/sleep.h" | |
| 20 #include "webrtc/test/gmock.h" | |
| 21 #include "webrtc/test/gtest.h" | |
| 22 | |
| 23 using ::testing::_; | |
| 24 using ::testing::AtLeast; | |
| 25 using ::testing::Ge; | |
| 26 using ::testing::Invoke; | |
| 27 using ::testing::NiceMock; | |
| 28 using ::testing::NotNull; | |
| 29 | |
| 30 namespace webrtc { | |
| 31 | |
| 32 #define SKIP_TEST_IF_NOT(requirements_satisfied) \ | |
| 33 do { \ | |
| 34 if (!requirements_satisfied) { \ | |
| 35 return; \ | |
| 36 } \ | |
| 37 } while (false) | |
| 38 | |
|
the sun
2017/03/16 07:57:35
you want to put all the local types in an anonymou
henrika_webrtc
2017/03/16 14:40:03
Done.
henrika_webrtc
2017/03/16 14:40:03
Acknowledged.
| |
| 39 // Number of callbacks (input or output) the tests waits for before we set | |
| 40 // an event indicating that the test was OK. | |
| 41 static const size_t kNumCallbacks = 10; | |
| 42 // Max amount of time we wait for an event to be set while counting callbacks. | |
| 43 static const int kTestTimeOutInMilliseconds = 10 * 1000; | |
| 44 | |
| 45 enum TransportType { | |
|
the sun
2017/03/16 07:57:35
enum class
henrika_webrtc
2017/03/16 14:40:03
Done.
| |
| 46 kPlayout = 0x1, | |
|
the sun
2017/03/16 07:57:35
I'd actually prefer to have separate enum members:
henrika_webrtc
2017/03/16 14:40:03
Agree. Will change. Hard to combine enum class and
| |
| 47 kRecording = 0x2, | |
| 48 }; | |
| 49 | |
| 50 // Mocks the AudioTransport object and proxies actions for the two callbacks | |
| 51 // (RecordedDataIsAvailable and NeedMorePlayData) to different implementations | |
| 52 // of AudioStreamInterface. | |
| 53 class MockAudioTransport : public test::MockAudioTransport { | |
| 54 public: | |
| 55 explicit MockAudioTransport(int type) | |
| 56 : event_(nullptr), | |
|
the sun
2017/03/16 07:57:35
prefer inline member initialization to ctor init l
henrika_webrtc
2017/03/16 14:40:03
Done.
| |
| 57 num_callbacks_(0), | |
| 58 type_(type), | |
| 59 play_count_(0), | |
| 60 rec_count_(0) {} | |
| 61 | |
| 62 virtual ~MockAudioTransport() {} | |
|
the sun
2017/03/16 07:57:35
override, not virtual
henrika_webrtc
2017/03/16 14:40:03
Done.
| |
| 63 | |
| 64 // Set default actions of the mock object. We are delegating to fake | |
| 65 // implementation where the number of callbacks is counted and an event | |
| 66 // is set after a certain number of callbacks. Audio parameters are also | |
| 67 // checked. | |
| 68 void HandleCallbacks(rtc::Event* event, int num_callbacks) { | |
| 69 event_ = event; | |
| 70 num_callbacks_ = num_callbacks; | |
| 71 if (play_mode()) { | |
| 72 ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _)) | |
| 73 .WillByDefault( | |
| 74 Invoke(this, &MockAudioTransport::RealNeedMorePlayData)); | |
| 75 } | |
| 76 if (rec_mode()) { | |
| 77 ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _)) | |
| 78 .WillByDefault( | |
| 79 Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable)); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 int32_t RealRecordedDataIsAvailable(const void* audio_buffer, | |
| 84 const size_t samples_per_channel, | |
| 85 const size_t bytes_per_frame, | |
| 86 const size_t channels, | |
| 87 const uint32_t sample_rate, | |
| 88 const uint32_t total_delay_ms, | |
| 89 const int32_t clock_drift, | |
| 90 const uint32_t current_mic_level, | |
| 91 const bool typing_status, | |
| 92 uint32_t& new_mic_level) { | |
| 93 EXPECT_TRUE(rec_mode()) << "No test is expecting these callbacks."; | |
| 94 // Store audio parameters once in the first callback. For all other | |
| 95 // callbacks, verify that the provided audio parameters are maintained and | |
| 96 // that each callback corresponds to 10ms for any given sample rate. | |
| 97 if (!record_parameters_.is_complete()) { | |
| 98 record_parameters_.reset(sample_rate, channels, samples_per_channel); | |
| 99 } else { | |
| 100 EXPECT_EQ(samples_per_channel, record_parameters_.frames_per_buffer()); | |
| 101 EXPECT_EQ(bytes_per_frame, record_parameters_.GetBytesPerFrame()); | |
| 102 EXPECT_EQ(channels, record_parameters_.channels()); | |
| 103 EXPECT_EQ(static_cast<int>(sample_rate), | |
| 104 record_parameters_.sample_rate()); | |
| 105 EXPECT_EQ(samples_per_channel, | |
| 106 record_parameters_.frames_per_10ms_buffer()); | |
| 107 } | |
| 108 rec_count_++; | |
| 109 // Signal the event after given amount of callbacks. | |
| 110 if (ReceivedEnoughCallbacks()) { | |
| 111 event_->Set(); | |
| 112 } | |
| 113 return 0; | |
| 114 } | |
| 115 | |
| 116 int32_t RealNeedMorePlayData(const size_t samples_per_channel, | |
| 117 const size_t bytes_per_frame, | |
| 118 const size_t channels, | |
| 119 const uint32_t sample_rate, | |
| 120 void* audio_buffer, | |
| 121 size_t& samples_per_channel_out, | |
| 122 int64_t* elapsed_time_ms, | |
| 123 int64_t* ntp_time_ms) { | |
| 124 EXPECT_TRUE(play_mode()) << "No test is expecting these callbacks."; | |
| 125 // Store audio parameters once in the first callback. For all other | |
| 126 // callbacks, verify that the provided audio parameters are maintained and | |
| 127 // that each callback corresponds to 10ms for any given sample rate. | |
| 128 if (!playout_parameters_.is_complete()) { | |
| 129 playout_parameters_.reset(sample_rate, channels, samples_per_channel); | |
| 130 } else { | |
| 131 EXPECT_EQ(samples_per_channel, playout_parameters_.frames_per_buffer()); | |
| 132 EXPECT_EQ(bytes_per_frame, playout_parameters_.GetBytesPerFrame()); | |
| 133 EXPECT_EQ(channels, playout_parameters_.channels()); | |
| 134 EXPECT_EQ(static_cast<int>(sample_rate), | |
| 135 playout_parameters_.sample_rate()); | |
| 136 EXPECT_EQ(samples_per_channel, | |
| 137 playout_parameters_.frames_per_10ms_buffer()); | |
| 138 } | |
| 139 play_count_++; | |
| 140 samples_per_channel_out = samples_per_channel; | |
| 141 // Fill the audio buffer with zeros to avoid disturbing audio. | |
| 142 const size_t num_bytes = samples_per_channel * bytes_per_frame; | |
| 143 std::memset(audio_buffer, 0, num_bytes); | |
| 144 // Signal the event after given amount of callbacks. | |
| 145 if (ReceivedEnoughCallbacks()) { | |
| 146 event_->Set(); | |
| 147 } | |
| 148 return 0; | |
| 149 } | |
| 150 | |
| 151 bool ReceivedEnoughCallbacks() { | |
| 152 bool recording_done = false; | |
| 153 if (rec_mode()) | |
|
the sun
2017/03/16 07:57:35
brackets, please
henrika_webrtc
2017/03/16 14:40:03
Done.
| |
| 154 recording_done = rec_count_ >= num_callbacks_; | |
| 155 else | |
| 156 recording_done = true; | |
| 157 bool playout_done = false; | |
| 158 if (play_mode()) | |
| 159 playout_done = play_count_ >= num_callbacks_; | |
| 160 else | |
| 161 playout_done = true; | |
| 162 return recording_done && playout_done; | |
| 163 } | |
| 164 | |
| 165 bool play_mode() const { return type_ & kPlayout; } | |
| 166 bool rec_mode() const { return type_ & kRecording; } | |
| 167 | |
| 168 private: | |
| 169 rtc::Event* event_; | |
| 170 size_t num_callbacks_; | |
| 171 int type_; | |
| 172 size_t play_count_; | |
| 173 size_t rec_count_; | |
| 174 AudioParameters playout_parameters_; | |
| 175 AudioParameters record_parameters_; | |
| 176 }; | |
| 177 | |
| 178 // AudioDeviceTest test fixture. | |
| 179 class AudioDeviceTest : public ::testing::Test { | |
| 180 protected: | |
| 181 AudioDeviceTest() : event_(false, false) { | |
| 182 rtc::LogMessage::LogToDebug(rtc::LS_INFO); | |
| 183 // Add extra logging fields here if needed for debugging. | |
| 184 // rtc::LogMessage::LogTimestamps(); | |
| 185 // rtc::LogMessage::LogThreads(); | |
| 186 audio_device_ = CreateAudioDevice(AudioDeviceModule::kPlatformDefaultAudio); | |
| 187 EXPECT_NE(audio_device_.get(), nullptr); | |
| 188 AudioDeviceModule::AudioLayer audio_layer; | |
| 189 EXPECT_EQ(0, audio_device_->ActiveAudioLayer(&audio_layer)); | |
| 190 if (audio_layer == AudioDeviceModule::kLinuxAlsaAudio) { | |
| 191 requirements_satisfied_ = false; | |
| 192 } | |
| 193 if (requirements_satisfied_) { | |
| 194 EXPECT_EQ(0, audio_device_->Init()); | |
| 195 const int16_t num_playout_devices = audio_device_->PlayoutDevices(); | |
| 196 const int16_t num_record_devices = audio_device_->RecordingDevices(); | |
| 197 requirements_satisfied_ = | |
| 198 num_playout_devices > 0 && num_record_devices > 0; | |
| 199 } | |
| 200 } | |
| 201 virtual ~AudioDeviceTest() {} | |
| 202 | |
| 203 // Combine all required initialization and do all of it in the test setup. | |
| 204 // This test suite does not focus on testing all components of the ADM but | |
| 205 // instead focus on the "pig picture" and how it is used in VoiceEngine. | |
| 206 // TODO(henrika): perhaps use SetUpTestCase() to share initial setup between | |
| 207 // all tests. | |
| 208 virtual void SetUp() { | |
| 209 if (!requirements_satisfied_) | |
| 210 return; | |
| 211 EXPECT_EQ(0, audio_device_->SetPlayoutDevice(0)); | |
| 212 EXPECT_EQ(0, audio_device_->InitSpeaker()); | |
| 213 EXPECT_EQ(0, audio_device_->SetRecordingDevice(0)); | |
| 214 EXPECT_EQ(0, audio_device_->InitMicrophone()); | |
| 215 EXPECT_EQ(0, audio_device_->StereoPlayoutIsAvailable(&stereo_playout_)); | |
| 216 EXPECT_EQ(0, audio_device_->SetStereoPlayout(stereo_playout_)); | |
| 217 EXPECT_EQ(0, audio_device_->StereoRecordingIsAvailable(&stereo_recording_)); | |
| 218 EXPECT_EQ(0, audio_device_->SetStereoRecording(stereo_recording_)); | |
| 219 EXPECT_EQ(0, audio_device_->SetAGC(false)); | |
| 220 EXPECT_FALSE(audio_device_->AGC()); | |
| 221 } | |
| 222 | |
| 223 virtual void TearDown() { EXPECT_EQ(0, audio_device_->Terminate()); } | |
| 224 | |
| 225 bool requirements_satisfied() const { | |
| 226 return requirements_satisfied_; | |
| 227 } | |
| 228 | |
| 229 const rtc::scoped_refptr<AudioDeviceModule>& audio_device() const { | |
| 230 return audio_device_; | |
| 231 } | |
| 232 | |
| 233 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDevice( | |
|
the sun
2017/03/16 07:57:35
Internal method only used once - fold into ctor.
henrika_webrtc
2017/03/16 14:40:03
Done.
| |
| 234 AudioDeviceModule::AudioLayer audio_layer) { | |
| 235 rtc::scoped_refptr<AudioDeviceModule> adm( | |
| 236 AudioDeviceModule::Create(0, audio_layer)); | |
| 237 return adm; | |
| 238 } | |
| 239 | |
| 240 void StartPlayout() { | |
| 241 EXPECT_FALSE(audio_device()->Playing()); | |
| 242 EXPECT_EQ(0, audio_device()->InitPlayout()); | |
| 243 EXPECT_TRUE(audio_device()->PlayoutIsInitialized()); | |
| 244 EXPECT_EQ(0, audio_device()->StartPlayout()); | |
| 245 EXPECT_TRUE(audio_device()->Playing()); | |
| 246 } | |
| 247 | |
| 248 void StopPlayout() { | |
| 249 EXPECT_EQ(0, audio_device()->StopPlayout()); | |
| 250 EXPECT_FALSE(audio_device()->Playing()); | |
| 251 EXPECT_FALSE(audio_device()->PlayoutIsInitialized()); | |
| 252 } | |
| 253 | |
| 254 void StartRecording() { | |
| 255 EXPECT_FALSE(audio_device()->Recording()); | |
| 256 EXPECT_EQ(0, audio_device()->InitRecording()); | |
| 257 EXPECT_TRUE(audio_device()->RecordingIsInitialized()); | |
| 258 EXPECT_EQ(0, audio_device()->StartRecording()); | |
| 259 EXPECT_TRUE(audio_device()->Recording()); | |
| 260 } | |
| 261 | |
| 262 void StopRecording() { | |
| 263 EXPECT_EQ(0, audio_device()->StopRecording()); | |
| 264 EXPECT_FALSE(audio_device()->Recording()); | |
| 265 EXPECT_FALSE(audio_device()->RecordingIsInitialized()); | |
| 266 } | |
| 267 | |
| 268 bool requirements_satisfied_ = true; | |
| 269 rtc::Event event_; | |
| 270 rtc::scoped_refptr<AudioDeviceModule> audio_device_; | |
| 271 bool stereo_playout_ = false; | |
| 272 bool stereo_recording_ = false; | |
| 273 }; | |
| 274 | |
| 275 // Uses the test fixture to create, initialize and destruct the ADM. | |
| 276 TEST_F(AudioDeviceTest, ConstructDestruct) {} | |
| 277 | |
| 278 TEST_F(AudioDeviceTest, InitTerminate) { | |
| 279 SKIP_TEST_IF_NOT(requirements_satisfied()); | |
| 280 // Initialization is part of the test fixture. | |
| 281 EXPECT_TRUE(audio_device()->Initialized()); | |
| 282 EXPECT_EQ(0, audio_device()->Terminate()); | |
| 283 EXPECT_FALSE(audio_device()->Initialized()); | |
| 284 } | |
| 285 | |
| 286 // Tests Start/Stop playout without any registered audio callback. | |
| 287 TEST_F(AudioDeviceTest, StartStopPlayout) { | |
| 288 SKIP_TEST_IF_NOT(requirements_satisfied()); | |
| 289 StartPlayout(); | |
| 290 StopPlayout(); | |
| 291 StartPlayout(); | |
| 292 StopPlayout(); | |
| 293 } | |
| 294 | |
| 295 // Tests Start/Stop recording without any registered audio callback. | |
| 296 TEST_F(AudioDeviceTest, StartStopRecording) { | |
| 297 SKIP_TEST_IF_NOT(requirements_satisfied()); | |
| 298 StartRecording(); | |
| 299 StopRecording(); | |
| 300 StartRecording(); | |
| 301 StopRecording(); | |
| 302 } | |
| 303 | |
| 304 // Start playout and verify that the native audio layer starts asking for real | |
| 305 // audio samples to play out using the NeedMorePlayData() callback. | |
| 306 // Note that we can't add expectations on audio parameters in EXPECT_CALL | |
| 307 // since parameter are not provided in the each callback. We therefore test and | |
| 308 // verify the parameters in the fake audio transport implementation instead. | |
| 309 TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) { | |
| 310 SKIP_TEST_IF_NOT(requirements_satisfied()); | |
| 311 MockAudioTransport mock(kPlayout); | |
| 312 mock.HandleCallbacks(&event_, kNumCallbacks); | |
| 313 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) | |
| 314 .Times(AtLeast(kNumCallbacks)); | |
| 315 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | |
| 316 StartPlayout(); | |
| 317 event_.Wait(kTestTimeOutInMilliseconds); | |
| 318 StopPlayout(); | |
| 319 } | |
| 320 | |
| 321 // Start recording and verify that the native audio layer starts providing real | |
| 322 // audio samples using the RecordedDataIsAvailable() callback. | |
| 323 TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) { | |
| 324 SKIP_TEST_IF_NOT(requirements_satisfied()); | |
| 325 MockAudioTransport mock(kRecording); | |
| 326 mock.HandleCallbacks(&event_, kNumCallbacks); | |
| 327 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, | |
| 328 false, _)) | |
| 329 .Times(AtLeast(kNumCallbacks)); | |
| 330 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | |
| 331 StartRecording(); | |
| 332 event_.Wait(kTestTimeOutInMilliseconds); | |
| 333 StopRecording(); | |
| 334 } | |
| 335 | |
| 336 // Start playout and recording (full-duplex audio) and verify that audio is | |
| 337 // active in both directions. | |
| 338 TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) { | |
| 339 SKIP_TEST_IF_NOT(requirements_satisfied()); | |
| 340 MockAudioTransport mock(kPlayout | kRecording); | |
| 341 mock.HandleCallbacks(&event_, kNumCallbacks); | |
| 342 EXPECT_CALL(mock, NeedMorePlayData(_, _, _, _, NotNull(), _, _, _)) | |
| 343 .Times(AtLeast(kNumCallbacks)); | |
| 344 EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(), _, _, _, _, Ge(0u), 0, _, | |
| 345 false, _)) | |
| 346 .Times(AtLeast(kNumCallbacks)); | |
| 347 EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); | |
| 348 StartPlayout(); | |
| 349 StartRecording(); | |
| 350 event_.Wait(kTestTimeOutInMilliseconds); | |
| 351 StopRecording(); | |
| 352 StopPlayout(); | |
| 353 } | |
| 354 | |
| 355 } // namespace webrtc | |
| OLD | NEW |