Home | History | Annotate | Download | only in rtcp
      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 
     23 namespace media {
     24 namespace cast {
     25 
     26 class LocalRtcpReceiverFeedback;
     27 class LocalRtcpRttFeedback;
     28 class PacedPacketSender;
     29 class RtcpReceiver;
     30 class RtcpSender;
     31 
     32 typedef std::pair<uint32, base::TimeTicks> RtcpSendTimePair;
     33 typedef std::map<uint32, base::TimeTicks> RtcpSendTimeMap;
     34 typedef std::queue<RtcpSendTimePair> RtcpSendTimeQueue;
     35 
     36 class RtcpSenderFeedback {
     37  public:
     38   virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) = 0;
     39 
     40   virtual ~RtcpSenderFeedback() {}
     41 };
     42 
     43 class RtpSenderStatistics {
     44  public:
     45   virtual void GetStatistics(const base::TimeTicks& now,
     46                              RtcpSenderInfo* sender_info) = 0;
     47 
     48   virtual ~RtpSenderStatistics() {}
     49 };
     50 
     51 class RtpReceiverStatistics {
     52  public:
     53   virtual void GetStatistics(uint8* fraction_lost,
     54                              uint32* cumulative_lost,  // 24 bits valid.
     55                              uint32* extended_high_sequence_number,
     56                              uint32* jitter) = 0;
     57 
     58   virtual ~RtpReceiverStatistics() {}
     59 };
     60 
     61 class Rtcp {
     62  public:
     63   Rtcp(scoped_refptr<CastEnvironment> cast_environment,
     64        RtcpSenderFeedback* sender_feedback,
     65        PacedPacketSender* paced_packet_sender,
     66        RtpSenderStatistics* rtp_sender_statistics,
     67        RtpReceiverStatistics* rtp_receiver_statistics,
     68        RtcpMode rtcp_mode,
     69        const base::TimeDelta& rtcp_interval,
     70        uint32 local_ssrc,
     71        uint32 remote_ssrc,
     72        const std::string& c_name);
     73 
     74   virtual ~Rtcp();
     75 
     76   static bool IsRtcpPacket(const uint8* rtcp_buffer, size_t length);
     77 
     78   static uint32 GetSsrcOfSender(const uint8* rtcp_buffer, size_t length);
     79 
     80   base::TimeTicks TimeToSendNextRtcpReport();
     81   // |sender_log_message| is optional; without it no log messages will be
     82   // attached to the RTCP report; instead a normal RTCP send report will be
     83   // sent.
     84   // Additionally if all messages in |sender_log_message| does
     85   // not fit in the packet the |sender_log_message| will contain the remaining
     86   // unsent messages.
     87   void SendRtcpFromRtpSender(RtcpSenderLogMessage* sender_log_message);
     88 
     89   // |cast_message| and |receiver_log| is optional; if |cast_message| is
     90   // provided the RTCP receiver report will append a Cast message containing
     91   // Acks and Nacks; if |receiver_log| is provided the RTCP receiver report will
     92   // append the log messages. If no argument is set a normal RTCP receiver
     93   // report will be sent. Additionally if all messages in |receiver_log| does
     94   // not fit in the packet the |receiver_log| will contain the remaining unsent
     95   // messages.
     96   void SendRtcpFromRtpReceiver(const RtcpCastMessage* cast_message,
     97                                RtcpReceiverLogMessage* receiver_log);
     98 
     99   void IncomingRtcpPacket(const uint8* rtcp_buffer, size_t length);
    100   bool Rtt(base::TimeDelta* rtt, base::TimeDelta* avg_rtt,
    101            base::TimeDelta* min_rtt,  base::TimeDelta* max_rtt) const;
    102   bool RtpTimestampInSenderTime(int frequency,
    103                                 uint32 rtp_timestamp,
    104                                 base::TimeTicks* rtp_timestamp_in_ticks) const;
    105 
    106  protected:
    107   int CheckForWrapAround(uint32 new_timestamp,
    108                          uint32 old_timestamp) const;
    109 
    110   void OnReceivedLipSyncInfo(uint32 rtp_timestamp,
    111                              uint32 ntp_seconds,
    112                              uint32 ntp_fraction);
    113 
    114  private:
    115   friend class LocalRtcpRttFeedback;
    116   friend class LocalRtcpReceiverFeedback;
    117 
    118   void SendRtcp(const base::TimeTicks& now,
    119                 uint32 packet_type_flags,
    120                 uint32 media_ssrc,
    121                 const RtcpCastMessage* cast_message);
    122 
    123   void OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction);
    124 
    125   void OnReceivedDelaySinceLastReport(uint32 receivers_ssrc,
    126                                       uint32 last_report,
    127                                       uint32 delay_since_last_report);
    128 
    129   void OnReceivedSendReportRequest();
    130 
    131   void UpdateRtt(const base::TimeDelta& sender_delay,
    132                  const base::TimeDelta& receiver_delay);
    133 
    134   void UpdateNextTimeToSendRtcp();
    135 
    136   void SaveLastSentNtpTime(const base::TimeTicks& now, uint32 last_ntp_seconds,
    137                            uint32 last_ntp_fraction);
    138 
    139   scoped_refptr<CastEnvironment> cast_environment_;
    140   const base::TimeDelta rtcp_interval_;
    141   const RtcpMode rtcp_mode_;
    142   const uint32 local_ssrc_;
    143   const uint32 remote_ssrc_;
    144 
    145   // Not owned by this class.
    146   RtpSenderStatistics* const rtp_sender_statistics_;
    147   RtpReceiverStatistics* const rtp_receiver_statistics_;
    148 
    149   scoped_ptr<LocalRtcpRttFeedback> rtt_feedback_;
    150   scoped_ptr<LocalRtcpReceiverFeedback> receiver_feedback_;
    151   scoped_ptr<RtcpSender> rtcp_sender_;
    152   scoped_ptr<RtcpReceiver> rtcp_receiver_;
    153 
    154   base::TimeTicks next_time_to_send_rtcp_;
    155   RtcpSendTimeMap last_reports_sent_map_;
    156   RtcpSendTimeQueue last_reports_sent_queue_;
    157   base::TimeTicks time_last_report_received_;
    158   uint32 last_report_received_;
    159 
    160   uint32 last_received_rtp_timestamp_;
    161   uint32 last_received_ntp_seconds_;
    162   uint32 last_received_ntp_fraction_;
    163 
    164   base::TimeDelta rtt_;
    165   base::TimeDelta min_rtt_;
    166   base::TimeDelta max_rtt_;
    167   int number_of_rtt_in_avg_;
    168   float avg_rtt_ms_;
    169 
    170   DISALLOW_COPY_AND_ASSIGN(Rtcp);
    171 };
    172 
    173 }  // namespace cast
    174 }  // namespace media
    175 
    176 #endif  // MEDIA_CAST_RTCP_RTCP_H_
    177