1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62

media / audio / system_glitch_reporter.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_AUDIO_SYSTEM_GLITCH_REPORTER_H_
#define MEDIA_AUDIO_SYSTEM_GLITCH_REPORTER_H_

#include <string>

#include "base/time/time.h"

namespace media {

// Aggregates and reports glitch statistics.
// Stats are aggregated and reported to UMA periodically every 1000th call to
// UpdateStats(), and longer-term (manually reset) stats are available via
// GetLongTermStatsAndReset().
class SystemGlitchReporter {
 public:
  // Used to determine which UMA metrics to log.
  enum class StreamType { kCapture, kRender };

  struct Stats {
    int glitches_detected = 0;
    base::TimeDelta total_glitch_duration;
    base::TimeDelta largest_glitch_duration;
  };

  SystemGlitchReporter(StreamType stream_type);

  ~SystemGlitchReporter();

  // Resets all state: both periodic and long-term stats.
  Stats GetLongTermStatsAndReset();

  // Updates statistics and metric reporting counters. Any non-zero
  // |glitch_duration| is considered a glitch.
  void UpdateStats(base::TimeDelta glitch_duration);

 private:
  const std::string num_glitches_detected_metric_name_;
  const std::string total_glitch_duration_metric_name_;
  const std::string largest_glitch_duration_metric_name_;
  const std::string early_glitch_detected_metric_name_;

  int callback_count_ = 0;

  // Stats reported periodically to UMA. Resets every 1000 callbacks and on
  // GetLongTermStatsAndReset().
  Stats short_term_stats_;

  // Stats that only reset on GetLongTermStatsAndReset().
  Stats long_term_stats_;

  // Long-term metric reported in ReportLongTermStatsAndReset().
  // Records whether any glitch occurred during the first 1000 callbacks.
  bool early_glitch_detected_ = false;
};

}  // namespace media

#endif  // MEDIA_AUDIO_SYSTEM_GLITCH_REPORTER_H_