ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rtppkt.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2015 Federico Montesino Pouzols <fedemp@altern.org>.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with GNU ccRTP. If not, see <http://www.gnu.org/licenses/>.
15 //
16 // As a special exception, you may use this file as part of a free software
17 // library without restriction. Specifically, if other files instantiate
18 // templates or use macros or inline functions from this file, or you compile
19 // this file and link it with other files to produce an executable, this
20 // file does not by itself cause the resulting executable to be covered by
21 // the GNU General Public License. This exception does not however
22 // invalidate any other reasons why the executable file might be covered by
23 // the GNU General Public License.
24 //
25 // This exception applies only to the code released under the name GNU
26 // ccRTP. If you copy code from other releases into a copy of GNU
27 // ccRTP, as the General Public License permits, the exception does
28 // not apply to the code that you add in this way. To avoid misleading
29 // anyone as to the status of such modified files, you must delete
30 // this exception notice from them.
31 //
32 // If you write modifications of your own for GNU ccRTP, it is your choice
33 // whether to permit this exception to apply to your modifications.
34 // If you do not wish that, delete this exception notice.
35 //
36 
37 #ifndef CCXX_RTP_RTPPKT_H_
38 #define CCXX_RTP_RTPPKT_H_
39 
40 #include <ccrtp/base.h>
41 #include <ccrtp/formats.h>
42 #include <ccrtp/CryptoContext.h>
43 
44 NAMESPACE_COMMONCPP
45 
70 class CryptoContext;
71 
73 {
74 private:
75  struct RTPFixedHeader;
76  struct RTPHeaderExt;
77 
78 public:
91  RTPPacket(const unsigned char* const block, size_t len,
92  bool duplicate = false);
93 
105  RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen, CryptoContext* pcc= NULL);
106 
113  inline uint32
115  { return hdrSize; }
116 
120  inline const uint8* const
121  getPayload() const
122  { return (uint8*)(buffer + getHeaderSize()); }
123 
127  inline uint32
129  { return payloadSize; }
130 
134  inline PayloadType
136  { return static_cast<PayloadType>(getHeader()->payload); }
137 
141  inline uint16
142  getSeqNum() const
143  { return cachedSeqNum; }
144 
148  inline uint32
149  getTimestamp() const
150  { return cachedTimestamp; }
151 
155  inline uint8
157  { return getHeader()->version; }
158 
163  inline bool
164  isPadded() const
165  { return getHeader()->padding; }
166 
173  inline uint8
175  { return buffer[total - 1]; }
176 
183  inline bool
184  isMarked() const
185  { return getHeader()->marker; }
186 
192  inline bool
193  isExtended() const
194  { return getHeader()->extension; }
195 
200  inline uint16
202  { return getHeader()->cc; }
203 
211  inline const uint32*
212  getCSRCs() const
213  { return static_cast<const uint32*>(&(getHeader()->sources[1])); }
214 
227  inline uint16
229  { return (isExtended()? getHeaderExt()->undefined : 0); }
230 
242  inline uint32
244  { return (isExtended()?
245  (static_cast<uint32>(ntohs(getHeaderExt()->length)) << 2) :
246  0); }
247 
254  inline const unsigned char*
256  { return (isExtended() ?
257  (reinterpret_cast<const unsigned char*>(getHeaderExt()) +
258  sizeof(RTPHeaderExt)) :
259  NULL); }
260 
267  inline const unsigned char* const
268  getRawPacket() const
269  { return buffer; }
270 
277  inline uint32
279  { return total; }
280 
281  inline uint32
283  { return total + srtpLength; }
284 
285  inline size_t
287  { return sizeof(RTPFixedHeader); }
288 
300  void reComputePayLength(bool padding);
301 
302 protected:
306  inline virtual ~RTPPacket()
307  { endPacket(); }
308 
312  void
313  endPacket();
314 
320  inline RTPFixedHeader*
321  getHeader() const
322  { return reinterpret_cast<RTPFixedHeader*>(buffer); }
323 
324  inline void
325  setExtension(bool e)
326  { getHeader()->extension = e; }
327 
335  inline const RTPHeaderExt*
336  getHeaderExt() const
337  {
338  uint32 fixsize = sizeof(RTPFixedHeader) + (getHeader()->cc << 2);
339  return (reinterpret_cast<RTPHeaderExt*>(buffer + fixsize));
340  }
341 
347  inline uint32
349  { return ntohl(getHeader()->timestamp); }
350 
351  inline void
352  setbuffer(const void* src, size_t len, size_t pos)
353  { memcpy(buffer + pos,src,len); }
354 
356  uint16 cachedSeqNum;
359 
367 
373  int32 srtpLength;
374 
376  uint32 total;
377 
379  uint32 payloadSize;
380 
381 private:
383  unsigned char* buffer;
385  uint32 hdrSize;
388 
389 #ifdef CCXX_PACKED
390 #pragma pack(1)
391 #endif
392 
403  {
404 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
405  unsigned char version:2;
407  unsigned char padding:1;
408  unsigned char extension:1;
409  unsigned char cc:4;
410  unsigned char marker:1;
411  unsigned char payload:7;
412 #else
413  unsigned char cc:4;
415  unsigned char extension:1;
416  unsigned char padding:1;
417  unsigned char version:2;
418  unsigned char payload:7;
419  unsigned char marker:1;
420 #endif
421  uint16 sequence;
422  uint32 timestamp;
423  uint32 sources[1];
424  };
425 
434 public:
436  {
437 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
438  uint8 event : 8;
439  bool ebit : 1;
440  bool rbit : 1;
441  uint8 vol : 6;
442  uint16 duration : 16;
443 #else
444  uint8 event : 8;
445  uint8 vol : 6;
446  bool rbit : 1;
447  bool ebit : 1;
448  uint16 duration : 16;
449 #endif
450  };
451 
452 private:
461  {
462  uint16 undefined;
463  uint16 length;
464  };
465 #ifdef CCXX_PACKED
466 #pragma pack()
467 #endif
468 
469  /* definitions for access to most common 2833 fields... */
470 
471 public:
477  inline struct RFC2833Payload *getRaw2833Payload(void)
478  {return (struct RFC2833Payload *)getPayload();}
479 
485  inline uint16 get2833Duration(void)
486  {return ntohs(getRaw2833Payload()->duration);}
487 
493  inline void set2833Duration(uint16 timestamp)
494  {getRaw2833Payload()->duration = htons(timestamp);}
495 };
496 
508 {
509 public:
536  OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
537  const unsigned char* const hdrext, uint32 hdrextlen,
538  const unsigned char* const data, size_t datalen,
539  uint8 paddinglen= 0, CryptoContext* pcc= NULL);
540 
561  OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
562  const unsigned char* const data, size_t datalen,
563  uint8 paddinglen= 0, CryptoContext* pcc= NULL);
564 
581  OutgoingRTPPkt(const unsigned char* const data, size_t datalen,
582  uint8 paddinglen= 0, CryptoContext* pcc= NULL);
583 
585  { }
586 
590  inline void
592  { getHeader()->payload = pt; }
593 
599  inline void
600  setSeqNum(uint16 seq)
601  {
602  cachedSeqNum = seq;
603  getHeader()->sequence = htons(seq);
604  }
605 
609  inline void
610  setTimestamp(uint32 pts)
611  {
612  cachedTimestamp = pts;
613  getHeader()->timestamp = htonl(pts);
614  }
615 
622  inline void
623  setSSRC(uint32 ssrc) const
624  { getHeader()->sources[0] = htonl(ssrc); }
625 
633  inline void
634  setSSRCNetwork(uint32 ssrc) const
635  { getHeader()->sources[0] = ssrc; }
636 
644  inline void
645  setMarker(bool mark)
646  { getHeader()->marker = mark; }
647 
654  void protect(uint32 ssrc, CryptoContext* pcc);
655 
659  inline bool
660  operator==(const OutgoingRTPPkt &p) const
661  { return ( this->getSeqNum() == p.getSeqNum() ); }
662 
666  inline bool
667  operator!=(const OutgoingRTPPkt &p) const
668  { return ( this->getSeqNum() != p.getSeqNum() ); }
669 
670 private:
675  OutgoingRTPPkt(const OutgoingRTPPkt &o);
676 
682  operator=(const OutgoingRTPPkt &o);
683 
688  void setCSRCArray(const uint32* const csrcs, uint16 numcsrc);
689 
690 };
691 
705 {
706 public:
719  IncomingRTPPkt(const unsigned char* block, size_t len);
720 
722  { }
723 
729  inline bool
731  { return headerValid; }
732 
739  inline uint32
740  getSSRC() const
741  { return cachedSSRC; }
742 
753  int32
754  unprotect(CryptoContext* pcc);
755 
760  inline bool
761  operator==(const IncomingRTPPkt &p) const
762  { return ( (this->getSeqNum() == p.getSeqNum()) &&
763  (this->getSSRC() == p.getSSRC()) ); }
764 
769  inline bool
770  operator!=(const IncomingRTPPkt &p) const
771  { return !( *this == p ); }
772 
773 private:
778  IncomingRTPPkt(const IncomingRTPPkt &ip);
779 
785  operator=(const IncomingRTPPkt &ip);
786 
790  uint32 cachedSSRC;
791  // Masks for RTP header validation: types matching RTCP SR or
792  // RR must be rejected to avoid accepting misaddressed RTCP
793  // packets.
794  static const uint16 RTP_INVALID_PT_MASK;
795  static const uint16 RTP_INVALID_PT_VALUE;
796 };
797  // rtppacket
799 
800 END_NAMESPACE
801 
802 #endif // ndef CCXX_RTP_RTPPKT_H_
803 
Fixed component of the variable-length header extension, appended to the fixed header, after the CSRC list, when X == 1.
Definition: rtppkt.h:460
uint32 total
total length, including header, payload and padding
Definition: rtppkt.h:376
bool isPadded() const
Ask whether the packet contains padding bytes at the end.
Definition: rtppkt.h:164
void setMarker(bool mark)
Specify the value of the marker bit.
Definition: rtppkt.h:645
size_t getSizeOfFixedHeader() const
Definition: rtppkt.h:286
~IncomingRTPPkt()
Definition: rtppkt.h:721
A base class for both IncomingRTPPkt and OutgoingRTPPkt.
Definition: rtppkt.h:72
uint16 undefined
to be defined
Definition: rtppkt.h:462
unsigned char payload
Payload type.
Definition: rtppkt.h:418
The implementation for a SRTP cryptographic context.
Definition: CryptoContext.h:82
PayloadType getPayloadType() const
Definition: rtppkt.h:135
RTP packets received from other participants.
Definition: rtppkt.h:704
uint32 getRawTimestamp() const
Obtain the absolute timestamp carried in the packet header.
Definition: rtppkt.h:348
const uint32 * getCSRCs() const
Get the 32-bit identifiers of the contributing sources for the packet as an array, of length getCSRCsCount().
Definition: rtppkt.h:212
~OutgoingRTPPkt()
Definition: rtppkt.h:584
uint32 getSSRC() const
Get synchronization source numeric identifier.
Definition: rtppkt.h:740
void setSeqNum(uint16 seq)
Sets the sequence number in the header.
Definition: rtppkt.h:600
unsigned char * buffer
packet in memory
Definition: rtppkt.h:383
void setSSRCNetwork(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition: rtppkt.h:634
static const uint16 RTP_INVALID_PT_VALUE
Definition: rtppkt.h:795
uint32 getTimestamp() const
Definition: rtppkt.h:149
uint32 getHdrExtSize() const
Get the length (in octets) of the data contained in the header extension.
Definition: rtppkt.h:243
uint32 sources[1]
contributing sources
Definition: rtppkt.h:423
const RTPHeaderExt * getHeaderExt() const
Get a pointer to RTPHeaderExt pointing after the RTP header (fixed part plus contributing sources)...
Definition: rtppkt.h:336
bool isHeaderValid()
Get validity of this packet.
Definition: rtppkt.h:730
const uint8 *const getPayload() const
Definition: rtppkt.h:121
bool duplicated
whether the object was contructed with duplicated = true
Definition: rtppkt.h:387
bool operator!=(const OutgoingRTPPkt &p) const
Outgoing packets are not equal if their sequence numbers differ.
Definition: rtppkt.h:667
const unsigned char * getHdrExtContent() const
Get the content of the header extension.
Definition: rtppkt.h:255
uint16 getHdrExtUndefined() const
Get the first 16 bits (in network order) of the header of the RTP header extension.
Definition: rtppkt.h:228
uint32 srtpDataOffset
Offset into packet memory pointing to area for SRTP data.
Definition: rtppkt.h:366
RTPFixedHeader * getHeader() const
Return low level structure for the header of the packet.
Definition: rtppkt.h:321
unsigned char marker
Marker bit.
Definition: rtppkt.h:419
bool operator==(const OutgoingRTPPkt &p) const
Outgoing packets are equal if their sequence numbers match.
Definition: rtppkt.h:660
uint8 getProtocolVersion() const
Definition: rtppkt.h:156
void setSSRC(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition: rtppkt.h:623
uint32 cachedTimestamp
Packet timestamp in host order (includes initial shift).
Definition: rtppkt.h:358
uint16 getSeqNum() const
Definition: rtppkt.h:142
virtual ~RTPPacket()
Destructor, free the buffer provided in the constructor.
Definition: rtppkt.h:306
uint32 getRawPacketSize() const
Get the raw packet length, including header, extension, payload and padding.
Definition: rtppkt.h:278
bool isExtended() const
Ask whether the packet contains header extensions.
Definition: rtppkt.h:193
#define __EXPORT
Definition: ZrtpCallback.h:40
bool operator==(const IncomingRTPPkt &p) const
Two incoming packets are equal if they come from sources with the same SSRC and have the same sequenc...
Definition: rtppkt.h:761
uint16 length
number of 32-bit words in the extension
Definition: rtppkt.h:463
static const uint16 RTP_INVALID_PT_MASK
Definition: rtppkt.h:794
void set2833Duration(uint16 timestamp)
Set 2833 duration field.
Definition: rtppkt.h:493
const unsigned char *const getRawPacket() const
Get the raw packet as it will be sent through the network.
Definition: rtppkt.h:268
uint16 get2833Duration(void)
Fetch 2833 duration field.
Definition: rtppkt.h:485
uint32 cachedSSRC
SSRC 32-bit identifier in host order.
Definition: rtppkt.h:790
uint8 getPaddingSize() const
Get the number of octets padding the end of the payload section.
Definition: rtppkt.h:174
struct RFC2833Payload * getRaw2833Payload(void)
Fetch a raw 2833 packet.
Definition: rtppkt.h:477
uint32 getPayloadSize() const
Definition: rtppkt.h:128
RTP fixed header as it is send through the network.
Definition: rtppkt.h:402
bool headerValid
Header validity, checked at construction time.
Definition: rtppkt.h:788
Base elements for RTP stacks: constants, types and global functions.
uint16 getCSRCsCount() const
Get the number of contributing sources specified in the packet header.
Definition: rtppkt.h:201
void setTimestamp(uint32 pts)
Definition: rtppkt.h:610
void setPayloadType(PayloadType pt)
Definition: rtppkt.h:591
void setbuffer(const void *src, size_t len, size_t pos)
Definition: rtppkt.h:352
bool isMarked() const
Ask whether the packet is marked (for isntance, is a new talk spurt in some audio profiles)...
Definition: rtppkt.h:184
uint16 sequence
sequence number
Definition: rtppkt.h:421
bool operator!=(const IncomingRTPPkt &p) const
Two incoming packets are not equal if they come from different sources or have different sequence num...
Definition: rtppkt.h:770
uint32 getRawPacketSizeSrtp() const
Definition: rtppkt.h:282
a structure defining RFC2833 Telephony events.
Definition: rtppkt.h:435
void setExtension(bool e)
Definition: rtppkt.h:325
Payload types and formats.
uint32 hdrSize
size of the header, including contributing sources and extensions
Definition: rtppkt.h:385
int32 srtpLength
Lebgth of additional SRTP data.
Definition: rtppkt.h:373
uint16 cachedSeqNum
Packet sequence number in host order.
Definition: rtppkt.h:356
uint32 getHeaderSize() const
Get the length of the header, including contributing sources identifiers and header extension...
Definition: rtppkt.h:114
RTP packets being sent.
Definition: rtppkt.h:507
uint32 payloadSize
note: payload (not full packet) size.
Definition: rtppkt.h:379
uint32 timestamp
timestamp
Definition: rtppkt.h:422
uint8 PayloadType
RTP payload type numeric identifier.
Definition: formats.h:62