Change StreamDataCounters to use Timestamp instead of int64_t

Bug: webrtc:13757
Change-Id: I11151682a07a2d95389f81cbd7f47f26ad8e67ce
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/306700
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40155}
This commit is contained in:
Danil Chapovalov 2023-05-26 11:30:26 +02:00 committed by WebRTC LUCI CQ
parent 5f32fa47a7
commit 0f1a2c5d97
10 changed files with 36 additions and 58 deletions

View File

@ -45,7 +45,7 @@ bool IsLegalRsidName(absl::string_view name) {
absl::c_all_of(name, isalnum)); absl::c_all_of(name, isalnum));
} }
StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {} StreamDataCounters::StreamDataCounters() = default;
RtpPacketCounter::RtpPacketCounter(const RtpPacket& packet) RtpPacketCounter::RtpPacketCounter(const RtpPacket& packet)
: header_bytes(packet.headers_size()), : header_bytes(packet.headers_size()),

View File

@ -286,19 +286,6 @@ struct RtpPacketCounter {
total_packet_delay += other.total_packet_delay; total_packet_delay += other.total_packet_delay;
} }
void Subtract(const RtpPacketCounter& other) {
RTC_DCHECK_GE(header_bytes, other.header_bytes);
header_bytes -= other.header_bytes;
RTC_DCHECK_GE(payload_bytes, other.payload_bytes);
payload_bytes -= other.payload_bytes;
RTC_DCHECK_GE(padding_bytes, other.padding_bytes);
padding_bytes -= other.padding_bytes;
RTC_DCHECK_GE(packets, other.packets);
packets -= other.packets;
RTC_DCHECK_GE(total_packet_delay, other.total_packet_delay);
total_packet_delay -= other.total_packet_delay;
}
bool operator==(const RtpPacketCounter& other) const { bool operator==(const RtpPacketCounter& other) const {
return header_bytes == other.header_bytes && return header_bytes == other.header_bytes &&
payload_bytes == other.payload_bytes && payload_bytes == other.payload_bytes &&
@ -331,28 +318,24 @@ struct StreamDataCounters {
transmitted.Add(other.transmitted); transmitted.Add(other.transmitted);
retransmitted.Add(other.retransmitted); retransmitted.Add(other.retransmitted);
fec.Add(other.fec); fec.Add(other.fec);
if (other.first_packet_time_ms != -1 && if (other.first_packet_time < first_packet_time) {
(other.first_packet_time_ms < first_packet_time_ms || // Use oldest time (excluding unsed value represented as plus infinity.
first_packet_time_ms == -1)) { first_packet_time = other.first_packet_time;
// Use oldest time.
first_packet_time_ms = other.first_packet_time_ms;
} }
} }
void Subtract(const StreamDataCounters& other) { void MaybeSetFirstPacketTime(Timestamp now) {
transmitted.Subtract(other.transmitted); if (first_packet_time == Timestamp::PlusInfinity()) {
retransmitted.Subtract(other.retransmitted); first_packet_time = now;
fec.Subtract(other.fec);
if (other.first_packet_time_ms != -1 &&
(other.first_packet_time_ms > first_packet_time_ms ||
first_packet_time_ms == -1)) {
// Use youngest time.
first_packet_time_ms = other.first_packet_time_ms;
} }
} }
int64_t TimeSinceFirstPacketInMs(int64_t now_ms) const { // Return time since first packet is send/received, or zero if such event
return (first_packet_time_ms == -1) ? -1 : (now_ms - first_packet_time_ms); // haven't happen.
TimeDelta TimeSinceFirstPacket(Timestamp now) const {
return first_packet_time == Timestamp::PlusInfinity()
? TimeDelta::Zero()
: now - first_packet_time;
} }
// Returns the number of bytes corresponding to the actual media payload (i.e. // Returns the number of bytes corresponding to the actual media payload (i.e.
@ -363,7 +346,9 @@ struct StreamDataCounters {
fec.payload_bytes; fec.payload_bytes;
} }
int64_t first_packet_time_ms; // Time when first packet is sent/received. // Time when first packet is sent/received.
Timestamp first_packet_time = Timestamp::PlusInfinity();
RtpPacketCounter transmitted; // Number of transmitted packets/bytes. RtpPacketCounter transmitted; // Number of transmitted packets/bytes.
RtpPacketCounter retransmitted; // Number of retransmitted packets/bytes. RtpPacketCounter retransmitted; // Number of retransmitted packets/bytes.
RtpPacketCounter fec; // Number of redundancy packets/bytes. RtpPacketCounter fec; // Number of redundancy packets/bytes.

View File

@ -434,14 +434,12 @@ bool DEPRECATED_RtpSenderEgress::SendPacketToNetwork(
} }
void DEPRECATED_RtpSenderEgress::UpdateRtpStats(const RtpPacketToSend& packet) { void DEPRECATED_RtpSenderEgress::UpdateRtpStats(const RtpPacketToSend& packet) {
int64_t now_ms = clock_->TimeInMilliseconds(); Timestamp now = clock_->CurrentTime();
StreamDataCounters* counters = StreamDataCounters* counters =
packet.Ssrc() == rtx_ssrc_ ? &rtx_rtp_stats_ : &rtp_stats_; packet.Ssrc() == rtx_ssrc_ ? &rtx_rtp_stats_ : &rtp_stats_;
if (counters->first_packet_time_ms == -1) { counters->MaybeSetFirstPacketTime(now);
counters->first_packet_time_ms = now_ms;
}
if (packet.packet_type() == RtpPacketMediaType::kForwardErrorCorrection) { if (packet.packet_type() == RtpPacketMediaType::kForwardErrorCorrection) {
counters->fec.AddPacket(packet); counters->fec.AddPacket(packet);
@ -454,7 +452,7 @@ void DEPRECATED_RtpSenderEgress::UpdateRtpStats(const RtpPacketToSend& packet) {
RTC_DCHECK(packet.packet_type().has_value()); RTC_DCHECK(packet.packet_type().has_value());
send_rates_[static_cast<size_t>(*packet.packet_type())].Update(packet.size(), send_rates_[static_cast<size_t>(*packet.packet_type())].Update(packet.size(),
now_ms); now.ms());
if (rtp_stats_callback_) { if (rtp_stats_callback_) {
rtp_stats_callback_->DataCountersUpdated(*counters, packet.Ssrc()); rtp_stats_callback_->DataCountersUpdated(*counters, packet.Ssrc());

View File

@ -110,7 +110,8 @@ bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet,
void StreamStatisticianImpl::UpdateCounters(const RtpPacketReceived& packet) { void StreamStatisticianImpl::UpdateCounters(const RtpPacketReceived& packet) {
RTC_DCHECK_EQ(ssrc_, packet.Ssrc()); RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
int64_t now_ms = clock_->TimeInMilliseconds(); Timestamp now = clock_->CurrentTime();
int64_t now_ms = now.ms();
incoming_bitrate_.Update(packet.size(), now_ms); incoming_bitrate_.Update(packet.size(), now_ms);
receive_counters_.transmitted.AddPacket(packet); receive_counters_.transmitted.AddPacket(packet);
@ -124,7 +125,7 @@ void StreamStatisticianImpl::UpdateCounters(const RtpPacketReceived& packet) {
received_seq_first_ = sequence_number; received_seq_first_ = sequence_number;
last_report_seq_max_ = sequence_number - 1; last_report_seq_max_ = sequence_number - 1;
received_seq_max_ = sequence_number - 1; received_seq_max_ = sequence_number - 1;
receive_counters_.first_packet_time_ms = now_ms; receive_counters_.first_packet_time = now;
} else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) { } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) {
return; return;
} }

View File

@ -259,12 +259,12 @@ TEST_P(ReceiveStatisticsTest, GetReceiveStreamDataCounters) {
ASSERT_TRUE(statistician != NULL); ASSERT_TRUE(statistician != NULL);
StreamDataCounters counters = statistician->GetReceiveStreamDataCounters(); StreamDataCounters counters = statistician->GetReceiveStreamDataCounters();
EXPECT_GT(counters.first_packet_time_ms, -1); EXPECT_TRUE(counters.first_packet_time.IsFinite());
EXPECT_EQ(1u, counters.transmitted.packets); EXPECT_EQ(1u, counters.transmitted.packets);
receive_statistics_->OnRtpPacket(packet1_); receive_statistics_->OnRtpPacket(packet1_);
counters = statistician->GetReceiveStreamDataCounters(); counters = statistician->GetReceiveStreamDataCounters();
EXPECT_GT(counters.first_packet_time_ms, -1); EXPECT_TRUE(counters.first_packet_time.IsFinite());
EXPECT_EQ(2u, counters.transmitted.packets); EXPECT_EQ(2u, counters.transmitted.packets);
} }

View File

@ -494,8 +494,8 @@ TEST_F(RtpRtcpImpl2Test, RtcpPacketTypeCounter_Nack) {
TEST_F(RtpRtcpImpl2Test, AddStreamDataCounters) { TEST_F(RtpRtcpImpl2Test, AddStreamDataCounters) {
StreamDataCounters rtp; StreamDataCounters rtp;
const int64_t kStartTimeMs = 1; const Timestamp kStartTime = Timestamp::Seconds(1);
rtp.first_packet_time_ms = kStartTimeMs; rtp.first_packet_time = kStartTime;
rtp.transmitted.packets = 1; rtp.transmitted.packets = 1;
rtp.transmitted.payload_bytes = 1; rtp.transmitted.payload_bytes = 1;
rtp.transmitted.header_bytes = 2; rtp.transmitted.header_bytes = 2;
@ -505,7 +505,6 @@ TEST_F(RtpRtcpImpl2Test, AddStreamDataCounters) {
rtp.transmitted.padding_bytes); rtp.transmitted.padding_bytes);
StreamDataCounters rtp2; StreamDataCounters rtp2;
rtp2.first_packet_time_ms = -1;
rtp2.transmitted.packets = 10; rtp2.transmitted.packets = 10;
rtp2.transmitted.payload_bytes = 10; rtp2.transmitted.payload_bytes = 10;
rtp2.retransmitted.header_bytes = 4; rtp2.retransmitted.header_bytes = 4;
@ -516,7 +515,7 @@ TEST_F(RtpRtcpImpl2Test, AddStreamDataCounters) {
StreamDataCounters sum = rtp; StreamDataCounters sum = rtp;
sum.Add(rtp2); sum.Add(rtp2);
EXPECT_EQ(kStartTimeMs, sum.first_packet_time_ms); EXPECT_EQ(sum.first_packet_time, kStartTime);
EXPECT_EQ(11U, sum.transmitted.packets); EXPECT_EQ(11U, sum.transmitted.packets);
EXPECT_EQ(11U, sum.transmitted.payload_bytes); EXPECT_EQ(11U, sum.transmitted.payload_bytes);
EXPECT_EQ(2U, sum.transmitted.header_bytes); EXPECT_EQ(2U, sum.transmitted.header_bytes);
@ -530,9 +529,9 @@ TEST_F(RtpRtcpImpl2Test, AddStreamDataCounters) {
rtp.transmitted.TotalBytes() + rtp2.transmitted.TotalBytes()); rtp.transmitted.TotalBytes() + rtp2.transmitted.TotalBytes());
StreamDataCounters rtp3; StreamDataCounters rtp3;
rtp3.first_packet_time_ms = kStartTimeMs + 10; rtp3.first_packet_time = kStartTime + TimeDelta::Millis(10);
sum.Add(rtp3); sum.Add(rtp3);
EXPECT_EQ(kStartTimeMs, sum.first_packet_time_ms); // Holds oldest time. EXPECT_EQ(sum.first_packet_time, kStartTime); // Holds oldest time.
} }
TEST_F(RtpRtcpImpl2Test, SendsInitialNackList) { TEST_F(RtpRtcpImpl2Test, SendsInitialNackList) {

View File

@ -581,9 +581,7 @@ void RtpSenderEgress::UpdateRtpStats(Timestamp now,
StreamDataCounters* counters = StreamDataCounters* counters =
packet_ssrc == rtx_ssrc_ ? &rtx_rtp_stats_ : &rtp_stats_; packet_ssrc == rtx_ssrc_ ? &rtx_rtp_stats_ : &rtp_stats_;
if (counters->first_packet_time_ms == -1) { counters->MaybeSetFirstPacketTime(now);
counters->first_packet_time_ms = now.ms();
}
if (packet_type == RtpPacketMediaType::kForwardErrorCorrection) { if (packet_type == RtpPacketMediaType::kForwardErrorCorrection) {
counters->fec.Add(counter); counters->fec.Add(counter);

View File

@ -391,10 +391,9 @@ void ReceiveStatisticsProxy::UpdateHistograms(
if (rtx_stats) if (rtx_stats)
rtp_rtx_stats.Add(*rtx_stats); rtp_rtx_stats.Add(*rtx_stats);
int64_t elapsed_sec = TimeDelta elapsed = rtp_rtx_stats.TimeSinceFirstPacket(clock_->CurrentTime());
rtp_rtx_stats.TimeSinceFirstPacketInMs(clock_->TimeInMilliseconds()) / if (elapsed >= TimeDelta::Seconds(metrics::kMinRunTimeInSeconds)) {
1000; int64_t elapsed_sec = elapsed.seconds();
if (elapsed_sec >= metrics::kMinRunTimeInSeconds) {
RTC_HISTOGRAM_COUNTS_10000( RTC_HISTOGRAM_COUNTS_10000(
"WebRTC.Video.BitrateReceivedInKbps", "WebRTC.Video.BitrateReceivedInKbps",
static_cast<int>(rtp_rtx_stats.transmitted.TotalBytes() * 8 / static_cast<int>(rtp_rtx_stats.transmitted.TotalBytes() * 8 /

View File

@ -1143,8 +1143,7 @@ TEST_F(ReceiveStatisticsProxyTest, AverageDelayOfDelayedFramesIsReported) {
TEST_F(ReceiveStatisticsProxyTest, TEST_F(ReceiveStatisticsProxyTest,
RtcpHistogramsNotUpdatedIfMinRuntimeHasNotPassed) { RtcpHistogramsNotUpdatedIfMinRuntimeHasNotPassed) {
StreamDataCounters data_counters; StreamDataCounters data_counters;
data_counters.first_packet_time_ms = data_counters.first_packet_time = time_controller_.GetClock()->CurrentTime();
time_controller_.GetClock()->TimeInMilliseconds();
time_controller_.AdvanceTime( time_controller_.AdvanceTime(
TimeDelta::Seconds(metrics::kMinRunTimeInSeconds) - TimeDelta::Millis(1)); TimeDelta::Seconds(metrics::kMinRunTimeInSeconds) - TimeDelta::Millis(1));
@ -1163,8 +1162,7 @@ TEST_F(ReceiveStatisticsProxyTest,
TEST_F(ReceiveStatisticsProxyTest, RtcpHistogramsAreUpdated) { TEST_F(ReceiveStatisticsProxyTest, RtcpHistogramsAreUpdated) {
StreamDataCounters data_counters; StreamDataCounters data_counters;
data_counters.first_packet_time_ms = data_counters.first_packet_time = time_controller_.GetClock()->CurrentTime();
time_controller_.GetClock()->TimeInMilliseconds();
time_controller_.AdvanceTime( time_controller_.AdvanceTime(
TimeDelta::Seconds(metrics::kMinRunTimeInSeconds)); TimeDelta::Seconds(metrics::kMinRunTimeInSeconds));

View File

@ -2618,7 +2618,7 @@ TEST_F(SendStatisticsProxyTest, ResetsRtpCountersOnContentChange) {
static_cast<StreamDataCountersCallback*>(statistics_proxy_.get()); static_cast<StreamDataCountersCallback*>(statistics_proxy_.get());
StreamDataCounters counters; StreamDataCounters counters;
StreamDataCounters rtx_counters; StreamDataCounters rtx_counters;
counters.first_packet_time_ms = fake_clock_.TimeInMilliseconds(); counters.first_packet_time = fake_clock_.CurrentTime();
const int kMinRequiredPeriodSamples = 8; const int kMinRequiredPeriodSamples = 8;
const int kPeriodIntervalMs = 2000; const int kPeriodIntervalMs = 2000;