OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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. | |
9 */ | |
10 | |
11 #include "webrtc/api/test/fakeaudiocapturemodule.h" | |
12 | |
13 #include <algorithm> | |
14 | |
15 #include "webrtc/base/criticalsection.h" | |
16 #include "webrtc/base/gunit.h" | |
17 #include "webrtc/base/scoped_ref_ptr.h" | |
18 #include "webrtc/base/thread.h" | |
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 void SetUp() override { | |
35 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); | |
36 EXPECT_TRUE(fake_audio_capture_module_.get() != NULL); | |
37 } | |
38 | |
39 // Callbacks inherited from webrtc::AudioTransport. | |
40 // ADM is pushing data. | |
41 int32_t RecordedDataIsAvailable(const void* audioSamples, | |
42 const size_t nSamples, | |
43 const size_t nBytesPerSample, | |
44 const size_t nChannels, | |
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 { | |
51 rtc::CritScope cs(&crit_); | |
52 rec_buffer_bytes_ = nSamples * nBytesPerSample; | |
53 if ((rec_buffer_bytes_ == 0) || | |
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 void PushCaptureData(int voe_channel, | |
66 const void* audio_data, | |
67 int bits_per_sample, | |
68 int sample_rate, | |
69 size_t number_of_channels, | |
70 size_t number_of_frames) override {} | |
71 | |
72 void PullRenderData(int bits_per_sample, | |
73 int sample_rate, | |
74 size_t number_of_channels, | |
75 size_t number_of_frames, | |
76 void* audio_data, | |
77 int64_t* elapsed_time_ms, | |
78 int64_t* ntp_time_ms) override {} | |
79 | |
80 // ADM is pulling data. | |
81 int32_t NeedMorePlayData(const size_t nSamples, | |
82 const size_t nBytesPerSample, | |
83 const size_t nChannels, | |
84 const uint32_t samplesPerSec, | |
85 void* audioSamples, | |
86 size_t& nSamplesOut, | |
87 int64_t* elapsed_time_ms, | |
88 int64_t* ntp_time_ms) override { | |
89 rtc::CritScope cs(&crit_); | |
90 ++pull_iterations_; | |
91 const size_t audio_buffer_size = nSamples * nBytesPerSample; | |
92 const size_t bytes_out = RecordedDataReceived() ? | |
93 CopyFromRecBuffer(audioSamples, audio_buffer_size): | |
94 GenerateZeroBuffer(audioSamples, audio_buffer_size); | |
95 nSamplesOut = bytes_out / nBytesPerSample; | |
96 *elapsed_time_ms = 0; | |
97 *ntp_time_ms = 0; | |
98 return 0; | |
99 } | |
100 | |
101 int push_iterations() const { | |
102 rtc::CritScope cs(&crit_); | |
103 return push_iterations_; | |
104 } | |
105 int pull_iterations() const { | |
106 rtc::CritScope cs(&crit_); | |
107 return pull_iterations_; | |
108 } | |
109 | |
110 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_; | |
111 | |
112 private: | |
113 bool RecordedDataReceived() const { | |
114 return rec_buffer_bytes_ != 0; | |
115 } | |
116 size_t GenerateZeroBuffer(void* audio_buffer, size_t audio_buffer_size) { | |
117 memset(audio_buffer, 0, audio_buffer_size); | |
118 return audio_buffer_size; | |
119 } | |
120 size_t CopyFromRecBuffer(void* audio_buffer, size_t audio_buffer_size) { | |
121 EXPECT_EQ(audio_buffer_size, rec_buffer_bytes_); | |
122 const size_t min_buffer_size = min(audio_buffer_size, rec_buffer_bytes_); | |
123 memcpy(audio_buffer, rec_buffer_, min_buffer_size); | |
124 return min_buffer_size; | |
125 } | |
126 | |
127 rtc::CriticalSection crit_; | |
128 | |
129 int push_iterations_; | |
130 int pull_iterations_; | |
131 | |
132 char rec_buffer_[FakeAudioCaptureModule::kNumberSamples * | |
133 FakeAudioCaptureModule::kNumberBytesPerSample]; | |
134 size_t rec_buffer_bytes_; | |
135 }; | |
136 | |
137 TEST_F(FakeAdmTest, TestProcess) { | |
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 be called successfully. | |
142 fake_audio_capture_module_->Process(); | |
143 } | |
144 | |
145 TEST_F(FakeAdmTest, PlayoutTest) { | |
146 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); | |
147 | |
148 bool stereo_available = false; | |
149 EXPECT_EQ(0, | |
150 fake_audio_capture_module_->StereoPlayoutIsAvailable( | |
151 &stereo_available)); | |
152 EXPECT_TRUE(stereo_available); | |
153 | |
154 EXPECT_NE(0, fake_audio_capture_module_->StartPlayout()); | |
155 EXPECT_FALSE(fake_audio_capture_module_->PlayoutIsInitialized()); | |
156 EXPECT_FALSE(fake_audio_capture_module_->Playing()); | |
157 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); | |
158 | |
159 EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout()); | |
160 EXPECT_TRUE(fake_audio_capture_module_->PlayoutIsInitialized()); | |
161 EXPECT_FALSE(fake_audio_capture_module_->Playing()); | |
162 | |
163 EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout()); | |
164 EXPECT_TRUE(fake_audio_capture_module_->Playing()); | |
165 | |
166 uint16_t delay_ms = 10; | |
167 EXPECT_EQ(0, fake_audio_capture_module_->PlayoutDelay(&delay_ms)); | |
168 EXPECT_EQ(0, delay_ms); | |
169 | |
170 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); | |
171 EXPECT_GE(0, push_iterations()); | |
172 | |
173 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); | |
174 EXPECT_FALSE(fake_audio_capture_module_->Playing()); | |
175 } | |
176 | |
177 TEST_F(FakeAdmTest, RecordTest) { | |
178 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); | |
179 | |
180 bool stereo_available = false; | |
181 EXPECT_EQ(0, fake_audio_capture_module_->StereoRecordingIsAvailable( | |
182 &stereo_available)); | |
183 EXPECT_FALSE(stereo_available); | |
184 | |
185 EXPECT_NE(0, fake_audio_capture_module_->StartRecording()); | |
186 EXPECT_FALSE(fake_audio_capture_module_->Recording()); | |
187 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); | |
188 | |
189 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); | |
190 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); | |
191 EXPECT_TRUE(fake_audio_capture_module_->Recording()); | |
192 | |
193 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); | |
194 EXPECT_GE(0, pull_iterations()); | |
195 | |
196 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); | |
197 EXPECT_FALSE(fake_audio_capture_module_->Recording()); | |
198 } | |
199 | |
200 TEST_F(FakeAdmTest, DuplexTest) { | |
201 EXPECT_EQ(0, fake_audio_capture_module_->RegisterAudioCallback(this)); | |
202 | |
203 EXPECT_EQ(0, fake_audio_capture_module_->InitPlayout()); | |
204 EXPECT_EQ(0, fake_audio_capture_module_->StartPlayout()); | |
205 | |
206 EXPECT_EQ(0, fake_audio_capture_module_->InitRecording()); | |
207 EXPECT_EQ(0, fake_audio_capture_module_->StartRecording()); | |
208 | |
209 EXPECT_TRUE_WAIT(push_iterations() > 0, kMsInSecond); | |
210 EXPECT_TRUE_WAIT(pull_iterations() > 0, kMsInSecond); | |
211 | |
212 EXPECT_EQ(0, fake_audio_capture_module_->StopPlayout()); | |
213 EXPECT_EQ(0, fake_audio_capture_module_->StopRecording()); | |
214 } | |
OLD | NEW |