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