JRTPLIB  3.11.2 (development version)
rtppacketbuilder.h
Go to the documentation of this file.
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 
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 namespace jrtplib
50 {
51 
52 class RTPSources;
53 
57 class JRTPLIB_IMPORTEXPORT RTPPacketBuilder : public RTPMemoryObject
58 {
59  JRTPLIB_NO_COPY(RTPPacketBuilder)
60 public:
66 
68  int Init(size_t maxpacksize);
69 
71  void Destroy();
72 
74  uint32_t GetPacketCount() { if (!init) return 0; return numpackets; }
75 
77  uint32_t GetPayloadOctetCount() { if (!init) return 0; return numpayloadbytes; }
78 
80  int SetMaximumPacketSize(size_t maxpacksize);
81 
83  int AddCSRC(uint32_t csrc);
84 
86  int DeleteCSRC(uint32_t csrc);
87 
89  void ClearCSRCList();
90 
96  int BuildPacket(const void *data,size_t len);
97 
103  int BuildPacket(const void *data,size_t len,
104  uint8_t pt,bool mark,uint32_t timestampinc);
105 
113  int BuildPacketEx(const void *data,size_t len,
114  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
115 
123  int BuildPacketEx(const void *data,size_t len,
124  uint8_t pt,bool mark,uint32_t timestampinc,
125  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
126 
128  uint8_t *GetPacket() { if (!init) return 0; return buffer; }
129 
131  size_t GetPacketLength() { if (!init) return 0; return packetlength; }
132 
134  int SetDefaultPayloadType(uint8_t pt);
135 
137  int SetDefaultMark(bool m);
138 
140  int SetDefaultTimestampIncrement(uint32_t timestampinc);
141 
148  int IncrementTimestamp(uint32_t inc);
149 
157  int IncrementTimestampDefault();
158 
163  uint32_t CreateNewSSRC();
164 
170  uint32_t CreateNewSSRC(RTPSources &sources);
171 
173  uint32_t GetSSRC() const { if (!init) return 0; return ssrc; }
174 
176  uint32_t GetTimestamp() const { if (!init) return 0; return timestamp; }
177 
179  uint16_t GetSequenceNumber() const { if (!init) return 0; return seqnr; }
180 
185  RTPTime GetPacketTime() const { if (!init) return RTPTime(0,0); return lastwallclocktime; }
186 
188  uint32_t GetPacketTimestamp() const { if (!init) return 0; return lastrtptimestamp; }
189 
194  void AdjustSSRC(uint32_t s) { ssrc = s; }
195 private:
196  int PrivateBuildPacket(const void *data,size_t len,
197  uint8_t pt,bool mark,uint32_t timestampinc,bool gotextension,
198  uint16_t hdrextID = 0,const void *hdrextdata = 0,size_t numhdrextwords = 0);
199 
200  RTPRandom &rtprnd;
201  size_t maxpacksize;
202  uint8_t *buffer;
203  size_t packetlength;
204 
205  uint32_t numpayloadbytes;
206  uint32_t numpackets;
207  bool init;
208 
209  uint32_t ssrc;
210  uint32_t timestamp;
211  uint16_t seqnr;
212 
213  uint32_t defaulttimestampinc;
214  uint8_t defaultpayloadtype;
215  bool defaultmark;
216 
217  bool deftsset,defptset,defmarkset;
218 
219  uint32_t csrcs[RTP_MAXCSRCS];
220  int numcsrcs;
221 
222  RTPTime lastwallclocktime;
223  uint32_t lastrtptimestamp;
224  uint32_t prevrtptimestamp;
225 };
226 
228 {
229  if (!init)
230  return ERR_RTP_PACKBUILD_NOTINIT;
231  defptset = true;
232  defaultpayloadtype = pt;
233  return 0;
234 }
235 
237 {
238  if (!init)
239  return ERR_RTP_PACKBUILD_NOTINIT;
240  defmarkset = true;
241  defaultmark = m;
242  return 0;
243 }
244 
245 inline int RTPPacketBuilder::SetDefaultTimestampIncrement(uint32_t timestampinc)
246 {
247  if (!init)
248  return ERR_RTP_PACKBUILD_NOTINIT;
249  deftsset = true;
250  defaulttimestampinc = timestampinc;
251  return 0;
252 }
253 
254 inline int RTPPacketBuilder::IncrementTimestamp(uint32_t inc)
255 {
256  if (!init)
257  return ERR_RTP_PACKBUILD_NOTINIT;
258  timestamp += inc;
259  return 0;
260 }
261 
263 {
264  if (!init)
265  return ERR_RTP_PACKBUILD_NOTINIT;
266  if (!deftsset)
267  return ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET;
268  timestamp += defaulttimestampinc;
269  return 0;
270 }
271 
272 } // end namespace
273 
274 #endif // RTPPACKETBUILDER_H
275 
A memory manager.
Definition: rtpmemorymanager.h:151
This class can be used to build RTP packets and is a bit more high-level than the RTPPacket class: it...
Definition: rtppacketbuilder.h:58
int IncrementTimestamp(uint32_t inc)
This function increments the timestamp with the amount given by inc.
Definition: rtppacketbuilder.h:254
int SetMaximumPacketSize(size_t maxpacksize)
Sets the maximum allowed packet size to maxpacksize.
uint32_t GetPacketCount()
Returns the number of packets which have been created with the current SSRC identifier.
Definition: rtppacketbuilder.h:74
uint32_t CreateNewSSRC(RTPSources &sources)
Creates a new SSRC to be used in generated packets.
uint32_t GetTimestamp() const
Returns the current RTP timestamp.
Definition: rtppacketbuilder.h:176
int AddCSRC(uint32_t csrc)
Adds a CSRC to the CSRC list which will be stored in the RTP packets.
uint16_t GetSequenceNumber() const
Returns the current sequence number.
Definition: rtppacketbuilder.h:179
void AdjustSSRC(uint32_t s)
Sets a specific SSRC to be used.
Definition: rtppacketbuilder.h:194
uint8_t * GetPacket()
Returns a pointer to the last built RTP packet data.
Definition: rtppacketbuilder.h:128
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)
Builds a packet with payload data and payload length len.
int BuildPacket(const void *data, size_t len)
Builds a packet with payload data and payload length len.
int IncrementTimestampDefault()
This function increments the timestamp with the amount given set by the SetDefaultTimestampIncrement ...
Definition: rtppacketbuilder.h:262
void Destroy()
Cleans up the builder.
uint32_t CreateNewSSRC()
Creates a new SSRC to be used in generated packets.
uint32_t GetPacketTimestamp() const
Returns the RTP timestamp which corresponds to the time returned by the previous function.
Definition: rtppacketbuilder.h:188
uint32_t GetPayloadOctetCount()
Returns the number of payload octets which have been generated with this SSRC identifier.
Definition: rtppacketbuilder.h:77
RTPTime GetPacketTime() const
Returns the time at which a packet was generated.
Definition: rtppacketbuilder.h:185
int SetDefaultTimestampIncrement(uint32_t timestampinc)
Sets the default timestamp increment to timestampinc.
Definition: rtppacketbuilder.h:245
int DeleteCSRC(uint32_t csrc)
Deletes a CSRC from the list which will be stored in the RTP packets.
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.
void ClearCSRCList()
Clears the CSRC list.
RTPPacketBuilder(RTPRandom &rtprand, RTPMemoryManager *mgr=0)
Constructs an instance which will use rtprand for generating random numbers (used to initialize the S...
int SetDefaultPayloadType(uint8_t pt)
Sets the default payload type to pt.
Definition: rtppacketbuilder.h:227
int BuildPacket(const void *data, size_t len, uint8_t pt, bool mark, uint32_t timestampinc)
Builds a packet with payload data and payload length len.
int Init(size_t maxpacksize)
Initializes the builder to only allow packets with a size below maxpacksize.
int SetDefaultMark(bool m)
Sets the default marker bit to m.
Definition: rtppacketbuilder.h:236
uint32_t GetSSRC() const
Returns the current SSRC identifier.
Definition: rtppacketbuilder.h:173
size_t GetPacketLength()
Returns the size of the last built RTP packet.
Definition: rtppacketbuilder.h:131
Interface for generating random numbers.
Definition: rtprandom.h:52
Represents a table in which information about the participating sources is kept.
Definition: rtpsources.h:75
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:86