diff --git a/webrtc/base/checks.h b/webrtc/base/checks.h index 5438d5989d..f64d2730e0 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 9d36663320..6fc4dc6aa4 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 358647f539..fce7721dd6 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)