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.
5 // This class maintains a bi-directional RTCP connection with a remote
8 #ifndef MEDIA_CAST_RTCP_RTCP_H_
9 #define MEDIA_CAST_RTCP_RTCP_H_
15 #include "base/basictypes.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/time/tick_clock.h"
19 #include "base/time/time.h"
20 #include "media/cast/cast_config.h"
21 #include "media/cast/cast_defines.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"
32 class LocalRtcpReceiverFeedback
;
33 class PacedPacketSender
;
37 typedef std::pair
<uint32
, base::TimeTicks
> RtcpSendTimePair
;
38 typedef std::map
<uint32
, base::TimeTicks
> RtcpSendTimeMap
;
39 typedef std::queue
<RtcpSendTimePair
> RtcpSendTimeQueue
;
41 // TODO(hclam): This should be renamed to RtcpSession.
44 Rtcp(const RtcpCastMessageCallback
& cast_callback
,
45 const RtcpRttCallback
& rtt_callback
,
46 const RtcpLogMessageCallback
& log_callback
,
47 base::TickClock
* clock
, // Not owned.
48 PacedPacketSender
* packet_sender
, // Not owned.
54 // Send a RTCP sender report.
55 // |current_time| is the current time reported by a tick clock.
56 // |current_time_as_rtp_timestamp| is the corresponding RTP timestamp.
57 // |send_packet_count| is the number of packets sent.
58 // |send_octet_count| is the number of octets sent.
59 void SendRtcpFromRtpSender(
60 base::TimeTicks current_time
,
61 uint32 current_time_as_rtp_timestamp
,
62 uint32 send_packet_count
,
63 size_t send_octet_count
);
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
);
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;
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
);
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(uint32
* rtp_timestamp
,
97 base::TimeTicks
* reference_time
) const;
99 void OnReceivedReceiverLog(const RtcpReceiverLogMessage
& receiver_log
);
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_
;
106 static bool IsRtcpPacket(const uint8
* packet
, size_t length
);
107 static uint32
GetSsrcOfSender(const uint8
* rtcp_buffer
, size_t length
);
109 uint32
GetLocalSsrc() const { return local_ssrc_
; }
110 uint32
GetRemoteSsrc() const { return remote_ssrc_
; }
113 void OnReceivedNtp(uint32 ntp_seconds
, uint32 ntp_fraction
);
114 void OnReceivedLipSyncInfo(uint32 rtp_timestamp
,
116 uint32 ntp_fraction
);
119 void OnReceivedDelaySinceLastReport(uint32 last_report
,
120 uint32 delay_since_last_report
);
122 void OnReceivedCastFeedback(const RtcpCastMessage
& cast_message
);
124 void SaveLastSentNtpTime(const base::TimeTicks
& now
,
125 uint32 last_ntp_seconds
,
126 uint32 last_ntp_fraction
);
128 // Remove duplicate events in |receiver_log|.
129 // Returns true if any events remain.
130 bool DedupeReceiverLog(RtcpReceiverLogMessage
* receiver_log
);
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 local_ssrc_
;
139 const uint32 remote_ssrc_
;
141 RtcpSendTimeMap last_reports_sent_map_
;
142 RtcpSendTimeQueue last_reports_sent_queue_
;
144 // The truncated (i.e., 64-->32-bit) NTP timestamp provided in the last report
145 // from the remote peer, along with the local time at which the report was
146 // received. These values are used for ping-pong'ing NTP timestamps between
147 // the peers so that they can estimate the network's round-trip time.
148 uint32 last_report_truncated_ntp_
;
149 base::TimeTicks time_last_report_received_
;
151 // Maintains a smoothed offset between the local clock and the remote clock.
152 // Calling this member's Current() method is only valid if
153 // |time_last_report_received_| is not "null."
154 ClockDriftSmoother local_clock_ahead_by_
;
156 // Latest "lip sync" info from the sender. The sender provides the RTP
157 // timestamp of some frame of its choosing and also a corresponding reference
158 // NTP timestamp sampled from a clock common to all media streams. It is
159 // expected that the sender will update this data regularly and in a timely
160 // manner (e.g., about once per second).
161 uint32 lip_sync_rtp_timestamp_
;
162 uint64 lip_sync_ntp_timestamp_
;
164 // The last measured network round trip time. This is updated with each
165 // sender report --> receiver report round trip. If this is zero, then the
166 // round trip time has not been measured yet.
167 base::TimeDelta current_round_trip_time_
;
169 base::TimeTicks largest_seen_timestamp_
;
171 // For extending received ACK frame IDs from 8-bit to 32-bit.
172 FrameIdWrapHelper ack_frame_id_wrap_helper_
;
174 // Maintains a history of receiver events.
175 typedef std::pair
<uint64
, uint64
> ReceiverEventKey
;
176 base::hash_set
<ReceiverEventKey
> receiver_event_key_set_
;
177 std::queue
<ReceiverEventKey
> receiver_event_key_queue_
;
179 DISALLOW_COPY_AND_ASSIGN(Rtcp
);
185 #endif // MEDIA_CAST_RTCP_RTCP_H_