jrtplib  3.6.0
rtppacketbuilder.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2006 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the "Expertisecentrum Digitale 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 
62  int Init(size_t maxpacksize);
63 
65  void Destroy();
66 
68  uint32_t GetPacketCount() { if (!init) return 0; return numpackets; }
69 
71  uint32_t GetPayloadOctetCount() { if (!init) return 0; return numpayloadbytes; }
72 
74  int SetMaximumPacketSize(size_t maxpacksize);
75 
77  int AddCSRC(uint32_t csrc);
78 
80  int DeleteCSRC(uint32_t csrc);
81 
83  void ClearCSRCList();
84 
90  int BuildPacket(const void *data,size_t len);
91 
97  int BuildPacket(const void *data,size_t len,
98  uint8_t pt,bool mark,uint32_t timestampinc);
99 
107  int BuildPacketEx(const void *data,size_t len,
108  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
109 
117  int BuildPacketEx(const void *data,size_t len,
118  uint8_t pt,bool mark,uint32_t timestampinc,
119  uint16_t hdrextID,const void *hdrextdata,size_t numhdrextwords);
120 
122  uint8_t *GetPacket() { if (!init) return 0; return buffer; }
123 
125  size_t GetPacketLength() { if (!init) return 0; return packetlength; }
126 
128  int SetDefaultPayloadType(uint8_t pt);
129 
131  int SetDefaultMark(bool m);
132 
134  int SetDefaultTimestampIncrement(uint32_t timestampinc);
135 
142  int IncrementTimestamp(uint32_t inc);
143 
152 
157  uint32_t CreateNewSSRC();
158 
164  uint32_t CreateNewSSRC(RTPSources &sources);
165 
167  uint32_t GetSSRC() const { if (!init) return 0; return ssrc; }
168 
170  uint32_t GetTimestamp() const { if (!init) return 0; return timestamp; }
171 
173  uint16_t GetSequenceNumber() const { if (!init) return 0; return seqnr; }
174 
179  RTPTime GetPacketTime() const { if (!init) return RTPTime(0,0); return lastwallclocktime; }
180 
182  uint32_t GetPacketTimestamp() const { if (!init) return 0; return lastrtptimestamp; }
183 private:
184  int PrivateBuildPacket(const void *data,size_t len,
185  uint8_t pt,bool mark,uint32_t timestampinc,bool gotextension,
186  uint16_t hdrextID = 0,const void *hdrextdata = 0,size_t numhdrextwords = 0);
187 
188  RTPRandom rtprnd;
189  size_t maxpacksize;
190  uint8_t *buffer;
191  size_t packetlength;
192 
193  uint32_t numpayloadbytes;
194  uint32_t numpackets;
195  bool init;
196 
197  uint32_t ssrc;
198  uint32_t timestamp;
199  uint16_t seqnr;
200 
201  uint32_t defaulttimestampinc;
202  uint8_t defaultpayloadtype;
203  bool defaultmark;
204 
205  bool deftsset,defptset,defmarkset;
206 
207  uint32_t csrcs[RTP_MAXCSRCS];
208  int numcsrcs;
209 
210  RTPTime lastwallclocktime;
211  uint32_t lastrtptimestamp;
212  uint32_t prevrtptimestamp;
213 };
214 
216 {
217  if (!init)
218  return ERR_RTP_PACKBUILD_NOTINIT;
219  defptset = true;
220  defaultpayloadtype = pt;
221  return 0;
222 }
223 
225 {
226  if (!init)
227  return ERR_RTP_PACKBUILD_NOTINIT;
228  defmarkset = true;
229  defaultmark = m;
230  return 0;
231 }
232 
233 inline int RTPPacketBuilder::SetDefaultTimestampIncrement(uint32_t timestampinc)
234 {
235  if (!init)
236  return ERR_RTP_PACKBUILD_NOTINIT;
237  deftsset = true;
238  defaulttimestampinc = timestampinc;
239  return 0;
240 }
241 
242 inline int RTPPacketBuilder::IncrementTimestamp(uint32_t inc)
243 {
244  if (!init)
245  return ERR_RTP_PACKBUILD_NOTINIT;
246  timestamp += inc;
247  return 0;
248 }
249 
251 {
252  if (!init)
253  return ERR_RTP_PACKBUILD_NOTINIT;
254  if (!deftsset)
255  return ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET;
256  timestamp += defaulttimestampinc;
257  return 0;
258 }
259 
260 #endif // RTPPACKETBUILDER_H
261 
int SetDefaultTimestampIncrement(uint32_t timestampinc)
Sets the default timestamp increment to timestampinc.
Definition: rtppacketbuilder.h:233
int IncrementTimestamp(uint32_t inc)
This function increments the timestamp with the amount given by inc.
Definition: rtppacketbuilder.h:242
RTPTime GetPacketTime() const
Returns the time at which a packet was generated.
Definition: rtppacketbuilder.h:179
RTPPacketBuilder(RTPMemoryManager *mgr=0)
Constructs an instance, optionally installing a memory manager.
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:71
int IncrementTimestampDefault()
This function increments the timestamp with the amount given set by the SetDefaultTimestampIncrement ...
Definition: rtppacketbuilder.h:250
int SetDefaultPayloadType(uint8_t pt)
Sets the default payload type to pt.
Definition: rtppacketbuilder.h:215
int SetDefaultMark(bool m)
Sets the default marker bit to m.
Definition: rtppacketbuilder.h:224
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:167
uint32_t GetTimestamp() const
Returns the current RTP timestamp.
Definition: rtppacketbuilder.h:170
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:173
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:125
uint32_t GetPacketCount()
Returns the number of packets which have been created with the current SSRC identifier.
Definition: rtppacketbuilder.h:68
int SetMaximumPacketSize(size_t maxpacksize)
Sets the maximum allowed packet size to maxpacksize.
The RTPRandom class can be used to generate random numbers.
Definition: rtprandom.h:46
void Destroy()
Cleans up the builder.
uint8_t * GetPacket()
Returns a pointer to the last built RTP packet data.
Definition: rtppacketbuilder.h:122
uint32_t GetPacketTimestamp() const
Returns the RTP timestamp which corresponds to the time returned by the previous function.
Definition: rtppacketbuilder.h:182
void ClearCSRCList()
Clears the CSRC list.
int BuildPacket(const void *data, size_t len)
Builds a packet with payload data and payload length len.