henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/app/webrtc/test/fakeaudiocapturemodule.h" |
| 29 | |
| 30 | #include <algorithm> |
| 31 | |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 32 | #include "webrtc/base/criticalsection.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 33 | #include "webrtc/base/gunit.h" |
| 34 | #include "webrtc/base/scoped_ref_ptr.h" |
| 35 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | |
| 37 | using std::min; |
| 38 | |
| 39 | class FakeAdmTest : public testing::Test, |
| 40 | public webrtc::AudioTransport { |
| 41 | protected: |
| 42 | static const int kMsInSecond = 1000; |
| 43 | |
| 44 | FakeAdmTest() |
| 45 | : push_iterations_(0), |
| 46 | pull_iterations_(0), |
| 47 | rec_buffer_bytes_(0) { |
| 48 | memset(rec_buffer_, 0, sizeof(rec_buffer_)); |
| 49 | } |
| 50 | |
| 51 | virtual void SetUp() { |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 52 | fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | EXPECT_TRUE(fake_audio_capture_module_.get() != NULL); |
| 54 | } |
| 55 | |
| 56 | // Callbacks inherited from webrtc::AudioTransport. |
| 57 | // ADM is pushing data. |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 58 | int32_t RecordedDataIsAvailable(const void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 59 | const size_t nSamples, |
| 60 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 61 | const size_t nChannels, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 62 | const uint32_t samplesPerSec, |
| 63 | const uint32_t totalDelayMS, |
| 64 | const int32_t clockDrift, |
| 65 | const uint32_t currentMicLevel, |
| 66 | const bool keyPressed, |
| 67 | uint32_t& newMicLevel) override { |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 68 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | rec_buffer_bytes_ = nSamples * nBytesPerSample; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 70 | if ((rec_buffer_bytes_ == 0) || |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | (rec_buffer_bytes_ > FakeAudioCaptureModule::kNumberSamples * |
| 72 | FakeAudioCaptureModule::kNumberBytesPerSample)) { |
| 73 | ADD_FAILURE(); |
| 74 | return -1; |
| 75 | } |
| 76 | memcpy(rec_buffer_, audioSamples, rec_buffer_bytes_); |
| 77 | ++push_iterations_; |
| 78 | newMicLevel = currentMicLevel; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | // ADM is pulling data. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 83 | int32_t NeedMorePlayData(const size_t nSamples, |
| 84 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 85 | const size_t nChannels, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 86 | const uint32_t samplesPerSec, |
| 87 | void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 88 | size_t& nSamplesOut, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 89 | int64_t* elapsed_time_ms, |
| 90 | int64_t* ntp_time_ms) override { |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 91 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | ++pull_iterations_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 93 | const size_t audio_buffer_size = nSamples * nBytesPerSample; |
| 94 | const size_t bytes_out = RecordedDataReceived() ? |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | CopyFromRecBuffer(audioSamples, audio_buffer_size): |
| 96 | GenerateZeroBuffer(audioSamples, audio_buffer_size); |
| 97 | nSamplesOut = bytes_out / nBytesPerSample; |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 98 | *elapsed_time_ms = 0; |
buildbot@webrtc.org | d852434 | 2014-07-14 20:05:09 +0000 | [diff] [blame] | 99 | *ntp_time_ms = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 103 | int push_iterations() const { |
| 104 | rtc::CritScope cs(&crit_); |
| 105 | return push_iterations_; |
| 106 | } |
| 107 | int pull_iterations() const { |
| 108 | rtc::CritScope cs(&crit_); |
| 109 | return pull_iterations_; |
| 110 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 112 | rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | bool RecordedDataReceived() const { |
| 116 | return rec_buffer_bytes_ != 0; |
| 117 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 118 | size_t GenerateZeroBuffer(void* audio_buffer, size_t audio_buffer_size) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | memset(audio_buffer, 0, audio_buffer_size); |
| 120 | return audio_buffer_size; |
| 121 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 122 | size_t CopyFromRecBuffer(void* audio_buffer, size_t audio_buffer_size) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 124 | 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] | 125 | memcpy(audio_buffer, rec_buffer_, min_buffer_size); |
| 126 | return min_buffer_size; |
| 127 | } |
| 128 | |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame^] | 129 | rtc::CriticalSection crit_; |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 130 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | int push_iterations_; |
| 132 | int pull_iterations_; |
| 133 | |
| 134 | char rec_buffer_[FakeAudioCaptureModule::kNumberSamples * |
| 135 | FakeAudioCaptureModule::kNumberBytesPerSample]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 136 | size_t rec_buffer_bytes_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | TEST_F(FakeAdmTest, TestProccess) { |
| 140 | // Next process call must be some time in the future (or now). |
| 141 | EXPECT_LE(0, fake_audio_capture_module_->TimeUntilNextProcess()); |
| 142 | // Process call updates TimeUntilNextProcess() but there are no guarantees on |
| 143 | // timing so just check that Process can ba called successfully. |
| 144 | EXPECT_LE(0, fake_audio_capture_module_->Process()); |
| 145 | } |
| 146 | |
| 147 | TEST_F(FakeAdmTest, PlayoutTest) { |
| 148 | EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); |
| 149 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | bool stereo_available = false; |
| 151 | EXPECT_EQ(0, |
| 152 | fake_audio_capture_module_->StereoPlayoutIsAvailable( |
| 153 | &stereo_available)); |
| 154 | EXPECT_TRUE(stereo_available); |
| 155 | |
| 156 | EXPECT_NE(0, fake_audio_capture_module_->StartPlayout()); |
| 157 | EXPECT_FALSE(fake_audio_capture_module_->PlayoutIsInitialized()); |
| 158 | EXPECT_FALSE(fake_audio_capture_module_->Playing()); |
| 159 | EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); |
| 160 | |
| 161 | EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout()); |
| 162 | EXPECT_TRUE(fake_audio_capture_module_->PlayoutIsInitialized()); |
| 163 | EXPECT_FALSE(fake_audio_capture_module_->Playing()); |
| 164 | |
| 165 | EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout()); |
| 166 | EXPECT_TRUE(fake_audio_capture_module_->Playing()); |
| 167 | |
| 168 | uint16_t delay_ms = 10; |
| 169 | EXPECT_EQ(0, fake_audio_capture_module_->PlayoutDelay(&delay_ms)); |
| 170 | EXPECT_EQ(0, delay_ms); |
| 171 | |
| 172 | EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); |
| 173 | EXPECT_GE(0, push_iterations()); |
| 174 | |
| 175 | EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); |
| 176 | EXPECT_FALSE(fake_audio_capture_module_->Playing()); |
| 177 | } |
| 178 | |
| 179 | TEST_F(FakeAdmTest, RecordTest) { |
| 180 | EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); |
| 181 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | bool stereo_available = false; |
| 183 | EXPECT_EQ(0, fake_audio_capture_module_->StereoRecordingIsAvailable( |
| 184 | &stereo_available)); |
| 185 | EXPECT_FALSE(stereo_available); |
| 186 | |
| 187 | EXPECT_NE(0, fake_audio_capture_module_->StartRecording()); |
| 188 | EXPECT_FALSE(fake_audio_capture_module_->Recording()); |
| 189 | EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); |
| 190 | |
| 191 | EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); |
| 192 | EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); |
| 193 | EXPECT_TRUE(fake_audio_capture_module_->Recording()); |
| 194 | |
| 195 | EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); |
| 196 | EXPECT_GE(0, pull_iterations()); |
| 197 | |
| 198 | EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); |
| 199 | EXPECT_FALSE(fake_audio_capture_module_->Recording()); |
| 200 | } |
| 201 | |
| 202 | TEST_F(FakeAdmTest, DuplexTest) { |
| 203 | EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); |
| 204 | |
| 205 | EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout()); |
| 206 | EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout()); |
| 207 | |
| 208 | EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); |
| 209 | EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); |
| 210 | |
| 211 | EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); |
| 212 | EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); |
| 213 | |
| 214 | EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); |
| 215 | EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); |
| 216 | } |