NetEq: Fix a bug in expand_rate and speech_expand_rate calculation

After a Merge operation, the statistics for number of samples
generated using Expand must be corrected, and the correction can in
fact be negative. However, a bug was introduced in
https://codereview.webrtc.org/1230503003 which uses a size_t to
represent the correction, which leads to wrap-around of the negative
value. This is not a problem in itself, since this value is added to
another size_t, with the effect that the desired subtraction happens
anyway.

The actual problem arises if the statistics are polled/reset before a
subtraction happens -- that is, between an Expand and a Merge
operation. This will lead to an actual wrap-around of the stats value,
and large expand_rate (16384) is reported.

BUG=webrtc:7554

Review-Url: https://codereview.webrtc.org/2859483005
Cr-Commit-Position: refs/heads/master@{#18029}
This commit is contained in:
henrik.lundin
2017-05-05 05:04:16 -07:00
committed by Commit bot
parent 2a28035627
commit 2979f55f95
5 changed files with 45 additions and 14 deletions

View File

@ -22,6 +22,16 @@
namespace webrtc {
namespace {
size_t AddIntToSizeTWithLowerCap(int a, size_t b) {
const size_t ret = b + a;
// If a + b is negative, resulting in a negative wrap, cap it to zero instead.
static_assert(sizeof(size_t) >= sizeof(int),
"int must not be wider than size_t for this to work");
return (a < 0 && ret > b) ? 0 : ret;
}
} // namespace
// Allocating the static const so that it can be passed by reference to
// RTC_DCHECK.
const size_t StatisticsCalculator::kLenWaitingTimes;
@ -148,6 +158,16 @@ void StatisticsCalculator::ExpandedNoiseSamples(size_t num_samples) {
expanded_noise_samples_ += num_samples;
}
void StatisticsCalculator::ExpandedVoiceSamplesCorrection(int num_samples) {
expanded_speech_samples_ =
AddIntToSizeTWithLowerCap(num_samples, expanded_speech_samples_);
}
void StatisticsCalculator::ExpandedNoiseSamplesCorrection(int num_samples) {
expanded_noise_samples_ =
AddIntToSizeTWithLowerCap(num_samples, expanded_noise_samples_);
}
void StatisticsCalculator::PreemptiveExpandedSamples(size_t num_samples) {
preemptive_samples_ += num_samples;
}