blob: 9e5dacfd920a63998026c3bcb96bed8f33eb04a6 [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"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
20namespace webrtc {
21
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022class DelayManager;
23
24// This class handles various network statistics in NetEq.
25class StatisticsCalculator {
26 public:
27 StatisticsCalculator();
28
Henrik Lundin1bb8cf82015-08-25 13:08:04 +020029 virtual ~StatisticsCalculator();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030
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.orgd94659d2013-01-29 12:09:21 +000037 // Reports that |num_samples| samples were produced through expansion, and
38 // that the expansion produced other than just noise samples.
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +020039 void ExpandedVoiceSamples(size_t num_samples, bool is_new_concealment_event);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
41 // Reports that |num_samples| samples were produced through expansion, and
42 // that the expansion produced only noise samples.
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +020043 void ExpandedNoiseSamples(size_t num_samples, bool is_new_concealment_event);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
henrik.lundin2979f552017-05-05 05:04:16 -070045 // 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@webrtc.orgd94659d2013-01-29 12:09:21 +000053 // Reports that |num_samples| samples were produced through preemptive
54 // expansion.
Peter Kastingdce40cf2015-08-24 14:52:23 -070055 void PreemptiveExpandedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056
57 // Reports that |num_samples| samples were removed through accelerate.
Peter Kastingdce40cf2015-08-24 14:52:23 -070058 void AcceleratedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059
60 // Reports that |num_samples| zeros were inserted into the output.
Peter Kastingdce40cf2015-08-24 14:52:23 -070061 void AddZeros(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062
63 // Reports that |num_packets| packets were discarded.
minyue-webrtcfae474c2017-07-05 11:17:40 +020064 virtual void PacketsDiscarded(size_t num_packets);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065
minyue-webrtc0c3ca752017-08-23 15:59:38 +020066 // Reports that |num_packets| packets samples were discarded.
67 virtual void SecondaryPacketsDiscarded(size_t num_samples);
68
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 // Reports that |num_samples| were lost.
Peter Kastingdce40cf2015-08-24 14:52:23 -070070 void LostSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071
72 // Increases the report interval counter with |num_samples| at a sample rate
Henrik Lundin1f4ffe02015-08-19 10:46:50 +020073 // of |fs_hz|. This is how the StatisticsCalculator gets notified that current
74 // time is increasing.
Peter Kastingdce40cf2015-08-24 14:52:23 -070075 void IncreaseCounter(size_t num_samples, int fs_hz);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076
Gustaf Ullbergb0a02072017-10-02 12:00:34 +020077 // Update jitter buffer delay counter.
78 void JitterBufferDelay(size_t num_samples, uint64_t waiting_time_ms);
79
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 // Stores new packet waiting time in waiting time statistics.
81 void StoreWaitingTime(int waiting_time_ms);
82
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +000083 // Reports that |num_samples| samples were decoded from secondary packets.
84 void SecondaryDecodedSamples(int num_samples);
85
Ivo Creusendc6d5532018-09-27 11:43:42 +020086 // Rerport that the packet buffer was flushed.
87 void FlushedPacketBuffer();
88
Henrik Lundinbef77e22015-08-18 14:58:09 +020089 // Logs a delayed packet outage event of |outage_duration_ms|. A delayed
90 // packet outage event is defined as an expand period caused not by an actual
91 // packet loss, but by a delayed packet.
92 virtual void LogDelayedPacketOutageEvent(int outage_duration_ms);
93
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094 // Returns the current network statistics in |stats|. The current sample rate
95 // is |fs_hz|, the total number of samples in packet buffer and sync buffer
96 // yet to play out is |num_samples_in_buffers|, and the number of samples per
Henrik Lundindccfc402017-09-25 12:30:58 +020097 // packet is |samples_per_packet|. The method does not populate
98 // |preferred_buffer_size_ms|, |jitter_peaks_found| or |clockdrift_ppm|; use
99 // the PopulateDelayManagerStats method for those.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100 void GetNetworkStatistics(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700101 size_t num_samples_in_buffers,
102 size_t samples_per_packet,
Yves Gerey665174f2018-06-19 15:03:05 +0200103 NetEqNetworkStatistics* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104
Henrik Lundindccfc402017-09-25 12:30:58 +0200105 // Populates |preferred_buffer_size_ms|, |jitter_peaks_found| and
106 // |clockdrift_ppm| in |stats|. This is a convenience method, and does not
107 // strictly have to be in the StatisticsCalculator class, but it makes sense
108 // since all other stats fields are populated by that class.
109 static void PopulateDelayManagerStats(int ms_per_packet,
110 const DelayManager& delay_manager,
111 NetEqNetworkStatistics* stats);
112
Steve Anton2dbc69f2017-08-24 17:15:13 -0700113 // Returns a copy of this class's lifetime statistics. These statistics are
114 // never reset.
115 NetEqLifetimeStatistics GetLifetimeStatistics() const;
116
Ivo Creusend1c2f782018-09-13 14:39:55 +0200117 NetEqOperationsAndState GetOperationsAndState() const;
118
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119 private:
120 static const int kMaxReportPeriod = 60; // Seconds before auto-reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200121 static const size_t kLenWaitingTimes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200123 class PeriodicUmaLogger {
124 public:
125 PeriodicUmaLogger(const std::string& uma_name,
126 int report_interval_ms,
127 int max_value);
128 virtual ~PeriodicUmaLogger();
129 void AdvanceClock(int step_ms);
130
131 protected:
132 void LogToUma(int value) const;
133 virtual int Metric() const = 0;
134 virtual void Reset() = 0;
135
136 const std::string uma_name_;
137 const int report_interval_ms_;
138 const int max_value_;
139 int timer_ = 0;
140 };
141
142 class PeriodicUmaCount final : public PeriodicUmaLogger {
143 public:
144 PeriodicUmaCount(const std::string& uma_name,
145 int report_interval_ms,
146 int max_value);
147 ~PeriodicUmaCount() override;
148 void RegisterSample();
149
150 protected:
151 int Metric() const override;
152 void Reset() override;
153
154 private:
155 int counter_ = 0;
156 };
157
158 class PeriodicUmaAverage final : public PeriodicUmaLogger {
159 public:
160 PeriodicUmaAverage(const std::string& uma_name,
161 int report_interval_ms,
162 int max_value);
163 ~PeriodicUmaAverage() override;
164 void RegisterSample(int value);
165
166 protected:
167 int Metric() const override;
168 void Reset() override;
169
170 private:
171 double sum_ = 0.0;
172 int counter_ = 0;
173 };
174
Henrik Lundinac0a5032017-09-25 12:22:46 +0200175 // Corrects the concealed samples counter in lifetime_stats_. The value of
176 // num_samples_ is added directly to the stat if the correction is positive.
177 // If the correction is negative, it is cached and will be subtracted against
178 // future additions to the counter. This is meant to be called from
179 // Expanded{Voice,Noise}Samples{Correction}.
Alex Narest7ff6ca52018-02-07 18:46:33 +0100180 void ConcealedSamplesCorrection(int num_samples, bool is_voice);
Henrik Lundinac0a5032017-09-25 12:22:46 +0200181
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182 // Calculates numerator / denominator, and returns the value in Q14.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700183 static uint16_t CalculateQ14Ratio(size_t numerator, uint32_t denominator);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184
Steve Anton2dbc69f2017-08-24 17:15:13 -0700185 NetEqLifetimeStatistics lifetime_stats_;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200186 NetEqOperationsAndState operations_and_state_;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200187 size_t concealed_samples_correction_ = 0;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100188 size_t voice_concealed_samples_correction_ = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700189 size_t preemptive_samples_;
190 size_t accelerate_samples_;
191 size_t added_zero_samples_;
192 size_t expanded_speech_samples_;
193 size_t expanded_noise_samples_;
194 size_t discarded_packets_;
195 size_t lost_timestamps_;
henrik.lundin@webrtc.org5e3d7c72014-10-08 12:10:53 +0000196 uint32_t timestamps_since_last_report_;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200197 std::deque<int> waiting_times_;
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000198 uint32_t secondary_decoded_samples_;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200199 size_t discarded_secondary_packets_;
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200200 PeriodicUmaCount delayed_packet_outage_counter_;
201 PeriodicUmaAverage excess_buffer_delay_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202
henrikg3c089d72015-09-16 05:37:44 -0700203 RTC_DISALLOW_COPY_AND_ASSIGN(StatisticsCalculator);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204};
205
206} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200207#endif // MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_