ccRTP
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
44NAMESPACE_COMMONCPP
45
70class CryptoContext;
71
72class __EXPORT RTPPacket
73{
74private:
75 struct RTPFixedHeader;
76 struct RTPHeaderExt;
77
78public:
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
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
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
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
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
302protected:
306 inline virtual ~RTPPacket()
307 { endPacket(); }
308
312 void
314
320 inline RTPFixedHeader*
321 getHeader() const
322 { return reinterpret_cast<RTPFixedHeader*>(buffer); }
323
324 inline void
326 { getHeader()->extension = e; }
327
335 inline const RTPHeaderExt*
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
359
367
374
376 uint32 total;
377
380
381private:
383 unsigned char* buffer;
385 uint32 hdrSize;
387 bool duplicated;
388
389#ifdef CCXX_PACKED
390#pragma pack(1)
391#endif
402 struct RTPFixedHeader
403 {
404#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
406 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
414 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
434public:
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
452private:
460 struct RTPHeaderExt
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
471public:
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
507class __EXPORT OutgoingRTPPkt : public RTPPacket
508{
509public:
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
661 { return ( this->getSeqNum() == p.getSeqNum() ); }
662
666 inline bool
668 { return ( this->getSeqNum() != p.getSeqNum() ); }
669
670private:
676
682 operator=(const OutgoingRTPPkt &o);
683
688 void setCSRCArray(const uint32* const csrcs, uint16 numcsrc);
689
690};
691
704class __EXPORT IncomingRTPPkt : public RTPPacket
705{
706public:
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
755
760 inline bool
762 { return ( (this->getSeqNum() == p.getSeqNum()) &&
763 (this->getSSRC() == p.getSSRC()) ); }
764
769 inline bool
771 { return !( *this == p ); }
772
773private:
779
785 operator=(const IncomingRTPPkt &ip);
786
788 bool headerValid;
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
800END_NAMESPACE
801
802#endif // ndef CCXX_RTP_RTPPKT_H_
803
Base elements for RTP stacks: constants, types and global functions.
The implementation for a SRTP cryptographic context.
Definition CryptoContext.h:82
RTP packets received from other participants.
Definition rtppkt.h:705
~IncomingRTPPkt()
Definition rtppkt.h:721
int32 unprotect(CryptoContext *pcc)
Unprotect a received packet.
uint32 getSSRC() const
Get synchronization source numeric identifier.
Definition rtppkt.h:740
bool isHeaderValid()
Get validity of this packet.
Definition rtppkt.h:730
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
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
IncomingRTPPkt(const unsigned char *block, size_t len)
Build an RTP packet object from a data buffer.
RTP packets being sent.
Definition rtppkt.h:508
void setSSRCNetwork(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition rtppkt.h:634
void setMarker(bool mark)
Specify the value of the marker bit.
Definition rtppkt.h:645
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:610
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:600
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:591
bool operator!=(const OutgoingRTPPkt &p) const
Outgoing packets are not equal if their sequence numbers differ.
Definition rtppkt.h:667
bool operator==(const OutgoingRTPPkt &p) const
Outgoing packets are equal if their sequence numbers match.
Definition rtppkt.h:660
void setSSRC(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition rtppkt.h:623
~OutgoingRTPPkt()
Definition rtppkt.h:584
A base class for both IncomingRTPPkt and OutgoingRTPPkt.
Definition rtppkt.h:73
size_t getSizeOfFixedHeader() const
Definition rtppkt.h:286
int32 srtpLength
Lebgth of additional SRTP data.
Definition rtppkt.h:373
uint32 srtpDataOffset
Offset into packet memory pointing to area for SRTP data.
Definition rtppkt.h:366
uint8 getProtocolVersion() const
Definition rtppkt.h:156
uint32 payloadSize
note: payload (not full packet) size.
Definition rtppkt.h:379
void endPacket()
Free memory allocated for the packet.
uint32 getRawPacketSizeSrtp() const
Definition rtppkt.h:282
uint8 getPaddingSize() const
Get the number of octets padding the end of the payload section.
Definition rtppkt.h:174
uint32 getTimestamp() const
Definition rtppkt.h:149
PayloadType getPayloadType() const
Definition rtppkt.h:135
void setbuffer(const void *src, size_t len, size_t pos)
Definition rtppkt.h:352
const unsigned char *const getRawPacket() const
Get the raw packet as it will be sent through the network.
Definition rtppkt.h:268
const uint8 *const getPayload() const
Definition rtppkt.h:121
bool isMarked() const
Ask whether the packet is marked (for isntance, is a new talk spurt in some audio profiles).
Definition rtppkt.h:184
bool isPadded() const
Ask whether the packet contains padding bytes at the end.
Definition rtppkt.h:164
uint16 getHdrExtUndefined() const
Get the first 16 bits (in network order) of the header of the RTP header extension.
Definition rtppkt.h:228
uint32 getRawPacketSize() const
Get the raw packet length, including header, extension, payload and padding.
Definition rtppkt.h:278
uint32 cachedTimestamp
Packet timestamp in host order (includes initial shift).
Definition rtppkt.h:358
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:243
uint32 getHeaderSize() const
Get the length of the header, including contributing sources identifiers and header extension,...
Definition rtppkt.h:114
bool isExtended() const
Ask whether the packet contains header extensions.
Definition rtppkt.h:193
uint16 getSeqNum() const
Definition rtppkt.h:142
RTPFixedHeader * getHeader() const
Return low level structure for the header of the packet.
Definition rtppkt.h:321
void set2833Duration(uint16 timestamp)
Set 2833 duration field.
Definition rtppkt.h:493
const RTPHeaderExt * getHeaderExt() const
Get a pointer to RTPHeaderExt pointing after the RTP header (fixed part plus contributing sources).
Definition rtppkt.h:336
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,...
Definition rtppkt.h:212
void setExtension(bool e)
Definition rtppkt.h:325
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:128
uint16 get2833Duration(void)
Fetch 2833 duration field.
Definition rtppkt.h:485
uint16 cachedSeqNum
Packet sequence number in host order.
Definition rtppkt.h:356
uint32 total
total length, including header, payload and padding
Definition rtppkt.h:376
uint16 getCSRCsCount() const
Get the number of contributing sources specified in the packet header.
Definition rtppkt.h:201
const unsigned char * getHdrExtContent() const
Get the content of the header extension.
Definition rtppkt.h:255
virtual ~RTPPacket()
Destructor, free the buffer provided in the constructor.
Definition rtppkt.h:306
struct RFC2833Payload * getRaw2833Payload(void)
Fetch a raw 2833 packet.
Definition rtppkt.h:477
Payload types and formats.
uint8 PayloadType
RTP payload type numeric identifier.
Definition formats.h:62
a structure defining RFC2833 Telephony events.
Definition rtppkt.h:436
uint16 duration
Definition rtppkt.h:448
uint8 event
Definition rtppkt.h:444
uint8 vol
Definition rtppkt.h:445
bool rbit
Definition rtppkt.h:446
bool ebit
Definition rtppkt.h:447