// 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;

enum class MetricType {
  kNumGlitches,
  kTotalDuration,
  kLargestDuration,
  kEarlyDetected
};

// Helper function to get the metric name.
const char* GetMetricName(SystemGlitchReporter::StreamType stream_type,
                          MetricType metric_type) {
  switch (stream_type) {
    case SystemGlitchReporter::StreamType::kCapture:
      switch (metric_type) {
        case MetricType::kNumGlitches:
          return "Media.Audio.Capture.Glitches2";
        case MetricType::kTotalDuration:
          return "Media.Audio.Capture.LostFramesInMs2";
        case MetricType::kLargestDuration:
          return "Media.Audio.Capture.LargestGlitchMs2";
        case MetricType::kEarlyDetected:
          return "Media.Audio.Capture.EarlyGlitchDetected";
      }
    case SystemGlitchReporter::StreamType::kRender:
      switch (metric_type) {
        case MetricType::kNumGlitches:
          return "Media.Audio.Render.Glitches2";
        case MetricType::kTotalDuration:
          return "Media.Audio.Render.LostFramesInMs2";
        case MetricType::kLargestDuration:
          return "Media.Audio.Render.LargestGlitchMs2";
        case MetricType::kEarlyDetected:
          return "Media.Audio.Render.EarlyGlitchDetected";
      }
    case SystemGlitchReporter::StreamType::kLoopback:
      switch (metric_type) {
        case MetricType::kNumGlitches:
          return "Media.Audio.Loopback.Glitches2";
        case MetricType::kTotalDuration:
          return "Media.Audio.Loopback.LostFramesInMs2";
        case MetricType::kLargestDuration:
          return "Media.Audio.Loopback.LargestGlitchMs2";
        case MetricType::kEarlyDetected:
          return "Media.Audio.Loopback.EarlyGlitchDetected";
      }
    case SystemGlitchReporter::StreamType::kLoopbackReference:
      switch (metric_type) {
        case MetricType::kNumGlitches:
          return "Media.Audio.LoopbackReference.Glitches2";
        case MetricType::kTotalDuration:
          return "Media.Audio.LoopbackReference.LostFramesInMs2";
        case MetricType::kLargestDuration:
          return "Media.Audio.LoopbackReference.LargestGlitchMs2";
        case MetricType::kEarlyDetected:
          return "Media.Audio.LoopbackReference.EarlyGlitchDetected";
      }
  }
}
}  // namespace

SystemGlitchReporter::SystemGlitchReporter(StreamType stream_type)
    : num_glitches_detected_metric_name_(
          GetMetricName(stream_type, MetricType::kNumGlitches)),
      total_glitch_duration_metric_name_(
          GetMetricName(stream_type, MetricType::kTotalDuration)),
      largest_glitch_duration_metric_name_(
          GetMetricName(stream_type, MetricType::kLargestDuration)),
      early_glitch_detected_metric_name_(
          GetMetricName(stream_type, MetricType::kEarlyDetected)) {}

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_INSTANT("audio", "OsGlitchDetected", "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
