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 |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 16 | #include <algorithm> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_coding/neteq/delay_manager.h" |
| 19 | #include "rtc_base/checks.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 20 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "system_wrappers/include/metrics.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | size_t AddIntToSizeTWithLowerCap(int a, size_t b) { |
| 27 | const size_t ret = b + a; |
| 28 | // If a + b is negative, resulting in a negative wrap, cap it to zero instead. |
| 29 | static_assert(sizeof(size_t) >= sizeof(int), |
| 30 | "int must not be wider than size_t for this to work"); |
| 31 | return (a < 0 && ret > b) ? 0 : ret; |
| 32 | } |
Henrik Lundin | 2a8bd09 | 2019-04-26 09:47:07 +0200 | [diff] [blame] | 33 | |
| 34 | constexpr int kInterruptionLenMs = 150; |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 35 | } // namespace |
| 36 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 37 | // Allocating the static const so that it can be passed by reference to |
| 38 | // RTC_DCHECK. |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 39 | const size_t StatisticsCalculator::kLenWaitingTimes; |
| 40 | |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 41 | StatisticsCalculator::PeriodicUmaLogger::PeriodicUmaLogger( |
| 42 | const std::string& uma_name, |
| 43 | int report_interval_ms, |
| 44 | int max_value) |
| 45 | : uma_name_(uma_name), |
| 46 | report_interval_ms_(report_interval_ms), |
| 47 | max_value_(max_value), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 48 | timer_(0) {} |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 49 | |
| 50 | StatisticsCalculator::PeriodicUmaLogger::~PeriodicUmaLogger() = default; |
| 51 | |
| 52 | void StatisticsCalculator::PeriodicUmaLogger::AdvanceClock(int step_ms) { |
| 53 | timer_ += step_ms; |
| 54 | if (timer_ < report_interval_ms_) { |
| 55 | return; |
| 56 | } |
| 57 | LogToUma(Metric()); |
| 58 | Reset(); |
| 59 | timer_ -= report_interval_ms_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 60 | RTC_DCHECK_GE(timer_, 0); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void StatisticsCalculator::PeriodicUmaLogger::LogToUma(int value) const { |
asapersson | 5380532 | 2015-12-21 01:46:20 -0800 | [diff] [blame] | 64 | RTC_HISTOGRAM_COUNTS_SPARSE(uma_name_, value, 1, max_value_, 50); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | StatisticsCalculator::PeriodicUmaCount::PeriodicUmaCount( |
| 68 | const std::string& uma_name, |
| 69 | int report_interval_ms, |
| 70 | int max_value) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 71 | : PeriodicUmaLogger(uma_name, report_interval_ms, max_value) {} |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 72 | |
| 73 | StatisticsCalculator::PeriodicUmaCount::~PeriodicUmaCount() { |
| 74 | // Log the count for the current (incomplete) interval. |
| 75 | LogToUma(Metric()); |
| 76 | } |
| 77 | |
| 78 | void StatisticsCalculator::PeriodicUmaCount::RegisterSample() { |
| 79 | ++counter_; |
| 80 | } |
| 81 | |
| 82 | int StatisticsCalculator::PeriodicUmaCount::Metric() const { |
| 83 | return counter_; |
| 84 | } |
| 85 | |
| 86 | void StatisticsCalculator::PeriodicUmaCount::Reset() { |
| 87 | counter_ = 0; |
| 88 | } |
| 89 | |
| 90 | StatisticsCalculator::PeriodicUmaAverage::PeriodicUmaAverage( |
| 91 | const std::string& uma_name, |
| 92 | int report_interval_ms, |
| 93 | int max_value) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | : PeriodicUmaLogger(uma_name, report_interval_ms, max_value) {} |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 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 Li | 34d990f | 2018-10-16 16:55:52 +0200 | [diff] [blame] | 132 | 1000), |
| 133 | buffer_full_counter_("WebRTC.Audio.JitterBufferFullPerMinute", |
| 134 | 60000, // 60 seconds report interval. |
| 135 | 100) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 136 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 137 | StatisticsCalculator::~StatisticsCalculator() = default; |
| 138 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 139 | void StatisticsCalculator::Reset() { |
| 140 | preemptive_samples_ = 0; |
| 141 | accelerate_samples_ = 0; |
| 142 | added_zero_samples_ = 0; |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 143 | expanded_speech_samples_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 144 | expanded_noise_samples_ = 0; |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 145 | secondary_decoded_samples_ = 0; |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 146 | discarded_secondary_packets_ = 0; |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 147 | waiting_times_.clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void StatisticsCalculator::ResetMcu() { |
| 151 | discarded_packets_ = 0; |
| 152 | lost_timestamps_ = 0; |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 153 | timestamps_since_last_report_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 156 | void StatisticsCalculator::ExpandedVoiceSamples(size_t num_samples, |
| 157 | bool is_new_concealment_event) { |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 158 | expanded_speech_samples_ += num_samples; |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 159 | ConcealedSamplesCorrection(rtc::dchecked_cast<int>(num_samples), true); |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 160 | lifetime_stats_.concealment_events += is_new_concealment_event; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 163 | void StatisticsCalculator::ExpandedNoiseSamples(size_t num_samples, |
| 164 | bool is_new_concealment_event) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 165 | expanded_noise_samples_ += num_samples; |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 166 | ConcealedSamplesCorrection(rtc::dchecked_cast<int>(num_samples), false); |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 167 | lifetime_stats_.concealment_events += is_new_concealment_event; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 168 | } |
| 169 | |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 170 | void StatisticsCalculator::ExpandedVoiceSamplesCorrection(int num_samples) { |
| 171 | expanded_speech_samples_ = |
| 172 | AddIntToSizeTWithLowerCap(num_samples, expanded_speech_samples_); |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 173 | ConcealedSamplesCorrection(num_samples, true); |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void StatisticsCalculator::ExpandedNoiseSamplesCorrection(int num_samples) { |
| 177 | expanded_noise_samples_ = |
| 178 | AddIntToSizeTWithLowerCap(num_samples, expanded_noise_samples_); |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 179 | ConcealedSamplesCorrection(num_samples, false); |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Henrik Lundin | 2a8bd09 | 2019-04-26 09:47:07 +0200 | [diff] [blame] | 182 | void StatisticsCalculator::DecodedOutputPlayed() { |
| 183 | decoded_output_played_ = true; |
| 184 | } |
| 185 | |
| 186 | void StatisticsCalculator::EndExpandEvent(int fs_hz) { |
| 187 | RTC_DCHECK_GE(lifetime_stats_.concealed_samples, |
| 188 | concealed_samples_at_event_end_); |
| 189 | const int event_duration_ms = |
| 190 | 1000 * |
| 191 | (lifetime_stats_.concealed_samples - concealed_samples_at_event_end_) / |
| 192 | fs_hz; |
| 193 | if (event_duration_ms >= kInterruptionLenMs && decoded_output_played_) { |
| 194 | lifetime_stats_.interruption_count++; |
| 195 | lifetime_stats_.total_interruption_duration_ms += event_duration_ms; |
| 196 | } |
| 197 | concealed_samples_at_event_end_ = lifetime_stats_.concealed_samples; |
| 198 | } |
| 199 | |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 200 | void StatisticsCalculator::ConcealedSamplesCorrection(int num_samples, |
| 201 | bool is_voice) { |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 202 | if (num_samples < 0) { |
| 203 | // Store negative correction to subtract from future positive additions. |
| 204 | // See also the function comment in the header file. |
| 205 | concealed_samples_correction_ -= num_samples; |
Ivo Creusen | bf4a221 | 2019-04-24 14:06:24 +0200 | [diff] [blame] | 206 | if (!is_voice) { |
| 207 | silent_concealed_samples_correction_ -= num_samples; |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 208 | } |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
| 212 | const size_t canceled_out = |
| 213 | std::min(static_cast<size_t>(num_samples), concealed_samples_correction_); |
| 214 | concealed_samples_correction_ -= canceled_out; |
| 215 | lifetime_stats_.concealed_samples += num_samples - canceled_out; |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 216 | |
Ivo Creusen | bf4a221 | 2019-04-24 14:06:24 +0200 | [diff] [blame] | 217 | if (!is_voice) { |
| 218 | const size_t silent_canceled_out = std::min( |
| 219 | static_cast<size_t>(num_samples), silent_concealed_samples_correction_); |
| 220 | silent_concealed_samples_correction_ -= silent_canceled_out; |
| 221 | lifetime_stats_.silent_concealed_samples += |
| 222 | num_samples - silent_canceled_out; |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 223 | } |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 226 | void StatisticsCalculator::PreemptiveExpandedSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 227 | preemptive_samples_ += num_samples; |
Ivo Creusen | d1c2f78 | 2018-09-13 14:39:55 +0200 | [diff] [blame] | 228 | operations_and_state_.preemptive_samples += num_samples; |
Ivo Creusen | bf4a221 | 2019-04-24 14:06:24 +0200 | [diff] [blame] | 229 | lifetime_stats_.inserted_samples_for_deceleration += num_samples; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 232 | void StatisticsCalculator::AcceleratedSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 233 | accelerate_samples_ += num_samples; |
Ivo Creusen | d1c2f78 | 2018-09-13 14:39:55 +0200 | [diff] [blame] | 234 | operations_and_state_.accelerate_samples += num_samples; |
Ivo Creusen | bf4a221 | 2019-04-24 14:06:24 +0200 | [diff] [blame] | 235 | lifetime_stats_.removed_samples_for_acceleration += num_samples; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 238 | void StatisticsCalculator::AddZeros(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 239 | added_zero_samples_ += num_samples; |
| 240 | } |
| 241 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 242 | void StatisticsCalculator::PacketsDiscarded(size_t num_packets) { |
Ivo Creusen | 2db46b0 | 2018-12-14 16:49:12 +0100 | [diff] [blame] | 243 | operations_and_state_.discarded_primary_packets += num_packets; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 244 | } |
| 245 | |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 246 | void StatisticsCalculator::SecondaryPacketsDiscarded(size_t num_packets) { |
| 247 | discarded_secondary_packets_ += num_packets; |
Ivo Creusen | bf4a221 | 2019-04-24 14:06:24 +0200 | [diff] [blame] | 248 | lifetime_stats_.fec_packets_discarded += num_packets; |
| 249 | } |
| 250 | |
| 251 | void StatisticsCalculator::SecondaryPacketsReceived(size_t num_packets) { |
| 252 | lifetime_stats_.fec_packets_received += num_packets; |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 255 | void StatisticsCalculator::LostSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 256 | lost_timestamps_ += num_samples; |
| 257 | } |
| 258 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 259 | void StatisticsCalculator::IncreaseCounter(size_t num_samples, int fs_hz) { |
| 260 | const int time_step_ms = |
| 261 | rtc::CheckedDivExact(static_cast<int>(1000 * num_samples), fs_hz); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 262 | delayed_packet_outage_counter_.AdvanceClock(time_step_ms); |
| 263 | excess_buffer_delay_.AdvanceClock(time_step_ms); |
Minyue Li | 34d990f | 2018-10-16 16:55:52 +0200 | [diff] [blame] | 264 | buffer_full_counter_.AdvanceClock(time_step_ms); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 265 | timestamps_since_last_report_ += static_cast<uint32_t>(num_samples); |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 266 | if (timestamps_since_last_report_ > |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 267 | static_cast<uint32_t>(fs_hz * kMaxReportPeriod)) { |
| 268 | lost_timestamps_ = 0; |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 269 | timestamps_since_last_report_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 270 | discarded_packets_ = 0; |
| 271 | } |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 272 | lifetime_stats_.total_samples_received += num_samples; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 275 | void StatisticsCalculator::JitterBufferDelay(size_t num_samples, |
| 276 | uint64_t waiting_time_ms) { |
| 277 | lifetime_stats_.jitter_buffer_delay_ms += waiting_time_ms * num_samples; |
Chen Xing | 0acffb5 | 2019-01-15 15:46:29 +0100 | [diff] [blame] | 278 | lifetime_stats_.jitter_buffer_emitted_count += num_samples; |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 279 | } |
| 280 | |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 281 | void StatisticsCalculator::SecondaryDecodedSamples(int num_samples) { |
| 282 | secondary_decoded_samples_ += num_samples; |
| 283 | } |
| 284 | |
Ivo Creusen | dc6d553 | 2018-09-27 11:43:42 +0200 | [diff] [blame] | 285 | void StatisticsCalculator::FlushedPacketBuffer() { |
| 286 | operations_and_state_.packet_buffer_flushes++; |
Minyue Li | 34d990f | 2018-10-16 16:55:52 +0200 | [diff] [blame] | 287 | buffer_full_counter_.RegisterSample(); |
Ivo Creusen | dc6d553 | 2018-09-27 11:43:42 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Jakob Ivarsson | 4450708 | 2019-03-05 16:59:03 +0100 | [diff] [blame] | 290 | void StatisticsCalculator::ReceivedPacket() { |
| 291 | ++lifetime_stats_.jitter_buffer_packets_received; |
| 292 | } |
| 293 | |
| 294 | void StatisticsCalculator::RelativePacketArrivalDelay(size_t delay_ms) { |
| 295 | lifetime_stats_.relative_packet_arrival_delay_ms += delay_ms; |
| 296 | } |
| 297 | |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 298 | void StatisticsCalculator::LogDelayedPacketOutageEvent(int num_samples, |
| 299 | int fs_hz) { |
| 300 | int outage_duration_ms = num_samples / (fs_hz / 1000); |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 301 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.DelayedPacketOutageEventMs", |
| 302 | outage_duration_ms, 1 /* min */, 2000 /* max */, |
| 303 | 100 /* bucket count */); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 304 | delayed_packet_outage_counter_.RegisterSample(); |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 305 | lifetime_stats_.delayed_packet_outage_samples += num_samples; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 306 | } |
| 307 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 308 | void StatisticsCalculator::StoreWaitingTime(int waiting_time_ms) { |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 309 | excess_buffer_delay_.RegisterSample(waiting_time_ms); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 310 | RTC_DCHECK_LE(waiting_times_.size(), kLenWaitingTimes); |
henrik.lundin | 1e346b2 | 2015-08-27 13:41:02 -0700 | [diff] [blame] | 311 | if (waiting_times_.size() == kLenWaitingTimes) { |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 312 | // Erase first value. |
| 313 | waiting_times_.pop_front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 314 | } |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 315 | waiting_times_.push_back(waiting_time_ms); |
Ivo Creusen | d1c2f78 | 2018-09-13 14:39:55 +0200 | [diff] [blame] | 316 | operations_and_state_.last_waiting_time_ms = waiting_time_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 319 | void StatisticsCalculator::GetNetworkStatistics(int fs_hz, |
| 320 | size_t num_samples_in_buffers, |
| 321 | size_t samples_per_packet, |
| 322 | NetEqNetworkStatistics* stats) { |
Henrik Lundin | dccfc40 | 2017-09-25 12:30:58 +0200 | [diff] [blame] | 323 | RTC_DCHECK_GT(fs_hz, 0); |
| 324 | RTC_DCHECK(stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 325 | |
| 326 | stats->added_zero_samples = added_zero_samples_; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 327 | stats->current_buffer_size_ms = |
| 328 | 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] | 329 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 330 | stats->packet_loss_rate = |
| 331 | CalculateQ14Ratio(lost_timestamps_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 332 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 333 | stats->accelerate_rate = |
| 334 | CalculateQ14Ratio(accelerate_samples_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 335 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 336 | stats->preemptive_rate = |
| 337 | CalculateQ14Ratio(preemptive_samples_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 338 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 339 | stats->expand_rate = |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 340 | CalculateQ14Ratio(expanded_speech_samples_ + expanded_noise_samples_, |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 341 | timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 342 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 343 | stats->speech_expand_rate = CalculateQ14Ratio(expanded_speech_samples_, |
| 344 | timestamps_since_last_report_); |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 345 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 346 | stats->secondary_decoded_rate = CalculateQ14Ratio( |
| 347 | secondary_decoded_samples_, timestamps_since_last_report_); |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 348 | |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 349 | const size_t discarded_secondary_samples = |
| 350 | discarded_secondary_packets_ * samples_per_packet; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 351 | stats->secondary_discarded_rate = |
| 352 | CalculateQ14Ratio(discarded_secondary_samples, |
| 353 | static_cast<uint32_t>(discarded_secondary_samples + |
| 354 | secondary_decoded_samples_)); |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 355 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 356 | if (waiting_times_.size() == 0) { |
| 357 | stats->mean_waiting_time_ms = -1; |
| 358 | stats->median_waiting_time_ms = -1; |
| 359 | stats->min_waiting_time_ms = -1; |
| 360 | stats->max_waiting_time_ms = -1; |
| 361 | } else { |
| 362 | std::sort(waiting_times_.begin(), waiting_times_.end()); |
| 363 | // Find mid-point elements. If the size is odd, the two values |
| 364 | // |middle_left| and |middle_right| will both be the one middle element; if |
| 365 | // the size is even, they will be the the two neighboring elements at the |
| 366 | // middle of the list. |
| 367 | const int middle_left = waiting_times_[(waiting_times_.size() - 1) / 2]; |
| 368 | const int middle_right = waiting_times_[waiting_times_.size() / 2]; |
| 369 | // Calculate the average of the two. (Works also for odd sizes.) |
| 370 | stats->median_waiting_time_ms = (middle_left + middle_right) / 2; |
| 371 | stats->min_waiting_time_ms = waiting_times_.front(); |
| 372 | stats->max_waiting_time_ms = waiting_times_.back(); |
| 373 | double sum = 0; |
| 374 | for (auto time : waiting_times_) { |
| 375 | sum += time; |
| 376 | } |
| 377 | stats->mean_waiting_time_ms = static_cast<int>(sum / waiting_times_.size()); |
| 378 | } |
| 379 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 380 | // Reset counters. |
| 381 | ResetMcu(); |
| 382 | Reset(); |
| 383 | } |
| 384 | |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 385 | NetEqLifetimeStatistics StatisticsCalculator::GetLifetimeStatistics() const { |
| 386 | return lifetime_stats_; |
| 387 | } |
| 388 | |
Ivo Creusen | d1c2f78 | 2018-09-13 14:39:55 +0200 | [diff] [blame] | 389 | NetEqOperationsAndState StatisticsCalculator::GetOperationsAndState() const { |
| 390 | return operations_and_state_; |
| 391 | } |
| 392 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 393 | uint16_t StatisticsCalculator::CalculateQ14Ratio(size_t numerator, |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 394 | uint32_t denominator) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 395 | if (numerator == 0) { |
| 396 | return 0; |
| 397 | } else if (numerator < denominator) { |
| 398 | // Ratio must be smaller than 1 in Q14. |
| 399 | assert((numerator << 14) / denominator < (1 << 14)); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 400 | return static_cast<uint16_t>((numerator << 14) / denominator); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 401 | } else { |
| 402 | // Will not produce a ratio larger than 1, since this is probably an error. |
| 403 | return 1 << 14; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | } // namespace webrtc |