rtppkt.h
Go to the documentation of this file.
// Copyright (C) 2002 Federico Montesino Pouzols <fedemp@altern.org>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// This exception applies only to the code released under the name GNU
// ccRTP. If you copy code from other releases into a copy of GNU
// ccRTP, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for GNU ccRTP, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//
#ifndef CCXX_RTP_RTPPKT_H_
#define CCXX_RTP_RTPPKT_H_
#include <ccrtp/base.h>
#include <ccrtp/formats.h>
#include <ccrtp/CryptoContext.h>
NAMESPACE_COMMONCPP
class CryptoContext;
class __EXPORT RTPPacket
{
private:
struct RTPFixedHeader;
struct RTPHeaderExt;
public:
RTPPacket(const unsigned char* const block, size_t len,
bool duplicate = false);
RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen, CryptoContext* pcc= NULL);
inline uint32
getHeaderSize() const
{ return hdrSize; }
inline const uint8* const
getPayload() const
{ return (uint8*)(buffer + getHeaderSize()); }
inline uint32
getPayloadSize() const
{ return payloadSize; }
inline PayloadType
getPayloadType() const
{ return static_cast<PayloadType>(getHeader()->payload); }
inline uint16
getSeqNum() const
{ return cachedSeqNum; }
inline uint32
getTimestamp() const
{ return cachedTimestamp; }
inline uint8
getProtocolVersion() const
{ return getHeader()->version; }
inline bool
isPadded() const
{ return getHeader()->padding; }
inline uint8
getPaddingSize() const
{ return buffer[total - 1]; }
inline bool
isMarked() const
{ return getHeader()->marker; }
inline bool
isExtended() const
{ return getHeader()->extension; }
inline uint16
getCSRCsCount() const
{ return getHeader()->cc; }
inline const uint32*
getCSRCs() const
{ return static_cast<const uint32*>(&(getHeader()->sources[1])); }
inline uint16
getHdrExtUndefined() const
{ return (isExtended()? getHeaderExt()->undefined : 0); }
inline uint32
getHdrExtSize() const
{ return (isExtended()?
(static_cast<uint32>(ntohs(getHeaderExt()->length)) << 2) :
0); }
inline const unsigned char*
getHdrExtContent() const
{ return (isExtended() ?
(reinterpret_cast<const unsigned char*>(getHeaderExt()) +
sizeof(RTPHeaderExt)) :
NULL); }
inline const unsigned char* const
getRawPacket() const
{ return buffer; }
inline uint32
getRawPacketSize() const
{ return total; }
inline uint32
getRawPacketSizeSrtp() const
{ return total + srtpLength; }
inline size_t
getSizeOfFixedHeader() const
{ return sizeof(RTPFixedHeader); }
void reComputePayLength(bool padding);
protected:
inline virtual ~RTPPacket()
{ endPacket(); }
void
endPacket();
inline RTPFixedHeader*
getHeader() const
{ return reinterpret_cast<RTPFixedHeader*>(buffer); }
inline void
setExtension(bool e)
{ getHeader()->extension = e; }
inline const RTPHeaderExt*
getHeaderExt() const
{
uint32 fixsize = sizeof(RTPFixedHeader) + (getHeader()->cc << 2);
return (reinterpret_cast<RTPHeaderExt*>(buffer + fixsize));
}
inline uint32
getRawTimestamp() const
{ return ntohl(getHeader()->timestamp); }
inline void
setbuffer(const void* src, size_t len, size_t pos)
{ memcpy(buffer + pos,src,len); }
uint16 cachedSeqNum;
uint32 cachedTimestamp;
uint32 srtpDataOffset;
int32 srtpLength;
uint32 total;
uint32 payloadSize;
private:
unsigned char* buffer;
uint32 hdrSize;
bool duplicated;
#ifdef CCXX_PACKED
#pragma pack(1)
#endif
struct RTPFixedHeader
{
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned char version:2;
unsigned char padding:1;
unsigned char extension:1;
unsigned char cc:4;
unsigned char marker:1;
unsigned char payload:7;
#else
unsigned char cc:4;
unsigned char extension:1;
unsigned char padding:1;
unsigned char version:2;
unsigned char payload:7;
unsigned char marker:1;
#endif
uint16 sequence;
uint32 timestamp;
uint32 sources[1];
};
public:
struct RFC2833Payload
{
#if __BYTE_ORDER == __BIG_ENDIAN
uint8 event : 8;
bool ebit : 1;
bool rbit : 1;
uint8 vol : 6;
uint16 duration : 16;
#else
uint8 event : 8;
uint8 vol : 6;
bool rbit : 1;
bool ebit : 1;
uint16 duration : 16;
#endif
};
private:
struct RTPHeaderExt
{
uint16 undefined;
uint16 length;
};
#ifdef CCXX_PACKED
#pragma pack()
#endif
/* definitions for access to most common 2833 fields... */
public:
inline struct RFC2833Payload *getRaw2833Payload(void)
{return (struct RFC2833Payload *)getPayload();}
inline uint16 get2833Duration(void)
{return ntohs(getRaw2833Payload()->duration);}
inline void set2833Duration(uint16 timestamp)
{getRaw2833Payload()->duration = htons(timestamp);}
};
class __EXPORT OutgoingRTPPkt : public RTPPacket
{
public:
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);
OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
const unsigned char* const data, size_t datalen,
uint8 paddinglen= 0, CryptoContext* pcc= NULL);
OutgoingRTPPkt(const unsigned char* const data, size_t datalen,
uint8 paddinglen= 0, CryptoContext* pcc= NULL);
~OutgoingRTPPkt()
{ }
inline void
setPayloadType(PayloadType pt)
{ getHeader()->payload = pt; }
inline void
setSeqNum(uint16 seq)
{
cachedSeqNum = seq;
getHeader()->sequence = htons(seq);
}
inline void
setTimestamp(uint32 pts)
{
cachedTimestamp = pts;
getHeader()->timestamp = htonl(pts);
}
inline void
setSSRC(uint32 ssrc) const
{ getHeader()->sources[0] = htonl(ssrc); }
inline void
setSSRCNetwork(uint32 ssrc) const
{ getHeader()->sources[0] = ssrc; }
inline void
setMarker(bool mark)
{ getHeader()->marker = mark; }
void protect(uint32 ssrc, CryptoContext* pcc);
inline bool
operator==(const OutgoingRTPPkt &p) const
{ return ( this->getSeqNum() == p.getSeqNum() ); }
inline bool
operator!=(const OutgoingRTPPkt &p) const
{ return ( this->getSeqNum() != p.getSeqNum() ); }
private:
OutgoingRTPPkt(const OutgoingRTPPkt &o);
OutgoingRTPPkt&
operator=(const OutgoingRTPPkt &o);
void setCSRCArray(const uint32* const csrcs, uint16 numcsrc);
};
class __EXPORT IncomingRTPPkt : public RTPPacket
{
public:
IncomingRTPPkt(const unsigned char* block, size_t len);
~IncomingRTPPkt()
{ }
inline bool
isHeaderValid()
{ return headerValid; }
inline uint32
getSSRC() const
{ return cachedSSRC; }
int32
unprotect(CryptoContext* pcc);
inline bool
operator==(const IncomingRTPPkt &p) const
{ return ( (this->getSeqNum() == p.getSeqNum()) &&
(this->getSSRC() == p.getSSRC()) ); }
inline bool
operator!=(const IncomingRTPPkt &p) const
{ return !( *this == p ); }
private:
IncomingRTPPkt(const IncomingRTPPkt &ip);
IncomingRTPPkt&
operator=(const IncomingRTPPkt &ip);
bool headerValid;
uint32 cachedSSRC;
// Masks for RTP header validation: types matching RTCP SR or
// RR must be rejected to avoid accepting misaddressed RTCP
// packets.
static const uint16 RTP_INVALID_PT_MASK;
static const uint16 RTP_INVALID_PT_VALUE;
};
// rtppacket
END_NAMESPACE
#endif // ndef CCXX_RTP_RTPPKT_H_
CryptoContext.h
base.h
Base elements for RTP stacks: constants, types and global functions.
CryptoContext
The implementation for a SRTP cryptographic context.
Definition CryptoContext.h:83
IncomingRTPPkt
RTP packets received from other participants.
Definition rtppkt.h:706
IncomingRTPPkt::~IncomingRTPPkt
~IncomingRTPPkt()
Definition rtppkt.h:722
IncomingRTPPkt::unprotect
int32 unprotect(CryptoContext *pcc)
Unprotect a received packet.
IncomingRTPPkt::getSSRC
uint32 getSSRC() const
Get synchronization source numeric identifier.
Definition rtppkt.h:741
IncomingRTPPkt::isHeaderValid
bool isHeaderValid()
Get validity of this packet.
Definition rtppkt.h:731
IncomingRTPPkt::operator!=
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
IncomingRTPPkt::operator==
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::IncomingRTPPkt
IncomingRTPPkt(const unsigned char *block, size_t len)
Build an RTP packet object from a data buffer.
OutgoingRTPPkt
RTP packets being sent.
Definition rtppkt.h:509
OutgoingRTPPkt::setSSRCNetwork
void setSSRCNetwork(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition rtppkt.h:635
OutgoingRTPPkt::setMarker
void setMarker(bool mark)
Specify the value of the marker bit.
Definition rtppkt.h:646
OutgoingRTPPkt::protect
void protect(uint32 ssrc, CryptoContext *pcc)
Called packet is setup.
OutgoingRTPPkt::OutgoingRTPPkt
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,...
OutgoingRTPPkt::setTimestamp
void setTimestamp(uint32 pts)
Definition rtppkt.h:611
OutgoingRTPPkt::OutgoingRTPPkt
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.
OutgoingRTPPkt::setSeqNum
void setSeqNum(uint16 seq)
Definition rtppkt.h:601
OutgoingRTPPkt::OutgoingRTPPkt
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...
OutgoingRTPPkt::setPayloadType
void setPayloadType(PayloadType pt)
Definition rtppkt.h:592
OutgoingRTPPkt::operator!=
bool operator!=(const OutgoingRTPPkt &p) const
Outgoing packets are not equal if their sequence numbers differ.
Definition rtppkt.h:668
OutgoingRTPPkt::operator==
bool operator==(const OutgoingRTPPkt &p) const
Outgoing packets are equal if their sequence numbers match.
Definition rtppkt.h:661
OutgoingRTPPkt::setSSRC
void setSSRC(uint32 ssrc) const
Set synchronization source numeric identifier.
Definition rtppkt.h:624
OutgoingRTPPkt::~OutgoingRTPPkt
~OutgoingRTPPkt()
Definition rtppkt.h:585
RTPPacket::getSizeOfFixedHeader
size_t getSizeOfFixedHeader() const
Definition rtppkt.h:287
RTPPacket::srtpLength
int32 srtpLength
Lebgth of additional SRTP data.
Definition rtppkt.h:374
RTPPacket::srtpDataOffset
uint32 srtpDataOffset
Offset into packet memory pointing to area for SRTP data.
Definition rtppkt.h:367
RTPPacket::getProtocolVersion
uint8 getProtocolVersion() const
Definition rtppkt.h:157
RTPPacket::payloadSize
uint32 payloadSize
note: payload (not full packet) size.
Definition rtppkt.h:380
RTPPacket::endPacket
void endPacket()
Free memory allocated for the packet.
RTPPacket::getRawPacketSizeSrtp
uint32 getRawPacketSizeSrtp() const
Definition rtppkt.h:283
RTPPacket::getPaddingSize
uint8 getPaddingSize() const
Get the number of octets padding the end of the payload section.
Definition rtppkt.h:175
RTPPacket::getTimestamp
uint32 getTimestamp() const
Definition rtppkt.h:150
RTPPacket::getPayloadType
PayloadType getPayloadType() const
Definition rtppkt.h:136
RTPPacket::setbuffer
void setbuffer(const void *src, size_t len, size_t pos)
Definition rtppkt.h:353
RTPPacket::getRawPacket
const unsigned char *const getRawPacket() const
Get the raw packet as it will be sent through the network.
Definition rtppkt.h:269
RTPPacket::getPayload
const uint8 *const getPayload() const
Definition rtppkt.h:122
RTPPacket::isMarked
bool isMarked() const
Ask whether the packet is marked (for isntance, is a new talk spurt in some audio profiles).
Definition rtppkt.h:185
RTPPacket::isPadded
bool isPadded() const
Ask whether the packet contains padding bytes at the end.
Definition rtppkt.h:165
RTPPacket::getHdrExtUndefined
uint16 getHdrExtUndefined() const
Get the first 16 bits (in network order) of the header of the RTP header extension.
Definition rtppkt.h:229
RTPPacket::getRawPacketSize
uint32 getRawPacketSize() const
Get the raw packet length, including header, extension, payload and padding.
Definition rtppkt.h:279
RTPPacket::cachedTimestamp
uint32 cachedTimestamp
Packet timestamp in host order (includes initial shift).
Definition rtppkt.h:359
RTPPacket::RTPPacket
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...
RTPPacket::getHdrExtSize
uint32 getHdrExtSize() const
Get the length (in octets) of the data contained in the header extension.
Definition rtppkt.h:244
RTPPacket::getHeaderSize
uint32 getHeaderSize() const
Get the length of the header, including contributing sources identifiers and header extension,...
Definition rtppkt.h:115
RTPPacket::isExtended
bool isExtended() const
Ask whether the packet contains header extensions.
Definition rtppkt.h:194
RTPPacket::getSeqNum
uint16 getSeqNum() const
Definition rtppkt.h:143
RTPPacket::getHeader
RTPFixedHeader * getHeader() const
Return low level structure for the header of the packet.
Definition rtppkt.h:322
RTPPacket::set2833Duration
void set2833Duration(uint16 timestamp)
Set 2833 duration field.
Definition rtppkt.h:494
RTPPacket::getHeaderExt
const RTPHeaderExt * getHeaderExt() const
Get a pointer to RTPHeaderExt pointing after the RTP header (fixed part plus contributing sources).
Definition rtppkt.h:337
RTPPacket::getRawTimestamp
uint32 getRawTimestamp() const
Obtain the absolute timestamp carried in the packet header.
Definition rtppkt.h:349
RTPPacket::getCSRCs
const uint32 * getCSRCs() const
Get the 32-bit identifiers of the contributing sources for the packet as an array,...
Definition rtppkt.h:213
RTPPacket::setExtension
void setExtension(bool e)
Definition rtppkt.h:326
RTPPacket::RTPPacket
RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen, CryptoContext *pcc=NULL)
Construct a packet object without specifying its real content yet.
RTPPacket::reComputePayLength
void reComputePayLength(bool padding)
Re-compute payload length.
RTPPacket::getPayloadSize
uint32 getPayloadSize() const
Definition rtppkt.h:129
RTPPacket::get2833Duration
uint16 get2833Duration(void)
Fetch 2833 duration field.
Definition rtppkt.h:486
RTPPacket::cachedSeqNum
uint16 cachedSeqNum
Packet sequence number in host order.
Definition rtppkt.h:357
RTPPacket::total
uint32 total
total length, including header, payload and padding
Definition rtppkt.h:377
RTPPacket::getCSRCsCount
uint16 getCSRCsCount() const
Get the number of contributing sources specified in the packet header.
Definition rtppkt.h:202
RTPPacket::getHdrExtContent
const unsigned char * getHdrExtContent() const
Get the content of the header extension.
Definition rtppkt.h:256
RTPPacket::~RTPPacket
virtual ~RTPPacket()
Destructor, free the buffer provided in the constructor.
Definition rtppkt.h:307
RTPPacket::getRaw2833Payload
struct RFC2833Payload * getRaw2833Payload(void)
Fetch a raw 2833 packet.
Definition rtppkt.h:478
formats.h
Payload types and formats.
PayloadType
uint8 PayloadType
RTP payload type numeric identifier.
Definition formats.h:63
RTPPacket::RFC2833Payload
a structure defining RFC2833 Telephony events.
Definition rtppkt.h:437
RTPPacket::RFC2833Payload::duration
uint16 duration
Definition rtppkt.h:443
RTPPacket::RFC2833Payload::event
uint8 event
Definition rtppkt.h:439
RTPPacket::RFC2833Payload::vol
uint8 vol
Definition rtppkt.h:442
RTPPacket::RFC2833Payload::rbit
bool rbit
Definition rtppkt.h:441
RTPPacket::RFC2833Payload::ebit
bool ebit
Definition rtppkt.h:440