blob: 20809f0829081c90fed38e9366baff49a15e647f [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2002 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 General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16//
17// As a special exception, you may use this file as part of a free software
18// library without restriction. Specifically, if other files instantiate
19// templates or use macros or inline functions from this file, or you compile
20// this file and link it with other files to produce an executable, this
21// file does not by itself cause the resulting executable to be covered by
22// the GNU General Public License. This exception does not however
23// invalidate any other reasons why the executable file might be covered by
24// the GNU General Public License.
25//
26// This exception applies only to the code released under the name GNU
27// ccRTP. If you copy code from other releases into a copy of GNU
28// ccRTP, as the General Public License permits, the exception does
29// not apply to the code that you add in this way. To avoid misleading
30// anyone as to the status of such modified files, you must delete
31// this exception notice from them.
32//
33// If you write modifications of your own for GNU ccRTP, it is your choice
34// whether to permit this exception to apply to your modifications.
35// If you do not wish that, delete this exception notice.
36//
37
Alexandre Lisionddd731e2014-01-31 11:50:08 -050038#ifndef CCXX_RTP_RTPPKT_H_
Emeric Vigier2f625822012-08-06 11:09:52 -040039#define CCXX_RTP_RTPPKT_H_
40
41#include <ccrtp/base.h>
42#include <ccrtp/formats.h>
43#include <ccrtp/CryptoContext.h>
44
Alexandre Lisionddd731e2014-01-31 11:50:08 -050045NAMESPACE_COMMONCPP
Emeric Vigier2f625822012-08-06 11:09:52 -040046
47/**
48 * @file rtppkt.h
49 *
50 * @short RTP packets handling.
51 **/
52
53/**
54 * @defgroup rtppacket RTP data packets manipulation.
55 * @{
56 **/
57
58/**
59 * @class RTPPacket
60 * @short A base class for both IncomingRTPPkt and OutgoingRTPPkt.
61 *
62 * Provides common low level header structures and related
63 * methods. This class provides an interface that allows for partial
64 * and generic manipulation of RTP data packets. Values are returned
65 * in host order, except raw structures, which are returned as they
66 * are sent through the network.
67 *
68 * @author David Sugar <dyfet@ostel.com>
69 **/
70
71class CryptoContext;
72
73class __EXPORT RTPPacket
74{
75private:
Alexandre Lisionddd731e2014-01-31 11:50:08 -050076 struct RTPFixedHeader;
77 struct RTPHeaderExt;
Emeric Vigier2f625822012-08-06 11:09:52 -040078
79public:
Alexandre Lisionddd731e2014-01-31 11:50:08 -050080 /**
81 * Constructor, construct a packet object given the memory
82 * zone its content (header and payload) is stored. Commonly
83 * used to build RTPPacket objects from incoming data.
84 *
85 * @param block whole packet
86 * @param len total length (header + payload + padding) of the
87 * packet
88 * @param duplicate whether to memcopy the packet. At present,
89 * this feature is not used.
90 * @note used in IncomingRTPPkt.
91 **/
92 RTPPacket(const unsigned char* const block, size_t len,
93 bool duplicate = false);
Emeric Vigier2f625822012-08-06 11:09:52 -040094
Alexandre Lisionddd731e2014-01-31 11:50:08 -050095 /**
96 * Construct a packet object without specifying its real
97 * content yet. Commonly used for outgoing packets. Header
98 * fields and payload must be filled in by another methods or
99 * by a derived constructor.
100 *
101 * @param hdrlen length of the header (including CSRC and extension).
102 * @param plen payload length.
103 * @param paddinglen pad packet to a multiple of paddinglen
104 * @note used in OutgoingRTPPkt.
105 */
Emeric Vigier2f625822012-08-06 11:09:52 -0400106 RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen, CryptoContext* pcc= NULL);
107
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500108 /**
109 * Get the length of the header, including contributing
110 * sources identifiers and header extension, if present.
111 *
112 * @return number of octets.
113 **/
114 inline uint32
115 getHeaderSize() const
116 { return hdrSize; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400117
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500118 /**
119 * @return pointer to the payload section of the packet.
120 **/
121 inline const uint8* const
122 getPayload() const
123 { return (uint8*)(buffer + getHeaderSize()); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400124
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500125 /**
126 * @return length of the payload section, in octets.
127 **/
128 inline uint32
129 getPayloadSize() const
130 { return payloadSize; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400131
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500132 /**
133 * @return value of the PT header field.
134 **/
135 inline PayloadType
136 getPayloadType() const
137 { return static_cast<PayloadType>(getHeader()->payload); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400138
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500139 /**
140 * @return value of the sequence number header field, in host order.
141 **/
142 inline uint16
143 getSeqNum() const
144 { return cachedSeqNum; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400145
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500146 /**
147 * @return packet timestamp in host order.
148 **/
149 inline uint32
150 getTimestamp() const
151 { return cachedTimestamp; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400152
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500153 /**
154 * @return RTP protocol version of packet.
155 **/
156 inline uint8
157 getProtocolVersion() const
158 { return getHeader()->version; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400159
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500160 /**
161 * Ask whether the packet contains padding bytes at the end
162 * @return true if the header padding bit is 1.
163 **/
164 inline bool
165 isPadded() const
166 { return getHeader()->padding; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400167
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500168 /**
169 * Get the number of octets padding the end of the payload
170 * section.
171 *
172 * @return Padding length in octets.
173 **/
174 inline uint8
175 getPaddingSize() const
176 { return buffer[total - 1]; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400177
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500178 /**
179 * Ask whether the packet is marked (for isntance, is a new
180 * talk spurt in some audio profiles).
181 *
182 * @return true is the header marker bit is 1.
183 **/
184 inline bool
185 isMarked() const
186 { return getHeader()->marker; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400187
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500188 /**
189 * Ask whether the packet contains header extensions.
190 *
191 * @return true if the header extension bit is 1.
192 **/
193 inline bool
194 isExtended() const
195 { return getHeader()->extension; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400196
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500197 /**
198 * Get the number of contributing sources specified in the
199 * packet header.
200 **/
201 inline uint16
202 getCSRCsCount() const
203 { return getHeader()->cc; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400204
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500205 /**
206 * Get the 32-bit identifiers of the contributing sources for
207 * the packet as an array, of length getCSRCsCount().
208 *
209 * @return An array of CSRC identifiers as they are in the
210 * packet (in network order).
211 **/
212 inline const uint32*
213 getCSRCs() const
214 { return static_cast<const uint32*>(&(getHeader()->sources[1])); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400215
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500216 /**
217 * Get the first 16 bits (in network order) of the header of
218 * the RTP header extension. Its meaning is undefined at this
219 * level.
220 *
221 * @return 0 if the packet has no header extension, otherwise
222 * the first 16 bits of the header extension, in
223 * network order.
224 *
225 * @note 0 could be a valid value for the first 16 bits, in
226 * that case RTPPacket::isExtended() should be use.
227 **/
228 inline uint16
229 getHdrExtUndefined() const
230 { return (isExtended()? getHeaderExt()->undefined : 0); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400231
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500232 /**
233 * Get the length (in octets) of the data contained in the
234 * header extension. Note that this length does not include
235 * the four octets at the beginning of the header extension.
236 *
237 * @return 0 if the packet has no header extension, otherwise
238 * the length.
239 *
240 * @note 0 is a valid value for this field, so
241 * RTPPacket::isExtended() should be used.
242 **/
243 inline uint32
244 getHdrExtSize() const
245 { return (isExtended()?
246 (static_cast<uint32>(ntohs(getHeaderExt()->length)) << 2) :
247 0); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400248
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500249 /**
250 * Get the content of the header extension.
251 *
252 * @return NULL if the packet has no header extension, otherwise
253 * a pointer to the packet header extension content.
254 **/
255 inline const unsigned char*
256 getHdrExtContent() const
257 { return (isExtended() ?
258 (reinterpret_cast<const unsigned char*>(getHeaderExt()) +
259 sizeof(RTPHeaderExt)) :
260 NULL); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400261
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500262 /**
263 * Get the raw packet as it will be sent through the network.
264 *
265 * @return memory zone where the raw packet structure is
266 * stored in.
267 **/
268 inline const unsigned char* const
269 getRawPacket() const
270 { return buffer; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400271
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500272 /**
273 * Get the raw packet length, including header, extension,
274 * payload and padding.
275 *
276 * @return size of the raw packet structure.
277 **/
278 inline uint32
279 getRawPacketSize() const
280 { return total; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400281
282 inline uint32
283 getRawPacketSizeSrtp() const
284 { return total + srtpLength; }
285
286 inline size_t
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500287 getSizeOfFixedHeader() const
288 { return sizeof(RTPFixedHeader); }
289
290 /**
Emeric Vigier2f625822012-08-06 11:09:52 -0400291 * Re-compute payload length.
292 *
293 * This recomputation may be necessary in case of SRTP. We need to decrypt
294 * the packet before we can handle padding. See @c takeInDataPacket in
295 * @c incqueue.cpp
296 *
297 * @param padding
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500298 * If true then set padding flag in RTP header and re-compute
Emeric Vigier2f625822012-08-06 11:09:52 -0400299 * payloadSize.
300 */
301 void reComputePayLength(bool padding);
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500302
Emeric Vigier2f625822012-08-06 11:09:52 -0400303protected:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500304 /**
305 * Destructor, free the buffer provided in the constructor.
306 **/
307 inline virtual ~RTPPacket()
308 { endPacket(); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400309
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500310 /**
311 * Free memory allocated for the packet.
312 **/
313 void
314 endPacket();
Emeric Vigier2f625822012-08-06 11:09:52 -0400315
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500316 /**
317 * Return low level structure for the header of the packet.
318 *
319 * @return RTPFixedHeader pointer to the header of the packet.
320 **/
321 inline RTPFixedHeader*
322 getHeader() const
323 { return reinterpret_cast<RTPFixedHeader*>(buffer); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400324
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500325 inline void
326 setExtension(bool e)
327 { getHeader()->extension = e; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400328
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500329 /**
330 * Get a pointer to RTPHeaderExt pointing after the RTP header
331 * (fixed part plus contributing sources). No check for
332 * for the X bit is done.
333 *
334 * @return header extension if present, garbage if not.
335 **/
336 inline const RTPHeaderExt*
337 getHeaderExt() const
338 {
Emeric Vigier2f625822012-08-06 11:09:52 -0400339 uint32 fixsize = sizeof(RTPFixedHeader) + (getHeader()->cc << 2);
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500340 return (reinterpret_cast<RTPHeaderExt*>(buffer + fixsize));
341 }
Emeric Vigier2f625822012-08-06 11:09:52 -0400342
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500343 /**
344 * Obtain the absolute timestamp carried in the packet header.
345 *
346 * @return 32-bit timestamp in host order.
347 **/
348 inline uint32
349 getRawTimestamp() const
350 { return ntohl(getHeader()->timestamp); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400351
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500352 inline void
353 setbuffer(const void* src, size_t len, size_t pos)
354 { memcpy(buffer + pos,src,len); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400355
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500356 /// Packet sequence number in host order.
357 uint16 cachedSeqNum;
358 /// Packet timestamp in host order (includes initial shift).
359 uint32 cachedTimestamp;
Emeric Vigier2f625822012-08-06 11:09:52 -0400360
361 /**
362 * Offset into packet memory pointing to area for SRTP data.
363 *
364 * This offset points to the memory where the SRTP protect will
365 * store the authentication and MKI data.
366 */
367 uint32 srtpDataOffset;
368
369 /**
370 * Lebgth of additional SRTP data.
371 *
372 * Covers the SRTP authentication and MKI data.
373 */
374 int32 srtpLength;
375
376 /// total length, including header, payload and padding
377 uint32 total;
378
379 /// note: payload (not full packet) size.
380 uint32 payloadSize;
381
382private:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500383 /// packet in memory
384 unsigned char* buffer;
385 /// size of the header, including contributing sources and extensions
386 uint32 hdrSize;
387 /// whether the object was contructed with duplicated = true
388 bool duplicated;
Emeric Vigier2f625822012-08-06 11:09:52 -0400389
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500390#ifdef CCXX_PACKED
Emeric Vigier2f625822012-08-06 11:09:52 -0400391#pragma pack(1)
392#endif
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500393 /**
394 * @struct RTPFixedHeader
395 * @short RTP fixed header as it is send through the network.
396 *
397 * A low-level representation for generic RTP packet header as
398 * defined in RFC 1889. A packet consists of the fixed RTP
399 * header, a possibly empty list of contributing sources and
400 * the payload. Header contents are kept in network (big
401 * endian) order.
402 **/
403 struct RTPFixedHeader
404 {
405#if __BYTE_ORDER == __BIG_ENDIAN
406 /// For big endian boxes
407 unsigned char version:2; ///< Version, currently 2
408 unsigned char padding:1; ///< Padding bit
409 unsigned char extension:1; ///< Extension bit
410 unsigned char cc:4; ///< CSRC count
411 unsigned char marker:1; ///< Marker bit
412 unsigned char payload:7; ///< Payload type
Emeric Vigier2f625822012-08-06 11:09:52 -0400413#else
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500414 /// For little endian boxes
415 unsigned char cc:4; ///< CSRC count
416 unsigned char extension:1; ///< Extension bit
417 unsigned char padding:1; ///< Padding bit
418 unsigned char version:2; ///< Version, currently 2
419 unsigned char payload:7; ///< Payload type
420 unsigned char marker:1; ///< Marker bit
Emeric Vigier2f625822012-08-06 11:09:52 -0400421#endif
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500422 uint16 sequence; ///< sequence number
423 uint32 timestamp; ///< timestamp
424 uint32 sources[1]; ///< contributing sources
425 };
Emeric Vigier2f625822012-08-06 11:09:52 -0400426
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500427 /**
428 * @struct RFC2833Payload
429 * @short a structure defining RFC2833 Telephony events.
430 *
431 * structure to define RFC2833 telephony events in RTP. You can
432 * use this by recasing the pointer returned by getPayload().
433 */
Emeric Vigier2f625822012-08-06 11:09:52 -0400434
435public:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500436 struct RFC2833Payload
437 {
Emeric Vigier2f625822012-08-06 11:09:52 -0400438#if __BYTE_ORDER == __BIG_ENDIAN
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500439 uint8 event : 8;
440 bool ebit : 1;
441 bool rbit : 1;
442 uint8 vol : 6;
443 uint16 duration : 16;
Emeric Vigier2f625822012-08-06 11:09:52 -0400444#else
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500445 uint8 event : 8;
446 uint8 vol : 6;
447 bool rbit : 1;
448 bool ebit : 1;
449 uint16 duration : 16;
Emeric Vigier2f625822012-08-06 11:09:52 -0400450#endif
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500451 };
Emeric Vigier2f625822012-08-06 11:09:52 -0400452
453private:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500454 /**
455 * @struct RTPHeaderExt
456 *
457 * Fixed component of the variable-length header extension,
458 * appended to the fixed header, after the CSRC list, when X
459 * == 1.
460 **/
461 struct RTPHeaderExt
462 {
463 uint16 undefined; ///< to be defined
464 uint16 length; ///< number of 32-bit words in the extension
465 };
466#ifdef CCXX_PACKED
Emeric Vigier2f625822012-08-06 11:09:52 -0400467#pragma pack()
468#endif
469
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500470 /* definitions for access to most common 2833 fields... */
Emeric Vigier2f625822012-08-06 11:09:52 -0400471
472public:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500473 /**
474 * Fetch a raw 2833 packet.
475 *
476 * @return low level 2833 data structure.
477 */
478 inline struct RFC2833Payload *getRaw2833Payload(void)
479 {return (struct RFC2833Payload *)getPayload();}
Emeric Vigier2f625822012-08-06 11:09:52 -0400480
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500481 /**
482 * Fetch 2833 duration field.
483 *
484 * @return 2833 duration in native host machine byte order.
485 */
486 inline uint16 get2833Duration(void)
487 {return ntohs(getRaw2833Payload()->duration);}
Emeric Vigier2f625822012-08-06 11:09:52 -0400488
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500489 /**
490 * Set 2833 duration field.
491 *
492 * @param timestamp to use, native host machine byte order.
493 */
494 inline void set2833Duration(uint16 timestamp)
495 {getRaw2833Payload()->duration = htons(timestamp);}
Emeric Vigier2f625822012-08-06 11:09:52 -0400496};
497
498/**
499 * @class OutgoingRTPPkt
500 * @short RTP packets being sent.
501 *
502 * This class is intented to construct packet objects just before they
503 * are inserted into the sending queue, so that they are processed in
504 * a understandable and format independent manner inside the stack.
505 *
506 * @author Federico Montesino Pouzols <fedemp@altern.org>
507 **/
508class __EXPORT OutgoingRTPPkt : public RTPPacket
509{
510public:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500511 /**
512 * Construct a new packet to be sent, containing several
513 * contributing source identifiers, header extensions and
514 * payload.
Emeric Vigier2f625822012-08-06 11:09:52 -0400515 *
516 * A new copy in memory (holding all this components
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500517 * along with the fixed header) is created. If the pointer
Emeric Vigier2f625822012-08-06 11:09:52 -0400518 * to the SRTP CryptoContext is not NULL and holds a CryptoContext
519 * for the SSRC take the SSRC data into account when computing
520 * the required memory buffer.
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500521 *
522 * @param csrcs array of countributing source 32-bit
523 * identifiers, in host order.
524 * @param numcsrc number of CSRC identifiers in the array.
525 * @param hdrext whole header extension.
526 * @param hdrextlen size of whole header extension, in octets.
527 * @param data payload.
528 * @param datalen payload length, in octets.
529 * @param paddinglen pad packet to a multiple of paddinglen.
Emeric Vigier2f625822012-08-06 11:09:52 -0400530 * @param pcc Pointer to the SRTP CryptoContext, defaults to NULL
531 * if not specified.
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500532 *
533 * @note For efficiency purposes, since this constructor is
534 * valid for all packets but is too complex for the common
535 * case, two simpler others are provided.
536 **/
537 OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
538 const unsigned char* const hdrext, uint32 hdrextlen,
539 const unsigned char* const data, size_t datalen,
Emeric Vigier2f625822012-08-06 11:09:52 -0400540 uint8 paddinglen= 0, CryptoContext* pcc= NULL);
541
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500542 /**
543 * Construct a new packet to be sent, containing several
544 * contributing source identifiers and payload.
Emeric Vigier2f625822012-08-06 11:09:52 -0400545 *
546 * A new copy in
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500547 * memory (holding all this components along with the fixed
Emeric Vigier2f625822012-08-06 11:09:52 -0400548 * header) is created. If the pointer
549 * to the SRTP CryptoContext is not NULL and holds a CryptoContext
550 * for the SSRC take the SSRC data into account when computing
551 * the required memory buffer.
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500552 *
553 * @param csrcs array of countributing source 32-bit
554 * identifiers, in host order.
555 * @param numcsrc number of CSRC identifiers in the array.
556 * @param data payload.
557 * @param datalen payload length, in octets.
558 * @param paddinglen pad packet to a multiple of paddinglen.
Emeric Vigier2f625822012-08-06 11:09:52 -0400559 * @param pcc Pointer to the SRTP CryptoContext, defaults to NULL
560 * if not specified.
561 **/
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500562 OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
563 const unsigned char* const data, size_t datalen,
Emeric Vigier2f625822012-08-06 11:09:52 -0400564 uint8 paddinglen= 0, CryptoContext* pcc= NULL);
565
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500566 /**
567 * Construct a new packet (fast variant, with no contributing
568 * sources and no header extension) to be sent.
Emeric Vigier2f625822012-08-06 11:09:52 -0400569 *
570 * A new copy in
571 * memory (holding the whole packet) is created. If the pointer
572 * to the SRTP CryptoContext is not NULL and holds a CryptoContext
573 * for the SSRC take the SSRC data into account when computing
574 * the required memory buffer.
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500575 *
576 * @param data payload.
577 * @param datalen payload length, in octets.
578 * @param paddinglen pad packet to a multiple of paddinglen.
Emeric Vigier2f625822012-08-06 11:09:52 -0400579 * @param pcc Pointer to the SRTP CryptoContext, defaults to NULL
580 * if not specified.
581 **/
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500582 OutgoingRTPPkt(const unsigned char* const data, size_t datalen,
Emeric Vigier2f625822012-08-06 11:09:52 -0400583 uint8 paddinglen= 0, CryptoContext* pcc= NULL);
584
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500585 ~OutgoingRTPPkt()
586 { }
Emeric Vigier2f625822012-08-06 11:09:52 -0400587
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500588 /**
589 * @param pt Packet payload type.
590 **/
591 inline void
592 setPayloadType(PayloadType pt)
593 { getHeader()->payload = pt; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400594
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500595 /**
Emeric Vigier2f625822012-08-06 11:09:52 -0400596 * Sets the sequence number in the header.
597 *
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500598 * @param seq Packet sequence number, in host order.
599 **/
600 inline void
601 setSeqNum(uint16 seq)
602 {
603 cachedSeqNum = seq;
604 getHeader()->sequence = htons(seq);
605 }
Emeric Vigier2f625822012-08-06 11:09:52 -0400606
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500607 /**
608 * @param pts Packet timestamp, in host order.
609 **/
610 inline void
611 setTimestamp(uint32 pts)
612 {
613 cachedTimestamp = pts;
614 getHeader()->timestamp = htonl(pts);
615 }
Emeric Vigier2f625822012-08-06 11:09:52 -0400616
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500617 /**
618 * Set synchronization source numeric identifier.
619 *
620 * @param ssrc 32-bit Synchronization SouRCe numeric
621 * identifier, in host order.
622 **/
623 inline void
624 setSSRC(uint32 ssrc) const
625 { getHeader()->sources[0] = htonl(ssrc); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400626
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500627 /**
628 * Set synchronization source numeric identifier. Special
629 * version to save endianness conversion.
630 *
631 * @param ssrc 32-bit Synchronization SouRCe numeric
632 * identifier, in network order.
633 **/
634 inline void
635 setSSRCNetwork(uint32 ssrc) const
636 { getHeader()->sources[0] = ssrc; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400637
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500638 /**
639 * Specify the value of the marker bit. By default, the marker
640 * bit of outgoing packets is false/0. This method allows to
641 * explicity specify and change that value.
642 *
643 * @param mark value for the market bit.
644 */
645 inline void
646 setMarker(bool mark)
647 { getHeader()->marker = mark; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400648
649 /**
650 * Called packet is setup.
651 *
652 * This private method computes the SRTP data and stores it in the
653 * packet. Then encrypt the payload data (ex padding).
654 */
655 void protect(uint32 ssrc, CryptoContext* pcc);
656
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500657 /**
658 * Outgoing packets are equal if their sequence numbers match.
659 **/
660 inline bool
661 operator==(const OutgoingRTPPkt &p) const
662 { return ( this->getSeqNum() == p.getSeqNum() ); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400663
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500664 /**
665 * Outgoing packets are not equal if their sequence numbers differ.
666 **/
667 inline bool
668 operator!=(const OutgoingRTPPkt &p) const
669 { return ( this->getSeqNum() != p.getSeqNum() ); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400670
671private:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500672 /**
673 * Copy constructor from objects of its same kind, declared
674 * private to avoid its use.
675 **/
676 OutgoingRTPPkt(const OutgoingRTPPkt &o);
Emeric Vigier2f625822012-08-06 11:09:52 -0400677
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500678 /**
679 * Assignment operator from objects of its same kind, declared
680 * private to avoid its use.
681 **/
682 OutgoingRTPPkt&
683 operator=(const OutgoingRTPPkt &o);
Emeric Vigier2f625822012-08-06 11:09:52 -0400684
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500685 /**
686 * Set the list of CSRC identifiers in an RTP packet,
687 * switching host to network order.
688 */
689 void setCSRCArray(const uint32* const csrcs, uint16 numcsrc);
Emeric Vigier2f625822012-08-06 11:09:52 -0400690
691};
692
693/**
694 * @class IncomingRTPPkt
695 *
696 * @short RTP packets received from other participants.
697 *
698 * This class is intented to construct a packet object just after
699 * every packet is received by the scheduled queue, so that they are
700 * processed in an understandable and format independent manner inside
701 * the stack.
702 *
703 * @author Federico Montesino Pouzols <fedemp@altern.org>
704 */
705class __EXPORT IncomingRTPPkt : public RTPPacket
706{
707public:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500708 /**
709 * Build an RTP packet object from a data buffer. This
710 * constructor first performs a generic RTP data packet header
711 * check, whose result can be checked via isHeaderValid().
712 *
713 * @param block pointer to the buffer the whole packet is stored in.
714 * @param len length of the whole packet, expressed in octets.
715 *
716 * @note If check fails, the packet object is
717 * incomplete. checking isHeaderValid() is recommended before
718 * using a new RTPPacket object.
719 **/
720 IncomingRTPPkt(const unsigned char* block, size_t len);
Emeric Vigier2f625822012-08-06 11:09:52 -0400721
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500722 ~IncomingRTPPkt()
723 { }
Emeric Vigier2f625822012-08-06 11:09:52 -0400724
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500725 /**
726 * Get validity of this packet
727 * @return whether the header check performed at construction
728 * time ended successfully.
729 **/
730 inline bool
731 isHeaderValid()
732 { return headerValid; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400733
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500734 /**
735 * Get synchronization source numeric identifier.
736 *
737 * @return 32-bits Synchronization SouRCe numeric identifier,
738 * in host order.
739 **/
740 inline uint32
741 getSSRC() const
742 { return cachedSSRC; }
Emeric Vigier2f625822012-08-06 11:09:52 -0400743
744 /**
745 * Unprotect a received packet.
746 *
747 * Perform SRTP processing on this packet.
748 *
749 * @param pcc Pointer to SRTP CryptoContext.
750 * @return
751 * one if no errors, -1 if authentication failed, -2 if
752 * replay check failed
753 */
754 int32
755 unprotect(CryptoContext* pcc);
756
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500757 /**
758 * Two incoming packets are equal if they come from sources
759 * with the same SSRC and have the same sequence number.
760 **/
761 inline bool
762 operator==(const IncomingRTPPkt &p) const
763 { return ( (this->getSeqNum() == p.getSeqNum()) &&
764 (this->getSSRC() == p.getSSRC()) ); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400765
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500766 /**
767 * Two incoming packets are not equal if they come from
768 * different sources or have different sequence numbers.
769 **/
770 inline bool
771 operator!=(const IncomingRTPPkt &p) const
772 { return !( *this == p ); }
Emeric Vigier2f625822012-08-06 11:09:52 -0400773
774private:
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500775 /**
776 * Copy constructor from objects of its same kind, declared
777 * private to avoid its use.
778 **/
779 IncomingRTPPkt(const IncomingRTPPkt &ip);
Emeric Vigier2f625822012-08-06 11:09:52 -0400780
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500781 /**
782 * Assignment operator from objects of its same kind, declared
783 * private to avoid its use.
784 */
785 IncomingRTPPkt&
786 operator=(const IncomingRTPPkt &ip);
Emeric Vigier2f625822012-08-06 11:09:52 -0400787
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500788 /// Header validity, checked at construction time.
789 bool headerValid;
790 /// SSRC 32-bit identifier in host order.
791 uint32 cachedSSRC;
792 // Masks for RTP header validation: types matching RTCP SR or
793 // RR must be rejected to avoid accepting misaddressed RTCP
794 // packets.
795 static const uint16 RTP_INVALID_PT_MASK;
796 static const uint16 RTP_INVALID_PT_VALUE;
Emeric Vigier2f625822012-08-06 11:09:52 -0400797};
798
799/** @}*/ // rtppacket
800
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500801END_NAMESPACE
Emeric Vigier2f625822012-08-06 11:09:52 -0400802
803#endif // ndef CCXX_RTP_RTPPKT_H_
804
805/** EMACS **
806 * Local variables:
807 * mode: c++
808 * c-basic-offset: 8
809 * End:
810 */