2013-01-29 12:09:21 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-06-09 08:10:28 +00:00
|
|
|
#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
#include <utility> // pair
|
|
|
|
|
|
2015-12-09 06:20:58 -08:00
|
|
|
#include "webrtc/base/checks.h"
|
Switch to base/logging.h in neteq_impl.cc
This change includes base/logging.h instead of the old and deprecated
system_wrappers/interface/logging.h. This requires some changes of the
actual logging invocations.
For reference the following regexps where used (in Eclipse) for a few
of the replacements:
find: LOG_FERR1\(\s*([^,]*),\s*([^,]*),\s*(.*)\);
replace: LOG($1) << "$2 " << $3;
find: LOG_FERR2\(\s*([^,]*),\s*([^,]*),\s*([^,]*),\s*(.*)\);
replace: LOG($1) << "$2 " << $3 << " " << $4;
BUG=4735
R=minyue@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/50229004 .
Cr-Commit-Position: refs/heads/master@{#9669}
2015-08-03 12:54:37 +02:00
|
|
|
#include "webrtc/base/logging.h"
|
2014-12-09 10:12:53 +00:00
|
|
|
#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-05-03 01:39:01 -07:00
|
|
|
DecoderDatabase::DecoderDatabase(
|
2016-05-19 10:48:04 -07:00
|
|
|
const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory)
|
2016-05-03 01:39:01 -07:00
|
|
|
: active_decoder_type_(-1),
|
|
|
|
|
active_cng_decoder_type_(-1),
|
2016-05-19 10:48:04 -07:00
|
|
|
decoder_factory_(decoder_factory) {}
|
2013-07-31 15:54:00 +00:00
|
|
|
|
2016-04-25 07:55:58 -07:00
|
|
|
DecoderDatabase::~DecoderDatabase() = default;
|
2013-07-31 15:54:00 +00:00
|
|
|
|
2016-09-23 02:19:43 -07:00
|
|
|
DecoderDatabase::DecoderInfo::DecoderInfo(const SdpAudioFormat& audio_format,
|
|
|
|
|
AudioDecoderFactory* factory)
|
|
|
|
|
: audio_format_(audio_format),
|
2016-08-26 05:41:23 -07:00
|
|
|
factory_(factory),
|
2016-06-16 03:18:00 -07:00
|
|
|
external_decoder_(nullptr),
|
2016-09-23 02:19:43 -07:00
|
|
|
cng_decoder_(CngDecoder::Create(audio_format)) {}
|
2016-05-31 06:28:03 -07:00
|
|
|
|
2016-04-19 05:03:45 -07:00
|
|
|
DecoderDatabase::DecoderInfo::DecoderInfo(NetEqDecoder ct,
|
2016-09-23 02:19:43 -07:00
|
|
|
AudioDecoderFactory* factory)
|
|
|
|
|
: audio_format_(*acm2::RentACodec::NetEqDecoderToSdpAudioFormat(ct)),
|
|
|
|
|
factory_(factory),
|
|
|
|
|
external_decoder_(nullptr),
|
|
|
|
|
cng_decoder_(CngDecoder::Create(audio_format_)) {}
|
|
|
|
|
|
|
|
|
|
DecoderDatabase::DecoderInfo::DecoderInfo(const SdpAudioFormat& audio_format,
|
2016-04-19 05:03:45 -07:00
|
|
|
AudioDecoder* ext_dec)
|
2016-09-23 02:19:43 -07:00
|
|
|
: audio_format_(audio_format),
|
|
|
|
|
factory_(nullptr),
|
2016-06-16 03:18:00 -07:00
|
|
|
external_decoder_(ext_dec) {
|
2016-05-31 06:28:03 -07:00
|
|
|
RTC_CHECK(ext_dec);
|
|
|
|
|
}
|
2016-04-19 05:03:45 -07:00
|
|
|
|
|
|
|
|
DecoderDatabase::DecoderInfo::DecoderInfo(DecoderInfo&&) = default;
|
|
|
|
|
DecoderDatabase::DecoderInfo::~DecoderInfo() = default;
|
|
|
|
|
|
2016-08-26 05:41:23 -07:00
|
|
|
AudioDecoder* DecoderDatabase::DecoderInfo::GetDecoder() const {
|
2016-09-23 02:19:43 -07:00
|
|
|
if (IsDtmf() || IsRed() || IsComfortNoise()) {
|
|
|
|
|
// These are handled internally, so they have no AudioDecoder objects.
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2016-06-16 03:18:00 -07:00
|
|
|
if (external_decoder_) {
|
2016-04-19 05:03:45 -07:00
|
|
|
RTC_DCHECK(!decoder_);
|
2016-06-16 03:18:00 -07:00
|
|
|
RTC_DCHECK(!cng_decoder_);
|
|
|
|
|
return external_decoder_;
|
2016-04-19 05:03:45 -07:00
|
|
|
}
|
|
|
|
|
if (!decoder_) {
|
2016-09-23 02:19:43 -07:00
|
|
|
// TODO(ossu): Keep a check here for now, since a number of tests create
|
|
|
|
|
// DecoderInfos without factories.
|
2016-08-26 05:41:23 -07:00
|
|
|
RTC_DCHECK(factory_);
|
2016-09-23 02:19:43 -07:00
|
|
|
decoder_ = factory_->MakeAudioDecoder(audio_format_);
|
2016-04-19 05:03:45 -07:00
|
|
|
}
|
2016-09-23 02:19:43 -07:00
|
|
|
RTC_DCHECK(decoder_) << "Failed to create: " << audio_format_;
|
2016-04-19 05:03:45 -07:00
|
|
|
return decoder_.get();
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-26 05:41:23 -07:00
|
|
|
bool DecoderDatabase::DecoderInfo::IsComfortNoise() const {
|
2016-09-23 02:19:43 -07:00
|
|
|
RTC_DCHECK_EQ(!!cng_decoder_, IsType("CN"));
|
|
|
|
|
return !!cng_decoder_;
|
2016-08-26 05:41:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::DecoderInfo::IsDtmf() const {
|
2016-09-23 02:19:43 -07:00
|
|
|
return IsType("telephone-event");
|
2016-08-26 05:41:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::DecoderInfo::IsRed() const {
|
2016-09-23 02:19:43 -07:00
|
|
|
return IsType("red");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::DecoderInfo::IsType(const char* name) const {
|
|
|
|
|
return STR_CASE_CMP(audio_format_.name.c_str(), name) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::DecoderInfo::IsType(const std::string& name) const {
|
|
|
|
|
return IsType(name.c_str());
|
2016-08-26 05:41:23 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-31 06:28:03 -07:00
|
|
|
rtc::Optional<DecoderDatabase::DecoderInfo::CngDecoder>
|
2016-09-23 02:19:43 -07:00
|
|
|
DecoderDatabase::DecoderInfo::CngDecoder::Create(const SdpAudioFormat& format) {
|
|
|
|
|
if (STR_CASE_CMP(format.name.c_str(), "CN") == 0) {
|
|
|
|
|
return rtc::Optional<CngDecoder>({format.clockrate_hz});
|
|
|
|
|
} else {
|
|
|
|
|
return rtc::Optional<CngDecoder>();
|
2016-05-31 06:28:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 15:54:00 +00:00
|
|
|
bool DecoderDatabase::Empty() const { return decoders_.empty(); }
|
|
|
|
|
|
2013-09-20 16:25:28 +00:00
|
|
|
int DecoderDatabase::Size() const { return static_cast<int>(decoders_.size()); }
|
2013-07-31 15:54:00 +00:00
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
void DecoderDatabase::Reset() {
|
|
|
|
|
decoders_.clear();
|
2016-04-25 07:55:58 -07:00
|
|
|
active_decoder_type_ = -1;
|
|
|
|
|
active_cng_decoder_type_ = -1;
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DecoderDatabase::RegisterPayload(uint8_t rtp_payload_type,
|
2015-12-09 06:20:58 -08:00
|
|
|
NetEqDecoder codec_type,
|
|
|
|
|
const std::string& name) {
|
2015-02-23 21:28:22 +00:00
|
|
|
if (rtp_payload_type > 0x7F) {
|
2013-01-29 12:09:21 +00:00
|
|
|
return kInvalidRtpPayloadType;
|
|
|
|
|
}
|
2016-09-23 02:19:43 -07:00
|
|
|
// kCodecArbitrary is only supported through InsertExternal.
|
|
|
|
|
if (codec_type == NetEqDecoder::kDecoderArbitrary ||
|
|
|
|
|
!CodecSupported(codec_type)) {
|
|
|
|
|
return kCodecNotSupported;
|
|
|
|
|
}
|
|
|
|
|
const auto opt_format =
|
|
|
|
|
acm2::RentACodec::NetEqDecoderToSdpAudioFormat(codec_type);
|
|
|
|
|
if (!opt_format) {
|
2013-01-29 12:09:21 +00:00
|
|
|
return kCodecNotSupported;
|
|
|
|
|
}
|
2016-09-23 02:19:43 -07:00
|
|
|
DecoderInfo info(*opt_format, decoder_factory_);
|
|
|
|
|
info.name = name;
|
2016-04-19 05:03:45 -07:00
|
|
|
auto ret =
|
|
|
|
|
decoders_.insert(std::make_pair(rtp_payload_type, std::move(info)));
|
2013-01-29 12:09:21 +00:00
|
|
|
if (ret.second == false) {
|
|
|
|
|
// Database already contains a decoder with type |rtp_payload_type|.
|
|
|
|
|
return kDecoderExists;
|
|
|
|
|
}
|
|
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DecoderDatabase::InsertExternal(uint8_t rtp_payload_type,
|
|
|
|
|
NetEqDecoder codec_type,
|
2015-12-09 06:20:58 -08:00
|
|
|
const std::string& codec_name,
|
2013-01-29 12:09:21 +00:00
|
|
|
AudioDecoder* decoder) {
|
|
|
|
|
if (rtp_payload_type > 0x7F) {
|
|
|
|
|
return kInvalidRtpPayloadType;
|
|
|
|
|
}
|
|
|
|
|
if (!decoder) {
|
|
|
|
|
return kInvalidPointer;
|
|
|
|
|
}
|
2016-09-23 02:19:43 -07:00
|
|
|
|
|
|
|
|
const auto opt_db_format =
|
|
|
|
|
acm2::RentACodec::NetEqDecoderToSdpAudioFormat(codec_type);
|
|
|
|
|
const SdpAudioFormat format = opt_db_format.value_or({"arbitrary", 0, 0});
|
|
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
std::pair<DecoderMap::iterator, bool> ret;
|
2016-09-23 02:19:43 -07:00
|
|
|
DecoderInfo info(format, decoder);
|
|
|
|
|
info.name = codec_name;
|
2016-04-19 05:03:45 -07:00
|
|
|
ret = decoders_.insert(std::make_pair(rtp_payload_type, std::move(info)));
|
2013-01-29 12:09:21 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
if (active_decoder_type_ == rtp_payload_type) {
|
|
|
|
|
active_decoder_type_ = -1; // No active decoder.
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
if (active_cng_decoder_type_ == rtp_payload_type) {
|
|
|
|
|
active_cng_decoder_type_ = -1; // No active CNG decoder.
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 04:02:25 -07:00
|
|
|
void DecoderDatabase::RemoveAll() {
|
|
|
|
|
decoders_.clear();
|
|
|
|
|
active_decoder_type_ = -1; // No active decoder.
|
|
|
|
|
active_cng_decoder_type_ = -1; // No active CNG decoder.
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
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 NULL;
|
|
|
|
|
}
|
2016-09-23 02:19:43 -07:00
|
|
|
return &it->second;
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DecoderDatabase::SetActiveDecoder(uint8_t rtp_payload_type,
|
|
|
|
|
bool* new_decoder) {
|
|
|
|
|
// Check that |rtp_payload_type| exists in the database.
|
2016-08-26 05:41:23 -07:00
|
|
|
const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
|
|
|
|
|
if (!info) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Decoder not found.
|
|
|
|
|
return kDecoderNotFound;
|
|
|
|
|
}
|
2016-08-26 05:41:23 -07:00
|
|
|
RTC_CHECK(!info->IsComfortNoise());
|
|
|
|
|
RTC_DCHECK(new_decoder);
|
2013-01-29 12:09:21 +00:00
|
|
|
*new_decoder = false;
|
2016-04-25 07:55:58 -07:00
|
|
|
if (active_decoder_type_ < 0) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// This is the first active decoder.
|
|
|
|
|
*new_decoder = true;
|
2016-04-25 07:55:58 -07:00
|
|
|
} else if (active_decoder_type_ != rtp_payload_type) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Moving from one active decoder to another. Delete the first one.
|
2016-08-26 05:41:23 -07:00
|
|
|
const DecoderInfo *old_info = GetDecoderInfo(active_decoder_type_);
|
|
|
|
|
RTC_DCHECK(old_info);
|
|
|
|
|
old_info->DropDecoder();
|
2013-01-29 12:09:21 +00:00
|
|
|
*new_decoder = true;
|
|
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
active_decoder_type_ = rtp_payload_type;
|
2013-01-29 12:09:21 +00:00
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-26 05:41:23 -07:00
|
|
|
AudioDecoder* DecoderDatabase::GetActiveDecoder() const {
|
2016-04-25 07:55:58 -07:00
|
|
|
if (active_decoder_type_ < 0) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// No active decoder.
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
return GetDecoder(active_decoder_type_);
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) {
|
|
|
|
|
// Check that |rtp_payload_type| exists in the database.
|
2016-08-26 05:41:23 -07:00
|
|
|
const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
|
|
|
|
|
if (!info) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Decoder not found.
|
|
|
|
|
return kDecoderNotFound;
|
|
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
if (active_cng_decoder_type_ >= 0 &&
|
|
|
|
|
active_cng_decoder_type_ != rtp_payload_type) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Moving from one active CNG decoder to another. Delete the first one.
|
2016-08-26 05:41:23 -07:00
|
|
|
RTC_DCHECK(active_cng_decoder_);
|
2016-04-25 07:55:58 -07:00
|
|
|
active_cng_decoder_.reset();
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
active_cng_decoder_type_ = rtp_payload_type;
|
2013-01-29 12:09:21 +00:00
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-26 05:41:23 -07:00
|
|
|
ComfortNoiseDecoder* DecoderDatabase::GetActiveCngDecoder() const {
|
2016-04-25 07:55:58 -07:00
|
|
|
if (active_cng_decoder_type_ < 0) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// No active CNG decoder.
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
if (!active_cng_decoder_) {
|
|
|
|
|
active_cng_decoder_.reset(new ComfortNoiseDecoder);
|
|
|
|
|
}
|
|
|
|
|
return active_cng_decoder_.get();
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-26 05:41:23 -07:00
|
|
|
AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) const {
|
|
|
|
|
const DecoderInfo *info = GetDecoderInfo(rtp_payload_type);
|
|
|
|
|
return info ? info->GetDecoder() : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 02:19:43 -07:00
|
|
|
bool DecoderDatabase::IsType(uint8_t rtp_payload_type, const char* name) const {
|
|
|
|
|
const DecoderInfo* info = GetDecoderInfo(rtp_payload_type);
|
|
|
|
|
return info && info->IsType(name);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-26 05:41:23 -07:00
|
|
|
bool DecoderDatabase::IsType(uint8_t rtp_payload_type,
|
2016-09-23 02:19:43 -07:00
|
|
|
const std::string& name) const {
|
|
|
|
|
return IsType(rtp_payload_type, name.c_str());
|
2016-08-26 05:41:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-29 12:09:21 +00:00
|
|
|
int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const {
|
|
|
|
|
PacketList::const_iterator it;
|
|
|
|
|
for (it = packet_list.begin(); it != packet_list.end(); ++it) {
|
2016-08-26 05:41:23 -07:00
|
|
|
if (!GetDecoderInfo((*it)->header.payloadType)) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// Payload type is not found.
|
Switch to base/logging.h in neteq_impl.cc
This change includes base/logging.h instead of the old and deprecated
system_wrappers/interface/logging.h. This requires some changes of the
actual logging invocations.
For reference the following regexps where used (in Eclipse) for a few
of the replacements:
find: LOG_FERR1\(\s*([^,]*),\s*([^,]*),\s*(.*)\);
replace: LOG($1) << "$2 " << $3;
find: LOG_FERR2\(\s*([^,]*),\s*([^,]*),\s*([^,]*),\s*(.*)\);
replace: LOG($1) << "$2 " << $3 << " " << $4;
BUG=4735
R=minyue@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/50229004 .
Cr-Commit-Position: refs/heads/master@{#9669}
2015-08-03 12:54:37 +02:00
|
|
|
LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type "
|
|
|
|
|
<< static_cast<int>((*it)->header.payloadType);
|
2013-01-29 12:09:21 +00:00
|
|
|
return kDecoderNotFound;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return kOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|