blob: 01ff0c9fdb365fcccc24153352d352fbcc79ff2f [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>
henrik.lundin4cf61dd2015-12-09 06:20:58 -080015#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000017#include "webrtc/base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include "webrtc/common_types.h" // NULL
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000019#include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000020#include "webrtc/modules/audio_coding/neteq/packet.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021#include "webrtc/typedefs.h"
22
23namespace webrtc {
24
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025class DecoderDatabase {
26 public:
27 enum DatabaseReturnCodes {
28 kOK = 0,
29 kInvalidRtpPayloadType = -1,
30 kCodecNotSupported = -2,
31 kInvalidSampleRate = -3,
32 kDecoderExists = -4,
33 kDecoderNotFound = -5,
34 kInvalidPointer = -6
35 };
36
37 // Struct used to store decoder info in the database.
38 struct DecoderInfo {
henrik.lundin4cf61dd2015-12-09 06:20:58 -080039 DecoderInfo() = default;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040 DecoderInfo(NetEqDecoder ct, int fs, AudioDecoder* dec, bool ext)
henrik.lundin4cf61dd2015-12-09 06:20:58 -080041 : DecoderInfo(ct, "", fs, dec, ext) {}
42 DecoderInfo(NetEqDecoder ct,
43 const std::string& nm,
44 int fs,
45 AudioDecoder* dec,
46 bool ext)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047 : codec_type(ct),
henrik.lundin4cf61dd2015-12-09 06:20:58 -080048 name(nm),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049 fs_hz(fs),
henrik.lundin4cf61dd2015-12-09 06:20:58 -080050 rtp_sample_rate_hz(fs),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051 decoder(dec),
henrik.lundin4cf61dd2015-12-09 06:20:58 -080052 external(ext) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053 ~DecoderInfo();
54
henrik.lundin4cf61dd2015-12-09 06:20:58 -080055 NetEqDecoder codec_type = NetEqDecoder::kDecoderArbitrary;
56 std::string name;
57 int fs_hz = 8000;
58 int rtp_sample_rate_hz = 8000;
59 AudioDecoder* decoder = nullptr;
60 bool external = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 };
62
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
64 // only 7 bits).
65 static const uint8_t kRtpPayloadTypeError = 0xFF;
66
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000067 DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000069 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070
71 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000072 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073
74 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000075 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076
77 // Resets the database, erasing all registered payload types, and deleting
78 // any AudioDecoder objects that were not externally created and inserted
79 // using InsertExternal().
80 virtual void Reset();
81
henrik.lundin4cf61dd2015-12-09 06:20:58 -080082 // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name|
83 // is only used to populate the name field in the DecoderInfo struct in the
84 // database, and can be arbitrary (including empty). Returns kOK on success;
85 // otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086 virtual int RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080087 NetEqDecoder codec_type,
88 const std::string& name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089
90 // Registers an externally created AudioDecoder object, and associates it
91 // as a decoder of type |codec_type| with |rtp_payload_type|.
92 virtual int InsertExternal(uint8_t rtp_payload_type,
93 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080094 const std::string& codec_name,
95 int fs_hz,
96 AudioDecoder* decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097
98 // Removes the entry for |rtp_payload_type| from the database.
99 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
100 virtual int Remove(uint8_t rtp_payload_type);
101
102 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
103 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
104 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
105
106 // Returns one RTP payload type associated with |codec_type|, or
107 // kDecoderNotFound if no entry exists for that value. Note that one
108 // |codec_type| may be registered with several RTP payload types, and the
109 // method may return any of them.
110 virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const;
111
112 // Returns a pointer to the AudioDecoder object associated with
113 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
114 // object does not exist for that decoder, the object is created.
115 virtual AudioDecoder* GetDecoder(uint8_t rtp_payload_type);
116
117 // Returns true if |rtp_payload_type| is registered as a |codec_type|.
118 virtual bool IsType(uint8_t rtp_payload_type,
119 NetEqDecoder codec_type) const;
120
121 // Returns true if |rtp_payload_type| is registered as comfort noise.
122 virtual bool IsComfortNoise(uint8_t rtp_payload_type) const;
123
124 // Returns true if |rtp_payload_type| is registered as DTMF.
125 virtual bool IsDtmf(uint8_t rtp_payload_type) const;
126
127 // Returns true if |rtp_payload_type| is registered as RED.
128 virtual bool IsRed(uint8_t rtp_payload_type) const;
129
130 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
131 // change of active decoder, |new_decoder| is set to true. The previous active
132 // decoder's AudioDecoder object is deleted.
133 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
134
135 // Returns the current active decoder, or NULL if no active decoder exists.
136 virtual AudioDecoder* GetActiveDecoder();
137
138 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
139 // call results in a change of active comfort noise decoder, the previous
140 // active decoder's AudioDecoder object is deleted.
141 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
142
143 // Returns the current active comfort noise decoder, or NULL if no active
144 // comfort noise decoder exists.
145 virtual AudioDecoder* GetActiveCngDecoder();
146
147 // Returns kOK if all packets in |packet_list| carry payload types that are
148 // registered in the database. Otherwise, returns kDecoderNotFound.
149 virtual int CheckPayloadTypes(const PacketList& packet_list) const;
150
151 private:
152 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
153
154 DecoderMap decoders_;
155 int active_decoder_;
156 int active_cng_decoder_;
157
henrikg3c089d72015-09-16 05:37:44 -0700158 RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159};
160
161} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000162#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_