Index: media/cast/net/rtcp/sender_rtcp_session.cc |
diff --git a/media/cast/net/rtcp/rtcp.cc b/media/cast/net/rtcp/sender_rtcp_session.cc |
similarity index 48% |
rename from media/cast/net/rtcp/rtcp.cc |
rename to media/cast/net/rtcp/sender_rtcp_session.cc |
index 00cccf2a13f8bb09af091fb0b78237d6aa571a52..e8d764f8d2aebbf83e226af60029e5b18dfc18d6 100644 |
--- a/media/cast/net/rtcp/rtcp.cc |
+++ b/media/cast/net/rtcp/sender_rtcp_session.cc |
@@ -1,22 +1,19 @@ |
-// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "media/cast/net/rtcp/rtcp.h" |
- |
+#include <algorithm> |
#include <limits> |
+#include <utility> |
+#include "base/big_endian.h" |
#include "base/time/time.h" |
-#include "media/cast/cast_environment.h" |
#include "media/cast/constants.h" |
-#include "media/cast/net/cast_transport_config.h" |
-#include "media/cast/net/cast_transport_defines.h" |
#include "media/cast/net/pacing/paced_sender.h" |
#include "media/cast/net/rtcp/rtcp_builder.h" |
#include "media/cast/net/rtcp/rtcp_defines.h" |
#include "media/cast/net/rtcp/rtcp_utility.h" |
- |
-using base::TimeDelta; |
+#include "media/cast/net/rtcp/sender_rtcp_session.h" |
namespace media { |
namespace cast { |
@@ -26,13 +23,12 @@ namespace { |
enum { |
kStatsHistoryWindowMs = 10000, // 10 seconds. |
- // Reject packets that are older than 0.5 seconds older than |
+ // Reject packets that are 0.5 seconds older than |
// the newest packet we've seen so far. This protects internal |
// states from crazy routers. (Based on RRTR) |
+ // TODO(isheriff): This should be done better. |
+ // See https://crbug.com/569261 |
kOutOfOrderMaxAgeMs = 500, |
- |
- // Minimum number of bytes required to make a valid RTCP packet. |
- kMinLengthOfRtcp = 8, |
}; |
// Create a NTP diff from seconds and fractions of seconds; delay_fraction is |
@@ -79,53 +75,29 @@ std::pair<uint64_t, uint64_t> GetReceiverEventKey( |
} // namespace |
-Rtcp::Rtcp(const RtcpCastMessageCallback& cast_callback, |
- const RtcpRttCallback& rtt_callback, |
- const RtcpLogMessageCallback& log_callback, |
- base::TickClock* clock, |
- PacedPacketSender* packet_sender, |
- uint32_t local_ssrc, |
- uint32_t remote_ssrc) |
- : cast_callback_(cast_callback), |
- rtt_callback_(rtt_callback), |
- log_callback_(log_callback), |
- clock_(clock), |
- rtcp_builder_(local_ssrc), |
+SenderRtcpSession::SenderRtcpSession( |
+ const RtcpCastMessageCallback& cast_callback, |
+ const RtcpRttCallback& rtt_callback, |
+ const RtcpLogMessageCallback& log_callback, |
+ base::TickClock* clock, |
+ PacedPacketSender* packet_sender, |
+ uint32_t local_ssrc, |
+ uint32_t remote_ssrc) |
+ : clock_(clock), |
packet_sender_(packet_sender), |
local_ssrc_(local_ssrc), |
remote_ssrc_(remote_ssrc), |
- parser_(local_ssrc_, remote_ssrc_), |
- last_report_truncated_ntp_(0), |
- local_clock_ahead_by_(ClockDriftSmoother::GetDefaultTimeConstant()), |
- lip_sync_ntp_timestamp_(0), |
+ cast_callback_(cast_callback), |
+ rtt_callback_(rtt_callback), |
+ log_callback_(log_callback), |
largest_seen_timestamp_(base::TimeTicks::FromInternalValue( |
std::numeric_limits<int64_t>::min())), |
+ parser_(local_ssrc, remote_ssrc), |
ack_frame_id_wrap_helper_(kFirstFrameId - 1) {} |
-Rtcp::~Rtcp() {} |
+SenderRtcpSession::~SenderRtcpSession() {} |
-bool Rtcp::IsRtcpPacket(const uint8_t* packet, size_t length) { |
- if (length < kMinLengthOfRtcp) { |
- LOG(ERROR) << "Invalid RTCP packet received."; |
- return false; |
- } |
- |
- uint8_t packet_type = packet[1]; |
- return packet_type >= kPacketTypeLow && packet_type <= kPacketTypeHigh; |
-} |
- |
-uint32_t Rtcp::GetSsrcOfSender(const uint8_t* rtcp_buffer, size_t length) { |
- if (length < kMinLengthOfRtcp) |
- return 0; |
- uint32_t ssrc_of_sender; |
- base::BigEndianReader big_endian_reader( |
- reinterpret_cast<const char*>(rtcp_buffer), length); |
- big_endian_reader.Skip(4); // Skip header. |
- big_endian_reader.ReadU32(&ssrc_of_sender); |
- return ssrc_of_sender; |
-} |
- |
-bool Rtcp::IncomingRtcpPacket(const uint8_t* data, size_t length) { |
+bool SenderRtcpSession::IncomingRtcpPacket(const uint8_t* data, size_t length) { |
// Check if this is a valid RTCP packet. |
if (!IsRtcpPacket(data, length)) { |
VLOG(1) << "Rtcp@" << this << "::IncomingRtcpPacket() -- " |
@@ -152,20 +124,9 @@ bool Rtcp::IncomingRtcpPacket(const uint8_t* data, size_t length) { |
kOutOfOrderMaxAgeMs) { |
// Reject packet, it is too old. |
VLOG(1) << "Rejecting RTCP packet as it is too old (" |
- << (largest_seen_timestamp_ - t).InMilliseconds() |
- << " ms)"; |
+ << (largest_seen_timestamp_ - t).InMilliseconds() << " ms)"; |
return true; |
} |
- |
- OnReceivedNtp(parser_.receiver_reference_time_report().ntp_seconds, |
- parser_.receiver_reference_time_report().ntp_fraction); |
- } |
- if (parser_.has_sender_report()) { |
- OnReceivedNtp(parser_.sender_report().ntp_seconds, |
- parser_.sender_report().ntp_fraction); |
- OnReceivedLipSyncInfo(parser_.sender_report().rtp_timestamp, |
- parser_.sender_report().ntp_seconds, |
- parser_.sender_report().ntp_fraction); |
} |
if (parser_.has_receiver_log()) { |
if (DedupeReceiverLog(parser_.mutable_receiver_log())) { |
@@ -186,16 +147,67 @@ bool Rtcp::IncomingRtcpPacket(const uint8_t* data, size_t length) { |
return true; |
} |
-bool Rtcp::DedupeReceiverLog(RtcpReceiverLogMessage* receiver_log) { |
+void SenderRtcpSession::OnReceivedDelaySinceLastReport( |
+ uint32_t last_report, |
+ uint32_t delay_since_last_report) { |
+ RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); |
+ if (it == last_reports_sent_map_.end()) { |
+ return; // Feedback on another report. |
+ } |
+ |
+ const base::TimeDelta sender_delay = clock_->NowTicks() - it->second; |
+ const base::TimeDelta receiver_delay = |
+ ConvertFromNtpDiff(delay_since_last_report); |
+ current_round_trip_time_ = sender_delay - receiver_delay; |
+ // If the round trip time was computed as less than 1 ms, assume clock |
+ // imprecision by one or both peers caused a bad value to be calculated. |
+ // While plenty of networks do easily achieve less than 1 ms round trip time, |
+ // such a level of precision cannot be measured with our approach; and 1 ms is |
+ // good enough to represent "under 1 ms" for our use cases. |
+ current_round_trip_time_ = |
+ std::max(current_round_trip_time_, base::TimeDelta::FromMilliseconds(1)); |
+ |
+ if (!rtt_callback_.is_null()) |
+ rtt_callback_.Run(current_round_trip_time_); |
+} |
+ |
+void SenderRtcpSession::SaveLastSentNtpTime(const base::TimeTicks& now, |
+ uint32_t last_ntp_seconds, |
+ uint32_t last_ntp_fraction) { |
+ // Make sure |now| is always greater than the last element in |
+ // |last_reports_sent_queue_|. |
+ if (!last_reports_sent_queue_.empty()) { |
+ DCHECK(now >= last_reports_sent_queue_.back().second); |
+ } |
+ |
+ uint32_t last_report = ConvertToNtpDiff(last_ntp_seconds, last_ntp_fraction); |
+ last_reports_sent_map_[last_report] = now; |
+ last_reports_sent_queue_.push(std::make_pair(last_report, now)); |
+ |
+ const base::TimeTicks timeout = |
+ now - base::TimeDelta::FromMilliseconds(kStatsHistoryWindowMs); |
+ |
+ // Cleanup old statistics older than |timeout|. |
+ while (!last_reports_sent_queue_.empty()) { |
+ RtcpSendTimePair oldest_report = last_reports_sent_queue_.front(); |
+ if (oldest_report.second < timeout) { |
+ last_reports_sent_map_.erase(oldest_report.first); |
+ last_reports_sent_queue_.pop(); |
+ } else { |
+ break; |
+ } |
+ } |
+} |
+ |
+bool SenderRtcpSession::DedupeReceiverLog( |
+ RtcpReceiverLogMessage* receiver_log) { |
RtcpReceiverLogMessage::iterator i = receiver_log->begin(); |
while (i != receiver_log->end()) { |
RtcpReceiverEventLogMessages* messages = &i->event_log_messages_; |
RtcpReceiverEventLogMessages::iterator j = messages->begin(); |
while (j != messages->end()) { |
- ReceiverEventKey key = GetReceiverEventKey(i->rtp_timestamp_, |
- j->event_timestamp, |
- j->type, |
- j->packet_id); |
+ ReceiverEventKey key = GetReceiverEventKey( |
+ i->rtp_timestamp_, j->event_timestamp, j->type, j->packet_id); |
RtcpReceiverEventLogMessages::iterator tmp = j; |
++j; |
if (receiver_event_key_set_.insert(key).second) { |
@@ -218,70 +230,16 @@ bool Rtcp::DedupeReceiverLog(RtcpReceiverLogMessage* receiver_log) { |
return !receiver_log->empty(); |
} |
-RtcpTimeData Rtcp::ConvertToNTPAndSave(base::TimeTicks now) { |
- RtcpTimeData ret; |
- ret.timestamp = now; |
- |
- // Attach our NTP to all RTCP packets; with this information a "smart" sender |
- // can make decisions based on how old the RTCP message is. |
- ConvertTimeTicksToNtp(now, &ret.ntp_seconds, &ret.ntp_fraction); |
- SaveLastSentNtpTime(now, ret.ntp_seconds, ret.ntp_fraction); |
- return ret; |
-} |
- |
-void Rtcp::SendRtcpFromRtpReceiver( |
- RtcpTimeData time_data, |
- const RtcpCastMessage* cast_message, |
- base::TimeDelta target_delay, |
- const ReceiverRtcpEventSubscriber::RtcpEvents* rtcp_events, |
- const RtpReceiverStatistics* rtp_receiver_statistics) const { |
- RtcpReportBlock report_block; |
- RtcpReceiverReferenceTimeReport rrtr; |
- rrtr.ntp_seconds = time_data.ntp_seconds; |
- rrtr.ntp_fraction = time_data.ntp_fraction; |
- |
- if (rtp_receiver_statistics) { |
- report_block.remote_ssrc = 0; // Not needed to set send side. |
- report_block.media_ssrc = remote_ssrc_; // SSRC of the RTP packet sender. |
- report_block.fraction_lost = rtp_receiver_statistics->fraction_lost; |
- report_block.cumulative_lost = rtp_receiver_statistics->cumulative_lost; |
- report_block.extended_high_sequence_number = |
- rtp_receiver_statistics->extended_high_sequence_number; |
- report_block.jitter = rtp_receiver_statistics->jitter; |
- report_block.last_sr = last_report_truncated_ntp_; |
- if (!time_last_report_received_.is_null()) { |
- uint32_t delay_seconds = 0; |
- uint32_t delay_fraction = 0; |
- base::TimeDelta delta = time_data.timestamp - time_last_report_received_; |
- ConvertTimeToFractions(delta.InMicroseconds(), &delay_seconds, |
- &delay_fraction); |
- report_block.delay_since_last_sr = |
- ConvertToNtpDiff(delay_seconds, delay_fraction); |
- } else { |
- report_block.delay_since_last_sr = 0; |
- } |
- } |
- RtcpBuilder rtcp_builder(local_ssrc_); |
- packet_sender_->SendRtcpPacket( |
- local_ssrc_, |
- rtcp_builder.BuildRtcpFromReceiver( |
- rtp_receiver_statistics ? &report_block : NULL, |
- &rrtr, |
- cast_message, |
- rtcp_events, |
- target_delay)); |
-} |
- |
-void Rtcp::SendRtcpFromRtpSender(base::TimeTicks current_time, |
- RtpTimeTicks current_time_as_rtp_timestamp, |
- uint32_t send_packet_count, |
- size_t send_octet_count) { |
+void SenderRtcpSession::SendRtcpReport( |
+ base::TimeTicks current_time, |
+ RtpTimeTicks current_time_as_rtp_timestamp, |
+ uint32_t send_packet_count, |
+ size_t send_octet_count) { |
uint32_t current_ntp_seconds = 0; |
uint32_t current_ntp_fractions = 0; |
ConvertTimeTicksToNtp(current_time, ¤t_ntp_seconds, |
¤t_ntp_fractions); |
- SaveLastSentNtpTime(current_time, current_ntp_seconds, |
- current_ntp_fractions); |
+ SaveLastSentNtpTime(current_time, current_ntp_seconds, current_ntp_fractions); |
RtcpSenderInfo sender_info; |
sender_info.ntp_seconds = current_ntp_seconds; |
@@ -290,128 +248,20 @@ void Rtcp::SendRtcpFromRtpSender(base::TimeTicks current_time, |
sender_info.send_packet_count = send_packet_count; |
sender_info.send_octet_count = send_octet_count; |
- packet_sender_->SendRtcpPacket( |
- local_ssrc_, |
- rtcp_builder_.BuildRtcpFromSender(sender_info)); |
-} |
- |
-void Rtcp::OnReceivedNtp(uint32_t ntp_seconds, uint32_t ntp_fraction) { |
- last_report_truncated_ntp_ = ConvertToNtpDiff(ntp_seconds, ntp_fraction); |
- |
- const base::TimeTicks now = clock_->NowTicks(); |
- time_last_report_received_ = now; |
- |
- // TODO(miu): This clock offset calculation does not account for packet |
- // transit time over the network. End2EndTest.EvilNetwork confirms that this |
- // contributes a very significant source of error here. Determine whether |
- // RTT should be factored-in, and how that changes the rest of the |
- // calculation. |
- const base::TimeDelta measured_offset = |
- now - ConvertNtpToTimeTicks(ntp_seconds, ntp_fraction); |
- local_clock_ahead_by_.Update(now, measured_offset); |
- if (measured_offset < local_clock_ahead_by_.Current()) { |
- // Logically, the minimum offset between the clocks has to be the correct |
- // one. For example, the time it took to transmit the current report may |
- // have been lower than usual, and so some of the error introduced by the |
- // transmission time can be eliminated. |
- local_clock_ahead_by_.Reset(now, measured_offset); |
- } |
- VLOG(1) << "Local clock is ahead of the remote clock by: " |
- << "measured=" << measured_offset.InMicroseconds() << " usec, " |
- << "filtered=" << local_clock_ahead_by_.Current().InMicroseconds() |
- << " usec."; |
-} |
- |
-void Rtcp::OnReceivedLipSyncInfo(RtpTimeTicks rtp_timestamp, |
- uint32_t ntp_seconds, |
- uint32_t ntp_fraction) { |
- if (ntp_seconds == 0) { |
- NOTREACHED(); |
- return; |
- } |
- lip_sync_rtp_timestamp_ = rtp_timestamp; |
- lip_sync_ntp_timestamp_ = |
- (static_cast<uint64_t>(ntp_seconds) << 32) | ntp_fraction; |
-} |
- |
-bool Rtcp::GetLatestLipSyncTimes(RtpTimeTicks* rtp_timestamp, |
- base::TimeTicks* reference_time) const { |
- if (!lip_sync_ntp_timestamp_) |
- return false; |
- |
- const base::TimeTicks local_reference_time = |
- ConvertNtpToTimeTicks( |
- static_cast<uint32_t>(lip_sync_ntp_timestamp_ >> 32), |
- static_cast<uint32_t>(lip_sync_ntp_timestamp_)) + |
- local_clock_ahead_by_.Current(); |
- |
- // Sanity-check: Getting regular lip sync updates? |
- DCHECK((clock_->NowTicks() - local_reference_time) < |
- base::TimeDelta::FromMinutes(1)); |
- |
- *rtp_timestamp = lip_sync_rtp_timestamp_; |
- *reference_time = local_reference_time; |
- return true; |
-} |
- |
-void Rtcp::OnReceivedDelaySinceLastReport(uint32_t last_report, |
- uint32_t delay_since_last_report) { |
- RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); |
- if (it == last_reports_sent_map_.end()) { |
- return; // Feedback on another report. |
- } |
- |
- const base::TimeDelta sender_delay = clock_->NowTicks() - it->second; |
- const base::TimeDelta receiver_delay = |
- ConvertFromNtpDiff(delay_since_last_report); |
- current_round_trip_time_ = sender_delay - receiver_delay; |
- // If the round trip time was computed as less than 1 ms, assume clock |
- // imprecision by one or both peers caused a bad value to be calculated. |
- // While plenty of networks do easily achieve less than 1 ms round trip time, |
- // such a level of precision cannot be measured with our approach; and 1 ms is |
- // good enough to represent "under 1 ms" for our use cases. |
- current_round_trip_time_ = |
- std::max(current_round_trip_time_, base::TimeDelta::FromMilliseconds(1)); |
- |
- if (!rtt_callback_.is_null()) |
- rtt_callback_.Run(current_round_trip_time_); |
+ RtcpBuilder rtcp_builder(local_ssrc_); |
+ packet_sender_->SendRtcpPacket(local_ssrc_, |
+ rtcp_builder.BuildRtcpFromSender(sender_info)); |
} |
-void Rtcp::OnReceivedCastFeedback(const RtcpCastMessage& cast_message) { |
+void SenderRtcpSession::OnReceivedCastFeedback( |
+ const RtcpCastMessage& cast_message) { |
if (cast_callback_.is_null()) |
return; |
cast_callback_.Run(cast_message); |
} |
-void Rtcp::SaveLastSentNtpTime(const base::TimeTicks& now, |
- uint32_t last_ntp_seconds, |
- uint32_t last_ntp_fraction) { |
- // Make sure |now| is always greater than the last element in |
- // |last_reports_sent_queue_|. |
- if (!last_reports_sent_queue_.empty()) { |
- DCHECK(now >= last_reports_sent_queue_.back().second); |
- } |
- |
- uint32_t last_report = ConvertToNtpDiff(last_ntp_seconds, last_ntp_fraction); |
- last_reports_sent_map_[last_report] = now; |
- last_reports_sent_queue_.push(std::make_pair(last_report, now)); |
- |
- const base::TimeTicks timeout = |
- now - TimeDelta::FromMilliseconds(kStatsHistoryWindowMs); |
- |
- // Cleanup old statistics older than |timeout|. |
- while (!last_reports_sent_queue_.empty()) { |
- RtcpSendTimePair oldest_report = last_reports_sent_queue_.front(); |
- if (oldest_report.second < timeout) { |
- last_reports_sent_map_.erase(oldest_report.first); |
- last_reports_sent_queue_.pop(); |
- } else { |
- break; |
- } |
- } |
-} |
- |
-void Rtcp::OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log) { |
+void SenderRtcpSession::OnReceivedReceiverLog( |
+ const RtcpReceiverLogMessage& receiver_log) { |
if (log_callback_.is_null()) |
return; |
log_callback_.Run(receiver_log); |