jrtplib  3.8.0
rtppacketbuilder.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2010 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 
37 #ifndef RTPPACKETBUILDER_H
38 
39 #define RTPPACKETBUILDER_H
40 
41 #include "rtpconfig.h"
42 #include "rtperrors.h"
43 #include "rtpdefines.h"
44 #include "rtprandom.h"
45 #include "rtptimeutilities.h"
46 #include "rtptypes.h"
47 #include "rtpmemoryobject.h"
48 
49 class RTPSources;
50 
54 class RTPPacketBuilder : public RTPMemoryObject
55 {
56 public:
60  RTPPacketBuilder(RTPRandom &rtprand, RTPMemoryManager *mgr = 0);
62 
64  int Init(size_t maxpacksize);
65 
67  void Destroy();
68 
70  uint32_t GetPacketCount() { if (!init) return 0; return numpackets; }
71 
73  uint32_t GetPayloadOctetCount() { if (!init) return 0; return numpayloadbytes; }
74 
76  int SetMaximumPacketSize(size_t maxpacksize);
77 
79  int AddCSRC(uint32_t csrc);
80 
82  int DeleteCSRC(uint32_t csrc);
83 
85  void ClearCSRCList();
86 
92  int BuildPacket(const void *data,size_t len);
93 
99  int BuildPacket(const void *data,size_t len,
100  uint8_t pt,bool mark,uint32_t timestampinc);
101 
109  int BuildPacketEx(const void *data,size_t len,
110  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
111 
119  int BuildPacketEx(const void *data,size_t len,
120  uint8_t pt,bool mark,uint32_t timestampinc,
121  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
122 
124  uint8_t *GetPacket() { if (!init) return 0; return buffer; }
125 
127  size_t GetPacketLength() { if (!init) return 0; return packetlength; }
128 
130  int SetDefaultPayloadType(uint8_t pt);
131 
133  int SetDefaultMark(bool m);
134 
136  int SetDefaultTimestampIncrement(uint32_t timestampinc);
137 
144  int IncrementTimestamp(uint32_t inc);
145 
154 
159  uint32_t CreateNewSSRC();
160 
166  uint32_t CreateNewSSRC(RTPSources &sources);
167 
169  uint32_t GetSSRC() const { if (!init) return 0; return ssrc; }
170 
172  uint32_t GetTimestamp() const { if (!init) return 0; return timestamp; }
173 
175  uint16_t GetSequenceNumber() const { if (!init) return 0; return seqnr; }
176 
181  RTPTime GetPacketTime() const { if (!init) return RTPTime(0,0); return lastwallclocktime; }
182 
184  uint32_t GetPacketTimestamp() const { if (!init) return 0; return lastrtptimestamp; }
185 
190  void AdjustSSRC(uint32_t s) { ssrc = s; }
191 private:
192  int PrivateBuildPacket(const void *data,size_t len,
193  uint8_t pt,bool mark,uint32_t timestampinc,bool gotextension,
194  uint16_t hdrextID = 0,const void *hdrextdata = 0,size_t numhdrextwords = 0);
195 
196  RTPRandom &rtprnd;
197  size_t maxpacksize;
198  uint8_t *buffer;
199  size_t packetlength;
200 
201  uint32_t numpayloadbytes;
202  uint32_t numpackets;
203  bool init;
204 
205  uint32_t ssrc;
206  uint32_t timestamp;
207  uint16_t seqnr;
208 
209  uint32_t defaulttimestampinc;
210  uint8_t defaultpayloadtype;
211  bool defaultmark;
212 
213  bool deftsset,defptset,defmarkset;
214 
215  uint32_t csrcs[RTP_MAXCSRCS];
216  int numcsrcs;
217 
218  RTPTime lastwallclocktime;
219  uint32_t lastrtptimestamp;
220  uint32_t prevrtptimestamp;
221 };
222 
224 {
225  if (!init)
226  return ERR_RTP_PACKBUILD_NOTINIT;
227  defptset = true;
228  defaultpayloadtype = pt;
229  return 0;
230 }
231 
233 {
234  if (!init)
235  return ERR_RTP_PACKBUILD_NOTINIT;
236  defmarkset = true;
237  defaultmark = m;
238  return 0;
239 }
240 
241 inline int RTPPacketBuilder::SetDefaultTimestampIncrement(uint32_t timestampinc)
242 {
243  if (!init)
244  return ERR_RTP_PACKBUILD_NOTINIT;
245  deftsset = true;
246  defaulttimestampinc = timestampinc;
247  return 0;
248 }
249 
250 inline int RTPPacketBuilder::IncrementTimestamp(uint32_t inc)
251 {
252  if (!init)
253  return ERR_RTP_PACKBUILD_NOTINIT;
254  timestamp += inc;
255  return 0;
256 }
257 
259 {
260  if (!init)
261  return ERR_RTP_PACKBUILD_NOTINIT;
262  if (!deftsset)
263  return ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET;
264  timestamp += defaulttimestampinc;
265  return 0;
266 }
267 
268 #endif // RTPPACKETBUILDER_H
269 
int SetDefaultTimestampIncrement(uint32_t timestampinc)
Sets the default timestamp increment to timestampinc.
Definition: rtppacketbuilder.h:241
int IncrementTimestamp(uint32_t inc)
This function increments the timestamp with the amount given by inc.
Definition: rtppacketbuilder.h:250
RTPTime GetPacketTime() const
Returns the time at which a packet was generated.
Definition: rtppacketbuilder.h:181
int AddCSRC(uint32_t csrc)
Adds a CSRC to the CSRC list which will be stored in the RTP packets.
A memory manager.
Definition: rtpmemorymanager.h:144
Represents a table in which information about the participating sources is kept.
Definition: rtpsources.h:71
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:77
uint32_t GetPayloadOctetCount()
Returns the number of payload octets which have been generated with this SSRC identifier.
Definition: rtppacketbuilder.h:73
int IncrementTimestampDefault()
This function increments the timestamp with the amount given set by the SetDefaultTimestampIncrement ...
Definition: rtppacketbuilder.h:258
int SetDefaultPayloadType(uint8_t pt)
Sets the default payload type to pt.
Definition: rtppacketbuilder.h:223
RTPPacketBuilder(RTPRandom &rtprand, RTPMemoryManager *mgr=0)
Constructs an instance which will use rtprand for generating random numbers (used to initialize the S...
int SetDefaultMark(bool m)
Sets the default marker bit to m.
Definition: rtppacketbuilder.h:232
void AdjustSSRC(uint32_t s)
Sets a specific SSRC to be used.
Definition: rtppacketbuilder.h:190
int BuildPacketEx(const void *data, size_t len, uint16_t hdrextID, const void *hdrextdata, size_t numhdrextwords)
Builds a packet with payload data and payload length len.
int DeleteCSRC(uint32_t csrc)
Deletes a CSRC from the list which will be stored in the RTP packets.
uint32_t GetSSRC() const
Returns the current SSRC identifier.
Definition: rtppacketbuilder.h:169
uint32_t GetTimestamp() const
Returns the current RTP timestamp.
Definition: rtppacketbuilder.h:172
uint32_t CreateNewSSRC()
Creates a new SSRC to be used in generated packets.
uint16_t GetSequenceNumber() const
Returns the current sequence number.
Definition: rtppacketbuilder.h:175
This class can be used to build RTP packets and is a bit more high-level than the RTPPacket class: it...
Definition: rtppacketbuilder.h:54
int Init(size_t maxpacksize)
Initializes the builder to only allow packets with a size below maxpacksize.
size_t GetPacketLength()
Returns the size of the last built RTP packet.
Definition: rtppacketbuilder.h:127
uint32_t GetPacketCount()
Returns the number of packets which have been created with the current SSRC identifier.
Definition: rtppacketbuilder.h:70
int SetMaximumPacketSize(size_t maxpacksize)
Sets the maximum allowed packet size to maxpacksize.
Interface for generating random numbers.
Definition: rtprandom.h:48
void Destroy()
Cleans up the builder.
uint8_t * GetPacket()
Returns a pointer to the last built RTP packet data.
Definition: rtppacketbuilder.h:124
uint32_t GetPacketTimestamp() const
Returns the RTP timestamp which corresponds to the time returned by the previous function.
Definition: rtppacketbuilder.h:184
void ClearCSRCList()
Clears the CSRC list.
int BuildPacket(const void *data, size_t len)
Builds a packet with payload data and payload length len.