blob: be3c0b7ab8f673489a6bf7db458c9a1d61613930 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/decoder_database.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
13#include <assert.h>
14#include <stdlib.h>
15
16#include <string>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
19#include "test/gmock.h"
20#include "test/gtest.h"
21#include "test/mock_audio_decoder.h"
22#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023
kwibergc0f2dcf2016-05-31 06:28:03 -070024using testing::_;
25using testing::Invoke;
26
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027namespace webrtc {
28
29TEST(DecoderDatabase, CreateAndDestroy) {
ossue725f7c2016-05-19 10:48:04 -070030 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031 EXPECT_EQ(0, db.Size());
32 EXPECT_TRUE(db.Empty());
33}
34
35TEST(DecoderDatabase, InsertAndRemove) {
ossue725f7c2016-05-19 10:48:04 -070036 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080038 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
39 EXPECT_EQ(
40 DecoderDatabase::kOK,
41 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCMu, kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042 EXPECT_EQ(1, db.Size());
43 EXPECT_FALSE(db.Empty());
44 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
45 EXPECT_EQ(0, db.Size());
46 EXPECT_TRUE(db.Empty());
47}
48
kwiberg6b19b562016-09-20 04:02:25 -070049TEST(DecoderDatabase, InsertAndRemoveAll) {
50 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
51 const std::string kCodecName1 = "Robert\'); DROP TABLE Students;";
52 const std::string kCodecName2 = "https://xkcd.com/327/";
53 EXPECT_EQ(DecoderDatabase::kOK,
54 db.RegisterPayload(0, NetEqDecoder::kDecoderPCMu, kCodecName1));
55 EXPECT_EQ(DecoderDatabase::kOK,
56 db.RegisterPayload(1, NetEqDecoder::kDecoderPCMa, kCodecName2));
57 EXPECT_EQ(2, db.Size());
58 EXPECT_FALSE(db.Empty());
59 db.RemoveAll();
60 EXPECT_EQ(0, db.Size());
61 EXPECT_TRUE(db.Empty());
62}
63
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064TEST(DecoderDatabase, GetDecoderInfo) {
kwibergc0f2dcf2016-05-31 06:28:03 -070065 rtc::scoped_refptr<MockAudioDecoderFactory> factory(
66 new rtc::RefCountedObject<MockAudioDecoderFactory>);
67 auto* decoder = new MockAudioDecoder;
68 EXPECT_CALL(*factory, MakeAudioDecoderMock(_, _))
69 .WillOnce(Invoke([decoder](const SdpAudioFormat& format,
70 std::unique_ptr<AudioDecoder>* dec) {
71 EXPECT_EQ("pcmu", format.name);
72 dec->reset(decoder);
73 }));
74 DecoderDatabase db(factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080076 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
77 EXPECT_EQ(
78 DecoderDatabase::kOK,
79 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCMu, kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 const DecoderDatabase::DecoderInfo* info;
81 info = db.GetDecoderInfo(kPayloadType);
82 ASSERT_TRUE(info != NULL);
ossuf1b08da2016-09-23 02:19:43 -070083 EXPECT_TRUE(info->IsType("pcmu"));
kwiberge9413062016-11-03 05:29:05 -070084 EXPECT_EQ(kCodecName, info->get_name());
kwibergc0f2dcf2016-05-31 06:28:03 -070085 EXPECT_EQ(decoder, db.GetDecoder(kPayloadType));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086 info = db.GetDecoderInfo(kPayloadType + 1); // Other payload type.
87 EXPECT_TRUE(info == NULL); // Should not be found.
88}
89
henrik.lundin@webrtc.orgbf93fb32014-05-14 10:42:03 +000090TEST(DecoderDatabase, GetDecoder) {
kwiberg5178ee82016-05-03 01:39:01 -070091 DecoderDatabase db(CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080093 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080095 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCM16B,
96 kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097 AudioDecoder* dec = db.GetDecoder(kPayloadType);
98 ASSERT_TRUE(dec != NULL);
99}
100
101TEST(DecoderDatabase, TypeTests) {
ossue725f7c2016-05-19 10:48:04 -0700102 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000103 const uint8_t kPayloadTypePcmU = 0;
104 const uint8_t kPayloadTypeCng = 13;
105 const uint8_t kPayloadTypeDtmf = 100;
106 const uint8_t kPayloadTypeRed = 101;
107 const uint8_t kPayloadNotUsed = 102;
108 // Load into database.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800109 EXPECT_EQ(
110 DecoderDatabase::kOK,
111 db.RegisterPayload(kPayloadTypePcmU, NetEqDecoder::kDecoderPCMu, "pcmu"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800113 db.RegisterPayload(kPayloadTypeCng, NetEqDecoder::kDecoderCNGnb,
114 "cng-nb"));
115 EXPECT_EQ(
116 DecoderDatabase::kOK,
117 db.RegisterPayload(kPayloadTypeDtmf, NetEqDecoder::kDecoderAVT, "avt"));
118 EXPECT_EQ(
119 DecoderDatabase::kOK,
120 db.RegisterPayload(kPayloadTypeRed, NetEqDecoder::kDecoderRED, "red"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 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));
ossuf1b08da2016-09-23 02:19:43 -0700129 EXPECT_FALSE(db.IsType(kPayloadTypePcmU, "isac"));
130 EXPECT_TRUE(db.IsType(kPayloadTypePcmU, "pcmu"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000131 EXPECT_TRUE(db.IsComfortNoise(kPayloadTypeCng));
132 EXPECT_TRUE(db.IsDtmf(kPayloadTypeDtmf));
133 EXPECT_TRUE(db.IsRed(kPayloadTypeRed));
134}
135
136TEST(DecoderDatabase, ExternalDecoder) {
ossue725f7c2016-05-19 10:48:04 -0700137 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000138 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800139 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000140 MockAudioDecoder decoder;
141 // Load into database.
142 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800143 db.InsertExternal(kPayloadType, NetEqDecoder::kDecoderPCMu,
kwiberg342f7402016-06-16 03:18:00 -0700144 kCodecName, &decoder));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000145 EXPECT_EQ(1, db.Size());
146 // Get decoder and make sure we get the external one.
147 EXPECT_EQ(&decoder, db.GetDecoder(kPayloadType));
148 // Get the decoder info struct and check it too.
149 const DecoderDatabase::DecoderInfo* info;
150 info = db.GetDecoderInfo(kPayloadType);
151 ASSERT_TRUE(info != NULL);
ossuf1b08da2016-09-23 02:19:43 -0700152 EXPECT_TRUE(info->IsType("pcmu"));
kwiberge9413062016-11-03 05:29:05 -0700153 EXPECT_EQ(info->get_name(), kCodecName);
154 EXPECT_EQ(kCodecName, info->get_name());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 // Expect not to delete the decoder when removing it from the database, since
156 // it was declared externally.
157 EXPECT_CALL(decoder, Die()).Times(0);
158 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
159 EXPECT_TRUE(db.Empty());
160
161 EXPECT_CALL(decoder, Die()).Times(1); // Will be called when |db| is deleted.
162}
163
164TEST(DecoderDatabase, CheckPayloadTypes) {
ossue725f7c2016-05-19 10:48:04 -0700165 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 // Load a number of payloads into the database. Payload types are 0, 1, ...,
167 // while the decoder type is the same for all payload types (this does not
168 // matter for the test).
169 const int kNumPayloads = 10;
170 for (uint8_t payload_type = 0; payload_type < kNumPayloads; ++payload_type) {
ossuf1b08da2016-09-23 02:19:43 -0700171 EXPECT_EQ(DecoderDatabase::kOK,
172 db.RegisterPayload(payload_type, NetEqDecoder::kDecoderPCMu, ""));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173 }
174 PacketList packet_list;
175 for (int i = 0; i < kNumPayloads + 1; ++i) {
176 // Create packet with payload type |i|. The last packet will have a payload
177 // type that is not registered in the decoder database.
ossua73f6c92016-10-24 08:25:28 -0700178 Packet packet;
179 packet.payload_type = i;
180 packet_list.push_back(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 }
182
183 // Expect to return false, since the last packet is of an unknown type.
184 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
185 db.CheckPayloadTypes(packet_list));
186
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 packet_list.pop_back(); // Remove the unknown one.
188
189 EXPECT_EQ(DecoderDatabase::kOK, db.CheckPayloadTypes(packet_list));
190
191 // Delete all packets.
192 PacketList::iterator it = packet_list.begin();
193 while (it != packet_list.end()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194 it = packet_list.erase(it);
195 }
196}
197
kwiberg98ab3a42015-09-30 21:54:21 -0700198#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
199#define IF_ISAC(x) x
200#else
201#define IF_ISAC(x) DISABLED_##x
202#endif
203
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204// Test the methods for setting and getting active speech and CNG decoders.
kwiberg98ab3a42015-09-30 21:54:21 -0700205TEST(DecoderDatabase, IF_ISAC(ActiveDecoders)) {
kwiberg5178ee82016-05-03 01:39:01 -0700206 DecoderDatabase db(CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207 // Load payload types.
kwibergee1879c2015-10-29 06:20:28 -0700208 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800209 db.RegisterPayload(0, NetEqDecoder::kDecoderPCMu, "pcmu"));
kwibergee1879c2015-10-29 06:20:28 -0700210 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800211 db.RegisterPayload(103, NetEqDecoder::kDecoderISAC, "isac"));
kwibergee1879c2015-10-29 06:20:28 -0700212 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800213 db.RegisterPayload(13, NetEqDecoder::kDecoderCNGnb, "cng-nb"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000214 // Verify that no decoders are active from the start.
215 EXPECT_EQ(NULL, db.GetActiveDecoder());
216 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
217
218 // Set active speech codec.
219 bool changed; // Should be true when the active decoder changed.
220 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
221 EXPECT_TRUE(changed);
222 AudioDecoder* decoder = db.GetActiveDecoder();
223 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000224
225 // Set the same again. Expect no change.
226 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
227 EXPECT_FALSE(changed);
228 decoder = db.GetActiveDecoder();
229 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000230
231 // Change active decoder.
232 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(103, &changed));
233 EXPECT_TRUE(changed);
234 decoder = db.GetActiveDecoder();
235 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000236
237 // Remove the active decoder, and verify that the active becomes NULL.
238 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(103));
239 EXPECT_EQ(NULL, db.GetActiveDecoder());
240
241 // Set active CNG codec.
242 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveCngDecoder(13));
ossu97ba30e2016-04-25 07:55:58 -0700243 ComfortNoiseDecoder* cng = db.GetActiveCngDecoder();
244 ASSERT_FALSE(cng == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000245
246 // Remove the active CNG decoder, and verify that the active becomes NULL.
247 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(13));
248 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
249
250 // Try to set non-existing codecs as active.
251 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
252 db.SetActiveDecoder(17, &changed));
253 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
254 db.SetActiveCngDecoder(17));
255}
256} // namespace webrtc