• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/types/optional.h"
20 #include "api/audio_codecs/audio_encoder.h"
21 #include "api/audio_codecs/audio_format.h"
22 #include "api/audio_codecs/opus/audio_encoder_opus_config.h"
23 #include "common_audio/smoothing_filter.h"
24 #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h"
25 #include "modules/audio_coding/codecs/opus/opus_interface.h"
26 #include "rtc_base/constructor_magic.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>(const std::string&,
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   int SampleRateHz() const override;
65   size_t NumChannels() const override;
66   int RtpTimestampRateHz() const override;
67   size_t Num10MsFramesInNextPacket() const override;
68   size_t Max10MsFramesInAPacket() const override;
69   int GetTargetBitrate() const override;
70 
71   void Reset() override;
72   bool SetFec(bool enable) override;
73 
74   // Set Opus DTX. Once enabled, Opus stops transmission, when it detects
75   // voice being inactive. During that, it still sends 2 packets (one for
76   // content, one for signaling) about every 400 ms.
77   bool SetDtx(bool enable) override;
78   bool GetDtx() const override;
79 
80   bool SetApplication(Application application) override;
81   void SetMaxPlaybackRate(int frequency_hz) override;
82   bool EnableAudioNetworkAdaptor(const std::string& config_string,
83                                  RtcEventLog* event_log) override;
84   void DisableAudioNetworkAdaptor() override;
85   void OnReceivedUplinkPacketLossFraction(
86       float uplink_packet_loss_fraction) override;
87   void OnReceivedTargetAudioBitrate(int target_audio_bitrate_bps) override;
88   void OnReceivedUplinkBandwidth(
89       int target_audio_bitrate_bps,
90       absl::optional<int64_t> bwe_period_ms) override;
91   void OnReceivedUplinkAllocation(BitrateAllocationUpdate update) override;
92   void OnReceivedRtt(int rtt_ms) override;
93   void OnReceivedOverhead(size_t overhead_bytes_per_packet) override;
94   void SetReceiverFrameLengthRange(int min_frame_length_ms,
95                                    int max_frame_length_ms) override;
96   ANAStats GetANAStats() const override;
97   absl::optional<std::pair<TimeDelta, TimeDelta> > GetFrameLengthRange()
98       const override;
supported_frame_lengths_ms()99   rtc::ArrayView<const int> supported_frame_lengths_ms() const {
100     return config_.supported_frame_lengths_ms;
101   }
102 
103   // Getters for testing.
packet_loss_rate()104   float packet_loss_rate() const { return packet_loss_rate_; }
application()105   AudioEncoderOpusConfig::ApplicationMode application() const {
106     return config_.application;
107   }
fec_enabled()108   bool fec_enabled() const { return config_.fec_enabled; }
num_channels_to_encode()109   size_t num_channels_to_encode() const { return num_channels_to_encode_; }
next_frame_length_ms()110   int next_frame_length_ms() const { return next_frame_length_ms_; }
111 
112  protected:
113   EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
114                          rtc::ArrayView<const int16_t> audio,
115                          rtc::Buffer* encoded) override;
116 
117  private:
118   class PacketLossFractionSmoother;
119 
120   static absl::optional<AudioEncoderOpusConfig> SdpToConfig(
121       const SdpAudioFormat& format);
122   static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs);
123   static AudioCodecInfo QueryAudioEncoder(const AudioEncoderOpusConfig& config);
124   static std::unique_ptr<AudioEncoder> MakeAudioEncoder(
125       const AudioEncoderOpusConfig&,
126       int payload_type);
127 
128   size_t Num10msFramesPerPacket() const;
129   size_t SamplesPer10msFrame() const;
130   size_t SufficientOutputBufferSize() const;
131   bool RecreateEncoderInstance(const AudioEncoderOpusConfig& config);
132   void SetFrameLength(int frame_length_ms);
133   void SetNumChannelsToEncode(size_t num_channels_to_encode);
134   void SetProjectedPacketLossRate(float fraction);
135 
136   void OnReceivedUplinkBandwidth(
137       int target_audio_bitrate_bps,
138       absl::optional<int64_t> bwe_period_ms,
139       absl::optional<int64_t> link_capacity_allocation);
140 
141   // TODO(minyue): remove "override" when we can deprecate
142   // |AudioEncoder::SetTargetBitrate|.
143   void SetTargetBitrate(int target_bps) override;
144 
145   void ApplyAudioNetworkAdaptor();
146   std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator(
147       const std::string& config_string,
148       RtcEventLog* event_log) const;
149 
150   void MaybeUpdateUplinkBandwidth();
151 
152   AudioEncoderOpusConfig config_;
153   const int payload_type_;
154   const bool send_side_bwe_with_overhead_;
155   const bool use_stable_target_for_adaptation_;
156   const bool adjust_bandwidth_;
157   bool bitrate_changed_;
158   // A multiplier for bitrates at 5 kbps and higher. The target bitrate
159   // will be multiplied by these multipliers, each multiplier is applied to a
160   // 1 kbps range.
161   std::vector<float> bitrate_multipliers_;
162   float packet_loss_rate_;
163   std::vector<int16_t> input_buffer_;
164   OpusEncInst* inst_;
165   uint32_t first_timestamp_in_buffer_;
166   size_t num_channels_to_encode_;
167   int next_frame_length_ms_;
168   int complexity_;
169   std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_;
170   const AudioNetworkAdaptorCreator audio_network_adaptor_creator_;
171   std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_;
172   absl::optional<size_t> overhead_bytes_per_packet_;
173   const std::unique_ptr<SmoothingFilter> bitrate_smoother_;
174   absl::optional<int64_t> bitrate_smoother_last_update_time_;
175 
176   friend struct AudioEncoderOpus;
177   RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpusImpl);
178 };
179 
180 }  // namespace webrtc
181 
182 #endif  // MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
183