ccRTP
rtcppkt.h
Go to the documentation of this file.
00001 // Copyright (C) 2001,2002,2004,2007 Federico Montesino Pouzols <fedemp@altern.org>.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 //
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // ccRTP.  If you copy code from other releases into a copy of GNU
00028 // ccRTP, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU ccRTP, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00038 #ifndef CCXX_RTP_RTCPPKT_H_
00039 #define CCXX_RTP_RTCPPKT_H_
00040 
00041 #include <ccrtp/base.h>
00042 
00043 NAMESPACE_COMMONCPP
00044 
00065 typedef enum
00066 {
00067     SDESItemTypeEND = 0,         
00068     SDESItemTypeCNAME,           
00069     SDESItemTypeNAME,            
00070     SDESItemTypeEMAIL,           
00071     SDESItemTypePHONE,           
00072     SDESItemTypeLOC,             
00073     SDESItemTypeTOOL,            
00074     SDESItemTypeNOTE,            
00075     SDESItemTypePRIV,            
00076     SDESItemTypeH323CADDR,       
00077     SDESItemTypeLast = SDESItemTypeH323CADDR 
00078 }       SDESItemType;
00079 
00090 class __EXPORT RTCPCompoundHandler
00091 {
00092 public:
00093     inline void setPathMTU(uint16 mtu)
00094     { pathMTU = mtu; }
00095 
00096     inline uint16 getPathMTU()
00097     { return pathMTU; }
00098 
00099 #ifdef  CCXX_PACKED
00100 #pragma pack(1)
00101 #endif
00102 
00109     struct ReceiverInfo
00110     {
00111         uint8 fractionLost;      
00112         uint8 lostMSB;           
00113         uint16 lostLSW;          
00114         uint32 highestSeqNum;    
00115         uint32 jitter;           
00116         uint32 lsr;              
00117         uint32 dlsr;             
00118     };
00119 
00126     struct RRBlock
00127     {
00128         uint32 ssrc;                   
00129         ReceiverInfo rinfo;            
00130     };
00131 
00138     struct RecvReport
00139     {
00140         uint32 ssrc;                 
00141         RRBlock blocks[1];           
00142     };
00143 
00150     struct SenderInfo
00151     {
00152         uint32 NTPMSW;              
00153         uint32 NTPLSW;              
00154         uint32 RTPTimestamp;        
00155         uint32 packetCount;         
00156         uint32 octetCount;          
00157     };
00158 
00164     struct SendReport
00165     {
00166         uint32 ssrc;       
00167         SenderInfo sinfo;  
00168         RRBlock blocks[1]; 
00169     };
00170 
00176     struct SDESItem
00177     {
00178         uint8 type;       
00179         uint8 len;        
00180         char data[1];     
00181     };
00182 
00188     struct SDESChunk
00189     {
00190         uint32 getSSRC() const
00191         { return (ntohl(ssrc)); }
00192 
00193         uint32 ssrc;      
00194         SDESItem item;    
00195     };
00196 
00202     struct BYEPacket
00203     {
00204         uint32 ssrc;          
00205         uint8 length;         
00206     };
00207 
00213     struct APPPacket
00214     {
00215         uint32 ssrc;           
00216         char name [4];         
00217 
00218 
00219         unsigned char data[1]; 
00220     };
00221 
00228     struct FIRPacket
00229     {
00230         uint32 ssrc;           
00231     };
00232 
00239     struct NACKPacket
00240     {
00241         uint32 ssrc;           
00242         uint16 fsn;            
00243         uint16 blp;            
00244     };
00245 
00251     struct RTCPFixedHeader
00252     {
00253 #if __BYTE_ORDER == __BIG_ENDIAN
00254 
00255         unsigned char version:2;      
00256         unsigned char padding:1;      
00257         unsigned char block_count:5;  
00258 #else
00259 
00260         unsigned char block_count:5;  
00261         unsigned char padding:1;      
00262         unsigned char version:2;      
00263 #endif
00264         uint8 type;    
00265         uint16 length; 
00266     };
00267 
00278     struct RTCPPacket
00279     {
00285         typedef enum {
00286             tSR = 200,      
00287             tRR,            
00288             tSDES,          
00289             tBYE,           
00290             tAPP,           
00291             tFIR   = 192,   
00292             tNACK  = 193,   
00293             tXR             
00294         }       Type;
00295 
00300         uint32 getLength() const
00301         { return ((ntohs(fh.length) + 1) << 2); }
00302 
00307         uint32 getSSRC()  const
00308         { return (ntohl(info.RR.ssrc)); } // SSRC is always the first
00309                           // word after fh.
00310 
00311         RTCPFixedHeader fh;           
00312 
00313         // An RTCP packet may be of any of the types defined
00314         // above, including APP specific ones.
00315         union
00316         {
00317             SendReport SR;
00318             RecvReport RR;
00319             SDESChunk SDES;
00320             BYEPacket BYE;
00321             APPPacket APP;
00322             NACKPacket NACK;
00323             FIRPacket FIR;
00324         }       info;        
00325     };
00326 #ifdef  CCXX_PACKED
00327 #pragma pack()
00328 #endif
00329 
00330 protected:
00331     enum { defaultPathMTU = 1500 };
00332 
00333     RTCPCompoundHandler(uint16 mtu = defaultPathMTU);
00334 
00335     ~RTCPCompoundHandler();
00336 
00348     bool
00349     checkCompoundRTCPHeader(size_t len);
00350 
00351     // buffer to hold RTCP compound packets being sent. Allocated
00352     // in construction time
00353     unsigned char* rtcpSendBuffer;
00354     // buffer to hold RTCP compound packets being
00355     // received. Allocated at construction time
00356     unsigned char* rtcpRecvBuffer;
00357 
00358     friend class RTCPSenderInfo;
00359     friend class RTCPReceiverInfo;
00360 private:
00361     // path MTU. RTCP packets should not be greater than this
00362     uint16 pathMTU;
00363     // masks for RTCP header validation;
00364     static const uint16 RTCP_VALID_MASK;
00365     static const uint16 RTCP_VALID_VALUE;
00366 };
00367 
00374 class __EXPORT RTCPReceiverInfo
00375 {
00376 public:
00377     RTCPReceiverInfo(void* ri)
00378     { memcpy(&receiverInfo,&ri,
00379          sizeof(RTCPCompoundHandler::ReceiverInfo));}
00380 
00381         RTCPReceiverInfo(RTCPCompoundHandler::ReceiverInfo& si)
00382         : receiverInfo( si )
00383         {
00384         }
00385 
00386     ~RTCPReceiverInfo()
00387     { }
00388 
00393     inline uint8
00394     getFractionLost() const
00395     { return receiverInfo.fractionLost; }
00396 
00397     inline uint32
00398     getCumulativePacketLost() const
00399     { return ( ((uint32)ntohs(receiverInfo.lostLSW)) +
00400            (((uint32)receiverInfo.lostMSB) << 16) ); }
00401 
00402     inline uint32
00403     getExtendedSeqNum() const
00404     { return ntohl(receiverInfo.highestSeqNum); }
00405 
00412     uint32
00413     getJitter() const
00414     { return ntohl(receiverInfo.jitter); }
00415 
00421     uint16
00422     getLastSRNTPTimestampInt() const
00423     { return (uint16)((ntohl(receiverInfo.lsr) & 0xFFFF0000) >> 16); }
00424 
00430     uint16
00431     getLastSRNTPTimestampFrac() const
00432     { return (uint16)(ntohl(receiverInfo.lsr) & 0xFFFF); }
00433 
00440     uint32
00441     getDelayLastSR() const
00442     { return ntohl(receiverInfo.dlsr); }
00443 
00444 private:
00445     RTCPCompoundHandler::ReceiverInfo receiverInfo;
00446 };
00447 
00454 class __EXPORT RTCPSenderInfo
00455 {
00456 public:
00457     RTCPSenderInfo(void* si)
00458     { memcpy(&senderInfo,&si,
00459          sizeof(RTCPCompoundHandler::SenderInfo));}
00460 
00461         RTCPSenderInfo(RTCPCompoundHandler::SenderInfo& si)
00462         : senderInfo( si )
00463         {
00464         }
00465 
00466     ~RTCPSenderInfo()
00467     { }
00468 
00473     uint32
00474     getNTPTimestampInt() const
00475     { return ntohl(senderInfo.NTPMSW); }
00476 
00481     uint32
00482     getNTPTimestampFrac() const
00483     { return ntohl(senderInfo.NTPLSW); }
00484 
00485     inline uint32
00486     getRTPTimestamp() const
00487     { return ntohl(senderInfo.RTPTimestamp); }
00488 
00492     inline uint32
00493     getPacketCount() const
00494     { return ntohl(senderInfo.packetCount); }
00495 
00496     inline uint32
00497     getOctetCount() const
00498     { return ntohl(senderInfo.octetCount); }
00499 
00500 private:
00501     RTCPCompoundHandler::SenderInfo senderInfo;
00502 };
00503 
00512 timeval
00513 NTP2Timeval(uint32 msw, uint32 lsw);
00514 
00522 uint32
00523 timevalIntervalTo65536(timeval& t);
00524  // rtcppacket
00526 
00527 END_NAMESPACE
00528 
00529 #endif  // ndef CCXX_RTP_RTCPPKT_H_
00530