ccRTP
rtppkt.h
Go to the documentation of this file.
1// Copyright (C) 2002 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 General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16//
17// As a special exception, you may use this file as part of a free software
18// library without restriction. Specifically, if other files instantiate
19// templates or use macros or inline functions from this file, or you compile
20// this file and link it with other files to produce an executable, this
21// file does not by itself cause the resulting executable to be covered by
22// the GNU General Public License. This exception does not however
23// invalidate any other reasons why the executable file might be covered by
24// the GNU General Public License.
25//
26// This exception applies only to the code released under the name GNU
27// ccRTP. If you copy code from other releases into a copy of GNU
28// ccRTP, as the General Public License permits, the exception does
29// not apply to the code that you add in this way. To avoid misleading
30// anyone as to the status of such modified files, you must delete
31// this exception notice from them.
32//
33// If you write modifications of your own for GNU ccRTP, it is your choice
34// whether to permit this exception to apply to your modifications.
35// If you do not wish that, delete this exception notice.
36//
37
38#ifndef CCXX_RTP_RTPPKT_H_
39#define CCXX_RTP_RTPPKT_H_
40
41#include <ccrtp/base.h>
42#include <ccrtp/formats.h>
43#include <ccrtp/CryptoContext.h>
44
45NAMESPACE_COMMONCPP
46
71class CryptoContext;
72
73class __EXPORT RTPPacket
74{
75private:
76 struct RTPFixedHeader;
77 struct RTPHeaderExt;
78
79public:
92 RTPPacket(const unsigned char* const block, size_t len,
93 bool duplicate = false);
94
106 RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen, CryptoContext* pcc= NULL);
107
114 inline uint32
116 { return hdrSize; }
117
121 inline const uint8* const
123 { return (uint8*)(buffer + getHeaderSize()); }
124
128 inline uint32
130 { return payloadSize; }
131
135 inline PayloadType
137 { return static_cast<PayloadType>(getHeader()->payload); }
138
142 inline uint16
143 getSeqNum() const
144 { return cachedSeqNum; }
145
149 inline uint32
151 { return cachedTimestamp; }
152
156 inline uint8
158 { return getHeader()->version; }
159
164 inline bool
165 isPadded() const
166 { return getHeader()->padding; }
167
174 inline uint8
176 { return buffer[total - 1]; }
177
184 inline bool
185 isMarked() const
186 { return getHeader()->marker; }
187
193 inline bool
195 { return getHeader()->extension; }
196
201 inline uint16
203 { return getHeader()->cc; }
204
212 inline const uint32*
213 getCSRCs() const
214 { return static_cast<const uint32*>(&(getHeader()->sources[1])); }
215
228 inline uint16
230 { return (isExtended()? getHeaderExt()->undefined : 0); }
231
243 inline uint32
245 { return (isExtended()?
246 (static_cast<uint32>(ntohs(getHeaderExt()->length)) << 2) :
247 0); }
248
255 inline const unsigned char*
257 { return (isExtended() ?
258 (reinterpret_cast<const unsigned char*>(getHeaderExt()) +
259 sizeof(RTPHeaderExt)) :
260 NULL); }
261
268 inline const unsigned char* const
270 { return buffer; }
271
278 inline uint32
280 { return total; }
281
282 inline uint32
284 { return total + srtpLength; }
285
286 inline size_t
288 { return sizeof(RTPFixedHeader); }
289
301 void reComputePayLength(bool padding);
302
303protected:
307 inline virtual ~RTPPacket()
308 { endPacket(); }
309
313 void
315
321 inline RTPFixedHeader*
322 getHeader() const
323 { return reinterpret_cast<RTPFixedHeader*>(buffer); }
324
325 inline void
327 { getHeader()->extension = e; }
328
336 inline const RTPHeaderExt*
338 {
339 uint32 fixsize = sizeof(RTPFixedHeader) + (getHeader()->cc << 2);
340 return (reinterpret_cast<RTPHeaderExt*>(buffer + fixsize));
341 }
342
348 inline uint32
350 { return ntohl(getHeader()->timestamp); }
351
352 inline void
353 setbuffer(const void* src, size_t len, size_t pos)
354 { memcpy(buffer + pos,src,len); }
355
360
368
375
377 uint32 total;
378
381
382private:
384 unsigned char* buffer;
386 uint32 hdrSize;
388 bool duplicated;
389
390#ifdef CCXX_PACKED
391#pragma pack(1)
392#endif
403 struct RTPFixedHeader
404 {
405#if __BYTE_ORDER == __BIG_ENDIAN
407 unsigned char version:2;
408 unsigned char padding:1;
409 unsigned char extension:1;
410 unsigned char cc:4;
411 unsigned char marker:1;
412 unsigned char payload:7;
413#else
415 unsigned char cc:4;
416 unsigned char extension:1;
417 unsigned char padding:1;
418 unsigned char version:2;
419 unsigned char payload:7;
420 unsigned char marker:1;
421#endif
422 uint16 sequence;
423 uint32 timestamp;
424 uint32 sources[1];
425 };
426
435public:
437 {
438#if __BYTE_ORDER == __BIG_ENDIAN
439 uint8 event : 8;
440 bool ebit : 1;
441 bool rbit : 1;
442 uint8 vol : 6;
443 uint16 duration : 16;
444#else
445 uint8 event : 8;
446 uint8 vol : 6;
447 bool rbit : 1;
448 bool ebit : 1;
449 uint16 duration : 16;
450#endif
451 };
452
453private:
461 struct RTPHeaderExt
462 {
463 uint16 undefined;
464 uint16 length;
465 };
466#ifdef CCXX_PACKED
467#pragma pack()
468#endif
469
470 /* definitions for access to most common 2833 fields... */
471
472public:
479 {return (struct RFC2833Payload *)getPayload();}
480
486 inline uint16 get2833Duration(void)
487 {return ntohs(getRaw2833Payload()->duration);}
488
494 inline void set2833Duration(uint16 timestamp)
495 {getRaw2833Payload()->duration = htons(timestamp);}
496};
497
508class __EXPORT OutgoingRTPPkt : public RTPPacket
509{
510public:
537 OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
538 const unsigned char* const hdrext, uint32 hdrextlen,
539 const unsigned char* const data, size_t datalen,
540 uint8 paddinglen= 0, CryptoContext* pcc= NULL);
541
562 OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
563 const unsigned char* const data, size_t datalen,
564 uint8 paddinglen= 0, CryptoContext* pcc= NULL);
565
582 OutgoingRTPPkt(const unsigned char* const data, size_t datalen,
583 uint8 paddinglen= 0, CryptoContext* pcc= NULL);
584
586 { }
587
591 inline void
593 { getHeader()->payload = pt; }
594
600 inline void
601 setSeqNum(uint16 seq)
602 {
603 cachedSeqNum = seq;
604 getHeader()->sequence = htons(seq);
605 }
606
610 inline void
611 setTimestamp(uint32 pts)
612 {
613 cachedTimestamp = pts;
614 getHeader()->timestamp = htonl(pts);
615 }
616
623 inline void
624 setSSRC(uint32 ssrc) const
625 { getHeader()->sources[0] = htonl(ssrc); }
626
634 inline void
635 setSSRCNetwork(uint32 ssrc) const
636 { getHeader()->sources[0] = ssrc; }
637
645 inline void
646 setMarker(bool mark)
647 { getHeader()->marker = mark; }
648
655 void protect(uint32 ssrc, CryptoContext* pcc);
656
660 inline bool
662 { return ( this->getSeqNum() == p.getSeqNum() ); }
663
667 inline bool
669 { return ( this->getSeqNum() != p.getSeqNum() ); }
670
671private:
677
683 operator=(const OutgoingRTPPkt &o);
684
689 void setCSRCArray(const uint32* const csrcs, uint16 numcsrc);
690
691};
692
705class __EXPORT IncomingRTPPkt : public RTPPacket
706{
707public:
720 IncomingRTPPkt(const unsigned char* block, size_t len);
721
723 { }
724
730 inline bool
732 { return headerValid; }
733
740 inline uint32
741 getSSRC() const
742 { return cachedSSRC; }
743
754 int32
756
761 inline bool
763 { return ( (this->getSeqNum() == p.getSeqNum()) &&
764 (this->getSSRC() == p.getSSRC()) ); }
765
770 inline bool
772 { return !( *this == p ); }
773
774private:
780
786 operator=(const IncomingRTPPkt &ip);
787
789 bool headerValid;
791 uint32 cachedSSRC;
792 // Masks for RTP header validation: types matching RTCP SR or
793 // RR must be rejected to avoid accepting misaddressed RTCP
794 // packets.
795 static const uint16 RTP_INVALID_PT_MASK;
796 static const uint16 RTP_INVALID_PT_VALUE;
797};
798 // rtppacket
800
801END_NAMESPACE
802
803#endif // ndef CCXX_RTP_RTPPKT_H_
804
Base elements for RTP stacks: constants, types and global functions.
The implementation for a SRTP cryptographic context.
Definition: CryptoContext.h:83
RTP packets received from other participants.
Definition: rtppkt.h:706
~IncomingRTPPkt()
Definition: rtppkt.h:722
int32 unprotect(CryptoContext *pcc)
Unprotect a received packet.
uint32 getSSRC() const
Get synchronization source numeric identifier.
Definition: rtppkt.h:741
bool isHeaderValid()
Get validity of this packet.
Definition: rtppkt.h:731
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:771
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:762
IncomingRTPPkt(const unsigned char *block, size_t len)
Build an RTP packet object from a data buffer.
RTP packets being sent.
Definition: rtppkt.h:509
void setSSRCNetwork(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition: rtppkt.h:635
void setMarker(bool mark)
Specify the value of the marker bit.
Definition: rtppkt.h:646
void protect(uint32 ssrc, CryptoContext *pcc)
Called packet is setup.
OutgoingRTPPkt(const uint32 *const csrcs, uint16 numcsrc, const unsigned char *const hdrext, uint32 hdrextlen, const unsigned char *const data, size_t datalen, uint8 paddinglen=0, CryptoContext *pcc=NULL)
Construct a new packet to be sent, containing several contributing source identifiers,...
void setTimestamp(uint32 pts)
Definition: rtppkt.h:611
OutgoingRTPPkt(const uint32 *const csrcs, uint16 numcsrc, const unsigned char *const data, size_t datalen, uint8 paddinglen=0, CryptoContext *pcc=NULL)
Construct a new packet to be sent, containing several contributing source identifiers and payload.
void setSeqNum(uint16 seq)
Definition: rtppkt.h:601
OutgoingRTPPkt(const unsigned char *const data, size_t datalen, uint8 paddinglen=0, CryptoContext *pcc=NULL)
Construct a new packet (fast variant, with no contributing sources and no header extension) to be sen...
void setPayloadType(PayloadType pt)
Definition: rtppkt.h:592
bool operator!=(const OutgoingRTPPkt &p) const
Outgoing packets are not equal if their sequence numbers differ.
Definition: rtppkt.h:668
bool operator==(const OutgoingRTPPkt &p) const
Outgoing packets are equal if their sequence numbers match.
Definition: rtppkt.h:661
void setSSRC(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition: rtppkt.h:624
~OutgoingRTPPkt()
Definition: rtppkt.h:585
A base class for both IncomingRTPPkt and OutgoingRTPPkt.
Definition: rtppkt.h:74
size_t getSizeOfFixedHeader() const
Definition: rtppkt.h:287
int32 srtpLength
Lebgth of additional SRTP data.
Definition: rtppkt.h:374
uint32 srtpDataOffset
Offset into packet memory pointing to area for SRTP data.
Definition: rtppkt.h:367
uint8 getProtocolVersion() const
Definition: rtppkt.h:157
uint32 payloadSize
note: payload (not full packet) size.
Definition: rtppkt.h:380
void endPacket()
Free memory allocated for the packet.
uint32 getRawPacketSizeSrtp() const
Definition: rtppkt.h:283
uint8 getPaddingSize() const
Get the number of octets padding the end of the payload section.
Definition: rtppkt.h:175
uint32 getTimestamp() const
Definition: rtppkt.h:150
PayloadType getPayloadType() const
Definition: rtppkt.h:136
void setbuffer(const void *src, size_t len, size_t pos)
Definition: rtppkt.h:353
const unsigned char *const getRawPacket() const
Get the raw packet as it will be sent through the network.
Definition: rtppkt.h:269
const uint8 *const getPayload() const
Definition: rtppkt.h:122
bool isMarked() const
Ask whether the packet is marked (for isntance, is a new talk spurt in some audio profiles).
Definition: rtppkt.h:185
bool isPadded() const
Ask whether the packet contains padding bytes at the end.
Definition: rtppkt.h:165
uint16 getHdrExtUndefined() const
Get the first 16 bits (in network order) of the header of the RTP header extension.
Definition: rtppkt.h:229
uint32 getRawPacketSize() const
Get the raw packet length, including header, extension, payload and padding.
Definition: rtppkt.h:279
uint32 cachedTimestamp
Packet timestamp in host order (includes initial shift).
Definition: rtppkt.h:359
RTPPacket(const unsigned char *const block, size_t len, bool duplicate=false)
Constructor, construct a packet object given the memory zone its content (header and payload) is stor...
uint32 getHdrExtSize() const
Get the length (in octets) of the data contained in the header extension.
Definition: rtppkt.h:244
uint32 getHeaderSize() const
Get the length of the header, including contributing sources identifiers and header extension,...
Definition: rtppkt.h:115
bool isExtended() const
Ask whether the packet contains header extensions.
Definition: rtppkt.h:194
uint16 getSeqNum() const
Definition: rtppkt.h:143
RTPFixedHeader * getHeader() const
Return low level structure for the header of the packet.
Definition: rtppkt.h:322
void set2833Duration(uint16 timestamp)
Set 2833 duration field.
Definition: rtppkt.h:494
const RTPHeaderExt * getHeaderExt() const
Get a pointer to RTPHeaderExt pointing after the RTP header (fixed part plus contributing sources).
Definition: rtppkt.h:337
uint32 getRawTimestamp() const
Obtain the absolute timestamp carried in the packet header.
Definition: rtppkt.h:349
const uint32 * getCSRCs() const
Get the 32-bit identifiers of the contributing sources for the packet as an array,...
Definition: rtppkt.h:213
void setExtension(bool e)
Definition: rtppkt.h:326
RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen, CryptoContext *pcc=NULL)
Construct a packet object without specifying its real content yet.
void reComputePayLength(bool padding)
Re-compute payload length.
uint32 getPayloadSize() const
Definition: rtppkt.h:129
uint16 get2833Duration(void)
Fetch 2833 duration field.
Definition: rtppkt.h:486
uint16 cachedSeqNum
Packet sequence number in host order.
Definition: rtppkt.h:357
uint32 total
total length, including header, payload and padding
Definition: rtppkt.h:377
uint16 getCSRCsCount() const
Get the number of contributing sources specified in the packet header.
Definition: rtppkt.h:202
const unsigned char * getHdrExtContent() const
Get the content of the header extension.
Definition: rtppkt.h:256
virtual ~RTPPacket()
Destructor, free the buffer provided in the constructor.
Definition: rtppkt.h:307
struct RFC2833Payload * getRaw2833Payload(void)
Fetch a raw 2833 packet.
Definition: rtppkt.h:478
Payload types and formats.
uint8 PayloadType
RTP payload type numeric identifier.
Definition: formats.h:63
a structure defining RFC2833 Telephony events.
Definition: rtppkt.h:437
uint16 duration
Definition: rtppkt.h:443
uint8 event
Definition: rtppkt.h:439
uint8 vol
Definition: rtppkt.h:442
bool rbit
Definition: rtppkt.h:441
bool ebit
Definition: rtppkt.h:440