blob: 203e502540003b5589c1f3c76102a83a993ef8c4 [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
11#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_DECODER_DATABASE_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_DECODER_DATABASE_H_
13
14#include <map>
15
16#include "webrtc/common_types.h" // NULL
17#include "webrtc/modules/audio_coding/neteq4/interface/audio_decoder.h"
18#include "webrtc/modules/audio_coding/neteq4/packet.h"
19#include "webrtc/system_wrappers/interface/constructor_magic.h"
20#include "webrtc/typedefs.h"
21
22namespace webrtc {
23
24// Forward declaration.
25class AudioDecoder;
26
27class DecoderDatabase {
28 public:
29 enum DatabaseReturnCodes {
30 kOK = 0,
31 kInvalidRtpPayloadType = -1,
32 kCodecNotSupported = -2,
33 kInvalidSampleRate = -3,
34 kDecoderExists = -4,
35 kDecoderNotFound = -5,
36 kInvalidPointer = -6
37 };
38
39 // Struct used to store decoder info in the database.
40 struct DecoderInfo {
41 // Constructors.
42 DecoderInfo()
43 : codec_type(kDecoderArbitrary),
44 fs_hz(8000),
45 decoder(NULL),
46 external(false) {
47 }
48 DecoderInfo(NetEqDecoder ct, int fs, AudioDecoder* dec, bool ext)
49 : codec_type(ct),
50 fs_hz(fs),
51 decoder(dec),
52 external(ext) {
53 }
54 // Destructor. (Defined in decoder_database.cc.)
55 ~DecoderInfo();
56
57 NetEqDecoder codec_type;
58 int fs_hz;
59 AudioDecoder* decoder;
60 bool external;
61 };
62
63 static const uint8_t kMaxRtpPayloadType = 0x7F; // Max for a 7-bit number.
64 // 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
68 DecoderDatabase()
69 : active_decoder_(-1),
70 active_cng_decoder_(-1) {
71 }
72
73 virtual ~DecoderDatabase() {}
74
75 // Returns true if the database is empty.
76 virtual bool Empty() const { return decoders_.empty(); }
77
78 // Returns the number of decoders registered in the database.
79 virtual int Size() const { return decoders_.size(); }
80
81 // Resets the database, erasing all registered payload types, and deleting
82 // any AudioDecoder objects that were not externally created and inserted
83 // using InsertExternal().
84 virtual void Reset();
85
86 // Registers |rtp_payload_type| as a decoder of type |codec_type|. Returns
87 // kOK on success; otherwise an error code.
88 virtual int RegisterPayload(uint8_t rtp_payload_type,
89 NetEqDecoder codec_type);
90
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,
95 int fs_hz, AudioDecoder* decoder);
96
97 // Removes the entry for |rtp_payload_type| from the database.
98 // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
99 virtual int Remove(uint8_t rtp_payload_type);
100
101 // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
102 // no decoder is registered with that |rtp_payload_type|, NULL is returned.
103 virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
104
105 // Returns one RTP payload type associated with |codec_type|, or
106 // kDecoderNotFound if no entry exists for that value. Note that one
107 // |codec_type| may be registered with several RTP payload types, and the
108 // method may return any of them.
109 virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const;
110
111 // Returns a pointer to the AudioDecoder object associated with
112 // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
113 // object does not exist for that decoder, the object is created.
114 virtual AudioDecoder* GetDecoder(uint8_t rtp_payload_type);
115
116 // Returns true if |rtp_payload_type| is registered as a |codec_type|.
117 virtual bool IsType(uint8_t rtp_payload_type,
118 NetEqDecoder codec_type) const;
119
120 // Returns true if |rtp_payload_type| is registered as comfort noise.
121 virtual bool IsComfortNoise(uint8_t rtp_payload_type) const;
122
123 // Returns true if |rtp_payload_type| is registered as DTMF.
124 virtual bool IsDtmf(uint8_t rtp_payload_type) const;
125
126 // Returns true if |rtp_payload_type| is registered as RED.
127 virtual bool IsRed(uint8_t rtp_payload_type) const;
128
129 // Sets the active decoder to be |rtp_payload_type|. If this call results in a
130 // change of active decoder, |new_decoder| is set to true. The previous active
131 // decoder's AudioDecoder object is deleted.
132 virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
133
134 // Returns the current active decoder, or NULL if no active decoder exists.
135 virtual AudioDecoder* GetActiveDecoder();
136
137 // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
138 // call results in a change of active comfort noise decoder, the previous
139 // active decoder's AudioDecoder object is deleted.
140 virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
141
142 // Returns the current active comfort noise decoder, or NULL if no active
143 // comfort noise decoder exists.
144 virtual AudioDecoder* GetActiveCngDecoder();
145
146 // Returns kOK if all packets in |packet_list| carry payload types that are
147 // registered in the database. Otherwise, returns kDecoderNotFound.
148 virtual int CheckPayloadTypes(const PacketList& packet_list) const;
149
150 private:
151 typedef std::map<uint8_t, DecoderInfo> DecoderMap;
152
153 DecoderMap decoders_;
154 int active_decoder_;
155 int active_cng_decoder_;
156
157 DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
158};
159
160} // namespace webrtc
161#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_DECODER_DATABASE_H_