| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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/rtcp/rtcp.h" | |
| 6 | |
| 7 #include "base/big_endian.h" | |
| 8 #include "media/cast/cast_config.h" | |
| 9 #include "media/cast/cast_defines.h" | |
| 10 #include "media/cast/cast_environment.h" | |
| 11 #include "media/cast/rtcp/rtcp_defines.h" | |
| 12 #include "media/cast/rtcp/rtcp_receiver.h" | |
| 13 #include "media/cast/rtcp/rtcp_sender.h" | |
| 14 #include "media/cast/rtcp/rtcp_utility.h" | |
| 15 #include "media/cast/transport/cast_transport_defines.h" | |
| 16 | |
| 17 using base::TimeDelta; | |
| 18 | |
| 19 namespace media { | |
| 20 namespace cast { | |
| 21 | |
| 22 static const int32 kMaxRttMs = 10000; // 10 seconds. | |
| 23 static const int32 kMaxDelayMs = 2000; // 2 seconds. | |
| 24 | |
| 25 class LocalRtcpRttFeedback : public RtcpRttFeedback { | |
| 26 public: | |
| 27 explicit LocalRtcpRttFeedback(Rtcp* rtcp) : rtcp_(rtcp) {} | |
| 28 | |
| 29 virtual void OnReceivedDelaySinceLastReport( | |
| 30 uint32 receivers_ssrc, uint32 last_report, | |
| 31 uint32 delay_since_last_report) OVERRIDE { | |
| 32 rtcp_->OnReceivedDelaySinceLastReport(receivers_ssrc, last_report, | |
| 33 delay_since_last_report); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 Rtcp* rtcp_; | |
| 38 }; | |
| 39 | |
| 40 class LocalRtcpReceiverFeedback : public RtcpReceiverFeedback { | |
| 41 public: | |
| 42 LocalRtcpReceiverFeedback(Rtcp* rtcp, | |
| 43 scoped_refptr<CastEnvironment> cast_environment) | |
| 44 : rtcp_(rtcp), cast_environment_(cast_environment) {} | |
| 45 | |
| 46 virtual void OnReceivedSenderReport( | |
| 47 const transport::RtcpSenderInfo& remote_sender_info) OVERRIDE { | |
| 48 rtcp_->OnReceivedNtp(remote_sender_info.ntp_seconds, | |
| 49 remote_sender_info.ntp_fraction); | |
| 50 if (remote_sender_info.send_packet_count != 0) { | |
| 51 rtcp_->OnReceivedLipSyncInfo(remote_sender_info.rtp_timestamp, | |
| 52 remote_sender_info.ntp_seconds, | |
| 53 remote_sender_info.ntp_fraction); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 virtual void OnReceiverReferenceTimeReport( | |
| 58 const RtcpReceiverReferenceTimeReport& remote_time_report) OVERRIDE { | |
| 59 rtcp_->OnReceivedNtp(remote_time_report.ntp_seconds, | |
| 60 remote_time_report.ntp_fraction); | |
| 61 } | |
| 62 | |
| 63 virtual void OnReceivedSendReportRequest() OVERRIDE { | |
| 64 rtcp_->OnReceivedSendReportRequest(); | |
| 65 } | |
| 66 | |
| 67 virtual void OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log) | |
| 68 OVERRIDE { | |
| 69 rtcp_->OnReceivedReceiverLog(receiver_log); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 Rtcp* rtcp_; | |
| 74 scoped_refptr<CastEnvironment> cast_environment_; | |
| 75 }; | |
| 76 | |
| 77 Rtcp::Rtcp(scoped_refptr<CastEnvironment> cast_environment, | |
| 78 RtcpSenderFeedback* sender_feedback, | |
| 79 transport::CastTransportSender* const transport_sender, | |
| 80 transport::PacedPacketSender* paced_packet_sender, | |
| 81 RtpReceiverStatistics* rtp_receiver_statistics, RtcpMode rtcp_mode, | |
| 82 const base::TimeDelta& rtcp_interval, uint32 local_ssrc, | |
| 83 uint32 remote_ssrc, const std::string& c_name, | |
| 84 EventMediaType event_media_type) | |
| 85 : cast_environment_(cast_environment), | |
| 86 transport_sender_(transport_sender), | |
| 87 rtcp_interval_(rtcp_interval), | |
| 88 rtcp_mode_(rtcp_mode), | |
| 89 local_ssrc_(local_ssrc), | |
| 90 remote_ssrc_(remote_ssrc), | |
| 91 c_name_(c_name), | |
| 92 event_media_type_(event_media_type), | |
| 93 rtp_receiver_statistics_(rtp_receiver_statistics), | |
| 94 rtt_feedback_(new LocalRtcpRttFeedback(this)), | |
| 95 receiver_feedback_(new LocalRtcpReceiverFeedback(this, cast_environment)), | |
| 96 rtcp_sender_(new RtcpSender(cast_environment, paced_packet_sender, | |
| 97 local_ssrc, c_name)), | |
| 98 last_report_truncated_ntp_(0), | |
| 99 local_clock_ahead_by_(ClockDriftSmoother::GetDefaultTimeConstant()), | |
| 100 lip_sync_rtp_timestamp_(0), | |
| 101 lip_sync_ntp_timestamp_(0), | |
| 102 min_rtt_(TimeDelta::FromMilliseconds(kMaxRttMs)), | |
| 103 number_of_rtt_in_avg_(0) { | |
| 104 rtcp_receiver_.reset(new RtcpReceiver(cast_environment, sender_feedback, | |
| 105 receiver_feedback_.get(), | |
| 106 rtt_feedback_.get(), local_ssrc)); | |
| 107 rtcp_receiver_->SetRemoteSSRC(remote_ssrc); | |
| 108 } | |
| 109 | |
| 110 Rtcp::~Rtcp() {} | |
| 111 | |
| 112 // static | |
| 113 bool Rtcp::IsRtcpPacket(const uint8* packet, size_t length) { | |
| 114 DCHECK_GE(length, kMinLengthOfRtcp) << "Invalid RTCP packet"; | |
| 115 if (length < kMinLengthOfRtcp) return false; | |
| 116 | |
| 117 uint8 packet_type = packet[1]; | |
| 118 if (packet_type >= transport::kPacketTypeLow && | |
| 119 packet_type <= transport::kPacketTypeHigh) { | |
| 120 return true; | |
| 121 } | |
| 122 return false; | |
| 123 } | |
| 124 | |
| 125 // static | |
| 126 uint32 Rtcp::GetSsrcOfSender(const uint8* rtcp_buffer, size_t length) { | |
| 127 DCHECK_GE(length, kMinLengthOfRtcp) << "Invalid RTCP packet"; | |
| 128 uint32 ssrc_of_sender; | |
| 129 base::BigEndianReader big_endian_reader( | |
| 130 reinterpret_cast<const char*>(rtcp_buffer), length); | |
| 131 big_endian_reader.Skip(4); // Skip header | |
| 132 big_endian_reader.ReadU32(&ssrc_of_sender); | |
| 133 return ssrc_of_sender; | |
| 134 } | |
| 135 | |
| 136 base::TimeTicks Rtcp::TimeToSendNextRtcpReport() { | |
| 137 if (next_time_to_send_rtcp_.is_null()) { | |
| 138 UpdateNextTimeToSendRtcp(); | |
| 139 } | |
| 140 return next_time_to_send_rtcp_; | |
| 141 } | |
| 142 | |
| 143 void Rtcp::IncomingRtcpPacket(const uint8* rtcp_buffer, size_t length) { | |
| 144 RtcpParser rtcp_parser(rtcp_buffer, length); | |
| 145 if (!rtcp_parser.IsValid()) { | |
| 146 // Silently ignore packet. | |
| 147 DLOG(ERROR) << "Received invalid RTCP packet"; | |
| 148 return; | |
| 149 } | |
| 150 rtcp_receiver_->IncomingRtcpPacket(&rtcp_parser); | |
| 151 } | |
| 152 | |
| 153 void Rtcp::SendRtcpFromRtpReceiver( | |
| 154 const RtcpCastMessage* cast_message, | |
| 155 const ReceiverRtcpEventSubscriber::RtcpEventMultiMap* rtcp_events) { | |
| 156 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 157 uint32 packet_type_flags = 0; | |
| 158 | |
| 159 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
| 160 transport::RtcpReportBlock report_block; | |
| 161 RtcpReceiverReferenceTimeReport rrtr; | |
| 162 | |
| 163 // Attach our NTP to all RTCP packets; with this information a "smart" sender | |
| 164 // can make decisions based on how old the RTCP message is. | |
| 165 packet_type_flags |= transport::kRtcpRrtr; | |
| 166 ConvertTimeTicksToNtp(now, &rrtr.ntp_seconds, &rrtr.ntp_fraction); | |
| 167 SaveLastSentNtpTime(now, rrtr.ntp_seconds, rrtr.ntp_fraction); | |
| 168 | |
| 169 if (cast_message) { | |
| 170 packet_type_flags |= transport::kRtcpCast; | |
| 171 } | |
| 172 if (rtcp_events) { | |
| 173 packet_type_flags |= transport::kRtcpReceiverLog; | |
| 174 } | |
| 175 if (rtcp_mode_ == kRtcpCompound || now >= next_time_to_send_rtcp_) { | |
| 176 packet_type_flags |= transport::kRtcpRr; | |
| 177 | |
| 178 report_block.remote_ssrc = 0; // Not needed to set send side. | |
| 179 report_block.media_ssrc = remote_ssrc_; // SSRC of the RTP packet sender. | |
| 180 if (rtp_receiver_statistics_) { | |
| 181 rtp_receiver_statistics_->GetStatistics( | |
| 182 &report_block.fraction_lost, &report_block.cumulative_lost, | |
| 183 &report_block.extended_high_sequence_number, &report_block.jitter); | |
| 184 } | |
| 185 | |
| 186 report_block.last_sr = last_report_truncated_ntp_; | |
| 187 if (!time_last_report_received_.is_null()) { | |
| 188 uint32 delay_seconds = 0; | |
| 189 uint32 delay_fraction = 0; | |
| 190 base::TimeDelta delta = now - time_last_report_received_; | |
| 191 ConvertTimeToFractions(delta.InMicroseconds(), &delay_seconds, | |
| 192 &delay_fraction); | |
| 193 report_block.delay_since_last_sr = | |
| 194 ConvertToNtpDiff(delay_seconds, delay_fraction); | |
| 195 } else { | |
| 196 report_block.delay_since_last_sr = 0; | |
| 197 } | |
| 198 UpdateNextTimeToSendRtcp(); | |
| 199 } | |
| 200 rtcp_sender_->SendRtcpFromRtpReceiver(packet_type_flags, | |
| 201 &report_block, | |
| 202 &rrtr, | |
| 203 cast_message, | |
| 204 rtcp_events, | |
| 205 target_delay_); | |
| 206 } | |
| 207 | |
| 208 void Rtcp::SendRtcpFromRtpSender(base::TimeTicks current_time, | |
| 209 uint32 current_time_as_rtp_timestamp) { | |
| 210 DCHECK(transport_sender_); | |
| 211 uint32 packet_type_flags = transport::kRtcpSr; | |
| 212 uint32 current_ntp_seconds = 0; | |
| 213 uint32 current_ntp_fractions = 0; | |
| 214 ConvertTimeTicksToNtp(current_time, ¤t_ntp_seconds, | |
| 215 ¤t_ntp_fractions); | |
| 216 SaveLastSentNtpTime(current_time, current_ntp_seconds, | |
| 217 current_ntp_fractions); | |
| 218 | |
| 219 transport::RtcpDlrrReportBlock dlrr; | |
| 220 if (!time_last_report_received_.is_null()) { | |
| 221 packet_type_flags |= transport::kRtcpDlrr; | |
| 222 dlrr.last_rr = last_report_truncated_ntp_; | |
| 223 uint32 delay_seconds = 0; | |
| 224 uint32 delay_fraction = 0; | |
| 225 base::TimeDelta delta = current_time - time_last_report_received_; | |
| 226 ConvertTimeToFractions(delta.InMicroseconds(), &delay_seconds, | |
| 227 &delay_fraction); | |
| 228 | |
| 229 dlrr.delay_since_last_rr = ConvertToNtpDiff(delay_seconds, delay_fraction); | |
| 230 } | |
| 231 | |
| 232 transport_sender_->SendRtcpFromRtpSender( | |
| 233 packet_type_flags, current_ntp_seconds, current_ntp_fractions, | |
| 234 current_time_as_rtp_timestamp, dlrr, local_ssrc_, c_name_); | |
| 235 UpdateNextTimeToSendRtcp(); | |
| 236 } | |
| 237 | |
| 238 void Rtcp::OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction) { | |
| 239 last_report_truncated_ntp_ = ConvertToNtpDiff(ntp_seconds, ntp_fraction); | |
| 240 | |
| 241 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
| 242 time_last_report_received_ = now; | |
| 243 | |
| 244 // TODO(miu): This clock offset calculation does not account for packet | |
| 245 // transit time over the network. End2EndTest.EvilNetwork confirms that this | |
| 246 // contributes a very significant source of error here. Fix this along with | |
| 247 // the RTT clean-up. | |
| 248 const base::TimeDelta measured_offset = | |
| 249 now - ConvertNtpToTimeTicks(ntp_seconds, ntp_fraction); | |
| 250 local_clock_ahead_by_.Update(now, measured_offset); | |
| 251 if (measured_offset < local_clock_ahead_by_.Current()) { | |
| 252 // Logically, the minimum offset between the clocks has to be the correct | |
| 253 // one. For example, the time it took to transmit the current report may | |
| 254 // have been lower than usual, and so some of the error introduced by the | |
| 255 // transmission time can be eliminated. | |
| 256 local_clock_ahead_by_.Reset(now, measured_offset); | |
| 257 } | |
| 258 VLOG(1) << "Local clock is ahead of the remote clock by: " | |
| 259 << "measured=" << measured_offset.InMicroseconds() << " usec, " | |
| 260 << "filtered=" << local_clock_ahead_by_.Current().InMicroseconds() | |
| 261 << " usec."; | |
| 262 } | |
| 263 | |
| 264 void Rtcp::OnReceivedLipSyncInfo(uint32 rtp_timestamp, uint32 ntp_seconds, | |
| 265 uint32 ntp_fraction) { | |
| 266 if (ntp_seconds == 0) { | |
| 267 NOTREACHED(); | |
| 268 return; | |
| 269 } | |
| 270 lip_sync_rtp_timestamp_ = rtp_timestamp; | |
| 271 lip_sync_ntp_timestamp_ = | |
| 272 (static_cast<uint64>(ntp_seconds) << 32) | ntp_fraction; | |
| 273 } | |
| 274 | |
| 275 bool Rtcp::GetLatestLipSyncTimes(uint32* rtp_timestamp, | |
| 276 base::TimeTicks* reference_time) const { | |
| 277 if (!lip_sync_ntp_timestamp_) | |
| 278 return false; | |
| 279 | |
| 280 const base::TimeTicks local_reference_time = | |
| 281 ConvertNtpToTimeTicks(static_cast<uint32>(lip_sync_ntp_timestamp_ >> 32), | |
| 282 static_cast<uint32>(lip_sync_ntp_timestamp_)) + | |
| 283 local_clock_ahead_by_.Current(); | |
| 284 | |
| 285 // Sanity-check: Getting regular lip sync updates? | |
| 286 DCHECK((cast_environment_->Clock()->NowTicks() - local_reference_time) < | |
| 287 base::TimeDelta::FromMinutes(1)); | |
| 288 | |
| 289 *rtp_timestamp = lip_sync_rtp_timestamp_; | |
| 290 *reference_time = local_reference_time; | |
| 291 return true; | |
| 292 } | |
| 293 | |
| 294 void Rtcp::OnReceivedSendReportRequest() { | |
| 295 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
| 296 | |
| 297 // Trigger a new RTCP report at next timer. | |
| 298 next_time_to_send_rtcp_ = now; | |
| 299 } | |
| 300 | |
| 301 void Rtcp::SetCastReceiverEventHistorySize(size_t size) { | |
| 302 rtcp_receiver_->SetCastReceiverEventHistorySize(size); | |
| 303 } | |
| 304 | |
| 305 void Rtcp::SetTargetDelay(base::TimeDelta target_delay) { | |
| 306 DCHECK(target_delay < TimeDelta::FromMilliseconds(kMaxDelayMs)); | |
| 307 target_delay_ = target_delay; | |
| 308 } | |
| 309 | |
| 310 void Rtcp::OnReceivedDelaySinceLastReport(uint32 receivers_ssrc, | |
| 311 uint32 last_report, | |
| 312 uint32 delay_since_last_report) { | |
| 313 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); | |
| 314 if (it == last_reports_sent_map_.end()) { | |
| 315 return; // Feedback on another report. | |
| 316 } | |
| 317 | |
| 318 base::TimeDelta sender_delay = | |
| 319 cast_environment_->Clock()->NowTicks() - it->second; | |
| 320 UpdateRtt(sender_delay, ConvertFromNtpDiff(delay_since_last_report)); | |
| 321 } | |
| 322 | |
| 323 void Rtcp::SaveLastSentNtpTime(const base::TimeTicks& now, | |
| 324 uint32 last_ntp_seconds, | |
| 325 uint32 last_ntp_fraction) { | |
| 326 // Make sure |now| is always greater than the last element in | |
| 327 // |last_reports_sent_queue_|. | |
| 328 if (!last_reports_sent_queue_.empty()) | |
| 329 DCHECK(now >= last_reports_sent_queue_.back().second); | |
| 330 | |
| 331 uint32 last_report = ConvertToNtpDiff(last_ntp_seconds, last_ntp_fraction); | |
| 332 last_reports_sent_map_[last_report] = now; | |
| 333 last_reports_sent_queue_.push(std::make_pair(last_report, now)); | |
| 334 | |
| 335 base::TimeTicks timeout = now - TimeDelta::FromMilliseconds(kMaxRttMs); | |
| 336 | |
| 337 // Cleanup old statistics older than |timeout|. | |
| 338 while (!last_reports_sent_queue_.empty()) { | |
| 339 RtcpSendTimePair oldest_report = last_reports_sent_queue_.front(); | |
| 340 if (oldest_report.second < timeout) { | |
| 341 last_reports_sent_map_.erase(oldest_report.first); | |
| 342 last_reports_sent_queue_.pop(); | |
| 343 } else { | |
| 344 break; | |
| 345 } | |
| 346 } | |
| 347 } | |
| 348 | |
| 349 void Rtcp::UpdateRtt(const base::TimeDelta& sender_delay, | |
| 350 const base::TimeDelta& receiver_delay) { | |
| 351 base::TimeDelta rtt = sender_delay - receiver_delay; | |
| 352 // TODO(miu): Find out why this must be >= 1 ms, and remove the fudge if it's | |
| 353 // bogus. | |
| 354 rtt = std::max(rtt, base::TimeDelta::FromMilliseconds(1)); | |
| 355 rtt_ = rtt; | |
| 356 min_rtt_ = std::min(min_rtt_, rtt); | |
| 357 max_rtt_ = std::max(max_rtt_, rtt); | |
| 358 | |
| 359 // TODO(miu): Replace "average for all time" with an EWMA, or suitable | |
| 360 // "average over recent past" mechanism. | |
| 361 if (number_of_rtt_in_avg_ != 0) { | |
| 362 // Integer math equivalent of (ac/(ac+1.0))*avg_rtt_ + (1.0/(ac+1.0))*rtt). | |
| 363 // (TimeDelta only supports math with other TimeDeltas and int64s.) | |
| 364 avg_rtt_ = (avg_rtt_ * number_of_rtt_in_avg_ + rtt) / | |
| 365 (number_of_rtt_in_avg_ + 1); | |
| 366 } else { | |
| 367 avg_rtt_ = rtt; | |
| 368 } | |
| 369 number_of_rtt_in_avg_++; | |
| 370 } | |
| 371 | |
| 372 bool Rtcp::Rtt(base::TimeDelta* rtt, base::TimeDelta* avg_rtt, | |
| 373 base::TimeDelta* min_rtt, base::TimeDelta* max_rtt) const { | |
| 374 DCHECK(rtt) << "Invalid argument"; | |
| 375 DCHECK(avg_rtt) << "Invalid argument"; | |
| 376 DCHECK(min_rtt) << "Invalid argument"; | |
| 377 DCHECK(max_rtt) << "Invalid argument"; | |
| 378 | |
| 379 if (number_of_rtt_in_avg_ == 0) return false; | |
| 380 | |
| 381 *rtt = rtt_; | |
| 382 *avg_rtt = avg_rtt_; | |
| 383 *min_rtt = min_rtt_; | |
| 384 *max_rtt = max_rtt_; | |
| 385 return true; | |
| 386 } | |
| 387 | |
| 388 void Rtcp::UpdateNextTimeToSendRtcp() { | |
| 389 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
| 390 next_time_to_send_rtcp_ = now + rtcp_interval_; | |
| 391 } | |
| 392 | |
| 393 void Rtcp::OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log) { | |
| 394 // Add received log messages into our log system. | |
| 395 RtcpReceiverLogMessage::const_iterator it = receiver_log.begin(); | |
| 396 for (; it != receiver_log.end(); ++it) { | |
| 397 uint32 rtp_timestamp = it->rtp_timestamp_; | |
| 398 | |
| 399 RtcpReceiverEventLogMessages::const_iterator event_it = | |
| 400 it->event_log_messages_.begin(); | |
| 401 for (; event_it != it->event_log_messages_.end(); ++event_it) { | |
| 402 switch (event_it->type) { | |
| 403 case PACKET_RECEIVED: | |
| 404 cast_environment_->Logging()->InsertPacketEvent( | |
| 405 event_it->event_timestamp, event_it->type, | |
| 406 event_media_type_, rtp_timestamp, | |
| 407 kFrameIdUnknown, event_it->packet_id, 0, 0); | |
| 408 break; | |
| 409 case FRAME_ACK_SENT: | |
| 410 case FRAME_DECODED: | |
| 411 cast_environment_->Logging()->InsertFrameEvent( | |
| 412 event_it->event_timestamp, event_it->type, event_media_type_, | |
| 413 rtp_timestamp, kFrameIdUnknown); | |
| 414 break; | |
| 415 case FRAME_PLAYOUT: | |
| 416 cast_environment_->Logging()->InsertFrameEventWithDelay( | |
| 417 event_it->event_timestamp, event_it->type, event_media_type_, | |
| 418 rtp_timestamp, kFrameIdUnknown, event_it->delay_delta); | |
| 419 break; | |
| 420 default: | |
| 421 VLOG(2) << "Received log message via RTCP that we did not expect: " | |
| 422 << static_cast<int>(event_it->type); | |
| 423 break; | |
| 424 } | |
| 425 } | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 } // namespace cast | |
| 430 } // namespace media | |
| OLD | NEW |