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