blob: 37969e4489e5ebc4fa3a6b9502b5c083361980bf [file] [log] [blame]
asapersson35151f32016-05-02 23:44:01 -07001/*
2 * Copyright (c) 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/send_delay_stats.h"
asapersson35151f32016-05-02 23:44:01 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstdint>
14#include <vector>
15
16#include "call/rtp_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/gtest.h"
asapersson35151f32016-05-02 23:44:01 -070019
20namespace webrtc {
21namespace {
22const uint32_t kSsrc1 = 17;
23const uint32_t kSsrc2 = 42;
24const uint32_t kRtxSsrc1 = 18;
25const uint32_t kRtxSsrc2 = 43;
26const uint16_t kPacketId = 2345;
27const int64_t kMaxPacketDelayMs = 11000;
asapersson40f54002016-06-09 00:09:22 -070028const int kMinRequiredPeriodicSamples = 5;
29const int kProcessIntervalMs = 2000;
asapersson35151f32016-05-02 23:44:01 -070030} // namespace
31
32class SendDelayStatsTest : public ::testing::Test {
33 public:
34 SendDelayStatsTest() : clock_(1234), config_(CreateConfig()) {}
35 virtual ~SendDelayStatsTest() {}
36
37 protected:
38 virtual void SetUp() {
39 stats_.reset(new SendDelayStats(&clock_));
40 stats_->AddSsrcs(config_);
41 }
42
43 VideoSendStream::Config CreateConfig() {
44 VideoSendStream::Config config(nullptr);
45 config.rtp.ssrcs.push_back(kSsrc1);
46 config.rtp.ssrcs.push_back(kSsrc2);
47 config.rtp.rtx.ssrcs.push_back(kRtxSsrc1);
48 config.rtp.rtx.ssrcs.push_back(kRtxSsrc2);
49 return config;
50 }
51
52 void OnSendPacket(uint16_t id, uint32_t ssrc) {
53 OnSendPacket(id, ssrc, clock_.TimeInMilliseconds());
54 }
55
56 void OnSendPacket(uint16_t id, uint32_t ssrc, int64_t capture_ms) {
57 SendPacketObserver* observer = stats_.get();
58 observer->OnSendPacket(id, capture_ms, ssrc);
59 }
60
61 bool OnSentPacket(uint16_t id) {
62 return stats_->OnSentPacket(id, clock_.TimeInMilliseconds());
63 }
64
65 SimulatedClock clock_;
66 VideoSendStream::Config config_;
67 std::unique_ptr<SendDelayStats> stats_;
68};
69
70TEST_F(SendDelayStatsTest, SentPacketFound) {
71 EXPECT_FALSE(OnSentPacket(kPacketId));
72 OnSendPacket(kPacketId, kSsrc1);
73 EXPECT_TRUE(OnSentPacket(kPacketId)); // Packet found.
74 EXPECT_FALSE(OnSentPacket(kPacketId)); // Packet removed when found.
75}
76
77TEST_F(SendDelayStatsTest, SentPacketNotFoundForNonRegisteredSsrc) {
78 OnSendPacket(kPacketId, kSsrc1);
79 EXPECT_TRUE(OnSentPacket(kPacketId));
80 OnSendPacket(kPacketId + 1, kSsrc2);
81 EXPECT_TRUE(OnSentPacket(kPacketId + 1));
82 OnSendPacket(kPacketId + 2, kRtxSsrc1); // RTX SSRC not registered.
83 EXPECT_FALSE(OnSentPacket(kPacketId + 2));
84}
85
86TEST_F(SendDelayStatsTest, SentPacketFoundWithMaxSendDelay) {
87 OnSendPacket(kPacketId, kSsrc1);
88 clock_.AdvanceTimeMilliseconds(kMaxPacketDelayMs - 1);
89 OnSendPacket(kPacketId + 1, kSsrc1); // kPacketId -> not old/removed.
90 EXPECT_TRUE(OnSentPacket(kPacketId)); // Packet found.
91 EXPECT_TRUE(OnSentPacket(kPacketId + 1)); // Packet found.
92}
93
94TEST_F(SendDelayStatsTest, OldPacketsRemoved) {
95 const int64_t kCaptureTimeMs = clock_.TimeInMilliseconds();
96 OnSendPacket(0xffffu, kSsrc1, kCaptureTimeMs);
97 OnSendPacket(0u, kSsrc1, kCaptureTimeMs);
98 OnSendPacket(1u, kSsrc1, kCaptureTimeMs + 1);
99 clock_.AdvanceTimeMilliseconds(kMaxPacketDelayMs); // 0xffff, 0 -> old.
100 OnSendPacket(2u, kSsrc1, kCaptureTimeMs + 2);
101
102 EXPECT_FALSE(OnSentPacket(0xffffu)); // Old removed.
103 EXPECT_FALSE(OnSentPacket(0u)); // Old removed.
104 EXPECT_TRUE(OnSentPacket(1u));
105 EXPECT_TRUE(OnSentPacket(2u));
106}
107
108TEST_F(SendDelayStatsTest, HistogramsAreUpdated) {
asapersson01d70a32016-05-20 06:29:46 -0700109 metrics::Reset();
asapersson35151f32016-05-02 23:44:01 -0700110 const int64_t kDelayMs1 = 5;
asapersson40f54002016-06-09 00:09:22 -0700111 const int64_t kDelayMs2 = 15;
112 const int kNumSamples = kMinRequiredPeriodicSamples * kProcessIntervalMs /
Yves Gerey665174f2018-06-19 15:03:05 +0200113 (kDelayMs1 + kDelayMs2) +
114 1;
asapersson40f54002016-06-09 00:09:22 -0700115
asapersson35151f32016-05-02 23:44:01 -0700116 uint16_t id = 0;
asapersson40f54002016-06-09 00:09:22 -0700117 for (int i = 0; i < kNumSamples; ++i) {
asapersson35151f32016-05-02 23:44:01 -0700118 OnSendPacket(++id, kSsrc1);
119 clock_.AdvanceTimeMilliseconds(kDelayMs1);
120 EXPECT_TRUE(OnSentPacket(id));
121 OnSendPacket(++id, kSsrc2);
122 clock_.AdvanceTimeMilliseconds(kDelayMs2);
123 EXPECT_TRUE(OnSentPacket(id));
124 }
125 stats_.reset();
asapersson01d70a32016-05-20 06:29:46 -0700126 EXPECT_EQ(2, metrics::NumSamples("WebRTC.Video.SendDelayInMs"));
127 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.SendDelayInMs", kDelayMs1));
128 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.SendDelayInMs", kDelayMs2));
asapersson35151f32016-05-02 23:44:01 -0700129}
130
131} // namespace webrtc