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 <map> 12 #include <queue> 13 #include <string> 14 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" 28 29 namespace media { 30 namespace cast { 31 32 class LocalRtcpReceiverFeedback; 33 class PacedPacketSender; 34 class RtcpReceiver; 35 class RtcpBuilder; 36 37 typedef std::pair<uint32, base::TimeTicks> RtcpSendTimePair; 38 typedef std::map<uint32, base::TimeTicks> RtcpSendTimeMap; 39 typedef std::queue<RtcpSendTimePair> RtcpSendTimeQueue; 40 41 class RtpReceiverStatistics { 42 public: 43 virtual void GetStatistics(uint8* fraction_lost, 44 uint32* cumulative_lost, // 24 bits valid. 45 uint32* extended_high_sequence_number, 46 uint32* jitter) = 0; 47 48 virtual ~RtpReceiverStatistics() {} 49 }; 50 51 // TODO(hclam): This should be renamed to RtcpSession. 52 class Rtcp { 53 public: 54 Rtcp(const RtcpCastMessageCallback& cast_callback, 55 const RtcpRttCallback& rtt_callback, 56 const RtcpLogMessageCallback& log_callback, 57 base::TickClock* clock, // Not owned. 58 PacedPacketSender* packet_sender, // Not owned. 59 uint32 local_ssrc, 60 uint32 remote_ssrc); 61 62 virtual ~Rtcp(); 63 64 // Send a RTCP sender report. 65 // |current_time| is the current time reported by a tick clock. 66 // |current_time_as_rtp_timestamp| is the corresponding RTP timestamp. 67 // |send_packet_count| is the number of packets sent. 68 // |send_octet_count| is the number of octets sent. 69 void SendRtcpFromRtpSender( 70 base::TimeTicks current_time, 71 uint32 current_time_as_rtp_timestamp, 72 uint32 send_packet_count, 73 size_t send_octet_count); 74 75 // |cast_message| and |rtcp_events| is optional; if |cast_message| is 76 // provided the RTCP receiver report will append a Cast message containing 77 // Acks and Nacks; |target_delay| is sent together with |cast_message|. 78 // If |rtcp_events| is provided the RTCP receiver report will append the 79 // log messages. 80 void SendRtcpFromRtpReceiver( 81 const RtcpCastMessage* cast_message, 82 base::TimeDelta target_delay, 83 const ReceiverRtcpEventSubscriber::RtcpEventMultiMap* rtcp_events, 84 RtpReceiverStatistics* rtp_receiver_statistics); 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. 90 bool IncomingRtcpPacket(const uint8* data, size_t length); 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(uint32* 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* packet, size_t length); 108 static uint32 GetSsrcOfSender(const uint8* rtcp_buffer, size_t length); 109 110 protected: 111 void OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction); 112 void OnReceivedLipSyncInfo(uint32 rtp_timestamp, 113 uint32 ntp_seconds, 114 uint32 ntp_fraction); 115 116 private: 117 void OnReceivedDelaySinceLastReport(uint32 last_report, 118 uint32 delay_since_last_report); 119 120 void OnReceivedCastFeedback(const RtcpCastMessage& cast_message); 121 122 void SaveLastSentNtpTime(const base::TimeTicks& now, 123 uint32 last_ntp_seconds, 124 uint32 last_ntp_fraction); 125 126 // Remove duplicate events in |receiver_log|. 127 // Returns true if any events remain. 128 bool DedupeReceiverLog(RtcpReceiverLogMessage* receiver_log); 129 130 const RtcpCastMessageCallback cast_callback_; 131 const RtcpRttCallback rtt_callback_; 132 const RtcpLogMessageCallback log_callback_; 133 base::TickClock* const clock_; // Not owned by this class. 134 RtcpBuilder rtcp_builder_; 135 PacedPacketSender* packet_sender_; // Not owned. 136 const uint32 local_ssrc_; 137 const uint32 remote_ssrc_; 138 139 RtcpSendTimeMap last_reports_sent_map_; 140 RtcpSendTimeQueue last_reports_sent_queue_; 141 142 // The truncated (i.e., 64-->32-bit) NTP timestamp provided in the last report 143 // from the remote peer, along with the local time at which the report was 144 // received. These values are used for ping-pong'ing NTP timestamps between 145 // the peers so that they can estimate the network's round-trip time. 146 uint32 last_report_truncated_ntp_; 147 base::TimeTicks time_last_report_received_; 148 149 // Maintains a smoothed offset between the local clock and the remote clock. 150 // Calling this member's Current() method is only valid if 151 // |time_last_report_received_| is not "null." 152 ClockDriftSmoother local_clock_ahead_by_; 153 154 // Latest "lip sync" info from the sender. The sender provides the RTP 155 // timestamp of some frame of its choosing and also a corresponding reference 156 // NTP timestamp sampled from a clock common to all media streams. It is 157 // expected that the sender will update this data regularly and in a timely 158 // manner (e.g., about once per second). 159 uint32 lip_sync_rtp_timestamp_; 160 uint64 lip_sync_ntp_timestamp_; 161 162 // The last measured network round trip time. This is updated with each 163 // sender report --> receiver report round trip. If this is zero, then the 164 // round trip time has not been measured yet. 165 base::TimeDelta current_round_trip_time_; 166 167 base::TimeTicks largest_seen_timestamp_; 168 169 // For extending received ACK frame IDs from 8-bit to 32-bit. 170 FrameIdWrapHelper ack_frame_id_wrap_helper_; 171 172 // Maintains a history of receiver events. 173 typedef std::pair<uint64, uint64> ReceiverEventKey; 174 base::hash_set<ReceiverEventKey> receiver_event_key_set_; 175 std::queue<ReceiverEventKey> receiver_event_key_queue_; 176 177 DISALLOW_COPY_AND_ASSIGN(Rtcp); 178 }; 179 180 } // namespace cast 181 } // namespace media 182 183 #endif // MEDIA_CAST_RTCP_RTCP_H_ 184