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_UTILITY_H_
      6 #define MEDIA_CAST_RTCP_RTCP_UTILITY_H_
      7 
      8 #include "media/cast/cast_config.h"
      9 #include "media/cast/cast_defines.h"
     10 #include "media/cast/rtcp/rtcp_defines.h"
     11 
     12 namespace media {
     13 namespace cast {
     14 
     15 static const int kRtcpRpsiDataSize = 30;
     16 
     17 // RFC 3550 page 44, including end null.
     18 static const size_t kRtcpCnameSize = 256;
     19 static const int kRtcpMaxNumberOfRembFeedbackSsrcs = 255;
     20 
     21 static const uint32 kRemb = ('R' << 24) + ('E' << 16) + ('M' << 8) + 'B';
     22 static const uint32 kCast = ('C' << 24) + ('A' << 16) + ('S' << 8) + 'T';
     23 
     24 static const uint8 kSenderLogSubtype = 1;
     25 static const uint8 kReceiverLogSubtype = 2;
     26 
     27 static const size_t kRtcpMaxReceiverLogMessages = 256;
     28 static const size_t kRtcpMaxNackFields = 253;
     29 static const size_t kRtcpMaxCastLossFields = 100;
     30 
     31 struct RtcpFieldReceiverReport {
     32   // RFC 3550.
     33   uint32 sender_ssrc;
     34   uint8 number_of_report_blocks;
     35 };
     36 
     37 struct RtcpFieldSenderReport {
     38   // RFC 3550.
     39   uint32 sender_ssrc;
     40   uint8  number_of_report_blocks;
     41   uint32 ntp_most_significant;
     42   uint32 ntp_least_significant;
     43   uint32 rtp_timestamp;
     44   uint32 sender_packet_count;
     45   uint32 sender_octet_count;
     46 };
     47 
     48 struct RtcpFieldReportBlockItem {
     49   // RFC 3550.
     50   uint32 ssrc;
     51   uint8  fraction_lost;
     52   uint32 cumulative_number_of_packets_lost;
     53   uint32 extended_highest_sequence_number;
     54   uint32 jitter;
     55   uint32 last_sender_report;
     56   uint32 delay_last_sender_report;
     57 };
     58 
     59 struct RtcpFieldSdesCName {
     60   // RFC 3550
     61   uint32 sender_ssrc;
     62   char name[kRtcpCnameSize];
     63 };
     64 
     65 struct RtcpFieldBye {
     66   // RFC 3550.
     67   uint32 sender_ssrc;
     68 };
     69 
     70 struct RtcpFieldGenericRtpFeedbackNack {
     71   // RFC 4585.
     72   uint32 sender_ssrc;
     73   uint32 media_ssrc;
     74 };
     75 
     76 struct RtcpFieldGenericRtpFeedbackNackItem {
     77   // RFC 4585.
     78   uint16 packet_id;
     79   uint16 bitmask;
     80 };
     81 
     82 struct RtcpFieldPayloadSpecificFir {
     83   // RFC 5104.
     84   uint32 sender_ssrc;
     85   uint32 media_ssrc;  // zero!
     86 };
     87 
     88 struct RtcpFieldPayloadSpecificFirItem {
     89   // RFC 5104.
     90   uint32 ssrc;
     91   uint8 command_sequence_number;
     92 };
     93 
     94 struct RtcpFieldPayloadSpecificPli {
     95   // RFC 4585.
     96   uint32 sender_ssrc;
     97   uint32 media_ssrc;
     98 };
     99 
    100 struct RtcpFieldPayloadSpecificRpsi {
    101   // RFC 4585.
    102   uint32 sender_ssrc;
    103   uint32 media_ssrc;
    104   uint8  payload_type;
    105   uint16 number_of_valid_bits;
    106   uint8  native_bit_string[kRtcpRpsiDataSize];
    107 };
    108 
    109 struct RtcpFieldXr {
    110   // RFC 3611.
    111   uint32 sender_ssrc;
    112 };
    113 
    114 struct RtcpFieldXrRrtr {
    115   // RFC 3611.
    116   uint32 ntp_most_significant;
    117   uint32 ntp_least_significant;
    118 };
    119 
    120 struct RtcpFieldXrDlrr {
    121   // RFC 3611.
    122   uint32 receivers_ssrc;
    123   uint32 last_receiver_report;
    124   uint32 delay_last_receiver_report;
    125 };
    126 
    127 struct RtcpFieldPayloadSpecificApplication {
    128   uint32 sender_ssrc;
    129   uint32 media_ssrc;
    130 };
    131 
    132 struct RtcpFieldPayloadSpecificRembItem {
    133   uint32 bitrate;
    134   uint8 number_of_ssrcs;
    135   uint32 ssrcs[kRtcpMaxNumberOfRembFeedbackSsrcs];
    136 };
    137 
    138 struct RtcpFieldPayloadSpecificCastItem {
    139   uint8 last_frame_id;
    140   uint8 number_of_lost_fields;
    141 };
    142 
    143 struct RtcpFieldPayloadSpecificCastNackItem {
    144   uint8 frame_id;
    145   uint16 packet_id;
    146   uint8 bitmask;
    147 };
    148 
    149 struct RtcpFieldApplicationSpecificCastReceiverLogItem {
    150   uint32 sender_ssrc;
    151   uint32 rtp_timestamp;
    152   uint32 event_timestamp_base;
    153   uint8 event;
    154   uint16 delay_delta_or_packet_id;
    155   uint16 event_timestamp_delta;
    156 };
    157 
    158 struct RtcpFieldApplicationSpecificCastSenderLogItem {
    159   uint32 sender_ssrc;
    160   uint8 status;
    161   uint32 rtp_timestamp;
    162 };
    163 
    164 union RtcpField {
    165   RtcpFieldReceiverReport               receiver_report;
    166   RtcpFieldSenderReport                 sender_report;
    167   RtcpFieldReportBlockItem              report_block_item;
    168   RtcpFieldSdesCName                    c_name;
    169   RtcpFieldBye                          bye;
    170 
    171   RtcpFieldXr                           extended_report;
    172   RtcpFieldXrRrtr                       rrtr;
    173   RtcpFieldXrDlrr                       dlrr;
    174 
    175   RtcpFieldGenericRtpFeedbackNack       nack;
    176   RtcpFieldGenericRtpFeedbackNackItem   nack_item;
    177 
    178   RtcpFieldPayloadSpecificPli           pli;
    179   RtcpFieldPayloadSpecificRpsi          rpsi;
    180   RtcpFieldPayloadSpecificFir           fir;
    181   RtcpFieldPayloadSpecificFirItem       fir_item;
    182   RtcpFieldPayloadSpecificApplication   application_specific;
    183   RtcpFieldPayloadSpecificRembItem      remb_item;
    184   RtcpFieldPayloadSpecificCastItem      cast_item;
    185   RtcpFieldPayloadSpecificCastNackItem  cast_nack_item;
    186 
    187   RtcpFieldApplicationSpecificCastReceiverLogItem cast_receiver_log;
    188   RtcpFieldApplicationSpecificCastSenderLogItem cast_sender_log;
    189 };
    190 
    191 enum RtcpFieldTypes {
    192   kRtcpNotValidCode,
    193 
    194   // RFC 3550.
    195   kRtcpRrCode,
    196   kRtcpSrCode,
    197   kRtcpReportBlockItemCode,
    198 
    199   kRtcpSdesCode,
    200   kRtcpSdesChunkCode,
    201   kRtcpByeCode,
    202 
    203   // RFC 3611.
    204   kRtcpXrCode,
    205   kRtcpXrRrtrCode,
    206   kRtcpXrDlrrCode,
    207   kRtcpXrUnknownItemCode,
    208 
    209   // RFC 4585.
    210   kRtcpGenericRtpFeedbackNackCode,
    211   kRtcpGenericRtpFeedbackNackItemCode,
    212 
    213   kRtcpPayloadSpecificPliCode,
    214   kRtcpPayloadSpecificRpsiCode,
    215   kRtcpPayloadSpecificAppCode,
    216 
    217   // Application specific.
    218   kRtcpPayloadSpecificRembCode,
    219   kRtcpPayloadSpecificRembItemCode,
    220   kRtcpPayloadSpecificCastCode,
    221   kRtcpPayloadSpecificCastNackItemCode,
    222   kRtcpApplicationSpecificCastReceiverLogCode,
    223   kRtcpApplicationSpecificCastReceiverLogFrameCode,
    224   kRtcpApplicationSpecificCastReceiverLogEventCode,
    225   kRtcpApplicationSpecificCastSenderLogCode,
    226 
    227   // RFC 5104.
    228   kRtcpPayloadSpecificFirCode,
    229   kRtcpPayloadSpecificFirItemCode,
    230 
    231   // RFC 6051.
    232   kRtcpGenericRtpFeedbackSrReqCode,
    233 };
    234 
    235 struct RtcpCommonHeader {
    236   uint8  V;   // Version.
    237   bool   P;   // Padding.
    238   uint8  IC;  // Item count / subtype.
    239   uint8  PT;  // Packet Type.
    240   uint16 length_in_octets;
    241 };
    242 
    243 enum RtcpPacketTypes {
    244   kPacketTypeLow = 194,  // SMPTE time-code mapping.
    245   kPacketTypeInterArrivalJitterReport = 195,
    246   kPacketTypeSenderReport = 200,
    247   kPacketTypeReceiverReport = 201,
    248   kPacketTypeSdes = 202,
    249   kPacketTypeBye = 203,
    250   kPacketTypeApplicationDefined = 204,
    251   kPacketTypeGenericRtpFeedback = 205,
    252   kPacketTypePayloadSpecific = 206,
    253   kPacketTypeXr = 207,
    254   kPacketTypeHigh = 210,  // Port Mapping.
    255 };
    256 
    257 class RtcpParser {
    258  public:
    259   RtcpParser(const uint8* rtcp_data, size_t rtcp_length);
    260   ~RtcpParser();
    261 
    262   RtcpFieldTypes FieldType() const;
    263   const RtcpField& Field() const;
    264 
    265   bool IsValid() const;
    266 
    267   RtcpFieldTypes Begin();
    268   RtcpFieldTypes Iterate();
    269 
    270  private:
    271   enum ParseState {
    272     kStateTopLevel,     // Top level packet
    273     kStateReportBlock,  // Sender/Receiver report report blocks.
    274     kStateSdes,
    275     kStateBye,
    276     kStateApplicationSpecificCastReceiverFrameLog,
    277     kStateApplicationSpecificCastReceiverEventLog,
    278     kStateApplicationSpecificCastSenderLog,
    279     kStateExtendedReportBlock,
    280     kStateExtendedReportDelaySinceLastReceiverReport,
    281     kStateGenericRtpFeedbackNack,
    282     kStatePayloadSpecificRpsi,
    283     kStatePayloadSpecificFir,
    284     kStatePayloadSpecificApplication,
    285     kStatePayloadSpecificRemb,      // Application specific Remb.
    286     kStatePayloadSpecificCast,      // Application specific Cast.
    287     kStatePayloadSpecificCastNack,  // Application specific Nack for Cast.
    288   };
    289 
    290   bool RtcpParseCommonHeader(const uint8* begin,
    291                              const uint8* end,
    292                              RtcpCommonHeader* parsed_header) const;
    293 
    294   void IterateTopLevel();
    295   void IterateReportBlockItem();
    296   void IterateSdesItem();
    297   void IterateByeItem();
    298   void IterateCastReceiverLogFrame();
    299   void IterateCastReceiverLogEvent();
    300   void IterateCastSenderLog();
    301   void IterateExtendedReportItem();
    302   void IterateExtendedReportDelaySinceLastReceiverReportItem();
    303   void IterateNackItem();
    304   void IterateRpsiItem();
    305   void IterateFirItem();
    306   void IteratePayloadSpecificAppItem();
    307   void IteratePayloadSpecificRembItem();
    308   void IteratePayloadSpecificCastItem();
    309   void IteratePayloadSpecificCastNackItem();
    310 
    311   void Validate();
    312   void EndCurrentBlock();
    313 
    314   bool ParseRR();
    315   bool ParseSR();
    316   bool ParseReportBlockItem();
    317 
    318   bool ParseSdes();
    319   bool ParseSdesItem();
    320   bool ParseSdesTypes();
    321   bool ParseBye();
    322   bool ParseByeItem();
    323   bool ParseApplicationDefined(uint8 subtype);
    324   bool ParseCastReceiverLogFrameItem();
    325   bool ParseCastReceiverLogEventItem();
    326   bool ParseCastSenderLogItem();
    327 
    328   bool ParseExtendedReport();
    329   bool ParseExtendedReportItem();
    330   bool ParseExtendedReportReceiverReferenceTimeReport();
    331   bool ParseExtendedReportDelaySinceLastReceiverReport();
    332 
    333   bool ParseFeedBackCommon(const RtcpCommonHeader& header);
    334   bool ParseNackItem();
    335   bool ParseRpsiItem();
    336   bool ParseFirItem();
    337   bool ParsePayloadSpecificAppItem();
    338   bool ParsePayloadSpecificRembItem();
    339   bool ParsePayloadSpecificCastItem();
    340   bool ParsePayloadSpecificCastNackItem();
    341 
    342  private:
    343   const uint8* const rtcp_data_begin_;
    344   const uint8* const rtcp_data_end_;
    345 
    346   bool valid_packet_;
    347   const uint8* rtcp_data_;
    348   const uint8* rtcp_block_end_;
    349 
    350   ParseState state_;
    351   uint8 number_of_blocks_;
    352   RtcpFieldTypes field_type_;
    353   RtcpField field_;
    354 
    355   DISALLOW_COPY_AND_ASSIGN(RtcpParser);
    356 };
    357 
    358 }  // namespace cast
    359 }  // namespace media
    360 
    361 #endif  // MEDIA_CAST_RTCP_RTCP_UTILITY_H_
    362