libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
rtppacket.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cassert>
11#include <cstring>
13
15
59class RtpPacket : public BufferView {
60 public:
63
66
68 static constexpr size_t HeaderSize = 12;
69
71 RtpPacket() = default;
72
80 : BufferView(std::move(buf), offset, size) { }
81
87 bool isNull() const { return data() == nullptr; }
88
97 bool isValid() const {
98 if(data() == nullptr) return false;
99 if(size() < HeaderSize) return false;
100 if(((data()[0] >> 6) & 0x03) != 2) return false;
101 return headerSize() != 0;
102 }
103
113 explicit RtpPacket(size_t packetSize)
114 : BufferView(Buffer::Ptr::create(packetSize), 0, packetSize)
115 {
116 std::memset(data(), 0, packetSize);
117 setVersion(2);
118 }
119
130 static List createList(size_t count, size_t packetSize) {
131 List ret;
132 if(count == 0 || packetSize == 0) return ret;
133 size_t totalSize = count * packetSize;
134 auto buf = Buffer::Ptr::create(totalSize);
135 std::memset(buf->data(), 0, totalSize);
136 ret.reserve(count);
137 for(size_t i = 0; i < count; ++i) {
139 pkt.setVersion(2);
140 ret.pushToBack(std::move(pkt));
141 }
142 return ret;
143 }
144
155 static List createList(const SizeList &sizes) {
156 List ret;
157 if(sizes.isEmpty()) return ret;
158 size_t totalSize = 0;
159 for(size_t i = 0; i < sizes.size(); ++i) totalSize += sizes[i];
160 if(totalSize == 0) return ret;
161 auto buf = Buffer::Ptr::create(totalSize);
162 std::memset(buf->data(), 0, totalSize);
163 ret.reserve(sizes.size());
164 size_t offset = 0;
165 for(size_t i = 0; i < sizes.size(); ++i) {
167 if(sizes[i] >= HeaderSize) pkt.setVersion(2);
168 ret.pushToBack(std::move(pkt));
169 offset += sizes[i];
170 }
171 return ret;
172 }
173
175 uint8_t version() const { return (hdr()[0] >> 6) & 0x03; }
176
178 void setVersion(uint8_t v) { hdr()[0] = (hdr()[0] & 0x3F) | ((v & 0x03) << 6); }
179
181 bool padding() const { return (hdr()[0] >> 5) & 0x01; }
182
184 void setPadding(bool p) { hdr()[0] = (hdr()[0] & 0xDF) | (p ? 0x20 : 0x00); }
185
187 bool extension() const { return (hdr()[0] >> 4) & 0x01; }
188
190 void setExtension(bool x) { hdr()[0] = (hdr()[0] & 0xEF) | (x ? 0x10 : 0x00); }
191
193 uint8_t csrcCount() const { return hdr()[0] & 0x0F; }
194
196 bool marker() const { return (hdr()[1] >> 7) & 0x01; }
197
199 void setMarker(bool m) { hdr()[1] = (hdr()[1] & 0x7F) | (m ? 0x80 : 0x00); }
200
202 uint8_t payloadType() const { return hdr()[1] & 0x7F; }
203
205 void setPayloadType(uint8_t pt) { hdr()[1] = (hdr()[1] & 0x80) | (pt & 0x7F); }
206
209 return (static_cast<uint16_t>(hdr()[2]) << 8) | hdr()[3];
210 }
211
214 hdr()[2] = static_cast<uint8_t>(seq >> 8);
215 hdr()[3] = static_cast<uint8_t>(seq & 0xFF);
216 }
217
220 return (static_cast<uint32_t>(hdr()[4]) << 24) |
221 (static_cast<uint32_t>(hdr()[5]) << 16) |
222 (static_cast<uint32_t>(hdr()[6]) << 8) |
223 static_cast<uint32_t>(hdr()[7]);
224 }
225
228 hdr()[4] = static_cast<uint8_t>((ts >> 24) & 0xFF);
229 hdr()[5] = static_cast<uint8_t>((ts >> 16) & 0xFF);
230 hdr()[6] = static_cast<uint8_t>((ts >> 8) & 0xFF);
231 hdr()[7] = static_cast<uint8_t>(ts & 0xFF);
232 }
233
235 uint32_t ssrc() const {
236 return (static_cast<uint32_t>(hdr()[8]) << 24) |
237 (static_cast<uint32_t>(hdr()[9]) << 16) |
238 (static_cast<uint32_t>(hdr()[10]) << 8) |
239 static_cast<uint32_t>(hdr()[11]);
240 }
241
244 hdr()[8] = static_cast<uint8_t>((s >> 24) & 0xFF);
245 hdr()[9] = static_cast<uint8_t>((s >> 16) & 0xFF);
246 hdr()[10] = static_cast<uint8_t>((s >> 8) & 0xFF);
247 hdr()[11] = static_cast<uint8_t>(s & 0xFF);
248 }
249
259 size_t headerSize() const {
260 if(size() < HeaderSize) return 0;
261 size_t hs = HeaderSize + csrcCount() * 4;
262 if(hs > size()) return 0;
263 if(extension()) {
264 // Need at least 4 bytes for the extension header
265 if(hs + 4 > size()) return 0;
266 const uint8_t *ext = data() + hs;
267 uint16_t extLen = (static_cast<uint16_t>(ext[2]) << 8) | ext[3];
268 hs += 4 + extLen * 4;
269 if(hs > size()) return 0;
270 }
271 return hs;
272 }
273
279 if(!extension()) return 0;
280 size_t extOffset = HeaderSize + csrcCount() * 4;
281 if(extOffset + 4 > size()) return 0;
282 const uint8_t *ext = data() + extOffset;
283 return (static_cast<uint16_t>(ext[0]) << 8) | ext[1];
284 }
285
291 if(!extension()) return 0;
292 size_t extOffset = HeaderSize + csrcCount() * 4;
293 if(extOffset + 4 > size()) return 0;
294 const uint8_t *ext = data() + extOffset;
295 return (static_cast<uint16_t>(ext[2]) << 8) | ext[3];
296 }
297
304 const uint8_t *payload() const {
305 size_t hs = headerSize();
306 if(hs == 0 || hs >= size()) return nullptr;
307 return data() + hs;
308 }
309
312 size_t hs = headerSize();
313 if(hs == 0 || hs >= size()) return nullptr;
314 return data() + hs;
315 }
316
323 size_t payloadSize() const {
324 size_t hs = headerSize();
325 if(hs == 0 || hs >= size()) return 0;
326 return size() - hs;
327 }
328
334 void clear() {
335 if(isNull()) return;
336 std::memset(data(), 0, size());
337 setVersion(2);
338 }
339
340 private:
341 const uint8_t *hdr() const {
342 assert(data() != nullptr && size() >= HeaderSize);
343 return data();
344 }
345 uint8_t *hdr() {
346 assert(data() != nullptr && size() >= HeaderSize);
347 return data();
348 }
349};
350
Lightweight view into a region of a shared Buffer.
Definition bufferview.h:41
size_t offset() const
Returns the byte offset into the buffer.
Definition bufferview.h:65
size_t size() const
Returns the byte size of this view.
Definition bufferview.h:68
const uint8_t * data() const
Returns a const pointer to this view's data.
Definition bufferview.h:74
Generic memory buffer descriptor with alignment and memory space support.
Definition buffer.h:85
Dynamic array container wrapping std::vector.
Definition list.h:40
List()=default
Default constructor. Creates an empty list.
A BufferView that interprets its data as an RTP packet.
Definition rtppacket.h:59
bool extension() const
Returns the extension flag.
Definition rtppacket.h:187
size_t headerSize() const
Returns the full header size including CSRC list and extension.
Definition rtppacket.h:259
uint8_t version() const
Returns the RTP version (normally 2).
Definition rtppacket.h:175
uint32_t ssrc() const
Returns the SSRC.
Definition rtppacket.h:235
bool isValid() const
Returns true if this is a valid RTP packet.
Definition rtppacket.h:97
static List createList(size_t count, size_t packetSize)
Creates a list of N RtpPackets packed into a single shared buffer.
Definition rtppacket.h:130
uint16_t extensionProfile() const
Returns the extension profile ID, or 0 if no extension is present.
Definition rtppacket.h:278
void setPayloadType(uint8_t pt)
Sets the payload type.
Definition rtppacket.h:205
promeki::List< size_t > SizeList
List of sizes.
Definition rtppacket.h:65
uint8_t csrcCount() const
Returns the CSRC count.
Definition rtppacket.h:193
uint32_t timestamp() const
Returns the timestamp.
Definition rtppacket.h:219
uint16_t sequenceNumber() const
Returns the sequence number.
Definition rtppacket.h:208
static List createList(const SizeList &sizes)
Creates a list of RtpPackets with varying sizes, packed into a single shared buffer.
Definition rtppacket.h:155
void setVersion(uint8_t v)
Sets the RTP version.
Definition rtppacket.h:178
void setMarker(bool m)
Sets the marker bit.
Definition rtppacket.h:199
void setSsrc(uint32_t s)
Sets the SSRC.
Definition rtppacket.h:243
uint8_t * payload()
Returns a pointer to the payload data (after the full header).
Definition rtppacket.h:311
size_t payloadSize() const
Returns the payload size in bytes.
Definition rtppacket.h:323
static constexpr size_t HeaderSize
Minimum RTP fixed header size in bytes.
Definition rtppacket.h:68
promeki::List< RtpPacket > List
List of RtpPacket values.
Definition rtppacket.h:62
RtpPacket(Buffer::Ptr buf, size_t offset, size_t size)
Constructs an RtpPacket referencing a region of a shared buffer.
Definition rtppacket.h:79
void setTimestamp(uint32_t ts)
Sets the timestamp.
Definition rtppacket.h:227
uint16_t extensionLength() const
Returns the extension data length in 32-bit words, or 0 if no extension.
Definition rtppacket.h:290
uint8_t payloadType() const
Returns the payload type (7 bits).
Definition rtppacket.h:202
bool padding() const
Returns the padding flag.
Definition rtppacket.h:181
void setExtension(bool x)
Sets the extension flag.
Definition rtppacket.h:190
void clear()
Zeroes the packet data and resets the RTP version to 2.
Definition rtppacket.h:334
RtpPacket()=default
Default constructor.
void setPadding(bool p)
Sets the padding flag.
Definition rtppacket.h:184
bool isNull() const
Returns true if the buffer pointer is null.
Definition rtppacket.h:87
void setSequenceNumber(uint16_t seq)
Sets the sequence number.
Definition rtppacket.h:213
RtpPacket(size_t packetSize)
Convenience constructor that allocates a buffer for a single packet.
Definition rtppacket.h:113
bool marker() const
Returns the marker bit.
Definition rtppacket.h:196
const uint8_t * payload() const
Returns a pointer to the payload data (after the full header).
Definition rtppacket.h:304
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19