| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "media/cast/net/rtcp/rtcp.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 | |
| 9 #include "base/time/time.h" | |
| 10 #include "media/cast/cast_environment.h" | |
| 11 #include "media/cast/constants.h" | |
| 12 #include "media/cast/net/cast_transport_config.h" | |
| 13 #include "media/cast/net/cast_transport_defines.h" | |
| 14 #include "media/cast/net/pacing/paced_sender.h" | |
| 15 #include "media/cast/net/rtcp/rtcp_builder.h" | |
| 16 #include "media/cast/net/rtcp/rtcp_defines.h" | |
| 17 #include "media/cast/net/rtcp/rtcp_utility.h" | |
| 18 | |
| 19 using base::TimeDelta; | |
| 20 | |
| 21 namespace media { | |
| 22 namespace cast { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 enum { | |
| 27 kStatsHistoryWindowMs = 10000, // 10 seconds. | |
| 28 | |
| 29 // Reject packets that are older than 0.5 seconds older than | |
| 30 // the newest packet we've seen so far. This protects internal | |
| 31 // states from crazy routers. (Based on RRTR) | |
| 32 kOutOfOrderMaxAgeMs = 500, | |
| 33 | |
| 34 // Minimum number of bytes required to make a valid RTCP packet. | |
| 35 kMinLengthOfRtcp = 8, | |
| 36 }; | |
| 37 | |
| 38 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is | |
| 39 // fractions of a second where 0x80000000 is half a second. | |
| 40 uint32_t ConvertToNtpDiff(uint32_t delay_seconds, uint32_t delay_fraction) { | |
| 41 return ((delay_seconds & 0x0000FFFF) << 16) + | |
| 42 ((delay_fraction & 0xFFFF0000) >> 16); | |
| 43 } | |
| 44 | |
| 45 // Parse a NTP diff value into a base::TimeDelta. | |
| 46 base::TimeDelta ConvertFromNtpDiff(uint32_t ntp_delay) { | |
| 47 int64_t delay_us = | |
| 48 (ntp_delay & 0x0000ffff) * base::Time::kMicrosecondsPerSecond; | |
| 49 delay_us >>= 16; | |
| 50 delay_us += | |
| 51 ((ntp_delay & 0xffff0000) >> 16) * base::Time::kMicrosecondsPerSecond; | |
| 52 return base::TimeDelta::FromMicroseconds(delay_us); | |
| 53 } | |
| 54 | |
| 55 // A receiver frame event is identified by frame RTP timestamp, event timestamp | |
| 56 // and event type. | |
| 57 // A receiver packet event is identified by all of the above plus packet id. | |
| 58 // The key format is as follows: | |
| 59 // First uint64_t: | |
| 60 // bits 0-11: zeroes (unused). | |
| 61 // bits 12-15: event type ID. | |
| 62 // bits 16-31: packet ID if packet event, 0 otherwise. | |
| 63 // bits 32-63: RTP timestamp. | |
| 64 // Second uint64_t: | |
| 65 // bits 0-63: event TimeTicks internal value. | |
| 66 std::pair<uint64_t, uint64_t> GetReceiverEventKey( | |
| 67 RtpTimeTicks frame_rtp_timestamp, | |
| 68 const base::TimeTicks& event_timestamp, | |
| 69 uint8_t event_type, | |
| 70 uint16_t packet_id_or_zero) { | |
| 71 uint64_t value1 = event_type; | |
| 72 value1 <<= 16; | |
| 73 value1 |= packet_id_or_zero; | |
| 74 value1 <<= 32; | |
| 75 value1 |= frame_rtp_timestamp.lower_32_bits(); | |
| 76 return std::make_pair( | |
| 77 value1, static_cast<uint64_t>(event_timestamp.ToInternalValue())); | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 Rtcp::Rtcp(const RtcpCastMessageCallback& cast_callback, | |
| 83 const RtcpRttCallback& rtt_callback, | |
| 84 const RtcpLogMessageCallback& log_callback, | |
| 85 base::TickClock* clock, | |
| 86 PacedPacketSender* packet_sender, | |
| 87 uint32_t local_ssrc, | |
| 88 uint32_t remote_ssrc) | |
| 89 : cast_callback_(cast_callback), | |
| 90 rtt_callback_(rtt_callback), | |
| 91 log_callback_(log_callback), | |
| 92 clock_(clock), | |
| 93 rtcp_builder_(local_ssrc), | |
| 94 packet_sender_(packet_sender), | |
| 95 local_ssrc_(local_ssrc), | |
| 96 remote_ssrc_(remote_ssrc), | |
| 97 parser_(local_ssrc_, remote_ssrc_), | |
| 98 last_report_truncated_ntp_(0), | |
| 99 local_clock_ahead_by_(ClockDriftSmoother::GetDefaultTimeConstant()), | |
| 100 lip_sync_ntp_timestamp_(0), | |
| 101 largest_seen_timestamp_(base::TimeTicks::FromInternalValue( | |
| 102 std::numeric_limits<int64_t>::min())), | |
| 103 ack_frame_id_wrap_helper_(kFirstFrameId - 1) {} | |
| 104 | |
| 105 Rtcp::~Rtcp() {} | |
| 106 | |
| 107 bool Rtcp::IsRtcpPacket(const uint8_t* packet, size_t length) { | |
| 108 if (length < kMinLengthOfRtcp) { | |
| 109 LOG(ERROR) << "Invalid RTCP packet received."; | |
| 110 return false; | |
| 111 } | |
| 112 | |
| 113 uint8_t packet_type = packet[1]; | |
| 114 return packet_type >= kPacketTypeLow && packet_type <= kPacketTypeHigh; | |
| 115 } | |
| 116 | |
| 117 uint32_t Rtcp::GetSsrcOfSender(const uint8_t* rtcp_buffer, size_t length) { | |
| 118 if (length < kMinLengthOfRtcp) | |
| 119 return 0; | |
| 120 uint32_t ssrc_of_sender; | |
| 121 base::BigEndianReader big_endian_reader( | |
| 122 reinterpret_cast<const char*>(rtcp_buffer), length); | |
| 123 big_endian_reader.Skip(4); // Skip header. | |
| 124 big_endian_reader.ReadU32(&ssrc_of_sender); | |
| 125 return ssrc_of_sender; | |
| 126 } | |
| 127 | |
| 128 bool Rtcp::IncomingRtcpPacket(const uint8_t* data, size_t length) { | |
| 129 // Check if this is a valid RTCP packet. | |
| 130 if (!IsRtcpPacket(data, length)) { | |
| 131 VLOG(1) << "Rtcp@" << this << "::IncomingRtcpPacket() -- " | |
| 132 << "Received an invalid (non-RTCP?) packet."; | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 // Check if this packet is to us. | |
| 137 uint32_t ssrc_of_sender = GetSsrcOfSender(data, length); | |
| 138 if (ssrc_of_sender != remote_ssrc_) { | |
| 139 return false; | |
| 140 } | |
| 141 | |
| 142 // Parse this packet. | |
| 143 base::BigEndianReader reader(reinterpret_cast<const char*>(data), length); | |
| 144 if (parser_.Parse(&reader)) { | |
| 145 if (parser_.has_receiver_reference_time_report()) { | |
| 146 base::TimeTicks t = ConvertNtpToTimeTicks( | |
| 147 parser_.receiver_reference_time_report().ntp_seconds, | |
| 148 parser_.receiver_reference_time_report().ntp_fraction); | |
| 149 if (t > largest_seen_timestamp_) { | |
| 150 largest_seen_timestamp_ = t; | |
| 151 } else if ((largest_seen_timestamp_ - t).InMilliseconds() > | |
| 152 kOutOfOrderMaxAgeMs) { | |
| 153 // Reject packet, it is too old. | |
| 154 VLOG(1) << "Rejecting RTCP packet as it is too old (" | |
| 155 << (largest_seen_timestamp_ - t).InMilliseconds() | |
| 156 << " ms)"; | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 160 OnReceivedNtp(parser_.receiver_reference_time_report().ntp_seconds, | |
| 161 parser_.receiver_reference_time_report().ntp_fraction); | |
| 162 } | |
| 163 if (parser_.has_sender_report()) { | |
| 164 OnReceivedNtp(parser_.sender_report().ntp_seconds, | |
| 165 parser_.sender_report().ntp_fraction); | |
| 166 OnReceivedLipSyncInfo(parser_.sender_report().rtp_timestamp, | |
| 167 parser_.sender_report().ntp_seconds, | |
| 168 parser_.sender_report().ntp_fraction); | |
| 169 } | |
| 170 if (parser_.has_receiver_log()) { | |
| 171 if (DedupeReceiverLog(parser_.mutable_receiver_log())) { | |
| 172 OnReceivedReceiverLog(parser_.receiver_log()); | |
| 173 } | |
| 174 } | |
| 175 if (parser_.has_last_report()) { | |
| 176 OnReceivedDelaySinceLastReport(parser_.last_report(), | |
| 177 parser_.delay_since_last_report()); | |
| 178 } | |
| 179 if (parser_.has_cast_message()) { | |
| 180 parser_.mutable_cast_message()->ack_frame_id = | |
| 181 ack_frame_id_wrap_helper_.MapTo32bitsFrameId( | |
| 182 parser_.mutable_cast_message()->ack_frame_id); | |
| 183 OnReceivedCastFeedback(parser_.cast_message()); | |
| 184 } | |
| 185 } | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 bool Rtcp::DedupeReceiverLog(RtcpReceiverLogMessage* receiver_log) { | |
| 190 RtcpReceiverLogMessage::iterator i = receiver_log->begin(); | |
| 191 while (i != receiver_log->end()) { | |
| 192 RtcpReceiverEventLogMessages* messages = &i->event_log_messages_; | |
| 193 RtcpReceiverEventLogMessages::iterator j = messages->begin(); | |
| 194 while (j != messages->end()) { | |
| 195 ReceiverEventKey key = GetReceiverEventKey(i->rtp_timestamp_, | |
| 196 j->event_timestamp, | |
| 197 j->type, | |
| 198 j->packet_id); | |
| 199 RtcpReceiverEventLogMessages::iterator tmp = j; | |
| 200 ++j; | |
| 201 if (receiver_event_key_set_.insert(key).second) { | |
| 202 receiver_event_key_queue_.push(key); | |
| 203 if (receiver_event_key_queue_.size() > kReceiverRtcpEventHistorySize) { | |
| 204 receiver_event_key_set_.erase(receiver_event_key_queue_.front()); | |
| 205 receiver_event_key_queue_.pop(); | |
| 206 } | |
| 207 } else { | |
| 208 messages->erase(tmp); | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 RtcpReceiverLogMessage::iterator tmp = i; | |
| 213 ++i; | |
| 214 if (messages->empty()) { | |
| 215 receiver_log->erase(tmp); | |
| 216 } | |
| 217 } | |
| 218 return !receiver_log->empty(); | |
| 219 } | |
| 220 | |
| 221 RtcpTimeData Rtcp::ConvertToNTPAndSave(base::TimeTicks now) { | |
| 222 RtcpTimeData ret; | |
| 223 ret.timestamp = now; | |
| 224 | |
| 225 // Attach our NTP to all RTCP packets; with this information a "smart" sender | |
| 226 // can make decisions based on how old the RTCP message is. | |
| 227 ConvertTimeTicksToNtp(now, &ret.ntp_seconds, &ret.ntp_fraction); | |
| 228 SaveLastSentNtpTime(now, ret.ntp_seconds, ret.ntp_fraction); | |
| 229 return ret; | |
| 230 } | |
| 231 | |
| 232 void Rtcp::SendRtcpFromRtpReceiver( | |
| 233 RtcpTimeData time_data, | |
| 234 const RtcpCastMessage* cast_message, | |
| 235 base::TimeDelta target_delay, | |
| 236 const ReceiverRtcpEventSubscriber::RtcpEvents* rtcp_events, | |
| 237 const RtpReceiverStatistics* rtp_receiver_statistics) const { | |
| 238 RtcpReportBlock report_block; | |
| 239 RtcpReceiverReferenceTimeReport rrtr; | |
| 240 rrtr.ntp_seconds = time_data.ntp_seconds; | |
| 241 rrtr.ntp_fraction = time_data.ntp_fraction; | |
| 242 | |
| 243 if (rtp_receiver_statistics) { | |
| 244 report_block.remote_ssrc = 0; // Not needed to set send side. | |
| 245 report_block.media_ssrc = remote_ssrc_; // SSRC of the RTP packet sender. | |
| 246 report_block.fraction_lost = rtp_receiver_statistics->fraction_lost; | |
| 247 report_block.cumulative_lost = rtp_receiver_statistics->cumulative_lost; | |
| 248 report_block.extended_high_sequence_number = | |
| 249 rtp_receiver_statistics->extended_high_sequence_number; | |
| 250 report_block.jitter = rtp_receiver_statistics->jitter; | |
| 251 report_block.last_sr = last_report_truncated_ntp_; | |
| 252 if (!time_last_report_received_.is_null()) { | |
| 253 uint32_t delay_seconds = 0; | |
| 254 uint32_t delay_fraction = 0; | |
| 255 base::TimeDelta delta = time_data.timestamp - time_last_report_received_; | |
| 256 ConvertTimeToFractions(delta.InMicroseconds(), &delay_seconds, | |
| 257 &delay_fraction); | |
| 258 report_block.delay_since_last_sr = | |
| 259 ConvertToNtpDiff(delay_seconds, delay_fraction); | |
| 260 } else { | |
| 261 report_block.delay_since_last_sr = 0; | |
| 262 } | |
| 263 } | |
| 264 RtcpBuilder rtcp_builder(local_ssrc_); | |
| 265 packet_sender_->SendRtcpPacket( | |
| 266 local_ssrc_, | |
| 267 rtcp_builder.BuildRtcpFromReceiver( | |
| 268 rtp_receiver_statistics ? &report_block : NULL, | |
| 269 &rrtr, | |
| 270 cast_message, | |
| 271 rtcp_events, | |
| 272 target_delay)); | |
| 273 } | |
| 274 | |
| 275 void Rtcp::SendRtcpFromRtpSender(base::TimeTicks current_time, | |
| 276 RtpTimeTicks current_time_as_rtp_timestamp, | |
| 277 uint32_t send_packet_count, | |
| 278 size_t send_octet_count) { | |
| 279 uint32_t current_ntp_seconds = 0; | |
| 280 uint32_t current_ntp_fractions = 0; | |
| 281 ConvertTimeTicksToNtp(current_time, ¤t_ntp_seconds, | |
| 282 ¤t_ntp_fractions); | |
| 283 SaveLastSentNtpTime(current_time, current_ntp_seconds, | |
| 284 current_ntp_fractions); | |
| 285 | |
| 286 RtcpSenderInfo sender_info; | |
| 287 sender_info.ntp_seconds = current_ntp_seconds; | |
| 288 sender_info.ntp_fraction = current_ntp_fractions; | |
| 289 sender_info.rtp_timestamp = current_time_as_rtp_timestamp; | |
| 290 sender_info.send_packet_count = send_packet_count; | |
| 291 sender_info.send_octet_count = send_octet_count; | |
| 292 | |
| 293 packet_sender_->SendRtcpPacket( | |
| 294 local_ssrc_, | |
| 295 rtcp_builder_.BuildRtcpFromSender(sender_info)); | |
| 296 } | |
| 297 | |
| 298 void Rtcp::OnReceivedNtp(uint32_t ntp_seconds, uint32_t ntp_fraction) { | |
| 299 last_report_truncated_ntp_ = ConvertToNtpDiff(ntp_seconds, ntp_fraction); | |
| 300 | |
| 301 const base::TimeTicks now = clock_->NowTicks(); | |
| 302 time_last_report_received_ = now; | |
| 303 | |
| 304 // TODO(miu): This clock offset calculation does not account for packet | |
| 305 // transit time over the network. End2EndTest.EvilNetwork confirms that this | |
| 306 // contributes a very significant source of error here. Determine whether | |
| 307 // RTT should be factored-in, and how that changes the rest of the | |
| 308 // calculation. | |
| 309 const base::TimeDelta measured_offset = | |
| 310 now - ConvertNtpToTimeTicks(ntp_seconds, ntp_fraction); | |
| 311 local_clock_ahead_by_.Update(now, measured_offset); | |
| 312 if (measured_offset < local_clock_ahead_by_.Current()) { | |
| 313 // Logically, the minimum offset between the clocks has to be the correct | |
| 314 // one. For example, the time it took to transmit the current report may | |
| 315 // have been lower than usual, and so some of the error introduced by the | |
| 316 // transmission time can be eliminated. | |
| 317 local_clock_ahead_by_.Reset(now, measured_offset); | |
| 318 } | |
| 319 VLOG(1) << "Local clock is ahead of the remote clock by: " | |
| 320 << "measured=" << measured_offset.InMicroseconds() << " usec, " | |
| 321 << "filtered=" << local_clock_ahead_by_.Current().InMicroseconds() | |
| 322 << " usec."; | |
| 323 } | |
| 324 | |
| 325 void Rtcp::OnReceivedLipSyncInfo(RtpTimeTicks rtp_timestamp, | |
| 326 uint32_t ntp_seconds, | |
| 327 uint32_t ntp_fraction) { | |
| 328 if (ntp_seconds == 0) { | |
| 329 NOTREACHED(); | |
| 330 return; | |
| 331 } | |
| 332 lip_sync_rtp_timestamp_ = rtp_timestamp; | |
| 333 lip_sync_ntp_timestamp_ = | |
| 334 (static_cast<uint64_t>(ntp_seconds) << 32) | ntp_fraction; | |
| 335 } | |
| 336 | |
| 337 bool Rtcp::GetLatestLipSyncTimes(RtpTimeTicks* rtp_timestamp, | |
| 338 base::TimeTicks* reference_time) const { | |
| 339 if (!lip_sync_ntp_timestamp_) | |
| 340 return false; | |
| 341 | |
| 342 const base::TimeTicks local_reference_time = | |
| 343 ConvertNtpToTimeTicks( | |
| 344 static_cast<uint32_t>(lip_sync_ntp_timestamp_ >> 32), | |
| 345 static_cast<uint32_t>(lip_sync_ntp_timestamp_)) + | |
| 346 local_clock_ahead_by_.Current(); | |
| 347 | |
| 348 // Sanity-check: Getting regular lip sync updates? | |
| 349 DCHECK((clock_->NowTicks() - local_reference_time) < | |
| 350 base::TimeDelta::FromMinutes(1)); | |
| 351 | |
| 352 *rtp_timestamp = lip_sync_rtp_timestamp_; | |
| 353 *reference_time = local_reference_time; | |
| 354 return true; | |
| 355 } | |
| 356 | |
| 357 void Rtcp::OnReceivedDelaySinceLastReport(uint32_t last_report, | |
| 358 uint32_t delay_since_last_report) { | |
| 359 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); | |
| 360 if (it == last_reports_sent_map_.end()) { | |
| 361 return; // Feedback on another report. | |
| 362 } | |
| 363 | |
| 364 const base::TimeDelta sender_delay = clock_->NowTicks() - it->second; | |
| 365 const base::TimeDelta receiver_delay = | |
| 366 ConvertFromNtpDiff(delay_since_last_report); | |
| 367 current_round_trip_time_ = sender_delay - receiver_delay; | |
| 368 // If the round trip time was computed as less than 1 ms, assume clock | |
| 369 // imprecision by one or both peers caused a bad value to be calculated. | |
| 370 // While plenty of networks do easily achieve less than 1 ms round trip time, | |
| 371 // such a level of precision cannot be measured with our approach; and 1 ms is | |
| 372 // good enough to represent "under 1 ms" for our use cases. | |
| 373 current_round_trip_time_ = | |
| 374 std::max(current_round_trip_time_, base::TimeDelta::FromMilliseconds(1)); | |
| 375 | |
| 376 if (!rtt_callback_.is_null()) | |
| 377 rtt_callback_.Run(current_round_trip_time_); | |
| 378 } | |
| 379 | |
| 380 void Rtcp::OnReceivedCastFeedback(const RtcpCastMessage& cast_message) { | |
| 381 if (cast_callback_.is_null()) | |
| 382 return; | |
| 383 cast_callback_.Run(cast_message); | |
| 384 } | |
| 385 | |
| 386 void Rtcp::SaveLastSentNtpTime(const base::TimeTicks& now, | |
| 387 uint32_t last_ntp_seconds, | |
| 388 uint32_t last_ntp_fraction) { | |
| 389 // Make sure |now| is always greater than the last element in | |
| 390 // |last_reports_sent_queue_|. | |
| 391 if (!last_reports_sent_queue_.empty()) { | |
| 392 DCHECK(now >= last_reports_sent_queue_.back().second); | |
| 393 } | |
| 394 | |
| 395 uint32_t last_report = ConvertToNtpDiff(last_ntp_seconds, last_ntp_fraction); | |
| 396 last_reports_sent_map_[last_report] = now; | |
| 397 last_reports_sent_queue_.push(std::make_pair(last_report, now)); | |
| 398 | |
| 399 const base::TimeTicks timeout = | |
| 400 now - TimeDelta::FromMilliseconds(kStatsHistoryWindowMs); | |
| 401 | |
| 402 // Cleanup old statistics older than |timeout|. | |
| 403 while (!last_reports_sent_queue_.empty()) { | |
| 404 RtcpSendTimePair oldest_report = last_reports_sent_queue_.front(); | |
| 405 if (oldest_report.second < timeout) { | |
| 406 last_reports_sent_map_.erase(oldest_report.first); | |
| 407 last_reports_sent_queue_.pop(); | |
| 408 } else { | |
| 409 break; | |
| 410 } | |
| 411 } | |
| 412 } | |
| 413 | |
| 414 void Rtcp::OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log) { | |
| 415 if (log_callback_.is_null()) | |
| 416 return; | |
| 417 log_callback_.Run(receiver_log); | |
| 418 } | |
| 419 | |
| 420 } // namespace cast | |
| 421 } // namespace media | |
| OLD | NEW |