1/*
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_NETEQ_DECODER_DATABASE_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
13
14#include <map>
15
16#include "webrtc/base/constructormagic.h"
17#include "webrtc/common_types.h"  // NULL
18#include "webrtc/modules/audio_coding/neteq/interface/audio_decoder.h"
19#include "webrtc/modules/audio_coding/neteq/packet.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
70  virtual ~DecoderDatabase();
71
72  // Returns true if the database is empty.
73  virtual bool Empty() const;
74
75  // Returns the number of decoders registered in the database.
76  virtual int Size() const;
77
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
83  // Registers |rtp_payload_type| as a decoder of type |codec_type|. Returns
84  // kOK on success; otherwise an error code.
85  virtual int RegisterPayload(uint8_t rtp_payload_type,
86                              NetEqDecoder codec_type);
87
88  // Registers an externally created AudioDecoder object, and associates it
89  // as a decoder of type |codec_type| with |rtp_payload_type|.
90  virtual int InsertExternal(uint8_t rtp_payload_type,
91                             NetEqDecoder codec_type,
92                             int fs_hz, AudioDecoder* decoder);
93
94  // Removes the entry for |rtp_payload_type| from the database.
95  // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
96  virtual int Remove(uint8_t rtp_payload_type);
97
98  // Returns a pointer to the DecoderInfo struct for |rtp_payload_type|. If
99  // no decoder is registered with that |rtp_payload_type|, NULL is returned.
100  virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
101
102  // Returns one RTP payload type associated with |codec_type|, or
103  // kDecoderNotFound if no entry exists for that value. Note that one
104  // |codec_type| may be registered with several RTP payload types, and the
105  // method may return any of them.
106  virtual uint8_t GetRtpPayloadType(NetEqDecoder codec_type) const;
107
108  // Returns a pointer to the AudioDecoder object associated with
109  // |rtp_payload_type|, or NULL if none is registered. If the AudioDecoder
110  // object does not exist for that decoder, the object is created.
111  virtual AudioDecoder* GetDecoder(uint8_t rtp_payload_type);
112
113  // Returns true if |rtp_payload_type| is registered as a |codec_type|.
114  virtual bool IsType(uint8_t rtp_payload_type,
115                      NetEqDecoder codec_type) const;
116
117  // Returns true if |rtp_payload_type| is registered as comfort noise.
118  virtual bool IsComfortNoise(uint8_t rtp_payload_type) const;
119
120  // Returns true if |rtp_payload_type| is registered as DTMF.
121  virtual bool IsDtmf(uint8_t rtp_payload_type) const;
122
123  // Returns true if |rtp_payload_type| is registered as RED.
124  virtual bool IsRed(uint8_t rtp_payload_type) const;
125
126  // Sets the active decoder to be |rtp_payload_type|. If this call results in a
127  // change of active decoder, |new_decoder| is set to true. The previous active
128  // decoder's AudioDecoder object is deleted.
129  virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
130
131  // Returns the current active decoder, or NULL if no active decoder exists.
132  virtual AudioDecoder* GetActiveDecoder();
133
134  // Sets the active comfort noise decoder to be |rtp_payload_type|. If this
135  // call results in a change of active comfort noise decoder, the previous
136  // active decoder's AudioDecoder object is deleted.
137  virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
138
139  // Returns the current active comfort noise decoder, or NULL if no active
140  // comfort noise decoder exists.
141  virtual AudioDecoder* GetActiveCngDecoder();
142
143  // Returns kOK if all packets in |packet_list| carry payload types that are
144  // registered in the database. Otherwise, returns kDecoderNotFound.
145  virtual int CheckPayloadTypes(const PacketList& packet_list) const;
146
147 private:
148  typedef std::map<uint8_t, DecoderInfo> DecoderMap;
149
150  DecoderMap decoders_;
151  int active_decoder_;
152  int active_cng_decoder_;
153
154  DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
155};
156
157}  // namespace webrtc
158#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
159