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 rtprawpacket.h
35  */
36 
37 #ifndef RTPRAWPACKET_H
38 
39 #define RTPRAWPACKET_H
40 
41 #include "rtpconfig.h"
42 #include "rtptimeutilities.h"
43 #include "rtpaddress.h"
44 #include "rtptypes.h"
45 #include "rtpmemoryobject.h"
46 #include "rtpstructs.h"
47 
48 namespace jrtplib
49 {
50 
51 /** This class is used by the transmission component to store the incoming RTP and RTCP data in. */
52 class JRTPLIB_IMPORTEXPORT RTPRawPacket : public RTPMemoryObject
53 {
54 	JRTPLIB_NO_COPY(RTPRawPacket)
55 public:
56     /** Creates an instance which stores data from \c data with length \c datalen.
57 	 *  Creates an instance which stores data from \c data with length \c datalen. Only the pointer
58 	 *  to the data is stored, no actual copy is made! The address from which this packet originated
59 	 *  is set to \c address and the time at which the packet was received is set to \c recvtime.
60 	 *  The flag which indicates whether this data is RTP or RTCP data is set to \c rtp.
61 	 *  If you don't know if it's an RTP or RTCP packet, you can use the other constructor which
62 	 *  tries to determine the type based on the header. A memory manager can be installed as well.
63 	 */
64 	RTPRawPacket(uint8_t *data,size_t datalen,RTPAddress *address,RTPTime &recvtime,bool rtp,RTPMemoryManager *mgr = 0);
65 
66     /** Creates an instance which stores data from \c data with length \c datalen.
67 	 *  Creates an instance which stores data from \c data with length \c datalen. Only the pointer
68 	 *  to the data is stored, no actual copy is made! The address from which this packet originated
69 	 *  is set to \c address and the time at which the packet was received is set to \c recvtime.
70 	 *  A memory manager can be installed as well. This is similar to the other constructor where
71 	 *  you have to specify yourself if the packet is supposed to contain RTP or RTCP data. In this version,
72 	 *  based on the header information the packet type will be determined.
73 	 */
74 	RTPRawPacket(uint8_t *data,size_t datalen,RTPAddress *address,RTPTime &recvtime,RTPMemoryManager *mgr = 0);
75 	~RTPRawPacket();
76 
77 	/** Returns the pointer to the data which is contained in this packet. */
GetData()78 	uint8_t *GetData()														{ return packetdata; }
79 
80 	/** Returns the length of the packet described by this instance. */
GetDataLength()81 	size_t GetDataLength() const											{ return packetdatalength; }
82 
83 	/** Returns the time at which this packet was received. */
GetReceiveTime()84 	RTPTime GetReceiveTime() const											{ return receivetime; }
85 
86 	/** Returns the address stored in this packet. */
GetSenderAddress()87 	const RTPAddress *GetSenderAddress() const								{ return senderaddress; }
88 
89 	/** Returns \c true if this data is RTP data, \c false if it is RTCP data. */
IsRTP()90 	bool IsRTP() const														{ return isrtp; }
91 
92 	/** Sets the pointer to the data stored in this packet to zero.
93 	 *  Sets the pointer to the data stored in this packet to zero. This will prevent
94 	 *  a \c delete call for the actual data when the destructor of RTPRawPacket is called.
95 	 *  This function is used by the RTPPacket and RTCPCompoundPacket classes to obtain
96 	 *  the packet data (without having to copy it)	and to make sure the data isn't deleted
97 	 *  when the destructor of RTPRawPacket is called.
98 	 */
ZeroData()99 	void ZeroData()															{ packetdata = 0; packetdatalength = 0; }
100 
101 	/** Allocates a number of bytes for RTP or RTCP data using the memory manager that
102 	 *  was used for this raw packet instance, can be useful if the RTPRawPacket::SetData
103 	 *  function will be used. */
104 	uint8_t *AllocateBytes(bool isrtp, int recvlen) const;
105 
106 	/** Deallocates the previously stored data and replaces it with the data that's
107 	 *  specified, can be useful when e.g. decrypting data in RTPSession::OnChangeIncomingData */
108 	void SetData(uint8_t *data, size_t datalen);
109 
110 	/** Deallocates the currently stored RTPAddress instance and replaces it
111 	 *  with the one that's specified (you probably don't need this function). */
112 	void SetSenderAddress(RTPAddress *address);
113 private:
114 	void DeleteData();
115 
116 	uint8_t *packetdata;
117 	size_t packetdatalength;
118 	RTPTime receivetime;
119 	RTPAddress *senderaddress;
120 	bool isrtp;
121 };
122 
RTPRawPacket(uint8_t * data,size_t datalen,RTPAddress * address,RTPTime & recvtime,bool rtp,RTPMemoryManager * mgr)123 inline RTPRawPacket::RTPRawPacket(uint8_t *data,size_t datalen,RTPAddress *address,RTPTime &recvtime,bool rtp,RTPMemoryManager *mgr):RTPMemoryObject(mgr),receivetime(recvtime)
124 {
125 	packetdata = data;
126 	packetdatalength = datalen;
127 	senderaddress = address;
128 	isrtp = rtp;
129 }
130 
RTPRawPacket(uint8_t * data,size_t datalen,RTPAddress * address,RTPTime & recvtime,RTPMemoryManager * mgr)131 inline RTPRawPacket::RTPRawPacket(uint8_t *data,size_t datalen,RTPAddress *address,RTPTime &recvtime,RTPMemoryManager *mgr):RTPMemoryObject(mgr),receivetime(recvtime)
132 {
133 	packetdata = data;
134 	packetdatalength = datalen;
135 	senderaddress = address;
136 
137 	isrtp = true;
138 	if (datalen >= sizeof(RTCPCommonHeader))
139 	{
140 		RTCPCommonHeader *rtcpheader = (RTCPCommonHeader *)data;
141 		uint8_t packettype = rtcpheader->packettype;
142 
143 		if (packettype >= 200 && packettype <= 204)
144 			isrtp = false;
145 	}
146 }
147 
~RTPRawPacket()148 inline RTPRawPacket::~RTPRawPacket()
149 {
150 	DeleteData();
151 }
152 
DeleteData()153 inline void RTPRawPacket::DeleteData()
154 {
155 	if (packetdata)
156 		RTPDeleteByteArray(packetdata,GetMemoryManager());
157 	if (senderaddress)
158 		RTPDelete(senderaddress,GetMemoryManager());
159 
160 	packetdata = 0;
161 	senderaddress = 0;
162 }
163 
AllocateBytes(bool isrtp,int recvlen)164 inline uint8_t *RTPRawPacket::AllocateBytes(bool isrtp, int recvlen) const
165 {
166 	JRTPLIB_UNUSED(isrtp); // possibly unused
167 	return RTPNew(GetMemoryManager(),(isrtp)?RTPMEM_TYPE_BUFFER_RECEIVEDRTPPACKET:RTPMEM_TYPE_BUFFER_RECEIVEDRTCPPACKET) uint8_t[recvlen];
168 }
169 
SetData(uint8_t * data,size_t datalen)170 inline void RTPRawPacket::SetData(uint8_t *data, size_t datalen)
171 {
172 	if (packetdata)
173 		RTPDeleteByteArray(packetdata,GetMemoryManager());
174 
175 	packetdata = data;
176 	packetdatalength = datalen;
177 }
178 
SetSenderAddress(RTPAddress * address)179 inline void RTPRawPacket::SetSenderAddress(RTPAddress *address)
180 {
181 	if (senderaddress)
182 		RTPDelete(senderaddress, GetMemoryManager());
183 
184 	senderaddress = address;
185 }
186 
187 } // end namespace
188 
189 #endif // RTPRAWPACKET_H
190 
191