decoder_database.h 8.0 KB

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