blob: a87def4aac392a2ce3ba136b0abcbf891c645a44 [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
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013#include <stdlib.h>
14
15#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Niels Möller84255bb2017-10-06 13:43:23 +020018#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#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) {
Karl Wiberg08126342018-03-20 19:18:55 +010030 DecoderDatabase db(new rtc::RefCountedObject<MockAudioDecoderFactory>,
Danil Chapovalovb6021232018-06-19 13:26:36 +020031 absl::nullopt);
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) {
Karl Wiberg31fbb542017-10-16 12:42:38 +020037 rtc::scoped_refptr<MockAudioDecoderFactory> factory(
38 new rtc::RefCountedObject<MockAudioDecoderFactory>);
39 EXPECT_CALL(*factory, IsSupportedDecoder(_))
40 .WillOnce(Invoke([](const SdpAudioFormat& format) {
41 EXPECT_EQ("pcmu", format.name);
42 return true;
43 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +020044 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080046 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
47 EXPECT_EQ(
48 DecoderDatabase::kOK,
49 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCMu, kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050 EXPECT_EQ(1, db.Size());
51 EXPECT_FALSE(db.Empty());
52 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
53 EXPECT_EQ(0, db.Size());
54 EXPECT_TRUE(db.Empty());
55}
56
kwiberg6b19b562016-09-20 04:02:25 -070057TEST(DecoderDatabase, InsertAndRemoveAll) {
Karl Wiberg31fbb542017-10-16 12:42:38 +020058 rtc::scoped_refptr<MockAudioDecoderFactory> factory(
59 new rtc::RefCountedObject<MockAudioDecoderFactory>);
60 EXPECT_CALL(*factory, IsSupportedDecoder(_))
61 .WillOnce(Invoke([](const SdpAudioFormat& format) {
62 EXPECT_EQ("pcmu", format.name);
63 return true;
64 }))
65 .WillOnce(Invoke([](const SdpAudioFormat& format) {
66 EXPECT_EQ("pcma", format.name);
67 return true;
68 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +020069 DecoderDatabase db(factory, absl::nullopt);
kwiberg6b19b562016-09-20 04:02:25 -070070 const std::string kCodecName1 = "Robert\'); DROP TABLE Students;";
71 const std::string kCodecName2 = "https://xkcd.com/327/";
72 EXPECT_EQ(DecoderDatabase::kOK,
73 db.RegisterPayload(0, NetEqDecoder::kDecoderPCMu, kCodecName1));
74 EXPECT_EQ(DecoderDatabase::kOK,
75 db.RegisterPayload(1, NetEqDecoder::kDecoderPCMa, kCodecName2));
76 EXPECT_EQ(2, db.Size());
77 EXPECT_FALSE(db.Empty());
78 db.RemoveAll();
79 EXPECT_EQ(0, db.Size());
80 EXPECT_TRUE(db.Empty());
81}
82
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083TEST(DecoderDatabase, GetDecoderInfo) {
kwibergc0f2dcf2016-05-31 06:28:03 -070084 rtc::scoped_refptr<MockAudioDecoderFactory> factory(
85 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wiberg31fbb542017-10-16 12:42:38 +020086 EXPECT_CALL(*factory, IsSupportedDecoder(_))
87 .WillOnce(Invoke([](const SdpAudioFormat& format) {
88 EXPECT_EQ("pcmu", format.name);
89 return true;
90 }));
kwibergc0f2dcf2016-05-31 06:28:03 -070091 auto* decoder = new MockAudioDecoder;
Karl Wibergd6fbf2a2018-02-27 13:37:31 +010092 EXPECT_CALL(*factory, MakeAudioDecoderMock(_, _, _))
kwibergc0f2dcf2016-05-31 06:28:03 -070093 .WillOnce(Invoke([decoder](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020094 absl::optional<AudioCodecPairId> codec_pair_id,
kwibergc0f2dcf2016-05-31 06:28:03 -070095 std::unique_ptr<AudioDecoder>* dec) {
96 EXPECT_EQ("pcmu", format.name);
97 dec->reset(decoder);
98 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +020099 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800101 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
102 EXPECT_EQ(
103 DecoderDatabase::kOK,
104 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCMu, kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105 const DecoderDatabase::DecoderInfo* info;
106 info = db.GetDecoderInfo(kPayloadType);
107 ASSERT_TRUE(info != NULL);
ossuf1b08da2016-09-23 02:19:43 -0700108 EXPECT_TRUE(info->IsType("pcmu"));
kwiberge9413062016-11-03 05:29:05 -0700109 EXPECT_EQ(kCodecName, info->get_name());
kwibergc0f2dcf2016-05-31 06:28:03 -0700110 EXPECT_EQ(decoder, db.GetDecoder(kPayloadType));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111 info = db.GetDecoderInfo(kPayloadType + 1); // Other payload type.
Yves Gerey665174f2018-06-19 15:03:05 +0200112 EXPECT_TRUE(info == NULL); // Should not be found.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113}
114
henrik.lundin@webrtc.orgbf93fb32014-05-14 10:42:03 +0000115TEST(DecoderDatabase, GetDecoder) {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200116 DecoderDatabase db(CreateBuiltinAudioDecoderFactory(), absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800118 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800120 db.RegisterPayload(kPayloadType, NetEqDecoder::kDecoderPCM16B,
121 kCodecName));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122 AudioDecoder* dec = db.GetDecoder(kPayloadType);
123 ASSERT_TRUE(dec != NULL);
124}
125
126TEST(DecoderDatabase, TypeTests) {
Karl Wiberg31fbb542017-10-16 12:42:38 +0200127 rtc::scoped_refptr<MockAudioDecoderFactory> factory(
128 new rtc::RefCountedObject<MockAudioDecoderFactory>);
129 EXPECT_CALL(*factory, IsSupportedDecoder(_))
130 .WillOnce(Invoke([](const SdpAudioFormat& format) {
131 EXPECT_EQ("pcmu", format.name);
132 return true;
133 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200134 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135 const uint8_t kPayloadTypePcmU = 0;
136 const uint8_t kPayloadTypeCng = 13;
137 const uint8_t kPayloadTypeDtmf = 100;
138 const uint8_t kPayloadTypeRed = 101;
139 const uint8_t kPayloadNotUsed = 102;
140 // Load into database.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800141 EXPECT_EQ(
142 DecoderDatabase::kOK,
143 db.RegisterPayload(kPayloadTypePcmU, NetEqDecoder::kDecoderPCMu, "pcmu"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144 EXPECT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800145 db.RegisterPayload(kPayloadTypeCng, NetEqDecoder::kDecoderCNGnb,
146 "cng-nb"));
147 EXPECT_EQ(
148 DecoderDatabase::kOK,
149 db.RegisterPayload(kPayloadTypeDtmf, NetEqDecoder::kDecoderAVT, "avt"));
150 EXPECT_EQ(
151 DecoderDatabase::kOK,
152 db.RegisterPayload(kPayloadTypeRed, NetEqDecoder::kDecoderRED, "red"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000153 EXPECT_EQ(4, db.Size());
154 // Test.
155 EXPECT_FALSE(db.IsComfortNoise(kPayloadNotUsed));
156 EXPECT_FALSE(db.IsDtmf(kPayloadNotUsed));
157 EXPECT_FALSE(db.IsRed(kPayloadNotUsed));
158 EXPECT_FALSE(db.IsComfortNoise(kPayloadTypePcmU));
159 EXPECT_FALSE(db.IsDtmf(kPayloadTypePcmU));
160 EXPECT_FALSE(db.IsRed(kPayloadTypePcmU));
ossuf1b08da2016-09-23 02:19:43 -0700161 EXPECT_FALSE(db.IsType(kPayloadTypePcmU, "isac"));
162 EXPECT_TRUE(db.IsType(kPayloadTypePcmU, "pcmu"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163 EXPECT_TRUE(db.IsComfortNoise(kPayloadTypeCng));
164 EXPECT_TRUE(db.IsDtmf(kPayloadTypeDtmf));
165 EXPECT_TRUE(db.IsRed(kPayloadTypeRed));
166}
167
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168TEST(DecoderDatabase, CheckPayloadTypes) {
Karl Wiberg31fbb542017-10-16 12:42:38 +0200169 constexpr int kNumPayloads = 10;
170 rtc::scoped_refptr<MockAudioDecoderFactory> factory(
171 new rtc::RefCountedObject<MockAudioDecoderFactory>);
172 EXPECT_CALL(*factory, IsSupportedDecoder(_))
173 .Times(kNumPayloads)
174 .WillRepeatedly(Invoke([](const SdpAudioFormat& format) {
175 EXPECT_EQ("pcmu", format.name);
176 return true;
177 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200178 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179 // Load a number of payloads into the database. Payload types are 0, 1, ...,
180 // while the decoder type is the same for all payload types (this does not
181 // matter for the test).
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 for (uint8_t payload_type = 0; payload_type < kNumPayloads; ++payload_type) {
ossuf1b08da2016-09-23 02:19:43 -0700183 EXPECT_EQ(DecoderDatabase::kOK,
184 db.RegisterPayload(payload_type, NetEqDecoder::kDecoderPCMu, ""));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000185 }
186 PacketList packet_list;
187 for (int i = 0; i < kNumPayloads + 1; ++i) {
188 // Create packet with payload type |i|. The last packet will have a payload
189 // type that is not registered in the decoder database.
ossua73f6c92016-10-24 08:25:28 -0700190 Packet packet;
191 packet.payload_type = i;
192 packet_list.push_back(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193 }
194
195 // Expect to return false, since the last packet is of an unknown type.
196 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
197 db.CheckPayloadTypes(packet_list));
198
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000199 packet_list.pop_back(); // Remove the unknown one.
200
201 EXPECT_EQ(DecoderDatabase::kOK, db.CheckPayloadTypes(packet_list));
202
203 // Delete all packets.
204 PacketList::iterator it = packet_list.begin();
205 while (it != packet_list.end()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206 it = packet_list.erase(it);
207 }
208}
209
kwiberg98ab3a42015-09-30 21:54:21 -0700210#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
211#define IF_ISAC(x) x
212#else
213#define IF_ISAC(x) DISABLED_##x
214#endif
215
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000216// Test the methods for setting and getting active speech and CNG decoders.
kwiberg98ab3a42015-09-30 21:54:21 -0700217TEST(DecoderDatabase, IF_ISAC(ActiveDecoders)) {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200218 DecoderDatabase db(CreateBuiltinAudioDecoderFactory(), absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000219 // Load payload types.
kwibergee1879c2015-10-29 06:20:28 -0700220 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800221 db.RegisterPayload(0, NetEqDecoder::kDecoderPCMu, "pcmu"));
kwibergee1879c2015-10-29 06:20:28 -0700222 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800223 db.RegisterPayload(103, NetEqDecoder::kDecoderISAC, "isac"));
kwibergee1879c2015-10-29 06:20:28 -0700224 ASSERT_EQ(DecoderDatabase::kOK,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800225 db.RegisterPayload(13, NetEqDecoder::kDecoderCNGnb, "cng-nb"));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000226 // Verify that no decoders are active from the start.
227 EXPECT_EQ(NULL, db.GetActiveDecoder());
228 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
229
230 // Set active speech codec.
231 bool changed; // Should be true when the active decoder changed.
232 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
233 EXPECT_TRUE(changed);
234 AudioDecoder* decoder = db.GetActiveDecoder();
235 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000236
237 // Set the same again. Expect no change.
238 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
239 EXPECT_FALSE(changed);
240 decoder = db.GetActiveDecoder();
241 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000242
243 // Change active decoder.
244 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(103, &changed));
245 EXPECT_TRUE(changed);
246 decoder = db.GetActiveDecoder();
247 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000248
249 // Remove the active decoder, and verify that the active becomes NULL.
250 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(103));
251 EXPECT_EQ(NULL, db.GetActiveDecoder());
252
253 // Set active CNG codec.
254 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveCngDecoder(13));
ossu97ba30e2016-04-25 07:55:58 -0700255 ComfortNoiseDecoder* cng = db.GetActiveCngDecoder();
256 ASSERT_FALSE(cng == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000257
258 // Remove the active CNG decoder, and verify that the active becomes NULL.
259 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(13));
260 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
261
262 // Try to set non-existing codecs as active.
263 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
264 db.SetActiveDecoder(17, &changed));
Yves Gerey665174f2018-06-19 15:03:05 +0200265 EXPECT_EQ(DecoderDatabase::kDecoderNotFound, db.SetActiveCngDecoder(17));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266}
267} // namespace webrtc