blob: 56f1d070eddfdebc1b1d29bbfe976c219d45f402 [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/gunit.h"
33#include "webrtc/base/scoped_ref_ptr.h"
34#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36using std::min;
37
38class FakeAdmTest : public testing::Test,
39 public webrtc::AudioTransport {
40 protected:
41 static const int kMsInSecond = 1000;
42
43 FakeAdmTest()
44 : push_iterations_(0),
45 pull_iterations_(0),
46 rec_buffer_bytes_(0) {
47 memset(rec_buffer_, 0, sizeof(rec_buffer_));
48 }
49
50 virtual void SetUp() {
51 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052 rtc::Thread::Current());
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,
59 const uint32_t nSamples,
60 const uint8_t nBytesPerSample,
61 const uint8_t nChannels,
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 {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 rec_buffer_bytes_ = nSamples * nBytesPerSample;
Peter Kastingb7e50542015-06-11 12:55:50 -070069 if ((rec_buffer_bytes_ == 0) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 (rec_buffer_bytes_ > FakeAudioCaptureModule::kNumberSamples *
71 FakeAudioCaptureModule::kNumberBytesPerSample)) {
72 ADD_FAILURE();
73 return -1;
74 }
75 memcpy(rec_buffer_, audioSamples, rec_buffer_bytes_);
76 ++push_iterations_;
77 newMicLevel = currentMicLevel;
78 return 0;
79 }
80
81 // ADM is pulling data.
Peter Kasting728d9032015-06-11 14:31:38 -070082 int32_t NeedMorePlayData(const uint32_t nSamples,
83 const uint8_t nBytesPerSample,
84 const uint8_t nChannels,
85 const uint32_t samplesPerSec,
86 void* audioSamples,
87 uint32_t& nSamplesOut,
88 int64_t* elapsed_time_ms,
89 int64_t* ntp_time_ms) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 ++pull_iterations_;
91 const uint32_t audio_buffer_size = nSamples * nBytesPerSample;
92 const uint32_t bytes_out = RecordedDataReceived() ?
93 CopyFromRecBuffer(audioSamples, audio_buffer_size):
94 GenerateZeroBuffer(audioSamples, audio_buffer_size);
95 nSamplesOut = bytes_out / nBytesPerSample;
wu@webrtc.org94454b72014-06-05 20:34:08 +000096 *elapsed_time_ms = 0;
buildbot@webrtc.orgd8524342014-07-14 20:05:09 +000097 *ntp_time_ms = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 return 0;
99 }
100
101 int push_iterations() const { return push_iterations_; }
102 int pull_iterations() const { return pull_iterations_; }
103
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 private:
107 bool RecordedDataReceived() const {
108 return rec_buffer_bytes_ != 0;
109 }
110 int32_t GenerateZeroBuffer(void* audio_buffer, uint32_t audio_buffer_size) {
111 memset(audio_buffer, 0, audio_buffer_size);
112 return audio_buffer_size;
113 }
114 int32_t CopyFromRecBuffer(void* audio_buffer, uint32_t audio_buffer_size) {
115 EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_);
116 const uint32_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_);
117 memcpy(audio_buffer, rec_buffer_, min_buffer_size);
118 return min_buffer_size;
119 }
120
121 int push_iterations_;
122 int pull_iterations_;
123
124 char rec_buffer_[FakeAudioCaptureModule::kNumberSamples *
125 FakeAudioCaptureModule::kNumberBytesPerSample];
126 uint32_t rec_buffer_bytes_;
127};
128
129TEST_F(FakeAdmTest, TestProccess) {
130 // Next process call must be some time in the future (or now).
131 EXPECT_LE(0, fake_audio_capture_module_->TimeUntilNextProcess());
132 // Process call updates TimeUntilNextProcess() but there are no guarantees on
133 // timing so just check that Process can ba called successfully.
134 EXPECT_LE(0, fake_audio_capture_module_->Process());
135}
136
137TEST_F(FakeAdmTest, PlayoutTest) {
138 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 bool stereo_available = false;
141 EXPECT_EQ(0,
142 fake_audio_capture_module_->StereoPlayoutIsAvailable(
143 &stereo_available));
144 EXPECT_TRUE(stereo_available);
145
146 EXPECT_NE(0, fake_audio_capture_module_->StartPlayout());
147 EXPECT_FALSE(fake_audio_capture_module_->PlayoutIsInitialized());
148 EXPECT_FALSE(fake_audio_capture_module_->Playing());
149 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
150
151 EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout());
152 EXPECT_TRUE(fake_audio_capture_module_->PlayoutIsInitialized());
153 EXPECT_FALSE(fake_audio_capture_module_->Playing());
154
155 EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout());
156 EXPECT_TRUE(fake_audio_capture_module_->Playing());
157
158 uint16_t delay_ms = 10;
159 EXPECT_EQ(0, fake_audio_capture_module_->PlayoutDelay(&delay_ms));
160 EXPECT_EQ(0, delay_ms);
161
162 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond);
163 EXPECT_GE(0, push_iterations());
164
165 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
166 EXPECT_FALSE(fake_audio_capture_module_->Playing());
167}
168
169TEST_F(FakeAdmTest, RecordTest) {
170 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
171
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 bool stereo_available = false;
173 EXPECT_EQ(0, fake_audio_capture_module_->StereoRecordingIsAvailable(
174 &stereo_available));
175 EXPECT_FALSE(stereo_available);
176
177 EXPECT_NE(0, fake_audio_capture_module_->StartRecording());
178 EXPECT_FALSE(fake_audio_capture_module_->Recording());
179 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
180
181 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording());
182 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording());
183 EXPECT_TRUE(fake_audio_capture_module_->Recording());
184
185 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond);
186 EXPECT_GE(0, pull_iterations());
187
188 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
189 EXPECT_FALSE(fake_audio_capture_module_->Recording());
190}
191
192TEST_F(FakeAdmTest, DuplexTest) {
193 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
194
195 EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout());
196 EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout());
197
198 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording());
199 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording());
200
201 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond);
202 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond);
203
204 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
205 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
206}