- add sources.
[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 <set>
11 #include <string>
12
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/tick_clock.h"
16 #include "base/time/time.h"
17 #include "media/cast/cast_config.h"
18 #include "media/cast/cast_defines.h"
19 #include "media/cast/rtcp/rtcp_defines.h"
20
21 namespace media {
22 namespace cast {
23
24 class LocalRtcpReceiverFeedback;
25 class LocalRtcpRttFeedback;
26 class PacedPacketSender;
27 class RtcpReceiver;
28 class RtcpSender;
29
30 class RtcpSenderFeedback {
31  public:
32   virtual void OnReceivedReportBlock(const RtcpReportBlock& report_block) = 0;
33
34   virtual void OnReceivedIntraFrameRequest() = 0;
35
36   virtual void OnReceivedRpsi(uint8 payload_type, uint64 picture_id) = 0;
37
38   virtual void OnReceivedRemb(uint32 bitrate) = 0;
39
40   virtual void OnReceivedNackRequest(
41       const std::list<uint16>& nack_sequence_numbers) = 0;
42
43   virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) = 0;
44
45   virtual ~RtcpSenderFeedback() {}
46 };
47
48 class RtpSenderStatistics {
49  public:
50   virtual void GetStatistics(const base::TimeTicks& now,
51                              RtcpSenderInfo* sender_info) = 0;
52
53   virtual ~RtpSenderStatistics() {}
54 };
55
56 class RtpReceiverStatistics {
57  public:
58   virtual void GetStatistics(uint8* fraction_lost,
59                              uint32* cumulative_lost,  // 24 bits valid.
60                              uint32* extended_high_sequence_number,
61                              uint32* jitter) = 0;
62
63   virtual ~RtpReceiverStatistics() {}
64 };
65
66 class Rtcp {
67  public:
68   Rtcp(base::TickClock* clock,
69        RtcpSenderFeedback* sender_feedback,
70        PacedPacketSender* paced_packet_sender,
71        RtpSenderStatistics* rtp_sender_statistics,
72        RtpReceiverStatistics* rtp_receiver_statistics,
73        RtcpMode rtcp_mode,
74        const base::TimeDelta& rtcp_interval,
75        bool sending_media,
76        uint32 local_ssrc,
77        const std::string& c_name);
78
79   virtual ~Rtcp();
80
81   static bool IsRtcpPacket(const uint8* rtcp_buffer, size_t length);
82
83   static uint32 GetSsrcOfSender(const uint8* rtcp_buffer, size_t length);
84
85   base::TimeTicks TimeToSendNextRtcpReport();
86   void SendRtcpReport(uint32 media_ssrc);
87   void SendRtcpPli(uint32 media_ssrc);
88   void SendRtcpCast(const RtcpCastMessage& cast_message);
89   void SetRemoteSSRC(uint32 ssrc);
90
91   void IncomingRtcpPacket(const uint8* rtcp_buffer, size_t length);
92   bool Rtt(base::TimeDelta* rtt, base::TimeDelta* avg_rtt,
93            base::TimeDelta* min_rtt,  base::TimeDelta* max_rtt) const;
94   bool RtpTimestampInSenderTime(int frequency,
95                                 uint32 rtp_timestamp,
96                                 base::TimeTicks* rtp_timestamp_in_ticks) const;
97
98  protected:
99   int CheckForWrapAround(uint32 new_timestamp,
100                          uint32 old_timestamp) const;
101
102   void OnReceivedLipSyncInfo(uint32 rtp_timestamp,
103                              uint32 ntp_seconds,
104                              uint32 ntp_fraction);
105
106  private:
107   friend class LocalRtcpRttFeedback;
108   friend class LocalRtcpReceiverFeedback;
109
110   void SendRtcp(const base::TimeTicks& now,
111                 uint32 packet_type_flags,
112                 uint32 media_ssrc,
113                 const RtcpCastMessage* cast_message);
114
115   void OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction);
116
117   void OnReceivedDelaySinceLastReport(uint32 receivers_ssrc,
118                                       uint32 last_report,
119                                       uint32 delay_since_last_report);
120
121   void OnReceivedSendReportRequest();
122
123   void UpdateRtt(const base::TimeDelta& sender_delay,
124                  const base::TimeDelta& receiver_delay);
125
126   void UpdateNextTimeToSendRtcp();
127
128   base::TickClock* const clock_;  // Not owned by this class.
129   const base::TimeDelta rtcp_interval_;
130   const RtcpMode rtcp_mode_;
131   const bool sending_media_;
132   const uint32 local_ssrc_;
133
134   // Not owned by this class.
135   RtpSenderStatistics* const rtp_sender_statistics_;
136   RtpReceiverStatistics* const rtp_receiver_statistics_;
137
138   scoped_ptr<LocalRtcpRttFeedback> rtt_feedback_;
139   scoped_ptr<LocalRtcpReceiverFeedback> receiver_feedback_;
140   scoped_ptr<RtcpSender> rtcp_sender_;
141   scoped_ptr<RtcpReceiver> rtcp_receiver_;
142
143   base::TimeTicks next_time_to_send_rtcp_;
144
145   base::TimeTicks time_last_report_sent_;
146   uint32 last_report_sent_;
147
148   base::TimeTicks time_last_report_received_;
149   uint32 last_report_received_;
150
151   uint32 last_received_rtp_timestamp_;
152   uint32 last_received_ntp_seconds_;
153   uint32 last_received_ntp_fraction_;
154
155   base::TimeDelta rtt_;
156   base::TimeDelta min_rtt_;
157   base::TimeDelta max_rtt_;
158   int number_of_rtt_in_avg_;
159   float avg_rtt_ms_;
160
161   DISALLOW_COPY_AND_ASSIGN(Rtcp);
162 };
163
164 }  // namespace cast
165 }  // namespace media
166
167 #endif  // MEDIA_CAST_RTCP_RTCP_H_