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 | #ifndef MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 14 | #include <deque> |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 15 | #include <string> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame^] | 17 | #include "api/neteq/neteq.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/constructor_magic.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | class DelayManager; |
| 23 | |
| 24 | // This class handles various network statistics in NetEq. |
| 25 | class StatisticsCalculator { |
| 26 | public: |
| 27 | StatisticsCalculator(); |
| 28 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 29 | virtual ~StatisticsCalculator(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 30 | |
| 31 | // Resets most of the counters. |
| 32 | void Reset(); |
| 33 | |
| 34 | // Resets the counters that are not handled by Reset(). |
| 35 | void ResetMcu(); |
| 36 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 37 | // Reports that |num_samples| samples were produced through expansion, and |
| 38 | // that the expansion produced other than just noise samples. |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 39 | void ExpandedVoiceSamples(size_t num_samples, bool is_new_concealment_event); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 40 | |
| 41 | // Reports that |num_samples| samples were produced through expansion, and |
| 42 | // that the expansion produced only noise samples. |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 43 | void ExpandedNoiseSamples(size_t num_samples, bool is_new_concealment_event); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | |
henrik.lundin | 2979f55 | 2017-05-05 05:04:16 -0700 | [diff] [blame] | 45 | // Corrects the statistics for number of samples produced through non-noise |
| 46 | // expansion by adding |num_samples| (negative or positive) to the current |
| 47 | // value. The result is capped to zero to avoid negative values. |
| 48 | void ExpandedVoiceSamplesCorrection(int num_samples); |
| 49 | |
| 50 | // Same as ExpandedVoiceSamplesCorrection but for noise samples. |
| 51 | void ExpandedNoiseSamplesCorrection(int num_samples); |
| 52 | |
Henrik Lundin | 2a8bd09 | 2019-04-26 09:47:07 +0200 | [diff] [blame] | 53 | void DecodedOutputPlayed(); |
| 54 | |
| 55 | // Mark end of expand event; triggers some stats to be reported. |
| 56 | void EndExpandEvent(int fs_hz); |
| 57 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 58 | // Reports that |num_samples| samples were produced through preemptive |
| 59 | // expansion. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 60 | void PreemptiveExpandedSamples(size_t num_samples); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 61 | |
| 62 | // Reports that |num_samples| samples were removed through accelerate. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 63 | void AcceleratedSamples(size_t num_samples); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | |
| 65 | // Reports that |num_samples| zeros were inserted into the output. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 66 | void AddZeros(size_t num_samples); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 67 | |
| 68 | // Reports that |num_packets| packets were discarded. |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 69 | virtual void PacketsDiscarded(size_t num_packets); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 70 | |
Ivo Creusen | bf4a221 | 2019-04-24 14:06:24 +0200 | [diff] [blame] | 71 | // Reports that |num_packets| secondary (FEC) packets were discarded. |
| 72 | virtual void SecondaryPacketsDiscarded(size_t num_packets); |
| 73 | |
| 74 | // Reports that |num_packets| secondary (FEC) packets were received. |
| 75 | virtual void SecondaryPacketsReceived(size_t num_packets); |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 76 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | // Reports that |num_samples| were lost. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 78 | void LostSamples(size_t num_samples); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 79 | |
| 80 | // Increases the report interval counter with |num_samples| at a sample rate |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 81 | // of |fs_hz|. This is how the StatisticsCalculator gets notified that current |
| 82 | // time is increasing. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 83 | void IncreaseCounter(size_t num_samples, int fs_hz); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 84 | |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 85 | // Update jitter buffer delay counter. |
| 86 | void JitterBufferDelay(size_t num_samples, uint64_t waiting_time_ms); |
| 87 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 88 | // Stores new packet waiting time in waiting time statistics. |
| 89 | void StoreWaitingTime(int waiting_time_ms); |
| 90 | |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 91 | // Reports that |num_samples| samples were decoded from secondary packets. |
| 92 | void SecondaryDecodedSamples(int num_samples); |
| 93 | |
Jakob Ivarsson | 4450708 | 2019-03-05 16:59:03 +0100 | [diff] [blame] | 94 | // Reports that the packet buffer was flushed. |
Ivo Creusen | dc6d553 | 2018-09-27 11:43:42 +0200 | [diff] [blame] | 95 | void FlushedPacketBuffer(); |
| 96 | |
Jakob Ivarsson | 4450708 | 2019-03-05 16:59:03 +0100 | [diff] [blame] | 97 | // Reports that the jitter buffer received a packet. |
| 98 | void ReceivedPacket(); |
| 99 | |
| 100 | // Reports that a received packet was delayed by |delay_ms| milliseconds. |
| 101 | virtual void RelativePacketArrivalDelay(size_t delay_ms); |
| 102 | |
Jakob Ivarsson | 352ce5c | 2018-11-27 12:52:16 +0100 | [diff] [blame] | 103 | // Logs a delayed packet outage event of |num_samples| expanded at a sample |
| 104 | // rate of |fs_hz|. A delayed packet outage event is defined as an expand |
| 105 | // period caused not by an actual packet loss, but by a delayed packet. |
| 106 | virtual void LogDelayedPacketOutageEvent(int num_samples, int fs_hz); |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 107 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 108 | // Returns the current network statistics in |stats|. The current sample rate |
| 109 | // is |fs_hz|, the total number of samples in packet buffer and sync buffer |
| 110 | // yet to play out is |num_samples_in_buffers|, and the number of samples per |
Henrik Lundin | dccfc40 | 2017-09-25 12:30:58 +0200 | [diff] [blame] | 111 | // packet is |samples_per_packet|. The method does not populate |
| 112 | // |preferred_buffer_size_ms|, |jitter_peaks_found| or |clockdrift_ppm|; use |
| 113 | // the PopulateDelayManagerStats method for those. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 114 | void GetNetworkStatistics(int fs_hz, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 115 | size_t num_samples_in_buffers, |
| 116 | size_t samples_per_packet, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 117 | NetEqNetworkStatistics* stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 118 | |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 119 | // Returns a copy of this class's lifetime statistics. These statistics are |
| 120 | // never reset. |
| 121 | NetEqLifetimeStatistics GetLifetimeStatistics() const; |
| 122 | |
Ivo Creusen | d1c2f78 | 2018-09-13 14:39:55 +0200 | [diff] [blame] | 123 | NetEqOperationsAndState GetOperationsAndState() const; |
| 124 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 125 | private: |
| 126 | static const int kMaxReportPeriod = 60; // Seconds before auto-reset. |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 127 | static const size_t kLenWaitingTimes = 100; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 128 | |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 129 | class PeriodicUmaLogger { |
| 130 | public: |
| 131 | PeriodicUmaLogger(const std::string& uma_name, |
| 132 | int report_interval_ms, |
| 133 | int max_value); |
| 134 | virtual ~PeriodicUmaLogger(); |
| 135 | void AdvanceClock(int step_ms); |
| 136 | |
| 137 | protected: |
| 138 | void LogToUma(int value) const; |
| 139 | virtual int Metric() const = 0; |
| 140 | virtual void Reset() = 0; |
| 141 | |
| 142 | const std::string uma_name_; |
| 143 | const int report_interval_ms_; |
| 144 | const int max_value_; |
| 145 | int timer_ = 0; |
| 146 | }; |
| 147 | |
| 148 | class PeriodicUmaCount final : public PeriodicUmaLogger { |
| 149 | public: |
| 150 | PeriodicUmaCount(const std::string& uma_name, |
| 151 | int report_interval_ms, |
| 152 | int max_value); |
| 153 | ~PeriodicUmaCount() override; |
| 154 | void RegisterSample(); |
| 155 | |
| 156 | protected: |
| 157 | int Metric() const override; |
| 158 | void Reset() override; |
| 159 | |
| 160 | private: |
| 161 | int counter_ = 0; |
| 162 | }; |
| 163 | |
| 164 | class PeriodicUmaAverage final : public PeriodicUmaLogger { |
| 165 | public: |
| 166 | PeriodicUmaAverage(const std::string& uma_name, |
| 167 | int report_interval_ms, |
| 168 | int max_value); |
| 169 | ~PeriodicUmaAverage() override; |
| 170 | void RegisterSample(int value); |
| 171 | |
| 172 | protected: |
| 173 | int Metric() const override; |
| 174 | void Reset() override; |
| 175 | |
| 176 | private: |
| 177 | double sum_ = 0.0; |
| 178 | int counter_ = 0; |
| 179 | }; |
| 180 | |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 181 | // Corrects the concealed samples counter in lifetime_stats_. The value of |
| 182 | // num_samples_ is added directly to the stat if the correction is positive. |
| 183 | // If the correction is negative, it is cached and will be subtracted against |
| 184 | // future additions to the counter. This is meant to be called from |
| 185 | // Expanded{Voice,Noise}Samples{Correction}. |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 186 | void ConcealedSamplesCorrection(int num_samples, bool is_voice); |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 187 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 188 | // Calculates numerator / denominator, and returns the value in Q14. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 189 | static uint16_t CalculateQ14Ratio(size_t numerator, uint32_t denominator); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 190 | |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 191 | NetEqLifetimeStatistics lifetime_stats_; |
Ivo Creusen | d1c2f78 | 2018-09-13 14:39:55 +0200 | [diff] [blame] | 192 | NetEqOperationsAndState operations_and_state_; |
Henrik Lundin | ac0a503 | 2017-09-25 12:22:46 +0200 | [diff] [blame] | 193 | size_t concealed_samples_correction_ = 0; |
Ivo Creusen | bf4a221 | 2019-04-24 14:06:24 +0200 | [diff] [blame] | 194 | size_t silent_concealed_samples_correction_ = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 195 | size_t preemptive_samples_; |
| 196 | size_t accelerate_samples_; |
| 197 | size_t added_zero_samples_; |
| 198 | size_t expanded_speech_samples_; |
| 199 | size_t expanded_noise_samples_; |
Henrik Lundin | 2a8bd09 | 2019-04-26 09:47:07 +0200 | [diff] [blame] | 200 | size_t concealed_samples_at_event_end_ = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 201 | size_t discarded_packets_; |
| 202 | size_t lost_timestamps_; |
henrik.lundin@webrtc.org | 5e3d7c7 | 2014-10-08 12:10:53 +0000 | [diff] [blame] | 203 | uint32_t timestamps_since_last_report_; |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 204 | std::deque<int> waiting_times_; |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 205 | uint32_t secondary_decoded_samples_; |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 206 | size_t discarded_secondary_packets_; |
Henrik Lundin | 1f4ffe0 | 2015-08-19 10:46:50 +0200 | [diff] [blame] | 207 | PeriodicUmaCount delayed_packet_outage_counter_; |
| 208 | PeriodicUmaAverage excess_buffer_delay_; |
Minyue Li | 34d990f | 2018-10-16 16:55:52 +0200 | [diff] [blame] | 209 | PeriodicUmaCount buffer_full_counter_; |
Henrik Lundin | 2a8bd09 | 2019-04-26 09:47:07 +0200 | [diff] [blame] | 210 | bool decoded_output_played_ = false; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 211 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 212 | RTC_DISALLOW_COPY_AND_ASSIGN(StatisticsCalculator); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 216 | #endif // MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_ |