blob: 4e3bafef7290eed8e66b758050fd621d004c7cee [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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
deadbeefee8c6d32015-08-13 14:27:18 -070032#include "webrtc/base/criticalsection.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
34#include "webrtc/base/scoped_ref_ptr.h"
35#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37using std::min;
38
39class 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() {
deadbeefee8c6d32015-08-13 14:27:18 -070052 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 EXPECT_TRUE(fake_audio_capture_module_.get() != NULL);
54 }
55
56 // Callbacks inherited from webrtc::AudioTransport.
57 // ADM is pushing data.
Peter Kasting728d9032015-06-11 14:31:38 -070058 int32_t RecordedDataIsAvailable(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070059 const size_t nSamples,
60 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -080061 const size_t nChannels,
Peter Kasting728d9032015-06-11 14:31:38 -070062 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 {
deadbeefee8c6d32015-08-13 14:27:18 -070068 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 rec_buffer_bytes_ = nSamples * nBytesPerSample;
Peter Kastingb7e50542015-06-11 12:55:50 -070070 if ((rec_buffer_bytes_ == 0) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 (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 Kastingdce40cf2015-08-24 14:52:23 -070083 int32_t NeedMorePlayData(const size_t nSamples,
84 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -080085 const size_t nChannels,
Peter Kasting728d9032015-06-11 14:31:38 -070086 const uint32_t samplesPerSec,
87 void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070088 size_t& nSamplesOut,
Peter Kasting728d9032015-06-11 14:31:38 -070089 int64_t* elapsed_time_ms,
90 int64_t* ntp_time_ms) override {
deadbeefee8c6d32015-08-13 14:27:18 -070091 rtc::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 ++pull_iterations_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070093 const size_t audio_buffer_size = nSamples * nBytesPerSample;
94 const size_t bytes_out = RecordedDataReceived() ?
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 CopyFromRecBuffer(audioSamples, audio_buffer_size):
96 GenerateZeroBuffer(audioSamples, audio_buffer_size);
97 nSamplesOut = bytes_out / nBytesPerSample;
wu@webrtc.org94454b72014-06-05 20:34:08 +000098 *elapsed_time_ms = 0;
buildbot@webrtc.orgd8524342014-07-14 20:05:09 +000099 *ntp_time_ms = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 return 0;
101 }
102
deadbeefee8c6d32015-08-13 14:27:18 -0700103 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.org28e20752013-07-10 00:45:36 +0000111
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 private:
115 bool RecordedDataReceived() const {
116 return rec_buffer_bytes_ != 0;
117 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700118 size_t GenerateZeroBuffer(void* audio_buffer, size_t audio_buffer_size) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 memset(audio_buffer, 0, audio_buffer_size);
120 return audio_buffer_size;
121 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700122 size_t CopyFromRecBuffer(void* audio_buffer, size_t audio_buffer_size) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700124 const size_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 memcpy(audio_buffer, rec_buffer_, min_buffer_size);
126 return min_buffer_size;
127 }
128
pbos5ad935c2016-01-25 03:52:44 -0800129 rtc::CriticalSection crit_;
deadbeefee8c6d32015-08-13 14:27:18 -0700130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 int push_iterations_;
132 int pull_iterations_;
133
134 char rec_buffer_[FakeAudioCaptureModule::kNumberSamples *
135 FakeAudioCaptureModule::kNumberBytesPerSample];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 size_t rec_buffer_bytes_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137};
138
139TEST_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
147TEST_F(FakeAdmTest, PlayoutTest) {
148 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
149
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 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
179TEST_F(FakeAdmTest, RecordTest) {
180 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
181
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 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
202TEST_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}