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

Unified Diff: media/cast/net/rtcp/rtcp.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/rtcp.h ('k') | media/cast/net/rtcp/rtcp_defines.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/cast/net/rtcp/rtcp.cc
diff --git a/media/cast/net/rtcp/rtcp.cc b/media/cast/net/rtcp/rtcp.cc
deleted file mode 100644
index 00cccf2a13f8bb09af091fb0b78237d6aa571a52..0000000000000000000000000000000000000000
--- a/media/cast/net/rtcp/rtcp.cc
+++ /dev/null
@@ -1,421 +0,0 @@
-// Copyright 2014 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 "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;
-
-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) {
- return ((delay_seconds & 0x0000FFFF) << 16) +
- ((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),
- 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) {}
-
-Rtcp::~Rtcp() {}
-
-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) {
- // Check if this is a valid RTCP packet.
- if (!IsRtcpPacket(data, length)) {
- VLOG(1) << "Rtcp@" << this << "::IncomingRtcpPacket() -- "
- << "Received an invalid (non-RTCP?) packet.";
- return false;
- }
-
- // Check if this packet is to us.
- uint32_t ssrc_of_sender = GetSsrcOfSender(data, length);
- if (ssrc_of_sender != remote_ssrc_) {
- return false;
- }
-
- // 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);
- 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())) {
- 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();
-}
-
-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) {
- 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.";
-}
-
-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_);
-}
-
-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/rtcp.h ('k') | media/cast/net/rtcp/rtcp_defines.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698