blob: 626420af881a80c9543da5fe770c234e7ce1664f [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"
Niels Möller84255bb2017-10-06 13:43:23 +020019#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "test/gmock.h"
21#include "test/gtest.h"
22#include "test/mock_audio_decoder.h"
23#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
kwibergc0f2dcf2016-05-31 06:28:03 -070025using testing::_;
26using testing::Invoke;
27
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000028namespace webrtc {
29
30TEST(DecoderDatabase, CreateAndDestroy) {
ossue725f7c2016-05-19 10:48:04 -070031 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 EXPECT_EQ(0, db.Size());
33 EXPECT_TRUE(db.Empty());
34}
35
36TEST(DecoderDatabase, InsertAndRemove) {
ossue725f7c2016-05-19 10:48:04 -070037 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080039 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
40 EXPECT_EQ(
41 DecoderDatabase::kOK,
42 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCMu, kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043 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
kwiberg6b19b562016-09-20 04:02:25 -070050TEST(DecoderDatabase, InsertAndRemoveAll) {
51 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
52 const std::string kCodecName1 = "Robert\'); DROP TABLE Students;";
53 const std::string kCodecName2 = "https://xkcd.com/327/";
54 EXPECT_EQ(DecoderDatabase::kOK,
55 db.RegisterPayload(0, NetEqDecoder::kDecoderPCMu, kCodecName1));
56 EXPECT_EQ(DecoderDatabase::kOK,
57 db.RegisterPayload(1, NetEqDecoder::kDecoderPCMa, kCodecName2));
58 EXPECT_EQ(2, db.Size());
59 EXPECT_FALSE(db.Empty());
60 db.RemoveAll();
61 EXPECT_EQ(0, db.Size());
62 EXPECT_TRUE(db.Empty());
63}
64
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065TEST(DecoderDatabase, GetDecoderInfo) {
kwibergc0f2dcf2016-05-31 06:28:03 -070066 rtc::scoped_refptr<MockAudioDecoderFactory> factory(
67 new rtc::RefCountedObject<MockAudioDecoderFactory>);
68 auto* decoder = new MockAudioDecoder;
69 EXPECT_CALL(*factory, MakeAudioDecoderMock(_, _))
70 .WillOnce(Invoke([decoder](const SdpAudioFormat& format,
71 std::unique_ptr<AudioDecoder>* dec) {
72 EXPECT_EQ("pcmu", format.name);
73 dec->reset(decoder);
74 }));
75 DecoderDatabase db(factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080077 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
78 EXPECT_EQ(
79 DecoderDatabase::kOK,
80 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCMu, kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 const DecoderDatabase::DecoderInfo* info;
82 info = db.GetDecoderInfo(kPayloadType);
83 ASSERT_TRUE(info != NULL);
ossuf1b08da2016-09-23 02:19:43 -070084 EXPECT_TRUE(info->IsType("pcmu"));
kwiberge9413062016-11-03 05:29:05 -070085 EXPECT_EQ(kCodecName, info->get_name());
kwibergc0f2dcf2016-05-31 06:28:03 -070086 EXPECT_EQ(decoder, db.GetDecoder(kPayloadType));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 info = db.GetDecoderInfo(kPayloadType + 1); // Other payload type.
88 EXPECT_TRUE(info == NULL); // Should not be found.
89}
90
henrik.lundin@webrtc.orgbf93fb32014-05-14 10:42:03 +000091TEST(DecoderDatabase, GetDecoder) {
kwiberg5178ee82016-05-03 01:39:01 -070092 DecoderDatabase db(CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080094 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080096 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCM16B,
97 kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098 AudioDecoder* dec = db.GetDecoder(kPayloadType);
99 ASSERT_TRUE(dec != NULL);
100}
101
102TEST(DecoderDatabase, TypeTests) {
ossue725f7c2016-05-19 10:48:04 -0700103 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104 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.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800110 EXPECT_EQ(
111 DecoderDatabase::kOK,
112 db.RegisterPayload(kPayloadTypePcmU, NetEqDecoder::kDecoderPCMu, "pcmu"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800114 db.RegisterPayload(kPayloadTypeCng, NetEqDecoder::kDecoderCNGnb,
115 "cng-nb"));
116 EXPECT_EQ(
117 DecoderDatabase::kOK,
118 db.RegisterPayload(kPayloadTypeDtmf, NetEqDecoder::kDecoderAVT, "avt"));
119 EXPECT_EQ(
120 DecoderDatabase::kOK,
121 db.RegisterPayload(kPayloadTypeRed, NetEqDecoder::kDecoderRED, "red"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122 EXPECT_EQ(4, db.Size());
123 // Test.
124 EXPECT_FALSE(db.IsComfortNoise(kPayloadNotUsed));
125 EXPECT_FALSE(db.IsDtmf(kPayloadNotUsed));
126 EXPECT_FALSE(db.IsRed(kPayloadNotUsed));
127 EXPECT_FALSE(db.IsComfortNoise(kPayloadTypePcmU));
128 EXPECT_FALSE(db.IsDtmf(kPayloadTypePcmU));
129 EXPECT_FALSE(db.IsRed(kPayloadTypePcmU));
ossuf1b08da2016-09-23 02:19:43 -0700130 EXPECT_FALSE(db.IsType(kPayloadTypePcmU, "isac"));
131 EXPECT_TRUE(db.IsType(kPayloadTypePcmU, "pcmu"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000132 EXPECT_TRUE(db.IsComfortNoise(kPayloadTypeCng));
133 EXPECT_TRUE(db.IsDtmf(kPayloadTypeDtmf));
134 EXPECT_TRUE(db.IsRed(kPayloadTypeRed));
135}
136
137TEST(DecoderDatabase, ExternalDecoder) {
ossue725f7c2016-05-19 10:48:04 -0700138 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000139 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800140 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000141 MockAudioDecoder decoder;
142 // Load into database.
143 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800144 db.InsertExternal(kPayloadType, NetEqDecoder::kDecoderPCMu,
kwiberg342f7402016-06-16 03:18:00 -0700145 kCodecName, &decoder));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146 EXPECT_EQ(1, db.Size());
147 // Get decoder and make sure we get the external one.
148 EXPECT_EQ(&decoder, db.GetDecoder(kPayloadType));
149 // Get the decoder info struct and check it too.
150 const DecoderDatabase::DecoderInfo* info;
151 info = db.GetDecoderInfo(kPayloadType);
152 ASSERT_TRUE(info != NULL);
ossuf1b08da2016-09-23 02:19:43 -0700153 EXPECT_TRUE(info->IsType("pcmu"));
kwiberge9413062016-11-03 05:29:05 -0700154 EXPECT_EQ(info->get_name(), kCodecName);
155 EXPECT_EQ(kCodecName, info->get_name());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 // Expect not to delete the decoder when removing it from the database, since
157 // it was declared externally.
158 EXPECT_CALL(decoder, Die()).Times(0);
159 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
160 EXPECT_TRUE(db.Empty());
161
162 EXPECT_CALL(decoder, Die()).Times(1); // Will be called when |db| is deleted.
163}
164
165TEST(DecoderDatabase, CheckPayloadTypes) {
ossue725f7c2016-05-19 10:48:04 -0700166 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 // Load a number of payloads into the database. Payload types are 0, 1, ...,
168 // while the decoder type is the same for all payload types (this does not
169 // matter for the test).
170 const int kNumPayloads = 10;
171 for (uint8_t payload_type = 0; payload_type < kNumPayloads; ++payload_type) {
ossuf1b08da2016-09-23 02:19:43 -0700172 EXPECT_EQ(DecoderDatabase::kOK,
173 db.RegisterPayload(payload_type, NetEqDecoder::kDecoderPCMu, ""));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 }
175 PacketList packet_list;
176 for (int i = 0; i < kNumPayloads + 1; ++i) {
177 // Create packet with payload type |i|. The last packet will have a payload
178 // type that is not registered in the decoder database.
ossua73f6c92016-10-24 08:25:28 -0700179 Packet packet;
180 packet.payload_type = i;
181 packet_list.push_back(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 }
183
184 // Expect to return false, since the last packet is of an unknown type.
185 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
186 db.CheckPayloadTypes(packet_list));
187
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000188 packet_list.pop_back(); // Remove the unknown one.
189
190 EXPECT_EQ(DecoderDatabase::kOK, db.CheckPayloadTypes(packet_list));
191
192 // Delete all packets.
193 PacketList::iterator it = packet_list.begin();
194 while (it != packet_list.end()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000195 it = packet_list.erase(it);
196 }
197}
198
kwiberg98ab3a42015-09-30 21:54:21 -0700199#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
200#define IF_ISAC(x) x
201#else
202#define IF_ISAC(x) DISABLED_##x
203#endif
204
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205// Test the methods for setting and getting active speech and CNG decoders.
kwiberg98ab3a42015-09-30 21:54:21 -0700206TEST(DecoderDatabase, IF_ISAC(ActiveDecoders)) {
kwiberg5178ee82016-05-03 01:39:01 -0700207 DecoderDatabase db(CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000208 // Load payload types.
kwibergee1879c2015-10-29 06:20:28 -0700209 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800210 db.RegisterPayload(0, NetEqDecoder::kDecoderPCMu, "pcmu"));
kwibergee1879c2015-10-29 06:20:28 -0700211 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800212 db.RegisterPayload(103, NetEqDecoder::kDecoderISAC, "isac"));
kwibergee1879c2015-10-29 06:20:28 -0700213 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800214 db.RegisterPayload(13, NetEqDecoder::kDecoderCNGnb, "cng-nb"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000215 // Verify that no decoders are active from the start.
216 EXPECT_EQ(NULL, db.GetActiveDecoder());
217 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
218
219 // Set active speech codec.
220 bool changed; // Should be true when the active decoder changed.
221 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
222 EXPECT_TRUE(changed);
223 AudioDecoder* decoder = db.GetActiveDecoder();
224 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000225
226 // Set the same again. Expect no change.
227 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
228 EXPECT_FALSE(changed);
229 decoder = db.GetActiveDecoder();
230 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000231
232 // Change active decoder.
233 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(103, &changed));
234 EXPECT_TRUE(changed);
235 decoder = db.GetActiveDecoder();
236 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000237
238 // Remove the active decoder, and verify that the active becomes NULL.
239 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(103));
240 EXPECT_EQ(NULL, db.GetActiveDecoder());
241
242 // Set active CNG codec.
243 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveCngDecoder(13));
ossu97ba30e2016-04-25 07:55:58 -0700244 ComfortNoiseDecoder* cng = db.GetActiveCngDecoder();
245 ASSERT_FALSE(cng == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000246
247 // Remove the active CNG decoder, and verify that the active becomes NULL.
248 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(13));
249 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
250
251 // Try to set non-existing codecs as active.
252 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
253 db.SetActiveDecoder(17, &changed));
254 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
255 db.SetActiveCngDecoder(17));
256}
257} // namespace webrtc