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 #include <string>
16 
17 #include "webrtc/base/constructormagic.h"
18 #include "webrtc/base/scoped_ptr.h"
19 #include "webrtc/common_types.h"  // NULL
20 #include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
21 #include "webrtc/modules/audio_coding/neteq/packet.h"
22 #include "webrtc/typedefs.h"
23 
24 namespace webrtc {
25 
26 class 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 {
40     DecoderInfo() = default;
DecoderInfoDecoderInfo41     DecoderInfo(NetEqDecoder ct, int fs, AudioDecoder* dec, bool ext)
42         : DecoderInfo(ct, "", fs, dec, ext) {}
DecoderInfoDecoderInfo43     DecoderInfo(NetEqDecoder ct,
44                 const std::string& nm,
45                 int fs,
46                 AudioDecoder* dec,
47                 bool ext)
48         : codec_type(ct),
49           name(nm),
50           fs_hz(fs),
51           rtp_sample_rate_hz(fs),
52           decoder(dec),
53           external(ext) {}
54     ~DecoderInfo();
55 
56     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;
62   };
63 
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|. 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.
87   virtual int RegisterPayload(uint8_t rtp_payload_type,
88                               NetEqDecoder codec_type,
89                               const std::string& name);
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                              const std::string& codec_name,
96                              int fs_hz,
97                              AudioDecoder* decoder);
98 
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 
159   RTC_DISALLOW_COPY_AND_ASSIGN(DecoderDatabase);
160 };
161 
162 }  // namespace webrtc
163 #endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
164