Debian Codegraph @

protolib/include/protoPktRTP.h

raw
1 #ifndef _PROTO_PKT_RTP
2 #define _PROTO_PKT_RTP
3
4 #include "protoPkt.h"
5 #include "protoDebug.h"
6 /**
7 * @class ProtoPktRTP
8 *
9 * @brief Useful for building/parsing Real-Time Protocol (RTP), RFC3550, messages.
10 */
11
12 /* RTP Packet format
13
14 0 1 2 3
15 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
16 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
17 |V=2|P|X| CC |M| PT | sequence number |
18 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
19 | timestamp |
20 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21 | synchronization source (SSRC) identifier |
22 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
23 | contributing source (CSRC) identifiers |
24 | .... |
25 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26
27 */
28
29 class ProtoPktRTP : public ProtoPkt
30 {
31 public:
32 ProtoPktRTP(void* bufferPtr = NULL,
33 unsigned int numBytes = 0,
34 unsigned int pktLength = 0, // inits from buffer if non-zero
35 bool freeOnDestruct = false);
36 virtual ~ProtoPktRTP();
37
38 enum {VERSION = 2};
39
40 static const UINT16 SEQUENCE_MAX; // 16 bits, unsigned
41 static const unsigned int BASE_HDR_LEN; // (12 bytes) base header size, as of RFC 3550
42 static const unsigned int CSRC_COUNT_MAX; // (15)
43
44 enum PayloadType
45 {
46 PCMU = 0,
47 CELP = 1,
48 G721 = 2,
49 GSM = 3,
50 G723 = 4, // patented, not in public domain?
51 DVI48K = 5, // 8k sample rate
52 DVI416K = 6, // 16k sample rate
53 LPC = 7,
54 PCMA = 8,
55 G722 = 9,
56 L16S = 10, // stereo
57 L16M = 11, // monaural
58 QCELP = 12,
59 CN = 13,
60 MPA = 14,
61 G728 = 15,
62
63 // ... (more to add later)
64 // note that dynamic payload types (96-127) are also allowed but must be defined by the application
65 // see RFC 3551, section 3
66 PT_MAX = 128 // ensures that enough size is set aside for dynamic PT's
67 };
68
69 /**
70 * @class Extension
71 *
72 * @brief Extension class for ProtoPkt
73 *
74 * (TBD) Perhaps not create a ProtoPktRTP::Extension class since an RTP packet
75 * can have only one extension anyway? Although this might be useful
76 * to subclass from to create C++ wrappers for different extension types?
77 */
78 class Extension : public ProtoPkt
79 {
80 friend class ProtoPktRTP;
81
82 public:
83 Extension(void* bufferPtr = NULL,
84 unsigned int numBytes = 0,
85 bool initFromBuffer = true,
86 bool freeOnDestruct = false);
87 virtual ~Extension();
88
89 // Use these to build an extension
90 bool Init(void* bufferPtr = NULL,
91 unsigned int bufferBytes = 0,
92 bool freeOnDestruct = false);
93 void SetType(UINT16 type)
94 {SetWord16(OFFSET_TYPE, type);}
95 bool SetData(const char* dataPtr, unsigned int numBytes);
96 void* AccessData()
97 {return AccessBuffer32(OFFSET_DATA);}
98 void SetDataLength(UINT16 numBytes)
99 {
100 ASSERT(0 == (numBytes & 0x03));
101 SetWord16(OFFSET_LENGTH, numBytes >> 2);
102 SetLength(4 + numBytes);
103 }
104 // "Pack()" should be called for an extension that is set using ProtoPktRTP::AttachExtension()
105 // (Call this after, the extension has been completely build)
106 bool Pack()
107 {return ((NULL != rtp_pkt) ? rtp_pkt->PackExtension(*this) : true);}
108
109 // Use these to parse an extension
110 bool InitFromBuffer(void* bufferPtr = NULL,
111 unsigned int bufferBytes = 0,
112 bool freeOnDestruct = false);
113 UINT16 GetType() const
114 {return GetWord16(OFFSET_TYPE);}
115 unsigned int GetDataLength() const
116 {return (GetWord16(OFFSET_LENGTH) << 2);}
117 const void* GetData() const
118 {return GetBuffer32(OFFSET_DATA);}
119
120
121 protected:
122 void AttachRtpPacket(ProtoPktRTP* rtpPkt)
123 {rtp_pkt = rtpPkt;}
124
125 enum
126 {
127 OFFSET_TYPE = 0, // UINT16 offset
128 OFFSET_LENGTH = OFFSET_TYPE+1, // UINT16 offset
129 OFFSET_DATA = ((OFFSET_LENGTH+1)*2 )/4 // UINT32 offset
130 };
131
132 ProtoPktRTP* rtp_pkt;
133 }; // end class ProtoPktRTP::Extension
134
135 // RTP packet parsing
136 bool InitFromBuffer(unsigned int pktLength,
137 void* bufferPtr = NULL,
138 unsigned int bufferBytes = 0,
139 bool freeOnDestruct = false);
140
141 UINT8 GetVersion() const
142 {return (GetUINT8(OFFSET_VERSION) >> 6);}
143
144 bool HasPadding() const
145 { return FlagIsSet(PADDING);}
146
147 bool HasExtension() const
148 {return FlagIsSet(EXTENSION);}
149
150 UINT8 GetCsrcCount() const
151 {return (GetUINT8(OFFSET_CSRC_COUNT) & 0x0f);}
152
153 bool HasMarker() const
154 {return FlagIsSet(MARKER);}
155
156 PayloadType GetPayloadType() const
157 {return (PayloadType)(GetUINT8(OFFSET_PAYLOAD_TYPE) & 0x7f);}
158
159 UINT16 GetSequence() const
160 {return GetWord16(OFFSET_SEQUENCE);}
161
162 UINT32 GetTimestamp() const
163 {return GetWord32(OFFSET_TIMESTAMP);}
164
165 const void* GetTimestampPtr() const
166 {return GetBuffer32(OFFSET_TIMESTAMP);}
167
168 UINT32 GetSsrc() const
169 {return GetWord32(OFFSET_SSRC);}
170
171 UINT32 GetCsrc(unsigned int index) const
172 {return (index < GetCsrcCount()) ? GetWord32(OFFSET_CSRC_LIST+index) : 0;}
173
174 bool GetExtension(Extension& extension);
175 unsigned int GetExtensionLength() const
176 {
177 return (HasExtension() ?
178 (4 + (GetWord16(((OFFSET_CSRC_LIST+GetCsrcCount()) << 1) + 1) << 2)) :
179 0);
180 }
181
182 static unsigned int GetBaseHeaderLength()
183 {return BASE_HDR_LEN;}
184
185 unsigned int GetHeaderLength() const
186 {return (BASE_HDR_LEN + (GetCsrcCount() << 2) + GetExtensionLength());}
187
188 UINT8 GetPaddingLength() const
189 {return (HasPadding() ?
190 GetUINT8(ProtoPkt::GetLength()-1) : 0);}
191
192 unsigned int GetPayloadLength() const
193 {return (ProtoPkt::GetLength() - GetPaddingLength() - BASE_HDR_LEN - 4*GetCsrcCount() - GetExtensionLength());}
194
195 const void* GetPayload() const
196 {return GetBuffer32(GetCsrcCount() + (BASE_HDR_LEN >> 2) + (GetExtensionLength() >> 2));}
197
198
199 // Message building
200 // if "bufferPtr == NULL", current "buffer_ptr" is used
201 bool Init(void* bufferPtr = NULL, unsigned int numBytes = 0, bool freeOnDestruct = false); // to be deprecated
202
203 bool InitIntoBuffer(void* bufferPtr = NULL, unsigned int numBytes = 0, bool freeOnDestruct = false)
204 {return Init(bufferPtr, numBytes, freeOnDestruct);}
205
206 void SetVersion(UINT8 version = VERSION)
207 {
208 UINT8& byte = AccessUINT8(OFFSET_VERSION);
209 byte &= 0x03f; // preserve what is already there
210 byte |= (version << 6);
211 }
212
213 void SetMarker()
214 {SetFlag(MARKER);}
215
216 void ClearMarker()
217 {ClearFlag(MARKER);}
218
219 void SetPayloadType(PayloadType payloadType) // 7 bits at present
220 {
221 UINT8& byte = AccessUINT8(OFFSET_PAYLOAD_TYPE);
222 byte &= 0x80; // preserves marker bit
223 byte |= ((UINT8)payloadType & 0x7f);
224 }
225
226 void SetSequence(UINT16 sequence) // may want this class to maintain the sequence # rather than get it from outside later on
227 {SetWord16(OFFSET_SEQUENCE, sequence);}
228
229 void SetTimestamp(UINT32 timestamp)
230 {SetWord32(OFFSET_TIMESTAMP, timestamp);}
231
232 void SetSSRC(UINT32 srcId)
233 {SetWord32(OFFSET_SSRC, srcId);}
234
235 // Must append any/all CSRC's before attaching extension or payload
236 bool AppendCsrc(UINT32 srcId);
237
238 // When an extension is used, "InitExtension()" must be called _after_
239 // all CCSRC have been appended, and _before_ payload is set
240 bool AttachExtension(Extension& extension);
241 bool PackExtension(Extension& extension);
242
243 // Set payload before any needed padding - may get info from RFC 2833?
244 void SetPayload(const char* dataPtr, unsigned int numBytes)
245 {
246 memcpy((char*)AccessPayload(), dataPtr, numBytes);
247 ProtoPkt::SetLength(GetHeaderLength() + numBytes);
248 }
249 void* AccessPayload()
250 {return AccessBuffer32((BASE_HDR_LEN >> 2) + GetCsrcCount() + (GetExtensionLength() >> 2));}
251 void SetPayloadLength(unsigned int numBytes)
252 {ProtoPkt::SetLength(GetHeaderLength() + numBytes);}
253
254 // Set padding, if needed, last!
255 void SetPadding(UINT8 numBytes, char* paddingPtr = NULL);
256
257 private:
258 // Bit masks for various flags in the first two bytes of RTP header
259 enum Flag
260 {
261 EXTENSION = 0x1000, // indicates extension is present
262 PADDING = 0x2000, // indicates padding is present
263 MARKER = 0x0080
264 };
265 bool FlagIsSet(Flag flag) const
266 {
267 return (0 != ((UINT16)flag & GetWord16(OFFSET_FLAGS)));
268 }
269 void SetFlag(Flag flag)
270 {
271 UINT16 word = GetWord16(OFFSET_FLAGS);
272 word |= (UINT16)flag;
273 SetWord16(OFFSET_FLAGS, word);
274 }
275
276 void ClearFlag(Flag flag)
277 {
278 UINT16 word = GetWord16(OFFSET_FLAGS);
279 word &= ~(UINT16)flag;
280 SetWord16(OFFSET_FLAGS, word);
281 }
282 void ClearAllFlags()
283 {
284 UINT16 clear = EXTENSION | PADDING | MARKER;
285 UINT16 word = GetWord16(OFFSET_FLAGS);
286 word &= ~clear;
287 SetWord16(OFFSET_FLAGS, word);
288 }
289 enum
290 {
291 OFFSET_VERSION = 0, // UINT8 offset
292 OFFSET_FLAGS = OFFSET_VERSION, // UINT16 offset (flags in first 2 bytes)
293 OFFSET_CSRC_COUNT = OFFSET_VERSION, // UINT8 offset, mask 4 lsb's
294 OFFSET_PAYLOAD_TYPE = OFFSET_VERSION+1, // UINT8 offset, mask 7 lsb's
295 OFFSET_SEQUENCE = (OFFSET_PAYLOAD_TYPE+1)/2,// UINT16 offset
296 OFFSET_TIMESTAMP = ((OFFSET_SEQUENCE+1)*2)/4,// UINT32 offset
297 OFFSET_SSRC = OFFSET_TIMESTAMP + 1, // UINT32 offset
298 OFFSET_CSRC_LIST = OFFSET_SSRC + 1 // UINT32 offset
299 };
300 }; // end class ProtoPktRTP
301
302 #endif // _PROTO_PKT_RTP
303