summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_processing/aec3/api_call_jitter_metrics.cc
blob: 45f56a5dce6fab1371f244e89e2c025f7a440c98 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/audio_processing/aec3/api_call_jitter_metrics.h"

#include <algorithm>
#include <limits>

#include "modules/audio_processing/aec3/aec3_common.h"
#include "system_wrappers/include/metrics.h"

namespace webrtc {
namespace {

bool TimeToReportMetrics(int frames_since_last_report) {
  constexpr int kNumFramesPerSecond = 100;
  constexpr int kReportingIntervalFrames = 10 * kNumFramesPerSecond;
  return frames_since_last_report == kReportingIntervalFrames;
}

}  // namespace

ApiCallJitterMetrics::Jitter::Jitter()
    : max_(0), min_(std::numeric_limits<int>::max()) {}

void ApiCallJitterMetrics::Jitter::Update(int num_api_calls_in_a_row) {
  min_ = std::min(min_, num_api_calls_in_a_row);
  max_ = std::max(max_, num_api_calls_in_a_row);
}

void ApiCallJitterMetrics::Jitter::Reset() {
  min_ = std::numeric_limits<int>::max();
  max_ = 0;
}

void ApiCallJitterMetrics::Reset() {
  render_jitter_.Reset();
  capture_jitter_.Reset();
  num_api_calls_in_a_row_ = 0;
  frames_since_last_report_ = 0;
  last_call_was_render_ = false;
  proper_call_observed_ = false;
}

void ApiCallJitterMetrics::ReportRenderCall() {
  if (!last_call_was_render_) {
    // If the previous call was a capture and a proper call has been observed
    // (containing both render and capture data), storing the last number of
    // capture calls into the metrics.
    if (proper_call_observed_) {
      capture_jitter_.Update(num_api_calls_in_a_row_);
    }

    // Reset the call counter to start counting render calls.
    num_api_calls_in_a_row_ = 0;
  }
  ++num_api_calls_in_a_row_;
  last_call_was_render_ = true;
}

void ApiCallJitterMetrics::ReportCaptureCall() {
  if (last_call_was_render_) {
    // If the previous call was a render and a proper call has been observed
    // (containing both render and capture data), storing the last number of
    // render calls into the metrics.
    if (proper_call_observed_) {
      render_jitter_.Update(num_api_calls_in_a_row_);
    }
    // Reset the call counter to start counting capture calls.
    num_api_calls_in_a_row_ = 0;

    // If this statement is reached, at least one render and one capture call
    // have been observed.
    proper_call_observed_ = true;
  }
  ++num_api_calls_in_a_row_;
  last_call_was_render_ = false;

  // Only report and update jitter metrics for when a proper call, containing
  // both render and capture data, has been observed.
  if (proper_call_observed_ &&
      TimeToReportMetrics(++frames_since_last_report_)) {
    // Report jitter, where the base basic unit is frames.
    constexpr int kMaxJitterToReport = 50;

    // Report max and min jitter for render and capture, in units of 20 ms.
    RTC_HISTOGRAM_COUNTS_LINEAR(
        "WebRTC.Audio.EchoCanceller.MaxRenderJitter",
        std::min(kMaxJitterToReport, render_jitter().max()), 1,
        kMaxJitterToReport, kMaxJitterToReport);
    RTC_HISTOGRAM_COUNTS_LINEAR(
        "WebRTC.Audio.EchoCanceller.MinRenderJitter",
        std::min(kMaxJitterToReport, render_jitter().min()), 1,
        kMaxJitterToReport, kMaxJitterToReport);

    RTC_HISTOGRAM_COUNTS_LINEAR(
        "WebRTC.Audio.EchoCanceller.MaxCaptureJitter",
        std::min(kMaxJitterToReport, capture_jitter().max()), 1,
        kMaxJitterToReport, kMaxJitterToReport);
    RTC_HISTOGRAM_COUNTS_LINEAR(
        "WebRTC.Audio.EchoCanceller.MinCaptureJitter",
        std::min(kMaxJitterToReport, capture_jitter().min()), 1,
        kMaxJitterToReport, kMaxJitterToReport);

    frames_since_last_report_ = 0;
    Reset();
  }
}

bool ApiCallJitterMetrics::WillReportMetricsAtNextCapture() const {
  return TimeToReportMetrics(frames_since_last_report_ + 1);
}

}  // namespace webrtc