From b6fab2b1cdcc8fd93ab8ac3dad19ee213a31a89e Mon Sep 17 00:00:00 2001 From: "henrik.lundin@webrtc.org" Date: Mon, 26 Jan 2015 11:08:53 +0000 Subject: [PATCH] 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 --- webrtc/base/checks.h | 8 ++++++++ .../codecs/isac/audio_encoder_isac_t_impl.h | 8 ++------ .../audio_coding/codecs/opus/audio_encoder_opus.cc | 13 ++++++------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/webrtc/base/checks.h b/webrtc/base/checks.h index 5438d5989..f64d2730e 100644 --- a/webrtc/base/checks.h +++ b/webrtc/base/checks.h @@ -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 +inline T CheckedDivExact(T a, T b) { + CHECK_EQ(a % b, static_cast(0)); + return a / b; +} + } // namespace rtc #endif // WEBRTC_BASE_CHECKS_H_ diff --git a/webrtc/modules/audio_coding/codecs/isac/audio_encoder_isac_t_impl.h b/webrtc/modules/audio_coding/codecs/isac/audio_encoder_isac_t_impl.h index 9d3666332..6fc4dc6aa 100644 --- a/webrtc/modules/audio_coding/codecs/isac/audio_encoder_isac_t_impl.h +++ b/webrtc/modules/audio_coding/codecs/isac/audio_encoder_isac_t_impl.h @@ -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 AudioEncoderDecoderIsacT::Config::Config() : payload_type(kIsacPayloadType), @@ -115,7 +110,8 @@ template int AudioEncoderDecoderIsacT::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 diff --git a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc index 358647f53..fce7721dd 100644 --- a/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc +++ b/webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc @@ -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( @@ -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(num_channels_)), ClampInt16(max_encoded_bytes), encoded); input_buffer_.clear(); if (r < 0)