diff --git a/modules/remote_bitrate_estimator/remote_estimator_proxy_unittest.cc b/modules/remote_bitrate_estimator/remote_estimator_proxy_unittest.cc index 02d428f580..c2360c0594 100644 --- a/modules/remote_bitrate_estimator/remote_estimator_proxy_unittest.cc +++ b/modules/remote_bitrate_estimator/remote_estimator_proxy_unittest.cc @@ -584,11 +584,11 @@ TEST_F(RemoteEstimatorProxyTest, ReportsIncomingPacketToNetworkStateEstimator) { first_send_timestamp = packet.sent_packet.send_time; })); // Incoming packet with abs sendtime but without transport sequence number. - proxy_.IncomingPacket({.arrival_time = kBaseTime, - .size = kDefaultPacketSize, - .ssrc = kMediaSsrc, - .absolute_send_time_24bits = - AbsoluteSendTime::MsTo24Bits(kBaseTime.ms())}); + proxy_.IncomingPacket( + {.arrival_time = kBaseTime, + .size = kDefaultPacketSize, + .ssrc = kMediaSsrc, + .absolute_send_time_24bits = AbsoluteSendTime::To24Bits(kBaseTime)}); // Expect packet with older abs send time to be treated as sent at the same // time as the previous packet due to reordering. @@ -603,15 +603,16 @@ TEST_F(RemoteEstimatorProxyTest, ReportsIncomingPacketToNetworkStateEstimator) { .size = kDefaultPacketSize, .ssrc = kMediaSsrc, .absolute_send_time_24bits = - AbsoluteSendTime::MsTo24Bits(kBaseTime.ms() - 12)}); + AbsoluteSendTime::To24Bits(kBaseTime - TimeDelta::Millis(12))}); } TEST_F(RemoteEstimatorProxyTest, IncomingPacketHandlesWrapInAbsSendTime) { // abs send time use 24bit precision. const uint32_t kFirstAbsSendTime = - AbsoluteSendTime::MsTo24Bits((1 << 24) - 30); + AbsoluteSendTime::To24Bits(Timestamp::Millis((1 << 24) - 30)); // Second abs send time has wrapped. - const uint32_t kSecondAbsSendTime = AbsoluteSendTime::MsTo24Bits((1 << 24)); + const uint32_t kSecondAbsSendTime = + AbsoluteSendTime::To24Bits(Timestamp::Millis(1 << 24)); const TimeDelta kExpectedAbsSendTimeDelta = TimeDelta::Millis(30); Timestamp first_send_timestamp = Timestamp::Millis(0); @@ -641,12 +642,13 @@ TEST_F(RemoteEstimatorProxyTest, IncomingPacketHandlesWrapInAbsSendTime) { } TEST_F(RemoteEstimatorProxyTest, SendTransportFeedbackAndNetworkStateUpdate) { - proxy_.IncomingPacket({.arrival_time = kBaseTime, - .size = kDefaultPacketSize, - .ssrc = kMediaSsrc, - .absolute_send_time_24bits = - AbsoluteSendTime::MsTo24Bits(kBaseTime.ms() - 1), - .transport_sequence_number = kBaseSeq}); + proxy_.IncomingPacket( + {.arrival_time = kBaseTime, + .size = kDefaultPacketSize, + .ssrc = kMediaSsrc, + .absolute_send_time_24bits = + AbsoluteSendTime::To24Bits(kBaseTime - TimeDelta::Millis(1)), + .transport_sequence_number = kBaseSeq}); EXPECT_CALL(network_state_estimator_, GetCurrentEstimate()) .WillOnce(Return(NetworkStateEstimate())); EXPECT_CALL(feedback_sender_, Call(SizeIs(2))); diff --git a/modules/rtp_rtcp/BUILD.gn b/modules/rtp_rtcp/BUILD.gn index bab5bf9868..300d68a950 100644 --- a/modules/rtp_rtcp/BUILD.gn +++ b/modules/rtp_rtcp/BUILD.gn @@ -358,6 +358,7 @@ rtc_source_set("rtp_rtcp_legacy") { "../../api/rtc_event_log", "../../api/transport:field_trial_based_config", "../../api/units:data_rate", + "../../api/units:timestamp", "../../api/video:video_bitrate_allocation", "../../logging:rtc_event_rtp_rtcp", "../../rtc_base:checks", diff --git a/modules/rtp_rtcp/source/deprecated/deprecated_rtp_sender_egress.cc b/modules/rtp_rtcp/source/deprecated/deprecated_rtp_sender_egress.cc index 35936c41ee..c6a9cfa85c 100644 --- a/modules/rtp_rtcp/source/deprecated/deprecated_rtp_sender_egress.cc +++ b/modules/rtp_rtcp/source/deprecated/deprecated_rtp_sender_egress.cc @@ -16,6 +16,7 @@ #include "absl/strings/match.h" #include "api/transport/field_trial_based_config.h" +#include "api/units/timestamp.h" #include "logging/rtc_event_log/events/rtc_event_rtp_packet_outgoing.h" #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h" #include "rtc_base/logging.h" @@ -105,7 +106,8 @@ void DEPRECATED_RtpSenderEgress::SendPacket( const uint32_t packet_ssrc = packet->Ssrc(); RTC_DCHECK(packet->packet_type().has_value()); RTC_DCHECK(HasCorrectSsrc(*packet)); - int64_t now_ms = clock_->TimeInMilliseconds(); + Timestamp now = clock_->CurrentTime(); + int64_t now_ms = now.ms(); if (is_audio_) { #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE @@ -160,15 +162,14 @@ void DEPRECATED_RtpSenderEgress::SendPacket( packet->SetExtension(kTimestampTicksPerMs * diff_ms); } if (packet->HasExtension()) { - packet->SetExtension( - AbsoluteSendTime::MsTo24Bits(now_ms)); + packet->SetExtension(AbsoluteSendTime::To24Bits(now)); } if (packet->HasExtension()) { if (populate_network2_timestamp_) { - packet->set_network2_time(Timestamp::Millis(now_ms)); + packet->set_network2_time(now); } else { - packet->set_pacer_exit_time(Timestamp::Millis(now_ms)); + packet->set_pacer_exit_time(now); } } @@ -200,7 +201,7 @@ void DEPRECATED_RtpSenderEgress::SendPacket( // actual sending fails. if (is_media && packet->allow_retransmission()) { packet_history_->PutRtpPacket(std::make_unique(*packet), - Timestamp::Millis(now_ms)); + now); } else if (packet->retransmitted_sequence_number()) { packet_history_->MarkPacketAsSent(*packet->retransmitted_sequence_number()); } diff --git a/modules/rtp_rtcp/source/rtp_header_extensions.h b/modules/rtp_rtcp/source/rtp_header_extensions.h index 05794174c1..62b19293ba 100644 --- a/modules/rtp_rtcp/source/rtp_header_extensions.h +++ b/modules/rtp_rtcp/source/rtp_header_extensions.h @@ -20,11 +20,13 @@ #include "api/array_view.h" #include "api/rtp_headers.h" #include "api/rtp_parameters.h" +#include "api/units/timestamp.h" #include "api/video/color_space.h" #include "api/video/video_content_type.h" #include "api/video/video_rotation.h" #include "api/video/video_timing.h" #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" +#include "rtc_base/numerics/divide_round.h" namespace webrtc { @@ -41,8 +43,12 @@ class AbsoluteSendTime { static size_t ValueSize(uint32_t time_24bits) { return kValueSizeBytes; } static bool Write(rtc::ArrayView data, uint32_t time_24bits); - static constexpr uint32_t MsTo24Bits(int64_t time_ms) { - return static_cast(((time_ms << 18) + 500) / 1000) & 0x00FFFFFF; + static constexpr uint32_t To24Bits(Timestamp time) { + int64_t time_us = time.us() % (int64_t{1 << 6} * 1'000'000); + int64_t time6x18 = DivideRoundToNearest(time_us << 18, 1'000'000); + RTC_DCHECK_GE(time6x18, 0); + RTC_DCHECK_LT(time6x18, 1 << 24); + return static_cast(time6x18); } }; diff --git a/modules/rtp_rtcp/source/rtp_sender_egress.cc b/modules/rtp_rtcp/source/rtp_sender_egress.cc index e0f68b444f..a4957f72e0 100644 --- a/modules/rtp_rtcp/source/rtp_sender_egress.cc +++ b/modules/rtp_rtcp/source/rtp_sender_egress.cc @@ -155,12 +155,12 @@ void RtpSenderEgress::SendPacket(RtpPacketToSend* packet, } const uint32_t packet_ssrc = packet->Ssrc(); - const int64_t now_ms = clock_->TimeInMilliseconds(); + const Timestamp now = clock_->CurrentTime(); #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE worker_queue_->PostTask( - ToQueuedTask(task_safety_, [this, now_ms, packet_ssrc]() { - BweTestLoggingPlot(now_ms, packet_ssrc); + ToQueuedTask(task_safety_, [this, now, packet_ssrc]() { + BweTestLoggingPlot(now.ms(), packet_ssrc); })); #endif @@ -225,20 +225,19 @@ void RtpSenderEgress::SendPacket(RtpPacketToSend* packet, // In case of VideoTimingExtension, since it's present not in every packet, // data after rtp header may be corrupted if these packets are protected by // the FEC. - int64_t diff_ms = now_ms - packet->capture_time().ms(); + TimeDelta diff = now - packet->capture_time(); if (packet->HasExtension()) { - packet->SetExtension(kTimestampTicksPerMs * diff_ms); + packet->SetExtension(kTimestampTicksPerMs * diff.ms()); } if (packet->HasExtension()) { - packet->SetExtension( - AbsoluteSendTime::MsTo24Bits(now_ms)); + packet->SetExtension(AbsoluteSendTime::To24Bits(now)); } if (packet->HasExtension()) { if (populate_network2_timestamp_) { - packet->set_network2_time(Timestamp::Millis(now_ms)); + packet->set_network2_time(now); } else { - packet->set_pacer_exit_time(Timestamp::Millis(now_ms)); + packet->set_pacer_exit_time(now); } } @@ -265,7 +264,7 @@ void RtpSenderEgress::SendPacket(RtpPacketToSend* packet, if (packet->packet_type() != RtpPacketMediaType::kPadding && packet->packet_type() != RtpPacketMediaType::kRetransmission) { - UpdateDelayStatistics(packet->capture_time().ms(), now_ms, packet_ssrc); + UpdateDelayStatistics(packet->capture_time().ms(), now.ms(), packet_ssrc); UpdateOnSendPacket(options.packet_id, packet->capture_time().ms(), packet_ssrc); } @@ -276,7 +275,7 @@ void RtpSenderEgress::SendPacket(RtpPacketToSend* packet, // actual sending fails. if (is_media && packet->allow_retransmission()) { packet_history_->PutRtpPacket(std::make_unique(*packet), - Timestamp::Millis(now_ms)); + now); } else if (packet->retransmitted_sequence_number()) { packet_history_->MarkPacketAsSent(*packet->retransmitted_sequence_number()); } @@ -295,10 +294,10 @@ void RtpSenderEgress::SendPacket(RtpPacketToSend* packet, RtpPacketCounter counter(*packet); size_t size = packet->size(); worker_queue_->PostTask( - ToQueuedTask(task_safety_, [this, now_ms, packet_ssrc, packet_type, + ToQueuedTask(task_safety_, [this, now, packet_ssrc, packet_type, counter = std::move(counter), size]() { RTC_DCHECK_RUN_ON(worker_queue_); - UpdateRtpStats(now_ms, packet_ssrc, packet_type, std::move(counter), + UpdateRtpStats(now.ms(), packet_ssrc, packet_type, std::move(counter), size); })); } diff --git a/modules/rtp_rtcp/source/rtp_sender_egress_unittest.cc b/modules/rtp_rtcp/source/rtp_sender_egress_unittest.cc index 33c06c4493..ddeeb8b002 100644 --- a/modules/rtp_rtcp/source/rtp_sender_egress_unittest.cc +++ b/modules/rtp_rtcp/source/rtp_sender_egress_unittest.cc @@ -754,7 +754,7 @@ TEST_P(RtpSenderEgressTest, SendPacketUpdatesExtensions) { EXPECT_EQ(received_packet.GetExtension(), kDiffMs * 90); EXPECT_EQ(received_packet.GetExtension(), - AbsoluteSendTime::MsTo24Bits(clock_->TimeInMilliseconds())); + AbsoluteSendTime::To24Bits(clock_->CurrentTime())); VideoSendTiming timing; EXPECT_TRUE(received_packet.GetExtension(&timing));