blob: 404f2575b29627a0ba9fe319d2d7c60e31a5bbb3 [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:
kwibergc0f2dcf2016-05-31 06:28:03 -070044 DecoderInfo(NetEqDecoder ct, const std::string& nm);
henrik.lundin4cf61dd2015-12-09 06:20:58 -080045 DecoderInfo(NetEqDecoder ct,
46 const std::string& nm,
kwiberg0fa0a972016-04-19 05:03:45 -070047 AudioDecoder* ext_dec);
48 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.
kwiberg5178ee82016-05-03 01:39:01 -070052 AudioDecoder* GetDecoder(AudioDecoderFactory* factory);
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.)
56 void DropDecoder() { decoder_.reset(); }
57
kwibergc0f2dcf2016-05-31 06:28:03 -070058 int SampleRateHz() const {
kwiberg342f7402016-06-16 03:18:00 -070059 RTC_DCHECK_EQ(1, !!decoder_ + !!external_decoder_ + !!cng_decoder_);
kwibergc0f2dcf2016-05-31 06:28:03 -070060 return decoder_ ? decoder_->SampleRateHz()
kwiberg342f7402016-06-16 03:18:00 -070061 : external_decoder_ ? external_decoder_->SampleRateHz()
62 : cng_decoder_->sample_rate_hz;
kwibergc0f2dcf2016-05-31 06:28:03 -070063 }
64
kwiberg0fa0a972016-04-19 05:03:45 -070065 const NetEqDecoder codec_type;
66 const std::string name;
kwiberg0fa0a972016-04-19 05:03:45 -070067
68 private:
kwiberg5178ee82016-05-03 01:39:01 -070069 const rtc::Optional<SdpAudioFormat> audio_format_;
kwiberg0fa0a972016-04-19 05:03:45 -070070 std::unique_ptr<AudioDecoder> decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -070071
72 // Set iff this is an external decoder.
kwiberg342f7402016-06-16 03:18:00 -070073 AudioDecoder* const external_decoder_;
kwibergc0f2dcf2016-05-31 06:28:03 -070074
75 // Set iff this is a comfort noise decoder.
76 struct CngDecoder {
77 static rtc::Optional<CngDecoder> Create(NetEqDecoder ct);
78 int sample_rate_hz;
79 };
80 const rtc::Optional<CngDecoder> cng_decoder_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 };
82
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
84 // only 7 bits).
85 static const uint8_t kRtpPayloadTypeError = 0xFF;
86
ossue725f7c2016-05-19 10:48:04 -070087 DecoderDatabase(
88 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000090 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091
92 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000093 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094
95 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000096 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097
98 // Resets the database, erasing all registered payload types, and deleting
99 // any AudioDecoder objects that were not externally created and inserted
100 // using InsertExternal().
101 virtual void Reset();
102
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800103 // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name|
104 // is only used to populate the name field in the DecoderInfo struct in the
105 // database, and can be arbitrary (including empty). Returns kOK on success;
106 // otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107 virtual int RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800108 NetEqDecoder codec_type,
109 const std::string& name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110
111 // Registers an externally created AudioDecoder object, and associates it
112 // as a decoder of type |codec_type| with |rtp_payload_type|.
113 virtual int InsertExternal(uint8_t rtp_payload_type,
114 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800115 const std::string& codec_name,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800116 AudioDecoder* decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117
118 // Removes the entry for |rtp_payload_type| from the database.
119 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
120 virtual int Remove(uint8_t rtp_payload_type);
121
122 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
123 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
124 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
125
126 // Returns one RTP payload type associated with |codec_type|, or
127 // kDecoderNotFound if no entry exists for that value. Note that one
128 // |codec_type| may be registered with several RTP payload types, and the
129 // method may return any of them.
130 virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const;
131
132 // Returns a pointer to the AudioDecoder object associated with
133 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
134 // object does not exist for that decoder, the object is created.
135 virtual AudioDecoder* GetDecoder(uint8_t rtp_payload_type);
136
137 // Returns true if |rtp_payload_type| is registered as a |codec_type|.
138 virtual bool IsType(uint8_t rtp_payload_type,
139 NetEqDecoder codec_type) const;
140
141 // Returns true if |rtp_payload_type| is registered as comfort noise.
142 virtual bool IsComfortNoise(uint8_t rtp_payload_type) const;
143
144 // Returns true if |rtp_payload_type| is registered as DTMF.
145 virtual bool IsDtmf(uint8_t rtp_payload_type) const;
146
147 // Returns true if |rtp_payload_type| is registered as RED.
148 virtual bool IsRed(uint8_t rtp_payload_type) const;
149
150 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
151 // change of active decoder, |new_decoder| is set to true. The previous active
152 // decoder's AudioDecoder object is deleted.
153 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
154
155 // Returns the current active decoder, or NULL if no active decoder exists.
156 virtual AudioDecoder* GetActiveDecoder();
157
158 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
159 // call results in a change of active comfort noise decoder, the previous
160 // active decoder's AudioDecoder object is deleted.
161 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
162
163 // Returns the current active comfort noise decoder, or NULL if no active
164 // comfort noise decoder exists.
ossu97ba30e2016-04-25 07:55:58 -0700165 virtual ComfortNoiseDecoder* GetActiveCngDecoder();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166
167 // Returns kOK if all packets in |packet_list| carry payload types that are
168 // registered in the database. Otherwise, returns kDecoderNotFound.
169 virtual int CheckPayloadTypes(const PacketList& packet_list) const;
170
171 private:
172 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
173
174 DecoderMap decoders_;
ossu97ba30e2016-04-25 07:55:58 -0700175 int active_decoder_type_;
176 int active_cng_decoder_type_;
177 std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_;
ossue725f7c2016-05-19 10:48:04 -0700178 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179
henrikg3c089d72015-09-16 05:37:44 -0700180 RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181};
182
183} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000184#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_