Line data Source code
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 WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
12 : #define WEBRTC_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 "webrtc/base/constructormagic.h"
20 : #include "webrtc/base/optional.h"
21 : #include "webrtc/common_audio/smoothing_filter.h"
22 : #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor.h"
23 : #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h"
24 : #include "webrtc/modules/audio_coding/codecs/audio_encoder.h"
25 :
26 : namespace webrtc {
27 :
28 : class RtcEventLog;
29 :
30 : struct CodecInst;
31 :
32 : class AudioEncoderOpus final : public AudioEncoder {
33 : public:
34 : enum ApplicationMode {
35 : kVoip = 0,
36 : kAudio = 1,
37 : };
38 :
39 0 : struct Config {
40 : Config();
41 : Config(const Config&);
42 : ~Config();
43 : Config& operator=(const Config&);
44 :
45 : bool IsOk() const;
46 : int GetBitrateBps() const;
47 : // Returns empty if the current bitrate falls within the hysteresis window,
48 : // defined by complexity_threshold_bps +/- complexity_threshold_window_bps.
49 : // Otherwise, returns the current complexity depending on whether the
50 : // current bitrate is above or below complexity_threshold_bps.
51 : rtc::Optional<int> GetNewComplexity() const;
52 :
53 : int frame_size_ms = 20;
54 : size_t num_channels = 1;
55 : int payload_type = 120;
56 : ApplicationMode application = kVoip;
57 : rtc::Optional<int> bitrate_bps; // Unset means to use default value.
58 : bool fec_enabled = false;
59 : int max_playback_rate_hz = 48000;
60 : int complexity = kDefaultComplexity;
61 : // This value may change in the struct's constructor.
62 : int low_rate_complexity = kDefaultComplexity;
63 : // low_rate_complexity is used when the bitrate is below this threshold.
64 : int complexity_threshold_bps = 12500;
65 : int complexity_threshold_window_bps = 1500;
66 : bool dtx_enabled = false;
67 : std::vector<int> supported_frame_lengths_ms;
68 0 : const Clock* clock = Clock::GetRealTimeClock();
69 : int uplink_bandwidth_update_interval_ms = 200;
70 :
71 : private:
72 : #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM)
73 : // If we are on Android, iOS and/or ARM, use a lower complexity setting as
74 : // default, to save encoder complexity.
75 : static const int kDefaultComplexity = 5;
76 : #else
77 : static const int kDefaultComplexity = 9;
78 : #endif
79 : };
80 :
81 : using AudioNetworkAdaptorCreator =
82 : std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&,
83 : RtcEventLog*,
84 : const Clock*)>;
85 : AudioEncoderOpus(
86 : const Config& config,
87 : AudioNetworkAdaptorCreator&& audio_network_adaptor_creator = nullptr,
88 : std::unique_ptr<SmoothingFilter> bitrate_smoother = nullptr);
89 :
90 : explicit AudioEncoderOpus(const CodecInst& codec_inst);
91 :
92 : ~AudioEncoderOpus() override;
93 :
94 : int SampleRateHz() const override;
95 : size_t NumChannels() const override;
96 : size_t Num10MsFramesInNextPacket() const override;
97 : size_t Max10MsFramesInAPacket() const override;
98 : int GetTargetBitrate() const override;
99 :
100 : void Reset() override;
101 : bool SetFec(bool enable) override;
102 :
103 : // Set Opus DTX. Once enabled, Opus stops transmission, when it detects voice
104 : // being inactive. During that, it still sends 2 packets (one for content, one
105 : // for signaling) about every 400 ms.
106 : bool SetDtx(bool enable) override;
107 : bool GetDtx() const override;
108 :
109 : bool SetApplication(Application application) override;
110 : void SetMaxPlaybackRate(int frequency_hz) override;
111 : bool EnableAudioNetworkAdaptor(const std::string& config_string,
112 : RtcEventLog* event_log,
113 : const Clock* clock) override;
114 : void DisableAudioNetworkAdaptor() override;
115 : void OnReceivedUplinkPacketLossFraction(
116 : float uplink_packet_loss_fraction) override;
117 : void OnReceivedUplinkBandwidth(
118 : int target_audio_bitrate_bps,
119 : rtc::Optional<int64_t> probing_interval_ms) override;
120 : void OnReceivedRtt(int rtt_ms) override;
121 : void OnReceivedOverhead(size_t overhead_bytes_per_packet) override;
122 : void SetReceiverFrameLengthRange(int min_frame_length_ms,
123 : int max_frame_length_ms) override;
124 0 : rtc::ArrayView<const int> supported_frame_lengths_ms() const {
125 0 : return config_.supported_frame_lengths_ms;
126 : }
127 :
128 : // Getters for testing.
129 : float packet_loss_rate() const { return packet_loss_rate_; }
130 : ApplicationMode application() const { return config_.application; }
131 : bool fec_enabled() const { return config_.fec_enabled; }
132 : size_t num_channels_to_encode() const { return num_channels_to_encode_; }
133 : int next_frame_length_ms() const { return next_frame_length_ms_; }
134 :
135 : protected:
136 : EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
137 : rtc::ArrayView<const int16_t> audio,
138 : rtc::Buffer* encoded) override;
139 :
140 : private:
141 : class PacketLossFractionSmoother;
142 :
143 : size_t Num10msFramesPerPacket() const;
144 : size_t SamplesPer10msFrame() const;
145 : size_t SufficientOutputBufferSize() const;
146 : bool RecreateEncoderInstance(const Config& config);
147 : void SetFrameLength(int frame_length_ms);
148 : void SetNumChannelsToEncode(size_t num_channels_to_encode);
149 : void SetProjectedPacketLossRate(float fraction);
150 :
151 : // TODO(minyue): remove "override" when we can deprecate
152 : // |AudioEncoder::SetTargetBitrate|.
153 : void SetTargetBitrate(int target_bps) override;
154 :
155 : void ApplyAudioNetworkAdaptor();
156 : std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator(
157 : const std::string& config_string,
158 : RtcEventLog* event_log,
159 : const Clock* clock) const;
160 :
161 : void MaybeUpdateUplinkBandwidth();
162 :
163 : Config config_;
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 : AudioNetworkAdaptorCreator audio_network_adaptor_creator_;
173 : std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_;
174 : rtc::Optional<size_t> overhead_bytes_per_packet_;
175 : const std::unique_ptr<SmoothingFilter> bitrate_smoother_;
176 : rtc::Optional<int64_t> bitrate_smoother_last_update_time_;
177 :
178 : RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpus);
179 : };
180 :
181 : } // namespace webrtc
182 :
183 : #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_
|