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 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" | 11 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" |
12 | 12 |
13 #include <stdlib.h> | |
14 | |
13 #include <algorithm> | 15 #include <algorithm> |
14 #include <iterator> | 16 #include <iterator> |
15 | 17 |
18 #include "webrtc/base/arraysize.h" | |
16 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
18 #include "webrtc/base/numerics/exp_filter.h" | 21 #include "webrtc/base/numerics/exp_filter.h" |
19 #include "webrtc/base/safe_conversions.h" | 22 #include "webrtc/base/safe_conversions.h" |
23 #include "webrtc/base/string_to_number.h" | |
20 #include "webrtc/base/timeutils.h" | 24 #include "webrtc/base/timeutils.h" |
21 #include "webrtc/common_types.h" | 25 #include "webrtc/common_types.h" |
22 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" | 26 #include "webrtc/modules/audio_coding/audio_network_adaptor/audio_network_adapto r_impl.h" |
23 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " | 27 #include "webrtc/modules/audio_coding/audio_network_adaptor/controller_manager.h " |
24 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 28 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
25 #include "webrtc/system_wrappers/include/field_trial.h" | 29 #include "webrtc/system_wrappers/include/field_trial.h" |
26 | 30 |
27 namespace webrtc { | 31 namespace webrtc { |
28 | 32 |
29 namespace { | 33 namespace { |
30 | 34 |
35 // Codec parameters for Opus. | |
36 // draft-spittka-payload-rtp-opus-03 | |
37 | |
38 // Recommended bitrates: | |
39 // 8-12 kb/s for NB speech, | |
40 // 16-20 kb/s for WB speech, | |
41 // 28-40 kb/s for FB speech, | |
42 // 48-64 kb/s for FB mono music, and | |
43 // 64-128 kb/s for FB stereo music. | |
44 // The current implementation applies the following values to mono signals, | |
45 // and multiplies them by 2 for stereo. | |
46 const int kOpusBitrateNbBps = 12000; | |
47 const int kOpusBitrateWbBps = 20000; | |
48 const int kOpusBitrateFbBps = 32000; | |
49 | |
50 // Opus API allows a min bitrate of 500bps, but Opus documentation suggests | |
51 // bitrate should be in the range between 6000 and 510000. | |
the sun
2017/03/14 20:48:29
nit: "between" excludes 6000 and 510000 - but belo
kwiberg-webrtc
2017/03/15 10:09:14
Well, I'd say it *sometimes* excludes the endpoint
ossu
2017/03/15 11:26:52
I don't interpret it as excluding. Quick, pick a n
the sun
2017/03/15 11:57:40
I'd actually say "from 1 to 5". :) Use the interva
kwiberg-webrtc
2017/03/15 13:33:18
Exactly.
kwiberg-webrtc
2017/03/15 13:33:18
"[...] the bitrate should be an integer n such tha
ossu
2017/03/16 18:03:58
This has got to be the biggest bike-shed in quite
kwiberg-webrtc
2017/03/17 10:20:02
Well, fortunately I was just pulling your leg. I t
| |
52 const int kOpusMinBitrateBps = 6000; | |
53 const int kOpusMaxBitrateBps = 510000; | |
ossu
2017/03/14 20:25:11
I removed the old kMin/MaxBitrateBps and replaced
kwiberg-webrtc
2017/03/15 13:33:18
Make these five constexpr?
hlundin-webrtc
2017/03/16 12:16:31
Looks good. This is in line with the documentation
| |
54 | |
31 constexpr int kSampleRateHz = 48000; | 55 constexpr int kSampleRateHz = 48000; |
32 | 56 |
33 // Opus API allows a min bitrate of 500bps, but Opus documentation suggests | 57 // These two lists must be sorted from low to high |
34 // a minimum bitrate of 6kbps. | |
35 constexpr int kMinBitrateBps = 6000; | |
36 | |
37 constexpr int kMaxBitrateBps = 512000; | |
38 | |
39 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME | 58 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME |
40 constexpr int kSupportedFrameLengths[] = {20, 60, 120}; | 59 constexpr int kANASupportedFrameLengths[] = {20, 60, 120}; |
60 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60, 120}; | |
41 #else | 61 #else |
42 constexpr int kSupportedFrameLengths[] = {20, 60}; | 62 constexpr int kANASupportedFrameLengths[] = {20, 60}; |
63 constexpr int kOpusSupportedFrameLengths[] = {10, 20, 40, 60}; | |
43 #endif | 64 #endif |
44 | 65 |
45 // PacketLossFractionSmoother uses an exponential filter with a time constant | 66 // PacketLossFractionSmoother uses an exponential filter with a time constant |
46 // of -1.0 / ln(0.9999) = 10000 ms. | 67 // of -1.0 / ln(0.9999) = 10000 ms. |
47 constexpr float kAlphaForPacketLossFractionSmoother = 0.9999f; | 68 constexpr float kAlphaForPacketLossFractionSmoother = 0.9999f; |
48 | 69 |
49 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) { | |
50 AudioEncoderOpus::Config config; | |
51 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | |
52 config.num_channels = codec_inst.channels; | |
53 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); | |
54 config.payload_type = codec_inst.pltype; | |
55 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | |
56 : AudioEncoderOpus::kAudio; | |
57 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | |
58 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY | |
59 config.low_rate_complexity = 9; | |
60 #endif | |
61 return config; | |
62 } | |
63 | |
64 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is | 70 // Optimize the loss rate to configure Opus. Basically, optimized loss rate is |
65 // the input loss rate rounded down to various levels, because a robustly good | 71 // the input loss rate rounded down to various levels, because a robustly good |
66 // audio quality is achieved by lowering the packet loss down. | 72 // audio quality is achieved by lowering the packet loss down. |
67 // Additionally, to prevent toggling, margins are used, i.e., when jumping to | 73 // Additionally, to prevent toggling, margins are used, i.e., when jumping to |
68 // a loss rate from below, a higher threshold is used than jumping to the same | 74 // a loss rate from below, a higher threshold is used than jumping to the same |
69 // level from above. | 75 // level from above. |
70 float OptimizePacketLossRate(float new_loss_rate, float old_loss_rate) { | 76 float OptimizePacketLossRate(float new_loss_rate, float old_loss_rate) { |
71 RTC_DCHECK_GE(new_loss_rate, 0.0f); | 77 RTC_DCHECK_GE(new_loss_rate, 0.0f); |
72 RTC_DCHECK_LE(new_loss_rate, 1.0f); | 78 RTC_DCHECK_LE(new_loss_rate, 1.0f); |
73 RTC_DCHECK_GE(old_loss_rate, 0.0f); | 79 RTC_DCHECK_GE(old_loss_rate, 0.0f); |
(...skipping 20 matching lines...) Expand all Loading... | |
94 kLossRate5Margin * | 100 kLossRate5Margin * |
95 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { | 101 (kPacketLossRate5 - old_loss_rate > 0 ? 1 : -1)) { |
96 return kPacketLossRate5; | 102 return kPacketLossRate5; |
97 } else if (new_loss_rate >= kPacketLossRate1) { | 103 } else if (new_loss_rate >= kPacketLossRate1) { |
98 return kPacketLossRate1; | 104 return kPacketLossRate1; |
99 } else { | 105 } else { |
100 return 0.0f; | 106 return 0.0f; |
101 } | 107 } |
102 } | 108 } |
103 | 109 |
110 rtc::Optional<std::string> GetFormatParameter(const SdpAudioFormat& format, | |
111 const std::string& param) { | |
kwiberg-webrtc
2017/03/15 13:33:18
Change this to return a const char* that points to
ossu
2017/03/16 18:03:57
I looked at it, but it makes a number of things le
kwiberg-webrtc
2017/03/17 10:20:02
Hmm. std::string::operator== accepts const char* a
| |
112 auto it = format.parameters.find(param); | |
113 return (it == format.parameters.end()) | |
114 ? rtc::Optional<std::string>() | |
115 : rtc::Optional<std::string>(it->second); | |
116 } | |
117 | |
118 template <typename T> | |
119 rtc::Optional<T> GetFormatParameter(const SdpAudioFormat& format, | |
120 const std::string& param) { | |
121 return rtc::StringToNumber<T>(GetFormatParameter(format, param).value_or("")); | |
122 }; | |
123 | |
124 int CalculateDefaultBitrate(int max_playback_rate, int num_channels) { | |
125 int bitrate = 0; | |
kwiberg-webrtc
2017/03/15 13:33:18
Ick. Default value that's never used, mutable vari
ossu
2017/03/16 18:03:57
It's not that bad :)
kwiberg-webrtc
2017/03/17 10:20:02
Hmm.
const int bitrate =
num_channels * (
| |
126 if (max_playback_rate <= 8000) { | |
127 bitrate = kOpusBitrateNbBps * num_channels; | |
128 } else if (max_playback_rate <= 16000) { | |
129 bitrate = kOpusBitrateWbBps * num_channels; | |
130 } else { | |
131 bitrate = kOpusBitrateFbBps * num_channels; | |
132 } | |
133 RTC_DCHECK_GE(bitrate, kOpusMinBitrateBps); | |
134 RTC_DCHECK_LE(bitrate, kOpusMaxBitrateBps); | |
135 return bitrate; | |
136 } | |
137 | |
138 // Get the maxaveragebitrate parameter in string-form, so we can properly figure | |
139 // out how invalid it is and accurately log invalid values. | |
140 int CalculateBitrate(int max_playback_rate_hz, | |
141 int num_channels, | |
142 rtc::Optional<std::string> bitrate_param) { | |
kwiberg-webrtc
2017/03/15 13:33:18
Make bitrate_param a const char*?
ossu
2017/03/16 18:03:58
I've kept this as-is since it'll interface directl
kwiberg-webrtc
2017/03/17 10:20:02
Acknowledged.
| |
143 const int default_bitrate = | |
144 CalculateDefaultBitrate(max_playback_rate_hz, num_channels); | |
145 | |
146 if (bitrate_param) { | |
kwiberg-webrtc
2017/03/15 13:33:18
Invert condition + early return?
ossu
2017/03/16 18:03:58
No. I like this flow. There's only one way we can
kwiberg-webrtc
2017/03/17 10:20:02
Acknowledged.
| |
147 const auto bitrate = rtc::StringToNumber<int>(*bitrate_param); | |
148 if (bitrate) { | |
kwiberg-webrtc
2017/03/15 13:33:18
Invert condition + early return?
ossu
2017/03/16 18:03:58
No, see above.
kwiberg-webrtc
2017/03/17 10:20:02
Acknowledged.
| |
149 if (*bitrate >= kOpusMinBitrateBps && *bitrate <= kOpusMaxBitrateBps) { | |
150 return *bitrate; | |
151 } | |
152 | |
153 const int new_bitrate = (*bitrate < kOpusMinBitrateBps) | |
154 ? kOpusMinBitrateBps | |
155 : kOpusMaxBitrateBps; | |
156 LOG(LS_WARNING) << "Invalid maxaveragebitrate " << *bitrate | |
157 << " clamped to " << new_bitrate; | |
158 return new_bitrate; | |
kwiberg-webrtc
2017/03/15 13:33:18
Don't we have at least one library function to do
ossu
2017/03/16 18:03:57
None that I can find. We probably should have one
kwiberg-webrtc
2017/03/17 10:20:02
You're right, we actually don't appear to have one
| |
159 } | |
160 | |
161 LOG(LS_WARNING) << "Invalid maxaveragebitrate \"" << *bitrate_param | |
162 << "\" replaced by default bitrate " << default_bitrate; | |
163 } | |
164 | |
165 return default_bitrate; | |
166 } | |
167 | |
168 rtc::Optional<int> GetChannelCount(const SdpAudioFormat& format) { | |
169 const auto param = GetFormatParameter(format, "stereo"); | |
170 if (!param || param == "0") { | |
171 return rtc::Optional<int>(1); | |
172 } else if (param == "1") { | |
173 return rtc::Optional<int>(2); | |
174 } | |
175 return rtc::Optional<int>(); | |
176 } | |
177 | |
178 rtc::Optional<int> GetMaxPlaybackRate(const SdpAudioFormat& format) { | |
179 const auto param = GetFormatParameter(format, "maxplaybackrate"); | |
180 if (!param) { | |
181 return rtc::Optional<int>(48000); | |
182 } | |
183 const auto parsed = rtc::StringToNumber<int>(*param); | |
184 if (parsed && *parsed >= 8000) { | |
185 return rtc::Optional<int>(*parsed); | |
186 } | |
187 return rtc::Optional<int>(); | |
188 } | |
189 | |
104 } // namespace | 190 } // namespace |
105 | 191 |
192 rtc::Optional<AudioFormatInfo> AudioEncoderOpus::QueryAudioFormat( | |
193 const SdpAudioFormat& format) { | |
194 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 && | |
195 format.clockrate_hz == 48000 && format.num_channels == 2) { | |
kwiberg-webrtc
2017/03/15 13:33:18
Invert condition + early return?
ossu
2017/03/16 18:03:58
As with the other one, I prefer having the error/f
kwiberg-webrtc
2017/03/17 10:20:02
Acknowledged.
| |
196 | |
197 const rtc::Optional<int> num_channels = GetChannelCount(format); | |
198 const rtc::Optional<int> max_playback_rate = GetMaxPlaybackRate(format); | |
199 if (num_channels && max_playback_rate) { | |
200 const int bitrate = | |
201 CalculateBitrate(*max_playback_rate, *num_channels, | |
202 GetFormatParameter(format, "maxaveragebitrate")); | |
203 AudioFormatInfo info(48000, *num_channels, bitrate, kOpusMinBitrateBps, | |
204 kOpusMaxBitrateBps); | |
205 info.allow_comfort_noise = false; | |
206 info.supports_network_adaption = true; | |
207 | |
208 return rtc::Optional<AudioFormatInfo>(info); | |
209 } | |
210 } | |
211 return rtc::Optional<AudioFormatInfo>(); | |
212 } | |
213 | |
214 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( | |
215 const CodecInst& codec_inst) { | |
216 AudioEncoderOpus::Config config; | |
217 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | |
218 config.num_channels = codec_inst.channels; | |
219 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); | |
220 config.payload_type = codec_inst.pltype; | |
221 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | |
222 : AudioEncoderOpus::kAudio; | |
223 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | |
224 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY | |
225 config.low_rate_complexity = 9; | |
226 #endif | |
227 return config; | |
228 } | |
229 | |
230 AudioEncoderOpus::Config AudioEncoderOpus::CreateConfig( | |
231 int payload_type, | |
232 const SdpAudioFormat& format) { | |
233 AudioEncoderOpus::Config config; | |
234 | |
235 // Normally, the first two parameters should already have been checked buy | |
kwiberg-webrtc
2017/03/15 13:33:18
by
ossu
2017/03/16 18:03:57
Acknowledged.
| |
236 // QueryAudioFormat At this point, we might as well fall back to something | |
kwiberg-webrtc
2017/03/15 13:33:18
.
ossu
2017/03/16 18:03:58
Acknowledged.
| |
237 // reasonable. | |
238 config.num_channels = GetChannelCount(format).value_or(1); | |
239 config.max_playback_rate_hz = GetMaxPlaybackRate(format).value_or(48000); | |
240 config.fec_enabled = (GetFormatParameter(format, "useinbandfec") == "1"); | |
241 config.dtx_enabled = (GetFormatParameter(format, "usedtx") == "1"); | |
242 config.bitrate_bps = rtc::Optional<int>( | |
243 CalculateBitrate(config.max_playback_rate_hz, config.num_channels, | |
244 GetFormatParameter(format, "maxaveragebitrate"))); | |
245 config.payload_type = payload_type; | |
246 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | |
247 : AudioEncoderOpus::kAudio; | |
248 #if WEBRTC_OPUS_VARIABLE_COMPLEXITY | |
249 config.low_rate_complexity = 9; | |
250 #endif | |
251 | |
252 constexpr int kMinSupportedFrameLength = kOpusSupportedFrameLengths[0]; | |
253 constexpr int kMaxSupportedFrameLength = | |
254 kOpusSupportedFrameLengths[arraysize(kOpusSupportedFrameLengths) - 1]; | |
kwiberg-webrtc
2017/03/15 13:33:18
Maybe
*(std::end(kOpusSupportedFrameLengths) -
ossu
2017/03/16 18:03:57
Tried it but the compiler claims thats's not const
kwiberg-webrtc
2017/03/17 10:20:02
Ah well.
| |
255 | |
256 const auto ptime = GetFormatParameter<int>(format, "ptime"); | |
257 if (ptime) { | |
258 // Pick the next highest supported frame length from | |
259 // kOpusSupportedFrameLengths. Default to the largest, if we find none. | |
260 config.frame_size_ms = kMaxSupportedFrameLength; | |
261 for (const int supported_frame_length : kOpusSupportedFrameLengths) { | |
262 if (supported_frame_length >= *ptime) { | |
263 config.frame_size_ms = supported_frame_length; | |
264 break; | |
265 } | |
266 } | |
267 } | |
268 | |
269 // For now, minptime and maxptime are only used with ANA. If ptime is outside | |
270 // of this range, it will get adjusted once ANA takes hold. Ideally, we'd now | |
kwiberg-webrtc
2017/03/15 13:33:18
know
ossu
2017/03/16 18:03:57
Maan.. I was typing in a hurry last night!
| |
271 // if ANA was to be used when setting up the config, and adjust accordingly. | |
272 const int min_frame_length_ms = | |
273 std::min(std::max(GetFormatParameter<int>(format, "minptime") | |
274 .value_or(kMinSupportedFrameLength), | |
275 kMinSupportedFrameLength), | |
276 kMaxSupportedFrameLength); | |
277 const int max_frame_length_ms = | |
278 std::min(std::max(GetFormatParameter<int>(format, "maxptime") | |
279 .value_or(kMaxSupportedFrameLength), | |
280 kMinSupportedFrameLength), | |
281 kMaxSupportedFrameLength); | |
282 if (min_frame_length_ms <= max_frame_length_ms) { | |
kwiberg-webrtc
2017/03/15 13:33:18
This conditional is just an optimization; since th
ossu
2017/03/16 18:03:57
Acknowledged.
| |
283 for (const int frame_length_ms : kANASupportedFrameLengths) { | |
284 if (frame_length_ms >= min_frame_length_ms && | |
285 frame_length_ms <= max_frame_length_ms) { | |
286 config.supported_frame_lengths_ms.push_back(frame_length_ms); | |
287 } | |
288 } | |
289 } | |
290 | |
291 // As a fallback, just pick the whole set of supported frame lengths. | |
292 if (config.supported_frame_lengths_ms.empty()) { | |
293 for (const int frame_length_ms : kANASupportedFrameLengths) { | |
294 config.supported_frame_lengths_ms.push_back(frame_length_ms); | |
kwiberg-webrtc
2017/03/15 13:33:18
std::vector::assign?
ossu
2017/03/16 18:03:58
I find it a bit less clear what's going on (for-lo
kwiberg-webrtc
2017/03/17 10:20:02
Acknowledged.
| |
295 } | |
296 } | |
297 | |
298 RTC_DCHECK(std::is_sorted(config.supported_frame_lengths_ms.begin(), | |
299 config.supported_frame_lengths_ms.end())); | |
300 | |
301 return config; | |
302 } | |
303 | |
106 class AudioEncoderOpus::PacketLossFractionSmoother { | 304 class AudioEncoderOpus::PacketLossFractionSmoother { |
107 public: | 305 public: |
108 explicit PacketLossFractionSmoother(const Clock* clock) | 306 explicit PacketLossFractionSmoother(const Clock* clock) |
109 : clock_(clock), | 307 : clock_(clock), |
110 last_sample_time_ms_(clock_->TimeInMilliseconds()), | 308 last_sample_time_ms_(clock_->TimeInMilliseconds()), |
111 smoother_(kAlphaForPacketLossFractionSmoother) {} | 309 smoother_(kAlphaForPacketLossFractionSmoother) {} |
112 | 310 |
113 // Gets the smoothed packet loss fraction. | 311 // Gets the smoothed packet loss fraction. |
114 float GetAverage() const { | 312 float GetAverage() const { |
115 float value = smoother_.filtered(); | 313 float value = smoother_.filtered(); |
(...skipping 24 matching lines...) Expand all Loading... | |
140 AudioEncoderOpus::Config::Config(const Config&) = default; | 338 AudioEncoderOpus::Config::Config(const Config&) = default; |
141 AudioEncoderOpus::Config::~Config() = default; | 339 AudioEncoderOpus::Config::~Config() = default; |
142 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; | 340 auto AudioEncoderOpus::Config::operator=(const Config&) -> Config& = default; |
143 | 341 |
144 bool AudioEncoderOpus::Config::IsOk() const { | 342 bool AudioEncoderOpus::Config::IsOk() const { |
145 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) | 343 if (frame_size_ms <= 0 || frame_size_ms % 10 != 0) |
146 return false; | 344 return false; |
147 if (num_channels != 1 && num_channels != 2) | 345 if (num_channels != 1 && num_channels != 2) |
148 return false; | 346 return false; |
149 if (bitrate_bps && | 347 if (bitrate_bps && |
150 (*bitrate_bps < kMinBitrateBps || *bitrate_bps > kMaxBitrateBps)) | 348 (*bitrate_bps < kOpusMinBitrateBps || *bitrate_bps > kOpusMaxBitrateBps)) |
151 return false; | 349 return false; |
152 if (complexity < 0 || complexity > 10) | 350 if (complexity < 0 || complexity > 10) |
153 return false; | 351 return false; |
154 if (low_rate_complexity < 0 || low_rate_complexity > 10) | 352 if (low_rate_complexity < 0 || low_rate_complexity > 10) |
155 return false; | 353 return false; |
156 return true; | 354 return true; |
157 } | 355 } |
158 | 356 |
159 int AudioEncoderOpus::Config::GetBitrateBps() const { | 357 int AudioEncoderOpus::Config::GetBitrateBps() const { |
160 RTC_DCHECK(IsOk()); | 358 RTC_DCHECK(IsOk()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 bitrate_smoother_(bitrate_smoother | 399 bitrate_smoother_(bitrate_smoother |
202 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( | 400 ? std::move(bitrate_smoother) : std::unique_ptr<SmoothingFilter>( |
203 // We choose 5sec as initial time constant due to empirical data. | 401 // We choose 5sec as initial time constant due to empirical data. |
204 new SmoothingFilterImpl(5000, config.clock))) { | 402 new SmoothingFilterImpl(5000, config.clock))) { |
205 RTC_CHECK(RecreateEncoderInstance(config)); | 403 RTC_CHECK(RecreateEncoderInstance(config)); |
206 } | 404 } |
207 | 405 |
208 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) | 406 AudioEncoderOpus::AudioEncoderOpus(const CodecInst& codec_inst) |
209 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} | 407 : AudioEncoderOpus(CreateConfig(codec_inst), nullptr) {} |
210 | 408 |
409 AudioEncoderOpus::AudioEncoderOpus(int payload_type, | |
410 const SdpAudioFormat& format) | |
411 : AudioEncoderOpus(CreateConfig(payload_type, format), nullptr) {} | |
412 | |
211 AudioEncoderOpus::~AudioEncoderOpus() { | 413 AudioEncoderOpus::~AudioEncoderOpus() { |
212 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); | 414 RTC_CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_)); |
213 } | 415 } |
214 | 416 |
215 int AudioEncoderOpus::SampleRateHz() const { | 417 int AudioEncoderOpus::SampleRateHz() const { |
216 return kSampleRateHz; | 418 return kSampleRateHz; |
217 } | 419 } |
218 | 420 |
219 size_t AudioEncoderOpus::NumChannels() const { | 421 size_t AudioEncoderOpus::NumChannels() const { |
220 return config_.num_channels; | 422 return config_.num_channels; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 } else if (send_side_bwe_with_overhead_) { | 530 } else if (send_side_bwe_with_overhead_) { |
329 if (!overhead_bytes_per_packet_) { | 531 if (!overhead_bytes_per_packet_) { |
330 LOG(LS_INFO) | 532 LOG(LS_INFO) |
331 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " | 533 << "AudioEncoderOpus: Overhead unknown, target audio bitrate " |
332 << target_audio_bitrate_bps << " bps is ignored."; | 534 << target_audio_bitrate_bps << " bps is ignored."; |
333 return; | 535 return; |
334 } | 536 } |
335 const int overhead_bps = static_cast<int>( | 537 const int overhead_bps = static_cast<int>( |
336 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); | 538 *overhead_bytes_per_packet_ * 8 * 100 / Num10MsFramesInNextPacket()); |
337 SetTargetBitrate(std::min( | 539 SetTargetBitrate(std::min( |
338 kMaxBitrateBps, | 540 kOpusMaxBitrateBps, |
339 std::max(kMinBitrateBps, target_audio_bitrate_bps - overhead_bps))); | 541 std::max(kOpusMinBitrateBps, target_audio_bitrate_bps - overhead_bps))); |
340 } else { | 542 } else { |
341 SetTargetBitrate(target_audio_bitrate_bps); | 543 SetTargetBitrate(target_audio_bitrate_bps); |
342 } | 544 } |
343 } | 545 } |
344 | 546 |
345 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { | 547 void AudioEncoderOpus::OnReceivedRtt(int rtt_ms) { |
346 if (!audio_network_adaptor_) | 548 if (!audio_network_adaptor_) |
347 return; | 549 return; |
348 audio_network_adaptor_->SetRtt(rtt_ms); | 550 audio_network_adaptor_->SetRtt(rtt_ms); |
349 ApplyAudioNetworkAdaptor(); | 551 ApplyAudioNetworkAdaptor(); |
(...skipping 10 matching lines...) Expand all Loading... | |
360 } | 562 } |
361 | 563 |
362 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, | 564 void AudioEncoderOpus::SetReceiverFrameLengthRange(int min_frame_length_ms, |
363 int max_frame_length_ms) { | 565 int max_frame_length_ms) { |
364 // Ensure that |SetReceiverFrameLengthRange| is called before | 566 // Ensure that |SetReceiverFrameLengthRange| is called before |
365 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate | 567 // |EnableAudioNetworkAdaptor|, otherwise we need to recreate |
366 // |audio_network_adaptor_|, which is not a needed use case. | 568 // |audio_network_adaptor_|, which is not a needed use case. |
367 RTC_DCHECK(!audio_network_adaptor_); | 569 RTC_DCHECK(!audio_network_adaptor_); |
368 | 570 |
369 config_.supported_frame_lengths_ms.clear(); | 571 config_.supported_frame_lengths_ms.clear(); |
370 std::copy_if(std::begin(kSupportedFrameLengths), | 572 std::copy_if(std::begin(kANASupportedFrameLengths), |
371 std::end(kSupportedFrameLengths), | 573 std::end(kANASupportedFrameLengths), |
372 std::back_inserter(config_.supported_frame_lengths_ms), | 574 std::back_inserter(config_.supported_frame_lengths_ms), |
373 [&](int frame_length_ms) { | 575 [&](int frame_length_ms) { |
374 return frame_length_ms >= min_frame_length_ms && | 576 return frame_length_ms >= min_frame_length_ms && |
375 frame_length_ms <= max_frame_length_ms; | 577 frame_length_ms <= max_frame_length_ms; |
376 }); | 578 }); |
377 RTC_DCHECK(std::is_sorted(config_.supported_frame_lengths_ms.begin(), | 579 RTC_DCHECK(std::is_sorted(config_.supported_frame_lengths_ms.begin(), |
378 config_.supported_frame_lengths_ms.end())); | 580 config_.supported_frame_lengths_ms.end())); |
379 } | 581 } |
380 | 582 |
381 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( | 583 AudioEncoder::EncodedInfo AudioEncoderOpus::EncodeImpl( |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
500 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); | 702 float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_); |
501 if (packet_loss_rate_ != opt_loss_rate) { | 703 if (packet_loss_rate_ != opt_loss_rate) { |
502 packet_loss_rate_ = opt_loss_rate; | 704 packet_loss_rate_ = opt_loss_rate; |
503 RTC_CHECK_EQ( | 705 RTC_CHECK_EQ( |
504 0, WebRtcOpus_SetPacketLossRate( | 706 0, WebRtcOpus_SetPacketLossRate( |
505 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); | 707 inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5))); |
506 } | 708 } |
507 } | 709 } |
508 | 710 |
509 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { | 711 void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) { |
510 config_.bitrate_bps = rtc::Optional<int>( | 712 config_.bitrate_bps = rtc::Optional<int>(std::max( |
511 std::max(std::min(bits_per_second, kMaxBitrateBps), kMinBitrateBps)); | 713 std::min(bits_per_second, kOpusMaxBitrateBps), kOpusMinBitrateBps)); |
512 RTC_DCHECK(config_.IsOk()); | 714 RTC_DCHECK(config_.IsOk()); |
513 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); | 715 RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps())); |
514 const auto new_complexity = config_.GetNewComplexity(); | 716 const auto new_complexity = config_.GetNewComplexity(); |
515 if (new_complexity && complexity_ != *new_complexity) { | 717 if (new_complexity && complexity_ != *new_complexity) { |
516 complexity_ = *new_complexity; | 718 complexity_ = *new_complexity; |
517 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); | 719 RTC_CHECK_EQ(0, WebRtcOpus_SetComplexity(inst_, complexity_)); |
518 } | 720 } |
519 } | 721 } |
520 | 722 |
521 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { | 723 void AudioEncoderOpus::ApplyAudioNetworkAdaptor() { |
(...skipping 20 matching lines...) Expand all Loading... | |
542 const std::string& config_string, | 744 const std::string& config_string, |
543 RtcEventLog* event_log, | 745 RtcEventLog* event_log, |
544 const Clock* clock) const { | 746 const Clock* clock) const { |
545 AudioNetworkAdaptorImpl::Config config; | 747 AudioNetworkAdaptorImpl::Config config; |
546 config.clock = clock; | 748 config.clock = clock; |
547 config.event_log = event_log; | 749 config.event_log = event_log; |
548 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( | 750 return std::unique_ptr<AudioNetworkAdaptor>(new AudioNetworkAdaptorImpl( |
549 config, | 751 config, |
550 ControllerManagerImpl::Create( | 752 ControllerManagerImpl::Create( |
551 config_string, NumChannels(), supported_frame_lengths_ms(), | 753 config_string, NumChannels(), supported_frame_lengths_ms(), |
552 kMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, | 754 kOpusMinBitrateBps, num_channels_to_encode_, next_frame_length_ms_, |
553 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); | 755 GetTargetBitrate(), config_.fec_enabled, GetDtx(), clock))); |
554 } | 756 } |
555 | 757 |
556 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { | 758 void AudioEncoderOpus::MaybeUpdateUplinkBandwidth() { |
557 if (audio_network_adaptor_) { | 759 if (audio_network_adaptor_) { |
558 int64_t now_ms = rtc::TimeMillis(); | 760 int64_t now_ms = rtc::TimeMillis(); |
559 if (!bitrate_smoother_last_update_time_ || | 761 if (!bitrate_smoother_last_update_time_ || |
560 now_ms - *bitrate_smoother_last_update_time_ >= | 762 now_ms - *bitrate_smoother_last_update_time_ >= |
561 config_.uplink_bandwidth_update_interval_ms) { | 763 config_.uplink_bandwidth_update_interval_ms) { |
562 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); | 764 rtc::Optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); |
563 if (smoothed_bitrate) | 765 if (smoothed_bitrate) |
564 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); | 766 audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); |
565 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); | 767 bitrate_smoother_last_update_time_ = rtc::Optional<int64_t>(now_ms); |
566 } | 768 } |
567 } | 769 } |
568 } | 770 } |
569 | 771 |
570 } // namespace webrtc | 772 } // namespace webrtc |
OLD | NEW |