blob: feb02038a2cd9e206150a59d625e2c5d3ed0a6d8 [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 <utility> // pair
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/audio_codecs/audio_decoder.h"
16#include "rtc_base/checks.h"
17#include "rtc_base/logging.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
19namespace webrtc {
20
kwiberg5178ee82016-05-03 01:39:01 -070021DecoderDatabase::DecoderDatabase(
ossue725f7c2016-05-19 10:48:04 -070022 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory)
kwiberg5178ee82016-05-03 01:39:01 -070023 : active_decoder_type_(-1),
24 active_cng_decoder_type_(-1),
ossue725f7c2016-05-19 10:48:04 -070025 decoder_factory_(decoder_factory) {}
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000026
ossu97ba30e2016-04-25 07:55:58 -070027DecoderDatabase::~DecoderDatabase() = default;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000028
ossuf1b08da2016-09-23 02:19:43 -070029DecoderDatabase::DecoderInfo::DecoderInfo(const SdpAudioFormat& audio_format,
kwiberge9413062016-11-03 05:29:05 -070030 AudioDecoderFactory* factory,
31 const std::string& codec_name)
32 : name_(codec_name),
33 audio_format_(audio_format),
ossu84bc9852016-08-26 05:41:23 -070034 factory_(factory),
kwiberg342f7402016-06-16 03:18:00 -070035 external_decoder_(nullptr),
ossu9f38c212016-10-04 05:23:32 -070036 cng_decoder_(CngDecoder::Create(audio_format)),
37 subtype_(SubtypeFromFormat(audio_format)) {}
kwibergc0f2dcf2016-05-31 06:28:03 -070038
kwiberge9413062016-11-03 05:29:05 -070039DecoderDatabase::DecoderInfo::DecoderInfo(const SdpAudioFormat& audio_format,
40 AudioDecoderFactory* factory)
41 : DecoderInfo(audio_format, factory, audio_format.name) {}
42
kwibergc0f2dcf2016-05-31 06:28:03 -070043DecoderDatabase::DecoderInfo::DecoderInfo(NetEqDecoder ct,
ossuf1b08da2016-09-23 02:19:43 -070044 AudioDecoderFactory* factory)
kwiberg65cb70d2017-03-03 06:16:28 -080045 : DecoderInfo(*NetEqDecoderToSdpAudioFormat(ct), factory) {}
ossuf1b08da2016-09-23 02:19:43 -070046
47DecoderDatabase::DecoderInfo::DecoderInfo(const SdpAudioFormat& audio_format,
kwiberge9413062016-11-03 05:29:05 -070048 AudioDecoder* ext_dec,
49 const std::string& codec_name)
50 : name_(codec_name),
51 audio_format_(audio_format),
ossuf1b08da2016-09-23 02:19:43 -070052 factory_(nullptr),
ossu9f38c212016-10-04 05:23:32 -070053 external_decoder_(ext_dec),
54 subtype_(Subtype::kNormal) {
kwibergc0f2dcf2016-05-31 06:28:03 -070055 RTC_CHECK(ext_dec);
56}
kwiberg0fa0a972016-04-19 05:03:45 -070057
58DecoderDatabase::DecoderInfo::DecoderInfo(DecoderInfo&&) = default;
59DecoderDatabase::DecoderInfo::~DecoderInfo() = default;
60
ossu84bc9852016-08-26 05:41:23 -070061AudioDecoder* DecoderDatabase::DecoderInfo::GetDecoder() const {
ossu9f38c212016-10-04 05:23:32 -070062 if (subtype_ != Subtype::kNormal) {
ossuf1b08da2016-09-23 02:19:43 -070063 // These are handled internally, so they have no AudioDecoder objects.
64 return nullptr;
65 }
kwiberg342f7402016-06-16 03:18:00 -070066 if (external_decoder_) {
kwiberg0fa0a972016-04-19 05:03:45 -070067 RTC_DCHECK(!decoder_);
kwiberg342f7402016-06-16 03:18:00 -070068 RTC_DCHECK(!cng_decoder_);
69 return external_decoder_;
kwiberg0fa0a972016-04-19 05:03:45 -070070 }
71 if (!decoder_) {
ossuf1b08da2016-09-23 02:19:43 -070072 // TODO(ossu): Keep a check here for now, since a number of tests create
73 // DecoderInfos without factories.
ossu84bc9852016-08-26 05:41:23 -070074 RTC_DCHECK(factory_);
ossuf1b08da2016-09-23 02:19:43 -070075 decoder_ = factory_->MakeAudioDecoder(audio_format_);
kwiberg0fa0a972016-04-19 05:03:45 -070076 }
ossuf1b08da2016-09-23 02:19:43 -070077 RTC_DCHECK(decoder_) << "Failed to create: " << audio_format_;
kwiberg0fa0a972016-04-19 05:03:45 -070078 return decoder_.get();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079}
80
ossuf1b08da2016-09-23 02:19:43 -070081bool DecoderDatabase::DecoderInfo::IsType(const char* name) const {
82 return STR_CASE_CMP(audio_format_.name.c_str(), name) == 0;
83}
84
85bool DecoderDatabase::DecoderInfo::IsType(const std::string& name) const {
86 return IsType(name.c_str());
ossu84bc9852016-08-26 05:41:23 -070087}
88
kwibergc0f2dcf2016-05-31 06:28:03 -070089rtc::Optional<DecoderDatabase::DecoderInfo::CngDecoder>
ossuf1b08da2016-09-23 02:19:43 -070090DecoderDatabase::DecoderInfo::CngDecoder::Create(const SdpAudioFormat& format) {
91 if (STR_CASE_CMP(format.name.c_str(), "CN") == 0) {
kwiberg5adaf732016-10-04 09:33:27 -070092 // CN has a 1:1 RTP clock rate to sample rate ratio.
93 const int sample_rate_hz = format.clockrate_hz;
94 RTC_DCHECK(sample_rate_hz == 8000 || sample_rate_hz == 16000 ||
95 sample_rate_hz == 32000 || sample_rate_hz == 48000);
96 return rtc::Optional<DecoderDatabase::DecoderInfo::CngDecoder>(
97 {sample_rate_hz});
ossuf1b08da2016-09-23 02:19:43 -070098 } else {
99 return rtc::Optional<CngDecoder>();
kwibergc0f2dcf2016-05-31 06:28:03 -0700100 }
101}
102
ossu9f38c212016-10-04 05:23:32 -0700103DecoderDatabase::DecoderInfo::Subtype
104DecoderDatabase::DecoderInfo::SubtypeFromFormat(const SdpAudioFormat& format) {
105 if (STR_CASE_CMP(format.name.c_str(), "CN") == 0) {
106 return Subtype::kComfortNoise;
107 } else if (STR_CASE_CMP(format.name.c_str(), "telephone-event") == 0) {
108 return Subtype::kDtmf;
109 } else if (STR_CASE_CMP(format.name.c_str(), "red") == 0) {
110 return Subtype::kRed;
111 }
112
113 return Subtype::kNormal;
114}
115
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000116bool DecoderDatabase::Empty() const { return decoders_.empty(); }
117
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000118int DecoderDatabase::Size() const { return static_cast<int>(decoders_.size()); }
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000119
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120void DecoderDatabase::Reset() {
121 decoders_.clear();
ossu97ba30e2016-04-25 07:55:58 -0700122 active_decoder_type_ = -1;
123 active_cng_decoder_type_ = -1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124}
125
kwiberg1c07c702017-03-27 07:15:49 -0700126std::vector<int> DecoderDatabase::SetCodecs(
127 const std::map<int, SdpAudioFormat>& codecs) {
128 // First collect all payload types that we'll remove or reassign, then remove
129 // them from the database.
130 std::vector<int> changed_payload_types;
131 for (const std::pair<uint8_t, const DecoderInfo&> kv : decoders_) {
132 auto i = codecs.find(kv.first);
133 if (i == codecs.end() || i->second != kv.second.GetFormat()) {
134 changed_payload_types.push_back(kv.first);
135 }
136 }
137 for (int pl_type : changed_payload_types) {
138 Remove(pl_type);
139 }
140
141 // Enter the new and changed payload type mappings into the database.
142 for (const auto& kv : codecs) {
143 const int& rtp_payload_type = kv.first;
144 const SdpAudioFormat& audio_format = kv.second;
145 RTC_DCHECK_GE(rtp_payload_type, 0);
146 RTC_DCHECK_LE(rtp_payload_type, 0x7f);
147 if (decoders_.count(rtp_payload_type) == 0) {
148 decoders_.insert(std::make_pair(
149 rtp_payload_type, DecoderInfo(audio_format, decoder_factory_.get())));
150 } else {
151 // The mapping for this payload type hasn't changed.
152 }
153 }
154
155 return changed_payload_types;
156}
157
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158int DecoderDatabase::RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800159 NetEqDecoder codec_type,
160 const std::string& name) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000161 if (rtp_payload_type > 0x7F) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162 return kInvalidRtpPayloadType;
163 }
ossuf1b08da2016-09-23 02:19:43 -0700164 // kCodecArbitrary is only supported through InsertExternal.
165 if (codec_type == NetEqDecoder::kDecoderArbitrary ||
166 !CodecSupported(codec_type)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 return kCodecNotSupported;
168 }
kwiberg65cb70d2017-03-03 06:16:28 -0800169 const auto opt_format = NetEqDecoderToSdpAudioFormat(codec_type);
ossuf1b08da2016-09-23 02:19:43 -0700170 if (!opt_format) {
171 return kCodecNotSupported;
172 }
kwiberge9413062016-11-03 05:29:05 -0700173 DecoderInfo info(*opt_format, decoder_factory_, name);
kwiberg0fa0a972016-04-19 05:03:45 -0700174 auto ret =
175 decoders_.insert(std::make_pair(rtp_payload_type, std::move(info)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000176 if (ret.second == false) {
177 // Database already contains a decoder with type |rtp_payload_type|.
178 return kDecoderExists;
179 }
180 return kOK;
181}
182
kwiberg5adaf732016-10-04 09:33:27 -0700183int DecoderDatabase::RegisterPayload(int rtp_payload_type,
184 const SdpAudioFormat& audio_format) {
185 if (rtp_payload_type < 0 || rtp_payload_type > 0x7f) {
186 return kInvalidRtpPayloadType;
187 }
188 const auto ret = decoders_.insert(std::make_pair(
189 rtp_payload_type, DecoderInfo(audio_format, decoder_factory_.get())));
190 if (ret.second == false) {
191 // Database already contains a decoder with type |rtp_payload_type|.
192 return kDecoderExists;
193 }
194 return kOK;
195}
196
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000197int DecoderDatabase::InsertExternal(uint8_t rtp_payload_type,
198 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800199 const std::string& codec_name,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000200 AudioDecoder* decoder) {
201 if (rtp_payload_type > 0x7F) {
202 return kInvalidRtpPayloadType;
203 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204 if (!decoder) {
205 return kInvalidPointer;
206 }
ossuf1b08da2016-09-23 02:19:43 -0700207
kwiberg65cb70d2017-03-03 06:16:28 -0800208 const auto opt_db_format = NetEqDecoderToSdpAudioFormat(codec_type);
ossuf1b08da2016-09-23 02:19:43 -0700209 const SdpAudioFormat format = opt_db_format.value_or({"arbitrary", 0, 0});
210
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000211 std::pair<DecoderMap::iterator, bool> ret;
kwiberge9413062016-11-03 05:29:05 -0700212 DecoderInfo info(format, decoder, codec_name);
kwiberg0fa0a972016-04-19 05:03:45 -0700213 ret = decoders_.insert(std::make_pair(rtp_payload_type, std::move(info)));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000214 if (ret.second == false) {
215 // Database already contains a decoder with type |rtp_payload_type|.
216 return kDecoderExists;
217 }
218 return kOK;
219}
220
221int DecoderDatabase::Remove(uint8_t rtp_payload_type) {
222 if (decoders_.erase(rtp_payload_type) == 0) {
223 // No decoder with that |rtp_payload_type|.
224 return kDecoderNotFound;
225 }
ossu97ba30e2016-04-25 07:55:58 -0700226 if (active_decoder_type_ == rtp_payload_type) {
227 active_decoder_type_ = -1; // No active decoder.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000228 }
ossu97ba30e2016-04-25 07:55:58 -0700229 if (active_cng_decoder_type_ == rtp_payload_type) {
230 active_cng_decoder_type_ = -1; // No active CNG decoder.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000231 }
232 return kOK;
233}
234
kwiberg6b19b562016-09-20 04:02:25 -0700235void DecoderDatabase::RemoveAll() {
236 decoders_.clear();
237 active_decoder_type_ = -1; // No active decoder.
238 active_cng_decoder_type_ = -1; // No active CNG decoder.
239}
240
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241const DecoderDatabase::DecoderInfo* DecoderDatabase::GetDecoderInfo(
242 uint8_t rtp_payload_type) const {
243 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
244 if (it == decoders_.end()) {
245 // Decoder not found.
246 return NULL;
247 }
ossuf1b08da2016-09-23 02:19:43 -0700248 return &it->second;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249}
250
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000251int DecoderDatabase::SetActiveDecoder(uint8_t rtp_payload_type,
252 bool* new_decoder) {
253 // Check that |rtp_payload_type| exists in the database.
ossu84bc9852016-08-26 05:41:23 -0700254 const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
255 if (!info) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000256 // Decoder not found.
257 return kDecoderNotFound;
258 }
ossu84bc9852016-08-26 05:41:23 -0700259 RTC_CHECK(!info->IsComfortNoise());
260 RTC_DCHECK(new_decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000261 *new_decoder = false;
ossu97ba30e2016-04-25 07:55:58 -0700262 if (active_decoder_type_ < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 // This is the first active decoder.
264 *new_decoder = true;
ossu97ba30e2016-04-25 07:55:58 -0700265 } else if (active_decoder_type_ != rtp_payload_type) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 // Moving from one active decoder to another. Delete the first one.
ossu84bc9852016-08-26 05:41:23 -0700267 const DecoderInfo *old_info = GetDecoderInfo(active_decoder_type_);
268 RTC_DCHECK(old_info);
269 old_info->DropDecoder();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270 *new_decoder = true;
271 }
ossu97ba30e2016-04-25 07:55:58 -0700272 active_decoder_type_ = rtp_payload_type;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273 return kOK;
274}
275
ossu84bc9852016-08-26 05:41:23 -0700276AudioDecoder* DecoderDatabase::GetActiveDecoder() const {
ossu97ba30e2016-04-25 07:55:58 -0700277 if (active_decoder_type_ < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278 // No active decoder.
279 return NULL;
280 }
ossu97ba30e2016-04-25 07:55:58 -0700281 return GetDecoder(active_decoder_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282}
283
284int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) {
285 // Check that |rtp_payload_type| exists in the database.
ossu84bc9852016-08-26 05:41:23 -0700286 const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
287 if (!info) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288 // Decoder not found.
289 return kDecoderNotFound;
290 }
ossu97ba30e2016-04-25 07:55:58 -0700291 if (active_cng_decoder_type_ >= 0 &&
292 active_cng_decoder_type_ != rtp_payload_type) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293 // Moving from one active CNG decoder to another. Delete the first one.
ossu84bc9852016-08-26 05:41:23 -0700294 RTC_DCHECK(active_cng_decoder_);
ossu97ba30e2016-04-25 07:55:58 -0700295 active_cng_decoder_.reset();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 }
ossu97ba30e2016-04-25 07:55:58 -0700297 active_cng_decoder_type_ = rtp_payload_type;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 return kOK;
299}
300
ossu84bc9852016-08-26 05:41:23 -0700301ComfortNoiseDecoder* DecoderDatabase::GetActiveCngDecoder() const {
ossu97ba30e2016-04-25 07:55:58 -0700302 if (active_cng_decoder_type_ < 0) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000303 // No active CNG decoder.
304 return NULL;
305 }
ossu97ba30e2016-04-25 07:55:58 -0700306 if (!active_cng_decoder_) {
307 active_cng_decoder_.reset(new ComfortNoiseDecoder);
308 }
309 return active_cng_decoder_.get();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310}
311
ossu84bc9852016-08-26 05:41:23 -0700312AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) const {
313 const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
314 return info ? info->GetDecoder() : nullptr;
315}
316
ossuf1b08da2016-09-23 02:19:43 -0700317bool DecoderDatabase::IsType(uint8_t rtp_payload_type, const char* name) const {
318 const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
319 return info && info->IsType(name);
320}
321
ossu84bc9852016-08-26 05:41:23 -0700322bool DecoderDatabase::IsType(uint8_t rtp_payload_type,
ossuf1b08da2016-09-23 02:19:43 -0700323 const std::string& name) const {
324 return IsType(rtp_payload_type, name.c_str());
ossu84bc9852016-08-26 05:41:23 -0700325}
326
327bool DecoderDatabase::IsComfortNoise(uint8_t rtp_payload_type) const {
328 const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
329 return info && info->IsComfortNoise();
330}
331
332bool DecoderDatabase::IsDtmf(uint8_t rtp_payload_type) const {
333 const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
334 return info && info->IsDtmf();
335}
336
337bool DecoderDatabase::IsRed(uint8_t rtp_payload_type) const {
338 const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
339 return info && info->IsRed();
340}
341
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const {
343 PacketList::const_iterator it;
344 for (it = packet_list.begin(); it != packet_list.end(); ++it) {
ossua73f6c92016-10-24 08:25:28 -0700345 if (!GetDecoderInfo(it->payload_type)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346 // Payload type is not found.
Henrik Lundind67a2192015-08-03 12:54:37 +0200347 LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type "
ossua73f6c92016-10-24 08:25:28 -0700348 << static_cast<int>(it->payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 return kDecoderNotFound;
350 }
351 }
352 return kOK;
353}
354
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355} // namespace webrtc