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

media / base / audio_glitch_info.cc [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.

#include "media/base/audio_glitch_info.h"

#include <utility>

#include "base/metrics/histogram_macros.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"

namespace media {

std::string AudioGlitchInfo::ToString() const {
  return base::StrCat(
      {"duration (ms): ", base::NumberToString(duration.InMilliseconds()),
       ", count: ", base::NumberToString(count)});
}

void AudioGlitchInfo::MaybeAddTraceEvent() const {
  if (!count) {
    return;
  }
  TRACE_EVENT_INSTANT("audio", "Glitch!", "glitch count", count,
                      "glitch duration", duration);
}

// static
AudioGlitchInfo AudioGlitchInfo::SingleBoundedSystemGlitch(
    const base::TimeDelta duration,
    const Direction direction) {
  CHECK(duration.is_positive());
  if (direction == Direction::kRender) {
    UMA_HISTOGRAM_LONG_TIMES("Media.Audio.Render.SystemGlitchDuration",
                             duration);
  } else {
    UMA_HISTOGRAM_LONG_TIMES("Media.Audio.Capture.SystemGlitchDuration",
                             duration);
  }
  return AudioGlitchInfo{
      .duration = std::clamp(duration, base::Seconds(0), base::Seconds(1)),
      .count = 1};
}

AudioGlitchInfo& AudioGlitchInfo::operator+=(const AudioGlitchInfo& other) {
  duration += other.duration;
  count += other.count;
  return *this;
}

bool operator==(const AudioGlitchInfo& lhs, const AudioGlitchInfo& rhs) {
  return lhs.duration == rhs.duration && lhs.count == rhs.count;
}

AudioGlitchInfo::Accumulator::Accumulator() = default;
AudioGlitchInfo::Accumulator::~Accumulator() = default;

void AudioGlitchInfo::Accumulator::Add(const AudioGlitchInfo& info) {
  pending_info_ += info;
}

AudioGlitchInfo AudioGlitchInfo::Accumulator::GetAndReset() {
  AudioGlitchInfo temp = std::move(pending_info_);
  pending_info_ = {};
  return temp;
}

}  // namespace media