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/decision_logic.h" |
| 18 | #include "modules/audio_coding/neteq/delay_manager.h" |
| 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/safe_conversions.h" |
| 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 | } |
| 33 | } // namespace |
| 34 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 35 | // Allocating the static const so that it can be passed by reference to |
| 36 | // RTC_DCHECK. |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 37 | const size_t StatisticsCalculator::kLenWaitingTimes; |
| 38 | |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 39 | StatisticsCalculator::PeriodicUmaLogger::PeriodicUmaLogger( |
| 40 | const std::string& uma_name, |
| 41 | int report_interval_ms, |
| 42 | int max_value) |
| 43 | : uma_name_(uma_name), |
| 44 | report_interval_ms_(report_interval_ms), |
| 45 | max_value_(max_value), |
| 46 | timer_(0) { |
| 47 | } |
| 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) |
| 70 | : PeriodicUmaLogger(uma_name, report_interval_ms, max_value) { |
| 71 | } |
| 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) |
| 94 | : PeriodicUmaLogger(uma_name, report_interval_ms, max_value) { |
| 95 | } |
| 96 | |
| 97 | StatisticsCalculator::PeriodicUmaAverage::~PeriodicUmaAverage() { |
| 98 | // Log the average for the current (incomplete) interval. |
| 99 | LogToUma(Metric()); |
| 100 | } |
| 101 | |
| 102 | void StatisticsCalculator::PeriodicUmaAverage::RegisterSample(int value) { |
| 103 | sum_ += value; |
| 104 | ++counter_; |
| 105 | } |
| 106 | |
| 107 | int StatisticsCalculator::PeriodicUmaAverage::Metric() const { |
henrik.lundin | e594213 | 2016-02-09 00:35:53 -0800 | [diff] [blame] | 108 | return counter_ == 0 ? 0 : static_cast<int>(sum_ / counter_); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void StatisticsCalculator::PeriodicUmaAverage::Reset() { |
| 112 | sum_ = 0.0; |
| 113 | counter_ = 0; |
| 114 | } |
| 115 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | StatisticsCalculator::StatisticsCalculator() |
| 117 | : preemptive_samples_(0), |
| 118 | accelerate_samples_(0), |
| 119 | added_zero_samples_(0), |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 120 | expanded_speech_samples_(0), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 121 | expanded_noise_samples_(0), |
| 122 | discarded_packets_(0), |
| 123 | lost_timestamps_(0), |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 124 | timestamps_since_last_report_(0), |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 125 | secondary_decoded_samples_(0), |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 126 | discarded_secondary_packets_(0), |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 127 | delayed_packet_outage_counter_( |
| 128 | "WebRTC.Audio.DelayedPacketOutageEventsPerMinute", |
| 129 | 60000, // 60 seconds report interval. |
| 130 | 100), |
| 131 | excess_buffer_delay_("WebRTC.Audio.AverageExcessBufferDelayMs", |
| 132 | 60000, // 60 seconds report interval. |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 133 | 1000) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 134 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 135 | StatisticsCalculator::~StatisticsCalculator() = default; |
| 136 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 137 | void StatisticsCalculator::Reset() { |
| 138 | preemptive_samples_ = 0; |
| 139 | accelerate_samples_ = 0; |
| 140 | added_zero_samples_ = 0; |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 141 | expanded_speech_samples_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 142 | expanded_noise_samples_ = 0; |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 143 | secondary_decoded_samples_ = 0; |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 144 | discarded_secondary_packets_ = 0; |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 145 | waiting_times_.clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void StatisticsCalculator::ResetMcu() { |
| 149 | discarded_packets_ = 0; |
| 150 | lost_timestamps_ = 0; |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 151 | timestamps_since_last_report_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame^] | 154 | void StatisticsCalculator::ExpandedVoiceSamples(size_t num_samples, |
| 155 | bool is_new_concealment_event) { |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 156 | expanded_speech_samples_ += num_samples; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 157 | lifetime_stats_.concealed_samples += num_samples; |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame^] | 158 | lifetime_stats_.concealment_events += is_new_concealment_event; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame^] | 161 | void StatisticsCalculator::ExpandedNoiseSamples(size_t num_samples, |
| 162 | bool is_new_concealment_event) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 163 | expanded_noise_samples_ += num_samples; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 164 | lifetime_stats_.concealed_samples += num_samples; |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame^] | 165 | lifetime_stats_.concealment_events += is_new_concealment_event; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 166 | } |
| 167 | |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 168 | void StatisticsCalculator::ExpandedVoiceSamplesCorrection(int num_samples) { |
| 169 | expanded_speech_samples_ = |
| 170 | AddIntToSizeTWithLowerCap(num_samples, expanded_speech_samples_); |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 171 | lifetime_stats_.concealed_samples += num_samples; |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void StatisticsCalculator::ExpandedNoiseSamplesCorrection(int num_samples) { |
| 175 | expanded_noise_samples_ = |
| 176 | AddIntToSizeTWithLowerCap(num_samples, expanded_noise_samples_); |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 177 | lifetime_stats_.concealed_samples += num_samples; |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 180 | void StatisticsCalculator::PreemptiveExpandedSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 181 | preemptive_samples_ += num_samples; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 182 | lifetime_stats_.concealed_samples += num_samples; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 185 | void StatisticsCalculator::AcceleratedSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 186 | accelerate_samples_ += num_samples; |
| 187 | } |
| 188 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 189 | void StatisticsCalculator::AddZeros(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 190 | added_zero_samples_ += num_samples; |
| 191 | } |
| 192 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 193 | void StatisticsCalculator::PacketsDiscarded(size_t num_packets) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 194 | discarded_packets_ += num_packets; |
| 195 | } |
| 196 | |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 197 | void StatisticsCalculator::SecondaryPacketsDiscarded(size_t num_packets) { |
| 198 | discarded_secondary_packets_ += num_packets; |
| 199 | } |
| 200 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 201 | void StatisticsCalculator::LostSamples(size_t num_samples) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 202 | lost_timestamps_ += num_samples; |
| 203 | } |
| 204 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 205 | void StatisticsCalculator::IncreaseCounter(size_t num_samples, int fs_hz) { |
| 206 | const int time_step_ms = |
| 207 | rtc::CheckedDivExact(static_cast<int>(1000 * num_samples), fs_hz); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 208 | delayed_packet_outage_counter_.AdvanceClock(time_step_ms); |
| 209 | excess_buffer_delay_.AdvanceClock(time_step_ms); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 210 | timestamps_since_last_report_ += static_cast<uint32_t>(num_samples); |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 211 | if (timestamps_since_last_report_ > |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 212 | static_cast<uint32_t>(fs_hz * kMaxReportPeriod)) { |
| 213 | lost_timestamps_ = 0; |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 214 | timestamps_since_last_report_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 215 | discarded_packets_ = 0; |
| 216 | } |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 217 | lifetime_stats_.total_samples_received += num_samples; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 218 | } |
| 219 | |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 220 | void StatisticsCalculator::SecondaryDecodedSamples(int num_samples) { |
| 221 | secondary_decoded_samples_ += num_samples; |
| 222 | } |
| 223 | |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 224 | void StatisticsCalculator::LogDelayedPacketOutageEvent(int outage_duration_ms) { |
asapersson | a2c58e2 | 2016-03-07 01:52:59 -0800 | [diff] [blame] | 225 | RTC_HISTOGRAM_COUNTS("WebRTC.Audio.DelayedPacketOutageEventMs", |
| 226 | outage_duration_ms, 1 /* min */, 2000 /* max */, |
| 227 | 100 /* bucket count */); |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 228 | delayed_packet_outage_counter_.RegisterSample(); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 229 | } |
| 230 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 231 | void StatisticsCalculator::StoreWaitingTime(int waiting_time_ms) { |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 232 | excess_buffer_delay_.RegisterSample(waiting_time_ms); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 233 | RTC_DCHECK_LE(waiting_times_.size(), kLenWaitingTimes); |
henrik.lundin | 1e346b2 | 2015-08-27 13:41:02 -0700 | [diff] [blame] | 234 | if (waiting_times_.size() == kLenWaitingTimes) { |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 235 | // Erase first value. |
| 236 | waiting_times_.pop_front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 237 | } |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 238 | waiting_times_.push_back(waiting_time_ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void StatisticsCalculator::GetNetworkStatistics( |
| 242 | int fs_hz, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 243 | size_t num_samples_in_buffers, |
| 244 | size_t samples_per_packet, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 245 | const DelayManager& delay_manager, |
| 246 | const DecisionLogic& decision_logic, |
| 247 | NetEqNetworkStatistics *stats) { |
| 248 | if (fs_hz <= 0 || !stats) { |
| 249 | assert(false); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | stats->added_zero_samples = added_zero_samples_; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 254 | stats->current_buffer_size_ms = |
| 255 | static_cast<uint16_t>(num_samples_in_buffers * 1000 / fs_hz); |
kwiberg | d3edd77 | 2017-03-01 18:52:48 -0800 | [diff] [blame] | 256 | const int ms_per_packet = rtc::dchecked_cast<int>( |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 257 | decision_logic.packet_length_samples() / (fs_hz / 1000)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 258 | stats->preferred_buffer_size_ms = (delay_manager.TargetLevel() >> 8) * |
| 259 | ms_per_packet; |
| 260 | stats->jitter_peaks_found = delay_manager.PeakFound(); |
henrik.lundin | 0d83857 | 2016-10-13 03:35:55 -0700 | [diff] [blame] | 261 | stats->clockdrift_ppm = |
| 262 | rtc::saturated_cast<int32_t>(delay_manager.EstimatedClockDriftPpm()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 263 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 264 | stats->packet_loss_rate = |
| 265 | CalculateQ14Ratio(lost_timestamps_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 266 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 267 | stats->accelerate_rate = |
| 268 | CalculateQ14Ratio(accelerate_samples_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 269 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 270 | stats->preemptive_rate = |
| 271 | CalculateQ14Ratio(preemptive_samples_, timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 272 | |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 273 | stats->expand_rate = |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 274 | CalculateQ14Ratio(expanded_speech_samples_ + expanded_noise_samples_, |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 275 | timestamps_since_last_report_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 276 | |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 277 | stats->speech_expand_rate = |
| 278 | CalculateQ14Ratio(expanded_speech_samples_, |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 279 | timestamps_since_last_report_); |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 280 | |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 281 | stats->secondary_decoded_rate = |
| 282 | CalculateQ14Ratio(secondary_decoded_samples_, |
| 283 | timestamps_since_last_report_); |
| 284 | |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 285 | const size_t discarded_secondary_samples = |
| 286 | discarded_secondary_packets_ * samples_per_packet; |
| 287 | stats->secondary_discarded_rate = CalculateQ14Ratio( |
| 288 | discarded_secondary_samples, |
lliuu | ff2824b | 2017-08-28 13:01:45 -0700 | [diff] [blame] | 289 | static_cast<uint32_t>(discarded_secondary_samples + |
| 290 | secondary_decoded_samples_)); |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 291 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 292 | if (waiting_times_.size() == 0) { |
| 293 | stats->mean_waiting_time_ms = -1; |
| 294 | stats->median_waiting_time_ms = -1; |
| 295 | stats->min_waiting_time_ms = -1; |
| 296 | stats->max_waiting_time_ms = -1; |
| 297 | } else { |
| 298 | std::sort(waiting_times_.begin(), waiting_times_.end()); |
| 299 | // Find mid-point elements. If the size is odd, the two values |
| 300 | // |middle_left| and |middle_right| will both be the one middle element; if |
| 301 | // the size is even, they will be the the two neighboring elements at the |
| 302 | // middle of the list. |
| 303 | const int middle_left = waiting_times_[(waiting_times_.size() - 1) / 2]; |
| 304 | const int middle_right = waiting_times_[waiting_times_.size() / 2]; |
| 305 | // Calculate the average of the two. (Works also for odd sizes.) |
| 306 | stats->median_waiting_time_ms = (middle_left + middle_right) / 2; |
| 307 | stats->min_waiting_time_ms = waiting_times_.front(); |
| 308 | stats->max_waiting_time_ms = waiting_times_.back(); |
| 309 | double sum = 0; |
| 310 | for (auto time : waiting_times_) { |
| 311 | sum += time; |
| 312 | } |
| 313 | stats->mean_waiting_time_ms = static_cast<int>(sum / waiting_times_.size()); |
| 314 | } |
| 315 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 316 | // Reset counters. |
| 317 | ResetMcu(); |
| 318 | Reset(); |
| 319 | } |
| 320 | |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 321 | NetEqLifetimeStatistics StatisticsCalculator::GetLifetimeStatistics() const { |
| 322 | return lifetime_stats_; |
| 323 | } |
| 324 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 325 | uint16_t StatisticsCalculator::CalculateQ14Ratio(size_t numerator, |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 326 | uint32_t denominator) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 327 | if (numerator == 0) { |
| 328 | return 0; |
| 329 | } else if (numerator < denominator) { |
| 330 | // Ratio must be smaller than 1 in Q14. |
| 331 | assert((numerator << 14) / denominator < (1 << 14)); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 332 | return static_cast<uint16_t>((numerator << 14) / denominator); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 333 | } else { |
| 334 | // Will not produce a ratio larger than 1, since this is probably an error. |
| 335 | return 1 << 14; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | } // namespace webrtc |