blob: 6c2ce540394a4e9b3f829b933d94dc0d17475532 [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 Bonadeid9708072019-01-25 20:26:48 +010020#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/codecs/cng/webrtc_cng.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_coding/neteq/packet.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023
24namespace webrtc {
25
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026class 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
kwiberg0fa0a972016-04-19 05:03:45 -070038 // Class that stores decoder info in the database.
39 class DecoderInfo {
40 public:
kwiberge9413062016-11-03 05:29:05 -070041 DecoderInfo(const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020042 absl::optional<AudioCodecPairId> codec_pair_id,
kwiberge9413062016-11-03 05:29:05 -070043 AudioDecoderFactory* factory,
44 const std::string& codec_name);
ossuf1b08da2016-09-23 02:19:43 -070045 explicit DecoderInfo(const SdpAudioFormat& audio_format,
Danil Chapovalovb6021232018-06-19 13:26:36 +020046 absl::optional<AudioCodecPairId> codec_pair_id,
ossuf1b08da2016-09-23 02:19:43 -070047 AudioDecoderFactory* factory = nullptr);
kwiberg0fa0a972016-04-19 05:03:45 -070048 DecoderInfo(DecoderInfo&&);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049 ~DecoderInfo();
50
kwiberg0fa0a972016-04-19 05:03:45 -070051 // Get the AudioDecoder object, creating it first if necessary.
ossu84bc9852016-08-26 05:41:23 -070052 AudioDecoder* GetDecoder() const;
kwiberg0fa0a972016-04-19 05:03:45 -070053
54 // Delete the AudioDecoder object, unless it's external. (This means we can
55 // always recreate it later if we need it.)
ossu84bc9852016-08-26 05:41:23 -070056 void DropDecoder() const { decoder_.reset(); }
kwiberg0fa0a972016-04-19 05:03:45 -070057
kwibergc0f2dcf2016-05-31 06:28:03 -070058 int SampleRateHz() const {
solenberg2779bab2016-11-17 04:45:19 -080059 if (IsDtmf()) {
60 // DTMF has a 1:1 mapping between clock rate and sample rate.
61 return audio_format_.clockrate_hz;
62 }
ossu09f15602016-08-29 03:59:05 -070063 const AudioDecoder* decoder = GetDecoder();
64 RTC_DCHECK_EQ(1, !!decoder + !!cng_decoder_);
65 return decoder ? decoder->SampleRateHz() : cng_decoder_->sample_rate_hz;
66 }
67
ossuf1b08da2016-09-23 02:19:43 -070068 const SdpAudioFormat& GetFormat() const { return audio_format_; }
kwibergc0f2dcf2016-05-31 06:28:03 -070069
ossuf1b08da2016-09-23 02:19:43 -070070 // Returns true if the decoder's format is comfort noise.
ossu9f38c212016-10-04 05:23:32 -070071 bool IsComfortNoise() const {
72 RTC_DCHECK_EQ(!!cng_decoder_, subtype_ == Subtype::kComfortNoise);
73 return subtype_ == Subtype::kComfortNoise;
74 }
ossu84bc9852016-08-26 05:41:23 -070075
ossuf1b08da2016-09-23 02:19:43 -070076 // Returns true if the decoder's format is DTMF.
Yves Gerey665174f2018-06-19 15:03:05 +020077 bool IsDtmf() const { return subtype_ == Subtype::kDtmf; }
ossu84bc9852016-08-26 05:41:23 -070078
ossuf1b08da2016-09-23 02:19:43 -070079 // Returns true if the decoder's format is RED.
Yves Gerey665174f2018-06-19 15:03:05 +020080 bool IsRed() const { return subtype_ == Subtype::kRed; }
ossu84bc9852016-08-26 05:41:23 -070081
Artem Titovd00ce742021-07-28 20:00:17 +020082 // Returns true if the decoder's format is named `name`.
ossuf1b08da2016-09-23 02:19:43 -070083 bool IsType(const char* name) const;
Artem Titovd00ce742021-07-28 20:00:17 +020084 // Returns true if the decoder's format is named `name`.
ossuf1b08da2016-09-23 02:19:43 -070085 bool IsType(const std::string& name) const;
86
kwiberge9413062016-11-03 05:29:05 -070087 const std::string& get_name() const { return name_; }
kwiberg0fa0a972016-04-19 05:03:45 -070088
89 private:
Artem Titovd00ce742021-07-28 20:00:17 +020090 // TODO(ossu): `name_` is kept here while we retain the old external
kwiberge9413062016-11-03 05:29:05 -070091 // decoder interface. Remove this once using an
92 // AudioDecoderFactory has supplanted the old functionality.
93 const std::string name_;
94
ossuf1b08da2016-09-23 02:19:43 -070095 const SdpAudioFormat audio_format_;
Danil Chapovalovb6021232018-06-19 13:26:36 +020096 const absl::optional<AudioCodecPairId> codec_pair_id_;
ossuf1b08da2016-09-23 02:19:43 -070097 AudioDecoderFactory* const factory_;
ossu84bc9852016-08-26 05:41:23 -070098 mutable std::unique_ptr<AudioDecoder> decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -070099
kwibergc0f2dcf2016-05-31 06:28:03 -0700100 // Set iff this is a comfort noise decoder.
101 struct CngDecoder {
Danil Chapovalovb6021232018-06-19 13:26:36 +0200102 static absl::optional<CngDecoder> Create(const SdpAudioFormat& format);
kwibergc0f2dcf2016-05-31 06:28:03 -0700103 int sample_rate_hz;
104 };
Danil Chapovalovb6021232018-06-19 13:26:36 +0200105 const absl::optional<CngDecoder> cng_decoder_;
ossu9f38c212016-10-04 05:23:32 -0700106
Yves Gerey665174f2018-06-19 15:03:05 +0200107 enum class Subtype : int8_t { kNormal, kComfortNoise, kDtmf, kRed };
ossu9f38c212016-10-04 05:23:32 -0700108
109 static Subtype SubtypeFromFormat(const SdpAudioFormat& format);
110
111 const Subtype subtype_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112 };
113
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
115 // only 7 bits).
116 static const uint8_t kRtpPayloadTypeError = 0xFF;
117
ossue725f7c2016-05-19 10:48:04 -0700118 DecoderDatabase(
Karl Wiberg08126342018-03-20 19:18:55 +0100119 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200120 absl::optional<AudioCodecPairId> codec_pair_id);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000122 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123
Byoungchan Lee604fd2f2022-01-21 09:49:39 +0900124 DecoderDatabase(const DecoderDatabase&) = delete;
125 DecoderDatabase& operator=(const DecoderDatabase&) = delete;
126
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000127 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000128 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129
130 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000131 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000132
133 // Resets the database, erasing all registered payload types, and deleting
134 // any AudioDecoder objects that were not externally created and inserted
135 // using InsertExternal().
136 virtual void Reset();
137
kwiberg1c07c702017-03-27 07:15:49 -0700138 // Replaces the existing set of decoders with the given set. Returns the
139 // payload types that were reassigned or removed while doing so.
140 virtual std::vector<int> SetCodecs(
141 const std::map<int, SdpAudioFormat>& codecs);
142
kwiberg5adaf732016-10-04 09:33:27 -0700143 // Registers a decoder for the given payload type. Returns kOK on success;
144 // otherwise an error code.
145 virtual int RegisterPayload(int rtp_payload_type,
146 const SdpAudioFormat& audio_format);
147
Artem Titovd00ce742021-07-28 20:00:17 +0200148 // Removes the entry for `rtp_payload_type` from the database.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000149 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
150 virtual int Remove(uint8_t rtp_payload_type);
151
kwiberg6b19b562016-09-20 04:02:25 -0700152 // Remove all entries.
153 virtual void RemoveAll();
154
Artem Titovd00ce742021-07-28 20:00:17 +0200155 // Returns a pointer to the DecoderInfo struct for `rtp_payload_type`. If
156 // no decoder is registered with that `rtp_payload_type`, NULL is returned.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000157 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
158
Artem Titovd00ce742021-07-28 20:00:17 +0200159 // Sets the active decoder to be `rtp_payload_type`. If this call results in a
160 // change of active decoder, `new_decoder` is set to true. The previous active
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161 // decoder's AudioDecoder object is deleted.
162 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
163
164 // Returns the current active decoder, or NULL if no active decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700165 virtual AudioDecoder* GetActiveDecoder() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166
Artem Titovd00ce742021-07-28 20:00:17 +0200167 // Sets the active comfort noise decoder to be `rtp_payload_type`. If this
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000168 // call results in a change of active comfort noise decoder, the previous
169 // active decoder's AudioDecoder object is deleted.
170 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
171
172 // Returns the current active comfort noise decoder, or NULL if no active
173 // comfort noise decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700174 virtual ComfortNoiseDecoder* GetActiveCngDecoder() const;
175
176 // The following are utility methods: they will look up DecoderInfo through
177 // GetDecoderInfo and call the respective method on that info object, if it
178 // exists.
179
180 // Returns a pointer to the AudioDecoder object associated with
Artem Titovd00ce742021-07-28 20:00:17 +0200181 // `rtp_payload_type`, or NULL if none is registered. If the AudioDecoder
ossu84bc9852016-08-26 05:41:23 -0700182 // object does not exist for that decoder, the object is created.
183 AudioDecoder* GetDecoder(uint8_t rtp_payload_type) const;
184
Artem Titovd00ce742021-07-28 20:00:17 +0200185 // Returns if `rtp_payload_type` is registered with a format named `name`.
ossuf1b08da2016-09-23 02:19:43 -0700186 bool IsType(uint8_t rtp_payload_type, const char* name) const;
187
Artem Titovd00ce742021-07-28 20:00:17 +0200188 // Returns if `rtp_payload_type` is registered with a format named `name`.
ossuf1b08da2016-09-23 02:19:43 -0700189 bool IsType(uint8_t rtp_payload_type, const std::string& name) const;
ossu84bc9852016-08-26 05:41:23 -0700190
Artem Titovd00ce742021-07-28 20:00:17 +0200191 // Returns true if `rtp_payload_type` is registered as comfort noise.
ossu84bc9852016-08-26 05:41:23 -0700192 bool IsComfortNoise(uint8_t rtp_payload_type) const;
193
Artem Titovd00ce742021-07-28 20:00:17 +0200194 // Returns true if `rtp_payload_type` is registered as DTMF.
ossu84bc9852016-08-26 05:41:23 -0700195 bool IsDtmf(uint8_t rtp_payload_type) const;
196
Artem Titovd00ce742021-07-28 20:00:17 +0200197 // Returns true if `rtp_payload_type` is registered as RED.
ossu84bc9852016-08-26 05:41:23 -0700198 bool IsRed(uint8_t rtp_payload_type) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000199
Artem Titovd00ce742021-07-28 20:00:17 +0200200 // Returns kOK if all packets in `packet_list` carry payload types that are
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201 // registered in the database. Otherwise, returns kDecoderNotFound.
ossu84bc9852016-08-26 05:41:23 -0700202 int CheckPayloadTypes(const PacketList& packet_list) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203
204 private:
205 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
206
207 DecoderMap decoders_;
ossu97ba30e2016-04-25 07:55:58 -0700208 int active_decoder_type_;
209 int active_cng_decoder_type_;
ossu84bc9852016-08-26 05:41:23 -0700210 mutable std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_;
ossue725f7c2016-05-19 10:48:04 -0700211 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200212 const absl::optional<AudioCodecPairId> codec_pair_id_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000213};
214
215} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200216#endif // MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_