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> |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 15 | #include <string> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 17 | #include "webrtc/base/constructormagic.h" |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ptr.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | #include "webrtc/common_types.h" // NULL |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/audio_coding/neteq/packet.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | #include "webrtc/typedefs.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 26 | class DecoderDatabase { |
| 27 | public: |
| 28 | enum DatabaseReturnCodes { |
| 29 | kOK = 0, |
| 30 | kInvalidRtpPayloadType = -1, |
| 31 | kCodecNotSupported = -2, |
| 32 | kInvalidSampleRate = -3, |
| 33 | kDecoderExists = -4, |
| 34 | kDecoderNotFound = -5, |
| 35 | kInvalidPointer = -6 |
| 36 | }; |
| 37 | |
| 38 | // Struct used to store decoder info in the database. |
| 39 | struct DecoderInfo { |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 40 | DecoderInfo() = default; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 41 | DecoderInfo(NetEqDecoder ct, int fs, AudioDecoder* dec, bool ext) |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 42 | : DecoderInfo(ct, "", fs, dec, ext) {} |
| 43 | DecoderInfo(NetEqDecoder ct, |
| 44 | const std::string& nm, |
| 45 | int fs, |
| 46 | AudioDecoder* dec, |
| 47 | bool ext) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | : codec_type(ct), |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 49 | name(nm), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 50 | fs_hz(fs), |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 51 | rtp_sample_rate_hz(fs), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | decoder(dec), |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 53 | external(ext) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | ~DecoderInfo(); |
| 55 | |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 56 | NetEqDecoder codec_type = NetEqDecoder::kDecoderArbitrary; |
| 57 | std::string name; |
| 58 | int fs_hz = 8000; |
| 59 | int rtp_sample_rate_hz = 8000; |
| 60 | AudioDecoder* decoder = nullptr; |
| 61 | bool external = false; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | // Maximum value for 8 bits, and an invalid RTP payload type (since it is |
| 65 | // only 7 bits). |
| 66 | static const uint8_t kRtpPayloadTypeError = 0xFF; |
| 67 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 68 | DecoderDatabase(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 69 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 70 | virtual ~DecoderDatabase(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 71 | |
| 72 | // Returns true if the database is empty. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 73 | virtual bool Empty() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 74 | |
| 75 | // Returns the number of decoders registered in the database. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 76 | virtual int Size() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | |
| 78 | // Resets the database, erasing all registered payload types, and deleting |
| 79 | // any AudioDecoder objects that were not externally created and inserted |
| 80 | // using InsertExternal(). |
| 81 | virtual void Reset(); |
| 82 | |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 83 | // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name| |
| 84 | // is only used to populate the name field in the DecoderInfo struct in the |
| 85 | // database, and can be arbitrary (including empty). Returns kOK on success; |
| 86 | // otherwise an error code. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 87 | virtual int RegisterPayload(uint8_t rtp_payload_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 88 | NetEqDecoder codec_type, |
| 89 | const std::string& name); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 90 | |
| 91 | // Registers an externally created AudioDecoder object, and associates it |
| 92 | // as a decoder of type |codec_type| with |rtp_payload_type|. |
| 93 | virtual int InsertExternal(uint8_t rtp_payload_type, |
| 94 | NetEqDecoder codec_type, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 95 | const std::string& codec_name, |
| 96 | int fs_hz, |
| 97 | AudioDecoder* decoder); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | |
| 99 | // Removes the entry for |rtp_payload_type| from the database. |
| 100 | // Returns kDecoderNotFound or kOK depending on the outcome of the operation. |
| 101 | virtual int Remove(uint8_t rtp_payload_type); |
| 102 | |
| 103 | // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If |
| 104 | // no decoder is registered with that |rtp_payload_type|, NULL is returned. |
| 105 | virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const; |
| 106 | |
| 107 | // Returns one RTP payload type associated with |codec_type|, or |
| 108 | // kDecoderNotFound if no entry exists for that value. Note that one |
| 109 | // |codec_type| may be registered with several RTP payload types, and the |
| 110 | // method may return any of them. |
| 111 | virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const; |
| 112 | |
| 113 | // Returns a pointer to the AudioDecoder object associated with |
| 114 | // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder |
| 115 | // object does not exist for that decoder, the object is created. |
| 116 | virtual AudioDecoder* GetDecoder(uint8_t rtp_payload_type); |
| 117 | |
| 118 | // Returns true if |rtp_payload_type| is registered as a |codec_type|. |
| 119 | virtual bool IsType(uint8_t rtp_payload_type, |
| 120 | NetEqDecoder codec_type) const; |
| 121 | |
| 122 | // Returns true if |rtp_payload_type| is registered as comfort noise. |
| 123 | virtual bool IsComfortNoise(uint8_t rtp_payload_type) const; |
| 124 | |
| 125 | // Returns true if |rtp_payload_type| is registered as DTMF. |
| 126 | virtual bool IsDtmf(uint8_t rtp_payload_type) const; |
| 127 | |
| 128 | // Returns true if |rtp_payload_type| is registered as RED. |
| 129 | virtual bool IsRed(uint8_t rtp_payload_type) const; |
| 130 | |
| 131 | // Sets the active decoder to be |rtp_payload_type|. If this call results in a |
| 132 | // change of active decoder, |new_decoder| is set to true. The previous active |
| 133 | // decoder's AudioDecoder object is deleted. |
| 134 | virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder); |
| 135 | |
| 136 | // Returns the current active decoder, or NULL if no active decoder exists. |
| 137 | virtual AudioDecoder* GetActiveDecoder(); |
| 138 | |
| 139 | // Sets the active comfort noise decoder to be |rtp_payload_type|. If this |
| 140 | // call results in a change of active comfort noise decoder, the previous |
| 141 | // active decoder's AudioDecoder object is deleted. |
| 142 | virtual int SetActiveCngDecoder(uint8_t rtp_payload_type); |
| 143 | |
| 144 | // Returns the current active comfort noise decoder, or NULL if no active |
| 145 | // comfort noise decoder exists. |
| 146 | virtual AudioDecoder* GetActiveCngDecoder(); |
| 147 | |
| 148 | // Returns kOK if all packets in |packet_list| carry payload types that are |
| 149 | // registered in the database. Otherwise, returns kDecoderNotFound. |
| 150 | virtual int CheckPayloadTypes(const PacketList& packet_list) const; |
| 151 | |
| 152 | private: |
| 153 | typedef std::map<uint8_t, DecoderInfo> DecoderMap; |
| 154 | |
| 155 | DecoderMap decoders_; |
| 156 | int active_decoder_; |
| 157 | int active_cng_decoder_; |
| 158 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 159 | RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // namespace webrtc |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 163 | #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_ |