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 <assert.h>
|
|
|
|
|
#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 {
|
|
|
|
|
|
2013-07-31 15:54:00 +00:00
|
|
|
DecoderDatabase::DecoderDatabase()
|
2016-04-25 07:55:58 -07:00
|
|
|
: active_decoder_type_(-1), active_cng_decoder_type_(-1) {
|
|
|
|
|
}
|
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-04-19 05:03:45 -07:00
|
|
|
DecoderDatabase::DecoderInfo::DecoderInfo(NetEqDecoder ct,
|
|
|
|
|
const std::string& nm,
|
|
|
|
|
int fs,
|
|
|
|
|
AudioDecoder* ext_dec)
|
|
|
|
|
: codec_type(ct),
|
|
|
|
|
name(nm),
|
|
|
|
|
fs_hz(fs),
|
|
|
|
|
external_decoder(ext_dec) {}
|
|
|
|
|
|
|
|
|
|
DecoderDatabase::DecoderInfo::DecoderInfo(DecoderInfo&&) = default;
|
|
|
|
|
DecoderDatabase::DecoderInfo::~DecoderInfo() = default;
|
|
|
|
|
|
|
|
|
|
AudioDecoder* DecoderDatabase::DecoderInfo::GetDecoder() {
|
|
|
|
|
if (external_decoder) {
|
|
|
|
|
RTC_DCHECK(!decoder_);
|
|
|
|
|
return external_decoder;
|
|
|
|
|
}
|
|
|
|
|
if (!decoder_) {
|
|
|
|
|
decoder_.reset(CreateAudioDecoder(codec_type));
|
|
|
|
|
}
|
|
|
|
|
RTC_DCHECK(decoder_);
|
|
|
|
|
return decoder_.get();
|
2013-01-29 12:09:21 +00: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;
|
|
|
|
|
}
|
2014-12-09 10:12:53 +00:00
|
|
|
if (!CodecSupported(codec_type)) {
|
2013-01-29 12:09:21 +00:00
|
|
|
return kCodecNotSupported;
|
|
|
|
|
}
|
2015-12-09 06:20:58 -08:00
|
|
|
const int fs_hz = CodecSampleRateHz(codec_type);
|
2016-04-19 05:03:45 -07:00
|
|
|
DecoderInfo info(codec_type, name, fs_hz, nullptr);
|
|
|
|
|
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
|
|
|
int fs_hz,
|
|
|
|
|
AudioDecoder* decoder) {
|
|
|
|
|
if (rtp_payload_type > 0x7F) {
|
|
|
|
|
return kInvalidRtpPayloadType;
|
|
|
|
|
}
|
2014-12-09 10:12:53 +00:00
|
|
|
if (!CodecSupported(codec_type)) {
|
2013-01-29 12:09:21 +00:00
|
|
|
return kCodecNotSupported;
|
|
|
|
|
}
|
|
|
|
|
if (fs_hz != 8000 && fs_hz != 16000 && fs_hz != 32000 && fs_hz != 48000) {
|
|
|
|
|
return kInvalidSampleRate;
|
|
|
|
|
}
|
|
|
|
|
if (!decoder) {
|
|
|
|
|
return kInvalidPointer;
|
|
|
|
|
}
|
|
|
|
|
std::pair<DecoderMap::iterator, bool> ret;
|
2016-04-19 05:03:45 -07:00
|
|
|
DecoderInfo info(codec_type, codec_name, fs_hz, decoder);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return &(*it).second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t DecoderDatabase::GetRtpPayloadType(
|
|
|
|
|
NetEqDecoder codec_type) const {
|
|
|
|
|
DecoderMap::const_iterator it;
|
|
|
|
|
for (it = decoders_.begin(); it != decoders_.end(); ++it) {
|
|
|
|
|
if ((*it).second.codec_type == codec_type) {
|
|
|
|
|
// Match found.
|
|
|
|
|
return (*it).first;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// No match.
|
|
|
|
|
return kRtpPayloadTypeError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) {
|
2016-04-25 07:55:58 -07:00
|
|
|
if (IsDtmf(rtp_payload_type) || IsRed(rtp_payload_type) ||
|
|
|
|
|
IsComfortNoise(rtp_payload_type)) {
|
2013-01-29 12:09:21 +00:00
|
|
|
// These are not real decoders.
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
DecoderMap::iterator it = decoders_.find(rtp_payload_type);
|
|
|
|
|
if (it == decoders_.end()) {
|
|
|
|
|
// Decoder not found.
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
DecoderInfo* info = &(*it).second;
|
2016-04-19 05:03:45 -07:00
|
|
|
return info->GetDecoder();
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::IsType(uint8_t rtp_payload_type,
|
|
|
|
|
NetEqDecoder codec_type) const {
|
|
|
|
|
DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
|
|
|
|
|
if (it == decoders_.end()) {
|
|
|
|
|
// Decoder not found.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return ((*it).second.codec_type == codec_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::IsComfortNoise(uint8_t rtp_payload_type) const {
|
2016-04-27 07:43:38 -07:00
|
|
|
DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
|
|
|
|
|
if (it == decoders_.end()) {
|
|
|
|
|
// Decoder not found.
|
2013-01-29 12:09:21 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-04-27 07:43:38 -07:00
|
|
|
const auto& type = it->second.codec_type;
|
|
|
|
|
return type == NetEqDecoder::kDecoderCNGnb
|
|
|
|
|
|| type == NetEqDecoder::kDecoderCNGwb
|
|
|
|
|
|| type == NetEqDecoder::kDecoderCNGswb32kHz
|
|
|
|
|
|| type == NetEqDecoder::kDecoderCNGswb48kHz;
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::IsDtmf(uint8_t rtp_payload_type) const {
|
2015-10-29 06:20:28 -07:00
|
|
|
return IsType(rtp_payload_type, NetEqDecoder::kDecoderAVT);
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DecoderDatabase::IsRed(uint8_t rtp_payload_type) const {
|
2015-10-29 06:20:28 -07:00
|
|
|
return IsType(rtp_payload_type, NetEqDecoder::kDecoderRED);
|
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.
|
|
|
|
|
DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
|
|
|
|
|
if (it == decoders_.end()) {
|
|
|
|
|
// Decoder not found.
|
|
|
|
|
return kDecoderNotFound;
|
|
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
RTC_CHECK(!IsComfortNoise(rtp_payload_type));
|
2013-01-29 12:09:21 +00:00
|
|
|
assert(new_decoder);
|
|
|
|
|
*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-04-25 07:55:58 -07:00
|
|
|
DecoderMap::iterator it = decoders_.find(active_decoder_type_);
|
2013-01-29 12:09:21 +00:00
|
|
|
if (it == decoders_.end()) {
|
|
|
|
|
// Decoder not found. This should not be possible.
|
|
|
|
|
assert(false);
|
|
|
|
|
return kDecoderNotFound;
|
|
|
|
|
}
|
2016-04-19 05:03:45 -07:00
|
|
|
it->second.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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioDecoder* DecoderDatabase::GetActiveDecoder() {
|
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.
|
|
|
|
|
DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
|
|
|
|
|
if (it == decoders_.end()) {
|
|
|
|
|
// 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-04-25 07:55:58 -07:00
|
|
|
DecoderMap::iterator it = decoders_.find(active_cng_decoder_type_);
|
2013-01-29 12:09:21 +00:00
|
|
|
if (it == decoders_.end()) {
|
|
|
|
|
// Decoder not found. This should not be possible.
|
|
|
|
|
assert(false);
|
|
|
|
|
return kDecoderNotFound;
|
|
|
|
|
}
|
2016-04-25 07:55:58 -07:00
|
|
|
// The CNG decoder should never be provided externally.
|
|
|
|
|
RTC_CHECK(!it->second.external_decoder);
|
|
|
|
|
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-04-25 07:55:58 -07:00
|
|
|
ComfortNoiseDecoder* DecoderDatabase::GetActiveCngDecoder() {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const {
|
|
|
|
|
PacketList::const_iterator it;
|
|
|
|
|
for (it = packet_list.begin(); it != packet_list.end(); ++it) {
|
|
|
|
|
if (decoders_.find((*it)->header.payloadType) == decoders_.end()) {
|
|
|
|
|
// 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
|