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 "base/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/test/simple_test_tick_clock.h"
8 #include "base/time/tick_clock.h"
9 #include "media/cast/cast_environment.h"
10 #include "media/cast/logging/logging_defines.h"
11 #include "media/cast/logging/receiver_time_offset_estimator_impl.h"
12 #include "media/cast/test/fake_single_thread_task_runner.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 class ReceiverTimeOffsetEstimatorImplTest
: public ::testing::Test
{
20 ReceiverTimeOffsetEstimatorImplTest()
21 : sender_clock_(new base::SimpleTestTickClock()),
22 task_runner_(new test::FakeSingleThreadTaskRunner(sender_clock_
)),
23 cast_environment_(new CastEnvironment(
24 scoped_ptr
<base::TickClock
>(sender_clock_
).Pass(),
28 cast_environment_
->Logging()->AddRawEventSubscriber(&estimator_
);
31 ~ReceiverTimeOffsetEstimatorImplTest() override
{
32 cast_environment_
->Logging()->RemoveRawEventSubscriber(&estimator_
);
35 void AdvanceClocks(base::TimeDelta time
) {
36 sender_clock_
->Advance(time
);
37 receiver_clock_
.Advance(time
);
40 base::SimpleTestTickClock
* sender_clock_
; // Owned by CastEnvironment.
41 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
42 scoped_refptr
<CastEnvironment
> cast_environment_
;
43 base::SimpleTestTickClock receiver_clock_
;
44 ReceiverTimeOffsetEstimatorImpl estimator_
;
47 // Suppose the true offset is 100ms.
48 // Event A occurred at sender time 20ms.
49 // Event B occurred at receiver time 130ms. (sender time 30ms)
50 // Event C occurred at sender time 60ms.
51 // Then the bound after all 3 events have arrived is [130-60=70, 130-20=110].
52 TEST_F(ReceiverTimeOffsetEstimatorImplTest
, EstimateOffset
) {
53 int64 true_offset_ms
= 100;
54 receiver_clock_
.Advance(base::TimeDelta::FromMilliseconds(true_offset_ms
));
56 base::TimeDelta lower_bound
;
57 base::TimeDelta upper_bound
;
59 EXPECT_FALSE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
61 RtpTimestamp rtp_timestamp
= 0;
64 AdvanceClocks(base::TimeDelta::FromMilliseconds(20));
66 cast_environment_
->Logging()->InsertEncodedFrameEvent(
67 sender_clock_
->NowTicks(),
68 FRAME_ENCODED
, VIDEO_EVENT
,
75 cast_environment_
->Logging()->InsertPacketEvent(
76 sender_clock_
->NowTicks(),
77 PACKET_SENT_TO_NETWORK
, VIDEO_EVENT
,
82 EXPECT_FALSE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
84 AdvanceClocks(base::TimeDelta::FromMilliseconds(10));
85 cast_environment_
->Logging()->InsertFrameEvent(
86 receiver_clock_
.NowTicks(), FRAME_ACK_SENT
, VIDEO_EVENT
,
87 rtp_timestamp
, frame_id
);
89 cast_environment_
->Logging()->InsertPacketEvent(
90 receiver_clock_
.NowTicks(),
91 PACKET_RECEIVED
, VIDEO_EVENT
,
96 EXPECT_FALSE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
98 AdvanceClocks(base::TimeDelta::FromMilliseconds(30));
99 cast_environment_
->Logging()->InsertFrameEvent(
100 sender_clock_
->NowTicks(), FRAME_ACK_RECEIVED
, VIDEO_EVENT
,
101 rtp_timestamp
, frame_id
);
103 EXPECT_TRUE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
105 int64 lower_bound_ms
= lower_bound
.InMilliseconds();
106 int64 upper_bound_ms
= upper_bound
.InMilliseconds();
107 EXPECT_EQ(70, lower_bound_ms
);
108 EXPECT_EQ(110, upper_bound_ms
);
109 EXPECT_GE(true_offset_ms
, lower_bound_ms
);
110 EXPECT_LE(true_offset_ms
, upper_bound_ms
);
113 // Same scenario as above, but event C arrives before event B. It doens't mean
114 // event C occurred before event B.
115 TEST_F(ReceiverTimeOffsetEstimatorImplTest
, EventCArrivesBeforeEventB
) {
116 int64 true_offset_ms
= 100;
117 receiver_clock_
.Advance(base::TimeDelta::FromMilliseconds(true_offset_ms
));
119 base::TimeDelta lower_bound
;
120 base::TimeDelta upper_bound
;
122 EXPECT_FALSE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
124 RtpTimestamp rtp_timestamp
= 0;
127 AdvanceClocks(base::TimeDelta::FromMilliseconds(20));
129 cast_environment_
->Logging()->InsertEncodedFrameEvent(
130 sender_clock_
->NowTicks(),
131 FRAME_ENCODED
, VIDEO_EVENT
,
138 cast_environment_
->Logging()->InsertPacketEvent(
139 sender_clock_
->NowTicks(),
140 PACKET_SENT_TO_NETWORK
, VIDEO_EVENT
,
145 EXPECT_FALSE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
147 AdvanceClocks(base::TimeDelta::FromMilliseconds(10));
148 base::TimeTicks event_b_time
= receiver_clock_
.NowTicks();
149 AdvanceClocks(base::TimeDelta::FromMilliseconds(30));
150 base::TimeTicks event_c_time
= sender_clock_
->NowTicks();
152 cast_environment_
->Logging()->InsertFrameEvent(
153 event_c_time
, FRAME_ACK_RECEIVED
, VIDEO_EVENT
, rtp_timestamp
, frame_id
);
155 EXPECT_FALSE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
157 cast_environment_
->Logging()->InsertPacketEvent(
159 PACKET_RECEIVED
, VIDEO_EVENT
,
164 cast_environment_
->Logging()->InsertFrameEvent(
165 event_b_time
, FRAME_ACK_SENT
, VIDEO_EVENT
, rtp_timestamp
, frame_id
);
167 EXPECT_TRUE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
169 int64 lower_bound_ms
= lower_bound
.InMilliseconds();
170 int64 upper_bound_ms
= upper_bound
.InMilliseconds();
171 EXPECT_EQ(70, lower_bound_ms
);
172 EXPECT_EQ(110, upper_bound_ms
);
173 EXPECT_GE(true_offset_ms
, lower_bound_ms
);
174 EXPECT_LE(true_offset_ms
, upper_bound_ms
);
177 TEST_F(ReceiverTimeOffsetEstimatorImplTest
, MultipleIterations
) {
178 int64 true_offset_ms
= 100;
179 receiver_clock_
.Advance(base::TimeDelta::FromMilliseconds(true_offset_ms
));
181 base::TimeDelta lower_bound
;
182 base::TimeDelta upper_bound
;
184 RtpTimestamp rtp_timestamp_a
= 0;
186 RtpTimestamp rtp_timestamp_b
= 90;
188 RtpTimestamp rtp_timestamp_c
= 180;
191 // Frame 1 times: [20, 30+100, 60]
192 // Frame 2 times: [30, 50+100, 55]
193 // Frame 3 times: [77, 80+100, 110]
194 // Bound should end up at [95, 103]
195 // Events times in chronological order: 20, 30 x2, 50, 55, 60, 77, 80, 110
196 AdvanceClocks(base::TimeDelta::FromMilliseconds(20));
197 cast_environment_
->Logging()->InsertEncodedFrameEvent(
198 sender_clock_
->NowTicks(),
199 FRAME_ENCODED
, VIDEO_EVENT
,
206 cast_environment_
->Logging()->InsertPacketEvent(
207 sender_clock_
->NowTicks(),
208 PACKET_SENT_TO_NETWORK
, VIDEO_EVENT
,
213 AdvanceClocks(base::TimeDelta::FromMilliseconds(10));
214 cast_environment_
->Logging()->InsertEncodedFrameEvent(
215 sender_clock_
->NowTicks(),
216 FRAME_ENCODED
, VIDEO_EVENT
,
223 cast_environment_
->Logging()->InsertPacketEvent(
224 sender_clock_
->NowTicks(),
225 PACKET_SENT_TO_NETWORK
, VIDEO_EVENT
,
230 cast_environment_
->Logging()->InsertFrameEvent(
231 receiver_clock_
.NowTicks(), FRAME_ACK_SENT
, VIDEO_EVENT
,
232 rtp_timestamp_a
, frame_id_a
);
234 AdvanceClocks(base::TimeDelta::FromMilliseconds(20));
236 cast_environment_
->Logging()->InsertPacketEvent(
237 receiver_clock_
.NowTicks(),
238 PACKET_RECEIVED
, VIDEO_EVENT
,
243 cast_environment_
->Logging()->InsertFrameEvent(
244 receiver_clock_
.NowTicks(), FRAME_ACK_SENT
, VIDEO_EVENT
,
245 rtp_timestamp_b
, frame_id_b
);
247 AdvanceClocks(base::TimeDelta::FromMilliseconds(5));
248 cast_environment_
->Logging()->InsertFrameEvent(sender_clock_
->NowTicks(),
254 AdvanceClocks(base::TimeDelta::FromMilliseconds(5));
255 cast_environment_
->Logging()->InsertFrameEvent(sender_clock_
->NowTicks(),
261 AdvanceClocks(base::TimeDelta::FromMilliseconds(17));
262 cast_environment_
->Logging()->InsertEncodedFrameEvent(
263 sender_clock_
->NowTicks(),
264 FRAME_ENCODED
, VIDEO_EVENT
,
271 cast_environment_
->Logging()->InsertPacketEvent(
272 sender_clock_
->NowTicks(),
273 PACKET_SENT_TO_NETWORK
, VIDEO_EVENT
,
278 AdvanceClocks(base::TimeDelta::FromMilliseconds(3));
279 cast_environment_
->Logging()->InsertPacketEvent(
280 receiver_clock_
.NowTicks(),
281 PACKET_RECEIVED
, VIDEO_EVENT
,
286 cast_environment_
->Logging()->InsertFrameEvent(
287 receiver_clock_
.NowTicks(), FRAME_ACK_SENT
, VIDEO_EVENT
,
288 rtp_timestamp_c
, frame_id_c
);
290 AdvanceClocks(base::TimeDelta::FromMilliseconds(30));
291 cast_environment_
->Logging()->InsertFrameEvent(sender_clock_
->NowTicks(),
297 EXPECT_TRUE(estimator_
.GetReceiverOffsetBounds(&lower_bound
, &upper_bound
));
298 int64 lower_bound_ms
= lower_bound
.InMilliseconds();
299 int64 upper_bound_ms
= upper_bound
.InMilliseconds();
300 EXPECT_GT(lower_bound_ms
, 90);
301 EXPECT_LE(lower_bound_ms
, true_offset_ms
);
302 EXPECT_LT(upper_bound_ms
, 150);
303 EXPECT_GT(upper_bound_ms
, true_offset_ms
);