• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
12 #define MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 
18 #include "absl/strings/string_view.h"
19 #include "api/audio_codecs/audio_decoder_factory.h"
20 #include "api/audio_codecs/audio_format.h"
21 #include "api/scoped_refptr.h"
22 #include "modules/audio_coding/codecs/cng/webrtc_cng.h"
23 #include "modules/audio_coding/neteq/packet.h"
24 
25 namespace webrtc {
26 
27 class 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   // Class that stores decoder info in the database.
40   class DecoderInfo {
41    public:
42     DecoderInfo(const SdpAudioFormat& audio_format,
43                 absl::optional<AudioCodecPairId> codec_pair_id,
44                 AudioDecoderFactory* factory,
45                 absl::string_view codec_name);
46     explicit DecoderInfo(const SdpAudioFormat& audio_format,
47                          absl::optional<AudioCodecPairId> codec_pair_id,
48                          AudioDecoderFactory* factory = nullptr);
49     DecoderInfo(DecoderInfo&&);
50     ~DecoderInfo();
51 
52     // Get the AudioDecoder object, creating it first if necessary.
53     AudioDecoder* GetDecoder() const;
54 
55     // Delete the AudioDecoder object, unless it's external. (This means we can
56     // always recreate it later if we need it.)
DropDecoder()57     void DropDecoder() const { decoder_.reset(); }
58 
SampleRateHz()59     int SampleRateHz() const {
60       if (IsDtmf()) {
61         // DTMF has a 1:1 mapping between clock rate and sample rate.
62         return audio_format_.clockrate_hz;
63       }
64       const AudioDecoder* decoder = GetDecoder();
65       RTC_DCHECK_EQ(1, !!decoder + !!cng_decoder_);
66       return decoder ? decoder->SampleRateHz() : cng_decoder_->sample_rate_hz;
67     }
68 
GetFormat()69     const SdpAudioFormat& GetFormat() const { return audio_format_; }
70 
71     // Returns true if the decoder's format is comfort noise.
IsComfortNoise()72     bool IsComfortNoise() const {
73       RTC_DCHECK_EQ(!!cng_decoder_, subtype_ == Subtype::kComfortNoise);
74       return subtype_ == Subtype::kComfortNoise;
75     }
76 
77     // Returns true if the decoder's format is DTMF.
IsDtmf()78     bool IsDtmf() const { return subtype_ == Subtype::kDtmf; }
79 
80     // Returns true if the decoder's format is RED.
IsRed()81     bool IsRed() const { return subtype_ == Subtype::kRed; }
82 
83     // Returns true if the decoder's format is named `name`.
84     bool IsType(absl::string_view name) const;
85 
get_name()86     const std::string& get_name() const { return name_; }
87 
88    private:
89     // TODO(ossu): `name_` is kept here while we retain the old external
90     //             decoder interface. Remove this once using an
91     //             AudioDecoderFactory has supplanted the old functionality.
92     const std::string name_;
93 
94     const SdpAudioFormat audio_format_;
95     const absl::optional<AudioCodecPairId> codec_pair_id_;
96     AudioDecoderFactory* const factory_;
97     mutable std::unique_ptr<AudioDecoder> decoder_;
98 
99     // Set iff this is a comfort noise decoder.
100     struct CngDecoder {
101       static absl::optional<CngDecoder> Create(const SdpAudioFormat& format);
102       int sample_rate_hz;
103     };
104     const absl::optional<CngDecoder> cng_decoder_;
105 
106     enum class Subtype : int8_t { kNormal, kComfortNoise, kDtmf, kRed };
107 
108     static Subtype SubtypeFromFormat(const SdpAudioFormat& format);
109 
110     const Subtype subtype_;
111   };
112 
113   // Maximum value for 8 bits, and an invalid RTP payload type (since it is
114   // only 7 bits).
115   static const uint8_t kRtpPayloadTypeError = 0xFF;
116 
117   DecoderDatabase(
118       const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory,
119       absl::optional<AudioCodecPairId> codec_pair_id);
120 
121   virtual ~DecoderDatabase();
122 
123   DecoderDatabase(const DecoderDatabase&) = delete;
124   DecoderDatabase& operator=(const DecoderDatabase&) = delete;
125 
126   // Returns true if the database is empty.
127   virtual bool Empty() const;
128 
129   // Returns the number of decoders registered in the database.
130   virtual int Size() const;
131 
132   // Replaces the existing set of decoders with the given set. Returns the
133   // payload types that were reassigned or removed while doing so.
134   virtual std::vector<int> SetCodecs(
135       const std::map<int, SdpAudioFormat>& codecs);
136 
137   // Registers a decoder for the given payload type. Returns kOK on success;
138   // otherwise an error code.
139   virtual int RegisterPayload(int rtp_payload_type,
140                               const SdpAudioFormat& audio_format);
141 
142   // Removes the entry for `rtp_payload_type` from the database.
143   // Returns kDecoderNotFound or kOK depending on the outcome of the operation.
144   virtual int Remove(uint8_t rtp_payload_type);
145 
146   // Remove all entries.
147   virtual void RemoveAll();
148 
149   // Returns a pointer to the DecoderInfo struct for `rtp_payload_type`. If
150   // no decoder is registered with that `rtp_payload_type`, NULL is returned.
151   virtual const DecoderInfo* GetDecoderInfo(uint8_t rtp_payload_type) const;
152 
153   // Sets the active decoder to be `rtp_payload_type`. If this call results in a
154   // change of active decoder, `new_decoder` is set to true. The previous active
155   // decoder's AudioDecoder object is deleted.
156   virtual int SetActiveDecoder(uint8_t rtp_payload_type, bool* new_decoder);
157 
158   // Returns the current active decoder, or NULL if no active decoder exists.
159   virtual AudioDecoder* GetActiveDecoder() const;
160 
161   // Sets the active comfort noise decoder to be `rtp_payload_type`. If this
162   // call results in a change of active comfort noise decoder, the previous
163   // active decoder's AudioDecoder object is deleted.
164   virtual int SetActiveCngDecoder(uint8_t rtp_payload_type);
165 
166   // Returns the current active comfort noise decoder, or NULL if no active
167   // comfort noise decoder exists.
168   virtual ComfortNoiseDecoder* GetActiveCngDecoder() const;
169 
170   // The following are utility methods: they will look up DecoderInfo through
171   // GetDecoderInfo and call the respective method on that info object, if it
172   // exists.
173 
174   // Returns a pointer to the AudioDecoder object associated with
175   // `rtp_payload_type`, or NULL if none is registered. If the AudioDecoder
176   // object does not exist for that decoder, the object is created.
177   AudioDecoder* GetDecoder(uint8_t rtp_payload_type) const;
178 
179   // Returns true if `rtp_payload_type` is registered as comfort noise.
180   bool IsComfortNoise(uint8_t rtp_payload_type) const;
181 
182   // Returns true if `rtp_payload_type` is registered as DTMF.
183   bool IsDtmf(uint8_t rtp_payload_type) const;
184 
185   // Returns true if `rtp_payload_type` is registered as RED.
186   bool IsRed(uint8_t rtp_payload_type) const;
187 
188   // Returns kOK if all packets in `packet_list` carry payload types that are
189   // registered in the database. Otherwise, returns kDecoderNotFound.
190   int CheckPayloadTypes(const PacketList& packet_list) const;
191 
192  private:
193   typedef std::map<uint8_t, DecoderInfo> DecoderMap;
194 
195   DecoderMap decoders_;
196   int active_decoder_type_;
197   int active_cng_decoder_type_;
198   mutable std::unique_ptr<ComfortNoiseDecoder> active_cng_decoder_;
199   rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
200   const absl::optional<AudioCodecPairId> codec_pair_id_;
201 };
202 
203 }  // namespace webrtc
204 #endif  // MODULES_AUDIO_CODING_NETEQ_DECODER_DATABASE_H_
205