tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

audio_encoder_opus.h (7487B)


      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 
     11 #ifndef MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
     12 #define MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <functional>
     17 #include <memory>
     18 #include <optional>
     19 #include <utility>
     20 #include <vector>
     21 
     22 #include "absl/strings/string_view.h"
     23 #include "api/array_view.h"
     24 #include "api/audio_codecs/audio_encoder.h"
     25 #include "api/audio_codecs/audio_format.h"
     26 #include "api/audio_codecs/opus/audio_encoder_opus_config.h"
     27 #include "api/call/bitrate_allocation.h"
     28 #include "api/environment/environment.h"
     29 #include "api/units/time_delta.h"
     30 #include "common_audio/smoothing_filter.h"
     31 #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h"
     32 #include "modules/audio_coding/codecs/opus/opus_interface.h"
     33 #include "rtc_base/buffer.h"
     34 
     35 namespace webrtc {
     36 
     37 class AudioEncoderOpusImpl final : public AudioEncoder {
     38 public:
     39  // Returns empty if the current bitrate falls within the hysteresis window,
     40  // defined by complexity_threshold_bps +/- complexity_threshold_window_bps.
     41  // Otherwise, returns the current complexity depending on whether the
     42  // current bitrate is above or below complexity_threshold_bps.
     43  static std::optional<int> GetNewComplexity(
     44      const AudioEncoderOpusConfig& config);
     45 
     46  // Returns OPUS_AUTO if the the current bitrate is above wideband threshold.
     47  // Returns empty if it is below, but bandwidth coincides with the desired one.
     48  // Otherwise returns the desired bandwidth.
     49  static std::optional<int> GetNewBandwidth(
     50      const AudioEncoderOpusConfig& config,
     51      OpusEncInst* inst);
     52 
     53  using AudioNetworkAdaptorCreator =
     54      std::function<std::unique_ptr<AudioNetworkAdaptor>(absl::string_view)>;
     55 
     56  static std::unique_ptr<AudioEncoderOpusImpl> CreateForTesting(
     57      const Environment& env,
     58      const AudioEncoderOpusConfig& config,
     59      int payload_type,
     60      const AudioNetworkAdaptorCreator& audio_network_adaptor_creator,
     61      std::unique_ptr<SmoothingFilter> bitrate_smoother);
     62 
     63  AudioEncoderOpusImpl(const Environment& env,
     64                       const AudioEncoderOpusConfig& config,
     65                       int payload_type);
     66 
     67  ~AudioEncoderOpusImpl() override;
     68 
     69  AudioEncoderOpusImpl(const AudioEncoderOpusImpl&) = delete;
     70  AudioEncoderOpusImpl& operator=(const AudioEncoderOpusImpl&) = delete;
     71 
     72  int SampleRateHz() const override;
     73  size_t NumChannels() const override;
     74  int RtpTimestampRateHz() const override;
     75  size_t Num10MsFramesInNextPacket() const override;
     76  size_t Max10MsFramesInAPacket() const override;
     77  int GetTargetBitrate() const override;
     78 
     79  void Reset() override;
     80  bool SetFec(bool enable) override;
     81 
     82  // Set Opus DTX. Once enabled, Opus stops transmission, when it detects
     83  // voice being inactive. During that, it still sends 2 packets (one for
     84  // content, one for signaling) about every 400 ms.
     85  bool SetDtx(bool enable) override;
     86  bool GetDtx() const override;
     87 
     88  bool SetApplication(Application application) override;
     89  void SetMaxPlaybackRate(int frequency_hz) override;
     90  bool EnableAudioNetworkAdaptor(absl::string_view config) override;
     91  void DisableAudioNetworkAdaptor() override;
     92  void OnReceivedUplinkPacketLossFraction(
     93      float uplink_packet_loss_fraction) override;
     94  void OnReceivedTargetAudioBitrate(int target_audio_bitrate_bps) override;
     95  void OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,
     96                                 std::optional<int64_t> bwe_period_ms) override;
     97  void OnReceivedUplinkAllocation(BitrateAllocationUpdate update) override;
     98  void OnReceivedRtt(int rtt_ms) override;
     99  void OnReceivedOverhead(size_t overhead_bytes_per_packet) override;
    100  void SetReceiverFrameLengthRange(int min_frame_length_ms,
    101                                   int max_frame_length_ms) override;
    102  ANAStats GetANAStats() const override;
    103  std::optional<std::pair<TimeDelta, TimeDelta> > GetFrameLengthRange()
    104      const override;
    105  ArrayView<const int> supported_frame_lengths_ms() const {
    106    return config_.supported_frame_lengths_ms;
    107  }
    108 
    109  // Getters for testing.
    110  float packet_loss_rate() const { return packet_loss_rate_; }
    111  AudioEncoderOpusConfig::ApplicationMode application() const {
    112    return config_.application;
    113  }
    114  bool fec_enabled() const { return config_.fec_enabled; }
    115  size_t num_channels_to_encode() const { return num_channels_to_encode_; }
    116  int next_frame_length_ms() const { return next_frame_length_ms_; }
    117 
    118 protected:
    119  EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
    120                         ArrayView<const int16_t> audio,
    121                         Buffer* encoded) override;
    122 
    123 private:
    124  class PacketLossFractionSmoother;
    125 
    126  AudioEncoderOpusImpl(
    127      const Environment& env,
    128      const AudioEncoderOpusConfig& config,
    129      int payload_type,
    130      const AudioNetworkAdaptorCreator& audio_network_adaptor_creator,
    131      std::unique_ptr<SmoothingFilter> bitrate_smoother);
    132 
    133  static std::optional<AudioEncoderOpusConfig> SdpToConfig(
    134      const SdpAudioFormat& format);
    135  static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs);
    136  static AudioCodecInfo QueryAudioEncoder(const AudioEncoderOpusConfig& config);
    137 
    138  size_t Num10msFramesPerPacket() const;
    139  size_t SamplesPer10msFrame() const;
    140  size_t SufficientOutputBufferSize() const;
    141  bool RecreateEncoderInstance(const AudioEncoderOpusConfig& config);
    142  void SetFrameLength(int frame_length_ms);
    143  void SetNumChannelsToEncode(size_t num_channels_to_encode);
    144  void SetProjectedPacketLossRate(float fraction);
    145  void OnReceivedUplinkBandwidthImpl(int target_audio_bitrate_bps,
    146                                     std::optional<int64_t> bwe_period_ms);
    147 
    148  // TODO(minyue): remove "override" when we can deprecate
    149  // `AudioEncoder::SetTargetBitrate`.
    150  void SetTargetBitrate(int target_bps) override;
    151 
    152  void ApplyAudioNetworkAdaptor();
    153  std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator(
    154      absl::string_view config_string) const;
    155 
    156  void MaybeUpdateUplinkBandwidth();
    157 
    158  const Environment env_;
    159  AudioEncoderOpusConfig config_;
    160  const int payload_type_;
    161  const bool adjust_bandwidth_;
    162  bool bitrate_changed_;
    163  // A multiplier for bitrates at 5 kbps and higher. The target bitrate
    164  // will be multiplied by these multipliers, each multiplier is applied to a
    165  // 1 kbps range.
    166  std::vector<float> bitrate_multipliers_;
    167  float packet_loss_rate_;
    168  std::vector<int16_t> input_buffer_;
    169  OpusEncInst* inst_;
    170  uint32_t first_timestamp_in_buffer_;
    171  size_t num_channels_to_encode_;
    172  int next_frame_length_ms_;
    173  int complexity_;
    174  std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_;
    175  const AudioNetworkAdaptorCreator audio_network_adaptor_creator_;
    176  std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_;
    177  std::optional<size_t> overhead_bytes_per_packet_;
    178  const std::unique_ptr<SmoothingFilter> bitrate_smoother_;
    179  std::optional<int64_t> bitrate_smoother_last_update_time_;
    180 
    181  friend struct AudioEncoderOpus;
    182 };
    183 
    184 }  // namespace webrtc
    185 
    186 #endif  // MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_