Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_UTILITY_H_
     12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_UTILITY_H_
     13 
     14 #include <stddef.h> // size_t, ptrdiff_t
     15 
     16 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
     17 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
     18 #include "webrtc/typedefs.h"
     19 
     20 namespace webrtc {
     21 namespace RTCPUtility {
     22     uint32_t MidNtp(uint32_t ntp_sec, uint32_t ntp_frac);
     23 
     24     // CNAME
     25     struct RTCPCnameInformation
     26     {
     27         char name[RTCP_CNAME_SIZE];
     28     };
     29     struct RTCPPacketRR
     30     {
     31         uint32_t SenderSSRC;
     32         uint8_t  NumberOfReportBlocks;
     33     };
     34     struct RTCPPacketSR
     35     {
     36         uint32_t SenderSSRC;
     37         uint8_t  NumberOfReportBlocks;
     38 
     39         // sender info
     40         uint32_t NTPMostSignificant;
     41         uint32_t NTPLeastSignificant;
     42         uint32_t RTPTimestamp;
     43         uint32_t SenderPacketCount;
     44         uint32_t SenderOctetCount;
     45     };
     46     struct RTCPPacketReportBlockItem
     47     {
     48         // report block
     49         uint32_t SSRC;
     50         uint8_t  FractionLost;
     51         uint32_t CumulativeNumOfPacketsLost;
     52         uint32_t ExtendedHighestSequenceNumber;
     53         uint32_t Jitter;
     54         uint32_t LastSR;
     55         uint32_t DelayLastSR;
     56     };
     57     struct RTCPPacketSDESCName
     58     {
     59         // RFC3550
     60         uint32_t SenderSSRC;
     61         char CName[RTCP_CNAME_SIZE];
     62     };
     63 
     64     struct RTCPPacketExtendedJitterReportItem
     65     {
     66         // RFC 5450
     67         uint32_t Jitter;
     68     };
     69 
     70     struct RTCPPacketBYE
     71     {
     72         uint32_t SenderSSRC;
     73     };
     74     struct RTCPPacketXR
     75     {
     76         // RFC 3611
     77         uint32_t OriginatorSSRC;
     78     };
     79     struct RTCPPacketXRReceiverReferenceTimeItem
     80     {
     81         // RFC 3611 4.4
     82         uint32_t NTPMostSignificant;
     83         uint32_t NTPLeastSignificant;
     84     };
     85     struct RTCPPacketXRDLRRReportBlockItem
     86     {
     87         // RFC 3611 4.5
     88         uint32_t SSRC;
     89         uint32_t LastRR;
     90         uint32_t DelayLastRR;
     91     };
     92     struct RTCPPacketXRVOIPMetricItem
     93     {
     94         // RFC 3611 4.7
     95         uint32_t    SSRC;
     96         uint8_t     lossRate;
     97         uint8_t     discardRate;
     98         uint8_t     burstDensity;
     99         uint8_t     gapDensity;
    100         uint16_t    burstDuration;
    101         uint16_t    gapDuration;
    102         uint16_t    roundTripDelay;
    103         uint16_t    endSystemDelay;
    104         uint8_t     signalLevel;
    105         uint8_t     noiseLevel;
    106         uint8_t     RERL;
    107         uint8_t     Gmin;
    108         uint8_t     Rfactor;
    109         uint8_t     extRfactor;
    110         uint8_t     MOSLQ;
    111         uint8_t     MOSCQ;
    112         uint8_t     RXconfig;
    113         uint16_t    JBnominal;
    114         uint16_t    JBmax;
    115         uint16_t    JBabsMax;
    116     };
    117 
    118     struct RTCPPacketRTPFBNACK
    119     {
    120         uint32_t SenderSSRC;
    121         uint32_t MediaSSRC;
    122     };
    123     struct RTCPPacketRTPFBNACKItem
    124     {
    125         // RFC4585
    126         uint16_t PacketID;
    127         uint16_t BitMask;
    128     };
    129 
    130     struct RTCPPacketRTPFBTMMBR
    131     {
    132         uint32_t SenderSSRC;
    133         uint32_t MediaSSRC; // zero!
    134     };
    135     struct RTCPPacketRTPFBTMMBRItem
    136     {
    137         // RFC5104
    138         uint32_t SSRC;
    139         uint32_t MaxTotalMediaBitRate; // In Kbit/s
    140         uint32_t MeasuredOverhead;
    141     };
    142 
    143     struct RTCPPacketRTPFBTMMBN
    144     {
    145         uint32_t SenderSSRC;
    146         uint32_t MediaSSRC; // zero!
    147     };
    148     struct RTCPPacketRTPFBTMMBNItem
    149     {
    150         // RFC5104
    151         uint32_t SSRC; // "Owner"
    152         uint32_t MaxTotalMediaBitRate;
    153         uint32_t MeasuredOverhead;
    154     };
    155 
    156     struct RTCPPacketPSFBFIR
    157     {
    158         uint32_t SenderSSRC;
    159         uint32_t MediaSSRC; // zero!
    160     };
    161     struct RTCPPacketPSFBFIRItem
    162     {
    163         // RFC5104
    164         uint32_t SSRC;
    165         uint8_t  CommandSequenceNumber;
    166     };
    167 
    168     struct RTCPPacketPSFBPLI
    169     {
    170         // RFC4585
    171         uint32_t SenderSSRC;
    172         uint32_t MediaSSRC;
    173     };
    174 
    175     struct RTCPPacketPSFBSLI
    176     {
    177         // RFC4585
    178         uint32_t SenderSSRC;
    179         uint32_t MediaSSRC;
    180     };
    181     struct RTCPPacketPSFBSLIItem
    182     {
    183         // RFC4585
    184         uint16_t FirstMB;
    185         uint16_t NumberOfMB;
    186         uint8_t PictureId;
    187     };
    188     struct RTCPPacketPSFBRPSI
    189     {
    190         // RFC4585
    191         uint32_t SenderSSRC;
    192         uint32_t MediaSSRC;
    193         uint8_t  PayloadType;
    194         uint16_t NumberOfValidBits;
    195         uint8_t  NativeBitString[RTCP_RPSI_DATA_SIZE];
    196     };
    197     struct RTCPPacketPSFBAPP
    198     {
    199         uint32_t SenderSSRC;
    200         uint32_t MediaSSRC;
    201     };
    202     struct RTCPPacketPSFBREMBItem
    203     {
    204         uint32_t BitRate;
    205         uint8_t NumberOfSSRCs;
    206         uint32_t SSRCs[MAX_NUMBER_OF_REMB_FEEDBACK_SSRCS];
    207     };
    208     // generic name APP
    209     struct RTCPPacketAPP
    210     {
    211         uint8_t     SubType;
    212         uint32_t    Name;
    213         uint8_t     Data[kRtcpAppCode_DATA_SIZE];
    214         uint16_t    Size;
    215     };
    216 
    217     union RTCPPacket
    218     {
    219         RTCPPacketRR              RR;
    220         RTCPPacketSR              SR;
    221         RTCPPacketReportBlockItem ReportBlockItem;
    222 
    223         RTCPPacketSDESCName       CName;
    224         RTCPPacketBYE             BYE;
    225 
    226         RTCPPacketExtendedJitterReportItem ExtendedJitterReportItem;
    227 
    228         RTCPPacketRTPFBNACK       NACK;
    229         RTCPPacketRTPFBNACKItem   NACKItem;
    230 
    231         RTCPPacketPSFBPLI         PLI;
    232         RTCPPacketPSFBSLI         SLI;
    233         RTCPPacketPSFBSLIItem     SLIItem;
    234         RTCPPacketPSFBRPSI        RPSI;
    235         RTCPPacketPSFBAPP         PSFBAPP;
    236         RTCPPacketPSFBREMBItem    REMBItem;
    237 
    238         RTCPPacketRTPFBTMMBR      TMMBR;
    239         RTCPPacketRTPFBTMMBRItem  TMMBRItem;
    240         RTCPPacketRTPFBTMMBN      TMMBN;
    241         RTCPPacketRTPFBTMMBNItem  TMMBNItem;
    242         RTCPPacketPSFBFIR         FIR;
    243         RTCPPacketPSFBFIRItem     FIRItem;
    244 
    245         RTCPPacketXR               XR;
    246         RTCPPacketXRReceiverReferenceTimeItem XRReceiverReferenceTimeItem;
    247         RTCPPacketXRDLRRReportBlockItem XRDLRRReportBlockItem;
    248         RTCPPacketXRVOIPMetricItem XRVOIPMetricItem;
    249 
    250         RTCPPacketAPP             APP;
    251     };
    252 
    253     enum RTCPPacketTypes
    254     {
    255         kRtcpNotValidCode,
    256 
    257         // RFC3550
    258         kRtcpRrCode,
    259         kRtcpSrCode,
    260         kRtcpReportBlockItemCode,
    261 
    262         kRtcpSdesCode,
    263         kRtcpSdesChunkCode,
    264         kRtcpByeCode,
    265 
    266         // RFC5450
    267         kRtcpExtendedIjCode,
    268         kRtcpExtendedIjItemCode,
    269 
    270         // RFC4585
    271         kRtcpRtpfbNackCode,
    272         kRtcpRtpfbNackItemCode,
    273 
    274         kRtcpPsfbPliCode,
    275         kRtcpPsfbRpsiCode,
    276         kRtcpPsfbSliCode,
    277         kRtcpPsfbSliItemCode,
    278         kRtcpPsfbAppCode,
    279         kRtcpPsfbRembCode,
    280         kRtcpPsfbRembItemCode,
    281 
    282         // RFC5104
    283         kRtcpRtpfbTmmbrCode,
    284         kRtcpRtpfbTmmbrItemCode,
    285         kRtcpRtpfbTmmbnCode,
    286         kRtcpRtpfbTmmbnItemCode,
    287         kRtcpPsfbFirCode,
    288         kRtcpPsfbFirItemCode,
    289 
    290         // draft-perkins-avt-rapid-rtp-sync
    291         kRtcpRtpfbSrReqCode,
    292 
    293         // RFC 3611
    294         kRtcpXrHeaderCode,
    295         kRtcpXrReceiverReferenceTimeCode,
    296         kRtcpXrDlrrReportBlockCode,
    297         kRtcpXrDlrrReportBlockItemCode,
    298         kRtcpXrVoipMetricCode,
    299 
    300         kRtcpAppCode,
    301         kRtcpAppItemCode,
    302     };
    303 
    304     struct RTCPRawPacket
    305     {
    306         const uint8_t* _ptrPacketBegin;
    307         const uint8_t* _ptrPacketEnd;
    308     };
    309 
    310     struct RTCPModRawPacket
    311     {
    312         uint8_t* _ptrPacketBegin;
    313         uint8_t* _ptrPacketEnd;
    314     };
    315 
    316     struct RTCPCommonHeader
    317     {
    318         uint8_t  V;  // Version
    319         bool           P;  // Padding
    320         uint8_t  IC; // Item count/subtype
    321         uint8_t  PT; // Packet Type
    322         uint16_t LengthInOctets;
    323     };
    324 
    325     enum RTCPPT
    326     {
    327         PT_IJ    = 195,
    328         PT_SR    = 200,
    329         PT_RR    = 201,
    330         PT_SDES  = 202,
    331         PT_BYE   = 203,
    332         PT_APP   = 204,
    333         PT_RTPFB = 205,
    334         PT_PSFB  = 206,
    335         PT_XR    = 207
    336     };
    337 
    338     // Extended report blocks, RFC 3611.
    339     enum RtcpXrBlockType {
    340       kBtReceiverReferenceTime = 4,
    341       kBtDlrr = 5,
    342       kBtVoipMetric = 7
    343     };
    344 
    345     bool RTCPParseCommonHeader( const uint8_t* ptrDataBegin,
    346                                 const uint8_t* ptrDataEnd,
    347                                 RTCPCommonHeader& parsedHeader);
    348 
    349     class RTCPParserV2
    350     {
    351     public:
    352         RTCPParserV2(const uint8_t* rtcpData,
    353                      size_t rtcpDataLength,
    354                      bool rtcpReducedSizeEnable); // Set to true, to allow non-compound RTCP!
    355         ~RTCPParserV2();
    356 
    357         RTCPPacketTypes PacketType() const;
    358         const RTCPPacket& Packet() const;
    359         const RTCPRawPacket& RawPacket() const;
    360         ptrdiff_t LengthLeft() const;
    361 
    362         bool IsValid() const;
    363 
    364         RTCPPacketTypes Begin();
    365         RTCPPacketTypes Iterate();
    366 
    367     private:
    368         enum ParseState
    369         {
    370             State_TopLevel,        // Top level packet
    371             State_ReportBlockItem, // SR/RR report block
    372             State_SDESChunk,       // SDES chunk
    373             State_BYEItem,         // BYE item
    374             State_ExtendedJitterItem, // Extended jitter report item
    375             State_RTPFB_NACKItem,  // NACK FCI item
    376             State_RTPFB_TMMBRItem, // TMMBR FCI item
    377             State_RTPFB_TMMBNItem, // TMMBN FCI item
    378             State_PSFB_SLIItem,    // SLI FCI item
    379             State_PSFB_RPSIItem,   // RPSI FCI item
    380             State_PSFB_FIRItem,    // FIR FCI item
    381             State_PSFB_AppItem,    // Application specific FCI item
    382             State_PSFB_REMBItem,   // Application specific REMB item
    383             State_XRItem,
    384             State_XR_DLLRItem,
    385             State_AppItem
    386         };
    387 
    388     private:
    389         void IterateTopLevel();
    390         void IterateReportBlockItem();
    391         void IterateSDESChunk();
    392         void IterateBYEItem();
    393         void IterateExtendedJitterItem();
    394         void IterateNACKItem();
    395         void IterateTMMBRItem();
    396         void IterateTMMBNItem();
    397         void IterateSLIItem();
    398         void IterateRPSIItem();
    399         void IterateFIRItem();
    400         void IteratePsfbAppItem();
    401         void IteratePsfbREMBItem();
    402         void IterateAppItem();
    403         void IterateXrItem();
    404         void IterateXrDlrrItem();
    405 
    406         void Validate();
    407         void EndCurrentBlock();
    408 
    409         bool ParseRR();
    410         bool ParseSR();
    411         bool ParseReportBlockItem();
    412 
    413         bool ParseSDES();
    414         bool ParseSDESChunk();
    415         bool ParseSDESItem();
    416 
    417         bool ParseBYE();
    418         bool ParseBYEItem();
    419 
    420         bool ParseIJ();
    421         bool ParseIJItem();
    422 
    423         bool ParseXr();
    424         bool ParseXrItem();
    425         bool ParseXrReceiverReferenceTimeItem(int block_length_4bytes);
    426         bool ParseXrDlrr(int block_length_4bytes);
    427         bool ParseXrDlrrItem();
    428         bool ParseXrVoipMetricItem(int block_length_4bytes);
    429         bool ParseXrUnsupportedBlockType(int block_length_4bytes);
    430 
    431         bool ParseFBCommon(const RTCPCommonHeader& header);
    432         bool ParseNACKItem();
    433         bool ParseTMMBRItem();
    434         bool ParseTMMBNItem();
    435         bool ParseSLIItem();
    436         bool ParseRPSIItem();
    437         bool ParseFIRItem();
    438         bool ParsePsfbAppItem();
    439         bool ParsePsfbREMBItem();
    440 
    441         bool ParseAPP(const RTCPCommonHeader& header);
    442         bool ParseAPPItem();
    443 
    444     private:
    445         const uint8_t* const _ptrRTCPDataBegin;
    446         const bool                 _RTCPReducedSizeEnable;
    447         const uint8_t* const _ptrRTCPDataEnd;
    448 
    449         bool                     _validPacket;
    450         const uint8_t*     _ptrRTCPData;
    451         const uint8_t*     _ptrRTCPBlockEnd;
    452 
    453         ParseState               _state;
    454         uint8_t            _numberOfBlocks;
    455 
    456         RTCPPacketTypes          _packetType;
    457         RTCPPacket               _packet;
    458     };
    459 
    460     class RTCPPacketIterator
    461     {
    462     public:
    463         RTCPPacketIterator(uint8_t* rtcpData,
    464                             size_t rtcpDataLength);
    465         ~RTCPPacketIterator();
    466 
    467         const RTCPCommonHeader* Begin();
    468         const RTCPCommonHeader* Iterate();
    469         const RTCPCommonHeader* Current();
    470 
    471     private:
    472         uint8_t* const     _ptrBegin;
    473         uint8_t* const     _ptrEnd;
    474 
    475         uint8_t*           _ptrBlock;
    476 
    477         RTCPCommonHeader         _header;
    478     };
    479 }  // RTCPUtility
    480 }  // namespace webrtc
    481 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_UTILITY_H_
    482