OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <algorithm> |
| 6 #include <limits> |
| 7 #include <utility> |
| 8 |
| 9 #include "base/big_endian.h" |
| 10 #include "base/time/time.h" |
| 11 #include "media/cast/constants.h" |
| 12 #include "media/cast/net/pacing/paced_sender.h" |
| 13 #include "media/cast/net/rtcp/rtcp_builder.h" |
| 14 #include "media/cast/net/rtcp/rtcp_defines.h" |
| 15 #include "media/cast/net/rtcp/rtcp_utility.h" |
| 16 #include "media/cast/net/rtcp/sender_rtcp_session.h" |
| 17 |
| 18 namespace media { |
| 19 namespace cast { |
| 20 |
| 21 namespace { |
| 22 |
| 23 enum { |
| 24 kStatsHistoryWindowMs = 10000, // 10 seconds. |
| 25 |
| 26 // Reject packets that are 0.5 seconds older than |
| 27 // the newest packet we've seen so far. This protects internal |
| 28 // states from crazy routers. (Based on RRTR) |
| 29 kOutOfOrderMaxAgeMs = 500, |
| 30 }; |
| 31 |
| 32 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is |
| 33 // fractions of a second where 0x80000000 is half a second. |
| 34 uint32_t ConvertToNtpDiff(uint32_t delay_seconds, uint32_t delay_fraction) { |
| 35 return ((delay_seconds & 0x0000FFFF) << 16) + |
| 36 ((delay_fraction & 0xFFFF0000) >> 16); |
| 37 } |
| 38 |
| 39 // Parse a NTP diff value into a base::TimeDelta. |
| 40 base::TimeDelta ConvertFromNtpDiff(uint32_t ntp_delay) { |
| 41 int64_t delay_us = |
| 42 (ntp_delay & 0x0000ffff) * base::Time::kMicrosecondsPerSecond; |
| 43 delay_us >>= 16; |
| 44 delay_us += |
| 45 ((ntp_delay & 0xffff0000) >> 16) * base::Time::kMicrosecondsPerSecond; |
| 46 return base::TimeDelta::FromMicroseconds(delay_us); |
| 47 } |
| 48 |
| 49 // A receiver frame event is identified by frame RTP timestamp, event timestamp |
| 50 // and event type. |
| 51 // A receiver packet event is identified by all of the above plus packet id. |
| 52 // The key format is as follows: |
| 53 // First uint64_t: |
| 54 // bits 0-11: zeroes (unused). |
| 55 // bits 12-15: event type ID. |
| 56 // bits 16-31: packet ID if packet event, 0 otherwise. |
| 57 // bits 32-63: RTP timestamp. |
| 58 // Second uint64_t: |
| 59 // bits 0-63: event TimeTicks internal value. |
| 60 std::pair<uint64_t, uint64_t> GetReceiverEventKey( |
| 61 RtpTimeTicks frame_rtp_timestamp, |
| 62 const base::TimeTicks& event_timestamp, |
| 63 uint8_t event_type, |
| 64 uint16_t packet_id_or_zero) { |
| 65 uint64_t value1 = event_type; |
| 66 value1 <<= 16; |
| 67 value1 |= packet_id_or_zero; |
| 68 value1 <<= 32; |
| 69 value1 |= frame_rtp_timestamp.lower_32_bits(); |
| 70 return std::make_pair( |
| 71 value1, static_cast<uint64_t>(event_timestamp.ToInternalValue())); |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 SenderRtcpSession::SenderRtcpSession( |
| 77 const RtcpCastMessageCallback& cast_callback, |
| 78 const RtcpRttCallback& rtt_callback, |
| 79 const RtcpLogMessageCallback& log_callback, |
| 80 base::TickClock* clock, |
| 81 PacedPacketSender* packet_sender, |
| 82 uint32_t local_ssrc, |
| 83 uint32_t remote_ssrc) |
| 84 : clock_(clock), |
| 85 packet_sender_(packet_sender), |
| 86 local_ssrc_(local_ssrc), |
| 87 remote_ssrc_(remote_ssrc), |
| 88 cast_callback_(cast_callback), |
| 89 rtt_callback_(rtt_callback), |
| 90 log_callback_(log_callback), |
| 91 largest_seen_timestamp_(base::TimeTicks::FromInternalValue( |
| 92 std::numeric_limits<int64_t>::min())), |
| 93 parser_(local_ssrc, remote_ssrc), |
| 94 ack_frame_id_wrap_helper_(kFirstFrameId - 1) {} |
| 95 |
| 96 SenderRtcpSession::~SenderRtcpSession() {} |
| 97 |
| 98 bool SenderRtcpSession::IncomingRtcpPacket(const uint8_t* data, size_t length) { |
| 99 // Check if this is a valid RTCP packet. |
| 100 if (!IsRtcpPacket(data, length)) { |
| 101 VLOG(1) << "Rtcp@" << this << "::IncomingRtcpPacket() -- " |
| 102 << "Received an invalid (non-RTCP?) packet."; |
| 103 return false; |
| 104 } |
| 105 |
| 106 // Check if this packet is to us. |
| 107 uint32_t ssrc_of_sender = GetSsrcOfSender(data, length); |
| 108 if (ssrc_of_sender != remote_ssrc_) { |
| 109 return false; |
| 110 } |
| 111 |
| 112 // Parse this packet. |
| 113 base::BigEndianReader reader(reinterpret_cast<const char*>(data), length); |
| 114 if (parser_.Parse(&reader)) { |
| 115 if (parser_.has_receiver_reference_time_report()) { |
| 116 base::TimeTicks t = ConvertNtpToTimeTicks( |
| 117 parser_.receiver_reference_time_report().ntp_seconds, |
| 118 parser_.receiver_reference_time_report().ntp_fraction); |
| 119 if (t > largest_seen_timestamp_) { |
| 120 largest_seen_timestamp_ = t; |
| 121 } else if ((largest_seen_timestamp_ - t).InMilliseconds() > |
| 122 kOutOfOrderMaxAgeMs) { |
| 123 // Reject packet, it is too old. |
| 124 VLOG(1) << "Rejecting RTCP packet as it is too old (" |
| 125 << (largest_seen_timestamp_ - t).InMilliseconds() << " ms)"; |
| 126 return true; |
| 127 } |
| 128 } |
| 129 if (parser_.has_receiver_log()) { |
| 130 if (DedupeReceiverLog(parser_.mutable_receiver_log())) { |
| 131 OnReceivedReceiverLog(parser_.receiver_log()); |
| 132 } |
| 133 } |
| 134 if (parser_.has_last_report()) { |
| 135 OnReceivedDelaySinceLastReport(parser_.last_report(), |
| 136 parser_.delay_since_last_report()); |
| 137 } |
| 138 if (parser_.has_cast_message()) { |
| 139 VLOG(1) << "1 sender_rtcp_session " |
| 140 << parser_.mutable_cast_message()->ack_frame_id; |
| 141 parser_.mutable_cast_message()->ack_frame_id = |
| 142 ack_frame_id_wrap_helper_.MapTo32bitsFrameId( |
| 143 parser_.mutable_cast_message()->ack_frame_id); |
| 144 VLOG(1) << "2 sender_rtcp_session " |
| 145 << parser_.mutable_cast_message()->ack_frame_id; |
| 146 OnReceivedCastFeedback(parser_.cast_message()); |
| 147 } |
| 148 } |
| 149 return true; |
| 150 } |
| 151 |
| 152 void SenderRtcpSession::OnReceivedDelaySinceLastReport( |
| 153 uint32_t last_report, |
| 154 uint32_t delay_since_last_report) { |
| 155 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); |
| 156 if (it == last_reports_sent_map_.end()) { |
| 157 return; // Feedback on another report. |
| 158 } |
| 159 |
| 160 const base::TimeDelta sender_delay = clock_->NowTicks() - it->second; |
| 161 const base::TimeDelta receiver_delay = |
| 162 ConvertFromNtpDiff(delay_since_last_report); |
| 163 current_round_trip_time_ = sender_delay - receiver_delay; |
| 164 // If the round trip time was computed as less than 1 ms, assume clock |
| 165 // imprecision by one or both peers caused a bad value to be calculated. |
| 166 // While plenty of networks do easily achieve less than 1 ms round trip time, |
| 167 // such a level of precision cannot be measured with our approach; and 1 ms is |
| 168 // good enough to represent "under 1 ms" for our use cases. |
| 169 current_round_trip_time_ = |
| 170 std::max(current_round_trip_time_, base::TimeDelta::FromMilliseconds(1)); |
| 171 |
| 172 if (!rtt_callback_.is_null()) |
| 173 rtt_callback_.Run(current_round_trip_time_); |
| 174 } |
| 175 |
| 176 void SenderRtcpSession::SaveLastSentNtpTime(const base::TimeTicks& now, |
| 177 uint32_t last_ntp_seconds, |
| 178 uint32_t last_ntp_fraction) { |
| 179 // Make sure |now| is always greater than the last element in |
| 180 // |last_reports_sent_queue_|. |
| 181 if (!last_reports_sent_queue_.empty()) { |
| 182 DCHECK(now >= last_reports_sent_queue_.back().second); |
| 183 } |
| 184 |
| 185 uint32_t last_report = ConvertToNtpDiff(last_ntp_seconds, last_ntp_fraction); |
| 186 last_reports_sent_map_[last_report] = now; |
| 187 last_reports_sent_queue_.push(std::make_pair(last_report, now)); |
| 188 |
| 189 const base::TimeTicks timeout = |
| 190 now - base::TimeDelta::FromMilliseconds(kStatsHistoryWindowMs); |
| 191 |
| 192 // Cleanup old statistics older than |timeout|. |
| 193 while (!last_reports_sent_queue_.empty()) { |
| 194 RtcpSendTimePair oldest_report = last_reports_sent_queue_.front(); |
| 195 if (oldest_report.second < timeout) { |
| 196 last_reports_sent_map_.erase(oldest_report.first); |
| 197 last_reports_sent_queue_.pop(); |
| 198 } else { |
| 199 break; |
| 200 } |
| 201 } |
| 202 } |
| 203 |
| 204 bool SenderRtcpSession::DedupeReceiverLog( |
| 205 RtcpReceiverLogMessage* receiver_log) { |
| 206 RtcpReceiverLogMessage::iterator i = receiver_log->begin(); |
| 207 while (i != receiver_log->end()) { |
| 208 RtcpReceiverEventLogMessages* messages = &i->event_log_messages_; |
| 209 RtcpReceiverEventLogMessages::iterator j = messages->begin(); |
| 210 while (j != messages->end()) { |
| 211 ReceiverEventKey key = GetReceiverEventKey( |
| 212 i->rtp_timestamp_, j->event_timestamp, j->type, j->packet_id); |
| 213 RtcpReceiverEventLogMessages::iterator tmp = j; |
| 214 ++j; |
| 215 if (receiver_event_key_set_.insert(key).second) { |
| 216 receiver_event_key_queue_.push(key); |
| 217 if (receiver_event_key_queue_.size() > kReceiverRtcpEventHistorySize) { |
| 218 receiver_event_key_set_.erase(receiver_event_key_queue_.front()); |
| 219 receiver_event_key_queue_.pop(); |
| 220 } |
| 221 } else { |
| 222 messages->erase(tmp); |
| 223 } |
| 224 } |
| 225 |
| 226 RtcpReceiverLogMessage::iterator tmp = i; |
| 227 ++i; |
| 228 if (messages->empty()) { |
| 229 receiver_log->erase(tmp); |
| 230 } |
| 231 } |
| 232 return !receiver_log->empty(); |
| 233 } |
| 234 |
| 235 void SenderRtcpSession::SendRtcpReport( |
| 236 base::TimeTicks current_time, |
| 237 RtpTimeTicks current_time_as_rtp_timestamp, |
| 238 uint32_t send_packet_count, |
| 239 size_t send_octet_count) { |
| 240 uint32_t current_ntp_seconds = 0; |
| 241 uint32_t current_ntp_fractions = 0; |
| 242 ConvertTimeTicksToNtp(current_time, ¤t_ntp_seconds, |
| 243 ¤t_ntp_fractions); |
| 244 SaveLastSentNtpTime(current_time, current_ntp_seconds, current_ntp_fractions); |
| 245 |
| 246 RtcpSenderInfo sender_info; |
| 247 sender_info.ntp_seconds = current_ntp_seconds; |
| 248 sender_info.ntp_fraction = current_ntp_fractions; |
| 249 sender_info.rtp_timestamp = current_time_as_rtp_timestamp; |
| 250 sender_info.send_packet_count = send_packet_count; |
| 251 sender_info.send_octet_count = send_octet_count; |
| 252 |
| 253 RtcpBuilder rtcp_builder(local_ssrc_); |
| 254 packet_sender_->SendRtcpPacket(local_ssrc_, |
| 255 rtcp_builder.BuildRtcpFromSender(sender_info)); |
| 256 } |
| 257 |
| 258 void SenderRtcpSession::OnReceivedCastFeedback( |
| 259 const RtcpCastMessage& cast_message) { |
| 260 if (cast_callback_.is_null()) |
| 261 return; |
| 262 cast_callback_.Run(cast_message); |
| 263 } |
| 264 |
| 265 void SenderRtcpSession::OnReceivedReceiverLog( |
| 266 const RtcpReceiverLogMessage& receiver_log) { |
| 267 if (log_callback_.is_null()) |
| 268 return; |
| 269 log_callback_.Run(receiver_log); |
| 270 } |
| 271 |
| 272 } // namespace cast |
| 273 } // namespace media |
OLD | NEW |