Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 /* | 1 /* | 
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 
| 3 * | 3 * | 
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found | 
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may | 
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. | 
| 9 */ | 9 */ | 
| 10 | 10 | 
| 11 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" | 11 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" | 
| 12 | 12 | 
| 13 #include <assert.h> | 13 #include <assert.h> | 
| 14 #include <utility> // pair | 14 #include <utility> // pair | 
| 15 | 15 | 
| 16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" | 
| 17 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" | 
| 18 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" | 18 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" | 
| 19 | 19 | 
| 20 namespace webrtc { | 20 namespace webrtc { | 
| 21 | 21 | 
| 22 DecoderDatabase::DecoderDatabase() | 22 DecoderDatabase::DecoderDatabase() | 
| 23 : active_decoder_(-1), active_cng_decoder_(-1) {} | 23 : active_decoder_type_(-1), active_cng_decoder_type_(-1) { | 
| 
 
hlundin-webrtc
2016/04/25 10:29:30
I just realized that these are probably good candi
 
 | |
| 24 } | |
| 24 | 25 | 
| 25 DecoderDatabase::~DecoderDatabase() {} | 26 DecoderDatabase::~DecoderDatabase() = default; | 
| 26 | 27 | 
| 27 DecoderDatabase::DecoderInfo::DecoderInfo(NetEqDecoder ct, | 28 DecoderDatabase::DecoderInfo::DecoderInfo(NetEqDecoder ct, | 
| 28 const std::string& nm, | 29 const std::string& nm, | 
| 29 int fs, | 30 int fs, | 
| 30 AudioDecoder* ext_dec) | 31 AudioDecoder* ext_dec) | 
| 31 : codec_type(ct), | 32 : codec_type(ct), | 
| 32 name(nm), | 33 name(nm), | 
| 33 fs_hz(fs), | 34 fs_hz(fs), | 
| 34 external_decoder(ext_dec) {} | 35 external_decoder(ext_dec) {} | 
| 35 | 36 | 
| (...skipping 11 matching lines...) Expand all Loading... | |
| 47 RTC_DCHECK(decoder_); | 48 RTC_DCHECK(decoder_); | 
| 48 return decoder_.get(); | 49 return decoder_.get(); | 
| 49 } | 50 } | 
| 50 | 51 | 
| 51 bool DecoderDatabase::Empty() const { return decoders_.empty(); } | 52 bool DecoderDatabase::Empty() const { return decoders_.empty(); } | 
| 52 | 53 | 
| 53 int DecoderDatabase::Size() const { return static_cast<int>(decoders_.size()); } | 54 int DecoderDatabase::Size() const { return static_cast<int>(decoders_.size()); } | 
| 54 | 55 | 
| 55 void DecoderDatabase::Reset() { | 56 void DecoderDatabase::Reset() { | 
| 56 decoders_.clear(); | 57 decoders_.clear(); | 
| 57 active_decoder_ = -1; | 58 active_decoder_type_ = -1; | 
| 58 active_cng_decoder_ = -1; | 59 active_cng_decoder_type_ = -1; | 
| 59 } | 60 } | 
| 60 | 61 | 
| 61 int DecoderDatabase::RegisterPayload(uint8_t rtp_payload_type, | 62 int DecoderDatabase::RegisterPayload(uint8_t rtp_payload_type, | 
| 62 NetEqDecoder codec_type, | 63 NetEqDecoder codec_type, | 
| 63 const std::string& name) { | 64 const std::string& name) { | 
| 64 if (rtp_payload_type > 0x7F) { | 65 if (rtp_payload_type > 0x7F) { | 
| 65 return kInvalidRtpPayloadType; | 66 return kInvalidRtpPayloadType; | 
| 66 } | 67 } | 
| 67 if (!CodecSupported(codec_type)) { | 68 if (!CodecSupported(codec_type)) { | 
| 68 return kCodecNotSupported; | 69 return kCodecNotSupported; | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 return kDecoderExists; | 104 return kDecoderExists; | 
| 104 } | 105 } | 
| 105 return kOK; | 106 return kOK; | 
| 106 } | 107 } | 
| 107 | 108 | 
| 108 int DecoderDatabase::Remove(uint8_t rtp_payload_type) { | 109 int DecoderDatabase::Remove(uint8_t rtp_payload_type) { | 
| 109 if (decoders_.erase(rtp_payload_type) == 0) { | 110 if (decoders_.erase(rtp_payload_type) == 0) { | 
| 110 // No decoder with that |rtp_payload_type|. | 111 // No decoder with that |rtp_payload_type|. | 
| 111 return kDecoderNotFound; | 112 return kDecoderNotFound; | 
| 112 } | 113 } | 
| 113 if (active_decoder_ == rtp_payload_type) { | 114 if (active_decoder_type_ == rtp_payload_type) { | 
| 114 active_decoder_ = -1; // No active decoder. | 115 active_decoder_type_ = -1; // No active decoder. | 
| 115 } | 116 } | 
| 116 if (active_cng_decoder_ == rtp_payload_type) { | 117 if (active_cng_decoder_type_ == rtp_payload_type) { | 
| 117 active_cng_decoder_ = -1; // No active CNG decoder. | 118 active_cng_decoder_type_ = -1; // No active CNG decoder. | 
| 118 } | 119 } | 
| 119 return kOK; | 120 return kOK; | 
| 120 } | 121 } | 
| 121 | 122 | 
| 122 const DecoderDatabase::DecoderInfo* DecoderDatabase::GetDecoderInfo( | 123 const DecoderDatabase::DecoderInfo* DecoderDatabase::GetDecoderInfo( | 
| 123 uint8_t rtp_payload_type) const { | 124 uint8_t rtp_payload_type) const { | 
| 124 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); | 125 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); | 
| 125 if (it == decoders_.end()) { | 126 if (it == decoders_.end()) { | 
| 126 // Decoder not found. | 127 // Decoder not found. | 
| 127 return NULL; | 128 return NULL; | 
| 128 } | 129 } | 
| 129 return &(*it).second; | 130 return &(*it).second; | 
| 130 } | 131 } | 
| 131 | 132 | 
| 132 uint8_t DecoderDatabase::GetRtpPayloadType( | 133 uint8_t DecoderDatabase::GetRtpPayloadType( | 
| 133 NetEqDecoder codec_type) const { | 134 NetEqDecoder codec_type) const { | 
| 134 DecoderMap::const_iterator it; | 135 DecoderMap::const_iterator it; | 
| 135 for (it = decoders_.begin(); it != decoders_.end(); ++it) { | 136 for (it = decoders_.begin(); it != decoders_.end(); ++it) { | 
| 136 if ((*it).second.codec_type == codec_type) { | 137 if ((*it).second.codec_type == codec_type) { | 
| 137 // Match found. | 138 // Match found. | 
| 138 return (*it).first; | 139 return (*it).first; | 
| 139 } | 140 } | 
| 140 } | 141 } | 
| 141 // No match. | 142 // No match. | 
| 142 return kRtpPayloadTypeError; | 143 return kRtpPayloadTypeError; | 
| 143 } | 144 } | 
| 144 | 145 | 
| 145 AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) { | 146 AudioDecoder* DecoderDatabase::GetDecoder(uint8_t rtp_payload_type) { | 
| 146 if (IsDtmf(rtp_payload_type) || IsRed(rtp_payload_type)) { | 147 if (IsDtmf(rtp_payload_type) || IsRed(rtp_payload_type) || | 
| 148 IsComfortNoise(rtp_payload_type)) { | |
| 147 // These are not real decoders. | 149 // These are not real decoders. | 
| 148 return NULL; | 150 return NULL; | 
| 149 } | 151 } | 
| 150 DecoderMap::iterator it = decoders_.find(rtp_payload_type); | 152 DecoderMap::iterator it = decoders_.find(rtp_payload_type); | 
| 151 if (it == decoders_.end()) { | 153 if (it == decoders_.end()) { | 
| 152 // Decoder not found. | 154 // Decoder not found. | 
| 153 return NULL; | 155 return NULL; | 
| 154 } | 156 } | 
| 155 DecoderInfo* info = &(*it).second; | 157 DecoderInfo* info = &(*it).second; | 
| 156 return info->GetDecoder(); | 158 return info->GetDecoder(); | 
| (...skipping 29 matching lines...) Expand all Loading... | |
| 186 } | 188 } | 
| 187 | 189 | 
| 188 int DecoderDatabase::SetActiveDecoder(uint8_t rtp_payload_type, | 190 int DecoderDatabase::SetActiveDecoder(uint8_t rtp_payload_type, | 
| 189 bool* new_decoder) { | 191 bool* new_decoder) { | 
| 190 // Check that |rtp_payload_type| exists in the database. | 192 // Check that |rtp_payload_type| exists in the database. | 
| 191 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); | 193 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); | 
| 192 if (it == decoders_.end()) { | 194 if (it == decoders_.end()) { | 
| 193 // Decoder not found. | 195 // Decoder not found. | 
| 194 return kDecoderNotFound; | 196 return kDecoderNotFound; | 
| 195 } | 197 } | 
| 198 RTC_CHECK(!IsComfortNoise(rtp_payload_type)); | |
| 196 assert(new_decoder); | 199 assert(new_decoder); | 
| 197 *new_decoder = false; | 200 *new_decoder = false; | 
| 198 if (active_decoder_ < 0) { | 201 if (active_decoder_type_ < 0) { | 
| 199 // This is the first active decoder. | 202 // This is the first active decoder. | 
| 200 *new_decoder = true; | 203 *new_decoder = true; | 
| 201 } else if (active_decoder_ != rtp_payload_type) { | 204 } else if (active_decoder_type_ != rtp_payload_type) { | 
| 202 // Moving from one active decoder to another. Delete the first one. | 205 // Moving from one active decoder to another. Delete the first one. | 
| 203 DecoderMap::iterator it = decoders_.find(active_decoder_); | 206 DecoderMap::iterator it = decoders_.find(active_decoder_type_); | 
| 204 if (it == decoders_.end()) { | 207 if (it == decoders_.end()) { | 
| 205 // Decoder not found. This should not be possible. | 208 // Decoder not found. This should not be possible. | 
| 206 assert(false); | 209 assert(false); | 
| 207 return kDecoderNotFound; | 210 return kDecoderNotFound; | 
| 208 } | 211 } | 
| 209 it->second.DropDecoder(); | 212 it->second.DropDecoder(); | 
| 210 *new_decoder = true; | 213 *new_decoder = true; | 
| 211 } | 214 } | 
| 212 active_decoder_ = rtp_payload_type; | 215 active_decoder_type_ = rtp_payload_type; | 
| 213 return kOK; | 216 return kOK; | 
| 214 } | 217 } | 
| 215 | 218 | 
| 216 AudioDecoder* DecoderDatabase::GetActiveDecoder() { | 219 AudioDecoder* DecoderDatabase::GetActiveDecoder() { | 
| 217 if (active_decoder_ < 0) { | 220 if (active_decoder_type_ < 0) { | 
| 218 // No active decoder. | 221 // No active decoder. | 
| 219 return NULL; | 222 return NULL; | 
| 220 } | 223 } | 
| 221 return GetDecoder(active_decoder_); | 224 return GetDecoder(active_decoder_type_); | 
| 222 } | 225 } | 
| 223 | 226 | 
| 224 int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) { | 227 int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) { | 
| 225 // Check that |rtp_payload_type| exists in the database. | 228 // Check that |rtp_payload_type| exists in the database. | 
| 226 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); | 229 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); | 
| 227 if (it == decoders_.end()) { | 230 if (it == decoders_.end()) { | 
| 228 // Decoder not found. | 231 // Decoder not found. | 
| 229 return kDecoderNotFound; | 232 return kDecoderNotFound; | 
| 230 } | 233 } | 
| 231 if (active_cng_decoder_ >= 0 && active_cng_decoder_ != rtp_payload_type) { | 234 if (active_cng_decoder_type_ >= 0 && | 
| 235 active_cng_decoder_type_ != rtp_payload_type) { | |
| 232 // Moving from one active CNG decoder to another. Delete the first one. | 236 // Moving from one active CNG decoder to another. Delete the first one. | 
| 233 DecoderMap::iterator it = decoders_.find(active_cng_decoder_); | 237 DecoderMap::iterator it = decoders_.find(active_cng_decoder_type_); | 
| 234 if (it == decoders_.end()) { | 238 if (it == decoders_.end()) { | 
| 235 // Decoder not found. This should not be possible. | 239 // Decoder not found. This should not be possible. | 
| 236 assert(false); | 240 assert(false); | 
| 237 return kDecoderNotFound; | 241 return kDecoderNotFound; | 
| 238 } | 242 } | 
| 239 it->second.DropDecoder(); | 243 // The CNG decoder should never be provided externally. | 
| 244 RTC_CHECK(!it->second.external_decoder); | |
| 245 active_cng_decoder_.reset(); | |
| 240 } | 246 } | 
| 241 active_cng_decoder_ = rtp_payload_type; | 247 active_cng_decoder_type_ = rtp_payload_type; | 
| 242 return kOK; | 248 return kOK; | 
| 243 } | 249 } | 
| 244 | 250 | 
| 245 AudioDecoder* DecoderDatabase::GetActiveCngDecoder() { | 251 ComfortNoiseDecoder* DecoderDatabase::GetActiveCngDecoder() { | 
| 246 if (active_cng_decoder_ < 0) { | 252 if (active_cng_decoder_type_ < 0) { | 
| 247 // No active CNG decoder. | 253 // No active CNG decoder. | 
| 248 return NULL; | 254 return NULL; | 
| 249 } | 255 } | 
| 250 return GetDecoder(active_cng_decoder_); | 256 if (!active_cng_decoder_) { | 
| 257 active_cng_decoder_.reset(new ComfortNoiseDecoder); | |
| 258 } | |
| 259 return active_cng_decoder_.get(); | |
| 251 } | 260 } | 
| 252 | 261 | 
| 253 int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const { | 262 int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const { | 
| 254 PacketList::const_iterator it; | 263 PacketList::const_iterator it; | 
| 255 for (it = packet_list.begin(); it != packet_list.end(); ++it) { | 264 for (it = packet_list.begin(); it != packet_list.end(); ++it) { | 
| 256 if (decoders_.find((*it)->header.payloadType) == decoders_.end()) { | 265 if (decoders_.find((*it)->header.payloadType) == decoders_.end()) { | 
| 257 // Payload type is not found. | 266 // Payload type is not found. | 
| 258 LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type " | 267 LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type " | 
| 259 << static_cast<int>((*it)->header.payloadType); | 268 << static_cast<int>((*it)->header.payloadType); | 
| 260 return kDecoderNotFound; | 269 return kDecoderNotFound; | 
| 261 } | 270 } | 
| 262 } | 271 } | 
| 263 return kOK; | 272 return kOK; | 
| 264 } | 273 } | 
| 265 | 274 | 
| 266 | 275 | 
| 267 } // namespace webrtc | 276 } // namespace webrtc | 
| OLD | NEW |