File: statistics_calculator_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (270 lines) | stat: -rw-r--r-- 10,585 bytes parent folder | download | duplicates (5)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *  Copyright (c) 2017 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_coding/neteq/statistics_calculator.h"

#include <cstddef>

#include "api/neteq/neteq.h"
#include "api/neteq/tick_timer.h"
#include "test/gtest.h"

namespace webrtc {

TEST(LifetimeStatistics, TotalSamplesReceived) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  stats.DecodedOutputPlayed();
  for (int i = 0; i < 10; ++i) {
    stats.IncreaseCounter(480, 48000);  // 10 ms at 48 kHz.
  }
  EXPECT_EQ(10 * 480u, stats.GetLifetimeStatistics().total_samples_received);
}

TEST(LifetimeStatistics, SamplesConcealed) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  stats.DecodedOutputPlayed();
  stats.ExpandedVoiceSamples(100, false);
  stats.ExpandedNoiseSamples(17, false);
  EXPECT_EQ(100u + 17u, stats.GetLifetimeStatistics().concealed_samples);
}

// This test verifies that a negative correction of concealed_samples does not
// result in a decrease in the stats value (because stats-consuming applications
// would not expect the value to decrease). Instead, the correction should be
// made to future increments to the stat.
TEST(LifetimeStatistics, SamplesConcealedCorrection) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  stats.DecodedOutputPlayed();
  stats.ExpandedVoiceSamples(100, false);
  EXPECT_EQ(100u, stats.GetLifetimeStatistics().concealed_samples);
  stats.ExpandedVoiceSamplesCorrection(-10);
  // Do not subtract directly, but keep the correction for later.
  EXPECT_EQ(100u, stats.GetLifetimeStatistics().concealed_samples);
  stats.ExpandedVoiceSamplesCorrection(20);
  // The total correction is 20 - 10.
  EXPECT_EQ(110u, stats.GetLifetimeStatistics().concealed_samples);

  // Also test correction done to the next ExpandedVoiceSamples call.
  stats.ExpandedVoiceSamplesCorrection(-17);
  EXPECT_EQ(110u, stats.GetLifetimeStatistics().concealed_samples);
  stats.ExpandedVoiceSamples(100, false);
  EXPECT_EQ(110u + 100u - 17u, stats.GetLifetimeStatistics().concealed_samples);
}

// This test verifies that neither "accelerate" nor "pre-emptive expand" reults
// in a modification to concealed_samples stats. Only PLC operations (i.e.,
// "expand" and "merge") should affect the stat.
TEST(LifetimeStatistics, NoUpdateOnTimeStretch) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  stats.DecodedOutputPlayed();
  stats.ExpandedVoiceSamples(100, false);
  stats.AcceleratedSamples(4711);
  stats.PreemptiveExpandedSamples(17);
  stats.ExpandedVoiceSamples(100, false);
  EXPECT_EQ(200u, stats.GetLifetimeStatistics().concealed_samples);
}

TEST(StatisticsCalculator, ExpandedSamplesCorrection) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  stats.DecodedOutputPlayed();
  NetEqNetworkStatistics stats_output;
  constexpr int kSampleRateHz = 48000;
  constexpr int k10MsSamples = kSampleRateHz / 100;
  constexpr int kPacketSizeMs = 20;
  constexpr size_t kSamplesPerPacket = kPacketSizeMs * kSampleRateHz / 1000;

  // Advance time by 10 ms.
  stats.IncreaseCounter(k10MsSamples, kSampleRateHz);

  stats.GetNetworkStatistics(kSamplesPerPacket, &stats_output);

  EXPECT_EQ(0u, stats_output.expand_rate);
  EXPECT_EQ(0u, stats_output.speech_expand_rate);

  // Correct with a negative value.
  stats.ExpandedVoiceSamplesCorrection(-100);
  stats.ExpandedNoiseSamplesCorrection(-100);
  stats.IncreaseCounter(k10MsSamples, kSampleRateHz);
  stats.GetNetworkStatistics(kSamplesPerPacket, &stats_output);
  // Expect no change, since negative values are disallowed.
  EXPECT_EQ(0u, stats_output.expand_rate);
  EXPECT_EQ(0u, stats_output.speech_expand_rate);

  // Correct with a positive value.
  stats.ExpandedVoiceSamplesCorrection(50);
  stats.ExpandedNoiseSamplesCorrection(200);
  stats.IncreaseCounter(k10MsSamples, kSampleRateHz);
  stats.GetNetworkStatistics(kSamplesPerPacket, &stats_output);
  // Calculate expected rates in Q14. Expand rate is noise + voice, while
  // speech expand rate is only voice.
  EXPECT_EQ(((50u + 200u) << 14) / k10MsSamples, stats_output.expand_rate);
  EXPECT_EQ((50u << 14) / k10MsSamples, stats_output.speech_expand_rate);
}

TEST(StatisticsCalculator, RelativePacketArrivalDelay) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);

  stats.RelativePacketArrivalDelay(50);
  NetEqLifetimeStatistics stats_output = stats.GetLifetimeStatistics();
  EXPECT_EQ(50u, stats_output.relative_packet_arrival_delay_ms);

  stats.RelativePacketArrivalDelay(20);
  stats_output = stats.GetLifetimeStatistics();
  EXPECT_EQ(70u, stats_output.relative_packet_arrival_delay_ms);
}

TEST(StatisticsCalculator, ReceivedPacket) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);

  stats.ReceivedPacket();
  NetEqLifetimeStatistics stats_output = stats.GetLifetimeStatistics();
  EXPECT_EQ(1u, stats_output.jitter_buffer_packets_received);

  stats.ReceivedPacket();
  stats_output = stats.GetLifetimeStatistics();
  EXPECT_EQ(2u, stats_output.jitter_buffer_packets_received);
}

TEST(StatisticsCalculator, InterruptionCounter) {
  constexpr int fs_khz = 48;
  constexpr int fs_hz = fs_khz * 1000;
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  stats.DecodedOutputPlayed();
  stats.EndExpandEvent(fs_hz);
  auto lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(0, lts.interruption_count);
  EXPECT_EQ(0, lts.total_interruption_duration_ms);

  // Add an event that is shorter than 150 ms. Should not be logged.
  stats.ExpandedVoiceSamples(10 * fs_khz, false);   // 10 ms.
  stats.ExpandedNoiseSamples(139 * fs_khz, false);  // 139 ms.
  stats.EndExpandEvent(fs_hz);
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(0, lts.interruption_count);

  // Add an event that is longer than 150 ms. Should be logged.
  stats.ExpandedVoiceSamples(140 * fs_khz, false);  // 140 ms.
  stats.ExpandedNoiseSamples(11 * fs_khz, false);   // 11 ms.
  stats.EndExpandEvent(fs_hz);
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(1, lts.interruption_count);
  EXPECT_EQ(151, lts.total_interruption_duration_ms);

  // Add one more long event.
  stats.ExpandedVoiceSamples(100 * fs_khz, false);   // 100 ms.
  stats.ExpandedNoiseSamples(5000 * fs_khz, false);  // 5000 ms.
  stats.EndExpandEvent(fs_hz);
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(2, lts.interruption_count);
  EXPECT_EQ(5100 + 151, lts.total_interruption_duration_ms);
}

TEST(StatisticsCalculator, InterruptionCounterDoNotLogBeforeDecoding) {
  constexpr int fs_khz = 48;
  constexpr int fs_hz = fs_khz * 1000;
  TickTimer timer;
  StatisticsCalculator stats(&timer);

  // Add an event that is longer than 150 ms. Should normally be logged, but we
  // have not called DecodedOutputPlayed() yet, so it shouldn't this time.
  stats.ExpandedVoiceSamples(151 * fs_khz, false);  // 151 ms.
  stats.EndExpandEvent(fs_hz);
  auto lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(0, lts.interruption_count);

  // Call DecodedOutputPlayed(). Logging should happen after this.
  stats.DecodedOutputPlayed();

  // Add one more long event.
  stats.ExpandedVoiceSamples(151 * fs_khz, false);  // 151 ms.
  stats.EndExpandEvent(fs_hz);
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(1, lts.interruption_count);
}

TEST(StatisticsCalculator, DiscardedPackets) {
  TickTimer timer;
  StatisticsCalculator statistics_calculator(&timer);
  EXPECT_EQ(0u,
            statistics_calculator.GetLifetimeStatistics().packets_discarded);

  statistics_calculator.PacketsDiscarded(1);
  EXPECT_EQ(1u,
            statistics_calculator.GetLifetimeStatistics().packets_discarded);

  statistics_calculator.PacketsDiscarded(10);
  EXPECT_EQ(11u,
            statistics_calculator.GetLifetimeStatistics().packets_discarded);

  // Calling `SecondaryPacketsDiscarded` does not modify `packets_discarded`.
  statistics_calculator.SecondaryPacketsDiscarded(1);
  EXPECT_EQ(11u,
            statistics_calculator.GetLifetimeStatistics().packets_discarded);

  // Calling `FlushedPacketBuffer` does not modify `packets_discarded`.
  statistics_calculator.FlushedPacketBuffer();
  EXPECT_EQ(11u,
            statistics_calculator.GetLifetimeStatistics().packets_discarded);
}

TEST(StatisticsCalculator, JitterBufferDelay) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  NetEqLifetimeStatistics lts;
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(lts.total_processing_delay_us, 0ul);
  stats.JitterBufferDelay(/*num_samples=*/480,
                          /*waiting_time_ms=*/90ul,
                          /*target_delay_ms=*/80ul,
                          /*unlimited_target_delay_ms=*/70,
                          /*processing_delay_us=*/100 * 1000ul);
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(lts.jitter_buffer_delay_ms / 480, 90ul);
  EXPECT_EQ(lts.jitter_buffer_target_delay_ms / 480, 80ul);
  EXPECT_EQ(lts.jitter_buffer_minimum_delay_ms / 480, 70ul);
  EXPECT_EQ(lts.total_processing_delay_us / 480, 100 * 1000ul);
  EXPECT_EQ(lts.jitter_buffer_emitted_count, 480ul);
  stats.JitterBufferDelay(/*num_samples=*/480,
                          /*waiting_time_ms=*/90ul,
                          /*target_delay_ms=*/80ul,
                          /*unlimited_target_delay_ms=*/70,
                          /*processing_delay_us=*/100 * 1000ul);
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(lts.jitter_buffer_delay_ms / 960, 90ul);
  EXPECT_EQ(lts.jitter_buffer_target_delay_ms / 960, 80ul);
  EXPECT_EQ(lts.jitter_buffer_minimum_delay_ms / 960, 70ul);
  EXPECT_EQ(lts.total_processing_delay_us / 960, 100 * 1000ul);
  EXPECT_EQ(lts.jitter_buffer_emitted_count, 960ul);
}

TEST(StatisticsCalculator, CountStatsAfterFirstDecodedPacket) {
  TickTimer timer;
  StatisticsCalculator stats(&timer);
  stats.IncreaseCounter(/*num_samples=*/480, /*fs_hz=*/48000);
  stats.ExpandedVoiceSamples(/*num_samples=*/480,
                             /*is_new_concealment_event=*/true);
  NetEqLifetimeStatistics lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(lts.total_samples_received, 0u);
  EXPECT_EQ(lts.concealed_samples, 0u);
  stats.DecodedOutputPlayed();
  stats.IncreaseCounter(/*num_samples=*/480, /*fs_hz=*/48000);
  lts = stats.GetLifetimeStatistics();
  EXPECT_EQ(lts.total_samples_received, 480u);
}

}  // namespace webrtc