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

Unified Diff: media/cast/net/rtcp/sender_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: Add missing rtcp_session.h and a few comments 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
Index: media/cast/net/rtcp/sender_rtcp_session.cc
diff --git a/media/cast/net/rtcp/sender_rtcp_session.cc b/media/cast/net/rtcp/sender_rtcp_session.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f19ba6d34989ebb0547b97dcf1b9cd9eb7d3c513
--- /dev/null
+++ b/media/cast/net/rtcp/sender_rtcp_session.cc
@@ -0,0 +1,269 @@
+// 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 <algorithm>
+#include <limits>
+#include <utility>
+
+#include "base/big_endian.h"
+#include "base/time/time.h"
+#include "media/cast/constants.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"
+#include "media/cast/net/rtcp/sender_rtcp_session.h"
+
+namespace media {
+namespace cast {
+
+namespace {
+
+enum {
+ kStatsHistoryWindowMs = 10000, // 10 seconds.
+
+ // 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)
+ kOutOfOrderMaxAgeMs = 500,
miu 2015/12/12 00:53:25 Can you put a TODO (and crbug) here too? It feels
Irfan 2015/12/12 01:07:37 Done.
+};
+
+// 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
+
+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),
+ 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) {}
+
+SenderRtcpSession::~SenderRtcpSession() {}
+
+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() -- "
+ << "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;
+ }
+ }
+ 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;
+}
+
+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);
+ 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 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, &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;
+
+ RtcpBuilder rtcp_builder(local_ssrc_);
+ packet_sender_->SendRtcpPacket(local_ssrc_,
+ rtcp_builder.BuildRtcpFromSender(sender_info));
+}
+
+void SenderRtcpSession::OnReceivedCastFeedback(
+ const RtcpCastMessage& cast_message) {
+ if (cast_callback_.is_null())
+ return;
+ cast_callback_.Run(cast_message);
+}
+
+void SenderRtcpSession::OnReceivedReceiverLog(
+ const RtcpReceiverLogMessage& receiver_log) {
+ if (log_callback_.is_null())
+ return;
+ log_callback_.Run(receiver_log);
+}
+
+} // namespace cast
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698