blob: 5c2fbf36938eefa144429c64cc0414e2a533b462 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_
12#define MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
Henrik Lundin1bb8cf82015-08-25 13:08:04 +020014#include <deque>
Henrik Lundin1f4ffe02015-08-19 10:46:50 +020015#include <string>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/include/neteq.h"
18#include "rtc_base/constructormagic.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020
21namespace webrtc {
22
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023class DelayManager;
24
25// This class handles various network statistics in NetEq.
26class StatisticsCalculator {
27 public:
28 StatisticsCalculator();
29
Henrik Lundin1bb8cf82015-08-25 13:08:04 +020030 virtual ~StatisticsCalculator();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031
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.orgd94659d2013-01-29 12:09:21 +000038 // Reports that |num_samples| samples were produced through expansion, and
39 // that the expansion produced other than just noise samples.
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +020040 void ExpandedVoiceSamples(size_t num_samples, bool is_new_concealment_event);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041
42 // Reports that |num_samples| samples were produced through expansion, and
43 // that the expansion produced only noise samples.
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +020044 void ExpandedNoiseSamples(size_t num_samples, bool is_new_concealment_event);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045
henrik.lundin2979f552017-05-05 05:04:16 -070046 // 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.orgd94659d2013-01-29 12:09:21 +000054 // Reports that |num_samples| samples were produced through preemptive
55 // expansion.
Peter Kastingdce40cf2015-08-24 14:52:23 -070056 void PreemptiveExpandedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057
58 // Reports that |num_samples| samples were removed through accelerate.
Peter Kastingdce40cf2015-08-24 14:52:23 -070059 void AcceleratedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
61 // Reports that |num_samples| zeros were inserted into the output.
Peter Kastingdce40cf2015-08-24 14:52:23 -070062 void AddZeros(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063
64 // Reports that |num_packets| packets were discarded.
minyue-webrtcfae474c2017-07-05 11:17:40 +020065 virtual void PacketsDiscarded(size_t num_packets);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066
minyue-webrtc0c3ca752017-08-23 15:59:38 +020067 // Reports that |num_packets| packets samples were discarded.
68 virtual void SecondaryPacketsDiscarded(size_t num_samples);
69
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070 // Reports that |num_samples| were lost.
Peter Kastingdce40cf2015-08-24 14:52:23 -070071 void LostSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072
73 // Increases the report interval counter with |num_samples| at a sample rate
Henrik Lundin1f4ffe02015-08-19 10:46:50 +020074 // of |fs_hz|. This is how the StatisticsCalculator gets notified that current
75 // time is increasing.
Peter Kastingdce40cf2015-08-24 14:52:23 -070076 void IncreaseCounter(size_t num_samples, int fs_hz);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077
78 // Stores new packet waiting time in waiting time statistics.
79 void StoreWaitingTime(int waiting_time_ms);
80
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +000081 // Reports that |num_samples| samples were decoded from secondary packets.
82 void SecondaryDecodedSamples(int num_samples);
83
Henrik Lundinbef77e22015-08-18 14:58:09 +020084 // Logs a delayed packet outage event of |outage_duration_ms|. A delayed
85 // packet outage event is defined as an expand period caused not by an actual
86 // packet loss, but by a delayed packet.
87 virtual void LogDelayedPacketOutageEvent(int outage_duration_ms);
88
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 // Returns the current network statistics in |stats|. The current sample rate
90 // is |fs_hz|, the total number of samples in packet buffer and sync buffer
91 // yet to play out is |num_samples_in_buffers|, and the number of samples per
Henrik Lundindccfc402017-09-25 12:30:58 +020092 // packet is |samples_per_packet|. The method does not populate
93 // |preferred_buffer_size_ms|, |jitter_peaks_found| or |clockdrift_ppm|; use
94 // the PopulateDelayManagerStats method for those.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095 void GetNetworkStatistics(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -070096 size_t num_samples_in_buffers,
97 size_t samples_per_packet,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098 NetEqNetworkStatistics *stats);
99
Henrik Lundindccfc402017-09-25 12:30:58 +0200100 // Populates |preferred_buffer_size_ms|, |jitter_peaks_found| and
101 // |clockdrift_ppm| in |stats|. This is a convenience method, and does not
102 // strictly have to be in the StatisticsCalculator class, but it makes sense
103 // since all other stats fields are populated by that class.
104 static void PopulateDelayManagerStats(int ms_per_packet,
105 const DelayManager& delay_manager,
106 NetEqNetworkStatistics* stats);
107
Steve Anton2dbc69f2017-08-24 17:15:13 -0700108 // Returns a copy of this class's lifetime statistics. These statistics are
109 // never reset.
110 NetEqLifetimeStatistics GetLifetimeStatistics() const;
111
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112 private:
113 static const int kMaxReportPeriod = 60; // Seconds before auto-reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200114 static const size_t kLenWaitingTimes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200116 class PeriodicUmaLogger {
117 public:
118 PeriodicUmaLogger(const std::string& uma_name,
119 int report_interval_ms,
120 int max_value);
121 virtual ~PeriodicUmaLogger();
122 void AdvanceClock(int step_ms);
123
124 protected:
125 void LogToUma(int value) const;
126 virtual int Metric() const = 0;
127 virtual void Reset() = 0;
128
129 const std::string uma_name_;
130 const int report_interval_ms_;
131 const int max_value_;
132 int timer_ = 0;
133 };
134
135 class PeriodicUmaCount final : public PeriodicUmaLogger {
136 public:
137 PeriodicUmaCount(const std::string& uma_name,
138 int report_interval_ms,
139 int max_value);
140 ~PeriodicUmaCount() override;
141 void RegisterSample();
142
143 protected:
144 int Metric() const override;
145 void Reset() override;
146
147 private:
148 int counter_ = 0;
149 };
150
151 class PeriodicUmaAverage final : public PeriodicUmaLogger {
152 public:
153 PeriodicUmaAverage(const std::string& uma_name,
154 int report_interval_ms,
155 int max_value);
156 ~PeriodicUmaAverage() override;
157 void RegisterSample(int value);
158
159 protected:
160 int Metric() const override;
161 void Reset() override;
162
163 private:
164 double sum_ = 0.0;
165 int counter_ = 0;
166 };
167
Henrik Lundinac0a5032017-09-25 12:22:46 +0200168 // Corrects the concealed samples counter in lifetime_stats_. The value of
169 // num_samples_ is added directly to the stat if the correction is positive.
170 // If the correction is negative, it is cached and will be subtracted against
171 // future additions to the counter. This is meant to be called from
172 // Expanded{Voice,Noise}Samples{Correction}.
173 void ConcealedSamplesCorrection(int num_samples);
174
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000175 // Calculates numerator / denominator, and returns the value in Q14.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700176 static uint16_t CalculateQ14Ratio(size_t numerator, uint32_t denominator);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000177
Steve Anton2dbc69f2017-08-24 17:15:13 -0700178 NetEqLifetimeStatistics lifetime_stats_;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200179 size_t concealed_samples_correction_ = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700180 size_t preemptive_samples_;
181 size_t accelerate_samples_;
182 size_t added_zero_samples_;
183 size_t expanded_speech_samples_;
184 size_t expanded_noise_samples_;
185 size_t discarded_packets_;
186 size_t lost_timestamps_;
henrik.lundin@webrtc.org5e3d7c72014-10-08 12:10:53 +0000187 uint32_t timestamps_since_last_report_;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200188 std::deque<int> waiting_times_;
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000189 uint32_t secondary_decoded_samples_;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200190 size_t discarded_secondary_packets_;
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200191 PeriodicUmaCount delayed_packet_outage_counter_;
192 PeriodicUmaAverage excess_buffer_delay_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193
henrikg3c089d72015-09-16 05:37:44 -0700194 RTC_DISALLOW_COPY_AND_ASSIGN(StatisticsCalculator);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000195};
196
197} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200198#endif // MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_