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