| 1 |
/** |
| 2 |
* @file protoPktRTP.cpp |
| 3 |
* |
| 4 |
* @brief Provides access to RTP Packets |
| 5 |
*/ |
| 6 |
#include "protoPktRTP.h" |
| 7 |
|
| 8 |
const UINT16 ProtoPktRTP::SEQUENCE_MAX = 0xffff; // 16 bits, unsigned (may move to private later) |
| 9 |
const unsigned int ProtoPktRTP::BASE_HDR_LEN = 12; // base header size, as of RFC 3550 |
| 10 |
const unsigned int ProtoPktRTP::CSRC_COUNT_MAX = 15; |
| 11 |
|
| 12 |
ProtoPktRTP::ProtoPktRTP(void* bufferPtr, |
| 13 |
unsigned int numBytes, |
| 14 |
unsigned int pktLength, |
| 15 |
bool freeOnDestruct) |
| 16 |
: ProtoPkt(bufferPtr, numBytes, freeOnDestruct) |
| 17 |
{ |
| 18 |
if (NULL != bufferPtr) |
| 19 |
{ |
| 20 |
if (0 != pktLength) |
| 21 |
InitFromBuffer(pktLength); |
| 22 |
else |
| 23 |
InitIntoBuffer(); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
ProtoPktRTP::~ProtoPktRTP() |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
bool ProtoPktRTP::InitFromBuffer(unsigned int pktLength, |
| 32 |
void* bufferPtr, |
| 33 |
unsigned int bufferBytes, |
| 34 |
bool freeOnDestruct) |
| 35 |
{ |
| 36 |
if (ProtoPkt::InitFromBuffer(pktLength, bufferPtr, bufferBytes, freeOnDestruct)) // sets "pkt_length" |
| 37 |
{ |
| 38 |
if (pktLength < BASE_HDR_LEN) |
| 39 |
{ |
| 40 |
if (NULL != bufferPtr) DetachBuffer(); |
| 41 |
PLOG(PL_ERROR, "ProtoPktRTP::InitFromBuffer() error: insufficient pktLength\n"); |
| 42 |
return false; |
| 43 |
} |
| 44 |
if (VERSION != GetVersion()) |
| 45 |
{ |
| 46 |
if (NULL != bufferPtr) DetachBuffer(); |
| 47 |
PLOG(PL_ERROR, "ProtoPktRTP::InitFromBuffer() error: incompatible version number: %d\n", GetVersion()); |
| 48 |
return false; |
| 49 |
} |
| 50 |
unsigned int hdrLength = BASE_HDR_LEN + GetCsrcCount()*sizeof(UINT32) + GetExtensionLength(); |
| 51 |
if (pktLength < hdrLength) |
| 52 |
{ |
| 53 |
PLOG(PL_ERROR, "ProtoPktRTP::InitFromBuffer() error: bad RTP header for given pkt_length\n"); |
| 54 |
} |
| 55 |
// (TBD) We could check the padding status, too for an extra validity check |
| 56 |
return true; |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
PLOG(PL_ERROR, "ProtoPktRTP::InitFromBuffer() error: insufficient buffer space\n"); |
| 61 |
return false; |
| 62 |
} |
| 63 |
} // end ProtoPktRTP::InitFromBuffer() |
| 64 |
|
| 65 |
bool ProtoPktRTP::GetExtension(Extension& extension) |
| 66 |
{ |
| 67 |
if (HasExtension()) |
| 68 |
{ |
| 69 |
unsigned int extOffset = OFFSET_CSRC_LIST + GetCsrcCount(); |
| 70 |
ASSERT(ProtoPkt::GetLength() > (extOffset << 2)); |
| 71 |
return extension.InitFromBuffer(AccessBuffer32(extOffset), ProtoPkt::GetLength() - (extOffset << 2)); |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
return false; |
| 76 |
} |
| 77 |
} // end ProtoPktRTP::GetExtension() |
| 78 |
|
| 79 |
bool ProtoPktRTP::Init(void* bufferPtr, unsigned int bufferBytes, bool freeOnDestruct) |
| 80 |
{ |
| 81 |
if (NULL != bufferPtr) AttachBuffer(bufferPtr, bufferBytes, freeOnDestruct); |
| 82 |
if (GetBufferLength() >= BASE_HDR_LEN) |
| 83 |
{ |
| 84 |
/// zero everything ... |
| 85 |
memset((char*)AccessBuffer(), 0, BASE_HDR_LEN); |
| 86 |
SetVersion(VERSION); |
| 87 |
ProtoPkt::SetLength(BASE_HDR_LEN); |
| 88 |
return true; |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
if (NULL != bufferPtr) DetachBuffer(); |
| 93 |
PLOG(PL_ERROR, "ProtoPktRTP::Init() error: insufficient buffer_ptr space\n"); |
| 94 |
ProtoPkt::SetLength(0); |
| 95 |
return false; |
| 96 |
} |
| 97 |
} // end ProtoPktRTP::Init() |
| 98 |
|
| 99 |
bool ProtoPktRTP::AppendCsrc(UINT32 csrcId) |
| 100 |
{ |
| 101 |
UINT8 index = GetCsrcCount(); // count before adding next contributing source (CSRC) (next index to use) |
| 102 |
if ((index < CSRC_COUNT_MAX) && ((BASE_HDR_LEN + (index << 2)) <= GetBufferLength())) |
| 103 |
{ |
| 104 |
SetWord32(OFFSET_CSRC_LIST+index, csrcId); |
| 105 |
UINT8& byte = AccessUINT8(OFFSET_CSRC_COUNT); |
| 106 |
byte &= 0xf0; // zero out any previous value |
| 107 |
byte |= ++index; // update |
| 108 |
ProtoPkt::SetLength(4 + ProtoPkt::GetLength()); |
| 109 |
return true; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
PLOG(PL_ERROR, "ProtoPktRTP::AppendCsrc() error: insufficient buffer_ptr or max CSRC count exceeded\n"); |
| 114 |
return false; // no more room in header (according to RFC 3550) |
| 115 |
} |
| 116 |
} // end ProtoPktRTP::AppendCsrc() |
| 117 |
|
| 118 |
bool ProtoPktRTP::AttachExtension(Extension& extension) |
| 119 |
{ |
| 120 |
unsigned int extOffset = OFFSET_CSRC_LIST + GetCsrcCount(); |
| 121 |
bool result = extension.Init(AccessBuffer32(extOffset), GetBufferLength() - (extOffset << 2)); |
| 122 |
if (result) extension.AttachRtpPacket(this); |
| 123 |
return result; |
| 124 |
} // end ProtoPktRTP::AttachExtension() |
| 125 |
|
| 126 |
bool ProtoPktRTP::PackExtension(Extension& extension) |
| 127 |
{ |
| 128 |
unsigned int newLength = (OFFSET_CSRC_LIST + GetCsrcCount()) << 2; // in bytes |
| 129 |
ASSERT(ProtoPkt::GetLength() == newLength); // ??? |
| 130 |
newLength += extension.GetLength(); |
| 131 |
if (newLength <= GetBufferLength()) |
| 132 |
{ |
| 133 |
ProtoPkt::SetLength(newLength); |
| 134 |
SetFlag(EXTENSION); |
| 135 |
return true; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
PLOG(PL_ERROR, "ProtoPktRTP::PackExtension() error: insufficient buffer_ptr space\n"); |
| 140 |
return false; |
| 141 |
} |
| 142 |
} |
| 143 |
// end ProtoPktRTP::PackExtension() |
| 144 |
|
| 145 |
|
| 146 |
void ProtoPktRTP::SetPadding(UINT8 numBytes, char* paddingPtr) |
| 147 |
{ |
| 148 |
if (numBytes > 0) |
| 149 |
{ |
| 150 |
memcpy(AccessBuffer(ProtoPkt::GetLength()), paddingPtr, (NULL != paddingPtr) ? numBytes : 0); |
| 151 |
ProtoPkt::SetLength(ProtoPkt::GetLength() + numBytes - GetPaddingLength()); |
| 152 |
SetUINT8(ProtoPkt::GetLength() - 1, numBytes); |
| 153 |
SetFlag(PADDING); |
| 154 |
} |
| 155 |
} // end ProtoPktRTP::SetPadding() |
| 156 |
|
| 157 |
|
| 158 |
ProtoPktRTP::Extension::Extension(void* bufferPtr, |
| 159 |
unsigned int numBytes, |
| 160 |
bool initFromBuffer, |
| 161 |
bool freeOnDestruct) |
| 162 |
: ProtoPkt(bufferPtr, numBytes, freeOnDestruct), rtp_pkt(NULL) |
| 163 |
{ |
| 164 |
if (initFromBuffer) InitFromBuffer(bufferPtr, numBytes, freeOnDestruct); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
ProtoPktRTP::Extension::~Extension() |
| 169 |
{ |
| 170 |
} |
| 171 |
|
| 172 |
bool ProtoPktRTP::Extension::InitFromBuffer(void* bufferPtr, |
| 173 |
unsigned int bufferBytes, |
| 174 |
bool freeOnDestruct) |
| 175 |
{ |
| 176 |
if (NULL != bufferPtr) AttachBuffer(bufferPtr, bufferBytes, freeOnDestruct); |
| 177 |
if (GetBufferLength() > (OFFSET_LENGTH << 1)) |
| 178 |
{ |
| 179 |
unsigned int extLength = GetDataLength() + 4; |
| 180 |
if (extLength <= GetBufferLength()) |
| 181 |
{ |
| 182 |
ProtoPkt::SetLength(extLength); |
| 183 |
return true; |
| 184 |
} |
| 185 |
} |
| 186 |
if (NULL != bufferPtr) DetachBuffer(); |
| 187 |
PLOG(PL_ERROR, "ProtoPktRTP::Extension::InitFromBuffer() error: insufficient buffer space\n"); |
| 188 |
return false; |
| 189 |
} // end ProtoPktRTP::InitFromBuffer() |
| 190 |
|
| 191 |
bool ProtoPktRTP::Extension::Init(void* bufferPtr, |
| 192 |
unsigned int bufferBytes, |
| 193 |
bool freeOnDestruct) |
| 194 |
{ |
| 195 |
if (NULL != bufferPtr) AttachBuffer(bufferPtr, bufferBytes, freeOnDestruct); |
| 196 |
if (bufferBytes >= 4) |
| 197 |
{ |
| 198 |
SetDataLength(0); |
| 199 |
return true; |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
PLOG(PL_ERROR, "ProtoPktRTP::Extension::Init() error: insufficient buffer space\n"); |
| 204 |
return false; |
| 205 |
} |
| 206 |
} // end ProtoPktRTP::Extension::Init() |
| 207 |
|
| 208 |
bool ProtoPktRTP::Extension::SetData(const char* dataPtr, unsigned int numBytes) |
| 209 |
{ |
| 210 |
if ((4 + numBytes) <= GetBufferLength()) |
| 211 |
{ |
| 212 |
memcpy((char*)AccessData(), dataPtr, numBytes); |
| 213 |
SetDataLength(numBytes); |
| 214 |
return true; |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
PLOG(PL_ERROR, "ProtoPktRTP::Extension::SetData() error: insufficient buffer space\n"); |
| 219 |
return false; |
| 220 |
} |
| 221 |
} // end ProtoPktRTP::Extension::SetData() |