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