henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/test/fakeaudiocapturemodule.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/criticalsection.h" |
| 16 | #include "rtc_base/gunit.h" |
| 17 | #include "rtc_base/scoped_ref_ptr.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
| 19 | using std::min; |
| 20 | |
| 21 | class FakeAdmTest : public testing::Test, |
| 22 | public webrtc::AudioTransport { |
| 23 | protected: |
| 24 | static const int kMsInSecond = 1000; |
| 25 | |
| 26 | FakeAdmTest() |
| 27 | : push_iterations_(0), |
| 28 | pull_iterations_(0), |
| 29 | rec_buffer_bytes_(0) { |
| 30 | memset(rec_buffer_, 0, sizeof(rec_buffer_)); |
| 31 | } |
| 32 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 33 | void SetUp() override { |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 34 | fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | EXPECT_TRUE(fake_audio_capture_module_.get() != NULL); |
| 36 | } |
| 37 | |
| 38 | // Callbacks inherited from webrtc::AudioTransport. |
| 39 | // ADM is pushing data. |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 40 | int32_t RecordedDataIsAvailable(const void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 41 | const size_t nSamples, |
| 42 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 43 | const size_t nChannels, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 44 | const uint32_t samplesPerSec, |
| 45 | const uint32_t totalDelayMS, |
| 46 | const int32_t clockDrift, |
| 47 | const uint32_t currentMicLevel, |
| 48 | const bool keyPressed, |
| 49 | uint32_t& newMicLevel) override { |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 50 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | rec_buffer_bytes_ = nSamples * nBytesPerSample; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 52 | if ((rec_buffer_bytes_ == 0) || |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | (rec_buffer_bytes_ > FakeAudioCaptureModule::kNumberSamples * |
| 54 | FakeAudioCaptureModule::kNumberBytesPerSample)) { |
| 55 | ADD_FAILURE(); |
| 56 | return -1; |
| 57 | } |
| 58 | memcpy(rec_buffer_, audioSamples, rec_buffer_bytes_); |
| 59 | ++push_iterations_; |
| 60 | newMicLevel = currentMicLevel; |
| 61 | return 0; |
| 62 | } |
| 63 | |
maxmorin | 1aee0b5 | 2016-08-15 11:46:19 -0700 | [diff] [blame] | 64 | void PushCaptureData(int voe_channel, |
| 65 | const void* audio_data, |
| 66 | int bits_per_sample, |
| 67 | int sample_rate, |
| 68 | size_t number_of_channels, |
| 69 | size_t number_of_frames) override {} |
| 70 | |
| 71 | void PullRenderData(int bits_per_sample, |
| 72 | int sample_rate, |
| 73 | size_t number_of_channels, |
| 74 | size_t number_of_frames, |
| 75 | void* audio_data, |
| 76 | int64_t* elapsed_time_ms, |
| 77 | int64_t* ntp_time_ms) override {} |
| 78 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | // ADM is pulling data. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 80 | int32_t NeedMorePlayData(const size_t nSamples, |
| 81 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 82 | const size_t nChannels, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 83 | const uint32_t samplesPerSec, |
| 84 | void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 85 | size_t& nSamplesOut, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 86 | int64_t* elapsed_time_ms, |
| 87 | int64_t* ntp_time_ms) override { |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 88 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | ++pull_iterations_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 90 | const size_t audio_buffer_size = nSamples * nBytesPerSample; |
| 91 | const size_t bytes_out = RecordedDataReceived() ? |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | CopyFromRecBuffer(audioSamples, audio_buffer_size): |
| 93 | GenerateZeroBuffer(audioSamples, audio_buffer_size); |
| 94 | nSamplesOut = bytes_out / nBytesPerSample; |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 95 | *elapsed_time_ms = 0; |
buildbot@webrtc.org | d852434 | 2014-07-14 20:05:09 +0000 | [diff] [blame] | 96 | *ntp_time_ms = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 100 | int push_iterations() const { |
| 101 | rtc::CritScope cs(&crit_); |
| 102 | return push_iterations_; |
| 103 | } |
| 104 | int pull_iterations() const { |
| 105 | rtc::CritScope cs(&crit_); |
| 106 | return pull_iterations_; |
| 107 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 109 | rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | bool RecordedDataReceived() const { |
| 113 | return rec_buffer_bytes_ != 0; |
| 114 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 115 | size_t GenerateZeroBuffer(void* audio_buffer, size_t audio_buffer_size) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | memset(audio_buffer, 0, audio_buffer_size); |
| 117 | return audio_buffer_size; |
| 118 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 119 | size_t CopyFromRecBuffer(void* audio_buffer, size_t audio_buffer_size) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 121 | const size_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | memcpy(audio_buffer, rec_buffer_, min_buffer_size); |
| 123 | return min_buffer_size; |
| 124 | } |
| 125 | |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 126 | rtc::CriticalSection crit_; |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 127 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | int push_iterations_; |
| 129 | int pull_iterations_; |
| 130 | |
| 131 | char rec_buffer_[FakeAudioCaptureModule::kNumberSamples * |
| 132 | FakeAudioCaptureModule::kNumberBytesPerSample]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 133 | size_t rec_buffer_bytes_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | TEST_F(FakeAdmTest, PlayoutTest) { |
| 137 | EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); |
| 138 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | bool stereo_available = false; |
| 140 | EXPECT_EQ(0, |
| 141 | fake_audio_capture_module_->StereoPlayoutIsAvailable( |
| 142 | &stereo_available)); |
| 143 | EXPECT_TRUE(stereo_available); |
| 144 | |
| 145 | EXPECT_NE(0, fake_audio_capture_module_->StartPlayout()); |
| 146 | EXPECT_FALSE(fake_audio_capture_module_->PlayoutIsInitialized()); |
| 147 | EXPECT_FALSE(fake_audio_capture_module_->Playing()); |
| 148 | EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); |
| 149 | |
| 150 | EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout()); |
| 151 | EXPECT_TRUE(fake_audio_capture_module_->PlayoutIsInitialized()); |
| 152 | EXPECT_FALSE(fake_audio_capture_module_->Playing()); |
| 153 | |
| 154 | EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout()); |
| 155 | EXPECT_TRUE(fake_audio_capture_module_->Playing()); |
| 156 | |
| 157 | uint16_t delay_ms = 10; |
| 158 | EXPECT_EQ(0, fake_audio_capture_module_->PlayoutDelay(&delay_ms)); |
| 159 | EXPECT_EQ(0, delay_ms); |
| 160 | |
| 161 | EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); |
| 162 | EXPECT_GE(0, push_iterations()); |
| 163 | |
| 164 | EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); |
| 165 | EXPECT_FALSE(fake_audio_capture_module_->Playing()); |
| 166 | } |
| 167 | |
| 168 | TEST_F(FakeAdmTest, RecordTest) { |
| 169 | EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); |
| 170 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 171 | bool stereo_available = false; |
| 172 | EXPECT_EQ(0, fake_audio_capture_module_->StereoRecordingIsAvailable( |
| 173 | &stereo_available)); |
| 174 | EXPECT_FALSE(stereo_available); |
| 175 | |
| 176 | EXPECT_NE(0, fake_audio_capture_module_->StartRecording()); |
| 177 | EXPECT_FALSE(fake_audio_capture_module_->Recording()); |
| 178 | EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); |
| 179 | |
| 180 | EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); |
| 181 | EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); |
| 182 | EXPECT_TRUE(fake_audio_capture_module_->Recording()); |
| 183 | |
| 184 | EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); |
| 185 | EXPECT_GE(0, pull_iterations()); |
| 186 | |
| 187 | EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); |
| 188 | EXPECT_FALSE(fake_audio_capture_module_->Recording()); |
| 189 | } |
| 190 | |
| 191 | TEST_F(FakeAdmTest, DuplexTest) { |
| 192 | EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); |
| 193 | |
| 194 | EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout()); |
| 195 | EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout()); |
| 196 | |
| 197 | EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); |
| 198 | EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); |
| 199 | |
| 200 | EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); |
| 201 | EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); |
| 202 | |
| 203 | EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); |
| 204 | EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); |
| 205 | } |