1 // Copyright 2013 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 #ifndef MEDIA_CAST_RTCP_RTCP_H_
6 #define MEDIA_CAST_RTCP_RTCP_H_
14 #include "base/basictypes.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/tick_clock.h"
17 #include "base/time/time.h"
18 #include "media/cast/cast_config.h"
19 #include "media/cast/cast_defines.h"
20 #include "media/cast/cast_environment.h"
21 #include "media/cast/rtcp/rtcp_defines.h"
22 #include "media/cast/transport/cast_transport_defines.h"
23 #include "media/cast/transport/cast_transport_sender.h"
24 #include "media/cast/transport/pacing/paced_sender.h"
29 class LocalRtcpReceiverFeedback;
30 class LocalRtcpRttFeedback;
31 class PacedPacketSender;
32 class ReceiverRtcpEventSubscriber;
36 typedef std::pair<uint32, base::TimeTicks> RtcpSendTimePair;
37 typedef std::map<uint32, base::TimeTicks> RtcpSendTimeMap;
38 typedef std::queue<RtcpSendTimePair> RtcpSendTimeQueue;
40 class RtcpSenderFeedback {
42 virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) = 0;
44 virtual ~RtcpSenderFeedback() {}
47 class RtpSenderStatistics {
49 virtual void GetStatistics(const base::TimeTicks& now,
50 transport::RtcpSenderInfo* sender_info) = 0;
52 virtual ~RtpSenderStatistics() {}
55 class RtpReceiverStatistics {
57 virtual void GetStatistics(uint8* fraction_lost,
58 uint32* cumulative_lost, // 24 bits valid.
59 uint32* extended_high_sequence_number,
62 virtual ~RtpReceiverStatistics() {}
67 // Rtcp accepts two transports, one to be used by Cast senders
68 // (CastTransportSender) only, and the other (PacedPacketSender) should only
69 // be used by the Cast receivers and test applications.
70 Rtcp(scoped_refptr<CastEnvironment> cast_environment,
71 RtcpSenderFeedback* sender_feedback,
72 transport::CastTransportSender* const transport_sender, // Send-side.
73 transport::PacedPacketSender* paced_packet_sender, // Receive side.
74 RtpSenderStatistics* rtp_sender_statistics,
75 RtpReceiverStatistics* rtp_receiver_statistics,
77 const base::TimeDelta& rtcp_interval,
80 const std::string& c_name);
84 static bool IsRtcpPacket(const uint8* rtcp_buffer, size_t length);
86 static uint32 GetSsrcOfSender(const uint8* rtcp_buffer, size_t length);
88 base::TimeTicks TimeToSendNextRtcpReport();
89 // |sender_log_message| is optional; without it no log messages will be
90 // attached to the RTCP report; instead a normal RTCP send report will be
92 // Additionally if all messages in |sender_log_message| does
93 // not fit in the packet the |sender_log_message| will contain the remaining
95 void SendRtcpFromRtpSender(
96 const transport::RtcpSenderLogMessage& sender_log_message);
98 // |cast_message| and |event_subscriber| is optional; if |cast_message| is
99 // provided the RTCP receiver report will append a Cast message containing
100 // Acks and Nacks; if |event_subscriber| is provided the RTCP receiver report
101 // will append the log messages from the subscriber.
102 void SendRtcpFromRtpReceiver(const RtcpCastMessage* cast_message,
103 ReceiverRtcpEventSubscriber* event_subscriber);
105 void IncomingRtcpPacket(const uint8* rtcp_buffer, size_t length);
106 bool Rtt(base::TimeDelta* rtt,
107 base::TimeDelta* avg_rtt,
108 base::TimeDelta* min_rtt,
109 base::TimeDelta* max_rtt) const;
110 bool RtpTimestampInSenderTime(int frequency,
111 uint32 rtp_timestamp,
112 base::TimeTicks* rtp_timestamp_in_ticks) const;
115 int CheckForWrapAround(uint32 new_timestamp, uint32 old_timestamp) const;
117 void OnReceivedLipSyncInfo(uint32 rtp_timestamp,
119 uint32 ntp_fraction);
122 friend class LocalRtcpRttFeedback;
123 friend class LocalRtcpReceiverFeedback;
125 void SendRtcp(const base::TimeTicks& now,
126 uint32 packet_type_flags,
128 const RtcpCastMessage* cast_message);
130 void OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction);
132 void OnReceivedDelaySinceLastReport(uint32 receivers_ssrc,
134 uint32 delay_since_last_report);
136 void OnReceivedSendReportRequest();
138 void UpdateRtt(const base::TimeDelta& sender_delay,
139 const base::TimeDelta& receiver_delay);
141 void UpdateNextTimeToSendRtcp();
143 void SaveLastSentNtpTime(const base::TimeTicks& now,
144 uint32 last_ntp_seconds,
145 uint32 last_ntp_fraction);
147 void SendRtcpFromRtpSenderOnTransportThread(
148 uint32 packet_type_flags,
149 const transport::RtcpSenderInfo& sender_info,
150 const transport::RtcpDlrrReportBlock& dlrr,
151 const transport::RtcpSenderLogMessage& sender_log,
155 scoped_refptr<CastEnvironment> cast_environment_;
156 transport::CastTransportSender* const transport_sender_;
157 const base::TimeDelta rtcp_interval_;
158 const RtcpMode rtcp_mode_;
159 const uint32 local_ssrc_;
160 const uint32 remote_ssrc_;
161 const std::string c_name_;
163 // Not owned by this class.
164 RtpSenderStatistics* const rtp_sender_statistics_;
165 RtpReceiverStatistics* const rtp_receiver_statistics_;
167 scoped_ptr<LocalRtcpRttFeedback> rtt_feedback_;
168 scoped_ptr<LocalRtcpReceiverFeedback> receiver_feedback_;
169 scoped_ptr<RtcpSender> rtcp_sender_;
170 scoped_ptr<RtcpReceiver> rtcp_receiver_;
172 base::TimeTicks next_time_to_send_rtcp_;
173 RtcpSendTimeMap last_reports_sent_map_;
174 RtcpSendTimeQueue last_reports_sent_queue_;
175 base::TimeTicks time_last_report_received_;
176 uint32 last_report_received_;
178 uint32 last_received_rtp_timestamp_;
179 uint32 last_received_ntp_seconds_;
180 uint32 last_received_ntp_fraction_;
182 base::TimeDelta rtt_;
183 base::TimeDelta min_rtt_;
184 base::TimeDelta max_rtt_;
185 int number_of_rtt_in_avg_;
188 DISALLOW_COPY_AND_ASSIGN(Rtcp);
194 #endif // MEDIA_CAST_RTCP_RTCP_H_