blob: a3f0715f1775b223f72ca44fd21a555074095551 [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"
19#include "typedefs.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020
21namespace webrtc {
22
23// Forward declarations.
24class DecisionLogic;
25class DelayManager;
26
27// This class handles various network statistics in NetEq.
28class StatisticsCalculator {
29 public:
30 StatisticsCalculator();
31
Henrik Lundin1bb8cf82015-08-25 13:08:04 +020032 virtual ~StatisticsCalculator();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033
34 // Resets most of the counters.
35 void Reset();
36
37 // Resets the counters that are not handled by Reset().
38 void ResetMcu();
39
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040 // Reports that |num_samples| samples were produced through expansion, and
41 // that the expansion produced other than just noise samples.
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 void ExpandedVoiceSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
44 // Reports that |num_samples| samples were produced through expansion, and
45 // that the expansion produced only noise samples.
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 void ExpandedNoiseSamples(size_t num_samples);
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
49 // expansion by adding |num_samples| (negative or positive) to the current
50 // 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.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056 // Reports that |num_samples| samples were produced through preemptive
57 // expansion.
Peter Kastingdce40cf2015-08-24 14:52:23 -070058 void PreemptiveExpandedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059
60 // Reports that |num_samples| samples were removed through accelerate.
Peter Kastingdce40cf2015-08-24 14:52:23 -070061 void AcceleratedSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062
63 // Reports that |num_samples| zeros were inserted into the output.
Peter Kastingdce40cf2015-08-24 14:52:23 -070064 void AddZeros(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065
66 // Reports that |num_packets| packets were discarded.
minyue-webrtcfae474c2017-07-05 11:17:40 +020067 virtual void PacketsDiscarded(size_t num_packets);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068
minyue-webrtc0c3ca752017-08-23 15:59:38 +020069 // Reports that |num_packets| packets samples were discarded.
70 virtual void SecondaryPacketsDiscarded(size_t num_samples);
71
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072 // Reports that |num_samples| were lost.
Peter Kastingdce40cf2015-08-24 14:52:23 -070073 void LostSamples(size_t num_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074
75 // Increases the report interval counter with |num_samples| at a sample rate
Henrik Lundin1f4ffe02015-08-19 10:46:50 +020076 // of |fs_hz|. This is how the StatisticsCalculator gets notified that current
77 // time is increasing.
Peter Kastingdce40cf2015-08-24 14:52:23 -070078 void IncreaseCounter(size_t num_samples, int fs_hz);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079
80 // 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
Henrik Lundinbef77e22015-08-18 14:58:09 +020086 // Logs a delayed packet outage event of |outage_duration_ms|. A delayed
87 // packet outage event is defined as an expand period caused not by an actual
88 // packet loss, but by a delayed packet.
89 virtual void LogDelayedPacketOutageEvent(int outage_duration_ms);
90
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 // Returns the current network statistics in |stats|. The current sample rate
92 // is |fs_hz|, the total number of samples in packet buffer and sync buffer
93 // yet to play out is |num_samples_in_buffers|, and the number of samples per
94 // packet is |samples_per_packet|.
95 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 const DelayManager& delay_manager,
99 const DecisionLogic& decision_logic,
100 NetEqNetworkStatistics *stats);
101
Steve Anton2dbc69f2017-08-24 17:15:13 -0700102 // Returns a copy of this class's lifetime statistics. These statistics are
103 // never reset.
104 NetEqLifetimeStatistics GetLifetimeStatistics() const;
105
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106 private:
107 static const int kMaxReportPeriod = 60; // Seconds before auto-reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200108 static const size_t kLenWaitingTimes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200110 class PeriodicUmaLogger {
111 public:
112 PeriodicUmaLogger(const std::string& uma_name,
113 int report_interval_ms,
114 int max_value);
115 virtual ~PeriodicUmaLogger();
116 void AdvanceClock(int step_ms);
117
118 protected:
119 void LogToUma(int value) const;
120 virtual int Metric() const = 0;
121 virtual void Reset() = 0;
122
123 const std::string uma_name_;
124 const int report_interval_ms_;
125 const int max_value_;
126 int timer_ = 0;
127 };
128
129 class PeriodicUmaCount final : public PeriodicUmaLogger {
130 public:
131 PeriodicUmaCount(const std::string& uma_name,
132 int report_interval_ms,
133 int max_value);
134 ~PeriodicUmaCount() override;
135 void RegisterSample();
136
137 protected:
138 int Metric() const override;
139 void Reset() override;
140
141 private:
142 int counter_ = 0;
143 };
144
145 class PeriodicUmaAverage final : public PeriodicUmaLogger {
146 public:
147 PeriodicUmaAverage(const std::string& uma_name,
148 int report_interval_ms,
149 int max_value);
150 ~PeriodicUmaAverage() override;
151 void RegisterSample(int value);
152
153 protected:
154 int Metric() const override;
155 void Reset() override;
156
157 private:
158 double sum_ = 0.0;
159 int counter_ = 0;
160 };
161
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000162 // Calculates numerator / denominator, and returns the value in Q14.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700163 static uint16_t CalculateQ14Ratio(size_t numerator, uint32_t denominator);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000164
Steve Anton2dbc69f2017-08-24 17:15:13 -0700165 // TODO(steveanton): Add unit tests for the lifetime stats.
166 NetEqLifetimeStatistics lifetime_stats_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700167 size_t preemptive_samples_;
168 size_t accelerate_samples_;
169 size_t added_zero_samples_;
170 size_t expanded_speech_samples_;
171 size_t expanded_noise_samples_;
172 size_t discarded_packets_;
173 size_t lost_timestamps_;
henrik.lundin@webrtc.org5e3d7c72014-10-08 12:10:53 +0000174 uint32_t timestamps_since_last_report_;
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200175 std::deque<int> waiting_times_;
minyue@webrtc.org2c1bcf22015-02-17 10:17:09 +0000176 uint32_t secondary_decoded_samples_;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200177 size_t discarded_secondary_packets_;
Henrik Lundin1f4ffe02015-08-19 10:46:50 +0200178 PeriodicUmaCount delayed_packet_outage_counter_;
179 PeriodicUmaAverage excess_buffer_delay_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180
henrikg3c089d72015-09-16 05:37:44 -0700181 RTC_DISALLOW_COPY_AND_ASSIGN(StatisticsCalculator);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000182};
183
184} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200185#endif // MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_