Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(857)

Side by Side Diff: media/cast/net/rtcp/sender_rtcp_session.cc

Issue 1520613004: cast: Split Rtcp into two for sender and receiver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@timestamp
Patch Set: Add missing rtcp_session.h and a few comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <algorithm>
6 #include <limits>
7 #include <utility>
8
9 #include "base/big_endian.h"
10 #include "base/time/time.h"
11 #include "media/cast/constants.h"
12 #include "media/cast/net/pacing/paced_sender.h"
13 #include "media/cast/net/rtcp/rtcp_builder.h"
14 #include "media/cast/net/rtcp/rtcp_defines.h"
15 #include "media/cast/net/rtcp/rtcp_utility.h"
16 #include "media/cast/net/rtcp/sender_rtcp_session.h"
17
18 namespace media {
19 namespace cast {
20
21 namespace {
22
23 enum {
24 kStatsHistoryWindowMs = 10000, // 10 seconds.
25
26 // Reject packets that are 0.5 seconds older than
27 // the newest packet we've seen so far. This protects internal
28 // states from crazy routers. (Based on RRTR)
29 kOutOfOrderMaxAgeMs = 500,
miu 2015/12/12 00:53:25 Can you put a TODO (and crbug) here too? It feels
Irfan 2015/12/12 01:07:37 Done.
30 };
31
32 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
33 // fractions of a second where 0x80000000 is half a second.
34 uint32_t ConvertToNtpDiff(uint32_t delay_seconds, uint32_t delay_fraction) {
35 return ((delay_seconds & 0x0000FFFF) << 16) +
36 ((delay_fraction & 0xFFFF0000) >> 16);
37 }
38
39 // Parse a NTP diff value into a base::TimeDelta.
40 base::TimeDelta ConvertFromNtpDiff(uint32_t ntp_delay) {
41 int64_t delay_us =
42 (ntp_delay & 0x0000ffff) * base::Time::kMicrosecondsPerSecond;
43 delay_us >>= 16;
44 delay_us +=
45 ((ntp_delay & 0xffff0000) >> 16) * base::Time::kMicrosecondsPerSecond;
46 return base::TimeDelta::FromMicroseconds(delay_us);
47 }
48
49 // A receiver frame event is identified by frame RTP timestamp, event timestamp
50 // and event type.
51 // A receiver packet event is identified by all of the above plus packet id.
52 // The key format is as follows:
53 // First uint64_t:
54 // bits 0-11: zeroes (unused).
55 // bits 12-15: event type ID.
56 // bits 16-31: packet ID if packet event, 0 otherwise.
57 // bits 32-63: RTP timestamp.
58 // Second uint64_t:
59 // bits 0-63: event TimeTicks internal value.
60 std::pair<uint64_t, uint64_t> GetReceiverEventKey(
61 RtpTimeTicks frame_rtp_timestamp,
62 const base::TimeTicks& event_timestamp,
63 uint8_t event_type,
64 uint16_t packet_id_or_zero) {
65 uint64_t value1 = event_type;
66 value1 <<= 16;
67 value1 |= packet_id_or_zero;
68 value1 <<= 32;
69 value1 |= frame_rtp_timestamp.lower_32_bits();
70 return std::make_pair(
71 value1, static_cast<uint64_t>(event_timestamp.ToInternalValue()));
72 }
73
74 } // namespace
75
76 SenderRtcpSession::SenderRtcpSession(
77 const RtcpCastMessageCallback& cast_callback,
78 const RtcpRttCallback& rtt_callback,
79 const RtcpLogMessageCallback& log_callback,
80 base::TickClock* clock,
81 PacedPacketSender* packet_sender,
82 uint32_t local_ssrc,
83 uint32_t remote_ssrc)
84 : clock_(clock),
85 packet_sender_(packet_sender),
86 local_ssrc_(local_ssrc),
87 remote_ssrc_(remote_ssrc),
88 cast_callback_(cast_callback),
89 rtt_callback_(rtt_callback),
90 log_callback_(log_callback),
91 largest_seen_timestamp_(base::TimeTicks::FromInternalValue(
92 std::numeric_limits<int64_t>::min())),
93 parser_(local_ssrc, remote_ssrc),
94 ack_frame_id_wrap_helper_(kFirstFrameId - 1) {}
95
96 SenderRtcpSession::~SenderRtcpSession() {}
97
98 bool SenderRtcpSession::IncomingRtcpPacket(const uint8_t* data, size_t length) {
99 // Check if this is a valid RTCP packet.
100 if (!IsRtcpPacket(data, length)) {
101 VLOG(1) << "Rtcp@" << this << "::IncomingRtcpPacket() -- "
102 << "Received an invalid (non-RTCP?) packet.";
103 return false;
104 }
105
106 // Check if this packet is to us.
107 uint32_t ssrc_of_sender = GetSsrcOfSender(data, length);
108 if (ssrc_of_sender != remote_ssrc_) {
109 return false;
110 }
111
112 // Parse this packet.
113 base::BigEndianReader reader(reinterpret_cast<const char*>(data), length);
114 if (parser_.Parse(&reader)) {
115 if (parser_.has_receiver_reference_time_report()) {
116 base::TimeTicks t = ConvertNtpToTimeTicks(
117 parser_.receiver_reference_time_report().ntp_seconds,
118 parser_.receiver_reference_time_report().ntp_fraction);
119 if (t > largest_seen_timestamp_) {
120 largest_seen_timestamp_ = t;
121 } else if ((largest_seen_timestamp_ - t).InMilliseconds() >
122 kOutOfOrderMaxAgeMs) {
123 // Reject packet, it is too old.
124 VLOG(1) << "Rejecting RTCP packet as it is too old ("
125 << (largest_seen_timestamp_ - t).InMilliseconds() << " ms)";
126 return true;
127 }
128 }
129 if (parser_.has_receiver_log()) {
130 if (DedupeReceiverLog(parser_.mutable_receiver_log())) {
131 OnReceivedReceiverLog(parser_.receiver_log());
132 }
133 }
134 if (parser_.has_last_report()) {
135 OnReceivedDelaySinceLastReport(parser_.last_report(),
136 parser_.delay_since_last_report());
137 }
138 if (parser_.has_cast_message()) {
139 parser_.mutable_cast_message()->ack_frame_id =
140 ack_frame_id_wrap_helper_.MapTo32bitsFrameId(
141 parser_.mutable_cast_message()->ack_frame_id);
142 OnReceivedCastFeedback(parser_.cast_message());
143 }
144 }
145 return true;
146 }
147
148 void SenderRtcpSession::OnReceivedDelaySinceLastReport(
149 uint32_t last_report,
150 uint32_t delay_since_last_report) {
151 RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report);
152 if (it == last_reports_sent_map_.end()) {
153 return; // Feedback on another report.
154 }
155
156 const base::TimeDelta sender_delay = clock_->NowTicks() - it->second;
157 const base::TimeDelta receiver_delay =
158 ConvertFromNtpDiff(delay_since_last_report);
159 current_round_trip_time_ = sender_delay - receiver_delay;
160 // If the round trip time was computed as less than 1 ms, assume clock
161 // imprecision by one or both peers caused a bad value to be calculated.
162 // While plenty of networks do easily achieve less than 1 ms round trip time,
163 // such a level of precision cannot be measured with our approach; and 1 ms is
164 // good enough to represent "under 1 ms" for our use cases.
165 current_round_trip_time_ =
166 std::max(current_round_trip_time_, base::TimeDelta::FromMilliseconds(1));
167
168 if (!rtt_callback_.is_null())
169 rtt_callback_.Run(current_round_trip_time_);
170 }
171
172 void SenderRtcpSession::SaveLastSentNtpTime(const base::TimeTicks& now,
173 uint32_t last_ntp_seconds,
174 uint32_t last_ntp_fraction) {
175 // Make sure |now| is always greater than the last element in
176 // |last_reports_sent_queue_|.
177 if (!last_reports_sent_queue_.empty()) {
178 DCHECK(now >= last_reports_sent_queue_.back().second);
179 }
180
181 uint32_t last_report = ConvertToNtpDiff(last_ntp_seconds, last_ntp_fraction);
182 last_reports_sent_map_[last_report] = now;
183 last_reports_sent_queue_.push(std::make_pair(last_report, now));
184
185 const base::TimeTicks timeout =
186 now - base::TimeDelta::FromMilliseconds(kStatsHistoryWindowMs);
187
188 // Cleanup old statistics older than |timeout|.
189 while (!last_reports_sent_queue_.empty()) {
190 RtcpSendTimePair oldest_report = last_reports_sent_queue_.front();
191 if (oldest_report.second < timeout) {
192 last_reports_sent_map_.erase(oldest_report.first);
193 last_reports_sent_queue_.pop();
194 } else {
195 break;
196 }
197 }
198 }
199
200 bool SenderRtcpSession::DedupeReceiverLog(
201 RtcpReceiverLogMessage* receiver_log) {
202 RtcpReceiverLogMessage::iterator i = receiver_log->begin();
203 while (i != receiver_log->end()) {
204 RtcpReceiverEventLogMessages* messages = &i->event_log_messages_;
205 RtcpReceiverEventLogMessages::iterator j = messages->begin();
206 while (j != messages->end()) {
207 ReceiverEventKey key = GetReceiverEventKey(
208 i->rtp_timestamp_, j->event_timestamp, j->type, j->packet_id);
209 RtcpReceiverEventLogMessages::iterator tmp = j;
210 ++j;
211 if (receiver_event_key_set_.insert(key).second) {
212 receiver_event_key_queue_.push(key);
213 if (receiver_event_key_queue_.size() > kReceiverRtcpEventHistorySize) {
214 receiver_event_key_set_.erase(receiver_event_key_queue_.front());
215 receiver_event_key_queue_.pop();
216 }
217 } else {
218 messages->erase(tmp);
219 }
220 }
221
222 RtcpReceiverLogMessage::iterator tmp = i;
223 ++i;
224 if (messages->empty()) {
225 receiver_log->erase(tmp);
226 }
227 }
228 return !receiver_log->empty();
229 }
230
231 void SenderRtcpSession::SendRtcpReport(
232 base::TimeTicks current_time,
233 RtpTimeTicks current_time_as_rtp_timestamp,
234 uint32_t send_packet_count,
235 size_t send_octet_count) {
236 uint32_t current_ntp_seconds = 0;
237 uint32_t current_ntp_fractions = 0;
238 ConvertTimeTicksToNtp(current_time, &current_ntp_seconds,
239 &current_ntp_fractions);
240 SaveLastSentNtpTime(current_time, current_ntp_seconds, current_ntp_fractions);
241
242 RtcpSenderInfo sender_info;
243 sender_info.ntp_seconds = current_ntp_seconds;
244 sender_info.ntp_fraction = current_ntp_fractions;
245 sender_info.rtp_timestamp = current_time_as_rtp_timestamp;
246 sender_info.send_packet_count = send_packet_count;
247 sender_info.send_octet_count = send_octet_count;
248
249 RtcpBuilder rtcp_builder(local_ssrc_);
250 packet_sender_->SendRtcpPacket(local_ssrc_,
251 rtcp_builder.BuildRtcpFromSender(sender_info));
252 }
253
254 void SenderRtcpSession::OnReceivedCastFeedback(
255 const RtcpCastMessage& cast_message) {
256 if (cast_callback_.is_null())
257 return;
258 cast_callback_.Run(cast_message);
259 }
260
261 void SenderRtcpSession::OnReceivedReceiverLog(
262 const RtcpReceiverLogMessage& receiver_log) {
263 if (log_callback_.is_null())
264 return;
265 log_callback_.Run(receiver_log);
266 }
267
268 } // namespace cast
269 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698