henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/statistics_calculator.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <string.h> // memset |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 15 | #include <algorithm> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/neteq/delay_manager.h" |
| 18 | #include "rtc_base/checks.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame^] | 19 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "system_wrappers/include/metrics.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 24 | namespace { |
| 25 | size_t AddIntToSizeTWithLowerCap(int a, size_t b) { |
| 26 | const size_t ret = b + a; |
| 27 | // If a + b is negative, resulting in a negative wrap, cap it to zero instead. |
| 28 | static_assert(sizeof(size_t) >= sizeof(int), |
| 29 | "int must not be wider than size_t for this to work"); |
| 30 | return (a < 0 && ret > b) ? 0 : ret; |
| 31 | } |
| 32 | } // namespace |
| 33 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 34 | // Allocating the static const so that it can be passed by reference to |
| 35 | // RTC_DCHECK. |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 36 | const size_t StatisticsCalculator::kLenWaitingTimes; |
| 37 | |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 38 | StatisticsCalculator::PeriodicUmaLogger::PeriodicUmaLogger( |
| 39 | const std::string& uma_name, |
| 40 | int report_interval_ms, |
| 41 | int max_value) |
| 42 | : uma_name_(uma_name), |
| 43 | report_interval_ms_(report_interval_ms), |
| 44 | max_value_(max_value), |
| 45 | timer_(0) { |
| 46 | } |
| 47 | |
| 48 | StatisticsCalculator::PeriodicUmaLogger::~PeriodicUmaLogger() = default; |
| 49 | |
| 50 | void StatisticsCalculator::PeriodicUmaLogger::AdvanceClock(int step_ms) { |
| 51 | timer_ += step_ms; |
| 52 | if (timer_ < report_interval_ms_) { |
| 53 | return; |
| 54 | } |
| 55 | LogToUma(Metric()); |
| 56 | Reset(); |
| 57 | timer_ -= report_interval_ms_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 58 | RTC_DCHECK_GE(timer_, 0); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void StatisticsCalculator::PeriodicUmaLogger::LogToUma(int value) const { |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 62 | RTC_HISTOGRAM_COUNTS_SPARSE(uma_name_, value, 1, max_value_, 50); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | StatisticsCalculator::PeriodicUmaCount::PeriodicUmaCount( |
| 66 | const std::string& uma_name, |
| 67 | int report_interval_ms, |
| 68 | int max_value) |
| 69 | : PeriodicUmaLogger(uma_name, report_interval_ms, max_value) { |
| 70 | } |
| 71 | |
| 72 | StatisticsCalculator::PeriodicUmaCount::~PeriodicUmaCount() { |
| 73 | // Log the count for the current (incomplete) interval. |
| 74 | LogToUma(Metric()); |
| 75 | } |
| 76 | |
| 77 | void StatisticsCalculator::PeriodicUmaCount::RegisterSample() { |
| 78 | ++counter_; |
| 79 | } |
| 80 | |
| 81 | int StatisticsCalculator::PeriodicUmaCount::Metric() const { |
| 82 | return counter_; |
| 83 | } |
| 84 | |
| 85 | void StatisticsCalculator::PeriodicUmaCount::Reset() { |
| 86 | counter_ = 0; |
| 87 | } |
| 88 | |
| 89 | StatisticsCalculator::PeriodicUmaAverage::PeriodicUmaAverage( |
| 90 | const std::string& uma_name, |
| 91 | int report_interval_ms, |
| 92 | int max_value) |
| 93 | : PeriodicUmaLogger(uma_name, report_interval_ms, max_value) { |
| 94 | } |
| 95 | |
| 96 | StatisticsCalculator::PeriodicUmaAverage::~PeriodicUmaAverage() { |
| 97 | // Log the average for the current (incomplete) interval. |
| 98 | LogToUma(Metric()); |
| 99 | } |
| 100 | |
| 101 | void StatisticsCalculator::PeriodicUmaAverage::RegisterSample(int value) { |
| 102 | sum_ += value; |
| 103 | ++counter_; |
| 104 | } |
| 105 | |
| 106 | int StatisticsCalculator::PeriodicUmaAverage::Metric() const { |
henrik.lundin | e594213 | 2016-02-09 00:35:53 -0800 | [diff] [blame] | 107 | return counter_ == 0 ? 0 : static_cast<int>(sum_ / counter_); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void StatisticsCalculator::PeriodicUmaAverage::Reset() { |
| 111 | sum_ = 0.0; |
| 112 | counter_ = 0; |
| 113 | } |
| 114 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 115 | StatisticsCalculator::StatisticsCalculator() |
| 116 | : preemptive_samples_(0), |
| 117 | accelerate_samples_(0), |
| 118 | added_zero_samples_(0), |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 119 | expanded_speech_samples_(0), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 120 | expanded_noise_samples_(0), |
| 121 | discarded_packets_(0), |
| 122 | lost_timestamps_(0), |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 123 | timestamps_since_last_report_(0), |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 124 | secondary_decoded_samples_(0), |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 125 | discarded_secondary_packets_(0), |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 126 | delayed_packet_outage_counter_( |
| 127 | "WebRTC.Audio.DelayedPacketOutageEventsPerMinute", |
| 128 | 60000, // 60 seconds report interval. |
| 129 | 100), |
| 130 | excess_buffer_delay_("WebRTC.Audio.AverageExcessBufferDelayMs", |
| 131 | 60000, // 60 seconds report interval. |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 132 | 1000) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 133 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 134 | StatisticsCalculator::~StatisticsCalculator() = default; |
| 135 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 136 | void StatisticsCalculator::Reset() { |
| 137 | preemptive_samples_ = 0; |
| 138 | accelerate_samples_ = 0; |
| 139 | added_zero_samples_ = 0; |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 140 | expanded_speech_samples_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 141 | expanded_noise_samples_ = 0; |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 142 | secondary_decoded_samples_ = 0; |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 143 | discarded_secondary_packets_ = 0; |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 144 | waiting_times_.clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void StatisticsCalculator::ResetMcu() { |
| 148 | discarded_packets_ = 0; |
| 149 | lost_timestamps_ = 0; |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 150 | timestamps_since_last_report_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 153 | void StatisticsCalculator::ExpandedVoiceSamples(size_t num_samples, |
| 154 | bool is_new_concealment_event) { |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 155 | expanded_speech_samples_ += num_samples; |
Karl Wiberg | f52a3a7 | 2017-10-16 12:41:36 +0200 | [diff] [blame] | 156 | ConcealedSamplesCorrection(rtc::dchecked_cast<int>(num_samples)); |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 157 | lifetime_stats_.concealment_events += is_new_concealment_event; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 160 | void StatisticsCalculator::ExpandedNoiseSamples(size_t num_samples, |
| 161 | bool is_new_concealment_event) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 162 | expanded_noise_samples_ += num_samples; |
Karl Wiberg | f52a3a7 | 2017-10-16 12:41:36 +0200 | [diff] [blame] | 163 | ConcealedSamplesCorrection(rtc::dchecked_cast<int>(num_samples)); |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 164 | lifetime_stats_.concealment_events += is_new_concealment_event; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 165 | } |
| 166 | |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 167 | void StatisticsCalculator::ExpandedVoiceSamplesCorrection(int num_samples) { |
| 168 | expanded_speech_samples_ = |
| 169 | AddIntToSizeTWithLowerCap(num_samples, expanded_speech_samples_); |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 170 | ConcealedSamplesCorrection(num_samples); |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void StatisticsCalculator::ExpandedNoiseSamplesCorrection(int num_samples) { |
| 174 | expanded_noise_samples_ = |
| 175 | AddIntToSizeTWithLowerCap(num_samples, expanded_noise_samples_); |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 176 | ConcealedSamplesCorrection(num_samples); |
| 177 | } |
| 178 | |
| 179 | void StatisticsCalculator::ConcealedSamplesCorrection(int num_samples) { |
| 180 | if (num_samples < 0) { |
| 181 | // Store negative correction to subtract from future positive additions. |
| 182 | // See also the function comment in the header file. |
| 183 | concealed_samples_correction_ -= num_samples; |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | const size_t canceled_out = |
| 188 | std::min(static_cast<size_t>(num_samples), concealed_samples_correction_); |
| 189 | concealed_samples_correction_ -= canceled_out; |
| 190 | lifetime_stats_.concealed_samples += num_samples - canceled_out; |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 193 | void StatisticsCalculator::PreemptiveExpandedSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 194 | preemptive_samples_ += num_samples; |
| 195 | } |
| 196 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 197 | void StatisticsCalculator::AcceleratedSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 198 | accelerate_samples_ += num_samples; |
| 199 | } |
| 200 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 201 | void StatisticsCalculator::AddZeros(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 202 | added_zero_samples_ += num_samples; |
| 203 | } |
| 204 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 205 | void StatisticsCalculator::PacketsDiscarded(size_t num_packets) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 206 | discarded_packets_ += num_packets; |
| 207 | } |
| 208 | |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 209 | void StatisticsCalculator::SecondaryPacketsDiscarded(size_t num_packets) { |
| 210 | discarded_secondary_packets_ += num_packets; |
| 211 | } |
| 212 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 213 | void StatisticsCalculator::LostSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 214 | lost_timestamps_ += num_samples; |
| 215 | } |
| 216 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 217 | void StatisticsCalculator::IncreaseCounter(size_t num_samples, int fs_hz) { |
| 218 | const int time_step_ms = |
| 219 | rtc::CheckedDivExact(static_cast<int>(1000 * num_samples), fs_hz); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 220 | delayed_packet_outage_counter_.AdvanceClock(time_step_ms); |
| 221 | excess_buffer_delay_.AdvanceClock(time_step_ms); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 222 | timestamps_since_last_report_ += static_cast<uint32_t>(num_samples); |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 223 | if (timestamps_since_last_report_ > |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 224 | static_cast<uint32_t>(fs_hz * kMaxReportPeriod)) { |
| 225 | lost_timestamps_ = 0; |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 226 | timestamps_since_last_report_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 227 | discarded_packets_ = 0; |
| 228 | } |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 229 | lifetime_stats_.total_samples_received += num_samples; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 232 | void StatisticsCalculator::JitterBufferDelay(size_t num_samples, |
| 233 | uint64_t waiting_time_ms) { |
| 234 | lifetime_stats_.jitter_buffer_delay_ms += waiting_time_ms * num_samples; |
| 235 | } |
| 236 | |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 237 | void StatisticsCalculator::SecondaryDecodedSamples(int num_samples) { |
| 238 | secondary_decoded_samples_ += num_samples; |
| 239 | } |
| 240 | |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 241 | void StatisticsCalculator::LogDelayedPacketOutageEvent(int outage_duration_ms) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 242 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.DelayedPacketOutageEventMs", |
| 243 | outage_duration_ms, 1 /* min */, 2000 /* max */, |
| 244 | 100 /* bucket count */); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 245 | delayed_packet_outage_counter_.RegisterSample(); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 246 | } |
| 247 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 248 | void StatisticsCalculator::StoreWaitingTime(int waiting_time_ms) { |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 249 | excess_buffer_delay_.RegisterSample(waiting_time_ms); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 250 | RTC_DCHECK_LE(waiting_times_.size(), kLenWaitingTimes); |
henrik.lundin | 1e346b2 | 2015-08-27 13:41:02 -0700 | [diff] [blame] | 251 | if (waiting_times_.size() == kLenWaitingTimes) { |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 252 | // Erase first value. |
| 253 | waiting_times_.pop_front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 254 | } |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 255 | waiting_times_.push_back(waiting_time_ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void StatisticsCalculator::GetNetworkStatistics( |
| 259 | int fs_hz, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 260 | size_t num_samples_in_buffers, |
| 261 | size_t samples_per_packet, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 262 | NetEqNetworkStatistics *stats) { |
Henrik Lundin | dccfc40 | 2017-09-25 12:30:58 +0200 | [diff] [blame] | 263 | RTC_DCHECK_GT(fs_hz, 0); |
| 264 | RTC_DCHECK(stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 265 | |
| 266 | stats->added_zero_samples = added_zero_samples_; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 267 | stats->current_buffer_size_ms = |
| 268 | static_cast<uint16_t>(num_samples_in_buffers * 1000 / fs_hz); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 269 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 270 | stats->packet_loss_rate = |
| 271 | CalculateQ14Ratio(lost_timestamps_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 272 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 273 | stats->accelerate_rate = |
| 274 | CalculateQ14Ratio(accelerate_samples_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 275 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 276 | stats->preemptive_rate = |
| 277 | CalculateQ14Ratio(preemptive_samples_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 278 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 279 | stats->expand_rate = |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 280 | CalculateQ14Ratio(expanded_speech_samples_ + expanded_noise_samples_, |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 281 | timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 282 | |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 283 | stats->speech_expand_rate = |
| 284 | CalculateQ14Ratio(expanded_speech_samples_, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 285 | timestamps_since_last_report_); |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 286 | |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 287 | stats->secondary_decoded_rate = |
| 288 | CalculateQ14Ratio(secondary_decoded_samples_, |
| 289 | timestamps_since_last_report_); |
| 290 | |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 291 | const size_t discarded_secondary_samples = |
| 292 | discarded_secondary_packets_ * samples_per_packet; |
| 293 | stats->secondary_discarded_rate = CalculateQ14Ratio( |
| 294 | discarded_secondary_samples, |
lliuu | ff2824b | 2017-08-28 13:01:45 -0700 | [diff] [blame] | 295 | static_cast<uint32_t>(discarded_secondary_samples + |
| 296 | secondary_decoded_samples_)); |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 297 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 298 | if (waiting_times_.size() == 0) { |
| 299 | stats->mean_waiting_time_ms = -1; |
| 300 | stats->median_waiting_time_ms = -1; |
| 301 | stats->min_waiting_time_ms = -1; |
| 302 | stats->max_waiting_time_ms = -1; |
| 303 | } else { |
| 304 | std::sort(waiting_times_.begin(), waiting_times_.end()); |
| 305 | // Find mid-point elements. If the size is odd, the two values |
| 306 | // |middle_left| and |middle_right| will both be the one middle element; if |
| 307 | // the size is even, they will be the the two neighboring elements at the |
| 308 | // middle of the list. |
| 309 | const int middle_left = waiting_times_[(waiting_times_.size() - 1) / 2]; |
| 310 | const int middle_right = waiting_times_[waiting_times_.size() / 2]; |
| 311 | // Calculate the average of the two. (Works also for odd sizes.) |
| 312 | stats->median_waiting_time_ms = (middle_left + middle_right) / 2; |
| 313 | stats->min_waiting_time_ms = waiting_times_.front(); |
| 314 | stats->max_waiting_time_ms = waiting_times_.back(); |
| 315 | double sum = 0; |
| 316 | for (auto time : waiting_times_) { |
| 317 | sum += time; |
| 318 | } |
| 319 | stats->mean_waiting_time_ms = static_cast<int>(sum / waiting_times_.size()); |
| 320 | } |
| 321 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 322 | // Reset counters. |
| 323 | ResetMcu(); |
| 324 | Reset(); |
| 325 | } |
| 326 | |
Henrik Lundin | dccfc40 | 2017-09-25 12:30:58 +0200 | [diff] [blame] | 327 | void StatisticsCalculator::PopulateDelayManagerStats( |
| 328 | int ms_per_packet, |
| 329 | const DelayManager& delay_manager, |
| 330 | NetEqNetworkStatistics* stats) { |
| 331 | RTC_DCHECK(stats); |
| 332 | stats->preferred_buffer_size_ms = |
| 333 | (delay_manager.TargetLevel() >> 8) * ms_per_packet; |
| 334 | stats->jitter_peaks_found = delay_manager.PeakFound(); |
| 335 | stats->clockdrift_ppm = |
| 336 | rtc::saturated_cast<int32_t>(delay_manager.EstimatedClockDriftPpm()); |
| 337 | } |
| 338 | |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 339 | NetEqLifetimeStatistics StatisticsCalculator::GetLifetimeStatistics() const { |
| 340 | return lifetime_stats_; |
| 341 | } |
| 342 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 343 | uint16_t StatisticsCalculator::CalculateQ14Ratio(size_t numerator, |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 344 | uint32_t denominator) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 345 | if (numerator == 0) { |
| 346 | return 0; |
| 347 | } else if (numerator < denominator) { |
| 348 | // Ratio must be smaller than 1 in Q14. |
| 349 | assert((numerator << 14) / denominator < (1 << 14)); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 350 | return static_cast<uint16_t>((numerator << 14) / denominator); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 351 | } else { |
| 352 | // Will not produce a ratio larger than 1, since this is probably an error. |
| 353 | return 1 << 14; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | } // namespace webrtc |