blob: 174e70df105989ae58c26497ff68ad8fd2d57417 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "system_wrappers/include/metrics.h"
14#include "system_wrappers/include/metrics_default.h"
15#include "test/gtest.h"
asapersson35151f32016-05-02 23:44:01 -070016
17namespace webrtc {
18namespace {
19const uint32_t kSsrc1 = 17;
20const uint32_t kSsrc2 = 42;
21const uint32_t kRtxSsrc1 = 18;
22const uint32_t kRtxSsrc2 = 43;
23const uint16_t kPacketId = 2345;
24const int64_t kMaxPacketDelayMs = 11000;
asapersson40f54002016-06-09 00:09:22 -070025const int kMinRequiredPeriodicSamples = 5;
26const int kProcessIntervalMs = 2000;
asapersson35151f32016-05-02 23:44:01 -070027} // namespace
28
29class SendDelayStatsTest : public ::testing::Test {
30 public:
31 SendDelayStatsTest() : clock_(1234), config_(CreateConfig()) {}
32 virtual ~SendDelayStatsTest() {}
33
34 protected:
35 virtual void SetUp() {
36 stats_.reset(new SendDelayStats(&clock_));
37 stats_->AddSsrcs(config_);
38 }
39
40 VideoSendStream::Config CreateConfig() {
41 VideoSendStream::Config config(nullptr);
42 config.rtp.ssrcs.push_back(kSsrc1);
43 config.rtp.ssrcs.push_back(kSsrc2);
44 config.rtp.rtx.ssrcs.push_back(kRtxSsrc1);
45 config.rtp.rtx.ssrcs.push_back(kRtxSsrc2);
46 return config;
47 }
48
49 void OnSendPacket(uint16_t id, uint32_t ssrc) {
50 OnSendPacket(id, ssrc, clock_.TimeInMilliseconds());
51 }
52
53 void OnSendPacket(uint16_t id, uint32_t ssrc, int64_t capture_ms) {
54 SendPacketObserver* observer = stats_.get();
55 observer->OnSendPacket(id, capture_ms, ssrc);
56 }
57
58 bool OnSentPacket(uint16_t id) {
59 return stats_->OnSentPacket(id, clock_.TimeInMilliseconds());
60 }
61
62 SimulatedClock clock_;
63 VideoSendStream::Config config_;
64 std::unique_ptr<SendDelayStats> stats_;
65};
66
67TEST_F(SendDelayStatsTest, SentPacketFound) {
68 EXPECT_FALSE(OnSentPacket(kPacketId));
69 OnSendPacket(kPacketId, kSsrc1);
70 EXPECT_TRUE(OnSentPacket(kPacketId)); // Packet found.
71 EXPECT_FALSE(OnSentPacket(kPacketId)); // Packet removed when found.
72}
73
74TEST_F(SendDelayStatsTest, SentPacketNotFoundForNonRegisteredSsrc) {
75 OnSendPacket(kPacketId, kSsrc1);
76 EXPECT_TRUE(OnSentPacket(kPacketId));
77 OnSendPacket(kPacketId + 1, kSsrc2);
78 EXPECT_TRUE(OnSentPacket(kPacketId + 1));
79 OnSendPacket(kPacketId + 2, kRtxSsrc1); // RTX SSRC not registered.
80 EXPECT_FALSE(OnSentPacket(kPacketId + 2));
81}
82
83TEST_F(SendDelayStatsTest, SentPacketFoundWithMaxSendDelay) {
84 OnSendPacket(kPacketId, kSsrc1);
85 clock_.AdvanceTimeMilliseconds(kMaxPacketDelayMs - 1);
86 OnSendPacket(kPacketId + 1, kSsrc1); // kPacketId -> not old/removed.
87 EXPECT_TRUE(OnSentPacket(kPacketId)); // Packet found.
88 EXPECT_TRUE(OnSentPacket(kPacketId + 1)); // Packet found.
89}
90
91TEST_F(SendDelayStatsTest, OldPacketsRemoved) {
92 const int64_t kCaptureTimeMs = clock_.TimeInMilliseconds();
93 OnSendPacket(0xffffu, kSsrc1, kCaptureTimeMs);
94 OnSendPacket(0u, kSsrc1, kCaptureTimeMs);
95 OnSendPacket(1u, kSsrc1, kCaptureTimeMs + 1);
96 clock_.AdvanceTimeMilliseconds(kMaxPacketDelayMs); // 0xffff, 0 -> old.
97 OnSendPacket(2u, kSsrc1, kCaptureTimeMs + 2);
98
99 EXPECT_FALSE(OnSentPacket(0xffffu)); // Old removed.
100 EXPECT_FALSE(OnSentPacket(0u)); // Old removed.
101 EXPECT_TRUE(OnSentPacket(1u));
102 EXPECT_TRUE(OnSentPacket(2u));
103}
104
105TEST_F(SendDelayStatsTest, HistogramsAreUpdated) {
asapersson01d70a32016-05-20 06:29:46 -0700106 metrics::Reset();
asapersson35151f32016-05-02 23:44:01 -0700107 const int64_t kDelayMs1 = 5;
asapersson40f54002016-06-09 00:09:22 -0700108 const int64_t kDelayMs2 = 15;
109 const int kNumSamples = kMinRequiredPeriodicSamples * kProcessIntervalMs /
110 (kDelayMs1 + kDelayMs2) + 1;
111
asapersson35151f32016-05-02 23:44:01 -0700112 uint16_t id = 0;
asapersson40f54002016-06-09 00:09:22 -0700113 for (int i = 0; i < kNumSamples; ++i) {
asapersson35151f32016-05-02 23:44:01 -0700114 OnSendPacket(++id, kSsrc1);
115 clock_.AdvanceTimeMilliseconds(kDelayMs1);
116 EXPECT_TRUE(OnSentPacket(id));
117 OnSendPacket(++id, kSsrc2);
118 clock_.AdvanceTimeMilliseconds(kDelayMs2);
119 EXPECT_TRUE(OnSentPacket(id));
120 }
121 stats_.reset();
asapersson01d70a32016-05-20 06:29:46 -0700122 EXPECT_EQ(2, metrics::NumSamples("WebRTC.Video.SendDelayInMs"));
123 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.SendDelayInMs", kDelayMs1));
124 EXPECT_EQ(1, metrics::NumEvents("WebRTC.Video.SendDelayInMs", kDelayMs2));
asapersson35151f32016-05-02 23:44:01 -0700125}
126
127} // namespace webrtc