blob: f34904fda84a294e787e1ea5501469e2f5f31184 [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.lundin4cf61dd2015-12-09 06:20:58 -080018#include "webrtc/base/scoped_ptr.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019#include "webrtc/common_types.h" // NULL
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000020#include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000021#include "webrtc/modules/audio_coding/neteq/packet.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022#include "webrtc/typedefs.h"
23
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
38 // Struct used to store decoder info in the database.
39 struct DecoderInfo {
henrik.lundin4cf61dd2015-12-09 06:20:58 -080040 DecoderInfo() = default;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041 DecoderInfo(NetEqDecoder ct, int fs, AudioDecoder* dec, bool ext)
henrik.lundin4cf61dd2015-12-09 06:20:58 -080042 : DecoderInfo(ct, "", fs, dec, ext) {}
43 DecoderInfo(NetEqDecoder ct,
44 const std::string& nm,
45 int fs,
46 AudioDecoder* dec,
47 bool ext)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048 : codec_type(ct),
henrik.lundin4cf61dd2015-12-09 06:20:58 -080049 name(nm),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050 fs_hz(fs),
henrik.lundin4cf61dd2015-12-09 06:20:58 -080051 rtp_sample_rate_hz(fs),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052 decoder(dec),
henrik.lundin4cf61dd2015-12-09 06:20:58 -080053 external(ext) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054 ~DecoderInfo();
55
henrik.lundin4cf61dd2015-12-09 06:20:58 -080056 NetEqDecoder codec_type = NetEqDecoder::kDecoderArbitrary;
57 std::string name;
58 int fs_hz = 8000;
59 int rtp_sample_rate_hz = 8000;
60 AudioDecoder* decoder = nullptr;
61 bool external = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062 };
63
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064 // Maximum value for 8 bits, and an invalid RTP payload type (since it is
65 // only 7 bits).
66 static const uint8_t kRtpPayloadTypeError = 0xFF;
67
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000068 DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000070 virtual ~DecoderDatabase();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071
72 // Returns true if the database is empty.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000073 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074
75 // Returns the number of decoders registered in the database.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000076 virtual int Size() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077
78 // Resets the database, erasing all registered payload types, and deleting
79 // any AudioDecoder objects that were not externally created and inserted
80 // using InsertExternal().
81 virtual void Reset();
82
henrik.lundin4cf61dd2015-12-09 06:20:58 -080083 // Registers |rtp_payload_type| as a decoder of type |codec_type|. The |name|
84 // is only used to populate the name field in the DecoderInfo struct in the
85 // database, and can be arbitrary (including empty). Returns kOK on success;
86 // otherwise an error code.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 virtual int RegisterPayload(uint8_t rtp_payload_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080088 NetEqDecoder codec_type,
89 const std::string& name);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090
91 // Registers an externally created AudioDecoder object, and associates it
92 // as a decoder of type |codec_type| with |rtp_payload_type|.
93 virtual int InsertExternal(uint8_t rtp_payload_type,
94 NetEqDecoder codec_type,
henrik.lundin4cf61dd2015-12-09 06:20:58 -080095 const std::string& codec_name,
96 int fs_hz,
97 AudioDecoder* decoder);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098
99 // Removes the entry for |rtp_payload_type| from the database.
100 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
101 virtual int Remove(uint8_t rtp_payload_type);
102
103 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
104 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
105 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
106
107 // Returns one RTP payload type associated with |codec_type|, or
108 // kDecoderNotFound if no entry exists for that value. Note that one
109 // |codec_type| may be registered with several RTP payload types, and the
110 // method may return any of them.
111 virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const;
112
113 // Returns a pointer to the AudioDecoder object associated with
114 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
115 // object does not exist for that decoder, the object is created.
116 virtual AudioDecoder* GetDecoder(uint8_t rtp_payload_type);
117
118 // Returns true if |rtp_payload_type| is registered as a |codec_type|.
119 virtual bool IsType(uint8_t rtp_payload_type,
120 NetEqDecoder codec_type) const;
121
122 // Returns true if |rtp_payload_type| is registered as comfort noise.
123 virtual bool IsComfortNoise(uint8_t rtp_payload_type) const;
124
125 // Returns true if |rtp_payload_type| is registered as DTMF.
126 virtual bool IsDtmf(uint8_t rtp_payload_type) const;
127
128 // Returns true if |rtp_payload_type| is registered as RED.
129 virtual bool IsRed(uint8_t rtp_payload_type) const;
130
131 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
132 // change of active decoder, |new_decoder| is set to true. The previous active
133 // decoder's AudioDecoder object is deleted.
134 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
135
136 // Returns the current active decoder, or NULL if no active decoder exists.
137 virtual AudioDecoder* GetActiveDecoder();
138
139 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
140 // call results in a change of active comfort noise decoder, the previous
141 // active decoder's AudioDecoder object is deleted.
142 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
143
144 // Returns the current active comfort noise decoder, or NULL if no active
145 // comfort noise decoder exists.
146 virtual AudioDecoder* GetActiveCngDecoder();
147
148 // Returns kOK if all packets in |packet_list| carry payload types that are
149 // registered in the database. Otherwise, returns kDecoderNotFound.
150 virtual int CheckPayloadTypes(const PacketList& packet_list) const;
151
152 private:
153 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
154
155 DecoderMap decoders_;
156 int active_decoder_;
157 int active_cng_decoder_;
158
henrikg3c089d72015-09-16 05:37:44 -0700159 RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160};
161
162} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000163#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_