OLD | NEW |
1 // Copyright 2015 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 <algorithm> | 5 #include <algorithm> |
6 #include <limits> | 6 #include <limits> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/big_endian.h" | 9 #include "base/big_endian.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 value1 <<= 16; | 61 value1 <<= 16; |
62 value1 |= packet_id_or_zero; | 62 value1 |= packet_id_or_zero; |
63 value1 <<= 32; | 63 value1 <<= 32; |
64 value1 |= frame_rtp_timestamp.lower_32_bits(); | 64 value1 |= frame_rtp_timestamp.lower_32_bits(); |
65 return std::make_pair( | 65 return std::make_pair( |
66 value1, static_cast<uint64_t>(event_timestamp.ToInternalValue())); | 66 value1, static_cast<uint64_t>(event_timestamp.ToInternalValue())); |
67 } | 67 } |
68 | 68 |
69 } // namespace | 69 } // namespace |
70 | 70 |
71 SenderRtcpSession::SenderRtcpSession( | 71 SenderRtcpSession::SenderRtcpSession(base::TickClock* clock, |
72 const RtcpCastMessageCallback& cast_callback, | 72 PacedPacketSender* packet_sender, |
73 const RtcpRttCallback& rtt_callback, | 73 RtcpObserver* observer, |
74 const RtcpLogMessageCallback& log_callback, | 74 uint32_t local_ssrc, |
75 const RtcpPliCallback pli_callback, | 75 uint32_t remote_ssrc) |
76 base::TickClock* clock, | |
77 PacedPacketSender* packet_sender, | |
78 uint32_t local_ssrc, | |
79 uint32_t remote_ssrc) | |
80 : clock_(clock), | 76 : clock_(clock), |
81 packet_sender_(packet_sender), | 77 packet_sender_(packet_sender), |
82 local_ssrc_(local_ssrc), | 78 local_ssrc_(local_ssrc), |
83 remote_ssrc_(remote_ssrc), | 79 remote_ssrc_(remote_ssrc), |
84 cast_callback_(cast_callback), | 80 rtcp_observer_(observer), |
85 rtt_callback_(rtt_callback), | |
86 log_callback_(log_callback), | |
87 pli_callback_(pli_callback), | |
88 largest_seen_timestamp_(base::TimeTicks::FromInternalValue( | 81 largest_seen_timestamp_(base::TimeTicks::FromInternalValue( |
89 std::numeric_limits<int64_t>::min())), | 82 std::numeric_limits<int64_t>::min())), |
90 parser_(local_ssrc, remote_ssrc) {} | 83 parser_(local_ssrc, remote_ssrc) {} |
91 | 84 |
92 SenderRtcpSession::~SenderRtcpSession() {} | 85 SenderRtcpSession::~SenderRtcpSession() {} |
93 | 86 |
94 void SenderRtcpSession::WillSendFrame(FrameId frame_id) { | 87 void SenderRtcpSession::WillSendFrame(FrameId frame_id) { |
95 if (parser_.max_valid_frame_id().is_null() || | 88 if (parser_.max_valid_frame_id().is_null() || |
96 frame_id > parser_.max_valid_frame_id()) { | 89 frame_id > parser_.max_valid_frame_id()) { |
97 parser_.SetMaxValidFrameId(frame_id); | 90 parser_.SetMaxValidFrameId(frame_id); |
(...skipping 10 matching lines...) Expand all Loading... |
108 | 101 |
109 // Check if this packet is to us. | 102 // Check if this packet is to us. |
110 uint32_t ssrc_of_sender = GetSsrcOfSender(data, length); | 103 uint32_t ssrc_of_sender = GetSsrcOfSender(data, length); |
111 if (ssrc_of_sender != remote_ssrc_) { | 104 if (ssrc_of_sender != remote_ssrc_) { |
112 return false; | 105 return false; |
113 } | 106 } |
114 | 107 |
115 // Parse this packet. | 108 // Parse this packet. |
116 base::BigEndianReader reader(reinterpret_cast<const char*>(data), length); | 109 base::BigEndianReader reader(reinterpret_cast<const char*>(data), length); |
117 if (parser_.Parse(&reader)) { | 110 if (parser_.Parse(&reader)) { |
118 if (parser_.has_picture_loss_indicator()) { | 111 if (parser_.has_picture_loss_indicator()) |
119 if (!pli_callback_.is_null()) | 112 rtcp_observer_->OnReceivedPli(); |
120 pli_callback_.Run(); | |
121 } | |
122 if (parser_.has_receiver_reference_time_report()) { | 113 if (parser_.has_receiver_reference_time_report()) { |
123 base::TimeTicks t = ConvertNtpToTimeTicks( | 114 base::TimeTicks t = ConvertNtpToTimeTicks( |
124 parser_.receiver_reference_time_report().ntp_seconds, | 115 parser_.receiver_reference_time_report().ntp_seconds, |
125 parser_.receiver_reference_time_report().ntp_fraction); | 116 parser_.receiver_reference_time_report().ntp_fraction); |
126 if (t > largest_seen_timestamp_) { | 117 if (t > largest_seen_timestamp_) { |
127 largest_seen_timestamp_ = t; | 118 largest_seen_timestamp_ = t; |
128 } else if ((largest_seen_timestamp_ - t).InMilliseconds() > | 119 } else if ((largest_seen_timestamp_ - t).InMilliseconds() > |
129 kOutOfOrderMaxAgeMs) { | 120 kOutOfOrderMaxAgeMs) { |
130 // Reject packet, it is too old. | 121 // Reject packet, it is too old. |
131 VLOG(1) << "Rejecting RTCP packet as it is too old (" | 122 VLOG(1) << "Rejecting RTCP packet as it is too old (" |
132 << (largest_seen_timestamp_ - t).InMilliseconds() << " ms)"; | 123 << (largest_seen_timestamp_ - t).InMilliseconds() << " ms)"; |
133 return true; | 124 return true; |
134 } | 125 } |
135 } | 126 } |
136 if (parser_.has_receiver_log()) { | 127 if (parser_.has_receiver_log()) { |
137 if (DedupeReceiverLog(parser_.mutable_receiver_log())) { | 128 if (DedupeReceiverLog(parser_.mutable_receiver_log())) { |
138 OnReceivedReceiverLog(parser_.receiver_log()); | 129 rtcp_observer_->OnReceivedReceiverLog(parser_.receiver_log()); |
139 } | 130 } |
140 } | 131 } |
141 if (parser_.has_last_report()) { | 132 if (parser_.has_last_report()) { |
142 OnReceivedDelaySinceLastReport(parser_.last_report(), | 133 OnReceivedDelaySinceLastReport(parser_.last_report(), |
143 parser_.delay_since_last_report()); | 134 parser_.delay_since_last_report()); |
144 } | 135 } |
145 if (parser_.has_cast_message()) { | 136 if (parser_.has_cast_message()) { |
146 OnReceivedCastFeedback(parser_.cast_message()); | 137 rtcp_observer_->OnReceivedCastMessage(parser_.cast_message()); |
147 } | 138 } |
148 } | 139 } |
149 return true; | 140 return true; |
150 } | 141 } |
151 | 142 |
152 void SenderRtcpSession::OnReceivedDelaySinceLastReport( | 143 void SenderRtcpSession::OnReceivedDelaySinceLastReport( |
153 uint32_t last_report, | 144 uint32_t last_report, |
154 uint32_t delay_since_last_report) { | 145 uint32_t delay_since_last_report) { |
155 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); | 146 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report); |
156 if (it == last_reports_sent_map_.end()) { | 147 if (it == last_reports_sent_map_.end()) { |
157 return; // Feedback on another report. | 148 return; // Feedback on another report. |
158 } | 149 } |
159 | 150 |
160 const base::TimeDelta sender_delay = clock_->NowTicks() - it->second; | 151 const base::TimeDelta sender_delay = clock_->NowTicks() - it->second; |
161 const base::TimeDelta receiver_delay = | 152 const base::TimeDelta receiver_delay = |
162 ConvertFromNtpDiff(delay_since_last_report); | 153 ConvertFromNtpDiff(delay_since_last_report); |
163 current_round_trip_time_ = sender_delay - receiver_delay; | 154 current_round_trip_time_ = sender_delay - receiver_delay; |
164 // If the round trip time was computed as less than 1 ms, assume clock | 155 // 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. | 156 // 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, | 157 // 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 | 158 // 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. | 159 // good enough to represent "under 1 ms" for our use cases. |
169 current_round_trip_time_ = | 160 current_round_trip_time_ = |
170 std::max(current_round_trip_time_, base::TimeDelta::FromMilliseconds(1)); | 161 std::max(current_round_trip_time_, base::TimeDelta::FromMilliseconds(1)); |
171 | 162 |
172 if (!rtt_callback_.is_null()) | 163 rtcp_observer_->OnReceivedRtt(current_round_trip_time_); |
173 rtt_callback_.Run(current_round_trip_time_); | |
174 } | 164 } |
175 | 165 |
176 void SenderRtcpSession::SaveLastSentNtpTime(const base::TimeTicks& now, | 166 void SenderRtcpSession::SaveLastSentNtpTime(const base::TimeTicks& now, |
177 uint32_t last_ntp_seconds, | 167 uint32_t last_ntp_seconds, |
178 uint32_t last_ntp_fraction) { | 168 uint32_t last_ntp_fraction) { |
179 // Make sure |now| is always greater than the last element in | 169 // Make sure |now| is always greater than the last element in |
180 // |last_reports_sent_queue_|. | 170 // |last_reports_sent_queue_|. |
181 if (!last_reports_sent_queue_.empty()) { | 171 if (!last_reports_sent_queue_.empty()) { |
182 DCHECK(now >= last_reports_sent_queue_.back().second); | 172 DCHECK(now >= last_reports_sent_queue_.back().second); |
183 } | 173 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 sender_info.ntp_fraction = current_ntp_fractions; | 238 sender_info.ntp_fraction = current_ntp_fractions; |
249 sender_info.rtp_timestamp = current_time_as_rtp_timestamp; | 239 sender_info.rtp_timestamp = current_time_as_rtp_timestamp; |
250 sender_info.send_packet_count = send_packet_count; | 240 sender_info.send_packet_count = send_packet_count; |
251 sender_info.send_octet_count = send_octet_count; | 241 sender_info.send_octet_count = send_octet_count; |
252 | 242 |
253 RtcpBuilder rtcp_builder(local_ssrc_); | 243 RtcpBuilder rtcp_builder(local_ssrc_); |
254 packet_sender_->SendRtcpPacket(local_ssrc_, | 244 packet_sender_->SendRtcpPacket(local_ssrc_, |
255 rtcp_builder.BuildRtcpFromSender(sender_info)); | 245 rtcp_builder.BuildRtcpFromSender(sender_info)); |
256 } | 246 } |
257 | 247 |
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 | 248 } // namespace cast |
273 } // namespace media | 249 } // namespace media |
OLD | NEW |