blob: 6112dd7fd4d90d6c29af35fd63194eb23a4d112f [file] [log] [blame]
sakale5ba44e2016-10-26 07:09:24 -07001/*
2 * Copyright 2016 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 "webrtc/video/receive_statistics_proxy.h"
12
asapersson2077f2f2017-05-11 05:37:35 -070013#include <limits>
sakale5ba44e2016-10-26 07:09:24 -070014#include <memory>
15
nissef93752a2017-05-10 05:25:59 -070016#include "webrtc/api/video/i420_buffer.h"
hbos50cfe1f2017-01-23 07:21:55 -080017#include "webrtc/api/video/video_frame.h"
18#include "webrtc/api/video/video_rotation.h"
asapersson6966bd52017-01-03 00:44:06 -080019#include "webrtc/modules/video_coding/include/video_codec_interface.h"
asapersson0c43f772016-11-30 01:42:26 -080020#include "webrtc/system_wrappers/include/metrics.h"
asaperssonde9e5ff2016-11-02 07:14:03 -070021#include "webrtc/system_wrappers/include/metrics_default.h"
sakale5ba44e2016-10-26 07:09:24 -070022#include "webrtc/test/gtest.h"
23
24namespace webrtc {
asaperssonde9e5ff2016-11-02 07:14:03 -070025namespace {
26const int64_t kFreqOffsetProcessIntervalInMs = 40000;
asapersson46c4e3c2016-11-03 06:48:19 -070027const uint32_t kLocalSsrc = 123;
28const uint32_t kRemoteSsrc = 456;
29const int kMinRequiredSamples = 200;
asaperssonde9e5ff2016-11-02 07:14:03 -070030} // namespace
sakale5ba44e2016-10-26 07:09:24 -070031
32// TODO(sakal): ReceiveStatisticsProxy is lacking unittesting.
33class ReceiveStatisticsProxyTest : public ::testing::Test {
34 public:
35 ReceiveStatisticsProxyTest() : fake_clock_(1234), config_(GetTestConfig()) {}
36 virtual ~ReceiveStatisticsProxyTest() {}
37
38 protected:
39 virtual void SetUp() {
asaperssonde9e5ff2016-11-02 07:14:03 -070040 metrics::Reset();
sakale5ba44e2016-10-26 07:09:24 -070041 statistics_proxy_.reset(new ReceiveStatisticsProxy(&config_, &fake_clock_));
42 }
43
44 VideoReceiveStream::Config GetTestConfig() {
45 VideoReceiveStream::Config config(nullptr);
asapersson46c4e3c2016-11-03 06:48:19 -070046 config.rtp.local_ssrc = kLocalSsrc;
47 config.rtp.remote_ssrc = kRemoteSsrc;
sakale5ba44e2016-10-26 07:09:24 -070048 return config;
49 }
50
asapersson2077f2f2017-05-11 05:37:35 -070051 void InsertFirstRtpPacket(uint32_t ssrc) {
52 StreamDataCounters counters;
53 counters.first_packet_time_ms = fake_clock_.TimeInMilliseconds();
54 statistics_proxy_->DataCountersUpdated(counters, ssrc);
55 }
56
57 VideoFrame CreateFrame(int width, int height) {
58 VideoFrame frame(I420Buffer::Create(width, height), 0, 0, kVideoRotation_0);
59 frame.set_ntp_time_ms(fake_clock_.CurrentNtpInMilliseconds());
60 return frame;
61 }
62
sakale5ba44e2016-10-26 07:09:24 -070063 SimulatedClock fake_clock_;
asaperssonde9e5ff2016-11-02 07:14:03 -070064 const VideoReceiveStream::Config config_;
sakale5ba44e2016-10-26 07:09:24 -070065 std::unique_ptr<ReceiveStatisticsProxy> statistics_proxy_;
sakale5ba44e2016-10-26 07:09:24 -070066};
67
68TEST_F(ReceiveStatisticsProxyTest, OnDecodedFrameIncreasesFramesDecoded) {
69 EXPECT_EQ(0u, statistics_proxy_->GetStats().frames_decoded);
70 for (uint32_t i = 1; i <= 3; ++i) {
ilnik00d802b2017-04-11 10:34:31 -070071 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(),
72 VideoContentType::UNSPECIFIED);
sakale5ba44e2016-10-26 07:09:24 -070073 EXPECT_EQ(i, statistics_proxy_->GetStats().frames_decoded);
74 }
75}
76
sakalcc452e12017-02-09 04:53:45 -080077TEST_F(ReceiveStatisticsProxyTest, OnDecodedFrameWithQpResetsFramesDecoded) {
78 EXPECT_EQ(0u, statistics_proxy_->GetStats().frames_decoded);
79 for (uint32_t i = 1; i <= 3; ++i) {
ilnik00d802b2017-04-11 10:34:31 -070080 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(),
81 VideoContentType::UNSPECIFIED);
sakalcc452e12017-02-09 04:53:45 -080082 EXPECT_EQ(i, statistics_proxy_->GetStats().frames_decoded);
83 }
ilnik00d802b2017-04-11 10:34:31 -070084 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(1u),
85 VideoContentType::UNSPECIFIED);
sakalcc452e12017-02-09 04:53:45 -080086 EXPECT_EQ(1u, statistics_proxy_->GetStats().frames_decoded);
87}
88
89TEST_F(ReceiveStatisticsProxyTest, OnDecodedFrameIncreasesQpSum) {
90 EXPECT_EQ(rtc::Optional<uint64_t>(), statistics_proxy_->GetStats().qp_sum);
ilnik00d802b2017-04-11 10:34:31 -070091 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(3u),
92 VideoContentType::UNSPECIFIED);
sakalcc452e12017-02-09 04:53:45 -080093 EXPECT_EQ(rtc::Optional<uint64_t>(3u), statistics_proxy_->GetStats().qp_sum);
ilnik00d802b2017-04-11 10:34:31 -070094 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(127u),
95 VideoContentType::UNSPECIFIED);
sakalcc452e12017-02-09 04:53:45 -080096 EXPECT_EQ(rtc::Optional<uint64_t>(130u),
97 statistics_proxy_->GetStats().qp_sum);
98}
99
ilnikf04afde2017-07-07 01:26:24 -0700100TEST_F(ReceiveStatisticsProxyTest,
101 OnDecodedFrameIncreasesInterframeDelayMsSum) {
102 const uint64_t kInterframeDelayMs1 = 100;
103 const uint64_t kInterframeDelayMs2 = 200;
104 EXPECT_EQ(0u, statistics_proxy_->GetStats().interframe_delay_sum_ms);
105 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(3u),
106 VideoContentType::UNSPECIFIED);
107 EXPECT_EQ(0u, statistics_proxy_->GetStats().interframe_delay_sum_ms);
108
109 fake_clock_.AdvanceTimeMilliseconds(kInterframeDelayMs1);
110 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(127u),
111 VideoContentType::UNSPECIFIED);
112 EXPECT_EQ(kInterframeDelayMs1,
113 statistics_proxy_->GetStats().interframe_delay_sum_ms);
114
115 fake_clock_.AdvanceTimeMilliseconds(kInterframeDelayMs2);
116 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(127u),
117 VideoContentType::UNSPECIFIED);
118 EXPECT_EQ(kInterframeDelayMs1 + kInterframeDelayMs2,
119 statistics_proxy_->GetStats().interframe_delay_sum_ms);
120}
121
sakalcc452e12017-02-09 04:53:45 -0800122TEST_F(ReceiveStatisticsProxyTest, OnDecodedFrameWithoutQpQpSumWontExist) {
123 EXPECT_EQ(rtc::Optional<uint64_t>(), statistics_proxy_->GetStats().qp_sum);
ilnik00d802b2017-04-11 10:34:31 -0700124 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(),
125 VideoContentType::UNSPECIFIED);
sakalcc452e12017-02-09 04:53:45 -0800126 EXPECT_EQ(rtc::Optional<uint64_t>(), statistics_proxy_->GetStats().qp_sum);
127}
128
129TEST_F(ReceiveStatisticsProxyTest, OnDecodedFrameWithoutQpResetsQpSum) {
130 EXPECT_EQ(rtc::Optional<uint64_t>(), statistics_proxy_->GetStats().qp_sum);
ilnik00d802b2017-04-11 10:34:31 -0700131 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(3u),
132 VideoContentType::UNSPECIFIED);
sakalcc452e12017-02-09 04:53:45 -0800133 EXPECT_EQ(rtc::Optional<uint64_t>(3u), statistics_proxy_->GetStats().qp_sum);
ilnik00d802b2017-04-11 10:34:31 -0700134 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(),
135 VideoContentType::UNSPECIFIED);
sakalcc452e12017-02-09 04:53:45 -0800136 EXPECT_EQ(rtc::Optional<uint64_t>(), statistics_proxy_->GetStats().qp_sum);
137}
138
hbos50cfe1f2017-01-23 07:21:55 -0800139TEST_F(ReceiveStatisticsProxyTest, OnRenderedFrameIncreasesFramesRendered) {
140 EXPECT_EQ(0u, statistics_proxy_->GetStats().frames_rendered);
ilnik00d802b2017-04-11 10:34:31 -0700141 webrtc::VideoFrame frame(webrtc::I420Buffer::Create(1, 1), 0, 0,
142 webrtc::kVideoRotation_0);
hbos50cfe1f2017-01-23 07:21:55 -0800143 for (uint32_t i = 1; i <= 3; ++i) {
144 statistics_proxy_->OnRenderedFrame(frame);
145 EXPECT_EQ(i, statistics_proxy_->GetStats().frames_rendered);
146 }
147}
148
asapersson46c4e3c2016-11-03 06:48:19 -0700149TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsSsrc) {
150 EXPECT_EQ(kRemoteSsrc, statistics_proxy_->GetStats().ssrc);
151}
152
153TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsIncomingPayloadType) {
154 const int kPayloadType = 111;
155 statistics_proxy_->OnIncomingPayloadType(kPayloadType);
156 EXPECT_EQ(kPayloadType, statistics_proxy_->GetStats().current_payload_type);
157}
158
asapersson6966bd52017-01-03 00:44:06 -0800159TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsDecoderImplementationName) {
160 const char* kName = "decoderName";
161 statistics_proxy_->OnDecoderImplementationName(kName);
162 EXPECT_STREQ(
163 kName, statistics_proxy_->GetStats().decoder_implementation_name.c_str());
164}
165
philipela45102f2017-02-22 05:30:39 -0800166TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsOnCompleteFrame) {
167 const int kFrameSizeBytes = 1000;
168 statistics_proxy_->OnCompleteFrame(true, kFrameSizeBytes);
169 VideoReceiveStream::Stats stats = statistics_proxy_->GetStats();
170 EXPECT_EQ(1, stats.network_frame_rate);
philipela45102f2017-02-22 05:30:39 -0800171 EXPECT_EQ(1, stats.frame_counts.key_frames);
172 EXPECT_EQ(0, stats.frame_counts.delta_frames);
asapersson46c4e3c2016-11-03 06:48:19 -0700173}
174
175TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsDecodeTimingStats) {
176 const int kDecodeMs = 1;
177 const int kMaxDecodeMs = 2;
178 const int kCurrentDelayMs = 3;
179 const int kTargetDelayMs = 4;
180 const int kJitterBufferMs = 5;
181 const int kMinPlayoutDelayMs = 6;
182 const int kRenderDelayMs = 7;
183 const int64_t kRttMs = 8;
philipela45102f2017-02-22 05:30:39 -0800184 statistics_proxy_->OnRttUpdate(kRttMs, 0);
185 statistics_proxy_->OnFrameBufferTimingsUpdated(
asapersson46c4e3c2016-11-03 06:48:19 -0700186 kDecodeMs, kMaxDecodeMs, kCurrentDelayMs, kTargetDelayMs, kJitterBufferMs,
philipela45102f2017-02-22 05:30:39 -0800187 kMinPlayoutDelayMs, kRenderDelayMs);
asapersson46c4e3c2016-11-03 06:48:19 -0700188 VideoReceiveStream::Stats stats = statistics_proxy_->GetStats();
189 EXPECT_EQ(kDecodeMs, stats.decode_ms);
190 EXPECT_EQ(kMaxDecodeMs, stats.max_decode_ms);
191 EXPECT_EQ(kCurrentDelayMs, stats.current_delay_ms);
192 EXPECT_EQ(kTargetDelayMs, stats.target_delay_ms);
193 EXPECT_EQ(kJitterBufferMs, stats.jitter_buffer_ms);
194 EXPECT_EQ(kMinPlayoutDelayMs, stats.min_playout_delay_ms);
195 EXPECT_EQ(kRenderDelayMs, stats.render_delay_ms);
196}
197
asapersson6966bd52017-01-03 00:44:06 -0800198TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsRtcpPacketTypeCounts) {
199 const uint32_t kFirPackets = 33;
200 const uint32_t kPliPackets = 44;
201 const uint32_t kNackPackets = 55;
202 RtcpPacketTypeCounter counter;
203 counter.fir_packets = kFirPackets;
204 counter.pli_packets = kPliPackets;
205 counter.nack_packets = kNackPackets;
206 statistics_proxy_->RtcpPacketTypesCounterUpdated(kRemoteSsrc, counter);
207 VideoReceiveStream::Stats stats = statistics_proxy_->GetStats();
208 EXPECT_EQ(kFirPackets, stats.rtcp_packet_type_counts.fir_packets);
209 EXPECT_EQ(kPliPackets, stats.rtcp_packet_type_counts.pli_packets);
210 EXPECT_EQ(kNackPackets, stats.rtcp_packet_type_counts.nack_packets);
211}
212
213TEST_F(ReceiveStatisticsProxyTest,
214 GetStatsReportsNoRtcpPacketTypeCountsForUnknownSsrc) {
215 RtcpPacketTypeCounter counter;
216 counter.fir_packets = 33;
217 statistics_proxy_->RtcpPacketTypesCounterUpdated(kRemoteSsrc + 1, counter);
218 EXPECT_EQ(0u,
219 statistics_proxy_->GetStats().rtcp_packet_type_counts.fir_packets);
220}
221
222TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsFrameCounts) {
223 const int kKeyFrames = 3;
224 const int kDeltaFrames = 22;
225 FrameCounts frame_counts;
226 frame_counts.key_frames = kKeyFrames;
227 frame_counts.delta_frames = kDeltaFrames;
228 statistics_proxy_->OnFrameCountsUpdated(frame_counts);
229 VideoReceiveStream::Stats stats = statistics_proxy_->GetStats();
230 EXPECT_EQ(kKeyFrames, stats.frame_counts.key_frames);
231 EXPECT_EQ(kDeltaFrames, stats.frame_counts.delta_frames);
232}
233
asapersson46c4e3c2016-11-03 06:48:19 -0700234TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsDiscardedPackets) {
235 const int kDiscardedPackets = 12;
236 statistics_proxy_->OnDiscardedPacketsUpdated(kDiscardedPackets);
237 EXPECT_EQ(kDiscardedPackets, statistics_proxy_->GetStats().discarded_packets);
238}
239
asapersson6966bd52017-01-03 00:44:06 -0800240TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsRtcpStats) {
241 const uint8_t kFracLost = 0;
242 const uint32_t kCumLost = 1;
243 const uint32_t kExtSeqNum = 10;
244 const uint32_t kJitter = 4;
245
246 RtcpStatistics rtcp_stats;
247 rtcp_stats.fraction_lost = kFracLost;
srte186d9c32017-08-04 05:03:53 -0700248 rtcp_stats.packets_lost = kCumLost;
249 rtcp_stats.extended_highest_sequence_number = kExtSeqNum;
asapersson6966bd52017-01-03 00:44:06 -0800250 rtcp_stats.jitter = kJitter;
251 statistics_proxy_->StatisticsUpdated(rtcp_stats, kRemoteSsrc);
252
253 VideoReceiveStream::Stats stats = statistics_proxy_->GetStats();
254 EXPECT_EQ(kFracLost, stats.rtcp_stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700255 EXPECT_EQ(kCumLost, stats.rtcp_stats.packets_lost);
256 EXPECT_EQ(kExtSeqNum, stats.rtcp_stats.extended_highest_sequence_number);
asapersson6966bd52017-01-03 00:44:06 -0800257 EXPECT_EQ(kJitter, stats.rtcp_stats.jitter);
258}
259
260TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsCName) {
261 const char* kName = "cName";
262 statistics_proxy_->CNameChanged(kName, kRemoteSsrc);
263 EXPECT_STREQ(kName, statistics_proxy_->GetStats().c_name.c_str());
264}
265
266TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsNoCNameForUnknownSsrc) {
267 const char* kName = "cName";
268 statistics_proxy_->CNameChanged(kName, kRemoteSsrc + 1);
269 EXPECT_STREQ("", statistics_proxy_->GetStats().c_name.c_str());
270}
271
ilnik2edc6842017-07-06 03:06:50 -0700272TEST_F(ReceiveStatisticsProxyTest,
273 GetTimingFrameInfoReportsLongestTimingFrame) {
274 const int64_t kShortEndToEndDelay = 10;
275 const int64_t kMedEndToEndDelay = 20;
276 const int64_t kLongEndToEndDelay = 100;
277 const uint32_t kExpectedRtpTimestamp = 2;
278 TimingFrameInfo info;
279 rtc::Optional<TimingFrameInfo> result;
280 info.rtp_timestamp = kExpectedRtpTimestamp - 1;
281 info.capture_time_ms = 0;
282 info.decode_finish_ms = kShortEndToEndDelay;
283 statistics_proxy_->OnTimingFrameInfoUpdated(info);
284 info.rtp_timestamp =
285 kExpectedRtpTimestamp; // this frame should be reported in the end.
286 info.capture_time_ms = 0;
287 info.decode_finish_ms = kLongEndToEndDelay;
288 statistics_proxy_->OnTimingFrameInfoUpdated(info);
289 info.rtp_timestamp = kExpectedRtpTimestamp + 1;
290 info.capture_time_ms = 0;
291 info.decode_finish_ms = kMedEndToEndDelay;
292 statistics_proxy_->OnTimingFrameInfoUpdated(info);
293 result = statistics_proxy_->GetAndResetTimingFrameInfo();
294 EXPECT_TRUE(result);
295 EXPECT_EQ(kExpectedRtpTimestamp, result->rtp_timestamp);
296}
297
298TEST_F(ReceiveStatisticsProxyTest, GetTimingFrameInfoTimingFramesReportedOnce) {
299 const int64_t kShortEndToEndDelay = 10;
300 const uint32_t kExpectedRtpTimestamp = 2;
301 TimingFrameInfo info;
302 rtc::Optional<TimingFrameInfo> result;
303 info.rtp_timestamp = kExpectedRtpTimestamp;
304 info.capture_time_ms = 0;
305 info.decode_finish_ms = kShortEndToEndDelay;
306 statistics_proxy_->OnTimingFrameInfoUpdated(info);
307 result = statistics_proxy_->GetAndResetTimingFrameInfo();
308 EXPECT_TRUE(result);
309 EXPECT_EQ(kExpectedRtpTimestamp, result->rtp_timestamp);
310 result = statistics_proxy_->GetAndResetTimingFrameInfo();
311 EXPECT_FALSE(result);
312}
313
asapersson46c4e3c2016-11-03 06:48:19 -0700314TEST_F(ReceiveStatisticsProxyTest, LifetimeHistogramIsUpdated) {
315 const int64_t kTimeSec = 3;
316 fake_clock_.AdvanceTimeMilliseconds(kTimeSec * 1000);
317 // Histograms are updated when the statistics_proxy_ is deleted.
318 statistics_proxy_.reset();
319 EXPECT_EQ(1,
320 metrics::NumSamples("WebRTC.Video.ReceiveStreamLifetimeInSeconds"));
321 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.ReceiveStreamLifetimeInSeconds",
322 kTimeSec));
323}
324
palmkvista40672a2017-01-13 05:58:34 -0800325TEST_F(ReceiveStatisticsProxyTest, BadCallHistogramsAreUpdated) {
326 // Based on the tuning parameters this will produce 7 uncertain states,
327 // then 10 certainly bad states. There has to be 10 certain states before
328 // any histograms are recorded.
329 const int kNumBadSamples = 17;
330
331 StreamDataCounters counters;
332 counters.first_packet_time_ms = fake_clock_.TimeInMilliseconds();
333 statistics_proxy_->DataCountersUpdated(counters, config_.rtp.remote_ssrc);
334
335 for (int i = 0; i < kNumBadSamples; ++i) {
336 // Since OnRenderedFrame is never called the fps in each sample will be 0,
337 // i.e. bad
338 fake_clock_.AdvanceTimeMilliseconds(1000);
339 statistics_proxy_->OnIncomingRate(0, 0);
340 }
341 // Histograms are updated when the statistics_proxy_ is deleted.
342 statistics_proxy_.reset();
343 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.BadCall.Any"));
344 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.BadCall.Any", 100));
345
346 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.BadCall.FrameRate"));
347 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.BadCall.FrameRate", 100));
348
349 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.BadCall.FrameRateVariance"));
350
351 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.BadCall.Qp"));
352}
353
asapersson0c43f772016-11-30 01:42:26 -0800354TEST_F(ReceiveStatisticsProxyTest, PacketLossHistogramIsUpdated) {
355 const uint32_t kCumLost1 = 1;
356 const uint32_t kExtSeqNum1 = 10;
357 const uint32_t kCumLost2 = 2;
358 const uint32_t kExtSeqNum2 = 20;
359
360 // One report block received.
361 RtcpStatistics rtcp_stats1;
srte186d9c32017-08-04 05:03:53 -0700362 rtcp_stats1.packets_lost = kCumLost1;
363 rtcp_stats1.extended_highest_sequence_number = kExtSeqNum1;
asapersson0c43f772016-11-30 01:42:26 -0800364 statistics_proxy_->StatisticsUpdated(rtcp_stats1, kRemoteSsrc);
365
366 // Two report blocks received.
367 RtcpStatistics rtcp_stats2;
srte186d9c32017-08-04 05:03:53 -0700368 rtcp_stats2.packets_lost = kCumLost2;
369 rtcp_stats2.extended_highest_sequence_number = kExtSeqNum2;
asapersson0c43f772016-11-30 01:42:26 -0800370 statistics_proxy_->StatisticsUpdated(rtcp_stats2, kRemoteSsrc);
371
372 // Two received report blocks but min run time has not passed.
373 fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds * 1000 - 1);
374 SetUp(); // Reset stat proxy causes histograms to be updated.
375 EXPECT_EQ(0,
376 metrics::NumSamples("WebRTC.Video.ReceivedPacketsLostInPercent"));
377
378 // Two report blocks received.
379 statistics_proxy_->StatisticsUpdated(rtcp_stats1, kRemoteSsrc);
380 statistics_proxy_->StatisticsUpdated(rtcp_stats2, kRemoteSsrc);
381
382 // Two received report blocks and min run time has passed.
383 fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds * 1000);
384 SetUp();
385 EXPECT_EQ(1,
386 metrics::NumSamples("WebRTC.Video.ReceivedPacketsLostInPercent"));
387 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.ReceivedPacketsLostInPercent",
388 (kCumLost2 - kCumLost1) * 100 /
389 (kExtSeqNum2 - kExtSeqNum1)));
390}
391
392TEST_F(ReceiveStatisticsProxyTest,
393 PacketLossHistogramIsNotUpdatedIfLessThanTwoReportBlocksAreReceived) {
394 RtcpStatistics rtcp_stats1;
srte186d9c32017-08-04 05:03:53 -0700395 rtcp_stats1.packets_lost = 1;
396 rtcp_stats1.extended_highest_sequence_number = 10;
asapersson0c43f772016-11-30 01:42:26 -0800397
398 // Min run time has passed but no received report block.
399 fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds * 1000);
400 SetUp(); // Reset stat proxy causes histograms to be updated.
401 EXPECT_EQ(0,
402 metrics::NumSamples("WebRTC.Video.ReceivedPacketsLostInPercent"));
403
404 // Min run time has passed but only one received report block.
405 statistics_proxy_->StatisticsUpdated(rtcp_stats1, kRemoteSsrc);
406 fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds * 1000);
407 SetUp();
408 EXPECT_EQ(0,
409 metrics::NumSamples("WebRTC.Video.ReceivedPacketsLostInPercent"));
410}
411
asapersson2077f2f2017-05-11 05:37:35 -0700412TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsAvSyncOffset) {
413 const int64_t kSyncOffsetMs = 22;
414 const double kFreqKhz = 90.0;
415 EXPECT_EQ(std::numeric_limits<int>::max(),
416 statistics_proxy_->GetStats().sync_offset_ms);
417 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz);
418 EXPECT_EQ(kSyncOffsetMs, statistics_proxy_->GetStats().sync_offset_ms);
419}
420
asapersson46c4e3c2016-11-03 06:48:19 -0700421TEST_F(ReceiveStatisticsProxyTest, AvSyncOffsetHistogramIsUpdated) {
422 const int64_t kSyncOffsetMs = 22;
423 const double kFreqKhz = 90.0;
424 for (int i = 0; i < kMinRequiredSamples; ++i)
425 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz);
426 // Histograms are updated when the statistics_proxy_ is deleted.
427 statistics_proxy_.reset();
428 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.AVSyncOffsetInMs"));
429 EXPECT_EQ(1,
430 metrics::NumEvents("WebRTC.Video.AVSyncOffsetInMs", kSyncOffsetMs));
431}
432
asaperssonde9e5ff2016-11-02 07:14:03 -0700433TEST_F(ReceiveStatisticsProxyTest, RtpToNtpFrequencyOffsetHistogramIsUpdated) {
434 const int64_t kSyncOffsetMs = 22;
435 const double kFreqKhz = 90.0;
436 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz);
437 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz + 2.2);
438 fake_clock_.AdvanceTimeMilliseconds(kFreqOffsetProcessIntervalInMs);
439 // Process interval passed, max diff: 2.
440 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz + 1.1);
441 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz - 4.2);
442 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz - 0.9);
443 fake_clock_.AdvanceTimeMilliseconds(kFreqOffsetProcessIntervalInMs);
444 // Process interval passed, max diff: 4.
445 statistics_proxy_->OnSyncOffsetUpdated(kSyncOffsetMs, kFreqKhz);
446 statistics_proxy_.reset();
447 // Average reported: (2 + 4) / 2 = 3.
448 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.RtpToNtpFreqOffsetInKhz"));
449 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.RtpToNtpFreqOffsetInKhz", 3));
450}
451
asapersson6966bd52017-01-03 00:44:06 -0800452TEST_F(ReceiveStatisticsProxyTest, Vp8QpHistogramIsUpdated) {
453 const int kQp = 22;
454 EncodedImage encoded_image;
455 encoded_image.qp_ = kQp;
456 CodecSpecificInfo codec_info;
457 codec_info.codecType = kVideoCodecVP8;
458
459 for (int i = 0; i < kMinRequiredSamples; ++i)
460 statistics_proxy_->OnPreDecode(encoded_image, &codec_info);
461
462 statistics_proxy_.reset();
463 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.Decoded.Vp8.Qp"));
464 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.Decoded.Vp8.Qp", kQp));
465}
466
467TEST_F(ReceiveStatisticsProxyTest, Vp8QpHistogramIsNotUpdatedForTooFewSamples) {
468 EncodedImage encoded_image;
469 encoded_image.qp_ = 22;
470 CodecSpecificInfo codec_info;
471 codec_info.codecType = kVideoCodecVP8;
472
473 for (int i = 0; i < kMinRequiredSamples - 1; ++i)
474 statistics_proxy_->OnPreDecode(encoded_image, &codec_info);
475
476 statistics_proxy_.reset();
477 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.Decoded.Vp8.Qp"));
478}
479
480TEST_F(ReceiveStatisticsProxyTest, Vp8QpHistogramIsNotUpdatedIfNoQpValue) {
481 EncodedImage encoded_image;
482 CodecSpecificInfo codec_info;
483 codec_info.codecType = kVideoCodecVP8;
484
485 for (int i = 0; i < kMinRequiredSamples; ++i)
486 statistics_proxy_->OnPreDecode(encoded_image, &codec_info);
487
488 statistics_proxy_.reset();
489 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.Decoded.Vp8.Qp"));
490}
491
asaperssonb99baf82017-04-20 04:05:43 -0700492TEST_F(ReceiveStatisticsProxyTest,
493 KeyFrameHistogramNotUpdatedForTooFewSamples) {
494 const bool kIsKeyFrame = false;
495 const int kFrameSizeBytes = 1000;
496
497 for (int i = 0; i < kMinRequiredSamples - 1; ++i)
498 statistics_proxy_->OnCompleteFrame(kIsKeyFrame, kFrameSizeBytes);
499
500 EXPECT_EQ(0, statistics_proxy_->GetStats().frame_counts.key_frames);
501 EXPECT_EQ(kMinRequiredSamples - 1,
502 statistics_proxy_->GetStats().frame_counts.delta_frames);
503
504 statistics_proxy_.reset();
505 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.KeyFramesReceivedInPermille"));
506}
507
508TEST_F(ReceiveStatisticsProxyTest,
509 KeyFrameHistogramUpdatedForMinRequiredSamples) {
510 const bool kIsKeyFrame = false;
511 const int kFrameSizeBytes = 1000;
512
513 for (int i = 0; i < kMinRequiredSamples; ++i)
514 statistics_proxy_->OnCompleteFrame(kIsKeyFrame, kFrameSizeBytes);
515
516 EXPECT_EQ(0, statistics_proxy_->GetStats().frame_counts.key_frames);
517 EXPECT_EQ(kMinRequiredSamples,
518 statistics_proxy_->GetStats().frame_counts.delta_frames);
519
520 statistics_proxy_.reset();
521 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.KeyFramesReceivedInPermille"));
522 EXPECT_EQ(1,
523 metrics::NumEvents("WebRTC.Video.KeyFramesReceivedInPermille", 0));
524}
525
526TEST_F(ReceiveStatisticsProxyTest, KeyFrameHistogramIsUpdated) {
527 const int kFrameSizeBytes = 1000;
528
529 for (int i = 0; i < kMinRequiredSamples; ++i)
530 statistics_proxy_->OnCompleteFrame(true, kFrameSizeBytes);
531
532 for (int i = 0; i < kMinRequiredSamples; ++i)
533 statistics_proxy_->OnCompleteFrame(false, kFrameSizeBytes);
534
535 EXPECT_EQ(kMinRequiredSamples,
536 statistics_proxy_->GetStats().frame_counts.key_frames);
537 EXPECT_EQ(kMinRequiredSamples,
538 statistics_proxy_->GetStats().frame_counts.delta_frames);
539
540 statistics_proxy_.reset();
541 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.KeyFramesReceivedInPermille"));
542 EXPECT_EQ(
543 1, metrics::NumEvents("WebRTC.Video.KeyFramesReceivedInPermille", 500));
544}
545
546TEST_F(ReceiveStatisticsProxyTest, TimingHistogramsNotUpdatedForTooFewSamples) {
547 const int kDecodeMs = 1;
548 const int kMaxDecodeMs = 2;
549 const int kCurrentDelayMs = 3;
550 const int kTargetDelayMs = 4;
551 const int kJitterBufferMs = 5;
552 const int kMinPlayoutDelayMs = 6;
553 const int kRenderDelayMs = 7;
554
555 for (int i = 0; i < kMinRequiredSamples - 1; ++i) {
556 statistics_proxy_->OnFrameBufferTimingsUpdated(
557 kDecodeMs, kMaxDecodeMs, kCurrentDelayMs, kTargetDelayMs,
558 kJitterBufferMs, kMinPlayoutDelayMs, kRenderDelayMs);
559 }
560
561 statistics_proxy_.reset();
562 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.DecodeTimeInMs"));
563 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.JitterBufferDelayInMs"));
564 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.TargetDelayInMs"));
565 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.CurrentDelayInMs"));
566 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.OnewayDelayInMs"));
567}
568
569TEST_F(ReceiveStatisticsProxyTest, TimingHistogramsAreUpdated) {
570 const int kDecodeMs = 1;
571 const int kMaxDecodeMs = 2;
572 const int kCurrentDelayMs = 3;
573 const int kTargetDelayMs = 4;
574 const int kJitterBufferMs = 5;
575 const int kMinPlayoutDelayMs = 6;
576 const int kRenderDelayMs = 7;
577
578 for (int i = 0; i < kMinRequiredSamples; ++i) {
579 statistics_proxy_->OnFrameBufferTimingsUpdated(
580 kDecodeMs, kMaxDecodeMs, kCurrentDelayMs, kTargetDelayMs,
581 kJitterBufferMs, kMinPlayoutDelayMs, kRenderDelayMs);
582 }
583
584 statistics_proxy_.reset();
585 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.DecodeTimeInMs"));
586 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.JitterBufferDelayInMs"));
587 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.TargetDelayInMs"));
588 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.CurrentDelayInMs"));
589 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.OnewayDelayInMs"));
590
591 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.DecodeTimeInMs", kDecodeMs));
592 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.JitterBufferDelayInMs",
593 kJitterBufferMs));
594 EXPECT_EQ(1,
595 metrics::NumEvents("WebRTC.Video.TargetDelayInMs", kTargetDelayMs));
596 EXPECT_EQ(
597 1, metrics::NumEvents("WebRTC.Video.CurrentDelayInMs", kCurrentDelayMs));
598 EXPECT_EQ(1,
599 metrics::NumEvents("WebRTC.Video.OnewayDelayInMs", kTargetDelayMs));
600}
601
sprang948b2752017-05-04 02:47:13 -0700602TEST_F(ReceiveStatisticsProxyTest, DoesNotReportStaleFramerates) {
603 const int kDefaultFps = 30;
604 const int kWidth = 320;
605 const int kHeight = 240;
606
607 rtc::scoped_refptr<VideoFrameBuffer> video_frame_buffer(
608 I420Buffer::Create(kWidth, kHeight));
609 VideoFrame frame(video_frame_buffer, kVideoRotation_0, 0);
610
611 for (int i = 0; i < kDefaultFps; ++i) {
612 // Since OnRenderedFrame is never called the fps in each sample will be 0,
613 // i.e. bad
614 frame.set_ntp_time_ms(fake_clock_.CurrentNtpInMilliseconds());
615 statistics_proxy_->OnDecodedFrame(rtc::Optional<uint8_t>(),
616 VideoContentType::UNSPECIFIED);
617 statistics_proxy_->OnRenderedFrame(frame);
618 fake_clock_.AdvanceTimeMilliseconds(1000 / kDefaultFps);
619 }
620
621 EXPECT_EQ(kDefaultFps, statistics_proxy_->GetStats().decode_frame_rate);
622 EXPECT_EQ(kDefaultFps, statistics_proxy_->GetStats().render_frame_rate);
623
624 // FPS trackers in stats proxy have a 1000ms sliding window.
625 fake_clock_.AdvanceTimeMilliseconds(1000);
626 EXPECT_EQ(0, statistics_proxy_->GetStats().decode_frame_rate);
627 EXPECT_EQ(0, statistics_proxy_->GetStats().render_frame_rate);
628}
629
asapersson2077f2f2017-05-11 05:37:35 -0700630TEST_F(ReceiveStatisticsProxyTest, GetStatsReportsReceivedFrameStats) {
631 const int kWidth = 160;
632 const int kHeight = 120;
633 EXPECT_EQ(0, statistics_proxy_->GetStats().width);
634 EXPECT_EQ(0, statistics_proxy_->GetStats().height);
635 EXPECT_EQ(0u, statistics_proxy_->GetStats().frames_rendered);
636
637 statistics_proxy_->OnRenderedFrame(CreateFrame(kWidth, kHeight));
638
639 EXPECT_EQ(kWidth, statistics_proxy_->GetStats().width);
640 EXPECT_EQ(kHeight, statistics_proxy_->GetStats().height);
641 EXPECT_EQ(1u, statistics_proxy_->GetStats().frames_rendered);
642}
643
644TEST_F(ReceiveStatisticsProxyTest,
645 ReceivedFrameHistogramsAreNotUpdatedForTooFewSamples) {
646 const int kWidth = 160;
647 const int kHeight = 120;
648
649 for (int i = 0; i < kMinRequiredSamples - 1; ++i)
650 statistics_proxy_->OnRenderedFrame(CreateFrame(kWidth, kHeight));
651
652 statistics_proxy_.reset();
653 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.ReceivedWidthInPixels"));
654 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.ReceivedHeightInPixels"));
655 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.RenderFramesPerSecond"));
656 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.RenderSqrtPixelsPerSecond"));
657}
658
659TEST_F(ReceiveStatisticsProxyTest, ReceivedFrameHistogramsAreUpdated) {
660 const int kWidth = 160;
661 const int kHeight = 120;
662
663 for (int i = 0; i < kMinRequiredSamples; ++i)
664 statistics_proxy_->OnRenderedFrame(CreateFrame(kWidth, kHeight));
665
666 statistics_proxy_.reset();
667 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.ReceivedWidthInPixels"));
668 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.ReceivedHeightInPixels"));
669 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.RenderFramesPerSecond"));
670 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.RenderSqrtPixelsPerSecond"));
671 EXPECT_EQ(1,
672 metrics::NumEvents("WebRTC.Video.ReceivedWidthInPixels", kWidth));
673 EXPECT_EQ(1,
674 metrics::NumEvents("WebRTC.Video.ReceivedHeightInPixels", kHeight));
675}
676
677TEST_F(ReceiveStatisticsProxyTest,
678 RtcpHistogramsNotUpdatedIfMinRuntimeHasNotPassed) {
679 InsertFirstRtpPacket(kRemoteSsrc);
680 fake_clock_.AdvanceTimeMilliseconds((metrics::kMinRunTimeInSeconds * 1000) -
681 1);
682
683 RtcpPacketTypeCounter counter;
684 statistics_proxy_->RtcpPacketTypesCounterUpdated(kRemoteSsrc, counter);
685
686 statistics_proxy_.reset();
687 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.FirPacketsSentPerMinute"));
688 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.PliPacketsSentPerMinute"));
689 EXPECT_EQ(0, metrics::NumSamples("WebRTC.Video.NackPacketsSentPerMinute"));
690}
691
692TEST_F(ReceiveStatisticsProxyTest, RtcpHistogramsAreUpdated) {
693 InsertFirstRtpPacket(kRemoteSsrc);
694 fake_clock_.AdvanceTimeMilliseconds(metrics::kMinRunTimeInSeconds * 1000);
695
696 const uint32_t kFirPackets = 100;
697 const uint32_t kPliPackets = 200;
698 const uint32_t kNackPackets = 300;
699
700 RtcpPacketTypeCounter counter;
701 counter.fir_packets = kFirPackets;
702 counter.pli_packets = kPliPackets;
703 counter.nack_packets = kNackPackets;
704 statistics_proxy_->RtcpPacketTypesCounterUpdated(kRemoteSsrc, counter);
705
706 statistics_proxy_.reset();
707 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.FirPacketsSentPerMinute"));
708 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.PliPacketsSentPerMinute"));
709 EXPECT_EQ(1, metrics::NumSamples("WebRTC.Video.NackPacketsSentPerMinute"));
710 EXPECT_EQ(
711 1, metrics::NumEvents("WebRTC.Video.FirPacketsSentPerMinute",
712 kFirPackets * 60 / metrics::kMinRunTimeInSeconds));
713 EXPECT_EQ(
714 1, metrics::NumEvents("WebRTC.Video.PliPacketsSentPerMinute",
715 kPliPackets * 60 / metrics::kMinRunTimeInSeconds));
716 EXPECT_EQ(
717 1, metrics::NumEvents("WebRTC.Video.NackPacketsSentPerMinute",
718 kNackPackets * 60 / metrics::kMinRunTimeInSeconds));
719}
720
sakale5ba44e2016-10-26 07:09:24 -0700721} // namespace webrtc