henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 15 | #include <memory> |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 16 | #include <string> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/audio_codecs/audio_decoder_factory.h" |
| 19 | #include "api/audio_codecs/audio_format.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 20 | #include "common_types.h" // NOLINT(build/include) // NULL |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/audio_coding/codecs/cng/webrtc_cng.h" |
Karl Wiberg | 31fbb54 | 2017-10-16 12:42:38 +0200 | [diff] [blame] | 22 | #include "modules/audio_coding/neteq/neteq_decoder_enum.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_coding/neteq/packet.h" |
| 24 | #include "rtc_base/constructormagic.h" |
| 25 | #include "rtc_base/scoped_ref_ptr.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 26 | #include "typedefs.h" // NOLINT(build/include) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 30 | class DecoderDatabase { |
| 31 | public: |
| 32 | enum DatabaseReturnCodes { |
| 33 | kOK = 0, |
| 34 | kInvalidRtpPayloadType = -1, |
| 35 | kCodecNotSupported = -2, |
| 36 | kInvalidSampleRate = -3, |
| 37 | kDecoderExists = -4, |
| 38 | kDecoderNotFound = -5, |
| 39 | kInvalidPointer = -6 |
| 40 | }; |
| 41 | |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 42 | // Class that stores decoder info in the database. |
| 43 | class DecoderInfo { |
| 44 | public: |
kwiberg | e941306 | 2016-11-03 05:29:05 -0700 | [diff] [blame] | 45 | DecoderInfo(const SdpAudioFormat& audio_format, |
| 46 | AudioDecoderFactory* factory, |
| 47 | const std::string& codec_name); |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 48 | explicit DecoderInfo(const SdpAudioFormat& audio_format, |
| 49 | AudioDecoderFactory* factory = nullptr); |
| 50 | explicit DecoderInfo(NetEqDecoder ct, |
| 51 | AudioDecoderFactory* factory = nullptr); |
kwiberg | e941306 | 2016-11-03 05:29:05 -0700 | [diff] [blame] | 52 | DecoderInfo(const SdpAudioFormat& audio_format, |
| 53 | AudioDecoder* ext_dec, |
| 54 | const std::string& codec_name); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 55 | DecoderInfo(DecoderInfo&&); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | ~DecoderInfo(); |
| 57 | |
Karl Wiberg | 31fbb54 | 2017-10-16 12:42:38 +0200 | [diff] [blame] | 58 | // Was this info object created with a specification that allows us to |
| 59 | // actually produce a decoder? |
| 60 | bool CanGetDecoder() const; |
| 61 | |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 62 | // Get the AudioDecoder object, creating it first if necessary. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 63 | AudioDecoder* GetDecoder() const; |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 64 | |
| 65 | // Delete the AudioDecoder object, unless it's external. (This means we can |
| 66 | // always recreate it later if we need it.) |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 67 | void DropDecoder() const { decoder_.reset(); } |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 68 | |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 69 | int SampleRateHz() const { |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 70 | if (IsDtmf()) { |
| 71 | // DTMF has a 1:1 mapping between clock rate and sample rate. |
| 72 | return audio_format_.clockrate_hz; |
| 73 | } |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 74 | const AudioDecoder* decoder = GetDecoder(); |
| 75 | RTC_DCHECK_EQ(1, !!decoder + !!cng_decoder_); |
| 76 | return decoder ? decoder->SampleRateHz() : cng_decoder_->sample_rate_hz; |
| 77 | } |
| 78 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 79 | const SdpAudioFormat& GetFormat() const { return audio_format_; } |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 80 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 81 | // Returns true if the decoder's format is comfort noise. |
ossu | 9f38c21 | 2016-10-04 05:23:32 -0700 | [diff] [blame] | 82 | bool IsComfortNoise() const { |
| 83 | RTC_DCHECK_EQ(!!cng_decoder_, subtype_ == Subtype::kComfortNoise); |
| 84 | return subtype_ == Subtype::kComfortNoise; |
| 85 | } |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 86 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 87 | // Returns true if the decoder's format is DTMF. |
ossu | 9f38c21 | 2016-10-04 05:23:32 -0700 | [diff] [blame] | 88 | bool IsDtmf() const { |
| 89 | return subtype_ == Subtype::kDtmf; |
| 90 | } |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 91 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 92 | // Returns true if the decoder's format is RED. |
ossu | 9f38c21 | 2016-10-04 05:23:32 -0700 | [diff] [blame] | 93 | bool IsRed() const { |
| 94 | return subtype_ == Subtype::kRed; |
| 95 | } |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 96 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 97 | // Returns true if the decoder's format is named |name|. |
| 98 | bool IsType(const char* name) const; |
| 99 | // Returns true if the decoder's format is named |name|. |
| 100 | bool IsType(const std::string& name) const; |
| 101 | |
kwiberg | e941306 | 2016-11-03 05:29:05 -0700 | [diff] [blame] | 102 | const std::string& get_name() const { return name_; } |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 103 | |
| 104 | private: |
kwiberg | e941306 | 2016-11-03 05:29:05 -0700 | [diff] [blame] | 105 | // TODO(ossu): |name_| is kept here while we retain the old external |
| 106 | // decoder interface. Remove this once using an |
| 107 | // AudioDecoderFactory has supplanted the old functionality. |
| 108 | const std::string name_; |
| 109 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 110 | const SdpAudioFormat audio_format_; |
| 111 | AudioDecoderFactory* const factory_; |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 112 | mutable std::unique_ptr<AudioDecoder> decoder_; |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 113 | |
| 114 | // Set iff this is an external decoder. |
kwiberg | 342f740 | 2016-06-16 03:18:00 -0700 | [diff] [blame] | 115 | AudioDecoder* const external_decoder_; |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 116 | |
| 117 | // Set iff this is a comfort noise decoder. |
| 118 | struct CngDecoder { |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 119 | static rtc::Optional<CngDecoder> Create(const SdpAudioFormat& format); |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 120 | int sample_rate_hz; |
| 121 | }; |
| 122 | const rtc::Optional<CngDecoder> cng_decoder_; |
ossu | 9f38c21 | 2016-10-04 05:23:32 -0700 | [diff] [blame] | 123 | |
| 124 | enum class Subtype : int8_t { |
| 125 | kNormal, |
| 126 | kComfortNoise, |
| 127 | kDtmf, |
| 128 | kRed |
| 129 | }; |
| 130 | |
| 131 | static Subtype SubtypeFromFormat(const SdpAudioFormat& format); |
| 132 | |
| 133 | const Subtype subtype_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 136 | // Maximum value for 8 bits, and an invalid RTP payload type (since it is |
| 137 | // only 7 bits). |
| 138 | static const uint8_t kRtpPayloadTypeError = 0xFF; |
| 139 | |
ossu | e725f7c | 2016-05-19 10:48:04 -0700 | [diff] [blame] | 140 | DecoderDatabase( |
| 141 | const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 142 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 143 | virtual ~DecoderDatabase(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 144 | |
| 145 | // Returns true if the database is empty. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 146 | virtual bool Empty() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 147 | |
| 148 | // Returns the number of decoders registered in the database. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 149 | virtual int Size() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 150 | |
| 151 | // Resets the database, erasing all registered payload types, and deleting |
| 152 | // any AudioDecoder objects that were not externally created and inserted |
| 153 | // using InsertExternal(). |
| 154 | virtual void Reset(); |
| 155 | |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 156 | // Replaces the existing set of decoders with the given set. Returns the |
| 157 | // payload types that were reassigned or removed while doing so. |
| 158 | virtual std::vector<int> SetCodecs( |
| 159 | const std::map<int, SdpAudioFormat>& codecs); |
| 160 | |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 161 | // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name| |
| 162 | // is only used to populate the name field in the DecoderInfo struct in the |
| 163 | // database, and can be arbitrary (including empty). Returns kOK on success; |
| 164 | // otherwise an error code. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 165 | virtual int RegisterPayload(uint8_t rtp_payload_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 166 | NetEqDecoder codec_type, |
| 167 | const std::string& name); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 168 | |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 169 | // Registers a decoder for the given payload type. Returns kOK on success; |
| 170 | // otherwise an error code. |
| 171 | virtual int RegisterPayload(int rtp_payload_type, |
| 172 | const SdpAudioFormat& audio_format); |
| 173 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 174 | // Registers an externally created AudioDecoder object, and associates it |
| 175 | // as a decoder of type |codec_type| with |rtp_payload_type|. |
| 176 | virtual int InsertExternal(uint8_t rtp_payload_type, |
| 177 | NetEqDecoder codec_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 178 | const std::string& codec_name, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 179 | AudioDecoder* decoder); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 180 | |
| 181 | // Removes the entry for |rtp_payload_type| from the database. |
| 182 | // Returns kDecoderNotFound or kOK depending on the outcome of the operation. |
| 183 | virtual int Remove(uint8_t rtp_payload_type); |
| 184 | |
kwiberg | 6b19b56 | 2016-09-20 04:02:25 -0700 | [diff] [blame] | 185 | // Remove all entries. |
| 186 | virtual void RemoveAll(); |
| 187 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 188 | // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If |
| 189 | // no decoder is registered with that |rtp_payload_type|, NULL is returned. |
| 190 | virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const; |
| 191 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 192 | // Sets the active decoder to be |rtp_payload_type|. If this call results in a |
| 193 | // change of active decoder, |new_decoder| is set to true. The previous active |
| 194 | // decoder's AudioDecoder object is deleted. |
| 195 | virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder); |
| 196 | |
| 197 | // Returns the current active decoder, or NULL if no active decoder exists. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 198 | virtual AudioDecoder* GetActiveDecoder() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 199 | |
| 200 | // Sets the active comfort noise decoder to be |rtp_payload_type|. If this |
| 201 | // call results in a change of active comfort noise decoder, the previous |
| 202 | // active decoder's AudioDecoder object is deleted. |
| 203 | virtual int SetActiveCngDecoder(uint8_t rtp_payload_type); |
| 204 | |
| 205 | // Returns the current active comfort noise decoder, or NULL if no active |
| 206 | // comfort noise decoder exists. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 207 | virtual ComfortNoiseDecoder* GetActiveCngDecoder() const; |
| 208 | |
| 209 | // The following are utility methods: they will look up DecoderInfo through |
| 210 | // GetDecoderInfo and call the respective method on that info object, if it |
| 211 | // exists. |
| 212 | |
| 213 | // Returns a pointer to the AudioDecoder object associated with |
| 214 | // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder |
| 215 | // object does not exist for that decoder, the object is created. |
| 216 | AudioDecoder* GetDecoder(uint8_t rtp_payload_type) const; |
| 217 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 218 | // Returns if |rtp_payload_type| is registered with a format named |name|. |
| 219 | bool IsType(uint8_t rtp_payload_type, const char* name) const; |
| 220 | |
| 221 | // Returns if |rtp_payload_type| is registered with a format named |name|. |
| 222 | bool IsType(uint8_t rtp_payload_type, const std::string& name) const; |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 223 | |
| 224 | // Returns true if |rtp_payload_type| is registered as comfort noise. |
| 225 | bool IsComfortNoise(uint8_t rtp_payload_type) const; |
| 226 | |
| 227 | // Returns true if |rtp_payload_type| is registered as DTMF. |
| 228 | bool IsDtmf(uint8_t rtp_payload_type) const; |
| 229 | |
| 230 | // Returns true if |rtp_payload_type| is registered as RED. |
| 231 | bool IsRed(uint8_t rtp_payload_type) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 232 | |
| 233 | // Returns kOK if all packets in |packet_list| carry payload types that are |
| 234 | // registered in the database. Otherwise, returns kDecoderNotFound. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 235 | int CheckPayloadTypes(const PacketList& packet_list) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 236 | |
| 237 | private: |
| 238 | typedef std::map<uint8_t, DecoderInfo> DecoderMap; |
| 239 | |
| 240 | DecoderMap decoders_; |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 241 | int active_decoder_type_; |
| 242 | int active_cng_decoder_type_; |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 243 | mutable std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_; |
ossu | e725f7c | 2016-05-19 10:48:04 -0700 | [diff] [blame] | 244 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 245 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 246 | RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 250 | #endif // MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_ |