OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ |
12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ | 12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ |
13 | 13 |
14 #include <functional> | 14 #include <functional> |
15 #include <memory> | 15 #include <memory> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "webrtc/api/audio_codecs/audio_encoder.h" | 19 #include "webrtc/api/audio_codecs/audio_encoder.h" |
20 #include "webrtc/api/audio_codecs/audio_format.h" | 20 #include "webrtc/api/audio_codecs/audio_format.h" |
21 #include "webrtc/api/audio_codecs/opus/audio_encoder_opus_config.h" | |
22 #include "webrtc/base/constructormagic.h" | 21 #include "webrtc/base/constructormagic.h" |
23 #include "webrtc/base/optional.h" | 22 #include "webrtc/base/optional.h" |
24 #include "webrtc/base/protobuf_utils.h" | 23 #include "webrtc/base/protobuf_utils.h" |
25 #include "webrtc/common_audio/smoothing_filter.h" | 24 #include "webrtc/common_audio/smoothing_filter.h" |
26 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ
k_adaptor.h" | 25 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ
k_adaptor.h" |
27 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 26 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
28 | 27 |
29 namespace webrtc { | 28 namespace webrtc { |
30 | 29 |
31 class RtcEventLog; | 30 class RtcEventLog; |
32 | 31 |
33 struct CodecInst; | 32 struct CodecInst; |
34 | 33 |
35 class AudioEncoderOpusImpl final : public AudioEncoder { | 34 class AudioEncoderOpus final : public AudioEncoder { |
36 public: | 35 public: |
37 static AudioEncoderOpusConfig CreateConfig(const CodecInst& codec_inst); | 36 enum ApplicationMode { |
38 static rtc::Optional<AudioEncoderOpusConfig> SdpToConfig( | 37 kVoip = 0, |
39 const SdpAudioFormat& format); | 38 kAudio = 1, |
| 39 }; |
40 | 40 |
41 // Returns empty if the current bitrate falls within the hysteresis window, | 41 struct Config { |
42 // defined by complexity_threshold_bps +/- complexity_threshold_window_bps. | 42 Config(); |
43 // Otherwise, returns the current complexity depending on whether the current | 43 Config(const Config&); |
44 // bitrate is above or below complexity_threshold_bps. | 44 ~Config(); |
45 static rtc::Optional<int> GetNewComplexity( | 45 Config& operator=(const Config&); |
46 const AudioEncoderOpusConfig& config); | 46 |
| 47 bool IsOk() const; |
| 48 int GetBitrateBps() const; |
| 49 // Returns empty if the current bitrate falls within the hysteresis window, |
| 50 // defined by complexity_threshold_bps +/- complexity_threshold_window_bps. |
| 51 // Otherwise, returns the current complexity depending on whether the |
| 52 // current bitrate is above or below complexity_threshold_bps. |
| 53 rtc::Optional<int> GetNewComplexity() const; |
| 54 |
| 55 static constexpr int kDefaultFrameSizeMs = 20; |
| 56 int frame_size_ms = kDefaultFrameSizeMs; |
| 57 size_t num_channels = 1; |
| 58 int payload_type = 120; |
| 59 ApplicationMode application = kVoip; |
| 60 rtc::Optional<int> bitrate_bps; // Unset means to use default value. |
| 61 bool fec_enabled = false; |
| 62 bool cbr_enabled = false; |
| 63 int max_playback_rate_hz = 48000; |
| 64 int complexity = kDefaultComplexity; |
| 65 // This value may change in the struct's constructor. |
| 66 int low_rate_complexity = kDefaultComplexity; |
| 67 // low_rate_complexity is used when the bitrate is below this threshold. |
| 68 int complexity_threshold_bps = 12500; |
| 69 int complexity_threshold_window_bps = 1500; |
| 70 bool dtx_enabled = false; |
| 71 std::vector<int> supported_frame_lengths_ms; |
| 72 int uplink_bandwidth_update_interval_ms = 200; |
| 73 |
| 74 private: |
| 75 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM) |
| 76 // If we are on Android, iOS and/or ARM, use a lower complexity setting as |
| 77 // default, to save encoder complexity. |
| 78 static const int kDefaultComplexity = 5; |
| 79 #else |
| 80 static const int kDefaultComplexity = 9; |
| 81 #endif |
| 82 }; |
| 83 |
| 84 static Config CreateConfig(int payload_type, const SdpAudioFormat& format); |
| 85 static Config CreateConfig(const CodecInst& codec_inst); |
47 | 86 |
48 using AudioNetworkAdaptorCreator = | 87 using AudioNetworkAdaptorCreator = |
49 std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&, | 88 std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&, |
50 RtcEventLog*)>; | 89 RtcEventLog*)>; |
51 AudioEncoderOpusImpl( | 90 AudioEncoderOpus( |
52 const AudioEncoderOpusConfig& config, | 91 const Config& config, |
53 int payload_type, | |
54 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator = nullptr, | 92 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator = nullptr, |
55 std::unique_ptr<SmoothingFilter> bitrate_smoother = nullptr); | 93 std::unique_ptr<SmoothingFilter> bitrate_smoother = nullptr); |
56 | 94 |
57 explicit AudioEncoderOpusImpl(const CodecInst& codec_inst); | 95 explicit AudioEncoderOpus(const CodecInst& codec_inst); |
58 AudioEncoderOpusImpl(int payload_type, const SdpAudioFormat& format); | 96 AudioEncoderOpus(int payload_type, const SdpAudioFormat& format); |
59 ~AudioEncoderOpusImpl() override; | 97 ~AudioEncoderOpus() override; |
60 | 98 |
61 // Static interface for use by BuiltinAudioEncoderFactory. | 99 // Static interface for use by BuiltinAudioEncoderFactory. |
62 static constexpr const char* GetPayloadName() { return "opus"; } | 100 static constexpr const char* GetPayloadName() { return "opus"; } |
63 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | 101 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( |
64 const SdpAudioFormat& format); | 102 const SdpAudioFormat& format); |
65 | 103 |
66 int SampleRateHz() const override; | 104 int SampleRateHz() const override; |
67 size_t NumChannels() const override; | 105 size_t NumChannels() const override; |
68 size_t Num10MsFramesInNextPacket() const override; | 106 size_t Num10MsFramesInNextPacket() const override; |
69 size_t Max10MsFramesInAPacket() const override; | 107 size_t Max10MsFramesInAPacket() const override; |
(...skipping 23 matching lines...) Expand all Loading... |
93 void OnReceivedRtt(int rtt_ms) override; | 131 void OnReceivedRtt(int rtt_ms) override; |
94 void OnReceivedOverhead(size_t overhead_bytes_per_packet) override; | 132 void OnReceivedOverhead(size_t overhead_bytes_per_packet) override; |
95 void SetReceiverFrameLengthRange(int min_frame_length_ms, | 133 void SetReceiverFrameLengthRange(int min_frame_length_ms, |
96 int max_frame_length_ms) override; | 134 int max_frame_length_ms) override; |
97 rtc::ArrayView<const int> supported_frame_lengths_ms() const { | 135 rtc::ArrayView<const int> supported_frame_lengths_ms() const { |
98 return config_.supported_frame_lengths_ms; | 136 return config_.supported_frame_lengths_ms; |
99 } | 137 } |
100 | 138 |
101 // Getters for testing. | 139 // Getters for testing. |
102 float packet_loss_rate() const { return packet_loss_rate_; } | 140 float packet_loss_rate() const { return packet_loss_rate_; } |
103 AudioEncoderOpusConfig::ApplicationMode application() const { | 141 ApplicationMode application() const { return config_.application; } |
104 return config_.application; | |
105 } | |
106 bool fec_enabled() const { return config_.fec_enabled; } | 142 bool fec_enabled() const { return config_.fec_enabled; } |
107 size_t num_channels_to_encode() const { return num_channels_to_encode_; } | 143 size_t num_channels_to_encode() const { return num_channels_to_encode_; } |
108 int next_frame_length_ms() const { return next_frame_length_ms_; } | 144 int next_frame_length_ms() const { return next_frame_length_ms_; } |
109 | 145 |
110 protected: | 146 protected: |
111 EncodedInfo EncodeImpl(uint32_t rtp_timestamp, | 147 EncodedInfo EncodeImpl(uint32_t rtp_timestamp, |
112 rtc::ArrayView<const int16_t> audio, | 148 rtc::ArrayView<const int16_t> audio, |
113 rtc::Buffer* encoded) override; | 149 rtc::Buffer* encoded) override; |
114 | 150 |
115 private: | 151 private: |
116 class PacketLossFractionSmoother; | 152 class PacketLossFractionSmoother; |
117 | 153 |
118 size_t Num10msFramesPerPacket() const; | 154 size_t Num10msFramesPerPacket() const; |
119 size_t SamplesPer10msFrame() const; | 155 size_t SamplesPer10msFrame() const; |
120 size_t SufficientOutputBufferSize() const; | 156 size_t SufficientOutputBufferSize() const; |
121 bool RecreateEncoderInstance(const AudioEncoderOpusConfig& config); | 157 bool RecreateEncoderInstance(const Config& config); |
122 void SetFrameLength(int frame_length_ms); | 158 void SetFrameLength(int frame_length_ms); |
123 void SetNumChannelsToEncode(size_t num_channels_to_encode); | 159 void SetNumChannelsToEncode(size_t num_channels_to_encode); |
124 void SetProjectedPacketLossRate(float fraction); | 160 void SetProjectedPacketLossRate(float fraction); |
125 | 161 |
126 // TODO(minyue): remove "override" when we can deprecate | 162 // TODO(minyue): remove "override" when we can deprecate |
127 // |AudioEncoder::SetTargetBitrate|. | 163 // |AudioEncoder::SetTargetBitrate|. |
128 void SetTargetBitrate(int target_bps) override; | 164 void SetTargetBitrate(int target_bps) override; |
129 | 165 |
130 void ApplyAudioNetworkAdaptor(); | 166 void ApplyAudioNetworkAdaptor(); |
131 std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator( | 167 std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator( |
132 const ProtoString& config_string, | 168 const ProtoString& config_string, |
133 RtcEventLog* event_log) const; | 169 RtcEventLog* event_log) const; |
134 | 170 |
135 void MaybeUpdateUplinkBandwidth(); | 171 void MaybeUpdateUplinkBandwidth(); |
136 | 172 |
137 AudioEncoderOpusConfig config_; | 173 Config config_; |
138 const int payload_type_; | |
139 const bool send_side_bwe_with_overhead_; | 174 const bool send_side_bwe_with_overhead_; |
140 float packet_loss_rate_; | 175 float packet_loss_rate_; |
141 std::vector<int16_t> input_buffer_; | 176 std::vector<int16_t> input_buffer_; |
142 OpusEncInst* inst_; | 177 OpusEncInst* inst_; |
143 uint32_t first_timestamp_in_buffer_; | 178 uint32_t first_timestamp_in_buffer_; |
144 size_t num_channels_to_encode_; | 179 size_t num_channels_to_encode_; |
145 int next_frame_length_ms_; | 180 int next_frame_length_ms_; |
146 int complexity_; | 181 int complexity_; |
147 std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_; | 182 std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_; |
148 AudioNetworkAdaptorCreator audio_network_adaptor_creator_; | 183 AudioNetworkAdaptorCreator audio_network_adaptor_creator_; |
149 std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_; | 184 std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_; |
150 rtc::Optional<size_t> overhead_bytes_per_packet_; | 185 rtc::Optional<size_t> overhead_bytes_per_packet_; |
151 const std::unique_ptr<SmoothingFilter> bitrate_smoother_; | 186 const std::unique_ptr<SmoothingFilter> bitrate_smoother_; |
152 rtc::Optional<int64_t> bitrate_smoother_last_update_time_; | 187 rtc::Optional<int64_t> bitrate_smoother_last_update_time_; |
153 | 188 |
154 RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpusImpl); | 189 RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpus); |
155 }; | 190 }; |
156 | 191 |
157 } // namespace webrtc | 192 } // namespace webrtc |
158 | 193 |
159 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ | 194 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ |
OLD | NEW |