• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef MEDIA_CAST_RTCP_RTCP_H_
6 #define MEDIA_CAST_RTCP_RTCP_H_
7 
8 #include <map>
9 #include <queue>
10 #include <string>
11 
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/tick_clock.h"
15 #include "base/time/time.h"
16 #include "media/cast/base/clock_drift_smoother.h"
17 #include "media/cast/cast_config.h"
18 #include "media/cast/cast_defines.h"
19 #include "media/cast/cast_environment.h"
20 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.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"
25 
26 namespace media {
27 namespace cast {
28 
29 class LocalRtcpReceiverFeedback;
30 class LocalRtcpRttFeedback;
31 class PacedPacketSender;
32 class RtcpReceiver;
33 class RtcpSender;
34 
35 typedef std::pair<uint32, base::TimeTicks> RtcpSendTimePair;
36 typedef std::map<uint32, base::TimeTicks> RtcpSendTimeMap;
37 typedef std::queue<RtcpSendTimePair> RtcpSendTimeQueue;
38 
39 class RtcpSenderFeedback {
40  public:
41   virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) = 0;
42 
~RtcpSenderFeedback()43   virtual ~RtcpSenderFeedback() {}
44 };
45 
46 class RtpReceiverStatistics {
47  public:
48   virtual void GetStatistics(uint8* fraction_lost,
49                              uint32* cumulative_lost,  // 24 bits valid.
50                              uint32* extended_high_sequence_number,
51                              uint32* jitter) = 0;
52 
~RtpReceiverStatistics()53   virtual ~RtpReceiverStatistics() {}
54 };
55 
56 class Rtcp {
57  public:
58   // Rtcp accepts two transports, one to be used by Cast senders
59   // (CastTransportSender) only, and the other (PacedPacketSender) should only
60   // be used by the Cast receivers and test applications.
61   Rtcp(scoped_refptr<CastEnvironment> cast_environment,
62        RtcpSenderFeedback* sender_feedback,
63        transport::CastTransportSender* const transport_sender,  // Send-side.
64        transport::PacedPacketSender* paced_packet_sender,       // Receive side.
65        RtpReceiverStatistics* rtp_receiver_statistics,
66        RtcpMode rtcp_mode,
67        const base::TimeDelta& rtcp_interval,
68        uint32 local_ssrc,
69        uint32 remote_ssrc,
70        const std::string& c_name,
71        EventMediaType event_media_type);
72 
73   virtual ~Rtcp();
74 
75   static bool IsRtcpPacket(const uint8* rtcp_buffer, size_t length);
76 
77   static uint32 GetSsrcOfSender(const uint8* rtcp_buffer, size_t length);
78 
79   base::TimeTicks TimeToSendNextRtcpReport();
80 
81   // Send a RTCP sender report.
82   // |current_time| is the current time reported by a tick clock.
83   // |current_time_as_rtp_timestamp| is the corresponding RTP timestamp.
84   void SendRtcpFromRtpSender(base::TimeTicks current_time,
85                              uint32 current_time_as_rtp_timestamp);
86 
87   // |cast_message| and |rtcp_events| is optional; if |cast_message| is
88   // provided the RTCP receiver report will append a Cast message containing
89   // Acks and Nacks; if |rtcp_events| is provided the RTCP receiver report
90   // will append the log messages.
91   void SendRtcpFromRtpReceiver(
92       const RtcpCastMessage* cast_message,
93       const ReceiverRtcpEventSubscriber::RtcpEventMultiMap* rtcp_events);
94 
95   void IncomingRtcpPacket(const uint8* rtcp_buffer, size_t length);
96 
97   // TODO(miu): Clean up this method and downstream code: Only VideoSender uses
98   // this (for congestion control), and only the |rtt| and |avg_rtt| values, and
99   // it's not clear that any of the downstream code is doing the right thing
100   // with this data.
101   bool Rtt(base::TimeDelta* rtt,
102            base::TimeDelta* avg_rtt,
103            base::TimeDelta* min_rtt,
104            base::TimeDelta* max_rtt) const;
105 
is_rtt_available()106   bool is_rtt_available() const { return number_of_rtt_in_avg_ > 0; }
107 
108   // If available, returns true and sets the output arguments to the latest
109   // lip-sync timestamps gleaned from the sender reports.  While the sender
110   // provides reference NTP times relative to its own wall clock, the
111   // |reference_time| returned here has been translated to the local
112   // CastEnvironment clock.
113   bool GetLatestLipSyncTimes(uint32* rtp_timestamp,
114                              base::TimeTicks* reference_time) const;
115 
116   // Set the history size to record Cast receiver events. The event history is
117   // used to remove duplicates. The history will store at most |size| events.
118   void SetCastReceiverEventHistorySize(size_t size);
119 
120   // Update the target delay. Will be added to every report sent back to the
121   // sender.
122   // TODO(miu): Remove this deprecated functionality. The sender ignores this.
123   void SetTargetDelay(base::TimeDelta target_delay);
124 
125   void OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log);
126 
127  protected:
128   void OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction);
129   void OnReceivedLipSyncInfo(uint32 rtp_timestamp,
130                              uint32 ntp_seconds,
131                              uint32 ntp_fraction);
132 
133  private:
134   friend class LocalRtcpRttFeedback;
135   friend class LocalRtcpReceiverFeedback;
136 
137   void OnReceivedDelaySinceLastReport(uint32 receivers_ssrc,
138                                       uint32 last_report,
139                                       uint32 delay_since_last_report);
140 
141   void OnReceivedSendReportRequest();
142 
143   void UpdateRtt(const base::TimeDelta& sender_delay,
144                  const base::TimeDelta& receiver_delay);
145 
146   void UpdateNextTimeToSendRtcp();
147 
148   void SaveLastSentNtpTime(const base::TimeTicks& now,
149                            uint32 last_ntp_seconds,
150                            uint32 last_ntp_fraction);
151 
152   scoped_refptr<CastEnvironment> cast_environment_;
153   transport::CastTransportSender* const transport_sender_;
154   const base::TimeDelta rtcp_interval_;
155   const RtcpMode rtcp_mode_;
156   const uint32 local_ssrc_;
157   const uint32 remote_ssrc_;
158   const std::string c_name_;
159   const EventMediaType event_media_type_;
160 
161   // Not owned by this class.
162   RtpReceiverStatistics* const rtp_receiver_statistics_;
163 
164   scoped_ptr<LocalRtcpRttFeedback> rtt_feedback_;
165   scoped_ptr<LocalRtcpReceiverFeedback> receiver_feedback_;
166   scoped_ptr<RtcpSender> rtcp_sender_;
167   scoped_ptr<RtcpReceiver> rtcp_receiver_;
168 
169   base::TimeTicks next_time_to_send_rtcp_;
170   RtcpSendTimeMap last_reports_sent_map_;
171   RtcpSendTimeQueue last_reports_sent_queue_;
172 
173   // The truncated (i.e., 64-->32-bit) NTP timestamp provided in the last report
174   // from the remote peer, along with the local time at which the report was
175   // received.  These values are used for ping-pong'ing NTP timestamps between
176   // the peers so that they can estimate the network's round-trip time.
177   uint32 last_report_truncated_ntp_;
178   base::TimeTicks time_last_report_received_;
179 
180   // Maintains a smoothed offset between the local clock and the remote clock.
181   // Calling this member's Current() method is only valid if
182   // |time_last_report_received_| is not "null."
183   ClockDriftSmoother local_clock_ahead_by_;
184 
185   // Latest "lip sync" info from the sender.  The sender provides the RTP
186   // timestamp of some frame of its choosing and also a corresponding reference
187   // NTP timestamp sampled from a clock common to all media streams.  It is
188   // expected that the sender will update this data regularly and in a timely
189   // manner (e.g., about once per second).
190   uint32 lip_sync_rtp_timestamp_;
191   uint64 lip_sync_ntp_timestamp_;
192 
193   base::TimeDelta rtt_;
194   base::TimeDelta min_rtt_;
195   base::TimeDelta max_rtt_;
196   int number_of_rtt_in_avg_;
197   double avg_rtt_ms_;
198   uint16 target_delay_ms_;
199 
200   DISALLOW_COPY_AND_ASSIGN(Rtcp);
201 };
202 
203 }  // namespace cast
204 }  // namespace media
205 
206 #endif  // MEDIA_CAST_RTCP_RTCP_H_
207