henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <utility> // pair |
| 15 | |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 16 | #include "webrtc/base/checks.h" |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 17 | #include "webrtc/base/logging.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 22 | DecoderDatabase::DecoderDatabase() |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 23 | : active_decoder_type_(-1), active_cng_decoder_type_(-1) { |
| 24 | } |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 25 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 26 | DecoderDatabase::~DecoderDatabase() = default; |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 27 | |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 28 | DecoderDatabase::DecoderInfo::DecoderInfo(NetEqDecoder ct, |
| 29 | const std::string& nm, |
| 30 | int fs, |
| 31 | AudioDecoder* ext_dec) |
| 32 | : codec_type(ct), |
| 33 | name(nm), |
| 34 | fs_hz(fs), |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 35 | external_decoder(ext_dec) {} |
| 36 | |
| 37 | DecoderDatabase::DecoderInfo::DecoderInfo(DecoderInfo&&) = default; |
| 38 | DecoderDatabase::DecoderInfo::~DecoderInfo() = default; |
| 39 | |
| 40 | AudioDecoder* DecoderDatabase::DecoderInfo::GetDecoder() { |
| 41 | if (external_decoder) { |
| 42 | RTC_DCHECK(!decoder_); |
| 43 | return external_decoder; |
| 44 | } |
| 45 | if (!decoder_) { |
| 46 | decoder_.reset(CreateAudioDecoder(codec_type)); |
| 47 | } |
| 48 | RTC_DCHECK(decoder_); |
| 49 | return decoder_.get(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 50 | } |
| 51 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 52 | bool DecoderDatabase::Empty() const { return decoders_.empty(); } |
| 53 | |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 54 | int DecoderDatabase::Size() const { return static_cast<int>(decoders_.size()); } |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 55 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | void DecoderDatabase::Reset() { |
| 57 | decoders_.clear(); |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 58 | active_decoder_type_ = -1; |
| 59 | active_cng_decoder_type_ = -1; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int DecoderDatabase::RegisterPayload(uint8_t rtp_payload_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 63 | NetEqDecoder codec_type, |
| 64 | const std::string& name) { |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 65 | if (rtp_payload_type > 0x7F) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 66 | return kInvalidRtpPayloadType; |
| 67 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 68 | if (!CodecSupported(codec_type)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 69 | return kCodecNotSupported; |
| 70 | } |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 71 | const int fs_hz = CodecSampleRateHz(codec_type); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 72 | DecoderInfo info(codec_type, name, fs_hz, nullptr); |
| 73 | auto ret = |
| 74 | decoders_.insert(std::make_pair(rtp_payload_type, std::move(info))); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | if (ret.second == false) { |
| 76 | // Database already contains a decoder with type |rtp_payload_type|. |
| 77 | return kDecoderExists; |
| 78 | } |
| 79 | return kOK; |
| 80 | } |
| 81 | |
| 82 | int DecoderDatabase::InsertExternal(uint8_t rtp_payload_type, |
| 83 | NetEqDecoder codec_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 84 | const std::string& codec_name, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 85 | int fs_hz, |
| 86 | AudioDecoder* decoder) { |
| 87 | if (rtp_payload_type > 0x7F) { |
| 88 | return kInvalidRtpPayloadType; |
| 89 | } |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 90 | if (!CodecSupported(codec_type)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 91 | return kCodecNotSupported; |
| 92 | } |
| 93 | if (fs_hz != 8000 && fs_hz != 16000 && fs_hz != 32000 && fs_hz != 48000) { |
| 94 | return kInvalidSampleRate; |
| 95 | } |
| 96 | if (!decoder) { |
| 97 | return kInvalidPointer; |
| 98 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 99 | std::pair<DecoderMap::iterator, bool> ret; |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 100 | DecoderInfo info(codec_type, codec_name, fs_hz, decoder); |
| 101 | ret = decoders_.insert(std::make_pair(rtp_payload_type, std::move(info))); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 102 | if (ret.second == false) { |
| 103 | // Database already contains a decoder with type |rtp_payload_type|. |
| 104 | return kDecoderExists; |
| 105 | } |
| 106 | return kOK; |
| 107 | } |
| 108 | |
| 109 | int DecoderDatabase::Remove(uint8_t rtp_payload_type) { |
| 110 | if (decoders_.erase(rtp_payload_type) == 0) { |
| 111 | // No decoder with that |rtp_payload_type|. |
| 112 | return kDecoderNotFound; |
| 113 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 114 | if (active_decoder_type_ == rtp_payload_type) { |
| 115 | active_decoder_type_ = -1; // No active decoder. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 117 | if (active_cng_decoder_type_ == rtp_payload_type) { |
| 118 | active_cng_decoder_type_ = -1; // No active CNG decoder. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 119 | } |
| 120 | return kOK; |
| 121 | } |
| 122 | |
| 123 | const DecoderDatabase::DecoderInfo* DecoderDatabase::GetDecoderInfo( |
| 124 | uint8_t rtp_payload_type) const { |
| 125 | DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); |
| 126 | if (it == decoders_.end()) { |
| 127 | // Decoder not found. |
| 128 | return NULL; |
| 129 | } |
| 130 | return &(*it).second; |
| 131 | } |
| 132 | |
| 133 | uint8_t DecoderDatabase::GetRtpPayloadType( |
| 134 | NetEqDecoder codec_type) const { |
| 135 | DecoderMap::const_iterator it; |
| 136 | for (it = decoders_.begin(); it != decoders_.end(); ++it) { |
| 137 | if ((*it).second.codec_type == codec_type) { |
| 138 | // Match found. |
| 139 | return (*it).first; |
| 140 | } |
| 141 | } |
| 142 | // No match. |
| 143 | return kRtpPayloadTypeError; |
| 144 | } |
| 145 | |
| 146 | AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 147 | if (IsDtmf(rtp_payload_type) || IsRed(rtp_payload_type) || |
| 148 | IsComfortNoise(rtp_payload_type)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 149 | // These are not real decoders. |
| 150 | return NULL; |
| 151 | } |
| 152 | DecoderMap::iterator it = decoders_.find(rtp_payload_type); |
| 153 | if (it == decoders_.end()) { |
| 154 | // Decoder not found. |
| 155 | return NULL; |
| 156 | } |
| 157 | DecoderInfo* info = &(*it).second; |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 158 | return info->GetDecoder(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool DecoderDatabase::IsType(uint8_t rtp_payload_type, |
| 162 | NetEqDecoder codec_type) const { |
| 163 | DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); |
| 164 | if (it == decoders_.end()) { |
| 165 | // Decoder not found. |
| 166 | return false; |
| 167 | } |
| 168 | return ((*it).second.codec_type == codec_type); |
| 169 | } |
| 170 | |
| 171 | bool DecoderDatabase::IsComfortNoise(uint8_t rtp_payload_type) const { |
ossu | 6353723 | 2016-04-27 07:43:38 -0700 | [diff] [blame^] | 172 | DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); |
| 173 | if (it == decoders_.end()) { |
| 174 | // Decoder not found. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 175 | return false; |
| 176 | } |
ossu | 6353723 | 2016-04-27 07:43:38 -0700 | [diff] [blame^] | 177 | const auto& type = it->second.codec_type; |
| 178 | return type == NetEqDecoder::kDecoderCNGnb |
| 179 | || type == NetEqDecoder::kDecoderCNGwb |
| 180 | || type == NetEqDecoder::kDecoderCNGswb32kHz |
| 181 | || type == NetEqDecoder::kDecoderCNGswb48kHz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool DecoderDatabase::IsDtmf(uint8_t rtp_payload_type) const { |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 185 | return IsType(rtp_payload_type, NetEqDecoder::kDecoderAVT); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool DecoderDatabase::IsRed(uint8_t rtp_payload_type) const { |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 189 | return IsType(rtp_payload_type, NetEqDecoder::kDecoderRED); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int DecoderDatabase::SetActiveDecoder(uint8_t rtp_payload_type, |
| 193 | bool* new_decoder) { |
| 194 | // Check that |rtp_payload_type| exists in the database. |
| 195 | DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); |
| 196 | if (it == decoders_.end()) { |
| 197 | // Decoder not found. |
| 198 | return kDecoderNotFound; |
| 199 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 200 | RTC_CHECK(!IsComfortNoise(rtp_payload_type)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 201 | assert(new_decoder); |
| 202 | *new_decoder = false; |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 203 | if (active_decoder_type_ < 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 204 | // This is the first active decoder. |
| 205 | *new_decoder = true; |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 206 | } else if (active_decoder_type_ != rtp_payload_type) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 207 | // Moving from one active decoder to another. Delete the first one. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 208 | DecoderMap::iterator it = decoders_.find(active_decoder_type_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 209 | if (it == decoders_.end()) { |
| 210 | // Decoder not found. This should not be possible. |
| 211 | assert(false); |
| 212 | return kDecoderNotFound; |
| 213 | } |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 214 | it->second.DropDecoder(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 215 | *new_decoder = true; |
| 216 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 217 | active_decoder_type_ = rtp_payload_type; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 218 | return kOK; |
| 219 | } |
| 220 | |
| 221 | AudioDecoder* DecoderDatabase::GetActiveDecoder() { |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 222 | if (active_decoder_type_ < 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 223 | // No active decoder. |
| 224 | return NULL; |
| 225 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 226 | return GetDecoder(active_decoder_type_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) { |
| 230 | // Check that |rtp_payload_type| exists in the database. |
| 231 | DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); |
| 232 | if (it == decoders_.end()) { |
| 233 | // Decoder not found. |
| 234 | return kDecoderNotFound; |
| 235 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 236 | if (active_cng_decoder_type_ >= 0 && |
| 237 | active_cng_decoder_type_ != rtp_payload_type) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 238 | // Moving from one active CNG decoder to another. Delete the first one. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 239 | DecoderMap::iterator it = decoders_.find(active_cng_decoder_type_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 240 | if (it == decoders_.end()) { |
| 241 | // Decoder not found. This should not be possible. |
| 242 | assert(false); |
| 243 | return kDecoderNotFound; |
| 244 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 245 | // The CNG decoder should never be provided externally. |
| 246 | RTC_CHECK(!it->second.external_decoder); |
| 247 | active_cng_decoder_.reset(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 248 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 249 | active_cng_decoder_type_ = rtp_payload_type; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 250 | return kOK; |
| 251 | } |
| 252 | |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 253 | ComfortNoiseDecoder* DecoderDatabase::GetActiveCngDecoder() { |
| 254 | if (active_cng_decoder_type_ < 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 255 | // No active CNG decoder. |
| 256 | return NULL; |
| 257 | } |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 258 | if (!active_cng_decoder_) { |
| 259 | active_cng_decoder_.reset(new ComfortNoiseDecoder); |
| 260 | } |
| 261 | return active_cng_decoder_.get(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const { |
| 265 | PacketList::const_iterator it; |
| 266 | for (it = packet_list.begin(); it != packet_list.end(); ++it) { |
| 267 | if (decoders_.find((*it)->header.payloadType) == decoders_.end()) { |
| 268 | // Payload type is not found. |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 269 | LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type " |
| 270 | << static_cast<int>((*it)->header.payloadType); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 271 | return kDecoderNotFound; |
| 272 | } |
| 273 | } |
| 274 | return kOK; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | } // namespace webrtc |