1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_coding/neteq/decoder_database.h"
#include <stddef.h>
#include <cstdint>
#include <list>
#include <map>
#include <optional>
#include <utility>
#include <vector>
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "api/audio_codecs/audio_codec_pair_id.h"
#include "api/audio_codecs/audio_decoder.h"
#include "api/audio_codecs/audio_decoder_factory.h"
#include "api/audio_codecs/audio_format.h"
#include "api/environment/environment.h"
#include "api/scoped_refptr.h"
#include "modules/audio_coding/codecs/cng/webrtc_cng.h"
#include "modules/audio_coding/neteq/packet.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
namespace webrtc {
DecoderDatabase::DecoderDatabase(
const Environment& env,
scoped_refptr<AudioDecoderFactory> decoder_factory,
std::optional<AudioCodecPairId> codec_pair_id)
: env_(env),
active_decoder_type_(-1),
active_cng_decoder_type_(-1),
decoder_factory_(std::move(decoder_factory)),
codec_pair_id_(codec_pair_id) {}
DecoderDatabase::~DecoderDatabase() = default;
DecoderDatabase::DecoderInfo::DecoderInfo(
const Environment& env,
const SdpAudioFormat& audio_format,
std::optional<AudioCodecPairId> codec_pair_id,
AudioDecoderFactory* factory)
: env_(env),
audio_format_(audio_format),
codec_pair_id_(codec_pair_id),
factory_(factory),
cng_decoder_(CngDecoder::Create(audio_format)),
subtype_(SubtypeFromFormat(audio_format)) {}
DecoderDatabase::DecoderInfo::DecoderInfo(DecoderInfo&&) = default;
DecoderDatabase::DecoderInfo::~DecoderInfo() = default;
AudioDecoder* DecoderDatabase::DecoderInfo::GetDecoder() const {
if (subtype_ != Subtype::kNormal) {
// These are handled internally, so they have no AudioDecoder objects.
return nullptr;
}
if (!decoder_) {
// TODO(ossu): Keep a check here for now, since a number of tests create
// DecoderInfos without factories.
RTC_DCHECK(factory_);
decoder_ = factory_->Create(env_, audio_format_, codec_pair_id_);
}
RTC_DCHECK(decoder_) << "Failed to create: " << absl::StrCat(audio_format_);
return decoder_.get();
}
bool DecoderDatabase::DecoderInfo::IsType(absl::string_view name) const {
return absl::EqualsIgnoreCase(audio_format_.name, name);
}
std::optional<DecoderDatabase::DecoderInfo::CngDecoder>
DecoderDatabase::DecoderInfo::CngDecoder::Create(const SdpAudioFormat& format) {
if (absl::EqualsIgnoreCase(format.name, "CN")) {
// CN has a 1:1 RTP clock rate to sample rate ratio.
const int sample_rate_hz = format.clockrate_hz;
RTC_DCHECK(sample_rate_hz == 8000 || sample_rate_hz == 16000 ||
sample_rate_hz == 32000 || sample_rate_hz == 48000);
return DecoderDatabase::DecoderInfo::CngDecoder{sample_rate_hz};
} else {
return std::nullopt;
}
}
DecoderDatabase::DecoderInfo::Subtype
DecoderDatabase::DecoderInfo::SubtypeFromFormat(const SdpAudioFormat& format) {
if (absl::EqualsIgnoreCase(format.name, "CN")) {
return Subtype::kComfortNoise;
} else if (absl::EqualsIgnoreCase(format.name, "telephone-event")) {
return Subtype::kDtmf;
} else if (absl::EqualsIgnoreCase(format.name, "red")) {
return Subtype::kRed;
}
return Subtype::kNormal;
}
bool DecoderDatabase::Empty() const {
return decoders_.empty();
}
int DecoderDatabase::Size() const {
return static_cast<int>(decoders_.size());
}
std::vector<int> DecoderDatabase::SetCodecs(
const std::map<int, SdpAudioFormat>& codecs) {
// First collect all payload types that we'll remove or reassign, then remove
// them from the database.
std::vector<int> changed_payload_types;
for (const std::pair<uint8_t, const DecoderInfo&> kv : decoders_) {
auto i = codecs.find(kv.first);
if (i == codecs.end() || i->second != kv.second.GetFormat()) {
changed_payload_types.push_back(kv.first);
}
}
for (int pl_type : changed_payload_types) {
Remove(pl_type);
}
// Enter the new and changed payload type mappings into the database.
for (const auto& kv : codecs) {
const int& rtp_payload_type = kv.first;
const SdpAudioFormat& audio_format = kv.second;
RTC_DCHECK_GE(rtp_payload_type, 0);
RTC_DCHECK_LE(rtp_payload_type, 0x7f);
if (decoders_.count(rtp_payload_type) == 0) {
decoders_.insert(std::make_pair(
rtp_payload_type, DecoderInfo(env_, audio_format, codec_pair_id_,
decoder_factory_.get())));
} else {
// The mapping for this payload type hasn't changed.
}
}
return changed_payload_types;
}
int DecoderDatabase::RegisterPayload(int rtp_payload_type,
const SdpAudioFormat& audio_format) {
if (rtp_payload_type < 0 || rtp_payload_type > 0x7f) {
return kInvalidRtpPayloadType;
}
const auto ret = decoders_.insert(std::make_pair(
rtp_payload_type,
DecoderInfo(env_, audio_format, codec_pair_id_, decoder_factory_.get())));
if (ret.second == false) {
// Database already contains a decoder with type `rtp_payload_type`.
return kDecoderExists;
}
return kOK;
}
int DecoderDatabase::Remove(uint8_t rtp_payload_type) {
if (decoders_.erase(rtp_payload_type) == 0) {
// No decoder with that `rtp_payload_type`.
return kDecoderNotFound;
}
if (active_decoder_type_ == rtp_payload_type) {
active_decoder_type_ = -1; // No active decoder.
}
if (active_cng_decoder_type_ == rtp_payload_type) {
active_cng_decoder_type_ = -1; // No active CNG decoder.
}
return kOK;
}
void DecoderDatabase::RemoveAll() {
decoders_.clear();
active_decoder_type_ = -1; // No active decoder.
active_cng_decoder_type_ = -1; // No active CNG decoder.
}
const DecoderDatabase::DecoderInfo* DecoderDatabase::GetDecoderInfo(
uint8_t rtp_payload_type) const {
DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
if (it == decoders_.end()) {
// Decoder not found.
return nullptr;
}
return &it->second;
}
int DecoderDatabase::SetActiveDecoder(uint8_t rtp_payload_type,
bool* new_decoder) {
// Check that `rtp_payload_type` exists in the database.
const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
if (!info) {
// Decoder not found.
return kDecoderNotFound;
}
RTC_CHECK(!info->IsComfortNoise());
RTC_DCHECK(new_decoder);
*new_decoder = false;
if (active_decoder_type_ < 0) {
// This is the first active decoder.
*new_decoder = true;
} else if (active_decoder_type_ != rtp_payload_type) {
// Moving from one active decoder to another. Delete the first one.
const DecoderInfo* old_info = GetDecoderInfo(active_decoder_type_);
RTC_DCHECK(old_info);
old_info->DropDecoder();
*new_decoder = true;
}
active_decoder_type_ = rtp_payload_type;
return kOK;
}
AudioDecoder* DecoderDatabase::GetActiveDecoder() const {
if (active_decoder_type_ < 0) {
// No active decoder.
return nullptr;
}
return GetDecoder(active_decoder_type_);
}
int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) {
// Check that `rtp_payload_type` exists in the database.
const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
if (!info) {
// Decoder not found.
return kDecoderNotFound;
}
if (active_cng_decoder_type_ >= 0 &&
active_cng_decoder_type_ != rtp_payload_type) {
// Moving from one active CNG decoder to another. Delete the first one.
RTC_DCHECK(active_cng_decoder_);
active_cng_decoder_.reset();
}
active_cng_decoder_type_ = rtp_payload_type;
return kOK;
}
ComfortNoiseDecoder* DecoderDatabase::GetActiveCngDecoder() const {
if (active_cng_decoder_type_ < 0) {
// No active CNG decoder.
return nullptr;
}
if (!active_cng_decoder_) {
active_cng_decoder_.reset(new ComfortNoiseDecoder);
}
return active_cng_decoder_.get();
}
AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) const {
const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
return info ? info->GetDecoder() : nullptr;
}
bool DecoderDatabase::IsComfortNoise(uint8_t rtp_payload_type) const {
const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
return info && info->IsComfortNoise();
}
bool DecoderDatabase::IsDtmf(uint8_t rtp_payload_type) const {
const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
return info && info->IsDtmf();
}
bool DecoderDatabase::IsRed(uint8_t rtp_payload_type) const {
const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
return info && info->IsRed();
}
int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const {
PacketList::const_iterator it;
for (it = packet_list.begin(); it != packet_list.end(); ++it) {
if (!GetDecoderInfo(it->payload_type)) {
// Payload type is not found.
RTC_LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type "
<< static_cast<int>(it->payload_type);
return kDecoderNotFound;
}
}
return kOK;
}
} // namespace webrtc
|