Add SafeClamp(), which accepts args of different types

Specifically, just like SafeMin() and SafeMax() it handles all
combinations of integer and all
combinations of floating-point arguments by picking a
result type that is guaranteed to be able to hold the result.

This CL also replaces a bunch of std::min + std:max call pairs with
calls to SafeClamp()---the ones that could easily be found by grep
because "min" and "max" were on the same line. :-)

BUG=webrtc:7459

Review-Url: https://codereview.webrtc.org/2808513003
Cr-Commit-Position: refs/heads/master@{#18542}
This commit is contained in:
kwiberg
2017-06-12 11:40:47 -07:00
committed by Commit Bot
parent d1114c7fef
commit 0703856b53
23 changed files with 443 additions and 97 deletions

View File

@ -20,6 +20,7 @@
#include "webrtc/base/numerics/exp_filter.h"
#include "webrtc/base/protobuf_utils.h"
#include "webrtc/base/safe_conversions.h"
#include "webrtc/base/safe_minmax.h"
#include "webrtc/base/string_to_number.h"
#include "webrtc/base/timeutils.h"
#include "webrtc/common_types.h"
@ -690,8 +691,8 @@ void AudioEncoderOpus::SetProjectedPacketLossRate(float fraction) {
}
void AudioEncoderOpus::SetTargetBitrate(int bits_per_second) {
config_.bitrate_bps = rtc::Optional<int>(std::max(
std::min(bits_per_second, kOpusMaxBitrateBps), kOpusMinBitrateBps));
config_.bitrate_bps = rtc::Optional<int>(rtc::SafeClamp<int>(
bits_per_second, kOpusMinBitrateBps, kOpusMaxBitrateBps));
RTC_DCHECK(config_.IsOk());
RTC_CHECK_EQ(0, WebRtcOpus_SetBitRate(inst_, config_.GetBitrateBps()));
const auto new_complexity = config_.GetNewComplexity();