Introduce rtc::CheckedDivExact

Use the new method to replace local ones in AudioEncoder{Opus,Isac}.

COAUTHOR:kwiberg@webrtc.org

R=andrew@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/34779004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@8148 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrik.lundin@webrtc.org 2015-01-26 11:08:53 +00:00
parent 19eb4e4b86
commit b6fab2b1cd
3 changed files with 16 additions and 13 deletions

View File

@ -217,6 +217,14 @@ class FatalMessage {
std::ostringstream stream_;
};
// Performs the integer division a/b and returns the result. CHECKs that the
// remainder is zero.
template <typename T>
inline T CheckedDivExact(T a, T b) {
CHECK_EQ(a % b, static_cast<T>(0));
return a / b;
}
} // namespace rtc
#endif // WEBRTC_BASE_CHECKS_H_

View File

@ -20,11 +20,6 @@ namespace webrtc {
const int kIsacPayloadType = 103;
inline int DivExact(int a, int b) {
CHECK_EQ(a % b, 0);
return a / b;
}
template <typename T>
AudioEncoderDecoderIsacT<T>::Config::Config()
: payload_type(kIsacPayloadType),
@ -115,7 +110,8 @@ template <typename T>
int AudioEncoderDecoderIsacT<T>::Num10MsFramesInNextPacket() const {
CriticalSectionScoped cs(lock_.get());
const int samples_in_next_packet = T::GetNewFrameLen(isac_state_);
return DivExact(samples_in_next_packet, DivExact(sample_rate_hz(), 100));
return rtc::CheckedDivExact(samples_in_next_packet,
rtc::CheckedDivExact(sample_rate_hz(), 100));
}
template <typename T>

View File

@ -19,10 +19,6 @@ namespace {
// We always encode at 48 kHz.
const int kSampleRateHz = 48000;
int DivExact(int a, int b) {
CHECK_EQ(a % b, 0);
return a / b;
}
int16_t ClampInt16(size_t x) {
return static_cast<int16_t>(
@ -52,11 +48,13 @@ bool AudioEncoderOpus::Config::IsOk() const {
}
AudioEncoderOpus::AudioEncoderOpus(const Config& config)
: num_10ms_frames_per_packet_(DivExact(config.frame_size_ms, 10)),
: num_10ms_frames_per_packet_(
rtc::CheckedDivExact(config.frame_size_ms, 10)),
num_channels_(config.num_channels),
payload_type_(config.payload_type),
application_(config.application),
samples_per_10ms_frame_(DivExact(kSampleRateHz, 100) * num_channels_) {
samples_per_10ms_frame_(rtc::CheckedDivExact(kSampleRateHz, 100) *
num_channels_) {
CHECK(config.IsOk());
input_buffer_.reserve(num_10ms_frames_per_packet_ * samples_per_10ms_frame_);
CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, num_channels_, application_));
@ -101,7 +99,8 @@ bool AudioEncoderOpus::EncodeInternal(uint32_t timestamp,
samples_per_10ms_frame_);
int16_t r = WebRtcOpus_Encode(
inst_, &input_buffer_[0],
DivExact(CastInt16(input_buffer_.size()), num_channels_),
rtc::CheckedDivExact(CastInt16(input_buffer_.size()),
static_cast<int16_t>(num_channels_)),
ClampInt16(max_encoded_bytes), encoded);
input_buffer_.clear();
if (r < 0)