Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Unified Diff: media/cast/net/rtcp/receiver_rtcp_session.cc

Issue 1520613004: cast: Split Rtcp into two for sender and receiver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@timestamp
Patch Set: Rebased Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/cast/net/rtcp/receiver_rtcp_session.h ('k') | media/cast/net/rtcp/rtcp.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/cast/net/rtcp/receiver_rtcp_session.cc
diff --git a/media/cast/net/rtcp/rtcp.cc b/media/cast/net/rtcp/receiver_rtcp_session.cc
similarity index 34%
copy from media/cast/net/rtcp/rtcp.cc
copy to media/cast/net/rtcp/receiver_rtcp_session.cc
index 00cccf2a13f8bb09af091fb0b78237d6aa571a52..ba917da6155be5ef2728e7f403a4dad78aba6759 100644
--- a/media/cast/net/rtcp/rtcp.cc
+++ b/media/cast/net/rtcp/receiver_rtcp_session.cc
@@ -1,40 +1,20 @@
-// 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 <limits>
-
-#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 "base/big_endian.h"
+#include "base/time/tick_clock.h"
#include "media/cast/net/pacing/paced_sender.h"
+#include "media/cast/net/rtcp/receiver_rtcp_session.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;
-
namespace media {
namespace cast {
namespace {
-enum {
- kStatsHistoryWindowMs = 10000, // 10 seconds.
-
- // Reject packets that are older than 0.5 seconds older than
- // the newest packet we've seen so far. This protects internal
- // states from crazy routers. (Based on RRTR)
- 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
// fractions of a second where 0x80000000 is half a second.
uint32_t ConvertToNtpDiff(uint32_t delay_seconds, uint32_t delay_fraction) {
@@ -42,90 +22,25 @@ uint32_t ConvertToNtpDiff(uint32_t delay_seconds, uint32_t delay_fraction) {
((delay_fraction & 0xFFFF0000) >> 16);
}
-// Parse a NTP diff value into a base::TimeDelta.
-base::TimeDelta ConvertFromNtpDiff(uint32_t ntp_delay) {
- int64_t delay_us =
- (ntp_delay & 0x0000ffff) * base::Time::kMicrosecondsPerSecond;
- delay_us >>= 16;
- delay_us +=
- ((ntp_delay & 0xffff0000) >> 16) * base::Time::kMicrosecondsPerSecond;
- return base::TimeDelta::FromMicroseconds(delay_us);
-}
-
-// A receiver frame event is identified by frame RTP timestamp, event timestamp
-// and event type.
-// A receiver packet event is identified by all of the above plus packet id.
-// The key format is as follows:
-// First uint64_t:
-// bits 0-11: zeroes (unused).
-// bits 12-15: event type ID.
-// bits 16-31: packet ID if packet event, 0 otherwise.
-// bits 32-63: RTP timestamp.
-// Second uint64_t:
-// bits 0-63: event TimeTicks internal value.
-std::pair<uint64_t, uint64_t> GetReceiverEventKey(
- RtpTimeTicks frame_rtp_timestamp,
- const base::TimeTicks& event_timestamp,
- uint8_t event_type,
- uint16_t packet_id_or_zero) {
- uint64_t value1 = event_type;
- value1 <<= 16;
- value1 |= packet_id_or_zero;
- value1 <<= 32;
- value1 |= frame_rtp_timestamp.lower_32_bits();
- return std::make_pair(
- value1, static_cast<uint64_t>(event_timestamp.ToInternalValue()));
-}
-
} // 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),
+ReceiverRtcpSession::ReceiverRtcpSession(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),
- largest_seen_timestamp_(base::TimeTicks::FromInternalValue(
- std::numeric_limits<int64_t>::min())),
- ack_frame_id_wrap_helper_(kFirstFrameId - 1) {}
+ parser_(local_ssrc, remote_ssrc) {}
-Rtcp::~Rtcp() {}
-
-bool Rtcp::IsRtcpPacket(const uint8_t* packet, size_t length) {
- if (length < kMinLengthOfRtcp) {
- LOG(ERROR) << "Invalid RTCP packet received.";
- return false;
- }
+ReceiverRtcpSession::~ReceiverRtcpSession() {}
- 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 ReceiverRtcpSession::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() -- "
@@ -142,24 +57,6 @@ bool Rtcp::IncomingRtcpPacket(const uint8_t* data, size_t length) {
// Parse this packet.
base::BigEndianReader reader(reinterpret_cast<const char*>(data), length);
if (parser_.Parse(&reader)) {
- if (parser_.has_receiver_reference_time_report()) {
- base::TimeTicks t = ConvertNtpToTimeTicks(
- parser_.receiver_reference_time_report().ntp_seconds,
- parser_.receiver_reference_time_report().ntp_fraction);
- if (t > largest_seen_timestamp_) {
- largest_seen_timestamp_ = t;
- } else if ((largest_seen_timestamp_ - t).InMilliseconds() >
- kOutOfOrderMaxAgeMs) {
- // Reject packet, it is too old.
- VLOG(1) << "Rejecting RTCP packet as it is too old ("
- << (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);
@@ -167,69 +64,39 @@ bool Rtcp::IncomingRtcpPacket(const uint8_t* data, size_t length) {
parser_.sender_report().ntp_seconds,
parser_.sender_report().ntp_fraction);
}
- if (parser_.has_receiver_log()) {
- if (DedupeReceiverLog(parser_.mutable_receiver_log())) {
- OnReceivedReceiverLog(parser_.receiver_log());
- }
- }
- if (parser_.has_last_report()) {
- OnReceivedDelaySinceLastReport(parser_.last_report(),
- parser_.delay_since_last_report());
- }
- if (parser_.has_cast_message()) {
- parser_.mutable_cast_message()->ack_frame_id =
- ack_frame_id_wrap_helper_.MapTo32bitsFrameId(
- parser_.mutable_cast_message()->ack_frame_id);
- OnReceivedCastFeedback(parser_.cast_message());
- }
}
return true;
}
-bool Rtcp::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);
- RtcpReceiverEventLogMessages::iterator tmp = j;
- ++j;
- if (receiver_event_key_set_.insert(key).second) {
- receiver_event_key_queue_.push(key);
- if (receiver_event_key_queue_.size() > kReceiverRtcpEventHistorySize) {
- receiver_event_key_set_.erase(receiver_event_key_queue_.front());
- receiver_event_key_queue_.pop();
- }
- } else {
- messages->erase(tmp);
- }
- }
-
- RtcpReceiverLogMessage::iterator tmp = i;
- ++i;
- if (messages->empty()) {
- receiver_log->erase(tmp);
- }
- }
- return !receiver_log->empty();
-}
+void ReceiverRtcpSession::OnReceivedNtp(uint32_t ntp_seconds,
+ uint32_t ntp_fraction) {
+ last_report_truncated_ntp_ = ConvertToNtpDiff(ntp_seconds, ntp_fraction);
-RtcpTimeData Rtcp::ConvertToNTPAndSave(base::TimeTicks now) {
- RtcpTimeData ret;
- ret.timestamp = now;
+ const base::TimeTicks now = clock_->NowTicks();
+ time_last_report_received_ = 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;
+ // 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::SendRtcpFromRtpReceiver(
+void ReceiverRtcpSession::SendRtcpReport(
RtcpTimeData time_data,
const RtcpCastMessage* cast_message,
base::TimeDelta target_delay,
@@ -263,68 +130,14 @@ void Rtcp::SendRtcpFromRtpReceiver(
}
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) {
- uint32_t current_ntp_seconds = 0;
- uint32_t current_ntp_fractions = 0;
- ConvertTimeTicksToNtp(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;
- sender_info.ntp_fraction = current_ntp_fractions;
- sender_info.rtp_timestamp = current_time_as_rtp_timestamp;
- 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.";
+ local_ssrc_, rtcp_builder.BuildRtcpFromReceiver(
+ rtp_receiver_statistics ? &report_block : NULL, &rrtr,
+ cast_message, rtcp_events, target_delay));
}
-void Rtcp::OnReceivedLipSyncInfo(RtpTimeTicks rtp_timestamp,
- uint32_t ntp_seconds,
- uint32_t ntp_fraction) {
+void ReceiverRtcpSession::OnReceivedLipSyncInfo(RtpTimeTicks rtp_timestamp,
+ uint32_t ntp_seconds,
+ uint32_t ntp_fraction) {
if (ntp_seconds == 0) {
NOTREACHED();
return;
@@ -334,8 +147,9 @@ void Rtcp::OnReceivedLipSyncInfo(RtpTimeTicks rtp_timestamp,
(static_cast<uint64_t>(ntp_seconds) << 32) | ntp_fraction;
}
-bool Rtcp::GetLatestLipSyncTimes(RtpTimeTicks* rtp_timestamp,
- base::TimeTicks* reference_time) const {
+bool ReceiverRtcpSession::GetLatestLipSyncTimes(
+ RtpTimeTicks* rtp_timestamp,
+ base::TimeTicks* reference_time) const {
if (!lip_sync_ntp_timestamp_)
return false;
@@ -354,68 +168,5 @@ bool Rtcp::GetLatestLipSyncTimes(RtpTimeTicks* rtp_timestamp,
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_);
-}
-
-void Rtcp::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) {
- if (log_callback_.is_null())
- return;
- log_callback_.Run(receiver_log);
-}
-
} // namespace cast
} // namespace media
« no previous file with comments | « media/cast/net/rtcp/receiver_rtcp_session.h ('k') | media/cast/net/rtcp/rtcp.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698