summaryrefslogtreecommitdiff
path: root/chromium/media/audio/system_glitch_reporter.cc
blob: a32a51dccf44e224dd4269199f8ed71d1392a71e (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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.

#include "media/audio/system_glitch_reporter.h"

#include "base/metrics/histogram_functions.h"
#include "base/trace_event/trace_event.h"

namespace media {

namespace {
// Logs once every 10s, assuming 10ms buffers.
constexpr static int kCallbacksPerLogPeriod = 1000;
}  // namespace

SystemGlitchReporter::SystemGlitchReporter(StreamType stream_type)
    : num_glitches_detected_metric_name_(stream_type == StreamType::kCapture
                                             ? "Media.Audio.Capture.Glitches2"
                                             : "Media.Audio.Render.Glitches2"),
      total_glitch_duration_metric_name_(
          stream_type == StreamType::kCapture
              ? "Media.Audio.Capture.LostFramesInMs2"
              : "Media.Audio.Render.LostFramesInMs2"),
      largest_glitch_duration_metric_name_(
          stream_type == StreamType::kCapture
              ? "Media.Audio.Capture.LargestGlitchMs2"
              : "Media.Audio.Render.LargestGlitchMs2"),
      early_glitch_detected_metric_name_(
          stream_type == StreamType::kCapture
              ? "Media.Audio.Capture.EarlyGlitchDetected"
              : "Media.Audio.Render.EarlyGlitchDetected") {}

SystemGlitchReporter::~SystemGlitchReporter() = default;

SystemGlitchReporter::Stats SystemGlitchReporter::GetLongTermStatsAndReset() {
  if (callback_count_ > 0) {
    base::UmaHistogramBoolean(early_glitch_detected_metric_name_,
                              early_glitch_detected_);
  }

  Stats result = long_term_stats_;
  callback_count_ = 0;
  short_term_stats_ = {};
  long_term_stats_ = {};
  early_glitch_detected_ = false;
  return result;
}

void SystemGlitchReporter::UpdateStats(base::TimeDelta glitch_duration) {
  ++callback_count_;

  if (glitch_duration.is_positive()) {
    TRACE_EVENT_INSTANT1("audio", "OsGlitchDetected", TRACE_EVENT_SCOPE_THREAD,
                         "glitch_duration_ms",
                         glitch_duration.InMilliseconds());

    if (callback_count_ <= kCallbacksPerLogPeriod)
      early_glitch_detected_ = true;

    ++short_term_stats_.glitches_detected;
    ++long_term_stats_.glitches_detected;

    short_term_stats_.total_glitch_duration += glitch_duration;
    long_term_stats_.total_glitch_duration += glitch_duration;

    short_term_stats_.largest_glitch_duration =
        std::max(short_term_stats_.largest_glitch_duration, glitch_duration);
    long_term_stats_.largest_glitch_duration =
        std::max(long_term_stats_.largest_glitch_duration, glitch_duration);
  }

  if (callback_count_ % kCallbacksPerLogPeriod != 0)
    return;

  // We record the glitch count even if there aren't any glitches, to get a
  // feel for how often we get no glitches vs the alternative.
  base::UmaHistogramCounts1000(num_glitches_detected_metric_name_,
                               short_term_stats_.glitches_detected);

  if (short_term_stats_.glitches_detected != 0) {
    base::UmaHistogramCounts1M(
        total_glitch_duration_metric_name_,
        short_term_stats_.total_glitch_duration.InMilliseconds());
    base::UmaHistogramCounts1M(
        largest_glitch_duration_metric_name_,
        short_term_stats_.largest_glitch_duration.InMilliseconds());
  }

  short_term_stats_ = {};
}

}  // namespace media