blob: e473ddf15e7286fc1ce0b4600eaacf7a3bc56dfd [file] [log] [blame]
turaj@webrtc.org42259e72012-12-11 02:15:12 +00001/*
2 * Copyright (c) 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/voice_engine/include/voe_codec.h"
12
13#include "gtest/gtest.h"
14#include "webrtc/modules/audio_device/include/audio_device.h"
15#include "webrtc/modules/audio_device/include/audio_device_defines.h"
16#include "webrtc/system_wrappers/interface/scoped_ptr.h"
17#include "webrtc/voice_engine/include/voe_base.h"
18#include "webrtc/voice_engine/include/voe_hardware.h"
19#include "webrtc/voice_engine/voice_engine_defines.h"
20
21namespace webrtc {
22namespace voe {
23namespace {
24
25
26class FakeAudioDeviceModule : public AudioDeviceModule {
27 public:
28 FakeAudioDeviceModule() {}
29 ~FakeAudioDeviceModule() {}
30 virtual int32_t AddRef() { return 0; }
31 virtual int32_t Release() { return 0; }
32 virtual int32_t RegisterEventObserver(AudioDeviceObserver* eventCallback) {
33 return 0;
34 }
35 virtual int32_t RegisterAudioCallback(AudioTransport* audioCallback) {
36 return 0;
37 }
38 virtual int32_t Init() { return 0; }
39 virtual int32_t SpeakerIsAvailable(bool* available) {
40 *available = true;
41 return 0;
42 }
43 virtual int32_t InitSpeaker() { return 0; }
44 virtual int32_t SetPlayoutDevice(uint16_t index) { return 0; }
45 virtual int32_t SetPlayoutDevice(WindowsDeviceType device) { return 0; }
46 virtual int32_t SetStereoPlayout(bool enable) { return 0; }
47 virtual int32_t StopPlayout() { return 0; }
48 virtual int32_t MicrophoneIsAvailable(bool* available) {
49 *available = true;
50 return 0;
51 }
52 virtual int32_t InitMicrophone() { return 0; }
53 virtual int32_t SetRecordingDevice(uint16_t index) { return 0; }
54 virtual int32_t SetRecordingDevice(WindowsDeviceType device) { return 0; }
55 virtual int32_t SetStereoRecording(bool enable) { return 0; }
56 virtual int32_t SetAGC(bool enable) { return 0; }
57 virtual int32_t StopRecording() { return 0; }
58 virtual int32_t TimeUntilNextProcess() { return 0; }
59 virtual int32_t Process() { return 0; }
60 virtual int32_t Terminate() { return 0; }
61
62 virtual int32_t ActiveAudioLayer(AudioLayer* audioLayer) const {
63 assert(false);
64 return 0;
65 }
66 virtual ErrorCode LastError() const {
67 assert(false);
68 return kAdmErrNone;
69 }
70 virtual bool Initialized() const {
71 assert(false);
72 return true;
73 }
74 virtual int16_t PlayoutDevices() {
75 assert(false);
76 return 0;
77 }
78 virtual int16_t RecordingDevices() {
79 assert(false);
80 return 0;
81 }
82 virtual int32_t PlayoutDeviceName(uint16_t index,
83 char name[kAdmMaxDeviceNameSize],
84 char guid[kAdmMaxGuidSize]) {
85 assert(false);
86 return 0;
87 }
88 virtual int32_t RecordingDeviceName(uint16_t index,
89 char name[kAdmMaxDeviceNameSize],
90 char guid[kAdmMaxGuidSize]) {
91 assert(false);
92 return 0;
93 }
94 virtual int32_t PlayoutIsAvailable(bool* available) {
95 assert(false);
96 return 0;
97 }
98 virtual int32_t InitPlayout() {
99 assert(false);
100 return 0;
101 }
102 virtual bool PlayoutIsInitialized() const {
103 assert(false);
104 return true;
105 }
106 virtual int32_t RecordingIsAvailable(bool* available) {
107 assert(false);
108 return 0;
109 }
110 virtual int32_t InitRecording() {
111 assert(false);
112 return 0;
113 }
114 virtual bool RecordingIsInitialized() const {
115 assert(false);
116 return true;
117 }
118 virtual int32_t StartPlayout() {
119 assert(false);
120 return 0;
121 }
122 virtual bool Playing() const {
123 assert(false);
124 return false;
125 }
126 virtual int32_t StartRecording() {
127 assert(false);
128 return 0;
129 }
130 virtual bool Recording() const {
131 assert(false);
132 return false;
133 }
134 virtual bool AGC() const {
135 assert(false);
136 return true;
137 }
138 virtual int32_t SetWaveOutVolume(uint16_t volumeLeft,
139 uint16_t volumeRight) {
140 assert(false);
141 return 0;
142 }
143 virtual int32_t WaveOutVolume(uint16_t* volumeLeft,
144 uint16_t* volumeRight) const {
145 assert(false);
146 return 0;
147 }
148 virtual bool SpeakerIsInitialized() const {
149 assert(false);
150 return true;
151 }
152 virtual bool MicrophoneIsInitialized() const {
153 assert(false);
154 return true;
155 }
156 virtual int32_t SpeakerVolumeIsAvailable(bool* available) {
157 assert(false);
158 return 0;
159 }
160 virtual int32_t SetSpeakerVolume(uint32_t volume) {
161 assert(false);
162 return 0;
163 }
164 virtual int32_t SpeakerVolume(uint32_t* volume) const {
165 assert(false);
166 return 0;
167 }
168 virtual int32_t MaxSpeakerVolume(uint32_t* maxVolume) const {
169 assert(false);
170 return 0;
171 }
172 virtual int32_t MinSpeakerVolume(uint32_t* minVolume) const {
173 assert(false);
174 return 0;
175 }
176 virtual int32_t SpeakerVolumeStepSize(uint16_t* stepSize) const {
177 assert(false);
178 return 0;
179 }
180 virtual int32_t MicrophoneVolumeIsAvailable(bool* available) {
181 assert(false);
182 return 0;
183 }
184 virtual int32_t SetMicrophoneVolume(uint32_t volume) {
185 assert(false);
186 return 0;
187 }
188 virtual int32_t MicrophoneVolume(uint32_t* volume) const {
189 assert(false);
190 return 0;
191 }
192 virtual int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const {
193 assert(false);
194 return 0;
195 }
196 virtual int32_t MinMicrophoneVolume(uint32_t* minVolume) const {
197 assert(false);
198 return 0;
199 }
200 virtual int32_t MicrophoneVolumeStepSize(uint16_t* stepSize) const {
201 assert(false);
202 return 0;
203 }
204 virtual int32_t SpeakerMuteIsAvailable(bool* available) {
205 assert(false);
206 return 0;
207 }
208 virtual int32_t SetSpeakerMute(bool enable) {
209 assert(false);
210 return 0;
211 }
212 virtual int32_t SpeakerMute(bool* enabled) const {
213 assert(false);
214 return 0;
215 }
216 virtual int32_t MicrophoneMuteIsAvailable(bool* available) {
217 assert(false);
218 return 0;
219 }
220 virtual int32_t SetMicrophoneMute(bool enable) {
221 assert(false);
222 return 0;
223 }
224 virtual int32_t MicrophoneMute(bool* enabled) const {
225 assert(false);
226 return 0;
227 }
228 virtual int32_t MicrophoneBoostIsAvailable(bool* available) {
229 assert(false);
230 return 0;
231 }
232 virtual int32_t SetMicrophoneBoost(bool enable) {
233 assert(false);
234 return 0;
235 }
236 virtual int32_t MicrophoneBoost(bool* enabled) const {
237 assert(false);
238 return 0;
239 }
240 virtual int32_t StereoPlayoutIsAvailable(bool* available) const {
241 *available = false;
242 return 0;
243 }
244 virtual int32_t StereoPlayout(bool* enabled) const {
245 assert(false);
246 return 0;
247 }
248 virtual int32_t StereoRecordingIsAvailable(bool* available) const {
249 *available = false;
250 return 0;
251 }
252 virtual int32_t StereoRecording(bool* enabled) const {
253 assert(false);
254 return 0;
255 }
256 virtual int32_t SetRecordingChannel(const ChannelType channel) {
257 assert(false);
258 return 0;
259 }
260 virtual int32_t RecordingChannel(ChannelType* channel) const {
261 assert(false);
262 return 0;
263 }
264 virtual int32_t SetPlayoutBuffer(const BufferType type,
265 uint16_t sizeMS = 0) {
266 assert(false);
267 return 0;
268 }
269 virtual int32_t PlayoutBuffer(BufferType* type, uint16_t* sizeMS) const {
270 assert(false);
271 return 0;
272 }
273 virtual int32_t PlayoutDelay(uint16_t* delayMS) const {
274 assert(false);
275 return 0;
276 }
277 virtual int32_t RecordingDelay(uint16_t* delayMS) const {
278 assert(false);
279 return 0;
280 }
281 virtual int32_t CPULoad(uint16_t* load) const {
282 assert(false);
283 return 0;
284 }
285 virtual int32_t StartRawOutputFileRecording(
286 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) {
287 assert(false);
288 return 0;
289 }
290 virtual int32_t StopRawOutputFileRecording() {
291 assert(false);
292 return 0;
293 }
294 virtual int32_t StartRawInputFileRecording(
295 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) {
296 assert(false);
297 return 0;
298 }
299 virtual int32_t StopRawInputFileRecording() {
300 assert(false);
301 return 0;
302 }
303 virtual int32_t SetRecordingSampleRate(const uint32_t samplesPerSec) {
304 assert(false);
305 return 0;
306 }
307 virtual int32_t RecordingSampleRate(uint32_t* samplesPerSec) const {
308 assert(false);
309 return 0;
310 }
311 virtual int32_t SetPlayoutSampleRate(const uint32_t samplesPerSec) {
312 assert(false);
313 return 0;
314 }
315 virtual int32_t PlayoutSampleRate(uint32_t* samplesPerSec) const {
316 assert(false);
317 return 0;
318 }
319 virtual int32_t ResetAudioDevice() {
320 assert(false);
321 return 0;
322 }
323 virtual int32_t SetLoudspeakerStatus(bool enable) {
324 assert(false);
325 return 0;
326 }
327 virtual int32_t GetLoudspeakerStatus(bool* enabled) const {
328 assert(false);
329 return 0;
330 }
331 virtual int32_t EnableBuiltInAEC(bool enable) {
332 assert(false);
333 return -1;
334 }
335 virtual bool BuiltInAECIsEnabled() const {
336 assert(false);
337 return false;
338 }
339};
340
341class VoECodecTest : public ::testing::Test {
342 protected:
343 VoECodecTest()
344 : voe_(VoiceEngine::Create()),
345 base_(VoEBase::GetInterface(voe_)),
346 voe_codec_(VoECodec::GetInterface(voe_)),
347 channel_(-1),
348 adm_(new FakeAudioDeviceModule),
349 red_payload_type_(-1) {
350 }
351
352 ~VoECodecTest() {}
353
354 void TearDown() {
355 base_->DeleteChannel(channel_);
356 base_->Terminate();
357 base_->Release();
358 voe_codec_->Release();
359 VoiceEngine::Delete(voe_);
360 }
361
362 void SetUp() {
363 // Check if all components are valid.
364 ASSERT_TRUE(voe_ != NULL);
365 ASSERT_TRUE(base_ != NULL);
366 ASSERT_TRUE(voe_codec_ != NULL);
367 ASSERT_TRUE(adm_.get() != NULL);
368 ASSERT_EQ(0, base_->Init(adm_.get()));
369 channel_ = base_->CreateChannel();
370 ASSERT_NE(-1, channel_);
371
372 CodecInst my_codec;
373
374 bool primary_found = false;
375 bool valid_secondary_found = false;
376 bool invalid_secondary_found = false;
377
378 // Find primary and secondary codecs.
379 int num_codecs = voe_codec_->NumOfCodecs();
380 int n = 0;
381 while (n < num_codecs && (!primary_found || !valid_secondary_found ||
382 !invalid_secondary_found || red_payload_type_ < 0)) {
383 EXPECT_EQ(0, voe_codec_->GetCodec(n, my_codec));
384 if (!STR_CASE_CMP(my_codec.plname, "isac") && my_codec.plfreq == 16000) {
385 memcpy(&valid_secondary_, &my_codec, sizeof(my_codec));
386 valid_secondary_found = true;
387 } else if (!STR_CASE_CMP(my_codec.plname, "isac") &&
388 my_codec.plfreq == 32000) {
389 memcpy(&invalid_secondary_, &my_codec, sizeof(my_codec));
390 invalid_secondary_found = true;
391 } else if (!STR_CASE_CMP(my_codec.plname, "L16") &&
392 my_codec.plfreq == 16000) {
393 memcpy(&primary_, &my_codec, sizeof(my_codec));
394 primary_found = true;
395 } else if (!STR_CASE_CMP(my_codec.plname, "RED")) {
396 red_payload_type_ = my_codec.pltype;
397 }
398 n++;
399 }
400
401 EXPECT_TRUE(primary_found);
402 EXPECT_TRUE(valid_secondary_found);
403 EXPECT_TRUE(invalid_secondary_found);
404 EXPECT_NE(-1, red_payload_type_);
405 }
406
407 VoiceEngine* voe_;
408 VoEBase* base_;
409 VoECodec* voe_codec_;
410 int channel_;
411 CodecInst primary_;
412 CodecInst valid_secondary_;
413 scoped_ptr<FakeAudioDeviceModule> adm_;
414
415 // A codec which is not valid to be registered as secondary codec.
416 CodecInst invalid_secondary_;
417 int red_payload_type_;
418};
419
420
421TEST_F(VoECodecTest, DualStreamSetSecondaryBeforePrimaryFails) {
422 // Setting secondary before a primary is registered should fail.
423 EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_,
424 red_payload_type_));
425 red_payload_type_ = 1;
426}
427
428TEST_F(VoECodecTest, DualStreamRegisterWithWrongInputsFails) {
429 // Register primary codec.
430 EXPECT_EQ(0, voe_codec_->SetSendCodec(channel_, primary_));
431
432 // Wrong secondary.
433 EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_, invalid_secondary_,
434 red_payload_type_));
435
henrike@webrtc.org09738612013-01-22 16:43:45 +0000436 // Wrong payload.
turaj@webrtc.org42259e72012-12-11 02:15:12 +0000437 EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_,
438 -1));
439 // Wrong channel.
440 EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_ + 1,
441 valid_secondary_,
442 red_payload_type_));
443}
444
445TEST_F(VoECodecTest, DualStreamGetSecodaryEncoder) {
446 // Register primary codec.
447 EXPECT_EQ(0, voe_codec_->SetSendCodec(channel_, primary_));
448
449 // Register a valid codec.
450 EXPECT_EQ(0, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_,
451 red_payload_type_));
452 CodecInst my_codec;
453
454 // Get secondary codec from wrong channel.
455 EXPECT_EQ(-1, voe_codec_->GetSecondarySendCodec(channel_ + 1, my_codec));
456
457 // Get secondary and compare.
458 memset(&my_codec, 0, sizeof(my_codec));
459 EXPECT_EQ(0, voe_codec_->GetSecondarySendCodec(channel_, my_codec));
460
461 EXPECT_EQ(valid_secondary_.plfreq, my_codec.plfreq);
462 EXPECT_EQ(valid_secondary_.channels, my_codec.channels);
463 EXPECT_EQ(valid_secondary_.pacsize, my_codec.pacsize);
464 EXPECT_EQ(valid_secondary_.rate, my_codec.rate);
465 EXPECT_EQ(valid_secondary_.pltype, my_codec.pltype);
466 EXPECT_EQ(0, STR_CASE_CMP(valid_secondary_.plname, my_codec.plname));
467}
468
469TEST_F(VoECodecTest, DualStreamRemoveSecondaryCodec) {
470 // Register primary codec.
471 EXPECT_EQ(0, voe_codec_->SetSendCodec(channel_, primary_));
472
473 // Register a valid codec.
474 EXPECT_EQ(0, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_,
475 red_payload_type_));
476 // Remove from wrong channel.
477 EXPECT_EQ(-1, voe_codec_->RemoveSecondarySendCodec(channel_ + 1));
478 EXPECT_EQ(0, voe_codec_->RemoveSecondarySendCodec(channel_));
479
480 CodecInst my_codec;
481
482 // Get should fail, if secondary is removed.
483 EXPECT_EQ(-1, voe_codec_->GetSecondarySendCodec(channel_, my_codec));
484}
485
486} // namespace
487} // namespace voe
488} // namespace webrtc