JRTPLIB  3.11.1
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 public:
63  RTPPacketBuilder(RTPRandom &rtprand, RTPMemoryManager *mgr = 0);
65 
67  int Init(size_t maxpacksize);
68 
70  void Destroy();
71 
73  uint32_t GetPacketCount() { if (!init) return 0; return numpackets; }
74 
76  uint32_t GetPayloadOctetCount() { if (!init) return 0; return numpayloadbytes; }
77 
79  int SetMaximumPacketSize(size_t maxpacksize);
80 
82  int AddCSRC(uint32_t csrc);
83 
85  int DeleteCSRC(uint32_t csrc);
86 
88  void ClearCSRCList();
89 
95  int BuildPacket(const void *data,size_t len);
96 
102  int BuildPacket(const void *data,size_t len,
103  uint8_t pt,bool mark,uint32_t timestampinc);
104 
112  int BuildPacketEx(const void *data,size_t len,
113  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
114 
122  int BuildPacketEx(const void *data,size_t len,
123  uint8_t pt,bool mark,uint32_t timestampinc,
124  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
125 
127  uint8_t *GetPacket() { if (!init) return 0; return buffer; }
128 
130  size_t GetPacketLength() { if (!init) return 0; return packetlength; }
131 
133  int SetDefaultPayloadType(uint8_t pt);
134 
136  int SetDefaultMark(bool m);
137 
139  int SetDefaultTimestampIncrement(uint32_t timestampinc);
140 
147  int IncrementTimestamp(uint32_t inc);
148 
156  int IncrementTimestampDefault();
157 
162  uint32_t CreateNewSSRC();
163 
169  uint32_t CreateNewSSRC(RTPSources &sources);
170 
172  uint32_t GetSSRC() const { if (!init) return 0; return ssrc; }
173 
175  uint32_t GetTimestamp() const { if (!init) return 0; return timestamp; }
176 
178  uint16_t GetSequenceNumber() const { if (!init) return 0; return seqnr; }
179 
184  RTPTime GetPacketTime() const { if (!init) return RTPTime(0,0); return lastwallclocktime; }
185 
187  uint32_t GetPacketTimestamp() const { if (!init) return 0; return lastrtptimestamp; }
188 
193  void AdjustSSRC(uint32_t s) { ssrc = s; }
194 private:
195  int PrivateBuildPacket(const void *data,size_t len,
196  uint8_t pt,bool mark,uint32_t timestampinc,bool gotextension,
197  uint16_t hdrextID = 0,const void *hdrextdata = 0,size_t numhdrextwords = 0);
198 
199  RTPRandom &rtprnd;
200  size_t maxpacksize;
201  uint8_t *buffer;
202  size_t packetlength;
203 
204  uint32_t numpayloadbytes;
205  uint32_t numpackets;
206  bool init;
207 
208  uint32_t ssrc;
209  uint32_t timestamp;
210  uint16_t seqnr;
211 
212  uint32_t defaulttimestampinc;
213  uint8_t defaultpayloadtype;
214  bool defaultmark;
215 
216  bool deftsset,defptset,defmarkset;
217 
218  uint32_t csrcs[RTP_MAXCSRCS];
219  int numcsrcs;
220 
221  RTPTime lastwallclocktime;
222  uint32_t lastrtptimestamp;
223  uint32_t prevrtptimestamp;
224 };
225 
227 {
228  if (!init)
229  return ERR_RTP_PACKBUILD_NOTINIT;
230  defptset = true;
231  defaultpayloadtype = pt;
232  return 0;
233 }
234 
236 {
237  if (!init)
238  return ERR_RTP_PACKBUILD_NOTINIT;
239  defmarkset = true;
240  defaultmark = m;
241  return 0;
242 }
243 
244 inline int RTPPacketBuilder::SetDefaultTimestampIncrement(uint32_t timestampinc)
245 {
246  if (!init)
247  return ERR_RTP_PACKBUILD_NOTINIT;
248  deftsset = true;
249  defaulttimestampinc = timestampinc;
250  return 0;
251 }
252 
253 inline int RTPPacketBuilder::IncrementTimestamp(uint32_t inc)
254 {
255  if (!init)
256  return ERR_RTP_PACKBUILD_NOTINIT;
257  timestamp += inc;
258  return 0;
259 }
260 
262 {
263  if (!init)
264  return ERR_RTP_PACKBUILD_NOTINIT;
265  if (!deftsset)
266  return ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET;
267  timestamp += defaulttimestampinc;
268  return 0;
269 }
270 
271 } // end namespace
272 
273 #endif // RTPPACKETBUILDER_H
274 
This class can be used to build RTP packets and is a bit more high-level than the RTPPacket class: it...
Definition: rtppacketbuilder.h:57
void AdjustSSRC(uint32_t s)
Sets a specific SSRC to be used.
Definition: rtppacketbuilder.h:193
int SetDefaultTimestampIncrement(uint32_t timestampinc)
Sets the default timestamp increment to timestampinc.
Definition: rtppacketbuilder.h:244
Interface for generating random numbers.
Definition: rtprandom.h:51
uint32_t GetSSRC() const
Returns the current SSRC identifier.
Definition: rtppacketbuilder.h:172
uint32_t GetPacketCount()
Returns the number of packets which have been created with the current SSRC identifier.
Definition: rtppacketbuilder.h:73
int IncrementTimestampDefault()
This function increments the timestamp with the amount given set by the SetDefaultTimestampIncrement ...
Definition: rtppacketbuilder.h:261
size_t GetPacketLength()
Returns the size of the last built RTP packet.
Definition: rtppacketbuilder.h:130
uint32_t GetPacketTimestamp() const
Returns the RTP timestamp which corresponds to the time returned by the previous function.
Definition: rtppacketbuilder.h:187
int SetDefaultMark(bool m)
Sets the default marker bit to m.
Definition: rtppacketbuilder.h:235
Represents a table in which information about the participating sources is kept.
Definition: rtpsources.h:74
RTPTime GetPacketTime() const
Returns the time at which a packet was generated.
Definition: rtppacketbuilder.h:184
uint32_t GetPayloadOctetCount()
Returns the number of payload octets which have been generated with this SSRC identifier.
Definition: rtppacketbuilder.h:76
uint16_t GetSequenceNumber() const
Returns the current sequence number.
Definition: rtppacketbuilder.h:178
int IncrementTimestamp(uint32_t inc)
This function increments the timestamp with the amount given by inc.
Definition: rtppacketbuilder.h:253
Definition: rtpfaketransmitter.h:64
uint32_t GetTimestamp() const
Returns the current RTP timestamp.
Definition: rtppacketbuilder.h:175
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:84
A memory manager.
Definition: rtpmemorymanager.h:150
int SetDefaultPayloadType(uint8_t pt)
Sets the default payload type to pt.
Definition: rtppacketbuilder.h:226
uint8_t * GetPacket()
Returns a pointer to the last built RTP packet data.
Definition: rtppacketbuilder.h:127