|  | /* | 
|  | *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 
|  | * | 
|  | *  Use of this source code is governed by a BSD-style license | 
|  | *  that can be found in the LICENSE file in the root of the source | 
|  | *  tree. An additional intellectual property rights grant can be found | 
|  | *  in the file PATENTS.  All contributing project authors may | 
|  | *  be found in the AUTHORS file in the root of the source tree. | 
|  | */ | 
|  |  | 
|  | #include "webrtc/system_wrappers/include/rtp_to_ntp.h" | 
|  | #include "webrtc/test/gtest.h" | 
|  |  | 
|  | namespace webrtc { | 
|  | namespace { | 
|  | const uint32_t kOneMsInNtpFrac = 4294967; | 
|  | const uint32_t kTimestampTicksPerMs = 90; | 
|  | }  // namespace | 
|  |  | 
|  | TEST(WrapAroundTests, NoWrap) { | 
|  | EXPECT_EQ(0, CheckForWrapArounds(0xFFFFFFFF, 0xFFFFFFFE)); | 
|  | EXPECT_EQ(0, CheckForWrapArounds(1, 0)); | 
|  | EXPECT_EQ(0, CheckForWrapArounds(0x00010000, 0x0000FFFF)); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, ForwardWrap) { | 
|  | EXPECT_EQ(1, CheckForWrapArounds(0, 0xFFFFFFFF)); | 
|  | EXPECT_EQ(1, CheckForWrapArounds(0, 0xFFFF0000)); | 
|  | EXPECT_EQ(1, CheckForWrapArounds(0x0000FFFF, 0xFFFFFFFF)); | 
|  | EXPECT_EQ(1, CheckForWrapArounds(0x0000FFFF, 0xFFFF0000)); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, BackwardWrap) { | 
|  | EXPECT_EQ(-1, CheckForWrapArounds(0xFFFFFFFF, 0)); | 
|  | EXPECT_EQ(-1, CheckForWrapArounds(0xFFFF0000, 0)); | 
|  | EXPECT_EQ(-1, CheckForWrapArounds(0xFFFFFFFF, 0x0000FFFF)); | 
|  | EXPECT_EQ(-1, CheckForWrapArounds(0xFFFF0000, 0x0000FFFF)); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, OldRtcpWrapped) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 0; | 
|  | uint32_t timestamp = 0; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp -= kTimestampTicksPerMs; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp -= kTimestampTicksPerMs; | 
|  | int64_t timestamp_in_ms = -1; | 
|  | // This expected to fail since it's highly unlikely that the older RTCP | 
|  | // has a much smaller RTP timestamp than the newer. | 
|  | EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms)); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, NewRtcpWrapped) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 0; | 
|  | uint32_t timestamp = 0xFFFFFFFF; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp += kTimestampTicksPerMs; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | int64_t timestamp_in_ms = -1; | 
|  | EXPECT_TRUE(RtpToNtpMs(rtcp.back().rtp_timestamp, rtcp, ×tamp_in_ms)); | 
|  | // Since this RTP packet has the same timestamp as the RTCP packet constructed | 
|  | // at time 0 it should be mapped to 0 as well. | 
|  | EXPECT_EQ(0, timestamp_in_ms); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, RtpWrapped) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 0; | 
|  | uint32_t timestamp = 0xFFFFFFFF - 2 * kTimestampTicksPerMs; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp += kTimestampTicksPerMs; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp += kTimestampTicksPerMs; | 
|  | int64_t timestamp_in_ms = -1; | 
|  | EXPECT_TRUE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms)); | 
|  | // Since this RTP packet has the same timestamp as the RTCP packet constructed | 
|  | // at time 0 it should be mapped to 0 as well. | 
|  | EXPECT_EQ(2, timestamp_in_ms); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, OldRtp_RtcpsWrapped) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 0; | 
|  | uint32_t timestamp = 0; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp += kTimestampTicksPerMs; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp -= 2*kTimestampTicksPerMs; | 
|  | int64_t timestamp_in_ms = -1; | 
|  | EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms)); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, OldRtp_NewRtcpWrapped) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 0; | 
|  | uint32_t timestamp = 0xFFFFFFFF; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp += kTimestampTicksPerMs; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp -= kTimestampTicksPerMs; | 
|  | int64_t timestamp_in_ms = -1; | 
|  | EXPECT_TRUE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms)); | 
|  | // Constructed at the same time as the first RTCP and should therefore be | 
|  | // mapped to zero. | 
|  | EXPECT_EQ(0, timestamp_in_ms); | 
|  | } | 
|  |  | 
|  | TEST(WrapAroundTests, OldRtp_OldRtcpWrapped) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 0; | 
|  | uint32_t timestamp = 0; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp -= kTimestampTicksPerMs; | 
|  | rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp)); | 
|  | ntp_frac += kOneMsInNtpFrac; | 
|  | timestamp += 2*kTimestampTicksPerMs; | 
|  | int64_t timestamp_in_ms = -1; | 
|  | EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms)); | 
|  | } | 
|  |  | 
|  | TEST(RtpToNtpTests, FailsForDecreasingRtpTimestamp) { | 
|  | const uint32_t kNtpSec1 = 3683354930; | 
|  | const uint32_t kNtpFrac1 = 699925050; | 
|  | const uint32_t kTimestamp1 = 2192705742; | 
|  | const uint32_t kNtpSec2 = kNtpSec1; | 
|  | const uint32_t kNtpFrac2 = kNtpFrac1 + kOneMsInNtpFrac; | 
|  | const uint32_t kTimestamp2 = kTimestamp1 - kTimestampTicksPerMs; | 
|  | RtcpList rtcp; | 
|  | rtcp.push_front(RtcpMeasurement(kNtpSec1, kNtpFrac1, kTimestamp1)); | 
|  | rtcp.push_front(RtcpMeasurement(kNtpSec2, kNtpFrac2, kTimestamp2)); | 
|  | int64_t timestamp_in_ms = -1; | 
|  | EXPECT_FALSE(RtpToNtpMs(kTimestamp1, rtcp, ×tamp_in_ms)); | 
|  | } | 
|  |  | 
|  | TEST(UpdateRtcpListTests, InjectRtcpSrWithEqualNtp) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 2; | 
|  | uint32_t timestamp = 0x12345678; | 
|  |  | 
|  | bool new_sr; | 
|  | EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | 
|  | EXPECT_TRUE(new_sr); | 
|  |  | 
|  | ++timestamp; | 
|  | EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | 
|  | EXPECT_FALSE(new_sr); | 
|  | } | 
|  |  | 
|  | TEST(UpdateRtcpListTests, InjectRtcpSrWithEqualTimestamp) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 2; | 
|  | uint32_t timestamp = 0x12345678; | 
|  |  | 
|  | bool new_sr; | 
|  | EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | 
|  | EXPECT_TRUE(new_sr); | 
|  |  | 
|  | ++ntp_frac; | 
|  | EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | 
|  | EXPECT_FALSE(new_sr); | 
|  | } | 
|  |  | 
|  | TEST(UpdateRtcpListTests, InjectRtcpSrWithZeroNtpFails) { | 
|  | RtcpList rtcp; | 
|  | uint32_t ntp_sec = 0; | 
|  | uint32_t ntp_frac = 0; | 
|  | uint32_t timestamp = 0x12345678; | 
|  |  | 
|  | bool new_sr; | 
|  | EXPECT_FALSE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | 
|  | } | 
|  | };  // namespace webrtc |