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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_ |
| 12 | #define WEBRTC_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 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 18 | #include "webrtc/base/constructormagic.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | #include "webrtc/common_types.h" // NULL |
kwiberg | 5178ee8 | 2016-05-03 01:39:01 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/audio_coding/codecs/audio_decoder_factory.h" |
| 21 | #include "webrtc/modules/audio_coding/codecs/audio_format.h" |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 22 | #include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 23 | #include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 24 | #include "webrtc/modules/audio_coding/neteq/packet.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 25 | #include "webrtc/typedefs.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 29 | class 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 | |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 41 | // Class that stores decoder info in the database. |
| 42 | class DecoderInfo { |
| 43 | public: |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 44 | DecoderInfo( |
| 45 | NetEqDecoder ct, |
| 46 | const std::string& nm, |
| 47 | AudioDecoderFactory* factory = nullptr); |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 48 | DecoderInfo(NetEqDecoder ct, |
| 49 | const std::string& nm, |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 50 | AudioDecoder* ext_dec); |
| 51 | DecoderInfo(DecoderInfo&&); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | ~DecoderInfo(); |
| 53 | |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 54 | // Get the AudioDecoder object, creating it first if necessary. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 55 | AudioDecoder* GetDecoder() const; |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 56 | |
| 57 | // Delete the AudioDecoder object, unless it's external. (This means we can |
| 58 | // always recreate it later if we need it.) |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 59 | void DropDecoder() const { decoder_.reset(); } |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 60 | |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 61 | int SampleRateHz() const { |
kwiberg | 342f740 | 2016-06-16 03:18:00 -0700 | [diff] [blame] | 62 | RTC_DCHECK_EQ(1, !!decoder_ + !!external_decoder_ + !!cng_decoder_); |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 63 | return decoder_ ? decoder_->SampleRateHz() |
kwiberg | 342f740 | 2016-06-16 03:18:00 -0700 | [diff] [blame] | 64 | : external_decoder_ ? external_decoder_->SampleRateHz() |
| 65 | : cng_decoder_->sample_rate_hz; |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 66 | } |
| 67 | |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 68 | // Returns true if |codec_type| is comfort noise. |
| 69 | bool IsComfortNoise() const; |
| 70 | |
| 71 | // Returns true if |codec_type| is DTMF. |
| 72 | bool IsDtmf() const; |
| 73 | |
| 74 | // Returns true if |codec_type| is RED. |
| 75 | bool IsRed() const; |
| 76 | |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 77 | const NetEqDecoder codec_type; |
| 78 | const std::string name; |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
kwiberg | 5178ee8 | 2016-05-03 01:39:01 -0700 | [diff] [blame] | 81 | const rtc::Optional<SdpAudioFormat> audio_format_; |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 82 | AudioDecoderFactory* factory_; |
| 83 | mutable std::unique_ptr<AudioDecoder> decoder_; |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 84 | |
| 85 | // Set iff this is an external decoder. |
kwiberg | 342f740 | 2016-06-16 03:18:00 -0700 | [diff] [blame] | 86 | AudioDecoder* const external_decoder_; |
kwiberg | c0f2dcf | 2016-05-31 06:28:03 -0700 | [diff] [blame] | 87 | |
| 88 | // Set iff this is a comfort noise decoder. |
| 89 | struct CngDecoder { |
| 90 | static rtc::Optional<CngDecoder> Create(NetEqDecoder ct); |
| 91 | int sample_rate_hz; |
| 92 | }; |
| 93 | const rtc::Optional<CngDecoder> cng_decoder_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 96 | // Maximum value for 8 bits, and an invalid RTP payload type (since it is |
| 97 | // only 7 bits). |
| 98 | static const uint8_t kRtpPayloadTypeError = 0xFF; |
| 99 | |
ossu | e725f7c | 2016-05-19 10:48:04 -0700 | [diff] [blame] | 100 | DecoderDatabase( |
| 101 | const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 102 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 103 | virtual ~DecoderDatabase(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 104 | |
| 105 | // Returns true if the database is empty. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 106 | virtual bool Empty() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 107 | |
| 108 | // Returns the number of decoders registered in the database. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 109 | virtual int Size() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 110 | |
| 111 | // Resets the database, erasing all registered payload types, and deleting |
| 112 | // any AudioDecoder objects that were not externally created and inserted |
| 113 | // using InsertExternal(). |
| 114 | virtual void Reset(); |
| 115 | |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 116 | // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name| |
| 117 | // is only used to populate the name field in the DecoderInfo struct in the |
| 118 | // database, and can be arbitrary (including empty). Returns kOK on success; |
| 119 | // otherwise an error code. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 120 | virtual int RegisterPayload(uint8_t rtp_payload_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 121 | NetEqDecoder codec_type, |
| 122 | const std::string& name); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 123 | |
| 124 | // Registers an externally created AudioDecoder object, and associates it |
| 125 | // as a decoder of type |codec_type| with |rtp_payload_type|. |
| 126 | virtual int InsertExternal(uint8_t rtp_payload_type, |
| 127 | NetEqDecoder codec_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 128 | const std::string& codec_name, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 129 | AudioDecoder* decoder); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 130 | |
| 131 | // Removes the entry for |rtp_payload_type| from the database. |
| 132 | // Returns kDecoderNotFound or kOK depending on the outcome of the operation. |
| 133 | virtual int Remove(uint8_t rtp_payload_type); |
| 134 | |
| 135 | // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If |
| 136 | // no decoder is registered with that |rtp_payload_type|, NULL is returned. |
| 137 | virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const; |
| 138 | |
| 139 | // Returns one RTP payload type associated with |codec_type|, or |
| 140 | // kDecoderNotFound if no entry exists for that value. Note that one |
| 141 | // |codec_type| may be registered with several RTP payload types, and the |
| 142 | // method may return any of them. |
| 143 | virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const; |
| 144 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 145 | // Sets the active decoder to be |rtp_payload_type|. If this call results in a |
| 146 | // change of active decoder, |new_decoder| is set to true. The previous active |
| 147 | // decoder's AudioDecoder object is deleted. |
| 148 | virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder); |
| 149 | |
| 150 | // Returns the current active decoder, or NULL if no active decoder exists. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 151 | virtual AudioDecoder* GetActiveDecoder() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 152 | |
| 153 | // Sets the active comfort noise decoder to be |rtp_payload_type|. If this |
| 154 | // call results in a change of active comfort noise decoder, the previous |
| 155 | // active decoder's AudioDecoder object is deleted. |
| 156 | virtual int SetActiveCngDecoder(uint8_t rtp_payload_type); |
| 157 | |
| 158 | // Returns the current active comfort noise decoder, or NULL if no active |
| 159 | // comfort noise decoder exists. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 160 | virtual ComfortNoiseDecoder* GetActiveCngDecoder() const; |
| 161 | |
| 162 | // The following are utility methods: they will look up DecoderInfo through |
| 163 | // GetDecoderInfo and call the respective method on that info object, if it |
| 164 | // exists. |
| 165 | |
| 166 | // Returns a pointer to the AudioDecoder object associated with |
| 167 | // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder |
| 168 | // object does not exist for that decoder, the object is created. |
| 169 | AudioDecoder* GetDecoder(uint8_t rtp_payload_type) const; |
| 170 | |
| 171 | // Returns true if |rtp_payload_type| is registered as a |codec_type|. |
| 172 | bool IsType(uint8_t rtp_payload_type, NetEqDecoder codec_type) const; |
| 173 | |
| 174 | // Returns true if |rtp_payload_type| is registered as comfort noise. |
| 175 | bool IsComfortNoise(uint8_t rtp_payload_type) const; |
| 176 | |
| 177 | // Returns true if |rtp_payload_type| is registered as DTMF. |
| 178 | bool IsDtmf(uint8_t rtp_payload_type) const; |
| 179 | |
| 180 | // Returns true if |rtp_payload_type| is registered as RED. |
| 181 | bool IsRed(uint8_t rtp_payload_type) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 182 | |
| 183 | // Returns kOK if all packets in |packet_list| carry payload types that are |
| 184 | // registered in the database. Otherwise, returns kDecoderNotFound. |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 185 | int CheckPayloadTypes(const PacketList& packet_list) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 186 | |
| 187 | private: |
| 188 | typedef std::map<uint8_t, DecoderInfo> DecoderMap; |
| 189 | |
| 190 | DecoderMap decoders_; |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 191 | int active_decoder_type_; |
| 192 | int active_cng_decoder_type_; |
ossu | 84bc985 | 2016-08-26 05:41:23 -0700 | [diff] [blame] | 193 | mutable std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_; |
ossu | e725f7c | 2016-05-19 10:48:04 -0700 | [diff] [blame] | 194 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 195 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 196 | RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // namespace webrtc |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 200 | #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_ |