blob: 33a22d02ddbd8424cb0ae39aecbdd9d1e8501dfc [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
Ali Tofigh714e3cb2022-07-20 12:53:07 +020017#include "absl/strings/string_view.h"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010018#include "api/neteq/neteq.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
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090031 StatisticsCalculator(const StatisticsCalculator&) = delete;
32 StatisticsCalculator& operator=(const StatisticsCalculator&) = delete;
33
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034 // Resets most of the counters.
35 void Reset();
36
37 // Resets the counters that are not handled by Reset().
38 void ResetMcu();
39
Artem Titovd00ce742021-07-28 20:00:17 +020040 // Reports that `num_samples` samples were produced through expansion, and
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041 // that the expansion produced other than just noise samples.
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +020042 void ExpandedVoiceSamples(size_t num_samples, bool is_new_concealment_event);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
Artem Titovd00ce742021-07-28 20:00:17 +020044 // Reports that `num_samples` samples were produced through expansion, and
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045 // that the expansion produced only noise samples.
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +020046 void ExpandedNoiseSamples(size_t num_samples, bool is_new_concealment_event);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
henrik.lundin2979f552017-05-05 05:04:16 -070048 // Corrects the statistics for number of samples produced through non-noise
Artem Titovd00ce742021-07-28 20:00:17 +020049 // expansion by adding `num_samples` (negative or positive) to the current
henrik.lundin2979f552017-05-05 05:04:16 -070050 // value. The result is capped to zero to avoid negative values.
51 void ExpandedVoiceSamplesCorrection(int num_samples);
52
53 // Same as ExpandedVoiceSamplesCorrection but for noise samples.
54 void ExpandedNoiseSamplesCorrection(int num_samples);
55
Henrik Lundin2a8bd092019-04-26 09:47:07 +020056 void DecodedOutputPlayed();
57
58 // Mark end of expand event; triggers some stats to be reported.
59 void EndExpandEvent(int fs_hz);
60
Artem Titovd00ce742021-07-28 20:00:17 +020061 // Reports that `num_samples` samples were produced through preemptive
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062 // expansion.
Peter Kastingdce40cf2015-08-24 14:52:23 -070063 void PreemptiveExpandedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064
Artem Titovd00ce742021-07-28 20:00:17 +020065 // Reports that `num_samples` samples were removed through accelerate.
Peter Kastingdce40cf2015-08-24 14:52:23 -070066 void AcceleratedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067
Jakob Ivarsson098c4ea2022-04-18 20:31:51 +020068 // Reports that `num_samples` comfort noise samples were generated.
69 void GeneratedNoiseSamples(size_t num_samples);
70
Artem Titovd00ce742021-07-28 20:00:17 +020071 // Reports that `num_packets` packets were discarded.
minyue-webrtcfae474c2017-07-05 11:17:40 +020072 virtual void PacketsDiscarded(size_t num_packets);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073
Artem Titovd00ce742021-07-28 20:00:17 +020074 // Reports that `num_packets` secondary (FEC) packets were discarded.
Ivo Creusenbf4a2212019-04-24 14:06:24 +020075 virtual void SecondaryPacketsDiscarded(size_t num_packets);
76
Artem Titovd00ce742021-07-28 20:00:17 +020077 // Reports that `num_packets` secondary (FEC) packets were received.
Ivo Creusenbf4a2212019-04-24 14:06:24 +020078 virtual void SecondaryPacketsReceived(size_t num_packets);
minyue-webrtc0c3ca752017-08-23 15:59:38 +020079
Artem Titovd00ce742021-07-28 20:00:17 +020080 // Increases the report interval counter with `num_samples` at a sample rate
81 // of `fs_hz`. This is how the StatisticsCalculator gets notified that current
Henrik Lundin1f4ffe02015-08-19 10:46:50 +020082 // time is increasing.
Peter Kastingdce40cf2015-08-24 14:52:23 -070083 void IncreaseCounter(size_t num_samples, int fs_hz);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084
Gustaf Ullbergb0a02072017-10-02 12:00:34 +020085 // Update jitter buffer delay counter.
Artem Titove618cc92020-03-11 11:18:54 +010086 void JitterBufferDelay(size_t num_samples,
87 uint64_t waiting_time_ms,
Ivo Creusen1a84b562022-07-19 16:33:10 +020088 uint64_t target_delay_ms,
89 uint64_t unlimited_target_delay_ms);
Gustaf Ullbergb0a02072017-10-02 12:00:34 +020090
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 // Stores new packet waiting time in waiting time statistics.
92 void StoreWaitingTime(int waiting_time_ms);
93
Artem Titovd00ce742021-07-28 20:00:17 +020094 // Reports that `num_samples` samples were decoded from secondary packets.
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +000095 void SecondaryDecodedSamples(int num_samples);
96
Jakob Ivarsson44507082019-03-05 16:59:03 +010097 // Reports that the packet buffer was flushed.
Ivo Creusendc6d5532018-09-27 11:43:42 +020098 void FlushedPacketBuffer();
99
Jakob Ivarsson44507082019-03-05 16:59:03 +0100100 // Reports that the jitter buffer received a packet.
101 void ReceivedPacket();
102
Artem Titovd00ce742021-07-28 20:00:17 +0200103 // Reports that a received packet was delayed by `delay_ms` milliseconds.
Jakob Ivarsson44507082019-03-05 16:59:03 +0100104 virtual void RelativePacketArrivalDelay(size_t delay_ms);
105
Artem Titovd00ce742021-07-28 20:00:17 +0200106 // Logs a delayed packet outage event of `num_samples` expanded at a sample
107 // rate of `fs_hz`. A delayed packet outage event is defined as an expand
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100108 // period caused not by an actual packet loss, but by a delayed packet.
109 virtual void LogDelayedPacketOutageEvent(int num_samples, int fs_hz);
Henrik Lundinbef77e22015-08-18 14:58:09 +0200110
Artem Titovd00ce742021-07-28 20:00:17 +0200111 // Returns the current network statistics in `stats`. The number of samples
112 // per packet is `samples_per_packet`. The method does not populate
113 // `preferred_buffer_size_ms`, `jitter_peaks_found` or `clockdrift_ppm`; use
Henrik Lundindccfc402017-09-25 12:30:58 +0200114 // the PopulateDelayManagerStats method for those.
Niels Möller6b4d9622020-09-14 10:47:50 +0200115 void GetNetworkStatistics(size_t samples_per_packet,
Yves Gerey665174f2018-06-19 15:03:05 +0200116 NetEqNetworkStatistics* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117
Steve Anton2dbc69f2017-08-24 17:15:13 -0700118 // Returns a copy of this class's lifetime statistics. These statistics are
119 // never reset.
120 NetEqLifetimeStatistics GetLifetimeStatistics() const;
121
Ivo Creusend1c2f782018-09-13 14:39:55 +0200122 NetEqOperationsAndState GetOperationsAndState() const;
123
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124 private:
125 static const int kMaxReportPeriod = 60; // Seconds before auto-reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200126 static const size_t kLenWaitingTimes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000127
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200128 class PeriodicUmaLogger {
129 public:
Ali Tofigh714e3cb2022-07-20 12:53:07 +0200130 PeriodicUmaLogger(absl::string_view uma_name,
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200131 int report_interval_ms,
132 int max_value);
133 virtual ~PeriodicUmaLogger();
134 void AdvanceClock(int step_ms);
135
136 protected:
137 void LogToUma(int value) const;
138 virtual int Metric() const = 0;
139 virtual void Reset() = 0;
140
141 const std::string uma_name_;
142 const int report_interval_ms_;
143 const int max_value_;
144 int timer_ = 0;
145 };
146
147 class PeriodicUmaCount final : public PeriodicUmaLogger {
148 public:
Ali Tofigh714e3cb2022-07-20 12:53:07 +0200149 PeriodicUmaCount(absl::string_view uma_name,
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200150 int report_interval_ms,
151 int max_value);
152 ~PeriodicUmaCount() override;
153 void RegisterSample();
154
155 protected:
156 int Metric() const override;
157 void Reset() override;
158
159 private:
160 int counter_ = 0;
161 };
162
163 class PeriodicUmaAverage final : public PeriodicUmaLogger {
164 public:
Ali Tofigh714e3cb2022-07-20 12:53:07 +0200165 PeriodicUmaAverage(absl::string_view uma_name,
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200166 int report_interval_ms,
167 int max_value);
168 ~PeriodicUmaAverage() override;
169 void RegisterSample(int value);
170
171 protected:
172 int Metric() const override;
173 void Reset() override;
174
175 private:
176 double sum_ = 0.0;
177 int counter_ = 0;
178 };
179
Henrik Lundinac0a5032017-09-25 12:22:46 +0200180 // Corrects the concealed samples counter in lifetime_stats_. The value of
181 // num_samples_ is added directly to the stat if the correction is positive.
182 // If the correction is negative, it is cached and will be subtracted against
183 // future additions to the counter. This is meant to be called from
184 // Expanded{Voice,Noise}Samples{Correction}.
Alex Narest7ff6ca52018-02-07 18:46:33 +0100185 void ConcealedSamplesCorrection(int num_samples, bool is_voice);
Henrik Lundinac0a5032017-09-25 12:22:46 +0200186
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 // Calculates numerator / denominator, and returns the value in Q14.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700188 static uint16_t CalculateQ14Ratio(size_t numerator, uint32_t denominator);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189
Steve Anton2dbc69f2017-08-24 17:15:13 -0700190 NetEqLifetimeStatistics lifetime_stats_;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200191 NetEqOperationsAndState operations_and_state_;
Henrik Lundinac0a5032017-09-25 12:22:46 +0200192 size_t concealed_samples_correction_ = 0;
Ivo Creusenbf4a2212019-04-24 14:06:24 +0200193 size_t silent_concealed_samples_correction_ = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700194 size_t preemptive_samples_;
195 size_t accelerate_samples_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700196 size_t expanded_speech_samples_;
197 size_t expanded_noise_samples_;
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200198 size_t concealed_samples_at_event_end_ = 0;
henrik.lundin@webrtc.org5e3d7c72014-10-08 12:10:53 +0000199 uint32_t timestamps_since_last_report_;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200200 std::deque<int> waiting_times_;
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000201 uint32_t secondary_decoded_samples_;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200202 size_t discarded_secondary_packets_;
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200203 PeriodicUmaCount delayed_packet_outage_counter_;
204 PeriodicUmaAverage excess_buffer_delay_;
Minyue Li34d990f2018-10-16 16:55:52 +0200205 PeriodicUmaCount buffer_full_counter_;
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200206 bool decoded_output_played_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207};
208
209} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200210#endif // MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_