SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
rtppacketbuilder.cpp
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 
33 #include "rtppacketbuilder.h"
34 #include "rtperrors.h"
35 #include "rtppacket.h"
36 #include "rtpsources.h"
37 #include <time.h>
38 #include <stdlib.h>
39 
40 namespace qrtplib
41 {
42 
44  rtprnd(r),
45  maxpacksize(0),
46  buffer(0),
47  packetlength(0),
48  lastwallclocktime(0, 0)
49 {
50  init = false;
51  deftsset = false;
52  defptset = false;
53  defmarkset = false;
54  defaultmark = false;
56  ssrc = 0;
57  timestamp = 0;
58  seqnr = 0;
59  prevrtptimestamp = 0;
60  lastrtptimestamp = 0;
62  numcsrcs = 0;
63  numpayloadbytes = 0;
64  numpackets = 0;
65  memset((char *) csrcs, 0, RTP_MAXCSRCS*sizeof(uint32_t));
66  timeinit.Dummy();
67 
68  //std::cout << (void *)(&rtprnd) << std::endl;
69 }
70 
72 {
73  Destroy();
74 }
75 
76 int RTPPacketBuilder::Init(unsigned int max)
77 {
78  if (init)
80  if (max <= 0)
82 
83  maxpacksize = max;
84  buffer = new uint8_t[max];
85  packetlength = 0;
86  numpackets = 0;
87 
88  CreateNewSSRC();
89 
90  deftsset = false;
91  defptset = false;
92  defmarkset = false;
93 
94  numcsrcs = 0;
95 
96  init = true;
97  return 0;
98 }
99 
101 {
102  if (!init)
103  return;
104  delete[] buffer;
105  init = false;
106 }
107 
109 {
110  uint8_t *newbuf;
111 
112  if (max <= 0)
114  newbuf = new uint8_t[max];
115 
116  delete[] buffer;
117  buffer = newbuf;
118  maxpacksize = max;
119  return 0;
120 }
121 
123 {
124  if (!init)
126  if (numcsrcs >= RTP_MAXCSRCS)
128 
129  int i;
130 
131  for (i = 0; i < numcsrcs; i++)
132  {
133  if (csrcs[i] == csrc)
135  }
136  csrcs[numcsrcs] = csrc;
137  numcsrcs++;
138  return 0;
139 }
140 
142 {
143  if (!init)
145 
146  int i = 0;
147  bool found = false;
148 
149  while (!found && i < numcsrcs)
150  {
151  if (csrcs[i] == csrc)
152  found = true;
153  else
154  i++;
155  }
156 
157  if (!found)
159 
160  // move the last csrc in the place of the deleted one
161  numcsrcs--;
162  if (numcsrcs > 0 && numcsrcs != i)
163  csrcs[i] = csrcs[numcsrcs];
164  return 0;
165 }
166 
168 {
169  if (!init)
170  return;
171  numcsrcs = 0;
172 }
173 
175 {
176  ssrc = rtprnd.GetRandom32();
179 
180  qDebug("RTPPacketBuilder::CreateNewSSRC: timestamp: %u", timestamp);
181 
182  // p 38: the count SHOULD be reset if the sender changes its SSRC identifier
183  numpayloadbytes = 0;
184  numpackets = 0;
185  return ssrc;
186 }
187 
189 {
190  bool found;
191 
192  do
193  {
194  ssrc = rtprnd.GetRandom32();
195  found = sources.GotEntry(ssrc);
196  } while (found);
197 
200 
201  // p 38: the count SHOULD be reset if the sender changes its SSRC identifier
202  numpayloadbytes = 0;
203  numpackets = 0;
204  return ssrc;
205 }
206 
207 int RTPPacketBuilder::BuildPacket(const void *data, unsigned int len)
208 {
209  if (!init)
211  if (!defptset)
213  if (!defmarkset)
215  if (!deftsset)
218 }
219 
220 int RTPPacketBuilder::BuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc)
221 {
222  if (!init)
224  return PrivateBuildPacket(data, len, pt, mark, timestampinc, false);
225 }
226 
227 int RTPPacketBuilder::BuildPacketEx(const void *data, unsigned int len, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords)
228 {
229  if (!init)
231  if (!defptset)
233  if (!defmarkset)
235  if (!deftsset)
237  return PrivateBuildPacket(data, len, defaultpayloadtype, defaultmark, defaulttimestampinc, true, hdrextID, hdrextdata, numhdrextwords);
238 }
239 
240 int RTPPacketBuilder::BuildPacketEx(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords)
241 {
242  if (!init)
244  return PrivateBuildPacket(data, len, pt, mark, timestampinc, true, hdrextID, hdrextdata, numhdrextwords);
245 
246 }
247 
248 int RTPPacketBuilder::PrivateBuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, bool gotextension, uint16_t hdrextID, const void *hdrextdata,
249  unsigned int numhdrextwords)
250 {
251  RTPPacket p(pt, data, len, seqnr, timestamp, ssrc, mark, numcsrcs, csrcs, gotextension, hdrextID, (uint16_t) numhdrextwords, hdrextdata, buffer, maxpacksize);
252  int status = p.GetCreationError();
253 
254  if (status < 0)
255  return status;
257 
258  if (numpackets == 0) // first packet
259  {
263  }
264  else if (timestamp != prevrtptimestamp)
265  {
269  }
270 
272  numpackets++;
273  timestamp += timestampinc;
274  seqnr++;
275 
276  //qDebug("RTPPacketBuilder::PrivateBuildPacket: numpackets: %u timestamp: %u timestampinc: %u seqnr: %u", numpackets, timestamp, timestampinc, seqnr);
277 
278  return 0;
279 }
280 
281 } // end namespace
282 
unsigned int GetPayloadLength() const
Definition: rtppacket.h:197
#define ERR_RTP_PACKBUILD_CSRCNOTINLIST
Definition: rtperrors.h:67
int Init(unsigned int maxpacksize)
RTPTimeInitializerObject timeinit
#define ERR_RTP_PACKBUILD_DEFAULTPAYLOADTYPENOTSET
Definition: rtperrors.h:69
#define ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET
Definition: rtperrors.h:70
virtual uint32_t GetRandom32()=0
int PrivateBuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, bool gotextension, uint16_t hdrextID=0, const void *hdrextdata=0, unsigned int numhdrextwords=0)
unsigned int GetPacketLength() const
Definition: rtppacket.h:191
virtual uint16_t GetRandom16()=0
RTPPacketBuilder(RTPRandom &rtprand)
#define ERR_RTP_PACKBUILD_NOTINIT
Definition: rtperrors.h:72
unsigned int uint32_t
Definition: rtptypes_win.h:46
int BuildPacket(const void *data, unsigned int len)
unsigned char uint8_t
Definition: rtptypes_win.h:42
#define ERR_RTP_PACKBUILD_CSRCLISTFULL
Definition: rtperrors.h:66
int GetCreationError() const
Definition: rtppacket.h:111
unsigned short uint16_t
Definition: rtptypes_win.h:44
int32_t i
Definition: decimators.h:244
int DeleteCSRC(uint32_t csrc)
#define RTP_MAXCSRCS
Definition: rtpdefines.h:38
uint32_t csrcs[RTP_MAXCSRCS]
static RTPTime CurrentTime()
#define ERR_RTP_PACKBUILD_ALREADYINIT
Definition: rtperrors.h:64
int BuildPacketEx(const void *data, unsigned int len, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords)
bool GotEntry(uint32_t ssrc)
Definition: rtpsources.cpp:625
#define ERR_RTP_PACKBUILD_INVALIDMAXPACKETSIZE
Definition: rtperrors.h:71
#define ERR_RTP_PACKBUILD_CSRCALREADYINLIST
Definition: rtperrors.h:65
#define ERR_RTP_PACKBUILD_DEFAULTMARKNOTSET
Definition: rtperrors.h:68
T max(const T &x, const T &y)
Definition: framework.h:446
int SetMaximumPacketSize(unsigned int maxpacksize)