OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cast/rtcp/rtcp.h" | 5 #include "media/cast/rtcp/rtcp.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | |
8 #include "base/rand_util.h" | 7 #include "base/rand_util.h" |
9 #include "media/cast/cast_config.h" | 8 #include "media/cast/cast_config.h" |
10 #include "media/cast/cast_defines.h" | 9 #include "media/cast/cast_defines.h" |
| 10 #include "media/cast/cast_environment.h" |
11 #include "media/cast/rtcp/rtcp_defines.h" | 11 #include "media/cast/rtcp/rtcp_defines.h" |
12 #include "media/cast/rtcp/rtcp_receiver.h" | 12 #include "media/cast/rtcp/rtcp_receiver.h" |
13 #include "media/cast/rtcp/rtcp_sender.h" | 13 #include "media/cast/rtcp/rtcp_sender.h" |
14 #include "media/cast/rtcp/rtcp_utility.h" | 14 #include "media/cast/rtcp/rtcp_utility.h" |
15 #include "net/base/big_endian.h" | 15 #include "net/base/big_endian.h" |
16 | 16 |
17 namespace media { | 17 namespace media { |
18 namespace cast { | 18 namespace cast { |
19 | 19 |
20 static const int kMaxRttMs = 10000; // 10 seconds. | 20 static const int kMaxRttMs = 10000; // 10 seconds. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 const RtcpSenderLogMessage& sender_log) OVERRIDE { | 93 const RtcpSenderLogMessage& sender_log) OVERRIDE { |
94 // TODO(pwestin): Implement. | 94 // TODO(pwestin): Implement. |
95 // Add received log messages into our log system. | 95 // Add received log messages into our log system. |
96 NOTIMPLEMENTED(); | 96 NOTIMPLEMENTED(); |
97 } | 97 } |
98 | 98 |
99 private: | 99 private: |
100 Rtcp* rtcp_; | 100 Rtcp* rtcp_; |
101 }; | 101 }; |
102 | 102 |
103 Rtcp::Rtcp(base::TickClock* clock, | 103 Rtcp::Rtcp(scoped_refptr<CastEnvironment> cast_environment, |
104 RtcpSenderFeedback* sender_feedback, | 104 RtcpSenderFeedback* sender_feedback, |
105 PacedPacketSender* paced_packet_sender, | 105 PacedPacketSender* paced_packet_sender, |
106 RtpSenderStatistics* rtp_sender_statistics, | 106 RtpSenderStatistics* rtp_sender_statistics, |
107 RtpReceiverStatistics* rtp_receiver_statistics, | 107 RtpReceiverStatistics* rtp_receiver_statistics, |
108 RtcpMode rtcp_mode, | 108 RtcpMode rtcp_mode, |
109 const base::TimeDelta& rtcp_interval, | 109 const base::TimeDelta& rtcp_interval, |
110 uint32 local_ssrc, | 110 uint32 local_ssrc, |
111 uint32 remote_ssrc, | 111 uint32 remote_ssrc, |
112 const std::string& c_name) | 112 const std::string& c_name) |
113 : rtcp_interval_(rtcp_interval), | 113 : rtcp_interval_(rtcp_interval), |
114 rtcp_mode_(rtcp_mode), | 114 rtcp_mode_(rtcp_mode), |
115 local_ssrc_(local_ssrc), | 115 local_ssrc_(local_ssrc), |
116 remote_ssrc_(remote_ssrc), | 116 remote_ssrc_(remote_ssrc), |
117 rtp_sender_statistics_(rtp_sender_statistics), | 117 rtp_sender_statistics_(rtp_sender_statistics), |
118 rtp_receiver_statistics_(rtp_receiver_statistics), | 118 rtp_receiver_statistics_(rtp_receiver_statistics), |
119 receiver_feedback_(new LocalRtcpReceiverFeedback(this)), | 119 receiver_feedback_(new LocalRtcpReceiverFeedback(this)), |
120 rtt_feedback_(new LocalRtcpRttFeedback(this)), | 120 rtt_feedback_(new LocalRtcpRttFeedback(this)), |
121 rtcp_sender_(new RtcpSender(paced_packet_sender, local_ssrc, c_name)), | 121 rtcp_sender_(new RtcpSender(cast_environment, paced_packet_sender, |
| 122 local_ssrc, c_name)), |
122 last_report_received_(0), | 123 last_report_received_(0), |
123 last_received_rtp_timestamp_(0), | 124 last_received_rtp_timestamp_(0), |
124 last_received_ntp_seconds_(0), | 125 last_received_ntp_seconds_(0), |
125 last_received_ntp_fraction_(0), | 126 last_received_ntp_fraction_(0), |
126 min_rtt_(base::TimeDelta::FromMilliseconds(kMaxRttMs)), | 127 min_rtt_(base::TimeDelta::FromMilliseconds(kMaxRttMs)), |
127 number_of_rtt_in_avg_(0), | 128 number_of_rtt_in_avg_(0), |
128 clock_(clock) { | 129 cast_environment_(cast_environment) { |
129 rtcp_receiver_.reset(new RtcpReceiver(sender_feedback, | 130 rtcp_receiver_.reset(new RtcpReceiver(cast_environment, |
| 131 sender_feedback, |
130 receiver_feedback_.get(), | 132 receiver_feedback_.get(), |
131 rtt_feedback_.get(), | 133 rtt_feedback_.get(), |
132 local_ssrc)); | 134 local_ssrc)); |
133 rtcp_receiver_->SetRemoteSSRC(remote_ssrc); | 135 rtcp_receiver_->SetRemoteSSRC(remote_ssrc); |
134 } | 136 } |
135 | 137 |
136 Rtcp::~Rtcp() {} | 138 Rtcp::~Rtcp() {} |
137 | 139 |
138 // static | 140 // static |
139 bool Rtcp::IsRtcpPacket(const uint8* packet, size_t length) { | 141 bool Rtcp::IsRtcpPacket(const uint8* packet, size_t length) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 DLOG(ERROR) << "Received invalid RTCP packet"; | 173 DLOG(ERROR) << "Received invalid RTCP packet"; |
172 return; | 174 return; |
173 } | 175 } |
174 rtcp_receiver_->IncomingRtcpPacket(&rtcp_parser); | 176 rtcp_receiver_->IncomingRtcpPacket(&rtcp_parser); |
175 } | 177 } |
176 | 178 |
177 void Rtcp::SendRtcpFromRtpReceiver(const RtcpCastMessage* cast_message, | 179 void Rtcp::SendRtcpFromRtpReceiver(const RtcpCastMessage* cast_message, |
178 const RtcpReceiverLogMessage* receiver_log) { | 180 const RtcpReceiverLogMessage* receiver_log) { |
179 uint32 packet_type_flags = 0; | 181 uint32 packet_type_flags = 0; |
180 | 182 |
181 base::TimeTicks now = clock_->NowTicks(); | 183 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
182 RtcpReportBlock report_block; | 184 RtcpReportBlock report_block; |
183 RtcpReceiverReferenceTimeReport rrtr; | 185 RtcpReceiverReferenceTimeReport rrtr; |
184 | 186 |
185 if (cast_message) { | 187 if (cast_message) { |
186 packet_type_flags |= RtcpSender::kRtcpCast; | 188 packet_type_flags |= RtcpSender::kRtcpCast; |
187 } | 189 } |
188 if (receiver_log) { | 190 if (receiver_log) { |
189 packet_type_flags |= RtcpSender::kRtcpReceiverLog; | 191 packet_type_flags |= RtcpSender::kRtcpReceiverLog; |
190 } | 192 } |
191 if (rtcp_mode_ == kRtcpCompound || now >= next_time_to_send_rtcp_) { | 193 if (rtcp_mode_ == kRtcpCompound || now >= next_time_to_send_rtcp_) { |
192 packet_type_flags |= RtcpSender::kRtcpRr; | 194 packet_type_flags |= RtcpSender::kRtcpRr; |
193 | 195 |
194 report_block.remote_ssrc = 0; // Not needed to set send side. | 196 report_block.remote_ssrc = 0; // Not needed to set send side. |
195 report_block.media_ssrc = remote_ssrc_; // SSRC of the RTP packet sender. | 197 report_block.media_ssrc = remote_ssrc_; // SSRC of the RTP packet sender. |
196 if (rtp_receiver_statistics_) { | 198 if (rtp_receiver_statistics_) { |
197 rtp_receiver_statistics_->GetStatistics( | 199 rtp_receiver_statistics_->GetStatistics( |
198 &report_block.fraction_lost, | 200 &report_block.fraction_lost, |
199 &report_block.cumulative_lost, | 201 &report_block.cumulative_lost, |
200 &report_block.extended_high_sequence_number, | 202 &report_block.extended_high_sequence_number, |
201 &report_block.jitter); | 203 &report_block.jitter); |
| 204 cast_environment_->Logging()->InsertGenericEvent(kJitterMs, |
| 205 report_block.jitter); |
| 206 cast_environment_->Logging()->InsertGenericEvent(kPacketLoss, |
| 207 report_block.fraction_lost); |
| 208 |
202 } | 209 } |
203 | 210 |
204 report_block.last_sr = last_report_received_; | 211 report_block.last_sr = last_report_received_; |
205 if (!time_last_report_received_.is_null()) { | 212 if (!time_last_report_received_.is_null()) { |
206 uint32 delay_seconds = 0; | 213 uint32 delay_seconds = 0; |
207 uint32 delay_fraction = 0; | 214 uint32 delay_fraction = 0; |
208 base::TimeDelta delta = now - time_last_report_received_; | 215 base::TimeDelta delta = now - time_last_report_received_; |
209 ConvertTimeToFractions(delta.InMicroseconds(), | 216 ConvertTimeToFractions(delta.InMicroseconds(), |
210 &delay_seconds, | 217 &delay_seconds, |
211 &delay_fraction); | 218 &delay_fraction); |
(...skipping 11 matching lines...) Expand all Loading... |
223 rtcp_sender_->SendRtcpFromRtpReceiver(packet_type_flags, | 230 rtcp_sender_->SendRtcpFromRtpReceiver(packet_type_flags, |
224 &report_block, | 231 &report_block, |
225 &rrtr, | 232 &rrtr, |
226 cast_message, | 233 cast_message, |
227 receiver_log); | 234 receiver_log); |
228 } | 235 } |
229 | 236 |
230 void Rtcp::SendRtcpFromRtpSender( | 237 void Rtcp::SendRtcpFromRtpSender( |
231 const RtcpSenderLogMessage* sender_log_message) { | 238 const RtcpSenderLogMessage* sender_log_message) { |
232 uint32 packet_type_flags = RtcpSender::kRtcpSr; | 239 uint32 packet_type_flags = RtcpSender::kRtcpSr; |
233 base::TimeTicks now = clock_->NowTicks(); | 240 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
234 | 241 |
235 RtcpSenderInfo sender_info; | 242 RtcpSenderInfo sender_info; |
236 RtcpDlrrReportBlock dlrr; | 243 RtcpDlrrReportBlock dlrr; |
237 | 244 |
238 if (sender_log_message) packet_type_flags |= RtcpSender::kRtcpSenderLog; | 245 if (sender_log_message) packet_type_flags |= RtcpSender::kRtcpSenderLog; |
239 | 246 |
240 if (rtp_sender_statistics_) { | 247 if (rtp_sender_statistics_) { |
241 rtp_sender_statistics_->GetStatistics(now, &sender_info); | 248 rtp_sender_statistics_->GetStatistics(now, &sender_info); |
242 } else { | 249 } else { |
243 memset(&sender_info, 0, sizeof(sender_info)); | 250 memset(&sender_info, 0, sizeof(sender_info)); |
(...skipping 16 matching lines...) Expand all Loading... |
260 rtcp_sender_->SendRtcpFromRtpSender(packet_type_flags, | 267 rtcp_sender_->SendRtcpFromRtpSender(packet_type_flags, |
261 &sender_info, | 268 &sender_info, |
262 &dlrr, | 269 &dlrr, |
263 sender_log_message); | 270 sender_log_message); |
264 UpdateNextTimeToSendRtcp(); | 271 UpdateNextTimeToSendRtcp(); |
265 } | 272 } |
266 | 273 |
267 void Rtcp::OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction) { | 274 void Rtcp::OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction) { |
268 last_report_received_ = (ntp_seconds << 16) + (ntp_fraction >> 16); | 275 last_report_received_ = (ntp_seconds << 16) + (ntp_fraction >> 16); |
269 | 276 |
270 base::TimeTicks now = clock_->NowTicks(); | 277 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
271 time_last_report_received_ = now; | 278 time_last_report_received_ = now; |
272 } | 279 } |
273 | 280 |
274 void Rtcp::OnReceivedLipSyncInfo(uint32 rtp_timestamp, | 281 void Rtcp::OnReceivedLipSyncInfo(uint32 rtp_timestamp, |
275 uint32 ntp_seconds, | 282 uint32 ntp_seconds, |
276 uint32 ntp_fraction) { | 283 uint32 ntp_fraction) { |
277 last_received_rtp_timestamp_ = rtp_timestamp; | 284 last_received_rtp_timestamp_ = rtp_timestamp; |
278 last_received_ntp_seconds_ = ntp_seconds; | 285 last_received_ntp_seconds_ = ntp_seconds; |
279 last_received_ntp_fraction_ = ntp_fraction; | 286 last_received_ntp_fraction_ = ntp_fraction; |
280 } | 287 } |
281 | 288 |
282 void Rtcp::OnReceivedSendReportRequest() { | 289 void Rtcp::OnReceivedSendReportRequest() { |
283 base::TimeTicks now = clock_->NowTicks(); | 290 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
284 | 291 |
285 // Trigger a new RTCP report at next timer. | 292 // Trigger a new RTCP report at next timer. |
286 next_time_to_send_rtcp_ = now; | 293 next_time_to_send_rtcp_ = now; |
287 } | 294 } |
288 | 295 |
289 bool Rtcp::RtpTimestampInSenderTime(int frequency, uint32 rtp_timestamp, | 296 bool Rtcp::RtpTimestampInSenderTime(int frequency, uint32 rtp_timestamp, |
290 base::TimeTicks* rtp_timestamp_in_ticks) const { | 297 base::TimeTicks* rtp_timestamp_in_ticks) const { |
291 if (last_received_ntp_seconds_ == 0) return false; | 298 if (last_received_ntp_seconds_ == 0) return false; |
292 | 299 |
293 int wrap = CheckForWrapAround(rtp_timestamp, last_received_rtp_timestamp_); | 300 int wrap = CheckForWrapAround(rtp_timestamp, last_received_rtp_timestamp_); |
(...skipping 23 matching lines...) Expand all Loading... |
317 return true; | 324 return true; |
318 } | 325 } |
319 | 326 |
320 void Rtcp::OnReceivedDelaySinceLastReport(uint32 receivers_ssrc, | 327 void Rtcp::OnReceivedDelaySinceLastReport(uint32 receivers_ssrc, |
321 uint32 last_report, | 328 uint32 last_report, |
322 uint32 delay_since_last_report) { | 329 uint32 delay_since_last_report) { |
323 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); | 330 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); |
324 if (it == last_reports_sent_map_.end()) { | 331 if (it == last_reports_sent_map_.end()) { |
325 return; // Feedback on another report. | 332 return; // Feedback on another report. |
326 } | 333 } |
327 base::TimeDelta sender_delay = clock_->NowTicks() - it->second; | 334 |
| 335 base::TimeDelta sender_delay = cast_environment_->Clock()->NowTicks() |
| 336 - it->second; |
328 UpdateRtt(sender_delay, ConvertFromNtpDiff(delay_since_last_report)); | 337 UpdateRtt(sender_delay, ConvertFromNtpDiff(delay_since_last_report)); |
329 } | 338 } |
330 | 339 |
331 void Rtcp::SaveLastSentNtpTime(const base::TimeTicks& now, | 340 void Rtcp::SaveLastSentNtpTime(const base::TimeTicks& now, |
332 uint32 last_ntp_seconds, | 341 uint32 last_ntp_seconds, |
333 uint32 last_ntp_fraction) { | 342 uint32 last_ntp_fraction) { |
334 // Make sure |now| is always greater than the last element in | 343 // Make sure |now| is always greater than the last element in |
335 // |last_reports_sent_queue_|. | 344 // |last_reports_sent_queue_|. |
336 if (!last_reports_sent_queue_.empty()) { | 345 if (!last_reports_sent_queue_.empty()) { |
337 DCHECK(now >= last_reports_sent_queue_.back().second); | 346 DCHECK(now >= last_reports_sent_queue_.back().second); |
(...skipping 26 matching lines...) Expand all Loading... |
364 max_rtt_ = std::max(max_rtt_, rtt); | 373 max_rtt_ = std::max(max_rtt_, rtt); |
365 | 374 |
366 if (number_of_rtt_in_avg_ != 0) { | 375 if (number_of_rtt_in_avg_ != 0) { |
367 float ac = static_cast<float>(number_of_rtt_in_avg_); | 376 float ac = static_cast<float>(number_of_rtt_in_avg_); |
368 avg_rtt_ms_= ((ac / (ac + 1.0)) * avg_rtt_ms_) + | 377 avg_rtt_ms_= ((ac / (ac + 1.0)) * avg_rtt_ms_) + |
369 ((1.0 / (ac + 1.0)) * rtt.InMilliseconds()); | 378 ((1.0 / (ac + 1.0)) * rtt.InMilliseconds()); |
370 } else { | 379 } else { |
371 avg_rtt_ms_ = rtt.InMilliseconds(); | 380 avg_rtt_ms_ = rtt.InMilliseconds(); |
372 } | 381 } |
373 number_of_rtt_in_avg_++; | 382 number_of_rtt_in_avg_++; |
374 TRACE_COUNTER_ID1("cast_rtcp", "RTT", local_ssrc_, rtt.InMilliseconds()); | |
375 } | 383 } |
376 | 384 |
377 bool Rtcp::Rtt(base::TimeDelta* rtt, | 385 bool Rtcp::Rtt(base::TimeDelta* rtt, |
378 base::TimeDelta* avg_rtt, | 386 base::TimeDelta* avg_rtt, |
379 base::TimeDelta* min_rtt, | 387 base::TimeDelta* min_rtt, |
380 base::TimeDelta* max_rtt) const { | 388 base::TimeDelta* max_rtt) const { |
381 DCHECK(rtt) << "Invalid argument"; | 389 DCHECK(rtt) << "Invalid argument"; |
382 DCHECK(avg_rtt) << "Invalid argument"; | 390 DCHECK(avg_rtt) << "Invalid argument"; |
383 DCHECK(min_rtt) << "Invalid argument"; | 391 DCHECK(min_rtt) << "Invalid argument"; |
384 DCHECK(max_rtt) << "Invalid argument"; | 392 DCHECK(max_rtt) << "Invalid argument"; |
385 | 393 |
386 if (number_of_rtt_in_avg_ == 0) return false; | 394 if (number_of_rtt_in_avg_ == 0) return false; |
| 395 cast_environment_->Logging()->InsertGenericEvent(kRttMs, |
| 396 rtt->InMilliseconds()); |
387 | 397 |
388 *rtt = rtt_; | 398 *rtt = rtt_; |
389 *avg_rtt = base::TimeDelta::FromMilliseconds(avg_rtt_ms_); | 399 *avg_rtt = base::TimeDelta::FromMilliseconds(avg_rtt_ms_); |
390 *min_rtt = min_rtt_; | 400 *min_rtt = min_rtt_; |
391 *max_rtt = max_rtt_; | 401 *max_rtt = max_rtt_; |
392 return true; | 402 return true; |
393 } | 403 } |
394 | 404 |
395 int Rtcp::CheckForWrapAround(uint32 new_timestamp, | 405 int Rtcp::CheckForWrapAround(uint32 new_timestamp, |
396 uint32 old_timestamp) const { | 406 uint32 old_timestamp) const { |
(...skipping 10 matching lines...) Expand all Loading... |
407 return -1; | 417 return -1; |
408 } | 418 } |
409 return 0; | 419 return 0; |
410 } | 420 } |
411 | 421 |
412 void Rtcp::UpdateNextTimeToSendRtcp() { | 422 void Rtcp::UpdateNextTimeToSendRtcp() { |
413 int random = base::RandInt(0, 999); | 423 int random = base::RandInt(0, 999); |
414 base::TimeDelta time_to_next = (rtcp_interval_ / 2) + | 424 base::TimeDelta time_to_next = (rtcp_interval_ / 2) + |
415 (rtcp_interval_ * random / 1000); | 425 (rtcp_interval_ * random / 1000); |
416 | 426 |
417 base::TimeTicks now = clock_->NowTicks(); | 427 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
418 next_time_to_send_rtcp_ = now + time_to_next; | 428 next_time_to_send_rtcp_ = now + time_to_next; |
419 } | 429 } |
420 | 430 |
421 } // namespace cast | 431 } // namespace cast |
422 } // namespace media | 432 } // namespace media |
OLD | NEW |