rtppkt.h

Go to the documentation of this file.
00001 // Copyright (C) 2002 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_RTPPKT_H_
00039 #define CCXX_RTP_RTPPKT_H_
00040 
00041 #include <ccrtp/base.h>
00042 #include <ccrtp/formats.h>
00043 
00044 #ifdef CCXX_NAMESPACES
00045 namespace ost {
00046 #endif
00047 
00071 class  __EXPORT RTPPacket
00072 {
00073 private:
00074         struct RTPFixedHeader;
00075         struct RTPHeaderExt;
00076                 
00077 public:
00090         RTPPacket(const unsigned char* const block, size_t len, 
00091                   bool duplicate = false);
00092 
00104         RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen);
00105 
00112         inline uint32
00113         getHeaderSize() const
00114         { return hdrSize; }
00115                 
00119         inline const uint8* const
00120         getPayload() const
00121         { return (uint8*)(buffer + getHeaderSize()); }
00122                 
00126         inline uint32
00127         getPayloadSize() const
00128         { return payloadSize; }
00129                 
00133         inline PayloadType
00134         getPayloadType() const
00135         { return static_cast<PayloadType>(getHeader()->payload); }
00136                 
00140         inline uint16
00141         getSeqNum() const
00142         { return cachedSeqNum; }
00143 
00147         inline uint32
00148         getTimestamp() const
00149         { return cachedTimestamp; }
00150 
00154         inline uint8
00155         getProtocolVersion() const
00156         { return getHeader()->version; }
00157 
00162         inline bool
00163         isPadded() const
00164         { return getHeader()->padding; }
00165                 
00172         inline uint8
00173         getPaddingSize() const
00174         { return buffer[total - 1]; }
00175 
00182         inline bool
00183         isMarked() const
00184         { return getHeader()->marker; }
00185                 
00191         inline bool
00192         isExtended() const
00193         { return getHeader()->extension; }
00194 
00199         inline uint16
00200         getCSRCsCount() const
00201         { return getHeader()->cc; }
00202                 
00210         inline const uint32*
00211         getCSRCs() const
00212         { return static_cast<const uint32*>(&(getHeader()->sources[1])); }
00213                 
00226         inline uint16
00227         getHdrExtUndefined() const
00228         { return (isExtended()? getHeaderExt()->undefined : 0); }
00229 
00241         inline uint32
00242         getHdrExtSize() const
00243         { return (isExtended()? 
00244                   (static_cast<uint32>(getHeaderExt()->length) << 2) : 
00245                   0); }
00246 
00253         inline const unsigned char*
00254         getHdrExtContent() const
00255         { return (isExtended() ? 
00256                   (reinterpret_cast<const unsigned char*>(getHeaderExt()) + 
00257                    sizeof(RTPHeaderExt)) :
00258                   NULL); }
00259 
00266         inline const unsigned char* const
00267         getRawPacket() const
00268         { return buffer; }
00269                 
00276         inline uint32
00277         getRawPacketSize() const
00278         { return total; };
00279 
00280         inline size_t
00281         getSizeOfFixedHeader() const
00282         { return sizeof(RTPFixedHeader); }
00283 
00284 protected:              
00288         inline virtual ~RTPPacket()
00289         { endPacket(); };
00290                 
00294         void 
00295         endPacket();
00296 
00302         inline RTPFixedHeader*
00303         getHeader() const
00304         { return reinterpret_cast<RTPFixedHeader*>(buffer); }
00305                 
00306         inline void
00307         setExtension(bool e)
00308         { getHeader()->extension = e; }
00309 
00317         inline const RTPHeaderExt*
00318         getHeaderExt() const
00319         {
00320          uint32 fixsize = sizeof(RTPFixedHeader) + (getHeader()->cc << 2); 
00321          return (reinterpret_cast<RTPHeaderExt*>(buffer + fixsize));
00322         }
00323 
00329         inline uint32
00330         getRawTimestamp() const
00331         { return ntohl(getHeader()->timestamp); };
00332 
00333         inline void 
00334         setbuffer(const void* src, size_t len, size_t pos)
00335         { memcpy(buffer + pos,src,len); }
00336 
00338         uint16 cachedSeqNum;
00340         uint32 cachedTimestamp;
00341 
00342 private:
00344         unsigned char* buffer;
00346         uint32 hdrSize;
00348         uint32 payloadSize;
00350         uint32 total;
00352         bool duplicated;
00353 
00354 #ifdef  CCXX_PACKED
00355 #pragma pack(1)
00356 #endif
00357 
00367         struct RTPFixedHeader
00368         {
00369 #if     __BYTE_ORDER == __BIG_ENDIAN
00370 
00371                 unsigned char version:2;       
00372                 unsigned char padding:1;       
00373                 unsigned char extension:1;     
00374                 unsigned char cc:4;            
00375                 unsigned char marker:1;        
00376                 unsigned char payload:7;       
00377 #else
00378 
00379                 unsigned char cc:4;            
00380                 unsigned char extension:1;     
00381                 unsigned char padding:1;       
00382                 unsigned char version:2;       
00383                 unsigned char payload:7;       
00384                 unsigned char marker:1;        
00385 #endif
00386                 uint16 sequence;        
00387                 uint32 timestamp;       
00388                 uint32 sources[1];      
00389         };
00390 
00399 public:
00400         struct RFC2833Payload
00401         {
00402 #if __BYTE_ORDER == __BIG_ENDIAN
00403                 uint8 event : 8;
00404                 bool ebit : 1;
00405                 bool rbit : 1;
00406                 uint8 vol : 6;
00407                 uint16 duration : 16;
00408 #else
00409                 uint8 event : 8;
00410                 uint8 vol : 6;
00411                 bool rbit : 1;
00412                 bool ebit : 1; 
00413                 uint16 duration : 16;
00414 #endif
00415         };      
00416 
00417 private:        
00425         struct RTPHeaderExt
00426         {
00427                 uint16 undefined; 
00428                 uint16 length;    
00429         };
00430 #ifdef  CCXX_PACKED
00431 #pragma pack()
00432 #endif
00433 
00434         /* definitions for access to most common 2833 fields... */
00435 
00436 public:
00442         inline struct RFC2833Payload *getRaw2833Payload(void)
00443                 {return (struct RFC2833Payload *)getPayload();};
00444 
00450         inline uint16 get2833Duration(void)
00451                 {return ntohs(getRaw2833Payload()->duration);};
00452 
00458         inline void set2833Duration(uint16 d)
00459                 {getRaw2833Payload()->duration = htons(d);};
00460 };
00461 
00472 class __EXPORT OutgoingRTPPkt : public RTPPacket
00473 {
00474 public:
00494         OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc, 
00495                        const unsigned char* const hdrext, uint32 hdrextlen,
00496                        const unsigned char* const data, size_t datalen,
00497                        uint8 paddinglen);
00498 
00512         OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc, 
00513                        const unsigned char* const data, size_t datalen,
00514                        uint8 paddinglen);
00515                 
00525         OutgoingRTPPkt(const unsigned char* const data, size_t datalen, 
00526                        uint8 paddinglen);
00527 
00528         ~OutgoingRTPPkt()
00529         { }
00530 
00534         inline void
00535         setPayloadType(PayloadType pt)
00536         { getHeader()->payload = pt; };
00537                 
00541         inline void
00542         setSeqNum(uint16 seq)
00543         {
00544                 cachedSeqNum = seq;
00545                 getHeader()->sequence = htons(seq); 
00546         }
00547 
00551         inline void
00552         setTimestamp(uint32 pts)
00553         { 
00554                 cachedTimestamp = pts; 
00555                 getHeader()->timestamp = htonl(pts);
00556         }
00557 
00564         inline void 
00565         setSSRC(uint32 ssrc) const
00566         { getHeader()->sources[0] = htonl(ssrc); }
00567 
00575         inline void
00576         setSSRCNetwork(uint32 ssrc) const
00577         { getHeader()->sources[0] = ssrc; }
00578         
00586         inline void
00587         setMarker(bool mark)
00588         { getHeader()->marker = mark; }
00589 
00593         inline bool 
00594         operator==(const OutgoingRTPPkt &p) const
00595         { return ( this->getSeqNum() == p.getSeqNum() ); }
00596 
00600         inline bool
00601         operator!=(const OutgoingRTPPkt &p) const
00602         { return ( this->getSeqNum() != p.getSeqNum() ); }
00603 
00604 private:
00609         OutgoingRTPPkt(const OutgoingRTPPkt &o);
00610         
00615         OutgoingRTPPkt&
00616         operator=(const OutgoingRTPPkt &o);
00617 
00622         void setCSRCArray(const uint32* const csrcs, uint16 numcsrc);
00623 };
00624 
00637 class __EXPORT IncomingRTPPkt : public RTPPacket
00638 {
00639 public:
00652         IncomingRTPPkt(const unsigned char* block, size_t len);
00653 
00654         ~IncomingRTPPkt()
00655         { }
00656 
00662         inline bool
00663         isHeaderValid()
00664         { return headerValid; }
00665 
00672         inline uint32 
00673         getSSRC() const
00674         { return cachedSSRC; }
00675 
00680         inline bool 
00681         operator==(const IncomingRTPPkt &p) const
00682         { return ( (this->getSeqNum() == p.getSeqNum()) && 
00683                    (this->getSSRC() == p.getSSRC()) ); }
00684 
00689         inline bool
00690         operator!=(const IncomingRTPPkt &p) const
00691         { return !( *this == p ); }
00692 
00693 private:
00698         IncomingRTPPkt(const IncomingRTPPkt &ip);
00699 
00704         IncomingRTPPkt&
00705         operator=(const IncomingRTPPkt &ip);
00706 
00708         bool headerValid;
00710         uint32 cachedSSRC;
00711         // Masks for RTP header validation: types matching RTCP SR or
00712         // RR must be rejected to avoid accepting misaddressed RTCP
00713         // packets.
00714         static const uint16 RTP_INVALID_PT_MASK;
00715         static const uint16 RTP_INVALID_PT_VALUE;
00716 };      
00717  // rtppacket
00719 
00720 #ifdef  CCXX_NAMESPACES
00721 }
00722 #endif
00723 
00724 #endif  // ndef CCXX_RTP_RTPPKT_H_
00725 

Generated on Fri Dec 9 09:15:04 2005 for ccRTP by  doxygen 1.4.5