• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "modules/audio_coding/neteq/decoder_database.h"
12 
13 #include <stdlib.h>
14 
15 #include <string>
16 
17 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
18 #include "test/gmock.h"
19 #include "test/gtest.h"
20 #include "test/mock_audio_decoder.h"
21 #include "test/mock_audio_decoder_factory.h"
22 
23 using ::testing::_;
24 using ::testing::Invoke;
25 
26 namespace webrtc {
27 
TEST(DecoderDatabase,CreateAndDestroy)28 TEST(DecoderDatabase, CreateAndDestroy) {
29   DecoderDatabase db(rtc::make_ref_counted<MockAudioDecoderFactory>(),
30                      absl::nullopt);
31   EXPECT_EQ(0, db.Size());
32   EXPECT_TRUE(db.Empty());
33 }
34 
TEST(DecoderDatabase,InsertAndRemove)35 TEST(DecoderDatabase, InsertAndRemove) {
36   auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
37   DecoderDatabase db(factory, absl::nullopt);
38   const uint8_t kPayloadType = 0;
39   const std::string kCodecName = "Robert\'); DROP TABLE Students;";
40   EXPECT_EQ(
41       DecoderDatabase::kOK,
42       db.RegisterPayload(kPayloadType, SdpAudioFormat(kCodecName, 8000, 1)));
43   EXPECT_EQ(1, db.Size());
44   EXPECT_FALSE(db.Empty());
45   EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
46   EXPECT_EQ(0, db.Size());
47   EXPECT_TRUE(db.Empty());
48 }
49 
TEST(DecoderDatabase,InsertAndRemoveAll)50 TEST(DecoderDatabase, InsertAndRemoveAll) {
51   auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
52   DecoderDatabase db(factory, absl::nullopt);
53   const std::string kCodecName1 = "Robert\'); DROP TABLE Students;";
54   const std::string kCodecName2 = "https://xkcd.com/327/";
55   EXPECT_EQ(DecoderDatabase::kOK,
56             db.RegisterPayload(0, SdpAudioFormat(kCodecName1, 8000, 1)));
57   EXPECT_EQ(DecoderDatabase::kOK,
58             db.RegisterPayload(1, SdpAudioFormat(kCodecName2, 8000, 1)));
59   EXPECT_EQ(2, db.Size());
60   EXPECT_FALSE(db.Empty());
61   db.RemoveAll();
62   EXPECT_EQ(0, db.Size());
63   EXPECT_TRUE(db.Empty());
64 }
65 
TEST(DecoderDatabase,GetDecoderInfo)66 TEST(DecoderDatabase, GetDecoderInfo) {
67   auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
68   auto* decoder = new MockAudioDecoder;
69   EXPECT_CALL(*factory, MakeAudioDecoderMock(_, _, _))
70       .WillOnce(Invoke([decoder](const SdpAudioFormat& format,
71                                  absl::optional<AudioCodecPairId> codec_pair_id,
72                                  std::unique_ptr<AudioDecoder>* dec) {
73         EXPECT_EQ("pcmu", format.name);
74         dec->reset(decoder);
75       }));
76   DecoderDatabase db(factory, absl::nullopt);
77   const uint8_t kPayloadType = 0;
78   const std::string kCodecName = "pcmu";
79   EXPECT_EQ(
80       DecoderDatabase::kOK,
81       db.RegisterPayload(kPayloadType, SdpAudioFormat(kCodecName, 8000, 1)));
82   const DecoderDatabase::DecoderInfo* info;
83   info = db.GetDecoderInfo(kPayloadType);
84   ASSERT_TRUE(info != NULL);
85   EXPECT_TRUE(info->IsType("pcmu"));
86   EXPECT_EQ(kCodecName, info->get_name());
87   EXPECT_EQ(decoder, db.GetDecoder(kPayloadType));
88   info = db.GetDecoderInfo(kPayloadType + 1);  // Other payload type.
89   EXPECT_TRUE(info == NULL);                   // Should not be found.
90 }
91 
TEST(DecoderDatabase,GetDecoder)92 TEST(DecoderDatabase, GetDecoder) {
93   DecoderDatabase db(CreateBuiltinAudioDecoderFactory(), absl::nullopt);
94   const uint8_t kPayloadType = 0;
95   EXPECT_EQ(DecoderDatabase::kOK,
96             db.RegisterPayload(kPayloadType, SdpAudioFormat("l16", 8000, 1)));
97   AudioDecoder* dec = db.GetDecoder(kPayloadType);
98   ASSERT_TRUE(dec != NULL);
99 }
100 
TEST(DecoderDatabase,TypeTests)101 TEST(DecoderDatabase, TypeTests) {
102   auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
103   DecoderDatabase db(factory, absl::nullopt);
104   const uint8_t kPayloadTypePcmU = 0;
105   const uint8_t kPayloadTypeCng = 13;
106   const uint8_t kPayloadTypeDtmf = 100;
107   const uint8_t kPayloadTypeRed = 101;
108   const uint8_t kPayloadNotUsed = 102;
109   // Load into database.
110   EXPECT_EQ(
111       DecoderDatabase::kOK,
112       db.RegisterPayload(kPayloadTypePcmU, SdpAudioFormat("pcmu", 8000, 1)));
113   EXPECT_EQ(DecoderDatabase::kOK,
114             db.RegisterPayload(kPayloadTypeCng, SdpAudioFormat("cn", 8000, 1)));
115   EXPECT_EQ(DecoderDatabase::kOK,
116             db.RegisterPayload(kPayloadTypeDtmf,
117                                SdpAudioFormat("telephone-event", 8000, 1)));
118   EXPECT_EQ(
119       DecoderDatabase::kOK,
120       db.RegisterPayload(kPayloadTypeRed, SdpAudioFormat("red", 8000, 1)));
121   EXPECT_EQ(4, db.Size());
122   // Test.
123   EXPECT_FALSE(db.IsComfortNoise(kPayloadNotUsed));
124   EXPECT_FALSE(db.IsDtmf(kPayloadNotUsed));
125   EXPECT_FALSE(db.IsRed(kPayloadNotUsed));
126   EXPECT_FALSE(db.IsComfortNoise(kPayloadTypePcmU));
127   EXPECT_FALSE(db.IsDtmf(kPayloadTypePcmU));
128   EXPECT_FALSE(db.IsRed(kPayloadTypePcmU));
129   EXPECT_TRUE(db.IsComfortNoise(kPayloadTypeCng));
130   EXPECT_TRUE(db.IsDtmf(kPayloadTypeDtmf));
131   EXPECT_TRUE(db.IsRed(kPayloadTypeRed));
132 }
133 
TEST(DecoderDatabase,CheckPayloadTypes)134 TEST(DecoderDatabase, CheckPayloadTypes) {
135   constexpr int kNumPayloads = 10;
136   auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
137   DecoderDatabase db(factory, absl::nullopt);
138   // Load a number of payloads into the database. Payload types are 0, 1, ...,
139   // while the decoder type is the same for all payload types (this does not
140   // matter for the test).
141   for (uint8_t payload_type = 0; payload_type < kNumPayloads; ++payload_type) {
142     EXPECT_EQ(
143         DecoderDatabase::kOK,
144         db.RegisterPayload(payload_type, SdpAudioFormat("pcmu", 8000, 1)));
145   }
146   PacketList packet_list;
147   for (int i = 0; i < kNumPayloads + 1; ++i) {
148     // Create packet with payload type `i`. The last packet will have a payload
149     // type that is not registered in the decoder database.
150     Packet packet;
151     packet.payload_type = i;
152     packet_list.push_back(std::move(packet));
153   }
154 
155   // Expect to return false, since the last packet is of an unknown type.
156   EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
157             db.CheckPayloadTypes(packet_list));
158 
159   packet_list.pop_back();  // Remove the unknown one.
160 
161   EXPECT_EQ(DecoderDatabase::kOK, db.CheckPayloadTypes(packet_list));
162 
163   // Delete all packets.
164   PacketList::iterator it = packet_list.begin();
165   while (it != packet_list.end()) {
166     it = packet_list.erase(it);
167   }
168 }
169 
170 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
171 #define IF_ISAC(x) x
172 #else
173 #define IF_ISAC(x) DISABLED_##x
174 #endif
175 
176 // Test the methods for setting and getting active speech and CNG decoders.
TEST(DecoderDatabase,IF_ISAC (ActiveDecoders))177 TEST(DecoderDatabase, IF_ISAC(ActiveDecoders)) {
178   DecoderDatabase db(CreateBuiltinAudioDecoderFactory(), absl::nullopt);
179   // Load payload types.
180   ASSERT_EQ(DecoderDatabase::kOK,
181             db.RegisterPayload(0, SdpAudioFormat("pcmu", 8000, 1)));
182   ASSERT_EQ(DecoderDatabase::kOK,
183             db.RegisterPayload(103, SdpAudioFormat("isac", 16000, 1)));
184   ASSERT_EQ(DecoderDatabase::kOK,
185             db.RegisterPayload(13, SdpAudioFormat("cn", 8000, 1)));
186   // Verify that no decoders are active from the start.
187   EXPECT_EQ(NULL, db.GetActiveDecoder());
188   EXPECT_EQ(NULL, db.GetActiveCngDecoder());
189 
190   // Set active speech codec.
191   bool changed;  // Should be true when the active decoder changed.
192   EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
193   EXPECT_TRUE(changed);
194   AudioDecoder* decoder = db.GetActiveDecoder();
195   ASSERT_FALSE(decoder == NULL);  // Should get a decoder here.
196 
197   // Set the same again. Expect no change.
198   EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
199   EXPECT_FALSE(changed);
200   decoder = db.GetActiveDecoder();
201   ASSERT_FALSE(decoder == NULL);  // Should get a decoder here.
202 
203   // Change active decoder.
204   EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(103, &changed));
205   EXPECT_TRUE(changed);
206   decoder = db.GetActiveDecoder();
207   ASSERT_FALSE(decoder == NULL);  // Should get a decoder here.
208 
209   // Remove the active decoder, and verify that the active becomes NULL.
210   EXPECT_EQ(DecoderDatabase::kOK, db.Remove(103));
211   EXPECT_EQ(NULL, db.GetActiveDecoder());
212 
213   // Set active CNG codec.
214   EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveCngDecoder(13));
215   ComfortNoiseDecoder* cng = db.GetActiveCngDecoder();
216   ASSERT_FALSE(cng == NULL);  // Should get a decoder here.
217 
218   // Remove the active CNG decoder, and verify that the active becomes NULL.
219   EXPECT_EQ(DecoderDatabase::kOK, db.Remove(13));
220   EXPECT_EQ(NULL, db.GetActiveCngDecoder());
221 
222   // Try to set non-existing codecs as active.
223   EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
224             db.SetActiveDecoder(17, &changed));
225   EXPECT_EQ(DecoderDatabase::kDecoderNotFound, db.SetActiveCngDecoder(17));
226 }
227 }  // namespace webrtc
228