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 // This class maintains a bi-directional RTCP connection with a remote | 5 #ifndef MEDIA_CAST_NET_RTCP_SENDER_RTCP_SESSION_H_ |
6 // peer. | 6 #define MEDIA_CAST_NET_RTCP_SENDER_RTCP_SESSION_H_ |
7 | |
8 #ifndef MEDIA_CAST_RTCP_RTCP_H_ | |
9 #define MEDIA_CAST_RTCP_RTCP_H_ | |
10 | |
11 #include <stddef.h> | |
12 #include <stdint.h> | |
13 | 7 |
14 #include <map> | 8 #include <map> |
15 #include <queue> | 9 #include <queue> |
16 #include <string> | 10 #include <utility> |
17 | 11 |
18 #include "base/macros.h" | 12 #include "base/containers/hash_tables.h" |
19 #include "base/memory/scoped_ptr.h" | |
20 #include "base/memory/weak_ptr.h" | |
21 #include "base/time/tick_clock.h" | |
22 #include "base/time/time.h" | 13 #include "base/time/time.h" |
23 #include "media/cast/common/clock_drift_smoother.h" | 14 #include "media/cast/net/pacing/paced_sender.h" |
24 #include "media/cast/net/cast_transport_defines.h" | |
25 #include "media/cast/net/cast_transport_sender.h" | |
26 #include "media/cast/net/rtcp/receiver_rtcp_event_subscriber.h" | |
27 #include "media/cast/net/rtcp/rtcp_builder.h" | |
28 #include "media/cast/net/rtcp/rtcp_defines.h" | 15 #include "media/cast/net/rtcp/rtcp_defines.h" |
| 16 #include "media/cast/net/rtcp/rtcp_session.h" |
29 #include "media/cast/net/rtcp/rtcp_utility.h" | 17 #include "media/cast/net/rtcp/rtcp_utility.h" |
30 | 18 |
31 namespace media { | 19 namespace media { |
32 namespace cast { | 20 namespace cast { |
33 | 21 |
34 class LocalRtcpReceiverFeedback; | |
35 class PacedPacketSender; | |
36 class RtcpReceiver; | |
37 class RtcpBuilder; | |
38 | |
39 typedef std::pair<uint32_t, base::TimeTicks> RtcpSendTimePair; | 22 typedef std::pair<uint32_t, base::TimeTicks> RtcpSendTimePair; |
40 typedef std::map<uint32_t, base::TimeTicks> RtcpSendTimeMap; | 23 typedef std::map<uint32_t, base::TimeTicks> RtcpSendTimeMap; |
41 typedef std::queue<RtcpSendTimePair> RtcpSendTimeQueue; | 24 typedef std::queue<RtcpSendTimePair> RtcpSendTimeQueue; |
42 | 25 |
43 // TODO(hclam): This should be renamed to RtcpSession. | 26 // This class represents a RTCP session on a RTP sender. It provides an |
44 class Rtcp { | 27 // interface to send RTCP sender report (SR). RTCP SR packets allow |
| 28 // receiver to maintain clock offsets and synchronize between audio and video. |
| 29 // |
| 30 // RTCP session on sender handles the following incoming RTCP reports |
| 31 // from receiver: |
| 32 // - Receiver reference time report: Helps with tracking largest timestamp |
| 33 // seen and as a result rejecting old RTCP reports. |
| 34 // - Receiver logs: The sender receives log events from the receiver and |
| 35 // invokes a callback passed. |
| 36 // - cast message: Receives feedback from receiver on missing packets/frames |
| 37 // and last frame id received and invokes a callback passed. |
| 38 // - Last report: The receiver provides feedback on delay since last report |
| 39 // received which helps it compute round trip time and invoke a callback. |
| 40 class SenderRtcpSession : public RtcpSession { |
45 public: | 41 public: |
46 Rtcp(const RtcpCastMessageCallback& cast_callback, | 42 SenderRtcpSession(const RtcpCastMessageCallback& cast_callback, |
47 const RtcpRttCallback& rtt_callback, | 43 const RtcpRttCallback& rtt_callback, |
48 const RtcpLogMessageCallback& log_callback, | 44 const RtcpLogMessageCallback& log_callback, |
49 base::TickClock* clock, // Not owned. | 45 base::TickClock* clock, // Not owned. |
50 PacedPacketSender* packet_sender, // Not owned. | 46 PacedPacketSender* packet_sender, // Not owned. |
51 uint32_t local_ssrc, | 47 uint32_t local_ssrc, |
52 uint32_t remote_ssrc); | 48 uint32_t remote_ssrc); |
53 | 49 |
54 virtual ~Rtcp(); | 50 ~SenderRtcpSession() override; |
| 51 |
| 52 // If greater than zero, this is the last measured network round trip time. |
| 53 base::TimeDelta current_round_trip_time() const { |
| 54 return current_round_trip_time_; |
| 55 } |
55 | 56 |
56 // Send a RTCP sender report. | 57 // Send a RTCP sender report. |
57 // |current_time| is the current time reported by a tick clock. | 58 // |current_time| is the current time reported by a tick clock. |
58 // |current_time_as_rtp_timestamp| is the corresponding RTP timestamp. | 59 // |current_time_as_rtp_timestamp| is the corresponding RTP timestamp. |
59 // |send_packet_count| is the number of packets sent. | 60 // |send_packet_count| is the number of packets sent. |
60 // |send_octet_count| is the number of octets sent. | 61 // |send_octet_count| is the number of octets sent. |
61 void SendRtcpFromRtpSender(base::TimeTicks current_time, | 62 void SendRtcpReport(base::TimeTicks current_time, |
62 RtpTimeTicks current_time_as_rtp_timestamp, | 63 RtpTimeTicks current_time_as_rtp_timestamp, |
63 uint32_t send_packet_count, | 64 uint32_t send_packet_count, |
64 size_t send_octet_count); | 65 size_t send_octet_count); |
65 | 66 |
66 // This function is meant to be used in conjunction with | 67 // Handle incoming RTCP packet. |
67 // SendRtcpFromRtpReceiver. | 68 // Returns false if it is not a RTCP packet or it is not directed to |
68 // |now| is converted to NTP and saved internally for | |
69 // future round-trip/lip-sync calculations. | |
70 // This is done in a separate method so that SendRtcpFromRtpReceiver can | |
71 // be done on a separate (temporary) RTCP object. | |
72 RtcpTimeData ConvertToNTPAndSave(base::TimeTicks now); | |
73 | |
74 // |cast_message|, |rtcp_events| and |rtp_receiver_statistics| are optional; | |
75 // if |cast_message| is provided the RTCP receiver report will append a Cast | |
76 // message containing Acks and Nacks; |target_delay| is sent together with | |
77 // |cast_message|. If |rtcp_events| is provided the RTCP receiver report will | |
78 // append the log messages. | |
79 void SendRtcpFromRtpReceiver( | |
80 RtcpTimeData time_data, | |
81 const RtcpCastMessage* cast_message, | |
82 base::TimeDelta target_delay, | |
83 const ReceiverRtcpEventSubscriber::RtcpEvents* rtcp_events, | |
84 const RtpReceiverStatistics* rtp_receiver_statistics) const; | |
85 | |
86 // Submit a received packet to this object. The packet will be parsed | |
87 // and used to maintain a RTCP session. | |
88 // Returns false if this is not a RTCP packet or it is not directed to | |
89 // this session, e.g. SSRC doesn't match. | 69 // this session, e.g. SSRC doesn't match. |
90 bool IncomingRtcpPacket(const uint8_t* data, size_t length); | 70 bool IncomingRtcpPacket(const uint8_t* data, size_t length) override; |
91 | |
92 // If available, returns true and sets the output arguments to the latest | |
93 // lip-sync timestamps gleaned from the sender reports. While the sender | |
94 // provides reference NTP times relative to its own wall clock, the | |
95 // |reference_time| returned here has been translated to the local | |
96 // CastEnvironment clock. | |
97 bool GetLatestLipSyncTimes(RtpTimeTicks* rtp_timestamp, | |
98 base::TimeTicks* reference_time) const; | |
99 | |
100 void OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log); | |
101 | |
102 // If greater than zero, this is the last measured network round trip time. | |
103 base::TimeDelta current_round_trip_time() const { | |
104 return current_round_trip_time_; | |
105 } | |
106 | |
107 static bool IsRtcpPacket(const uint8_t* packet, size_t length); | |
108 static uint32_t GetSsrcOfSender(const uint8_t* rtcp_buffer, size_t length); | |
109 | |
110 uint32_t GetLocalSsrc() const { return local_ssrc_; } | |
111 uint32_t GetRemoteSsrc() const { return remote_ssrc_; } | |
112 | |
113 protected: | |
114 void OnReceivedNtp(uint32_t ntp_seconds, uint32_t ntp_fraction); | |
115 void OnReceivedLipSyncInfo(RtpTimeTicks rtp_timestamp, | |
116 uint32_t ntp_seconds, | |
117 uint32_t ntp_fraction); | |
118 | 71 |
119 private: | 72 private: |
| 73 // Received last report information from RTP receiver which helps compute |
| 74 // round trip time. |
120 void OnReceivedDelaySinceLastReport(uint32_t last_report, | 75 void OnReceivedDelaySinceLastReport(uint32_t last_report, |
121 uint32_t delay_since_last_report); | 76 uint32_t delay_since_last_report); |
122 | 77 |
| 78 // Received logs from RTP receiver. |
| 79 void OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log); |
| 80 |
| 81 // Received cast message containing details of missing packets/frames and |
| 82 // last frame id received. |
123 void OnReceivedCastFeedback(const RtcpCastMessage& cast_message); | 83 void OnReceivedCastFeedback(const RtcpCastMessage& cast_message); |
124 | 84 |
125 void SaveLastSentNtpTime(const base::TimeTicks& now, | |
126 uint32_t last_ntp_seconds, | |
127 uint32_t last_ntp_fraction); | |
128 | |
129 // Remove duplicate events in |receiver_log|. | 85 // Remove duplicate events in |receiver_log|. |
130 // Returns true if any events remain. | 86 // Returns true if any events remain. |
131 bool DedupeReceiverLog(RtcpReceiverLogMessage* receiver_log); | 87 bool DedupeReceiverLog(RtcpReceiverLogMessage* receiver_log); |
132 | 88 |
| 89 // Save last sent NTP time on RTPC SR. This helps map the sent time when a |
| 90 // last report is received from RTP receiver to compute RTT. |
| 91 void SaveLastSentNtpTime(const base::TimeTicks& now, |
| 92 uint32_t last_ntp_seconds, |
| 93 uint32_t last_ntp_fraction); |
| 94 |
| 95 base::TickClock* const clock_; // Not owned. |
| 96 PacedPacketSender* packet_sender_; // Not owned. |
| 97 const uint32_t local_ssrc_; |
| 98 const uint32_t remote_ssrc_; |
| 99 |
133 const RtcpCastMessageCallback cast_callback_; | 100 const RtcpCastMessageCallback cast_callback_; |
134 const RtcpRttCallback rtt_callback_; | 101 const RtcpRttCallback rtt_callback_; |
135 const RtcpLogMessageCallback log_callback_; | 102 const RtcpLogMessageCallback log_callback_; |
136 base::TickClock* const clock_; // Not owned by this class. | 103 |
137 RtcpBuilder rtcp_builder_; | 104 // Computed from RTCP RRTR report. |
138 PacedPacketSender* packet_sender_; // Not owned. | 105 base::TimeTicks largest_seen_timestamp_; |
139 const uint32_t local_ssrc_; | |
140 const uint32_t remote_ssrc_; | |
141 | 106 |
142 // The RTCP packet parser is re-used when parsing each RTCP packet. It | 107 // The RTCP packet parser is re-used when parsing each RTCP packet. It |
143 // remembers state about prior RTP timestamps and other sequence values to | 108 // remembers state about prior RTP timestamps and other sequence values to |
144 // re-construct "expanded" values. | 109 // re-construct "expanded" values. |
145 RtcpParser parser_; | 110 RtcpParser parser_; |
146 | 111 |
147 RtcpSendTimeMap last_reports_sent_map_; | |
148 RtcpSendTimeQueue last_reports_sent_queue_; | |
149 | |
150 // The truncated (i.e., 64-->32-bit) NTP timestamp provided in the last report | |
151 // from the remote peer, along with the local time at which the report was | |
152 // received. These values are used for ping-pong'ing NTP timestamps between | |
153 // the peers so that they can estimate the network's round-trip time. | |
154 uint32_t last_report_truncated_ntp_; | |
155 base::TimeTicks time_last_report_received_; | |
156 | |
157 // Maintains a smoothed offset between the local clock and the remote clock. | |
158 // Calling this member's Current() method is only valid if | |
159 // |time_last_report_received_| is not "null." | |
160 ClockDriftSmoother local_clock_ahead_by_; | |
161 | |
162 // Latest "lip sync" info from the sender. The sender provides the RTP | |
163 // timestamp of some frame of its choosing and also a corresponding reference | |
164 // NTP timestamp sampled from a clock common to all media streams. It is | |
165 // expected that the sender will update this data regularly and in a timely | |
166 // manner (e.g., about once per second). | |
167 RtpTimeTicks lip_sync_rtp_timestamp_; | |
168 uint64_t lip_sync_ntp_timestamp_; | |
169 | |
170 // The last measured network round trip time. This is updated with each | |
171 // sender report --> receiver report round trip. If this is zero, then the | |
172 // round trip time has not been measured yet. | |
173 base::TimeDelta current_round_trip_time_; | |
174 | |
175 base::TimeTicks largest_seen_timestamp_; | |
176 | |
177 // For extending received ACK frame IDs from 8-bit to 32-bit. | 112 // For extending received ACK frame IDs from 8-bit to 32-bit. |
178 FrameIdWrapHelper ack_frame_id_wrap_helper_; | 113 FrameIdWrapHelper ack_frame_id_wrap_helper_; |
179 | 114 |
180 // Maintains a history of receiver events. | 115 // Maintains a history of receiver events. |
181 typedef std::pair<uint64_t, uint64_t> ReceiverEventKey; | 116 typedef std::pair<uint64_t, uint64_t> ReceiverEventKey; |
182 base::hash_set<ReceiverEventKey> receiver_event_key_set_; | 117 base::hash_set<ReceiverEventKey> receiver_event_key_set_; |
183 std::queue<ReceiverEventKey> receiver_event_key_queue_; | 118 std::queue<ReceiverEventKey> receiver_event_key_queue_; |
184 | 119 |
185 DISALLOW_COPY_AND_ASSIGN(Rtcp); | 120 // The last measured network round trip time. This is updated with each |
| 121 // sender report --> receiver report round trip. If this is zero, then the |
| 122 // round trip time has not been measured yet. |
| 123 base::TimeDelta current_round_trip_time_; |
| 124 |
| 125 // Map of NTP timestamp to local time that helps with RTT calculation |
| 126 // when last report is received from RTP receiver. |
| 127 RtcpSendTimeMap last_reports_sent_map_; |
| 128 RtcpSendTimeQueue last_reports_sent_queue_; |
| 129 |
| 130 DISALLOW_COPY_AND_ASSIGN(SenderRtcpSession); |
186 }; | 131 }; |
187 | 132 |
188 } // namespace cast | 133 } // namespace cast |
189 } // namespace media | 134 } // namespace media |
190 | 135 |
191 #endif // MEDIA_CAST_RTCP_RTCP_H_ | 136 #endif // MEDIA_CAST_NET_RTCP_SENDER_RTCP_SESSION_H_ |
OLD | NEW |