tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

statistics_calculator.h (7825B)


      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 
     11 #ifndef MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_
     12 #define MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <deque>
     17 #include <string>
     18 
     19 #include "absl/strings/string_view.h"
     20 #include "api/neteq/neteq.h"
     21 #include "api/neteq/tick_timer.h"
     22 #include "modules/audio_coding/neteq/expand_uma_logger.h"
     23 
     24 namespace webrtc {
     25 
     26 class DelayManager;
     27 
     28 // This class handles various network statistics in NetEq.
     29 class StatisticsCalculator {
     30 public:
     31  StatisticsCalculator(TickTimer* tick_timer);
     32 
     33  virtual ~StatisticsCalculator();
     34 
     35  StatisticsCalculator(const StatisticsCalculator&) = delete;
     36  StatisticsCalculator& operator=(const StatisticsCalculator&) = delete;
     37 
     38  // Resets most of the counters.
     39  void Reset();
     40 
     41  // Resets the counters that are not handled by Reset().
     42  void ResetMcu();
     43 
     44  // Reports that `num_samples` samples were produced through expansion, and
     45  // that the expansion produced other than just noise samples.
     46  void ExpandedVoiceSamples(size_t num_samples, bool is_new_concealment_event);
     47 
     48  // Reports that `num_samples` samples were produced through expansion, and
     49  // that the expansion produced only noise samples.
     50  void ExpandedNoiseSamples(size_t num_samples, bool is_new_concealment_event);
     51 
     52  // Corrects the statistics for number of samples produced through non-noise
     53  // expansion by adding `num_samples` (negative or positive) to the current
     54  // value. The result is capped to zero to avoid negative values.
     55  void ExpandedVoiceSamplesCorrection(int num_samples);
     56 
     57  // Same as ExpandedVoiceSamplesCorrection but for noise samples.
     58  void ExpandedNoiseSamplesCorrection(int num_samples);
     59 
     60  void DecodedOutputPlayed();
     61 
     62  // Mark end of expand event; triggers some stats to be reported.
     63  void EndExpandEvent(int fs_hz);
     64 
     65  // Reports that `num_samples` samples were produced through preemptive
     66  // expansion.
     67  void PreemptiveExpandedSamples(size_t num_samples);
     68 
     69  // Reports that `num_samples` samples were removed through accelerate.
     70  void AcceleratedSamples(size_t num_samples);
     71 
     72  // Reports that `num_samples` comfort noise samples were generated.
     73  void GeneratedNoiseSamples(size_t num_samples);
     74 
     75  // Reports that `num_packets` packets were discarded.
     76  virtual void PacketsDiscarded(size_t num_packets);
     77 
     78  // Reports that `num_packets` secondary (FEC) packets were discarded.
     79  virtual void SecondaryPacketsDiscarded(size_t num_packets);
     80 
     81  // Reports that `num_packets` secondary (FEC) packets were received.
     82  virtual void SecondaryPacketsReceived(size_t num_packets);
     83 
     84  // Increases the report interval counter with `num_samples` at a sample rate
     85  // of `fs_hz`. This is how the StatisticsCalculator gets notified that current
     86  // time is increasing.
     87  void IncreaseCounter(size_t num_samples, int fs_hz);
     88 
     89  // Update jitter buffer delay counter.
     90  void JitterBufferDelay(size_t num_samples,
     91                         uint64_t waiting_time_ms,
     92                         uint64_t target_delay_ms,
     93                         uint64_t unlimited_target_delay_ms,
     94                         uint64_t processing_delay_us);
     95 
     96  // Stores new packet waiting time in waiting time statistics.
     97  void StoreWaitingTime(int waiting_time_ms);
     98 
     99  // Reports that `num_samples` samples were decoded from secondary packets.
    100  void SecondaryDecodedSamples(int num_samples);
    101 
    102  // Reports that the packet buffer was flushed.
    103  void FlushedPacketBuffer();
    104 
    105  // Reports that the jitter buffer received a packet.
    106  void ReceivedPacket();
    107 
    108  // Reports that a received packet was delayed by `delay_ms` milliseconds.
    109  virtual void RelativePacketArrivalDelay(size_t delay_ms);
    110 
    111  // Logs a delayed packet outage event of `num_samples` expanded at a sample
    112  // rate of `fs_hz`. A delayed packet outage event is defined as an expand
    113  // period caused not by an actual packet loss, but by a delayed packet.
    114  virtual void LogDelayedPacketOutageEvent(int num_samples, int fs_hz);
    115 
    116  // Returns the current network statistics in `stats`. The number of samples
    117  // per packet is `samples_per_packet`. The method does not populate
    118  // `preferred_buffer_size_ms`, `jitter_peaks_found` or `clockdrift_ppm`; use
    119  // the PopulateDelayManagerStats method for those.
    120  void GetNetworkStatistics(size_t samples_per_packet,
    121                            NetEqNetworkStatistics* stats);
    122 
    123  // Returns a copy of this class's lifetime statistics. These statistics are
    124  // never reset.
    125  NetEqLifetimeStatistics GetLifetimeStatistics() const;
    126 
    127  NetEqOperationsAndState GetOperationsAndState() const;
    128 
    129 private:
    130  static const int kMaxReportPeriod = 60;  // Seconds before auto-reset.
    131  static const size_t kLenWaitingTimes = 100;
    132 
    133  class PeriodicUmaLogger {
    134   public:
    135    PeriodicUmaLogger(absl::string_view uma_name,
    136                      int report_interval_ms,
    137                      int max_value);
    138    virtual ~PeriodicUmaLogger();
    139    void AdvanceClock(int step_ms);
    140 
    141   protected:
    142    void LogToUma(int value) const;
    143    virtual int Metric() const = 0;
    144    virtual void Reset() = 0;
    145 
    146    const std::string uma_name_;
    147    const int report_interval_ms_;
    148    const int max_value_;
    149    int timer_ = 0;
    150  };
    151 
    152  class PeriodicUmaCount final : public PeriodicUmaLogger {
    153   public:
    154    PeriodicUmaCount(absl::string_view uma_name,
    155                     int report_interval_ms,
    156                     int max_value);
    157    ~PeriodicUmaCount() override;
    158    void RegisterSample();
    159 
    160   protected:
    161    int Metric() const override;
    162    void Reset() override;
    163 
    164   private:
    165    int counter_ = 0;
    166  };
    167 
    168  class PeriodicUmaAverage final : public PeriodicUmaLogger {
    169   public:
    170    PeriodicUmaAverage(absl::string_view uma_name,
    171                       int report_interval_ms,
    172                       int max_value);
    173    ~PeriodicUmaAverage() override;
    174    void RegisterSample(int value);
    175 
    176   protected:
    177    int Metric() const override;
    178    void Reset() override;
    179 
    180   private:
    181    double sum_ = 0.0;
    182    int counter_ = 0;
    183  };
    184 
    185  // Corrects the concealed samples counter in lifetime_stats_. The value of
    186  // num_samples_ is added directly to the stat if the correction is positive.
    187  // If the correction is negative, it is cached and will be subtracted against
    188  // future additions to the counter. This is meant to be called from
    189  // Expanded{Voice,Noise}Samples{Correction}.
    190  void ConcealedSamplesCorrection(int num_samples, bool is_voice);
    191 
    192  // Calculates numerator / denominator, and returns the value in Q14.
    193  static uint16_t CalculateQ14Ratio(size_t numerator, uint32_t denominator);
    194 
    195  NetEqLifetimeStatistics lifetime_stats_;
    196  NetEqOperationsAndState operations_and_state_;
    197  size_t concealed_samples_correction_ = 0;
    198  size_t silent_concealed_samples_correction_ = 0;
    199  size_t preemptive_samples_;
    200  size_t accelerate_samples_;
    201  size_t expanded_speech_samples_;
    202  size_t expanded_noise_samples_;
    203  size_t concealed_samples_at_event_end_ = 0;
    204  uint32_t timestamps_since_last_report_;
    205  std::deque<int> waiting_times_;
    206  uint32_t secondary_decoded_samples_;
    207  size_t discarded_secondary_packets_;
    208  PeriodicUmaCount delayed_packet_outage_counter_;
    209  PeriodicUmaAverage excess_buffer_delay_;
    210  PeriodicUmaCount buffer_full_counter_;
    211  bool decoded_output_played_ = false;
    212  ExpandUmaLogger expand_uma_logger_;
    213  ExpandUmaLogger speech_expand_uma_logger_;
    214 };
    215 
    216 }  // namespace webrtc
    217 #endif  // MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_