Index: media/cast/rtcp/rtcp_unittest.cc |
diff --git a/media/cast/rtcp/rtcp_unittest.cc b/media/cast/rtcp/rtcp_unittest.cc |
index 16aaef24f33510f35a9419324f67e4a6c278cd30..16477c1df9a147a2e5541d7f61a8d4891994ec2e 100644 |
--- a/media/cast/rtcp/rtcp_unittest.cc |
+++ b/media/cast/rtcp/rtcp_unittest.cc |
@@ -26,7 +26,6 @@ static const uint32 kSenderSsrc = 0x10203; |
static const uint32 kReceiverSsrc = 0x40506; |
static const std::string kCName("test@10.1.1.1"); |
static const uint32 kRtcpIntervalMs = 500; |
-static const int64 kStartMillisecond = INT64_C(12345678900000); |
static const int64 kAddedDelay = 123; |
static const int64 kAddedShortDelay = 100; |
@@ -143,7 +142,7 @@ class RtcpPeer : public Rtcp { |
c_name, |
true) {} |
- using Rtcp::CheckForWrapAround; |
+ using Rtcp::OnReceivedNtp; |
using Rtcp::OnReceivedLipSyncInfo; |
}; |
@@ -160,8 +159,7 @@ class RtcpTest : public ::testing::Test { |
sender_to_receiver_(testing_clock_), |
receiver_to_sender_(cast_environment_, testing_clock_), |
rtp_sender_stats_(kVideoFrequency) { |
- testing_clock_->Advance( |
- base::TimeDelta::FromMilliseconds(kStartMillisecond)); |
+ testing_clock_->Advance(base::TimeTicks::Now() - base::TimeTicks()); |
net::IPEndPoint dummy_endpoint; |
transport_sender_.reset(new transport::CastTransportSenderImpl( |
NULL, |
@@ -204,8 +202,7 @@ class RtcpTest : public ::testing::Test { |
}; |
TEST_F(RtcpTest, TimeToSend) { |
- base::TimeTicks start_time; |
- start_time += base::TimeDelta::FromMilliseconds(kStartMillisecond); |
+ const base::TimeTicks start_time = cast_environment_->Clock()->NowTicks(); |
Rtcp rtcp(cast_environment_, |
&mock_sender_feedback_, |
transport_sender_.get(), |
@@ -510,136 +507,167 @@ TEST_F(RtcpTest, NtpAndTime) { |
const int64 kSecondsbetweenYear1900and2030 = INT64_C(47481 * 24 * 60 * 60); |
uint32 ntp_seconds_1 = 0; |
- uint32 ntp_fractions_1 = 0; |
+ uint32 ntp_fraction_1 = 0; |
base::TimeTicks input_time = base::TimeTicks::Now(); |
- ConvertTimeTicksToNtp(input_time, &ntp_seconds_1, &ntp_fractions_1); |
+ ConvertTimeTicksToNtp(input_time, &ntp_seconds_1, &ntp_fraction_1); |
// Verify absolute value. |
EXPECT_GT(ntp_seconds_1, kSecondsbetweenYear1900and2010); |
EXPECT_LT(ntp_seconds_1, kSecondsbetweenYear1900and2030); |
- base::TimeTicks out_1 = ConvertNtpToTimeTicks(ntp_seconds_1, ntp_fractions_1); |
+ base::TimeTicks out_1 = ConvertNtpToTimeTicks(ntp_seconds_1, ntp_fraction_1); |
EXPECT_EQ(input_time, out_1); // Verify inverse. |
base::TimeDelta time_delta = base::TimeDelta::FromMilliseconds(1000); |
input_time += time_delta; |
uint32 ntp_seconds_2 = 0; |
- uint32 ntp_fractions_2 = 0; |
+ uint32 ntp_fraction_2 = 0; |
- ConvertTimeTicksToNtp(input_time, &ntp_seconds_2, &ntp_fractions_2); |
- base::TimeTicks out_2 = ConvertNtpToTimeTicks(ntp_seconds_2, ntp_fractions_2); |
+ ConvertTimeTicksToNtp(input_time, &ntp_seconds_2, &ntp_fraction_2); |
+ base::TimeTicks out_2 = ConvertNtpToTimeTicks(ntp_seconds_2, ntp_fraction_2); |
EXPECT_EQ(input_time, out_2); // Verify inverse. |
// Verify delta. |
EXPECT_EQ((out_2 - out_1), time_delta); |
EXPECT_EQ((ntp_seconds_2 - ntp_seconds_1), UINT32_C(1)); |
- EXPECT_NEAR(ntp_fractions_2, ntp_fractions_1, 1); |
+ EXPECT_NEAR(ntp_fraction_2, ntp_fraction_1, 1); |
time_delta = base::TimeDelta::FromMilliseconds(500); |
input_time += time_delta; |
uint32 ntp_seconds_3 = 0; |
- uint32 ntp_fractions_3 = 0; |
+ uint32 ntp_fraction_3 = 0; |
- ConvertTimeTicksToNtp(input_time, &ntp_seconds_3, &ntp_fractions_3); |
- base::TimeTicks out_3 = ConvertNtpToTimeTicks(ntp_seconds_3, ntp_fractions_3); |
+ ConvertTimeTicksToNtp(input_time, &ntp_seconds_3, &ntp_fraction_3); |
+ base::TimeTicks out_3 = ConvertNtpToTimeTicks(ntp_seconds_3, ntp_fraction_3); |
EXPECT_EQ(input_time, out_3); // Verify inverse. |
// Verify delta. |
EXPECT_EQ((out_3 - out_2), time_delta); |
- EXPECT_NEAR((ntp_fractions_3 - ntp_fractions_2), 0xffffffff / 2, 1); |
+ EXPECT_NEAR((ntp_fraction_3 - ntp_fraction_2), 0xffffffff / 2, 1); |
} |
-TEST_F(RtcpTest, WrapAround) { |
- RtcpPeer rtcp_peer(cast_environment_, |
- &mock_sender_feedback_, |
- transport_sender_.get(), |
- NULL, |
- NULL, |
- kRtcpReducedSize, |
- base::TimeDelta::FromMilliseconds(kRtcpIntervalMs), |
- kReceiverSsrc, |
- kSenderSsrc, |
- kCName); |
- uint32 new_timestamp = 0; |
- uint32 old_timestamp = 0; |
- EXPECT_EQ(0, rtcp_peer.CheckForWrapAround(new_timestamp, old_timestamp)); |
- new_timestamp = 1234567890; |
- old_timestamp = 1234567000; |
- EXPECT_EQ(0, rtcp_peer.CheckForWrapAround(new_timestamp, old_timestamp)); |
- new_timestamp = 1234567000; |
- old_timestamp = 1234567890; |
- EXPECT_EQ(0, rtcp_peer.CheckForWrapAround(new_timestamp, old_timestamp)); |
- new_timestamp = 123; |
- old_timestamp = 4234567890u; |
- EXPECT_EQ(1, rtcp_peer.CheckForWrapAround(new_timestamp, old_timestamp)); |
- new_timestamp = 4234567890u; |
- old_timestamp = 123; |
- EXPECT_EQ(-1, rtcp_peer.CheckForWrapAround(new_timestamp, old_timestamp)); |
+TEST_F(RtcpTest, UsesReportsToConvertRemoteNtpTimeToLocalTimeTicks) { |
+ RtcpPeer rtcp(cast_environment_, |
+ &mock_sender_feedback_, |
+ transport_sender_.get(), |
+ &receiver_to_sender_, |
+ NULL, |
+ kRtcpReducedSize, |
+ base::TimeDelta::FromMilliseconds(kRtcpIntervalMs), |
+ kReceiverSsrc, |
+ kSenderSsrc, |
+ kCName); |
+ |
+ // Start out with a simulated "remote clock" that is perfectly synchronized to |
+ // the local clock. |
+ base::SimpleTestTickClock remote_clock; |
+ remote_clock.Advance(testing_clock_->NowTicks() - base::TimeTicks()); |
+ uint32 remote_ntp_seconds = 0; |
+ uint32 remote_ntp_fraction = 0; |
+ ConvertTimeTicksToNtp( |
+ remote_clock.NowTicks(), &remote_ntp_seconds, &remote_ntp_fraction); |
+ |
+ // Expect the operation fails before the first call to OnReceivedNtp(). |
+ EXPECT_TRUE(rtcp.ToApproximateLocalTime(remote_ntp_seconds, |
+ remote_ntp_fraction).is_null()); |
+ |
+ // Now receive the NTP timestamp and expect ToApproximateLocalTime() will |
+ // return current time according to both clocks. |
+ rtcp.OnReceivedNtp(remote_ntp_seconds, remote_ntp_fraction); |
+ uint32 ntp_seconds = remote_ntp_seconds; |
+ uint32 ntp_fraction = remote_ntp_fraction; |
+ EXPECT_EQ(testing_clock_->NowTicks(), |
+ rtcp.ToApproximateLocalTime(ntp_seconds, ntp_fraction)); |
+ EXPECT_EQ(remote_clock.NowTicks(), |
+ rtcp.ToApproximateLocalTime(ntp_seconds, ntp_fraction)); |
+ |
+ // Now advance the local clock and receive the same NTP timestamp again, and |
+ // expect ToApproximateLocalTime() will still return the current local time. |
+ const base::TimeDelta local_advance_by = |
+ base::TimeDelta::FromMicroseconds(1234567); |
+ testing_clock_->Advance(local_advance_by); |
+ EXPECT_EQ(testing_clock_->NowTicks() - local_advance_by, |
+ rtcp.ToApproximateLocalTime(ntp_seconds, ntp_fraction)); |
+ rtcp.OnReceivedNtp(remote_ntp_seconds, remote_ntp_fraction); |
+ EXPECT_EQ(testing_clock_->NowTicks(), |
+ rtcp.ToApproximateLocalTime(ntp_seconds, ntp_fraction)); |
+ |
+ // Advance sender clock and provide a new NTP timestamp. When |
+ // ToApproximateLocalTime() is called with the old NTP timestamp, expect to |
+ // get a result that is backwards in time by the same amount the sender clock |
+ // was advanced. |
+ const base::TimeDelta sender_ahead_by = |
+ base::TimeDelta::FromMicroseconds(987654); |
+ remote_clock.Advance(sender_ahead_by); |
+ ConvertTimeTicksToNtp( |
+ remote_clock.NowTicks(), &remote_ntp_seconds, &remote_ntp_fraction); |
+ rtcp.OnReceivedNtp(remote_ntp_seconds, remote_ntp_fraction); |
+ EXPECT_EQ(testing_clock_->NowTicks() - sender_ahead_by, |
+ rtcp.ToApproximateLocalTime(ntp_seconds, ntp_fraction)); |
} |
-TEST_F(RtcpTest, RtpTimestampInSenderTime) { |
- RtcpPeer rtcp_peer(cast_environment_, |
- &mock_sender_feedback_, |
- transport_sender_.get(), |
- &receiver_to_sender_, |
- NULL, |
- kRtcpReducedSize, |
- base::TimeDelta::FromMilliseconds(kRtcpIntervalMs), |
- kReceiverSsrc, |
- kSenderSsrc, |
- kCName); |
- int frequency = 32000; |
+TEST_F(RtcpTest, UsesLipSyncToExtrapolateCaptureTime) { |
+ RtcpPeer rtcp(cast_environment_, |
+ &mock_sender_feedback_, |
+ transport_sender_.get(), |
+ &receiver_to_sender_, |
+ NULL, |
+ kRtcpReducedSize, |
+ base::TimeDelta::FromMilliseconds(kRtcpIntervalMs), |
+ kReceiverSsrc, |
+ kSenderSsrc, |
+ kCName); |
+ const int frequency = 32000; |
uint32 rtp_timestamp = 64000; |
- base::TimeTicks rtp_timestamp_in_ticks; |
- // Test fail before we get a OnReceivedLipSyncInfo. |
- EXPECT_FALSE(rtcp_peer.RtpTimestampInSenderTime( |
- frequency, rtp_timestamp, &rtp_timestamp_in_ticks)); |
+ // Expect the operation fails before we get the first OnReceivedLipSyncInfo. |
+ EXPECT_TRUE( |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency).is_null()); |
uint32 ntp_seconds = 0; |
- uint32 ntp_fractions = 0; |
- uint64 input_time_us = 12345678901000LL; |
- base::TimeTicks input_time; |
- input_time += base::TimeDelta::FromMicroseconds(input_time_us); |
+ uint32 ntp_fraction = 0; |
+ const base::TimeTicks lip_sync_time = cast_environment_->Clock()->NowTicks(); |
// Test exact match. |
- ConvertTimeTicksToNtp(input_time, &ntp_seconds, &ntp_fractions); |
- rtcp_peer.OnReceivedLipSyncInfo(rtp_timestamp, ntp_seconds, ntp_fractions); |
- EXPECT_TRUE(rtcp_peer.RtpTimestampInSenderTime( |
- frequency, rtp_timestamp, &rtp_timestamp_in_ticks)); |
- EXPECT_EQ(input_time, rtp_timestamp_in_ticks); |
+ ConvertTimeTicksToNtp(lip_sync_time, &ntp_seconds, &ntp_fraction); |
+ rtcp.OnReceivedNtp(ntp_seconds, ntp_fraction); |
+ rtcp.OnReceivedLipSyncInfo(rtp_timestamp, ntp_seconds, ntp_fraction); |
+ EXPECT_FALSE( |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency).is_null()); |
+ EXPECT_EQ(lip_sync_time, |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency)); |
// Test older rtp_timestamp. |
rtp_timestamp = 32000; |
- EXPECT_TRUE(rtcp_peer.RtpTimestampInSenderTime( |
- frequency, rtp_timestamp, &rtp_timestamp_in_ticks)); |
- EXPECT_EQ(input_time - base::TimeDelta::FromMilliseconds(1000), |
- rtp_timestamp_in_ticks); |
+ EXPECT_FALSE( |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency).is_null()); |
+ EXPECT_EQ(lip_sync_time - base::TimeDelta::FromMilliseconds(1000), |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency)); |
// Test older rtp_timestamp with wrap. |
rtp_timestamp = 4294903296u; |
- EXPECT_TRUE(rtcp_peer.RtpTimestampInSenderTime( |
- frequency, rtp_timestamp, &rtp_timestamp_in_ticks)); |
- EXPECT_EQ(input_time - base::TimeDelta::FromMilliseconds(4000), |
- rtp_timestamp_in_ticks); |
+ EXPECT_FALSE( |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency).is_null()); |
+ EXPECT_EQ(lip_sync_time - base::TimeDelta::FromMilliseconds(4000), |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency)); |
// Test newer rtp_timestamp. |
rtp_timestamp = 128000; |
- EXPECT_TRUE(rtcp_peer.RtpTimestampInSenderTime( |
- frequency, rtp_timestamp, &rtp_timestamp_in_ticks)); |
- EXPECT_EQ(input_time + base::TimeDelta::FromMilliseconds(2000), |
- rtp_timestamp_in_ticks); |
+ EXPECT_FALSE( |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency).is_null()); |
+ EXPECT_EQ(lip_sync_time + base::TimeDelta::FromMilliseconds(2000), |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency)); |
// Test newer rtp_timestamp with wrap. |
rtp_timestamp = 4294903296u; |
- rtcp_peer.OnReceivedLipSyncInfo(rtp_timestamp, ntp_seconds, ntp_fractions); |
+ rtcp.OnReceivedLipSyncInfo(rtp_timestamp, ntp_seconds, ntp_fraction); |
rtp_timestamp = 64000; |
- EXPECT_TRUE(rtcp_peer.RtpTimestampInSenderTime( |
- frequency, rtp_timestamp, &rtp_timestamp_in_ticks)); |
- EXPECT_EQ(input_time + base::TimeDelta::FromMilliseconds(4000), |
- rtp_timestamp_in_ticks); |
+ EXPECT_FALSE( |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency).is_null()); |
+ EXPECT_EQ(lip_sync_time + base::TimeDelta::FromMilliseconds(4000), |
+ rtcp.ToApproximateCaptureTime(rtp_timestamp, frequency)); |
} |
} // namespace cast |