audio_encoder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2014 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 API_AUDIO_CODECS_AUDIO_ENCODER_H_
  11. #define API_AUDIO_CODECS_AUDIO_ENCODER_H_
  12. #include <memory>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/array_view.h"
  18. #include "api/call/bitrate_allocation.h"
  19. #include "api/units/time_delta.h"
  20. #include "rtc_base/buffer.h"
  21. #include "rtc_base/deprecation.h"
  22. namespace webrtc {
  23. class RtcEventLog;
  24. // Statistics related to Audio Network Adaptation.
  25. struct ANAStats {
  26. ANAStats();
  27. ANAStats(const ANAStats&);
  28. ~ANAStats();
  29. // Number of actions taken by the ANA bitrate controller since the start of
  30. // the call. If this value is not set, it indicates that the bitrate
  31. // controller is disabled.
  32. absl::optional<uint32_t> bitrate_action_counter;
  33. // Number of actions taken by the ANA channel controller since the start of
  34. // the call. If this value is not set, it indicates that the channel
  35. // controller is disabled.
  36. absl::optional<uint32_t> channel_action_counter;
  37. // Number of actions taken by the ANA DTX controller since the start of the
  38. // call. If this value is not set, it indicates that the DTX controller is
  39. // disabled.
  40. absl::optional<uint32_t> dtx_action_counter;
  41. // Number of actions taken by the ANA FEC controller since the start of the
  42. // call. If this value is not set, it indicates that the FEC controller is
  43. // disabled.
  44. absl::optional<uint32_t> fec_action_counter;
  45. // Number of times the ANA frame length controller decided to increase the
  46. // frame length since the start of the call. If this value is not set, it
  47. // indicates that the frame length controller is disabled.
  48. absl::optional<uint32_t> frame_length_increase_counter;
  49. // Number of times the ANA frame length controller decided to decrease the
  50. // frame length since the start of the call. If this value is not set, it
  51. // indicates that the frame length controller is disabled.
  52. absl::optional<uint32_t> frame_length_decrease_counter;
  53. // The uplink packet loss fractions as set by the ANA FEC controller. If this
  54. // value is not set, it indicates that the ANA FEC controller is not active.
  55. absl::optional<float> uplink_packet_loss_fraction;
  56. };
  57. // This is the interface class for encoders in AudioCoding module. Each codec
  58. // type must have an implementation of this class.
  59. class AudioEncoder {
  60. public:
  61. // Used for UMA logging of codec usage. The same codecs, with the
  62. // same values, must be listed in
  63. // src/tools/metrics/histograms/histograms.xml in chromium to log
  64. // correct values.
  65. enum class CodecType {
  66. kOther = 0, // Codec not specified, and/or not listed in this enum
  67. kOpus = 1,
  68. kIsac = 2,
  69. kPcmA = 3,
  70. kPcmU = 4,
  71. kG722 = 5,
  72. kIlbc = 6,
  73. // Number of histogram bins in the UMA logging of codec types. The
  74. // total number of different codecs that are logged cannot exceed this
  75. // number.
  76. kMaxLoggedAudioCodecTypes
  77. };
  78. struct EncodedInfoLeaf {
  79. size_t encoded_bytes = 0;
  80. uint32_t encoded_timestamp = 0;
  81. int payload_type = 0;
  82. bool send_even_if_empty = false;
  83. bool speech = true;
  84. CodecType encoder_type = CodecType::kOther;
  85. };
  86. // This is the main struct for auxiliary encoding information. Each encoded
  87. // packet should be accompanied by one EncodedInfo struct, containing the
  88. // total number of |encoded_bytes|, the |encoded_timestamp| and the
  89. // |payload_type|. If the packet contains redundant encodings, the |redundant|
  90. // vector will be populated with EncodedInfoLeaf structs. Each struct in the
  91. // vector represents one encoding; the order of structs in the vector is the
  92. // same as the order in which the actual payloads are written to the byte
  93. // stream. When EncoderInfoLeaf structs are present in the vector, the main
  94. // struct's |encoded_bytes| will be the sum of all the |encoded_bytes| in the
  95. // vector.
  96. struct EncodedInfo : public EncodedInfoLeaf {
  97. EncodedInfo();
  98. EncodedInfo(const EncodedInfo&);
  99. EncodedInfo(EncodedInfo&&);
  100. ~EncodedInfo();
  101. EncodedInfo& operator=(const EncodedInfo&);
  102. EncodedInfo& operator=(EncodedInfo&&);
  103. std::vector<EncodedInfoLeaf> redundant;
  104. };
  105. virtual ~AudioEncoder() = default;
  106. // Returns the input sample rate in Hz and the number of input channels.
  107. // These are constants set at instantiation time.
  108. virtual int SampleRateHz() const = 0;
  109. virtual size_t NumChannels() const = 0;
  110. // Returns the rate at which the RTP timestamps are updated. The default
  111. // implementation returns SampleRateHz().
  112. virtual int RtpTimestampRateHz() const;
  113. // Returns the number of 10 ms frames the encoder will put in the next
  114. // packet. This value may only change when Encode() outputs a packet; i.e.,
  115. // the encoder may vary the number of 10 ms frames from packet to packet, but
  116. // it must decide the length of the next packet no later than when outputting
  117. // the preceding packet.
  118. virtual size_t Num10MsFramesInNextPacket() const = 0;
  119. // Returns the maximum value that can be returned by
  120. // Num10MsFramesInNextPacket().
  121. virtual size_t Max10MsFramesInAPacket() const = 0;
  122. // Returns the current target bitrate in bits/s. The value -1 means that the
  123. // codec adapts the target automatically, and a current target cannot be
  124. // provided.
  125. virtual int GetTargetBitrate() const = 0;
  126. // Accepts one 10 ms block of input audio (i.e., SampleRateHz() / 100 *
  127. // NumChannels() samples). Multi-channel audio must be sample-interleaved.
  128. // The encoder appends zero or more bytes of output to |encoded| and returns
  129. // additional encoding information. Encode() checks some preconditions, calls
  130. // EncodeImpl() which does the actual work, and then checks some
  131. // postconditions.
  132. EncodedInfo Encode(uint32_t rtp_timestamp,
  133. rtc::ArrayView<const int16_t> audio,
  134. rtc::Buffer* encoded);
  135. // Resets the encoder to its starting state, discarding any input that has
  136. // been fed to the encoder but not yet emitted in a packet.
  137. virtual void Reset() = 0;
  138. // Enables or disables codec-internal FEC (forward error correction). Returns
  139. // true if the codec was able to comply. The default implementation returns
  140. // true when asked to disable FEC and false when asked to enable it (meaning
  141. // that FEC isn't supported).
  142. virtual bool SetFec(bool enable);
  143. // Enables or disables codec-internal VAD/DTX. Returns true if the codec was
  144. // able to comply. The default implementation returns true when asked to
  145. // disable DTX and false when asked to enable it (meaning that DTX isn't
  146. // supported).
  147. virtual bool SetDtx(bool enable);
  148. // Returns the status of codec-internal DTX. The default implementation always
  149. // returns false.
  150. virtual bool GetDtx() const;
  151. // Sets the application mode. Returns true if the codec was able to comply.
  152. // The default implementation just returns false.
  153. enum class Application { kSpeech, kAudio };
  154. virtual bool SetApplication(Application application);
  155. // Tells the encoder about the highest sample rate the decoder is expected to
  156. // use when decoding the bitstream. The encoder would typically use this
  157. // information to adjust the quality of the encoding. The default
  158. // implementation does nothing.
  159. virtual void SetMaxPlaybackRate(int frequency_hz);
  160. // This is to be deprecated. Please use |OnReceivedTargetAudioBitrate|
  161. // instead.
  162. // Tells the encoder what average bitrate we'd like it to produce. The
  163. // encoder is free to adjust or disregard the given bitrate (the default
  164. // implementation does the latter).
  165. RTC_DEPRECATED virtual void SetTargetBitrate(int target_bps);
  166. // Causes this encoder to let go of any other encoders it contains, and
  167. // returns a pointer to an array where they are stored (which is required to
  168. // live as long as this encoder). Unless the returned array is empty, you may
  169. // not call any methods on this encoder afterwards, except for the
  170. // destructor. The default implementation just returns an empty array.
  171. // NOTE: This method is subject to change. Do not call or override it.
  172. virtual rtc::ArrayView<std::unique_ptr<AudioEncoder>>
  173. ReclaimContainedEncoders();
  174. // Enables audio network adaptor. Returns true if successful.
  175. virtual bool EnableAudioNetworkAdaptor(const std::string& config_string,
  176. RtcEventLog* event_log);
  177. // Disables audio network adaptor.
  178. virtual void DisableAudioNetworkAdaptor();
  179. // Provides uplink packet loss fraction to this encoder to allow it to adapt.
  180. // |uplink_packet_loss_fraction| is in the range [0.0, 1.0].
  181. virtual void OnReceivedUplinkPacketLossFraction(
  182. float uplink_packet_loss_fraction);
  183. RTC_DEPRECATED virtual void OnReceivedUplinkRecoverablePacketLossFraction(
  184. float uplink_recoverable_packet_loss_fraction);
  185. // Provides target audio bitrate to this encoder to allow it to adapt.
  186. virtual void OnReceivedTargetAudioBitrate(int target_bps);
  187. // Provides target audio bitrate and corresponding probing interval of
  188. // the bandwidth estimator to this encoder to allow it to adapt.
  189. virtual void OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,
  190. absl::optional<int64_t> bwe_period_ms);
  191. // Provides target audio bitrate and corresponding probing interval of
  192. // the bandwidth estimator to this encoder to allow it to adapt.
  193. virtual void OnReceivedUplinkAllocation(BitrateAllocationUpdate update);
  194. // Provides RTT to this encoder to allow it to adapt.
  195. virtual void OnReceivedRtt(int rtt_ms);
  196. // Provides overhead to this encoder to adapt. The overhead is the number of
  197. // bytes that will be added to each packet the encoder generates.
  198. virtual void OnReceivedOverhead(size_t overhead_bytes_per_packet);
  199. // To allow encoder to adapt its frame length, it must be provided the frame
  200. // length range that receivers can accept.
  201. virtual void SetReceiverFrameLengthRange(int min_frame_length_ms,
  202. int max_frame_length_ms);
  203. // Get statistics related to audio network adaptation.
  204. virtual ANAStats GetANAStats() const;
  205. // The range of frame lengths that are supported or nullopt if there's no sch
  206. // information. This is used to calculated the full bitrate range, including
  207. // overhead.
  208. virtual absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
  209. const = 0;
  210. protected:
  211. // Subclasses implement this to perform the actual encoding. Called by
  212. // Encode().
  213. virtual EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
  214. rtc::ArrayView<const int16_t> audio,
  215. rtc::Buffer* encoded) = 0;
  216. };
  217. } // namespace webrtc
  218. #endif // API_AUDIO_CODECS_AUDIO_ENCODER_H_