1/*
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 "testing/gtest/include/gtest/gtest.h"
14#include "webrtc/base/scoped_ptr.h"
15#include "webrtc/modules/audio_device/include/fake_audio_device.h"
16#include "webrtc/voice_engine/include/voe_base.h"
17#include "webrtc/voice_engine/include/voe_hardware.h"
18#include "webrtc/voice_engine/voice_engine_defines.h"
19
20namespace webrtc {
21namespace voe {
22namespace {
23
24class VoECodecTest : public ::testing::Test {
25 protected:
26  VoECodecTest()
27      : voe_(VoiceEngine::Create()),
28        base_(VoEBase::GetInterface(voe_)),
29        voe_codec_(VoECodec::GetInterface(voe_)),
30        channel_(-1),
31        adm_(new FakeAudioDeviceModule),
32        red_payload_type_(-1) {}
33
34  ~VoECodecTest() {}
35
36  void TearDown() {
37    base_->DeleteChannel(channel_);
38    base_->Terminate();
39    base_->Release();
40    voe_codec_->Release();
41    VoiceEngine::Delete(voe_);
42  }
43
44  void SetUp() {
45    // Check if all components are valid.
46    ASSERT_TRUE(voe_ != NULL);
47    ASSERT_TRUE(base_ != NULL);
48    ASSERT_TRUE(voe_codec_ != NULL);
49    ASSERT_TRUE(adm_.get() != NULL);
50    ASSERT_EQ(0, base_->Init(adm_.get()));
51    channel_ = base_->CreateChannel();
52    ASSERT_NE(-1, channel_);
53
54    CodecInst my_codec;
55
56    bool primary_found = false;
57    bool valid_secondary_found = false;
58    bool invalid_secondary_found = false;
59
60    // Find primary and secondary codecs.
61    int num_codecs = voe_codec_->NumOfCodecs();
62    int n = 0;
63    while (n < num_codecs &&
64           (!primary_found || !valid_secondary_found ||
65            !invalid_secondary_found || red_payload_type_ < 0)) {
66      EXPECT_EQ(0, voe_codec_->GetCodec(n, my_codec));
67      if (!STR_CASE_CMP(my_codec.plname, "isac") && my_codec.plfreq == 16000) {
68        memcpy(&valid_secondary_, &my_codec, sizeof(my_codec));
69        valid_secondary_found = true;
70      } else if (!STR_CASE_CMP(my_codec.plname, "isac") &&
71                 my_codec.plfreq == 32000) {
72        memcpy(&invalid_secondary_, &my_codec, sizeof(my_codec));
73        invalid_secondary_found = true;
74      } else if (!STR_CASE_CMP(my_codec.plname, "L16") &&
75                 my_codec.plfreq == 16000) {
76        memcpy(&primary_, &my_codec, sizeof(my_codec));
77        primary_found = true;
78      } else if (!STR_CASE_CMP(my_codec.plname, "RED")) {
79        red_payload_type_ = my_codec.pltype;
80      }
81      n++;
82    }
83
84    EXPECT_TRUE(primary_found);
85    EXPECT_TRUE(valid_secondary_found);
86    EXPECT_TRUE(invalid_secondary_found);
87    EXPECT_NE(-1, red_payload_type_);
88  }
89
90  VoiceEngine* voe_;
91  VoEBase* base_;
92  VoECodec* voe_codec_;
93  int channel_;
94  CodecInst primary_;
95  CodecInst valid_secondary_;
96  rtc::scoped_ptr<FakeAudioDeviceModule> adm_;
97
98  // A codec which is not valid to be registered as secondary codec.
99  CodecInst invalid_secondary_;
100  int red_payload_type_;
101};
102
103TEST(VoECodecInst, TestCompareCodecInstances) {
104  CodecInst codec1, codec2;
105  memset(&codec1, 0, sizeof(CodecInst));
106  memset(&codec2, 0, sizeof(CodecInst));
107
108  codec1.pltype = 101;
109  strncpy(codec1.plname, "isac", 4);
110  codec1.plfreq = 8000;
111  codec1.pacsize = 110;
112  codec1.channels = 1;
113  codec1.rate = 8000;
114  memcpy(&codec2, &codec1, sizeof(CodecInst));
115  // Compare two codecs now.
116  EXPECT_TRUE(codec1 == codec2);
117  EXPECT_FALSE(codec1 != codec2);
118
119  // Changing pltype.
120  codec2.pltype = 102;
121  EXPECT_FALSE(codec1 == codec2);
122  EXPECT_TRUE(codec1 != codec2);
123
124  // Reset to codec2 to codec1 state.
125  memcpy(&codec2, &codec1, sizeof(CodecInst));
126  // payload name should be case insensitive.
127  strncpy(codec2.plname, "ISAC", 4);
128  EXPECT_TRUE(codec1 == codec2);
129
130  // Test modifying the |plfreq|
131  codec2.plfreq = 16000;
132  EXPECT_FALSE(codec1 == codec2);
133
134  // Reset to codec2 to codec1 state.
135  memcpy(&codec2, &codec1, sizeof(CodecInst));
136  // Test modifying the |pacsize|.
137  codec2.pacsize = 440;
138  EXPECT_FALSE(codec1 == codec2);
139
140  // Reset to codec2 to codec1 state.
141  memcpy(&codec2, &codec1, sizeof(CodecInst));
142  // Test modifying the |channels|.
143  codec2.channels = 2;
144  EXPECT_FALSE(codec1 == codec2);
145
146  // Reset to codec2 to codec1 state.
147  memcpy(&codec2, &codec1, sizeof(CodecInst));
148  // Test modifying the |rate|.
149  codec2.rate = 0;
150  EXPECT_FALSE(codec1 == codec2);
151}
152
153}  // namespace
154}  // namespace voe
155}  // namespace webrtc
156