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