xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/api_call_jitter_metrics_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_processing/aec3/api_call_jitter_metrics.h"
12 
13 #include "modules/audio_processing/aec3/aec3_common.h"
14 #include "test/gtest.h"
15 
16 namespace webrtc {
17 
18 // Verify constant jitter.
TEST(ApiCallJitterMetrics,ConstantJitter)19 TEST(ApiCallJitterMetrics, ConstantJitter) {
20   for (int jitter = 1; jitter < 20; ++jitter) {
21     ApiCallJitterMetrics metrics;
22     for (size_t k = 0; k < 30 * kNumBlocksPerSecond; ++k) {
23       for (int j = 0; j < jitter; ++j) {
24         metrics.ReportRenderCall();
25       }
26 
27       for (int j = 0; j < jitter; ++j) {
28         metrics.ReportCaptureCall();
29 
30         if (metrics.WillReportMetricsAtNextCapture()) {
31           EXPECT_EQ(jitter, metrics.render_jitter().min());
32           EXPECT_EQ(jitter, metrics.render_jitter().max());
33           EXPECT_EQ(jitter, metrics.capture_jitter().min());
34           EXPECT_EQ(jitter, metrics.capture_jitter().max());
35         }
36       }
37     }
38   }
39 }
40 
41 // Verify peaky jitter for the render.
TEST(ApiCallJitterMetrics,JitterPeakRender)42 TEST(ApiCallJitterMetrics, JitterPeakRender) {
43   constexpr int kMinJitter = 2;
44   constexpr int kJitterPeak = 10;
45   constexpr int kPeakInterval = 100;
46 
47   ApiCallJitterMetrics metrics;
48   int render_surplus = 0;
49 
50   for (size_t k = 0; k < 30 * kNumBlocksPerSecond; ++k) {
51     const int num_render_calls =
52         k % kPeakInterval == 0 ? kJitterPeak : kMinJitter;
53     for (int j = 0; j < num_render_calls; ++j) {
54       metrics.ReportRenderCall();
55       ++render_surplus;
56     }
57 
58     ASSERT_LE(kMinJitter, render_surplus);
59     const int num_capture_calls =
60         render_surplus == kMinJitter ? kMinJitter : kMinJitter + 1;
61     for (int j = 0; j < num_capture_calls; ++j) {
62       metrics.ReportCaptureCall();
63 
64       if (metrics.WillReportMetricsAtNextCapture()) {
65         EXPECT_EQ(kMinJitter, metrics.render_jitter().min());
66         EXPECT_EQ(kJitterPeak, metrics.render_jitter().max());
67         EXPECT_EQ(kMinJitter, metrics.capture_jitter().min());
68         EXPECT_EQ(kMinJitter + 1, metrics.capture_jitter().max());
69       }
70       --render_surplus;
71     }
72   }
73 }
74 
75 // Verify peaky jitter for the capture.
TEST(ApiCallJitterMetrics,JitterPeakCapture)76 TEST(ApiCallJitterMetrics, JitterPeakCapture) {
77   constexpr int kMinJitter = 2;
78   constexpr int kJitterPeak = 10;
79   constexpr int kPeakInterval = 100;
80 
81   ApiCallJitterMetrics metrics;
82   int capture_surplus = kMinJitter;
83 
84   for (size_t k = 0; k < 30 * kNumBlocksPerSecond; ++k) {
85     ASSERT_LE(kMinJitter, capture_surplus);
86     const int num_render_calls =
87         capture_surplus == kMinJitter ? kMinJitter : kMinJitter + 1;
88     for (int j = 0; j < num_render_calls; ++j) {
89       metrics.ReportRenderCall();
90       --capture_surplus;
91     }
92 
93     const int num_capture_calls =
94         k % kPeakInterval == 0 ? kJitterPeak : kMinJitter;
95     for (int j = 0; j < num_capture_calls; ++j) {
96       metrics.ReportCaptureCall();
97 
98       if (metrics.WillReportMetricsAtNextCapture()) {
99         EXPECT_EQ(kMinJitter, metrics.render_jitter().min());
100         EXPECT_EQ(kMinJitter + 1, metrics.render_jitter().max());
101         EXPECT_EQ(kMinJitter, metrics.capture_jitter().min());
102         EXPECT_EQ(kJitterPeak, metrics.capture_jitter().max());
103       }
104       ++capture_surplus;
105     }
106   }
107 }
108 
109 }  // namespace webrtc
110