asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/send_delay_stats.h" |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "system_wrappers/include/metrics.h" |
| 14 | #include "system_wrappers/include/metrics_default.h" |
| 15 | #include "test/gtest.h" |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace { |
| 19 | const uint32_t kSsrc1 = 17; |
| 20 | const uint32_t kSsrc2 = 42; |
| 21 | const uint32_t kRtxSsrc1 = 18; |
| 22 | const uint32_t kRtxSsrc2 = 43; |
| 23 | const uint16_t kPacketId = 2345; |
| 24 | const int64_t kMaxPacketDelayMs = 11000; |
asapersson | 40f5400 | 2016-06-09 00:09:22 -0700 | [diff] [blame] | 25 | const int kMinRequiredPeriodicSamples = 5; |
| 26 | const int kProcessIntervalMs = 2000; |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 27 | } // namespace |
| 28 | |
| 29 | class 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 | |
| 67 | TEST_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 | |
| 74 | TEST_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 | |
| 83 | TEST_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 | |
| 91 | TEST_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 | |
| 105 | TEST_F(SendDelayStatsTest, HistogramsAreUpdated) { |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 106 | metrics::Reset(); |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 107 | const int64_t kDelayMs1 = 5; |
asapersson | 40f5400 | 2016-06-09 00:09:22 -0700 | [diff] [blame] | 108 | const int64_t kDelayMs2 = 15; |
| 109 | const int kNumSamples = kMinRequiredPeriodicSamples * kProcessIntervalMs / |
| 110 | (kDelayMs1 + kDelayMs2) + 1; |
| 111 | |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 112 | uint16_t id = 0; |
asapersson | 40f5400 | 2016-06-09 00:09:22 -0700 | [diff] [blame] | 113 | for (int i = 0; i < kNumSamples; ++i) { |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 114 | 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(); |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 122 | 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)); |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace webrtc |