3fef49cc283325828bc213789988b41cf565645e
[platform/framework/web/crosswalk.git] / src / media / cast / rtcp / rtcp.h
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 <list>
9 #include <map>
10 #include <queue>
11 #include <set>
12 #include <string>
13
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"
25
26 namespace media {
27 namespace cast {
28
29 class LocalRtcpReceiverFeedback;
30 class LocalRtcpRttFeedback;
31 class PacedPacketSender;
32 class ReceiverRtcpEventSubscriber;
33 class RtcpReceiver;
34 class RtcpSender;
35
36 typedef std::pair<uint32, base::TimeTicks> RtcpSendTimePair;
37 typedef std::map<uint32, base::TimeTicks> RtcpSendTimeMap;
38 typedef std::queue<RtcpSendTimePair> RtcpSendTimeQueue;
39
40 class RtcpSenderFeedback {
41  public:
42   virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) = 0;
43
44   virtual ~RtcpSenderFeedback() {}
45 };
46
47 class RtpSenderStatistics {
48  public:
49   virtual void GetStatistics(const base::TimeTicks& now,
50                              transport::RtcpSenderInfo* sender_info) = 0;
51
52   virtual ~RtpSenderStatistics() {}
53 };
54
55 class RtpReceiverStatistics {
56  public:
57   virtual void GetStatistics(uint8* fraction_lost,
58                              uint32* cumulative_lost,  // 24 bits valid.
59                              uint32* extended_high_sequence_number,
60                              uint32* jitter) = 0;
61
62   virtual ~RtpReceiverStatistics() {}
63 };
64
65 class Rtcp {
66  public:
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,
76        RtcpMode rtcp_mode,
77        const base::TimeDelta& rtcp_interval,
78        uint32 local_ssrc,
79        uint32 remote_ssrc,
80        const std::string& c_name);
81
82   virtual ~Rtcp();
83
84   static bool IsRtcpPacket(const uint8* rtcp_buffer, size_t length);
85
86   static uint32 GetSsrcOfSender(const uint8* rtcp_buffer, size_t length);
87
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
91   // sent.
92   // Additionally if all messages in |sender_log_message| does
93   // not fit in the packet the |sender_log_message| will contain the remaining
94   // unsent messages.
95   void SendRtcpFromRtpSender(
96       const transport::RtcpSenderLogMessage& sender_log_message);
97
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);
104
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;
113
114  protected:
115   int CheckForWrapAround(uint32 new_timestamp, uint32 old_timestamp) const;
116
117   void OnReceivedLipSyncInfo(uint32 rtp_timestamp,
118                              uint32 ntp_seconds,
119                              uint32 ntp_fraction);
120
121  private:
122   friend class LocalRtcpRttFeedback;
123   friend class LocalRtcpReceiverFeedback;
124
125   void SendRtcp(const base::TimeTicks& now,
126                 uint32 packet_type_flags,
127                 uint32 media_ssrc,
128                 const RtcpCastMessage* cast_message);
129
130   void OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction);
131
132   void OnReceivedDelaySinceLastReport(uint32 receivers_ssrc,
133                                       uint32 last_report,
134                                       uint32 delay_since_last_report);
135
136   void OnReceivedSendReportRequest();
137
138   void UpdateRtt(const base::TimeDelta& sender_delay,
139                  const base::TimeDelta& receiver_delay);
140
141   void UpdateNextTimeToSendRtcp();
142
143   void SaveLastSentNtpTime(const base::TimeTicks& now,
144                            uint32 last_ntp_seconds,
145                            uint32 last_ntp_fraction);
146
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,
152       uint32 sending_ssrc,
153       std::string c_name);
154
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_;
162
163   // Not owned by this class.
164   RtpSenderStatistics* const rtp_sender_statistics_;
165   RtpReceiverStatistics* const rtp_receiver_statistics_;
166
167   scoped_ptr<LocalRtcpRttFeedback> rtt_feedback_;
168   scoped_ptr<LocalRtcpReceiverFeedback> receiver_feedback_;
169   scoped_ptr<RtcpSender> rtcp_sender_;
170   scoped_ptr<RtcpReceiver> rtcp_receiver_;
171
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_;
177
178   uint32 last_received_rtp_timestamp_;
179   uint32 last_received_ntp_seconds_;
180   uint32 last_received_ntp_fraction_;
181
182   base::TimeDelta rtt_;
183   base::TimeDelta min_rtt_;
184   base::TimeDelta max_rtt_;
185   int number_of_rtt_in_avg_;
186   float avg_rtt_ms_;
187
188   DISALLOW_COPY_AND_ASSIGN(Rtcp);
189 };
190
191 }  // namespace cast
192 }  // namespace media
193
194 #endif  // MEDIA_CAST_RTCP_RTCP_H_