blob: 9e63c1cf311ee02be23a8fa7d5c6a06392493743 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
2 * libjingle
3 * Copyright 2012, Google Inc.
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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5232#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:3635
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:5252 rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:3653 EXPECT_TRUE(fake_audio_capture_module_.get() != NULL);
54 }
55
56 // Callbacks inherited from webrtc::AudioTransport.
57 // ADM is pushing data.
58 virtual 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) {
68 rec_buffer_bytes_ = nSamples * nBytesPerSample;
69 if ((rec_buffer_bytes_ <= 0) ||
70 (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.
82 virtual 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,
wu@webrtc.orgcb711f772014-05-19 17:39:1187 uint32_t& nSamplesOut,
buildbot@webrtc.orgd8524342014-07-14 20:05:0988#ifdef USE_WEBRTC_DEV_BRANCH
wu@webrtc.org94454b72014-06-05 20:34:0889 int64_t* elapsed_time_ms,
wu@webrtc.orgcb711f772014-05-19 17:39:1190#else
buildbot@webrtc.orgd8524342014-07-14 20:05:0991 uint32_t* rtp_timestamp,
wu@webrtc.orgcb711f772014-05-19 17:39:1192#endif
buildbot@webrtc.orgd8524342014-07-14 20:05:0993 int64_t* ntp_time_ms) {
henrike@webrtc.org28e20752013-07-10 00:45:3694 ++pull_iterations_;
95 const uint32_t audio_buffer_size = nSamples * nBytesPerSample;
96 const uint32_t bytes_out = RecordedDataReceived() ?
97 CopyFromRecBuffer(audioSamples, audio_buffer_size):
98 GenerateZeroBuffer(audioSamples, audio_buffer_size);
99 nSamplesOut = bytes_out / nBytesPerSample;
wu@webrtc.orgcb711f772014-05-19 17:39:11100#ifdef USE_WEBRTC_DEV_BRANCH
wu@webrtc.org94454b72014-06-05 20:34:08101 *elapsed_time_ms = 0;
buildbot@webrtc.orgd8524342014-07-14 20:05:09102#else
103 *rtp_timestamp = 0;
wu@webrtc.orgcb711f772014-05-19 17:39:11104#endif
buildbot@webrtc.orgd8524342014-07-14 20:05:09105 *ntp_time_ms = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36106 return 0;
107 }
108
109 int push_iterations() const { return push_iterations_; }
110 int pull_iterations() const { return pull_iterations_; }
111
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52112 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36113
114 private:
115 bool RecordedDataReceived() const {
116 return rec_buffer_bytes_ != 0;
117 }
118 int32_t GenerateZeroBuffer(void* audio_buffer, uint32_t audio_buffer_size) {
119 memset(audio_buffer, 0, audio_buffer_size);
120 return audio_buffer_size;
121 }
122 int32_t CopyFromRecBuffer(void* audio_buffer, uint32_t audio_buffer_size) {
123 EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_);
124 const uint32_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_);
125 memcpy(audio_buffer, rec_buffer_, min_buffer_size);
126 return min_buffer_size;
127 }
128
129 int push_iterations_;
130 int pull_iterations_;
131
132 char rec_buffer_[FakeAudioCaptureModule::kNumberSamples *
133 FakeAudioCaptureModule::kNumberBytesPerSample];
134 uint32_t rec_buffer_bytes_;
135};
136
137TEST_F(FakeAdmTest, TestProccess) {
138 // Next process call must be some time in the future (or now).
139 EXPECT_LE(0, fake_audio_capture_module_->TimeUntilNextProcess());
140 // Process call updates TimeUntilNextProcess() but there are no guarantees on
141 // timing so just check that Process can ba called successfully.
142 EXPECT_LE(0, fake_audio_capture_module_->Process());
143}
144
145TEST_F(FakeAdmTest, PlayoutTest) {
146 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
147
148 bool speaker_available = false;
149 EXPECT_EQ(0, fake_audio_capture_module_->SpeakerIsAvailable(
150 &speaker_available));
151 EXPECT_TRUE(speaker_available);
152
153 bool stereo_available = false;
154 EXPECT_EQ(0,
155 fake_audio_capture_module_->StereoPlayoutIsAvailable(
156 &stereo_available));
157 EXPECT_TRUE(stereo_available);
158
159 EXPECT_NE(0, fake_audio_capture_module_->StartPlayout());
160 EXPECT_FALSE(fake_audio_capture_module_->PlayoutIsInitialized());
161 EXPECT_FALSE(fake_audio_capture_module_->Playing());
162 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
163
164 EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout());
165 EXPECT_TRUE(fake_audio_capture_module_->PlayoutIsInitialized());
166 EXPECT_FALSE(fake_audio_capture_module_->Playing());
167
168 EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout());
169 EXPECT_TRUE(fake_audio_capture_module_->Playing());
170
171 uint16_t delay_ms = 10;
172 EXPECT_EQ(0, fake_audio_capture_module_->PlayoutDelay(&delay_ms));
173 EXPECT_EQ(0, delay_ms);
174
175 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond);
176 EXPECT_GE(0, push_iterations());
177
178 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
179 EXPECT_FALSE(fake_audio_capture_module_->Playing());
180}
181
182TEST_F(FakeAdmTest, RecordTest) {
183 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
184
185 bool microphone_available = false;
186 EXPECT_EQ(0, fake_audio_capture_module_->MicrophoneIsAvailable(
187 &microphone_available));
188 EXPECT_TRUE(microphone_available);
189
190 bool stereo_available = false;
191 EXPECT_EQ(0, fake_audio_capture_module_->StereoRecordingIsAvailable(
192 &stereo_available));
193 EXPECT_FALSE(stereo_available);
194
195 EXPECT_NE(0, fake_audio_capture_module_->StartRecording());
196 EXPECT_FALSE(fake_audio_capture_module_->Recording());
197 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
198
199 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording());
200 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording());
201 EXPECT_TRUE(fake_audio_capture_module_->Recording());
202
203 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond);
204 EXPECT_GE(0, pull_iterations());
205
206 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
207 EXPECT_FALSE(fake_audio_capture_module_->Recording());
208}
209
210TEST_F(FakeAdmTest, DuplexTest) {
211 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this));
212
213 EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout());
214 EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout());
215
216 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording());
217 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording());
218
219 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond);
220 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond);
221
222 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout());
223 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording());
224}