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