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