blob: 7bbddd63d40ce8276d97733d4d8dbf9f6ffde340 [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
16#include <list>
17#include <type_traits>
18#include <utility>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
Niels Möller2edab4c2018-10-22 09:48:08 +020020#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/audio_codecs/audio_decoder.h"
22#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020024#include "rtc_base/strings/audio_format_to_string.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025
26namespace webrtc {
27
kwiberg5178ee82016-05-03 01:39:01 -070028DecoderDatabase::DecoderDatabase(
Karl Wiberg08126342018-03-20 19:18:55 +010029 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory,
Danil Chapovalovb6021232018-06-19 13:26:36 +020030 absl::optional<AudioCodecPairId> codec_pair_id)
kwiberg5178ee82016-05-03 01:39:01 -070031 : active_decoder_type_(-1),
32 active_cng_decoder_type_(-1),
Karl Wiberg08126342018-03-20 19:18:55 +010033 decoder_factory_(decoder_factory),
34 codec_pair_id_(codec_pair_id) {}
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000035
ossu97ba30e2016-04-25 07:55:58 -070036DecoderDatabase::~DecoderDatabase() = default;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000037
Karl Wiberg08126342018-03-20 19:18:55 +010038DecoderDatabase::DecoderInfo::DecoderInfo(
39 const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020040 absl::optional<AudioCodecPairId> codec_pair_id,
Karl Wiberg08126342018-03-20 19:18:55 +010041 AudioDecoderFactory* factory,
42 const std::string& codec_name)
kwiberge9413062016-11-03 05:29:05 -070043 : name_(codec_name),
44 audio_format_(audio_format),
Karl Wiberg08126342018-03-20 19:18:55 +010045 codec_pair_id_(codec_pair_id),
ossu84bc9852016-08-26 05:41:23 -070046 factory_(factory),
ossu9f38c212016-10-04 05:23:32 -070047 cng_decoder_(CngDecoder::Create(audio_format)),
48 subtype_(SubtypeFromFormat(audio_format)) {}
kwibergc0f2dcf2016-05-31 06:28:03 -070049
Karl Wiberg08126342018-03-20 19:18:55 +010050DecoderDatabase::DecoderInfo::DecoderInfo(
51 const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020052 absl::optional<AudioCodecPairId> codec_pair_id,
Karl Wiberg08126342018-03-20 19:18:55 +010053 AudioDecoderFactory* factory)
54 : DecoderInfo(audio_format, codec_pair_id, factory, audio_format.name) {}
kwiberge9413062016-11-03 05:29:05 -070055
kwiberg0fa0a972016-04-19 05:03:45 -070056DecoderDatabase::DecoderInfo::DecoderInfo(DecoderInfo&&) = default;
57DecoderDatabase::DecoderInfo::~DecoderInfo() = default;
58
ossu84bc9852016-08-26 05:41:23 -070059AudioDecoder* DecoderDatabase::DecoderInfo::GetDecoder() const {
ossu9f38c212016-10-04 05:23:32 -070060 if (subtype_ != Subtype::kNormal) {
ossuf1b08da2016-09-23 02:19:43 -070061 // These are handled internally, so they have no AudioDecoder objects.
62 return nullptr;
63 }
kwiberg0fa0a972016-04-19 05:03:45 -070064 if (!decoder_) {
ossuf1b08da2016-09-23 02:19:43 -070065 // TODO(ossu): Keep a check here for now, since a number of tests create
66 // DecoderInfos without factories.
ossu84bc9852016-08-26 05:41:23 -070067 RTC_DCHECK(factory_);
Karl Wiberg08126342018-03-20 19:18:55 +010068 decoder_ = factory_->MakeAudioDecoder(audio_format_, codec_pair_id_);
kwiberg0fa0a972016-04-19 05:03:45 -070069 }
Jonas Olssonabbe8412018-04-03 13:40:05 +020070 RTC_DCHECK(decoder_) << "Failed to create: " << rtc::ToString(audio_format_);
kwiberg0fa0a972016-04-19 05:03:45 -070071 return decoder_.get();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072}
73
ossuf1b08da2016-09-23 02:19:43 -070074bool DecoderDatabase::DecoderInfo::IsType(const char* name) const {
Niels Möller2edab4c2018-10-22 09:48:08 +020075 return absl::EqualsIgnoreCase(audio_format_.name, name);
ossuf1b08da2016-09-23 02:19:43 -070076}
77
78bool DecoderDatabase::DecoderInfo::IsType(const std::string& name) const {
79 return IsType(name.c_str());
ossu84bc9852016-08-26 05:41:23 -070080}
81
Danil Chapovalovb6021232018-06-19 13:26:36 +020082absl::optional<DecoderDatabase::DecoderInfo::CngDecoder>
ossuf1b08da2016-09-23 02:19:43 -070083DecoderDatabase::DecoderInfo::CngDecoder::Create(const SdpAudioFormat& format) {
Niels Möller2edab4c2018-10-22 09:48:08 +020084 if (absl::EqualsIgnoreCase(format.name, "CN")) {
kwiberg5adaf732016-10-04 09:33:27 -070085 // CN has a 1:1 RTP clock rate to sample rate ratio.
86 const int sample_rate_hz = format.clockrate_hz;
87 RTC_DCHECK(sample_rate_hz == 8000 || sample_rate_hz == 16000 ||
88 sample_rate_hz == 32000 || sample_rate_hz == 48000);
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010089 return DecoderDatabase::DecoderInfo::CngDecoder{sample_rate_hz};
ossuf1b08da2016-09-23 02:19:43 -070090 } else {
Danil Chapovalovb6021232018-06-19 13:26:36 +020091 return absl::nullopt;
kwibergc0f2dcf2016-05-31 06:28:03 -070092 }
93}
94
ossu9f38c212016-10-04 05:23:32 -070095DecoderDatabase::DecoderInfo::Subtype
96DecoderDatabase::DecoderInfo::SubtypeFromFormat(const SdpAudioFormat& format) {
Niels Möller2edab4c2018-10-22 09:48:08 +020097 if (absl::EqualsIgnoreCase(format.name, "CN")) {
ossu9f38c212016-10-04 05:23:32 -070098 return Subtype::kComfortNoise;
Niels Möller2edab4c2018-10-22 09:48:08 +020099 } else if (absl::EqualsIgnoreCase(format.name, "telephone-event")) {
ossu9f38c212016-10-04 05:23:32 -0700100 return Subtype::kDtmf;
Niels Möller2edab4c2018-10-22 09:48:08 +0200101 } else if (absl::EqualsIgnoreCase(format.name, "red")) {
ossu9f38c212016-10-04 05:23:32 -0700102 return Subtype::kRed;
103 }
104
105 return Subtype::kNormal;
106}
107
Yves Gerey665174f2018-06-19 15:03:05 +0200108bool DecoderDatabase::Empty() const {
109 return decoders_.empty();
110}
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000111
Yves Gerey665174f2018-06-19 15:03:05 +0200112int DecoderDatabase::Size() const {
113 return static_cast<int>(decoders_.size());
114}
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000115
kwiberg1c07c702017-03-27 07:15:49 -0700116std::vector<int> DecoderDatabase::SetCodecs(
117 const std::map<int, SdpAudioFormat>& codecs) {
118 // First collect all payload types that we'll remove or reassign, then remove
119 // them from the database.
120 std::vector<int> changed_payload_types;
121 for (const std::pair<uint8_t, const DecoderInfo&> kv : decoders_) {
122 auto i = codecs.find(kv.first);
123 if (i == codecs.end() || i->second != kv.second.GetFormat()) {
124 changed_payload_types.push_back(kv.first);
125 }
126 }
127 for (int pl_type : changed_payload_types) {
128 Remove(pl_type);
129 }
130
131 // Enter the new and changed payload type mappings into the database.
132 for (const auto& kv : codecs) {
133 const int& rtp_payload_type = kv.first;
134 const SdpAudioFormat& audio_format = kv.second;
135 RTC_DCHECK_GE(rtp_payload_type, 0);
136 RTC_DCHECK_LE(rtp_payload_type, 0x7f);
137 if (decoders_.count(rtp_payload_type) == 0) {
138 decoders_.insert(std::make_pair(
Karl Wiberg08126342018-03-20 19:18:55 +0100139 rtp_payload_type,
140 DecoderInfo(audio_format, codec_pair_id_, decoder_factory_.get())));
kwiberg1c07c702017-03-27 07:15:49 -0700141 } else {
142 // The mapping for this payload type hasn't changed.
143 }
144 }
145
146 return changed_payload_types;
147}
148
kwiberg5adaf732016-10-04 09:33:27 -0700149int DecoderDatabase::RegisterPayload(int rtp_payload_type,
150 const SdpAudioFormat& audio_format) {
151 if (rtp_payload_type < 0 || rtp_payload_type > 0x7f) {
152 return kInvalidRtpPayloadType;
153 }
154 const auto ret = decoders_.insert(std::make_pair(
Karl Wiberg08126342018-03-20 19:18:55 +0100155 rtp_payload_type,
156 DecoderInfo(audio_format, codec_pair_id_, decoder_factory_.get())));
kwiberg5adaf732016-10-04 09:33:27 -0700157 if (ret.second == false) {
Artem Titovd00ce742021-07-28 20:00:17 +0200158 // Database already contains a decoder with type `rtp_payload_type`.
kwiberg5adaf732016-10-04 09:33:27 -0700159 return kDecoderExists;
160 }
161 return kOK;
162}
163
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000164int DecoderDatabase::Remove(uint8_t rtp_payload_type) {
165 if (decoders_.erase(rtp_payload_type) == 0) {
Artem Titovd00ce742021-07-28 20:00:17 +0200166 // No decoder with that `rtp_payload_type`.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 return kDecoderNotFound;
168 }
ossu97ba30e2016-04-25 07:55:58 -0700169 if (active_decoder_type_ == rtp_payload_type) {
170 active_decoder_type_ = -1; // No active decoder.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171 }
ossu97ba30e2016-04-25 07:55:58 -0700172 if (active_cng_decoder_type_ == rtp_payload_type) {
173 active_cng_decoder_type_ = -1; // No active CNG decoder.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 }
175 return kOK;
176}
177
kwiberg6b19b562016-09-20 04:02:25 -0700178void DecoderDatabase::RemoveAll() {
179 decoders_.clear();
180 active_decoder_type_ = -1; // No active decoder.
181 active_cng_decoder_type_ = -1; // No active CNG decoder.
182}
183
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184const DecoderDatabase::DecoderInfo* DecoderDatabase::GetDecoderInfo(
185 uint8_t rtp_payload_type) const {
186 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
187 if (it == decoders_.end()) {
188 // Decoder not found.
189 return NULL;
190 }
ossuf1b08da2016-09-23 02:19:43 -0700191 return &it->second;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000192}
193
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194int DecoderDatabase::SetActiveDecoder(uint8_t rtp_payload_type,
195 bool* new_decoder) {
Artem Titovd00ce742021-07-28 20:00:17 +0200196 // Check that `rtp_payload_type` exists in the database.
Yves Gerey665174f2018-06-19 15:03:05 +0200197 const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
ossu84bc9852016-08-26 05:41:23 -0700198 if (!info) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000199 // Decoder not found.
200 return kDecoderNotFound;
201 }
ossu84bc9852016-08-26 05:41:23 -0700202 RTC_CHECK(!info->IsComfortNoise());
203 RTC_DCHECK(new_decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204 *new_decoder = false;
ossu97ba30e2016-04-25 07:55:58 -0700205 if (active_decoder_type_ < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206 // This is the first active decoder.
207 *new_decoder = true;
ossu97ba30e2016-04-25 07:55:58 -0700208 } else if (active_decoder_type_ != rtp_payload_type) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000209 // Moving from one active decoder to another. Delete the first one.
Yves Gerey665174f2018-06-19 15:03:05 +0200210 const DecoderInfo* old_info = GetDecoderInfo(active_decoder_type_);
ossu84bc9852016-08-26 05:41:23 -0700211 RTC_DCHECK(old_info);
212 old_info->DropDecoder();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000213 *new_decoder = true;
214 }
ossu97ba30e2016-04-25 07:55:58 -0700215 active_decoder_type_ = rtp_payload_type;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000216 return kOK;
217}
218
ossu84bc9852016-08-26 05:41:23 -0700219AudioDecoder* DecoderDatabase::GetActiveDecoder() const {
ossu97ba30e2016-04-25 07:55:58 -0700220 if (active_decoder_type_ < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000221 // No active decoder.
222 return NULL;
223 }
ossu97ba30e2016-04-25 07:55:58 -0700224 return GetDecoder(active_decoder_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000225}
226
227int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) {
Artem Titovd00ce742021-07-28 20:00:17 +0200228 // Check that `rtp_payload_type` exists in the database.
Yves Gerey665174f2018-06-19 15:03:05 +0200229 const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
ossu84bc9852016-08-26 05:41:23 -0700230 if (!info) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000231 // Decoder not found.
232 return kDecoderNotFound;
233 }
ossu97ba30e2016-04-25 07:55:58 -0700234 if (active_cng_decoder_type_ >= 0 &&
235 active_cng_decoder_type_ != rtp_payload_type) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000236 // Moving from one active CNG decoder to another. Delete the first one.
ossu84bc9852016-08-26 05:41:23 -0700237 RTC_DCHECK(active_cng_decoder_);
ossu97ba30e2016-04-25 07:55:58 -0700238 active_cng_decoder_.reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000239 }
ossu97ba30e2016-04-25 07:55:58 -0700240 active_cng_decoder_type_ = rtp_payload_type;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241 return kOK;
242}
243
ossu84bc9852016-08-26 05:41:23 -0700244ComfortNoiseDecoder* DecoderDatabase::GetActiveCngDecoder() const {
ossu97ba30e2016-04-25 07:55:58 -0700245 if (active_cng_decoder_type_ < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000246 // No active CNG decoder.
247 return NULL;
248 }
ossu97ba30e2016-04-25 07:55:58 -0700249 if (!active_cng_decoder_) {
250 active_cng_decoder_.reset(new ComfortNoiseDecoder);
251 }
252 return active_cng_decoder_.get();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253}
254
ossu84bc9852016-08-26 05:41:23 -0700255AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200256 const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
ossu84bc9852016-08-26 05:41:23 -0700257 return info ? info->GetDecoder() : nullptr;
258}
259
ossu84bc9852016-08-26 05:41:23 -0700260bool DecoderDatabase::IsComfortNoise(uint8_t rtp_payload_type) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200261 const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
ossu84bc9852016-08-26 05:41:23 -0700262 return info && info->IsComfortNoise();
263}
264
265bool DecoderDatabase::IsDtmf(uint8_t rtp_payload_type) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200266 const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
ossu84bc9852016-08-26 05:41:23 -0700267 return info && info->IsDtmf();
268}
269
270bool DecoderDatabase::IsRed(uint8_t rtp_payload_type) const {
Yves Gerey665174f2018-06-19 15:03:05 +0200271 const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
ossu84bc9852016-08-26 05:41:23 -0700272 return info && info->IsRed();
273}
274
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const {
276 PacketList::const_iterator it;
277 for (it = packet_list.begin(); it != packet_list.end(); ++it) {
ossua73f6c92016-10-24 08:25:28 -0700278 if (!GetDecoderInfo(it->payload_type)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279 // Payload type is not found.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100280 RTC_LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type "
281 << static_cast<int>(it->payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 return kDecoderNotFound;
283 }
284 }
285 return kOK;
286}
287
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288} // namespace webrtc