Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(678)

Side by Side Diff: webrtc/modules/audio_coding/neteq/decoder_database.cc

Issue 1899733002: NetEq: Simplify DecoderDatabase::DecoderInfo (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: review comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_(-1), active_cng_decoder_(-1) {}
24 24
25 DecoderDatabase::~DecoderDatabase() {} 25 DecoderDatabase::~DecoderDatabase() {}
26 26
27 DecoderDatabase::DecoderInfo::~DecoderInfo() { 27 DecoderDatabase::DecoderInfo::DecoderInfo(NetEqDecoder ct,
28 if (!external) delete decoder; 28 const std::string& nm,
29 int fs,
30 AudioDecoder* ext_dec)
31 : codec_type(ct),
32 name(nm),
33 fs_hz(fs),
34 rtp_sample_rate_hz(fs),
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();
29 } 50 }
30 51
31 bool DecoderDatabase::Empty() const { return decoders_.empty(); } 52 bool DecoderDatabase::Empty() const { return decoders_.empty(); }
32 53
33 int DecoderDatabase::Size() const { return static_cast<int>(decoders_.size()); } 54 int DecoderDatabase::Size() const { return static_cast<int>(decoders_.size()); }
34 55
35 void DecoderDatabase::Reset() { 56 void DecoderDatabase::Reset() {
36 decoders_.clear(); 57 decoders_.clear();
37 active_decoder_ = -1; 58 active_decoder_ = -1;
38 active_cng_decoder_ = -1; 59 active_cng_decoder_ = -1;
39 } 60 }
40 61
41 int DecoderDatabase::RegisterPayload(uint8_t rtp_payload_type, 62 int DecoderDatabase::RegisterPayload(uint8_t rtp_payload_type,
42 NetEqDecoder codec_type, 63 NetEqDecoder codec_type,
43 const std::string& name) { 64 const std::string& name) {
44 if (rtp_payload_type > 0x7F) { 65 if (rtp_payload_type > 0x7F) {
45 return kInvalidRtpPayloadType; 66 return kInvalidRtpPayloadType;
46 } 67 }
47 if (!CodecSupported(codec_type)) { 68 if (!CodecSupported(codec_type)) {
48 return kCodecNotSupported; 69 return kCodecNotSupported;
49 } 70 }
50 const int fs_hz = CodecSampleRateHz(codec_type); 71 const int fs_hz = CodecSampleRateHz(codec_type);
51 DecoderInfo info(codec_type, name, fs_hz, NULL, false); 72 DecoderInfo info(codec_type, name, fs_hz, nullptr);
52 auto ret = decoders_.insert(std::make_pair(rtp_payload_type, info)); 73 auto ret =
74 decoders_.insert(std::make_pair(rtp_payload_type, std::move(info)));
53 if (ret.second == false) { 75 if (ret.second == false) {
54 // Database already contains a decoder with type |rtp_payload_type|. 76 // Database already contains a decoder with type |rtp_payload_type|.
55 return kDecoderExists; 77 return kDecoderExists;
56 } 78 }
57 return kOK; 79 return kOK;
58 } 80 }
59 81
60 int DecoderDatabase::InsertExternal(uint8_t rtp_payload_type, 82 int DecoderDatabase::InsertExternal(uint8_t rtp_payload_type,
61 NetEqDecoder codec_type, 83 NetEqDecoder codec_type,
62 const std::string& codec_name, 84 const std::string& codec_name,
63 int fs_hz, 85 int fs_hz,
64 AudioDecoder* decoder) { 86 AudioDecoder* decoder) {
65 if (rtp_payload_type > 0x7F) { 87 if (rtp_payload_type > 0x7F) {
66 return kInvalidRtpPayloadType; 88 return kInvalidRtpPayloadType;
67 } 89 }
68 if (!CodecSupported(codec_type)) { 90 if (!CodecSupported(codec_type)) {
69 return kCodecNotSupported; 91 return kCodecNotSupported;
70 } 92 }
71 if (fs_hz != 8000 && fs_hz != 16000 && fs_hz != 32000 && fs_hz != 48000) { 93 if (fs_hz != 8000 && fs_hz != 16000 && fs_hz != 32000 && fs_hz != 48000) {
72 return kInvalidSampleRate; 94 return kInvalidSampleRate;
73 } 95 }
74 if (!decoder) { 96 if (!decoder) {
75 return kInvalidPointer; 97 return kInvalidPointer;
76 } 98 }
77 std::pair<DecoderMap::iterator, bool> ret; 99 std::pair<DecoderMap::iterator, bool> ret;
78 DecoderInfo info(codec_type, codec_name, fs_hz, decoder, true); 100 DecoderInfo info(codec_type, codec_name, fs_hz, decoder);
79 ret = decoders_.insert(std::make_pair(rtp_payload_type, info)); 101 ret = decoders_.insert(std::make_pair(rtp_payload_type, std::move(info)));
80 if (ret.second == false) { 102 if (ret.second == false) {
81 // Database already contains a decoder with type |rtp_payload_type|. 103 // Database already contains a decoder with type |rtp_payload_type|.
82 return kDecoderExists; 104 return kDecoderExists;
83 } 105 }
84 return kOK; 106 return kOK;
85 } 107 }
86 108
87 int DecoderDatabase::Remove(uint8_t rtp_payload_type) { 109 int DecoderDatabase::Remove(uint8_t rtp_payload_type) {
88 if (decoders_.erase(rtp_payload_type) == 0) { 110 if (decoders_.erase(rtp_payload_type) == 0) {
89 // No decoder with that |rtp_payload_type|. 111 // No decoder with that |rtp_payload_type|.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 if (IsDtmf(rtp_payload_type) || IsRed(rtp_payload_type)) { 147 if (IsDtmf(rtp_payload_type) || IsRed(rtp_payload_type)) {
126 // These are not real decoders. 148 // These are not real decoders.
127 return NULL; 149 return NULL;
128 } 150 }
129 DecoderMap::iterator it = decoders_.find(rtp_payload_type); 151 DecoderMap::iterator it = decoders_.find(rtp_payload_type);
130 if (it == decoders_.end()) { 152 if (it == decoders_.end()) {
131 // Decoder not found. 153 // Decoder not found.
132 return NULL; 154 return NULL;
133 } 155 }
134 DecoderInfo* info = &(*it).second; 156 DecoderInfo* info = &(*it).second;
135 if (!info->decoder) { 157 return info->GetDecoder();
136 // Create the decoder object.
137 AudioDecoder* decoder = CreateAudioDecoder(info->codec_type);
138 assert(decoder); // Should not be able to have an unsupported codec here.
139 info->decoder = decoder;
140 }
141 return info->decoder;
142 } 158 }
143 159
144 bool DecoderDatabase::IsType(uint8_t rtp_payload_type, 160 bool DecoderDatabase::IsType(uint8_t rtp_payload_type,
145 NetEqDecoder codec_type) const { 161 NetEqDecoder codec_type) const {
146 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); 162 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
147 if (it == decoders_.end()) { 163 if (it == decoders_.end()) {
148 // Decoder not found. 164 // Decoder not found.
149 return false; 165 return false;
150 } 166 }
151 return ((*it).second.codec_type == codec_type); 167 return ((*it).second.codec_type == codec_type);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 // This is the first active decoder. 200 // This is the first active decoder.
185 *new_decoder = true; 201 *new_decoder = true;
186 } else if (active_decoder_ != rtp_payload_type) { 202 } else if (active_decoder_ != rtp_payload_type) {
187 // Moving from one active decoder to another. Delete the first one. 203 // Moving from one active decoder to another. Delete the first one.
188 DecoderMap::iterator it = decoders_.find(active_decoder_); 204 DecoderMap::iterator it = decoders_.find(active_decoder_);
189 if (it == decoders_.end()) { 205 if (it == decoders_.end()) {
190 // Decoder not found. This should not be possible. 206 // Decoder not found. This should not be possible.
191 assert(false); 207 assert(false);
192 return kDecoderNotFound; 208 return kDecoderNotFound;
193 } 209 }
194 if (!(*it).second.external) { 210 it->second.DropDecoder();
195 // Delete the AudioDecoder object, unless it is an externally created
196 // decoder.
197 delete (*it).second.decoder;
198 (*it).second.decoder = NULL;
199 }
200 *new_decoder = true; 211 *new_decoder = true;
201 } 212 }
202 active_decoder_ = rtp_payload_type; 213 active_decoder_ = rtp_payload_type;
203 return kOK; 214 return kOK;
204 } 215 }
205 216
206 AudioDecoder* DecoderDatabase::GetActiveDecoder() { 217 AudioDecoder* DecoderDatabase::GetActiveDecoder() {
207 if (active_decoder_ < 0) { 218 if (active_decoder_ < 0) {
208 // No active decoder. 219 // No active decoder.
209 return NULL; 220 return NULL;
210 } 221 }
211 return GetDecoder(active_decoder_); 222 return GetDecoder(active_decoder_);
212 } 223 }
213 224
214 int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) { 225 int DecoderDatabase::SetActiveCngDecoder(uint8_t rtp_payload_type) {
215 // Check that |rtp_payload_type| exists in the database. 226 // Check that |rtp_payload_type| exists in the database.
216 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type); 227 DecoderMap::const_iterator it = decoders_.find(rtp_payload_type);
217 if (it == decoders_.end()) { 228 if (it == decoders_.end()) {
218 // Decoder not found. 229 // Decoder not found.
219 return kDecoderNotFound; 230 return kDecoderNotFound;
220 } 231 }
221 if (active_cng_decoder_ >= 0 && active_cng_decoder_ != rtp_payload_type) { 232 if (active_cng_decoder_ >= 0 && active_cng_decoder_ != rtp_payload_type) {
222 // Moving from one active CNG decoder to another. Delete the first one. 233 // Moving from one active CNG decoder to another. Delete the first one.
223 DecoderMap::iterator it = decoders_.find(active_cng_decoder_); 234 DecoderMap::iterator it = decoders_.find(active_cng_decoder_);
224 if (it == decoders_.end()) { 235 if (it == decoders_.end()) {
225 // Decoder not found. This should not be possible. 236 // Decoder not found. This should not be possible.
226 assert(false); 237 assert(false);
227 return kDecoderNotFound; 238 return kDecoderNotFound;
228 } 239 }
229 if (!(*it).second.external) { 240 it->second.DropDecoder();
230 // Delete the AudioDecoder object, unless it is an externally created
231 // decoder.
232 delete (*it).second.decoder;
233 (*it).second.decoder = NULL;
234 }
235 } 241 }
236 active_cng_decoder_ = rtp_payload_type; 242 active_cng_decoder_ = rtp_payload_type;
237 return kOK; 243 return kOK;
238 } 244 }
239 245
240 AudioDecoder* DecoderDatabase::GetActiveCngDecoder() { 246 AudioDecoder* DecoderDatabase::GetActiveCngDecoder() {
241 if (active_cng_decoder_ < 0) { 247 if (active_cng_decoder_ < 0) {
242 // No active CNG decoder. 248 // No active CNG decoder.
243 return NULL; 249 return NULL;
244 } 250 }
245 return GetDecoder(active_cng_decoder_); 251 return GetDecoder(active_cng_decoder_);
246 } 252 }
247 253
248 int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const { 254 int DecoderDatabase::CheckPayloadTypes(const PacketList& packet_list) const {
249 PacketList::const_iterator it; 255 PacketList::const_iterator it;
250 for (it = packet_list.begin(); it != packet_list.end(); ++it) { 256 for (it = packet_list.begin(); it != packet_list.end(); ++it) {
251 if (decoders_.find((*it)->header.payloadType) == decoders_.end()) { 257 if (decoders_.find((*it)->header.payloadType) == decoders_.end()) {
252 // Payload type is not found. 258 // Payload type is not found.
253 LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type " 259 LOG(LS_WARNING) << "CheckPayloadTypes: unknown RTP payload type "
254 << static_cast<int>((*it)->header.payloadType); 260 << static_cast<int>((*it)->header.payloadType);
255 return kDecoderNotFound; 261 return kDecoderNotFound;
256 } 262 }
257 } 263 }
258 return kOK; 264 return kOK;
259 } 265 }
260 266
261 267
262 } // namespace webrtc 268 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/neteq/decoder_database.h ('k') | webrtc/modules/audio_coding/neteq/decoder_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698