1 /*
2 
3   This file is a part of JRTPLIB
4   Copyright (c) 1999-2017 Jori Liesenborgs
5 
6   Contact: jori.liesenborgs@gmail.com
7 
8   This library was developed at the Expertise Centre for Digital Media
9   (http://www.edm.uhasselt.be), a research center of the Hasselt University
10   (http://www.uhasselt.be). The library is based upon work done for
11   my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13   Permission is hereby granted, free of charge, to any person obtaining a
14   copy of this software and associated documentation files (the "Software"),
15   to deal in the Software without restriction, including without limitation
16   the rights to use, copy, modify, merge, publish, distribute, sublicense,
17   and/or sell copies of the Software, and to permit persons to whom the
18   Software is furnished to do so, subject to the following conditions:
19 
20   The above copyright notice and this permission notice shall be included
21   in all copies or substantial portions of the Software.
22 
23   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29   IN THE SOFTWARE.
30 
31 */
32 
33 /**
34  * \file rtppacket.h
35  */
36 
37 #ifndef RTPPACKET_H
38 
39 #define RTPPACKET_H
40 
41 #include "rtpconfig.h"
42 #include "rtptypes.h"
43 #include "rtptimeutilities.h"
44 #include "rtpmemoryobject.h"
45 
46 namespace jrtplib
47 {
48 
49 class RTPRawPacket;
50 
51 /** Represents an RTP Packet.
52  *  The RTPPacket class can be used to parse a RTPRawPacket instance if it represents RTP data.
53  *  The class can also be used to create a new RTP packet according to the parameters specified by
54  *  the user.
55  */
56 class JRTPLIB_IMPORTEXPORT RTPPacket : public RTPMemoryObject
57 {
58 	JRTPLIB_NO_COPY(RTPPacket)
59 public:
60 	/** Creates an RTPPacket instance based upon the data in \c rawpack, optionally installing a memory manager.
61 	 *  Creates an RTPPacket instance based upon the data in \c rawpack, optionally installing a memory manager.
62 	 *  If successful, the data is moved from the raw packet to the RTPPacket instance.
63 	 */
64 	RTPPacket(RTPRawPacket &rawpack,RTPMemoryManager *mgr = 0);
65 
66 	/** Creates a new buffer for an RTP packet and fills in the fields according to the specified parameters.
67 	 *  Creates a new buffer for an RTP packet and fills in the fields according to the specified parameters.
68 	 *  If \c maxpacksize is not equal to zero, an error is generated if the total packet size would exceed
69 	 *  \c maxpacksize. The arguments of the constructor are self-explanatory. Note that the size of a header
70 	 *  extension is specified in a number of 32-bit words. A memory manager can be installed.
71 	 */
72 	RTPPacket(uint8_t payloadtype,const void *payloaddata,size_t payloadlen,uint16_t seqnr,
73 		  uint32_t timestamp,uint32_t ssrc,bool gotmarker,uint8_t numcsrcs,const uint32_t *csrcs,
74 		  bool gotextension,uint16_t extensionid,uint16_t extensionlen_numwords,const void *extensiondata,
75 		  size_t maxpacksize, RTPMemoryManager *mgr = 0);
76 
77 	/** This constructor is similar to the other constructor, but here data is stored in an external buffer
78 	 *  \c buffer with size \c buffersize. */
79 	RTPPacket(uint8_t payloadtype,const void *payloaddata,size_t payloadlen,uint16_t seqnr,
80 		  uint32_t timestamp,uint32_t ssrc,bool gotmarker,uint8_t numcsrcs,const uint32_t *csrcs,
81 		  bool gotextension,uint16_t extensionid,uint16_t extensionlen_numwords,const void *extensiondata,
82 		  void *buffer,size_t buffersize,RTPMemoryManager *mgr = 0);
83 
~RTPPacket()84 	virtual ~RTPPacket()																{ if (packet && !externalbuffer) RTPDeleteByteArray(packet,GetMemoryManager());  }
85 
86 	/** If an error occurred in one of the constructors, this function returns the error code. */
GetCreationError()87 	int GetCreationError() const														{ return error; }
88 
89 	/** Returns \c true if the RTP packet has a header extension and \c false otherwise. */
HasExtension()90 	bool HasExtension() const															{ return hasextension; }
91 
92 	/** Returns \c true if the marker bit was set and \c false otherwise. */
HasMarker()93 	bool HasMarker() const																{ return hasmarker; }
94 
95 	/** Returns the number of CSRCs contained in this packet. */
GetCSRCCount()96 	int GetCSRCCount() const															{ return numcsrcs; }
97 
98 	/** Returns a specific CSRC identifier.
99 	 *  Returns a specific CSRC identifier. The parameter \c num can go from 0 to GetCSRCCount()-1.
100 	 */
101 	uint32_t GetCSRC(int num) const;
102 
103 	/** Returns the payload type of the packet. */
GetPayloadType()104 	uint8_t GetPayloadType() const														{ return payloadtype; }
105 
106 	/** Returns the extended sequence number of the packet.
107 	 *  Returns the extended sequence number of the packet. When the packet is just received,
108 	 *  only the low $16$ bits will be set. The high 16 bits can be filled in later.
109 	 */
GetExtendedSequenceNumber()110 	uint32_t GetExtendedSequenceNumber() const											{ return extseqnr; }
111 
112 	/** Returns the sequence number of this packet. */
GetSequenceNumber()113 	uint16_t GetSequenceNumber() const													{ return (uint16_t)(extseqnr&0x0000FFFF); }
114 
115 	/** Sets the extended sequence number of this packet to \c seq. */
SetExtendedSequenceNumber(uint32_t seq)116 	void SetExtendedSequenceNumber(uint32_t seq)										{ extseqnr = seq; }
117 
118 	/** Returns the timestamp of this packet. */
GetTimestamp()119 	uint32_t GetTimestamp() const														{ return timestamp; }
120 
121 	/** Returns the SSRC identifier stored in this packet. */
GetSSRC()122 	uint32_t GetSSRC() const															{ return ssrc; }
123 
124 	/** Returns a pointer to the data of the entire packet. */
GetPacketData()125 	uint8_t *GetPacketData() const														{ return packet; }
126 
127 	/** Returns a pointer to the actual payload data. */
GetPayloadData()128 	uint8_t *GetPayloadData() const														{ return payload; }
129 
130 	/** Returns the length of the entire packet. */
GetPacketLength()131 	size_t GetPacketLength() const														{ return packetlength; }
132 
133 	/** Returns the payload length. */
GetPayloadLength()134 	size_t GetPayloadLength() const														{ return payloadlength; }
135 
136 	/** If a header extension is present, this function returns the extension identifier. */
GetExtensionID()137 	uint16_t GetExtensionID() const														{ return extid; }
138 
139 	/** Returns the length of the header extension data. */
GetExtensionData()140 	uint8_t *GetExtensionData() const													{ return extension; }
141 
142 	/** Returns the length of the header extension data. */
GetExtensionLength()143 	size_t GetExtensionLength() const													{ return extensionlength; }
144 #ifdef RTPDEBUG
145 	void Dump();
146 #endif // RTPDEBUG
147 
148 	/** Returns the time at which this packet was received.
149 	 *  When an RTPPacket instance is created from an RTPRawPacket instance, the raw packet's
150 	 *  reception time is stored in the RTPPacket instance. This function then retrieves that
151 	 *  time.
152 	 */
GetReceiveTime()153 	RTPTime GetReceiveTime() const														{ return receivetime; }
154 private:
155 	void Clear();
156 	int ParseRawPacket(RTPRawPacket &rawpack);
157 	int BuildPacket(uint8_t payloadtype,const void *payloaddata,size_t payloadlen,uint16_t seqnr,
158 	                uint32_t timestamp,uint32_t ssrc,bool gotmarker,uint8_t numcsrcs,const uint32_t *csrcs,
159 	                bool gotextension,uint16_t extensionid,uint16_t extensionlen_numwords,const void *extensiondata,
160 	                void *buffer,size_t maxsize);
161 
162 	int error;
163 
164 	bool hasextension,hasmarker;
165 	int numcsrcs;
166 
167 	uint8_t payloadtype;
168 	uint32_t extseqnr,timestamp,ssrc;
169 	uint8_t *packet,*payload;
170 	size_t packetlength,payloadlength;
171 
172 	uint16_t extid;
173 	uint8_t *extension;
174 	size_t extensionlength;
175 
176 	bool externalbuffer;
177 
178 	RTPTime receivetime;
179 };
180 
181 } // end namespace
182 
183 #endif // RTPPACKET_H
184 
185