blob: 131e769079fe7f56fd56fb77d9541556e1491024 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
12#define MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
14#include <map>
kwiberg84be5112016-04-27 01:19:58 -070015#include <memory>
henrik.lundin4cf61dd2015-12-09 06:20:58 -080016#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/audio_decoder_factory.h"
19#include "api/audio_codecs/audio_format.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "common_types.h" // NOLINT(build/include) // NULL
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/codecs/cng/webrtc_cng.h"
Karl Wiberg31fbb542017-10-16 12:42:38 +020022#include "modules/audio_coding/neteq/neteq_decoder_enum.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/neteq/packet.h"
24#include "rtc_base/constructormagic.h"
25#include "rtc_base/scoped_ref_ptr.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026
27namespace webrtc {
28
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029class DecoderDatabase {
30 public:
31 enum DatabaseReturnCodes {
32 kOK = 0,
33 kInvalidRtpPayloadType = -1,
34 kCodecNotSupported = -2,
35 kInvalidSampleRate = -3,
36 kDecoderExists = -4,
37 kDecoderNotFound = -5,
38 kInvalidPointer = -6
39 };
40
kwiberg0fa0a972016-04-19 05:03:45 -070041 // Class that stores decoder info in the database.
42 class DecoderInfo {
43 public:
kwiberge9413062016-11-03 05:29:05 -070044 DecoderInfo(const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020045 absl::optional<AudioCodecPairId> codec_pair_id,
kwiberge9413062016-11-03 05:29:05 -070046 AudioDecoderFactory* factory,
47 const std::string& codec_name);
ossuf1b08da2016-09-23 02:19:43 -070048 explicit DecoderInfo(const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020049 absl::optional<AudioCodecPairId> codec_pair_id,
ossuf1b08da2016-09-23 02:19:43 -070050 AudioDecoderFactory* factory = nullptr);
51 explicit DecoderInfo(NetEqDecoder ct,
Danil Chapovalovb6021232018-06-19 13:26:36 +020052 absl::optional<AudioCodecPairId> codec_pair_id,
ossuf1b08da2016-09-23 02:19:43 -070053 AudioDecoderFactory* factory = nullptr);
kwiberge9413062016-11-03 05:29:05 -070054 DecoderInfo(const SdpAudioFormat& audio_format,
55 AudioDecoder* ext_dec,
56 const std::string& codec_name);
kwiberg0fa0a972016-04-19 05:03:45 -070057 DecoderInfo(DecoderInfo&&);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058 ~DecoderInfo();
59
Karl Wiberg31fbb542017-10-16 12:42:38 +020060 // Was this info object created with a specification that allows us to
61 // actually produce a decoder?
62 bool CanGetDecoder() const;
63
kwiberg0fa0a972016-04-19 05:03:45 -070064 // Get the AudioDecoder object, creating it first if necessary.
ossu84bc9852016-08-26 05:41:23 -070065 AudioDecoder* GetDecoder() const;
kwiberg0fa0a972016-04-19 05:03:45 -070066
67 // Delete the AudioDecoder object, unless it's external. (This means we can
68 // always recreate it later if we need it.)
ossu84bc9852016-08-26 05:41:23 -070069 void DropDecoder() const { decoder_.reset(); }
kwiberg0fa0a972016-04-19 05:03:45 -070070
kwibergc0f2dcf2016-05-31 06:28:03 -070071 int SampleRateHz() const {
solenberg2779bab2016-11-17 04:45:19 -080072 if (IsDtmf()) {
73 // DTMF has a 1:1 mapping between clock rate and sample rate.
74 return audio_format_.clockrate_hz;
75 }
ossu09f15602016-08-29 03:59:05 -070076 const AudioDecoder* decoder = GetDecoder();
77 RTC_DCHECK_EQ(1, !!decoder + !!cng_decoder_);
78 return decoder ? decoder->SampleRateHz() : cng_decoder_->sample_rate_hz;
79 }
80
ossuf1b08da2016-09-23 02:19:43 -070081 const SdpAudioFormat& GetFormat() const { return audio_format_; }
kwibergc0f2dcf2016-05-31 06:28:03 -070082
ossuf1b08da2016-09-23 02:19:43 -070083 // Returns true if the decoder's format is comfort noise.
ossu9f38c212016-10-04 05:23:32 -070084 bool IsComfortNoise() const {
85 RTC_DCHECK_EQ(!!cng_decoder_, subtype_ == Subtype::kComfortNoise);
86 return subtype_ == Subtype::kComfortNoise;
87 }
ossu84bc9852016-08-26 05:41:23 -070088
ossuf1b08da2016-09-23 02:19:43 -070089 // Returns true if the decoder's format is DTMF.
Yves Gerey665174f2018-06-19 15:03:05 +020090 bool IsDtmf() const { return subtype_ == Subtype::kDtmf; }
ossu84bc9852016-08-26 05:41:23 -070091
ossuf1b08da2016-09-23 02:19:43 -070092 // Returns true if the decoder's format is RED.
Yves Gerey665174f2018-06-19 15:03:05 +020093 bool IsRed() const { return subtype_ == Subtype::kRed; }
ossu84bc9852016-08-26 05:41:23 -070094
ossuf1b08da2016-09-23 02:19:43 -070095 // Returns true if the decoder's format is named |name|.
96 bool IsType(const char* name) const;
97 // Returns true if the decoder's format is named |name|.
98 bool IsType(const std::string& name) const;
99
kwiberge9413062016-11-03 05:29:05 -0700100 const std::string& get_name() const { return name_; }
kwiberg0fa0a972016-04-19 05:03:45 -0700101
102 private:
kwiberge9413062016-11-03 05:29:05 -0700103 // TODO(ossu): |name_| is kept here while we retain the old external
104 // decoder interface. Remove this once using an
105 // AudioDecoderFactory has supplanted the old functionality.
106 const std::string name_;
107
ossuf1b08da2016-09-23 02:19:43 -0700108 const SdpAudioFormat audio_format_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200109 const absl::optional<AudioCodecPairId> codec_pair_id_;
ossuf1b08da2016-09-23 02:19:43 -0700110 AudioDecoderFactory* const factory_;
ossu84bc9852016-08-26 05:41:23 -0700111 mutable std::unique_ptr<AudioDecoder> decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -0700112
113 // Set iff this is an external decoder.
kwiberg342f7402016-06-16 03:18:00 -0700114 AudioDecoder* const external_decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -0700115
116 // Set iff this is a comfort noise decoder.
117 struct CngDecoder {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200118 static absl::optional<CngDecoder> Create(const SdpAudioFormat& format);
kwibergc0f2dcf2016-05-31 06:28:03 -0700119 int sample_rate_hz;
120 };
Danil Chapovalovb6021232018-06-19 13:26:36 +0200121 const absl::optional<CngDecoder> cng_decoder_;
ossu9f38c212016-10-04 05:23:32 -0700122
Yves Gerey665174f2018-06-19 15:03:05 +0200123 enum class Subtype : int8_t { kNormal, kComfortNoise, kDtmf, kRed };
ossu9f38c212016-10-04 05:23:32 -0700124
125 static Subtype SubtypeFromFormat(const SdpAudioFormat& format);
126
127 const Subtype subtype_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000128 };
129
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
131 // only 7 bits).
132 static const uint8_t kRtpPayloadTypeError = 0xFF;
133
ossue725f7c2016-05-19 10:48:04 -0700134 DecoderDatabase(
Karl Wiberg08126342018-03-20 19:18:55 +0100135 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200136 absl::optional<AudioCodecPairId> codec_pair_id);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000137
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000138 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000139
140 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000141 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000142
143 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000144 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000145
146 // Resets the database, erasing all registered payload types, and deleting
147 // any AudioDecoder objects that were not externally created and inserted
148 // using InsertExternal().
149 virtual void Reset();
150
kwiberg1c07c702017-03-27 07:15:49 -0700151 // Replaces the existing set of decoders with the given set. Returns the
152 // payload types that were reassigned or removed while doing so.
153 virtual std::vector<int> SetCodecs(
154 const std::map<int, SdpAudioFormat>& codecs);
155
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800156 // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name|
157 // is only used to populate the name field in the DecoderInfo struct in the
158 // database, and can be arbitrary (including empty). Returns kOK on success;
159 // otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160 virtual int RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800161 NetEqDecoder codec_type,
162 const std::string& name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163
kwiberg5adaf732016-10-04 09:33:27 -0700164 // Registers a decoder for the given payload type. Returns kOK on success;
165 // otherwise an error code.
166 virtual int RegisterPayload(int rtp_payload_type,
167 const SdpAudioFormat& audio_format);
168
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000169 // Registers an externally created AudioDecoder object, and associates it
170 // as a decoder of type |codec_type| with |rtp_payload_type|.
171 virtual int InsertExternal(uint8_t rtp_payload_type,
172 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800173 const std::string& codec_name,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800174 AudioDecoder* decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000175
176 // Removes the entry for |rtp_payload_type| from the database.
177 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
178 virtual int Remove(uint8_t rtp_payload_type);
179
kwiberg6b19b562016-09-20 04:02:25 -0700180 // Remove all entries.
181 virtual void RemoveAll();
182
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000183 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
184 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
185 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
186
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
188 // change of active decoder, |new_decoder| is set to true. The previous active
189 // decoder's AudioDecoder object is deleted.
190 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
191
192 // Returns the current active decoder, or NULL if no active decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700193 virtual AudioDecoder* GetActiveDecoder() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194
195 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
196 // call results in a change of active comfort noise decoder, the previous
197 // active decoder's AudioDecoder object is deleted.
198 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
199
200 // Returns the current active comfort noise decoder, or NULL if no active
201 // comfort noise decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700202 virtual ComfortNoiseDecoder* GetActiveCngDecoder() const;
203
204 // The following are utility methods: they will look up DecoderInfo through
205 // GetDecoderInfo and call the respective method on that info object, if it
206 // exists.
207
208 // Returns a pointer to the AudioDecoder object associated with
209 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
210 // object does not exist for that decoder, the object is created.
211 AudioDecoder* GetDecoder(uint8_t rtp_payload_type) const;
212
ossuf1b08da2016-09-23 02:19:43 -0700213 // Returns if |rtp_payload_type| is registered with a format named |name|.
214 bool IsType(uint8_t rtp_payload_type, const char* name) const;
215
216 // Returns if |rtp_payload_type| is registered with a format named |name|.
217 bool IsType(uint8_t rtp_payload_type, const std::string& name) const;
ossu84bc9852016-08-26 05:41:23 -0700218
219 // Returns true if |rtp_payload_type| is registered as comfort noise.
220 bool IsComfortNoise(uint8_t rtp_payload_type) const;
221
222 // Returns true if |rtp_payload_type| is registered as DTMF.
223 bool IsDtmf(uint8_t rtp_payload_type) const;
224
225 // Returns true if |rtp_payload_type| is registered as RED.
226 bool IsRed(uint8_t rtp_payload_type) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000227
228 // Returns kOK if all packets in |packet_list| carry payload types that are
229 // registered in the database. Otherwise, returns kDecoderNotFound.
ossu84bc9852016-08-26 05:41:23 -0700230 int CheckPayloadTypes(const PacketList& packet_list) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000231
232 private:
233 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
234
235 DecoderMap decoders_;
ossu97ba30e2016-04-25 07:55:58 -0700236 int active_decoder_type_;
237 int active_cng_decoder_type_;
ossu84bc9852016-08-26 05:41:23 -0700238 mutable std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_;
ossue725f7c2016-05-19 10:48:04 -0700239 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200240 const absl::optional<AudioCodecPairId> codec_pair_id_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000241
henrikg3c089d72015-09-16 05:37:44 -0700242 RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000243};
244
245} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200246#endif // MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_