blob: 33bee8d6f5672f4de8e6f65b286ea603b9abe725 [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"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/ref_counted_object.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
Mirko Bonadei6a489f22019-04-09 15:11:12 +020024using ::testing::_;
25using ::testing::Invoke;
kwibergc0f2dcf2016-05-31 06:28:03 -070026
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027namespace webrtc {
28
29TEST(DecoderDatabase, CreateAndDestroy) {
Tommi87f70902021-04-27 14:43:08 +020030 DecoderDatabase db(rtc::make_ref_counted<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) {
Tommi87f70902021-04-27 14:43:08 +020037 auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
Danil Chapovalovb6021232018-06-19 13:26:36 +020038 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 const uint8_t kPayloadType = 0;
henrik.lundin4cf61dd2015-12-09 06:20:58 -080040 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
41 EXPECT_EQ(
42 DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +010043 db.RegisterPayload(kPayloadType, SdpAudioFormat(kCodecName, 8000, 1)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044 EXPECT_EQ(1, db.Size());
45 EXPECT_FALSE(db.Empty());
46 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(kPayloadType));
47 EXPECT_EQ(0, db.Size());
48 EXPECT_TRUE(db.Empty());
49}
50
kwiberg6b19b562016-09-20 04:02:25 -070051TEST(DecoderDatabase, InsertAndRemoveAll) {
Tommi87f70902021-04-27 14:43:08 +020052 auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
Danil Chapovalovb6021232018-06-19 13:26:36 +020053 DecoderDatabase db(factory, absl::nullopt);
kwiberg6b19b562016-09-20 04:02:25 -070054 const std::string kCodecName1 = "Robert\'); DROP TABLE Students;";
55 const std::string kCodecName2 = "https://xkcd.com/327/";
56 EXPECT_EQ(DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +010057 db.RegisterPayload(0, SdpAudioFormat(kCodecName1, 8000, 1)));
kwiberg6b19b562016-09-20 04:02:25 -070058 EXPECT_EQ(DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +010059 db.RegisterPayload(1, SdpAudioFormat(kCodecName2, 8000, 1)));
kwiberg6b19b562016-09-20 04:02:25 -070060 EXPECT_EQ(2, db.Size());
61 EXPECT_FALSE(db.Empty());
62 db.RemoveAll();
63 EXPECT_EQ(0, db.Size());
64 EXPECT_TRUE(db.Empty());
65}
66
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067TEST(DecoderDatabase, GetDecoderInfo) {
Tommi87f70902021-04-27 14:43:08 +020068 auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
kwibergc0f2dcf2016-05-31 06:28:03 -070069 auto* decoder = new MockAudioDecoder;
Karl Wibergd6fbf2a2018-02-27 13:37:31 +010070 EXPECT_CALL(*factory, MakeAudioDecoderMock(_, _, _))
kwibergc0f2dcf2016-05-31 06:28:03 -070071 .WillOnce(Invoke([decoder](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020072 absl::optional<AudioCodecPairId> codec_pair_id,
kwibergc0f2dcf2016-05-31 06:28:03 -070073 std::unique_ptr<AudioDecoder>* dec) {
74 EXPECT_EQ("pcmu", format.name);
75 dec->reset(decoder);
76 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +020077 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078 const uint8_t kPayloadType = 0;
Niels Möller05543682019-01-10 16:55:06 +010079 const std::string kCodecName = "pcmu";
henrik.lundin4cf61dd2015-12-09 06:20:58 -080080 EXPECT_EQ(
81 DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +010082 db.RegisterPayload(kPayloadType, SdpAudioFormat(kCodecName, 8000, 1)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083 const DecoderDatabase::DecoderInfo* info;
84 info = db.GetDecoderInfo(kPayloadType);
85 ASSERT_TRUE(info != NULL);
ossuf1b08da2016-09-23 02:19:43 -070086 EXPECT_TRUE(info->IsType("pcmu"));
kwiberge9413062016-11-03 05:29:05 -070087 EXPECT_EQ(kCodecName, info->get_name());
kwibergc0f2dcf2016-05-31 06:28:03 -070088 EXPECT_EQ(decoder, db.GetDecoder(kPayloadType));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 info = db.GetDecoderInfo(kPayloadType + 1); // Other payload type.
Yves Gerey665174f2018-06-19 15:03:05 +020090 EXPECT_TRUE(info == NULL); // Should not be found.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091}
92
henrik.lundin@webrtc.orgbf93fb32014-05-14 10:42:03 +000093TEST(DecoderDatabase, GetDecoder) {
Danil Chapovalovb6021232018-06-19 13:26:36 +020094 DecoderDatabase db(CreateBuiltinAudioDecoderFactory(), absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095 const uint8_t kPayloadType = 0;
96 EXPECT_EQ(DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +010097 db.RegisterPayload(kPayloadType, SdpAudioFormat("l16", 8000, 1)));
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) {
Tommi87f70902021-04-27 14:43:08 +0200103 auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200104 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105 const uint8_t kPayloadTypePcmU = 0;
106 const uint8_t kPayloadTypeCng = 13;
107 const uint8_t kPayloadTypeDtmf = 100;
108 const uint8_t kPayloadTypeRed = 101;
109 const uint8_t kPayloadNotUsed = 102;
110 // Load into database.
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800111 EXPECT_EQ(
112 DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +0100113 db.RegisterPayload(kPayloadTypePcmU, SdpAudioFormat("pcmu", 8000, 1)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114 EXPECT_EQ(DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +0100115 db.RegisterPayload(kPayloadTypeCng, SdpAudioFormat("cn", 8000, 1)));
116 EXPECT_EQ(DecoderDatabase::kOK,
117 db.RegisterPayload(kPayloadTypeDtmf,
118 SdpAudioFormat("telephone-event", 8000, 1)));
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800119 EXPECT_EQ(
120 DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +0100121 db.RegisterPayload(kPayloadTypeRed, SdpAudioFormat("red", 8000, 1)));
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
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000137TEST(DecoderDatabase, CheckPayloadTypes) {
Karl Wiberg31fbb542017-10-16 12:42:38 +0200138 constexpr int kNumPayloads = 10;
Tommi87f70902021-04-27 14:43:08 +0200139 auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200140 DecoderDatabase db(factory, absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000141 // Load a number of payloads into the database. Payload types are 0, 1, ...,
142 // while the decoder type is the same for all payload types (this does not
143 // matter for the test).
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144 for (uint8_t payload_type = 0; payload_type < kNumPayloads; ++payload_type) {
Niels Möller05543682019-01-10 16:55:06 +0100145 EXPECT_EQ(
146 DecoderDatabase::kOK,
147 db.RegisterPayload(payload_type, SdpAudioFormat("pcmu", 8000, 1)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148 }
149 PacketList packet_list;
150 for (int i = 0; i < kNumPayloads + 1; ++i) {
151 // Create packet with payload type |i|. The last packet will have a payload
152 // type that is not registered in the decoder database.
ossua73f6c92016-10-24 08:25:28 -0700153 Packet packet;
154 packet.payload_type = i;
155 packet_list.push_back(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 }
157
158 // Expect to return false, since the last packet is of an unknown type.
159 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
160 db.CheckPayloadTypes(packet_list));
161
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162 packet_list.pop_back(); // Remove the unknown one.
163
164 EXPECT_EQ(DecoderDatabase::kOK, db.CheckPayloadTypes(packet_list));
165
166 // Delete all packets.
167 PacketList::iterator it = packet_list.begin();
168 while (it != packet_list.end()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169 it = packet_list.erase(it);
170 }
171}
172
kwiberg98ab3a42015-09-30 21:54:21 -0700173#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
174#define IF_ISAC(x) x
175#else
176#define IF_ISAC(x) DISABLED_##x
177#endif
178
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179// Test the methods for setting and getting active speech and CNG decoders.
kwiberg98ab3a42015-09-30 21:54:21 -0700180TEST(DecoderDatabase, IF_ISAC(ActiveDecoders)) {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200181 DecoderDatabase db(CreateBuiltinAudioDecoderFactory(), absl::nullopt);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 // Load payload types.
kwibergee1879c2015-10-29 06:20:28 -0700183 ASSERT_EQ(DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +0100184 db.RegisterPayload(0, SdpAudioFormat("pcmu", 8000, 1)));
kwibergee1879c2015-10-29 06:20:28 -0700185 ASSERT_EQ(DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +0100186 db.RegisterPayload(103, SdpAudioFormat("isac", 16000, 1)));
kwibergee1879c2015-10-29 06:20:28 -0700187 ASSERT_EQ(DecoderDatabase::kOK,
Niels Möller05543682019-01-10 16:55:06 +0100188 db.RegisterPayload(13, SdpAudioFormat("cn", 8000, 1)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 // Verify that no decoders are active from the start.
190 EXPECT_EQ(NULL, db.GetActiveDecoder());
191 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
192
193 // Set active speech codec.
194 bool changed; // Should be true when the active decoder changed.
195 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
196 EXPECT_TRUE(changed);
197 AudioDecoder* decoder = db.GetActiveDecoder();
198 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000199
200 // Set the same again. Expect no change.
201 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(0, &changed));
202 EXPECT_FALSE(changed);
203 decoder = db.GetActiveDecoder();
204 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205
206 // Change active decoder.
207 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveDecoder(103, &changed));
208 EXPECT_TRUE(changed);
209 decoder = db.GetActiveDecoder();
210 ASSERT_FALSE(decoder == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000211
212 // Remove the active decoder, and verify that the active becomes NULL.
213 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(103));
214 EXPECT_EQ(NULL, db.GetActiveDecoder());
215
216 // Set active CNG codec.
217 EXPECT_EQ(DecoderDatabase::kOK, db.SetActiveCngDecoder(13));
ossu97ba30e2016-04-25 07:55:58 -0700218 ComfortNoiseDecoder* cng = db.GetActiveCngDecoder();
219 ASSERT_FALSE(cng == NULL); // Should get a decoder here.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000220
221 // Remove the active CNG decoder, and verify that the active becomes NULL.
222 EXPECT_EQ(DecoderDatabase::kOK, db.Remove(13));
223 EXPECT_EQ(NULL, db.GetActiveCngDecoder());
224
225 // Try to set non-existing codecs as active.
226 EXPECT_EQ(DecoderDatabase::kDecoderNotFound,
227 db.SetActiveDecoder(17, &changed));
Yves Gerey665174f2018-06-19 15:03:05 +0200228 EXPECT_EQ(DecoderDatabase::kDecoderNotFound, db.SetActiveCngDecoder(17));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000229}
230} // namespace webrtc