blob: 107d2f31ff984349f5cc6c76f17a0c224af0afdc [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"
Mirko Bonadei71207422017-09-15 13:58:09 +020026#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027
28namespace webrtc {
29
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030class 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
kwiberg0fa0a972016-04-19 05:03:45 -070042 // Class that stores decoder info in the database.
43 class DecoderInfo {
44 public:
kwiberge9413062016-11-03 05:29:05 -070045 DecoderInfo(const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020046 absl::optional<AudioCodecPairId> codec_pair_id,
kwiberge9413062016-11-03 05:29:05 -070047 AudioDecoderFactory* factory,
48 const std::string& codec_name);
ossuf1b08da2016-09-23 02:19:43 -070049 explicit DecoderInfo(const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020050 absl::optional<AudioCodecPairId> codec_pair_id,
ossuf1b08da2016-09-23 02:19:43 -070051 AudioDecoderFactory* factory = nullptr);
52 explicit DecoderInfo(NetEqDecoder ct,
Danil Chapovalovb6021232018-06-19 13:26:36 +020053 absl::optional<AudioCodecPairId> codec_pair_id,
ossuf1b08da2016-09-23 02:19:43 -070054 AudioDecoderFactory* factory = nullptr);
kwiberge9413062016-11-03 05:29:05 -070055 DecoderInfo(const SdpAudioFormat& audio_format,
56 AudioDecoder* ext_dec,
57 const std::string& codec_name);
kwiberg0fa0a972016-04-19 05:03:45 -070058 DecoderInfo(DecoderInfo&&);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059 ~DecoderInfo();
60
Karl Wiberg31fbb542017-10-16 12:42:38 +020061 // Was this info object created with a specification that allows us to
62 // actually produce a decoder?
63 bool CanGetDecoder() const;
64
kwiberg0fa0a972016-04-19 05:03:45 -070065 // Get the AudioDecoder object, creating it first if necessary.
ossu84bc9852016-08-26 05:41:23 -070066 AudioDecoder* GetDecoder() const;
kwiberg0fa0a972016-04-19 05:03:45 -070067
68 // Delete the AudioDecoder object, unless it's external. (This means we can
69 // always recreate it later if we need it.)
ossu84bc9852016-08-26 05:41:23 -070070 void DropDecoder() const { decoder_.reset(); }
kwiberg0fa0a972016-04-19 05:03:45 -070071
kwibergc0f2dcf2016-05-31 06:28:03 -070072 int SampleRateHz() const {
solenberg2779bab2016-11-17 04:45:19 -080073 if (IsDtmf()) {
74 // DTMF has a 1:1 mapping between clock rate and sample rate.
75 return audio_format_.clockrate_hz;
76 }
ossu09f15602016-08-29 03:59:05 -070077 const AudioDecoder* decoder = GetDecoder();
78 RTC_DCHECK_EQ(1, !!decoder + !!cng_decoder_);
79 return decoder ? decoder->SampleRateHz() : cng_decoder_->sample_rate_hz;
80 }
81
ossuf1b08da2016-09-23 02:19:43 -070082 const SdpAudioFormat& GetFormat() const { return audio_format_; }
kwibergc0f2dcf2016-05-31 06:28:03 -070083
ossuf1b08da2016-09-23 02:19:43 -070084 // Returns true if the decoder's format is comfort noise.
ossu9f38c212016-10-04 05:23:32 -070085 bool IsComfortNoise() const {
86 RTC_DCHECK_EQ(!!cng_decoder_, subtype_ == Subtype::kComfortNoise);
87 return subtype_ == Subtype::kComfortNoise;
88 }
ossu84bc9852016-08-26 05:41:23 -070089
ossuf1b08da2016-09-23 02:19:43 -070090 // Returns true if the decoder's format is DTMF.
Yves Gerey665174f2018-06-19 15:03:05 +020091 bool IsDtmf() const { return subtype_ == Subtype::kDtmf; }
ossu84bc9852016-08-26 05:41:23 -070092
ossuf1b08da2016-09-23 02:19:43 -070093 // Returns true if the decoder's format is RED.
Yves Gerey665174f2018-06-19 15:03:05 +020094 bool IsRed() const { return subtype_ == Subtype::kRed; }
ossu84bc9852016-08-26 05:41:23 -070095
ossuf1b08da2016-09-23 02:19:43 -070096 // Returns true if the decoder's format is named |name|.
97 bool IsType(const char* name) const;
98 // Returns true if the decoder's format is named |name|.
99 bool IsType(const std::string& name) const;
100
kwiberge9413062016-11-03 05:29:05 -0700101 const std::string& get_name() const { return name_; }
kwiberg0fa0a972016-04-19 05:03:45 -0700102
103 private:
kwiberge9413062016-11-03 05:29:05 -0700104 // TODO(ossu): |name_| is kept here while we retain the old external
105 // decoder interface. Remove this once using an
106 // AudioDecoderFactory has supplanted the old functionality.
107 const std::string name_;
108
ossuf1b08da2016-09-23 02:19:43 -0700109 const SdpAudioFormat audio_format_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200110 const absl::optional<AudioCodecPairId> codec_pair_id_;
ossuf1b08da2016-09-23 02:19:43 -0700111 AudioDecoderFactory* const factory_;
ossu84bc9852016-08-26 05:41:23 -0700112 mutable std::unique_ptr<AudioDecoder> decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -0700113
114 // Set iff this is an external decoder.
kwiberg342f7402016-06-16 03:18:00 -0700115 AudioDecoder* const external_decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -0700116
117 // Set iff this is a comfort noise decoder.
118 struct CngDecoder {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200119 static absl::optional<CngDecoder> Create(const SdpAudioFormat& format);
kwibergc0f2dcf2016-05-31 06:28:03 -0700120 int sample_rate_hz;
121 };
Danil Chapovalovb6021232018-06-19 13:26:36 +0200122 const absl::optional<CngDecoder> cng_decoder_;
ossu9f38c212016-10-04 05:23:32 -0700123
Yves Gerey665174f2018-06-19 15:03:05 +0200124 enum class Subtype : int8_t { kNormal, kComfortNoise, kDtmf, kRed };
ossu9f38c212016-10-04 05:23:32 -0700125
126 static Subtype SubtypeFromFormat(const SdpAudioFormat& format);
127
128 const Subtype subtype_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129 };
130
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000131 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
132 // only 7 bits).
133 static const uint8_t kRtpPayloadTypeError = 0xFF;
134
ossue725f7c2016-05-19 10:48:04 -0700135 DecoderDatabase(
Karl Wiberg08126342018-03-20 19:18:55 +0100136 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200137 absl::optional<AudioCodecPairId> codec_pair_id);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000138
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000139 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000140
141 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000142 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000143
144 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000145 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146
147 // Resets the database, erasing all registered payload types, and deleting
148 // any AudioDecoder objects that were not externally created and inserted
149 // using InsertExternal().
150 virtual void Reset();
151
kwiberg1c07c702017-03-27 07:15:49 -0700152 // Replaces the existing set of decoders with the given set. Returns the
153 // payload types that were reassigned or removed while doing so.
154 virtual std::vector<int> SetCodecs(
155 const std::map<int, SdpAudioFormat>& codecs);
156
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800157 // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name|
158 // is only used to populate the name field in the DecoderInfo struct in the
159 // database, and can be arbitrary (including empty). Returns kOK on success;
160 // otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161 virtual int RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800162 NetEqDecoder codec_type,
163 const std::string& name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000164
kwiberg5adaf732016-10-04 09:33:27 -0700165 // Registers a decoder for the given payload type. Returns kOK on success;
166 // otherwise an error code.
167 virtual int RegisterPayload(int rtp_payload_type,
168 const SdpAudioFormat& audio_format);
169
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 // Registers an externally created AudioDecoder object, and associates it
171 // as a decoder of type |codec_type| with |rtp_payload_type|.
172 virtual int InsertExternal(uint8_t rtp_payload_type,
173 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800174 const std::string& codec_name,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800175 AudioDecoder* decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000176
177 // Removes the entry for |rtp_payload_type| from the database.
178 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
179 virtual int Remove(uint8_t rtp_payload_type);
180
kwiberg6b19b562016-09-20 04:02:25 -0700181 // Remove all entries.
182 virtual void RemoveAll();
183
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
185 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
186 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
187
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000188 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
189 // change of active decoder, |new_decoder| is set to true. The previous active
190 // decoder's AudioDecoder object is deleted.
191 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
192
193 // Returns the current active decoder, or NULL if no active decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700194 virtual AudioDecoder* GetActiveDecoder() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000195
196 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
197 // call results in a change of active comfort noise decoder, the previous
198 // active decoder's AudioDecoder object is deleted.
199 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
200
201 // Returns the current active comfort noise decoder, or NULL if no active
202 // comfort noise decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700203 virtual ComfortNoiseDecoder* GetActiveCngDecoder() const;
204
205 // The following are utility methods: they will look up DecoderInfo through
206 // GetDecoderInfo and call the respective method on that info object, if it
207 // exists.
208
209 // Returns a pointer to the AudioDecoder object associated with
210 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
211 // object does not exist for that decoder, the object is created.
212 AudioDecoder* GetDecoder(uint8_t rtp_payload_type) const;
213
ossuf1b08da2016-09-23 02:19:43 -0700214 // Returns if |rtp_payload_type| is registered with a format named |name|.
215 bool IsType(uint8_t rtp_payload_type, const char* name) const;
216
217 // Returns if |rtp_payload_type| is registered with a format named |name|.
218 bool IsType(uint8_t rtp_payload_type, const std::string& name) const;
ossu84bc9852016-08-26 05:41:23 -0700219
220 // Returns true if |rtp_payload_type| is registered as comfort noise.
221 bool IsComfortNoise(uint8_t rtp_payload_type) const;
222
223 // Returns true if |rtp_payload_type| is registered as DTMF.
224 bool IsDtmf(uint8_t rtp_payload_type) const;
225
226 // Returns true if |rtp_payload_type| is registered as RED.
227 bool IsRed(uint8_t rtp_payload_type) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000228
229 // Returns kOK if all packets in |packet_list| carry payload types that are
230 // registered in the database. Otherwise, returns kDecoderNotFound.
ossu84bc9852016-08-26 05:41:23 -0700231 int CheckPayloadTypes(const PacketList& packet_list) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000232
233 private:
234 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
235
236 DecoderMap decoders_;
ossu97ba30e2016-04-25 07:55:58 -0700237 int active_decoder_type_;
238 int active_cng_decoder_type_;
ossu84bc9852016-08-26 05:41:23 -0700239 mutable std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_;
ossue725f7c2016-05-19 10:48:04 -0700240 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200241 const absl::optional<AudioCodecPairId> codec_pair_id_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000242
henrikg3c089d72015-09-16 05:37:44 -0700243 RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000244};
245
246} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200247#endif // MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_