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