system_glitch_reporter.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_AUDIO_SYSTEM_GLITCH_REPORTER_H_
  5. #define MEDIA_AUDIO_SYSTEM_GLITCH_REPORTER_H_
  6. #include <string>
  7. #include "base/time/time.h"
  8. namespace media {
  9. // Aggregates and reports glitch statistics.
  10. // Stats are aggregated and reported to UMA periodically every 1000th call to
  11. // UpdateStats(), and longer-term (manually reset) stats are available via
  12. // GetLongTermStatsAndReset().
  13. class SystemGlitchReporter {
  14. public:
  15. // Used to determine which UMA metrics to log.
  16. enum class StreamType { kCapture, kRender };
  17. struct Stats {
  18. int glitches_detected = 0;
  19. base::TimeDelta total_glitch_duration;
  20. base::TimeDelta largest_glitch_duration;
  21. };
  22. SystemGlitchReporter(StreamType stream_type);
  23. ~SystemGlitchReporter();
  24. // Resets all state: both periodic and long-term stats.
  25. Stats GetLongTermStatsAndReset();
  26. // Updates statistics and metric reporting counters. Any non-zero
  27. // |glitch_duration| is considered a glitch.
  28. void UpdateStats(base::TimeDelta glitch_duration);
  29. private:
  30. const std::string num_glitches_detected_metric_name_;
  31. const std::string total_glitch_duration_metric_name_;
  32. const std::string largest_glitch_duration_metric_name_;
  33. const std::string early_glitch_detected_metric_name_;
  34. int callback_count_ = 0;
  35. // Stats reported periodically to UMA. Resets every 1000 callbacks and on
  36. // GetLongTermStatsAndReset().
  37. Stats short_term_stats_;
  38. // Stats that only reset on GetLongTermStatsAndReset().
  39. Stats long_term_stats_;
  40. // Long-term metric reported in ReportLongTermStatsAndReset().
  41. // Records whether any glitch occurred during the first 1000 callbacks.
  42. bool early_glitch_detected_ = false;
  43. };
  44. } // namespace media
  45. #endif // MEDIA_AUDIO_SYSTEM_GLITCH_REPORTER_H_