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