Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / net / rtp / receiver_stats_unittest.cc
blob4bde4a30c7ef8b2086f58a426b11adf643457485
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <gtest/gtest.h>
7 #include <stdint.h>
9 #include "base/test/simple_test_tick_clock.h"
10 #include "base/time/time.h"
11 #include "media/cast/net/rtp/receiver_stats.h"
12 #include "media/cast/net/rtp/rtp_receiver_defines.h"
14 namespace media {
15 namespace cast {
17 static const int64 kStartMillisecond = INT64_C(12345678900000);
18 static const uint32 kStdTimeIncrementMs = 33;
20 class ReceiverStatsTest : public ::testing::Test {
21 protected:
22 ReceiverStatsTest()
23 : stats_(&testing_clock_) {
24 testing_clock_.Advance(
25 base::TimeDelta::FromMilliseconds(kStartMillisecond));
26 start_time_ = testing_clock_.NowTicks();
27 delta_increments_ = base::TimeDelta::FromMilliseconds(kStdTimeIncrementMs);
29 ~ReceiverStatsTest() override {}
31 uint32 ExpectedJitter(uint32 const_interval, int num_packets) {
32 float jitter = 0;
33 // Assume timestamps have a constant kStdTimeIncrementMs interval.
34 float float_interval =
35 static_cast<float>(const_interval - kStdTimeIncrementMs);
36 for (int i = 0; i < num_packets; ++i) {
37 jitter += (float_interval - jitter) / 16;
39 return static_cast<uint32>(jitter + 0.5f);
42 ReceiverStats stats_;
43 RtpCastHeader rtp_header_;
44 base::SimpleTestTickClock testing_clock_;
45 base::TimeTicks start_time_;
46 base::TimeDelta delta_increments_;
48 DISALLOW_COPY_AND_ASSIGN(ReceiverStatsTest);
51 TEST_F(ReceiverStatsTest, ResetState) {
52 RtpReceiverStatistics s = stats_.GetStatistics();
53 EXPECT_EQ(0u, s.fraction_lost);
54 EXPECT_EQ(0u, s.cumulative_lost);
55 EXPECT_EQ(0u, s.extended_high_sequence_number);
56 EXPECT_EQ(0u, s.jitter);
59 TEST_F(ReceiverStatsTest, LossCount) {
60 for (int i = 0; i < 300; ++i) {
61 if (i % 4)
62 stats_.UpdateStatistics(rtp_header_);
63 if (i % 3) {
64 rtp_header_.rtp_timestamp += 33 * 90;
66 ++rtp_header_.sequence_number;
67 testing_clock_.Advance(delta_increments_);
69 RtpReceiverStatistics s = stats_.GetStatistics();
70 EXPECT_EQ(63u, s.fraction_lost);
71 EXPECT_EQ(74u, s.cumulative_lost);
72 // Build extended sequence number.
73 const uint32 extended_seq_num = rtp_header_.sequence_number - 1;
74 EXPECT_EQ(extended_seq_num, s.extended_high_sequence_number);
77 TEST_F(ReceiverStatsTest, NoLossWrap) {
78 rtp_header_.sequence_number = 65500;
79 for (int i = 0; i < 300; ++i) {
80 stats_.UpdateStatistics(rtp_header_);
81 if (i % 3) {
82 rtp_header_.rtp_timestamp += 33 * 90;
84 ++rtp_header_.sequence_number;
85 testing_clock_.Advance(delta_increments_);
87 RtpReceiverStatistics s = stats_.GetStatistics();
88 EXPECT_EQ(0u, s.fraction_lost);
89 EXPECT_EQ(0u, s.cumulative_lost);
90 // Build extended sequence number (one wrap cycle).
91 const uint32 extended_seq_num = (1 << 16) + rtp_header_.sequence_number - 1;
92 EXPECT_EQ(extended_seq_num, s.extended_high_sequence_number);
95 TEST_F(ReceiverStatsTest, LossCountWrap) {
96 const uint32 kStartSequenceNumber = 65500;
97 rtp_header_.sequence_number = kStartSequenceNumber;
98 for (int i = 0; i < 300; ++i) {
99 if (i % 4)
100 stats_.UpdateStatistics(rtp_header_);
101 if (i % 3)
102 // Update timestamp.
103 ++rtp_header_.rtp_timestamp;
104 ++rtp_header_.sequence_number;
105 testing_clock_.Advance(delta_increments_);
107 RtpReceiverStatistics s = stats_.GetStatistics();
108 EXPECT_EQ(63u, s.fraction_lost);
109 EXPECT_EQ(74u, s.cumulative_lost);
110 // Build extended sequence number (one wrap cycle).
111 const uint32 extended_seq_num = (1 << 16) + rtp_header_.sequence_number - 1;
112 EXPECT_EQ(extended_seq_num, s.extended_high_sequence_number);
115 TEST_F(ReceiverStatsTest, BasicJitter) {
116 for (int i = 0; i < 300; ++i) {
117 stats_.UpdateStatistics(rtp_header_);
118 ++rtp_header_.sequence_number;
119 rtp_header_.rtp_timestamp += 33 * 90;
120 testing_clock_.Advance(delta_increments_);
122 RtpReceiverStatistics s = stats_.GetStatistics();
123 EXPECT_FALSE(s.fraction_lost);
124 EXPECT_FALSE(s.cumulative_lost);
125 // Build extended sequence number (one wrap cycle).
126 const uint32 extended_seq_num = rtp_header_.sequence_number - 1;
127 EXPECT_EQ(extended_seq_num, s.extended_high_sequence_number);
128 EXPECT_EQ(ExpectedJitter(kStdTimeIncrementMs, 300), s.jitter);
131 TEST_F(ReceiverStatsTest, NonTrivialJitter) {
132 const int kAdditionalIncrement = 5;
133 for (int i = 0; i < 300; ++i) {
134 stats_.UpdateStatistics(rtp_header_);
135 ++rtp_header_.sequence_number;
136 rtp_header_.rtp_timestamp += 33 * 90;
137 base::TimeDelta additional_delta =
138 base::TimeDelta::FromMilliseconds(kAdditionalIncrement);
139 testing_clock_.Advance(delta_increments_ + additional_delta);
141 RtpReceiverStatistics s = stats_.GetStatistics();
142 EXPECT_FALSE(s.fraction_lost);
143 EXPECT_FALSE(s.cumulative_lost);
144 // Build extended sequence number (one wrap cycle).
145 const uint32 extended_seq_num = rtp_header_.sequence_number - 1;
146 EXPECT_EQ(extended_seq_num, s.extended_high_sequence_number);
147 EXPECT_EQ(ExpectedJitter(kStdTimeIncrementMs + kAdditionalIncrement, 300),
148 s.jitter);
151 } // namespace cast
152 } // namespace media