summaryrefslogtreecommitdiffstats
path: root/src/rtppacketbuilder.h
blob: bb341642e794048a027396e4ad6909f231575871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*

  This file is a part of JRTPLIB
  Copyright (c) 1999-2007 Jori Liesenborgs

  Contact: jori.liesenborgs@gmail.com

  This library was developed at the "Expertisecentrum Digitale Media"
  (http://www.edm.uhasselt.be), a research center of the Hasselt University
  (http://www.uhasselt.be). The library is based upon work done for 
  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

*/

/**
 * \file rtppacketbuilder.h
 */

#ifndef RTPPACKETBUILDER_H

#define RTPPACKETBUILDER_H

#include "rtpconfig.h"
#include "rtperrors.h"
#include "rtpdefines.h"
#include "rtprandom.h"
#include "rtptimeutilities.h"
#include "rtptypes.h"
#include "rtpmemoryobject.h"

class RTPSources;

/** This class can be used to build RTP packets and is a bit more high-level than the RTPPacket 
 *  class: it generates an SSRC identifier, keeps track of timestamp and sequence number etc.
 */
class RTPPacketBuilder : public RTPMemoryObject
{
public:
	/** Constructs an instance, optionally installing a memory manager. */
	RTPPacketBuilder(RTPMemoryManager *mgr = 0);
	~RTPPacketBuilder();

	/** Initializes the builder to only allow packets with a size below \c maxpacksize. */
	int Init(size_t maxpacksize);

	/** Cleans up the builder. */
	void Destroy();

	/** Returns the number of packets which have been created with the current SSRC identifier. */
	uint32_t GetPacketCount()					{ if (!init) return 0; return numpackets; }

	/** Returns the number of payload octets which have been generated with this SSRC identifier. */
	uint32_t GetPayloadOctetCount()				{ if (!init) return 0; return numpayloadbytes; }

	/** Sets the maximum allowed packet size to \c maxpacksize. */
	int SetMaximumPacketSize(size_t maxpacksize);

	/** Adds a CSRC to the CSRC list which will be stored in the RTP packets. */
	int AddCSRC(uint32_t csrc);

	/** Deletes a CSRC from the list which will be stored in the RTP packets. */
	int DeleteCSRC(uint32_t csrc);

	/** Clears the CSRC list. */
	void ClearCSRCList();	
	
	/** Builds a packet with payload \c data and payload length \c len.
	 *  Builds a packet with payload \c data and payload length \c len. The payload type, marker 
	 *  and timestamp increment used will be those that have been set using the \c SetDefault 
	 *  functions below.
	 */
	int BuildPacket(const void *data,size_t len);

	/** Builds a packet with payload \c data and payload length \c len.
	 *  Builds a packet with payload \c data and payload length \c len. The payload type will be 
	 *  set to \c pt, the marker bit to \c mark and after building this packet, the timestamp will
	 *  be incremented with \c timestamp.
	 */
	int BuildPacket(const void *data,size_t len,
	                uint8_t pt,bool mark,uint32_t timestampinc);

	/** Builds a packet with payload \c data and payload length \c len.
	 *  Builds a packet with payload \c data and payload length \c len. The payload type, marker 
	 *  and timestamp increment used will be those that have been set using the \c SetDefault 
	 *  functions below. This packet will also contain an RTP header extension with identifier 
	 *  \c hdrextID and data \c hdrextdata. The length of the header extension data is given by 
	 *  \c numhdrextwords which expresses the length in a number of 32-bit words.
	 */
	int BuildPacketEx(const void *data,size_t len,
	                  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);

	/** Builds a packet with payload \c data and payload length \c len. 
	 *  Builds a packet with payload \c data and payload length \c len. The payload type will be set 
	 *  to \c pt, the marker bit to \c mark and after building this packet, the timestamp will 
	 *  be incremented with \c timestamp. This packet will also contain an RTP header extension 
	 *  with identifier \c hdrextID and data \c hdrextdata. The length of the header extension 
	 *  data is given by \c numhdrextwords which expresses the length in a number of 32-bit words.
	 */
	int BuildPacketEx(const void *data,size_t len,
	                  uint8_t pt,bool mark,uint32_t timestampinc,
	                  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);

	/** Returns a pointer to the last built RTP packet data. */
	uint8_t *GetPacket()						{ if (!init) return 0; return buffer; }

	/** Returns the size of the last built RTP packet. */
	size_t GetPacketLength()					{ if (!init) return 0; return packetlength; }
	
	/** Sets the default payload type to \c pt. */
	int SetDefaultPayloadType(uint8_t pt);

	/** Sets the default marker bit to \c m. */
	int SetDefaultMark(bool m);

	/** Sets the default timestamp increment to \c timestampinc. */
	int SetDefaultTimestampIncrement(uint32_t timestampinc);

	/** This function increments the timestamp with the amount given by \c inc.
	 *  This function increments the timestamp with the amount given by \c inc. This can be useful 
	 *  if, for example, a packet was not sent because it contained only silence. Then, this function 
	 *  should be called to increment the timestamp with the appropriate amount so that the next packets 
	 *  will still be played at the correct time at other hosts.
	 */
	int IncrementTimestamp(uint32_t inc);

	/** This function increments the timestamp with the amount given set by the SetDefaultTimestampIncrement
	 *  member function. 
	 *  This function increments the timestamp with the amount given set by the SetDefaultTimestampIncrement
	 *  member function. This can be useful if, for example, a packet was not sent because it contained only silence.
	 *  Then, this function should be called to increment the timestamp with the appropriate amount so that the next
	 *  packets will still be played at the correct time at other hosts.	
	 */
	int IncrementTimestampDefault();
	
	/** Creates a new SSRC to be used in generated packets. 
	 *  Creates a new SSRC to be used in generated packets. This will also generate new timestamp and 
	 *  sequence number offsets.
	 */
	uint32_t CreateNewSSRC();

	/** Creates a new SSRC to be used in generated packets. 
	 *  Creates a new SSRC to be used in generated packets. This will also generate new timestamp and 
	 *  sequence number offsets. The source table \c sources is used to make sure that the chosen SSRC 
	 *  isn't used by another participant yet.
	 */
	uint32_t CreateNewSSRC(RTPSources &sources);

	/** Returns the current SSRC identifier. */
	uint32_t GetSSRC() const					{ if (!init) return 0; return ssrc; }

	/** Returns the current RTP timestamp. */
	uint32_t GetTimestamp() const					{ if (!init) return 0; return timestamp; }

	/** Returns the current sequence number. */
	uint16_t GetSequenceNumber() const				{ if (!init) return 0; return seqnr; }

	/** Returns the time at which a packet was generated.
	 *  Returns the time at which a packet was generated. This is not necessarily the time at which 
	 *  the last RTP packet was generated: if the timestamp increment was zero, the time is not updated.
	 */
	RTPTime GetPacketTime() const					{ if (!init) return RTPTime(0,0); return lastwallclocktime; }

	/** Returns the RTP timestamp which corresponds to the time returned by the previous function. */
	uint32_t GetPacketTimestamp() const				{ if (!init) return 0; return lastrtptimestamp; }
private:
	int PrivateBuildPacket(const void *data,size_t len,
	                  uint8_t pt,bool mark,uint32_t timestampinc,bool gotextension,
	                  uint16_t hdrextID = 0,const void *hdrextdata = 0,size_t numhdrextwords = 0);

	RTPRandom rtprnd;	
	size_t maxpacksize;
	uint8_t *buffer;
	size_t packetlength;
	
	uint32_t numpayloadbytes;
	uint32_t numpackets;
	bool init;

	uint32_t ssrc;
	uint32_t timestamp;
	uint16_t seqnr;

	uint32_t defaulttimestampinc;
	uint8_t defaultpayloadtype;
	bool defaultmark;

	bool deftsset,defptset,defmarkset;

	uint32_t csrcs[RTP_MAXCSRCS];
	int numcsrcs;

	RTPTime lastwallclocktime;
	uint32_t lastrtptimestamp;
	uint32_t prevrtptimestamp;
};

inline int RTPPacketBuilder::SetDefaultPayloadType(uint8_t pt)
{
	if (!init)
		return ERR_RTP_PACKBUILD_NOTINIT;
	defptset = true;
	defaultpayloadtype = pt;
	return 0;
}

inline int RTPPacketBuilder::SetDefaultMark(bool m)
{
	if (!init)
		return ERR_RTP_PACKBUILD_NOTINIT;
	defmarkset = true;
	defaultmark = m;
	return 0;
}

inline int RTPPacketBuilder::SetDefaultTimestampIncrement(uint32_t timestampinc)
{
	if (!init)
		return ERR_RTP_PACKBUILD_NOTINIT;
	deftsset = true;
	defaulttimestampinc = timestampinc;
	return 0;
}

inline int RTPPacketBuilder::IncrementTimestamp(uint32_t inc)
{
	if (!init)
		return ERR_RTP_PACKBUILD_NOTINIT;
	timestamp += inc;
	return 0;
}

inline int RTPPacketBuilder::IncrementTimestampDefault()
{
	if (!init)
		return ERR_RTP_PACKBUILD_NOTINIT;
	if (!deftsset)
		return ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET;
	timestamp += defaulttimestampinc;
	return 0;
}

#endif // RTPPACKETBUILDER_H