blob: 36248562bc483332917fc9f1ad4b0603f56ec6be [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
12#define WEBRTC_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
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000018#include "webrtc/base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019#include "webrtc/common_types.h" // NULL
kwiberg5178ee82016-05-03 01:39:01 -070020#include "webrtc/modules/audio_coding/codecs/audio_decoder_factory.h"
21#include "webrtc/modules/audio_coding/codecs/audio_format.h"
ossu97ba30e2016-04-25 07:55:58 -070022#include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000023#include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000024#include "webrtc/modules/audio_coding/neteq/packet.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025#include "webrtc/typedefs.h"
26
27namespace webrtc {
28
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029class 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
kwiberg0fa0a972016-04-19 05:03:45 -070041 // Class that stores decoder info in the database.
42 class DecoderInfo {
43 public:
ossuf1b08da2016-09-23 02:19:43 -070044 explicit DecoderInfo(const SdpAudioFormat& audio_format,
45 AudioDecoderFactory* factory = nullptr);
46 explicit DecoderInfo(NetEqDecoder ct,
47 AudioDecoderFactory* factory = nullptr);
48 DecoderInfo(const SdpAudioFormat& audio_format, AudioDecoder* ext_dec);
kwiberg0fa0a972016-04-19 05:03:45 -070049 DecoderInfo(DecoderInfo&&);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050 ~DecoderInfo();
51
kwiberg0fa0a972016-04-19 05:03:45 -070052 // Get the AudioDecoder object, creating it first if necessary.
ossu84bc9852016-08-26 05:41:23 -070053 AudioDecoder* GetDecoder() const;
kwiberg0fa0a972016-04-19 05:03:45 -070054
55 // Delete the AudioDecoder object, unless it's external. (This means we can
56 // always recreate it later if we need it.)
ossu84bc9852016-08-26 05:41:23 -070057 void DropDecoder() const { decoder_.reset(); }
kwiberg0fa0a972016-04-19 05:03:45 -070058
kwibergc0f2dcf2016-05-31 06:28:03 -070059 int SampleRateHz() const {
ossu09f15602016-08-29 03:59:05 -070060 const AudioDecoder* decoder = GetDecoder();
61 RTC_DCHECK_EQ(1, !!decoder + !!cng_decoder_);
62 return decoder ? decoder->SampleRateHz() : cng_decoder_->sample_rate_hz;
63 }
64
ossuf1b08da2016-09-23 02:19:43 -070065 const SdpAudioFormat& GetFormat() const { return audio_format_; }
kwibergc0f2dcf2016-05-31 06:28:03 -070066
ossuf1b08da2016-09-23 02:19:43 -070067 // Returns true if the decoder's format is comfort noise.
ossu84bc9852016-08-26 05:41:23 -070068 bool IsComfortNoise() const;
69
ossuf1b08da2016-09-23 02:19:43 -070070 // Returns true if the decoder's format is DTMF.
ossu84bc9852016-08-26 05:41:23 -070071 bool IsDtmf() const;
72
ossuf1b08da2016-09-23 02:19:43 -070073 // Returns true if the decoder's format is RED.
ossu84bc9852016-08-26 05:41:23 -070074 bool IsRed() const;
75
ossuf1b08da2016-09-23 02:19:43 -070076 // Returns true if the decoder's format is named |name|.
77 bool IsType(const char* name) const;
78 // Returns true if the decoder's format is named |name|.
79 bool IsType(const std::string& name) const;
80
81 // TODO(ossu): |name| is kept here while we retain the old external decoder
82 // interface. Remove this once using an AudioDecoderFactory has
83 // supplanted the old functionality.
84 std::string name;
kwiberg0fa0a972016-04-19 05:03:45 -070085
86 private:
ossuf1b08da2016-09-23 02:19:43 -070087 const SdpAudioFormat audio_format_;
88 AudioDecoderFactory* const factory_;
ossu84bc9852016-08-26 05:41:23 -070089 mutable std::unique_ptr<AudioDecoder> decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -070090
91 // Set iff this is an external decoder.
kwiberg342f7402016-06-16 03:18:00 -070092 AudioDecoder* const external_decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -070093
94 // Set iff this is a comfort noise decoder.
95 struct CngDecoder {
ossuf1b08da2016-09-23 02:19:43 -070096 static rtc::Optional<CngDecoder> Create(const SdpAudioFormat& format);
kwibergc0f2dcf2016-05-31 06:28:03 -070097 int sample_rate_hz;
98 };
99 const rtc::Optional<CngDecoder> cng_decoder_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100 };
101
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
103 // only 7 bits).
104 static const uint8_t kRtpPayloadTypeError = 0xFF;
105
ossue725f7c2016-05-19 10:48:04 -0700106 DecoderDatabase(
107 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000109 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110
111 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000112 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113
114 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000115 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116
117 // Resets the database, erasing all registered payload types, and deleting
118 // any AudioDecoder objects that were not externally created and inserted
119 // using InsertExternal().
120 virtual void Reset();
121
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800122 // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name|
123 // is only used to populate the name field in the DecoderInfo struct in the
124 // database, and can be arbitrary (including empty). Returns kOK on success;
125 // otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000126 virtual int RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800127 NetEqDecoder codec_type,
128 const std::string& name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129
130 // Registers an externally created AudioDecoder object, and associates it
131 // as a decoder of type |codec_type| with |rtp_payload_type|.
132 virtual int InsertExternal(uint8_t rtp_payload_type,
133 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800134 const std::string& codec_name,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800135 AudioDecoder* decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136
137 // Removes the entry for |rtp_payload_type| from the database.
138 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
139 virtual int Remove(uint8_t rtp_payload_type);
140
kwiberg6b19b562016-09-20 04:02:25 -0700141 // Remove all entries.
142 virtual void RemoveAll();
143
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
145 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
146 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
147
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
149 // change of active decoder, |new_decoder| is set to true. The previous active
150 // decoder's AudioDecoder object is deleted.
151 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
152
153 // Returns the current active decoder, or NULL if no active decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700154 virtual AudioDecoder* GetActiveDecoder() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155
156 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
157 // call results in a change of active comfort noise decoder, the previous
158 // active decoder's AudioDecoder object is deleted.
159 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
160
161 // Returns the current active comfort noise decoder, or NULL if no active
162 // comfort noise decoder exists.
ossu84bc9852016-08-26 05:41:23 -0700163 virtual ComfortNoiseDecoder* GetActiveCngDecoder() const;
164
165 // The following are utility methods: they will look up DecoderInfo through
166 // GetDecoderInfo and call the respective method on that info object, if it
167 // exists.
168
169 // Returns a pointer to the AudioDecoder object associated with
170 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
171 // object does not exist for that decoder, the object is created.
172 AudioDecoder* GetDecoder(uint8_t rtp_payload_type) const;
173
ossuf1b08da2016-09-23 02:19:43 -0700174 // Returns if |rtp_payload_type| is registered with a format named |name|.
175 bool IsType(uint8_t rtp_payload_type, const char* name) const;
176
177 // Returns if |rtp_payload_type| is registered with a format named |name|.
178 bool IsType(uint8_t rtp_payload_type, const std::string& name) const;
ossu84bc9852016-08-26 05:41:23 -0700179
180 // Returns true if |rtp_payload_type| is registered as comfort noise.
181 bool IsComfortNoise(uint8_t rtp_payload_type) const;
182
183 // Returns true if |rtp_payload_type| is registered as DTMF.
184 bool IsDtmf(uint8_t rtp_payload_type) const;
185
186 // Returns true if |rtp_payload_type| is registered as RED.
187 bool IsRed(uint8_t rtp_payload_type) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000188
189 // Returns kOK if all packets in |packet_list| carry payload types that are
190 // registered in the database. Otherwise, returns kDecoderNotFound.
ossu84bc9852016-08-26 05:41:23 -0700191 int CheckPayloadTypes(const PacketList& packet_list) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000192
193 private:
194 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
195
196 DecoderMap decoders_;
ossu97ba30e2016-04-25 07:55:58 -0700197 int active_decoder_type_;
198 int active_cng_decoder_type_;
ossu84bc9852016-08-26 05:41:23 -0700199 mutable std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_;
ossue725f7c2016-05-19 10:48:04 -0700200 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201
henrikg3c089d72015-09-16 05:37:44 -0700202 RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203};
204
205} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000206#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_