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