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.
rtpsourcedata.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 "rtpsourcedata.h"
34 #include "rtpdefines.h"
35 #include "rtpaddress.h"
36 
37 #define ACCEPTPACKETCODE \
38  *accept = true; \
39  \
40  sentdata = true; \
41  packetsreceived++; \
42  numnewpackets++; \
43  \
44  if (pack->GetExtendedSequenceNumber() == 0) \
45  { \
46  baseseqnr = 0x0000FFFF; \
47  numcycles = 0x00010000; \
48  } \
49  else \
50  baseseqnr = pack->GetExtendedSequenceNumber() - 1; \
51  \
52  exthighseqnr = baseseqnr + 1; \
53  prevpacktime = receivetime; \
54  prevexthighseqnr = baseseqnr; \
55  savedextseqnr = baseseqnr; \
56  \
57  pack->SetExtendedSequenceNumber(exthighseqnr); \
58  \
59  prevtimestamp = pack->GetTimestamp(); \
60  lastmsgtime = prevpacktime; \
61  if (!ownpacket) /* for own packet, this value is set on an outgoing packet */ \
62  lastrtptime = prevpacktime;
63 
64 namespace qrtplib
65 {
66 
68  RTPPacket *pack,
69  const RTPTime &receivetime,
70  double tsunit,
71  bool ownpacket,
72  bool *accept)
73 {
74  // Note that the sequence number in the RTP packet is still just the
75  // 16 bit number contained in the RTP header
76 
77  if (!sentdata) // no valid packets received yet
78  {
80  }
81  else // already got packets
82  {
83  uint16_t maxseq16;
84  uint32_t extseqnr;
85 
86  // Adjust max extended sequence number and set extende seq nr of packet
87 
88  *accept = true;
90  numnewpackets++;
91 
92  maxseq16 = (uint16_t) (exthighseqnr & 0x0000FFFF);
93  if (pack->GetExtendedSequenceNumber() >= maxseq16)
94  {
95  extseqnr = numcycles + pack->GetExtendedSequenceNumber();
96  exthighseqnr = extseqnr;
97  }
98  else
99  {
100  uint16_t dif1, dif2;
101 
102  dif1 = ((uint16_t) pack->GetExtendedSequenceNumber());
103  dif1 -= maxseq16;
104  dif2 = maxseq16;
105  dif2 -= ((uint16_t) pack->GetExtendedSequenceNumber());
106  if (dif1 < dif2)
107  {
108  numcycles += 0x00010000;
109  extseqnr = numcycles + pack->GetExtendedSequenceNumber();
110  exthighseqnr = extseqnr;
111  }
112  else
113  extseqnr = numcycles + pack->GetExtendedSequenceNumber();
114  }
115 
116  pack->SetExtendedSequenceNumber(extseqnr);
117 
118  // Calculate jitter
119 
120  if (tsunit > 0)
121  {
122 #if 0
123  RTPTime curtime = receivetime;
124  double diffts1,diffts2,diff;
125 
126  curtime -= prevpacktime;
127  diffts1 = curtime.GetDouble()/tsunit;
128  diffts2 = (double)pack->GetTimestamp() - (double)prevtimestamp;
129  diff = diffts1 - diffts2;
130  if (diff < 0)
131  diff = -diff;
132  diff -= djitter;
133  diff /= 16.0;
134  djitter += diff;
136 #else
137  RTPTime curtime = receivetime;
138  double diffts1, diffts2, diff;
139  uint32_t curts = pack->GetTimestamp();
140 
141  curtime -= prevpacktime;
142  diffts1 = curtime.GetDouble() / tsunit;
143 
144  if (curts > prevtimestamp)
145  {
146  uint32_t unsigneddiff = curts - prevtimestamp;
147 
148  if (unsigneddiff < 0x10000000) // okay, curts realy is larger than prevtimestamp
149  diffts2 = (double) unsigneddiff;
150  else
151  {
152  // wraparound occurred and curts is actually smaller than prevtimestamp
153 
154  unsigneddiff = -unsigneddiff; // to get the actual difference (in absolute value)
155  diffts2 = -((double) unsigneddiff);
156  }
157  }
158  else if (curts < prevtimestamp)
159  {
160  uint32_t unsigneddiff = prevtimestamp - curts;
161 
162  if (unsigneddiff < 0x10000000) // okay, curts really is smaller than prevtimestamp
163  diffts2 = -((double) unsigneddiff); // negative since we actually need curts-prevtimestamp
164  else
165  {
166  // wraparound occurred and curts is actually larger than prevtimestamp
167 
168  unsigneddiff = -unsigneddiff; // to get the actual difference (in absolute value)
169  diffts2 = (double) unsigneddiff;
170  }
171  }
172  else
173  diffts2 = 0;
174 
175  diff = diffts1 - diffts2;
176  if (diff < 0)
177  diff = -diff;
178  diff -= djitter;
179  diff /= 16.0;
180  djitter += diff;
181  jitter = (uint32_t) djitter;
182 #endif
183  }
184  else
185  {
186  djitter = 0;
187  jitter = 0;
188  }
189 
190  prevpacktime = receivetime;
191  prevtimestamp = pack->GetTimestamp();
193  if (!ownpacket) // for own packet, this value is set on an outgoing packet
195  }
196 }
197 
199  byetime(0, 0)
200 {
201  ssrc = s;
202  issender = false;
203  iscsrc = false;
204  timestampunit = -1;
205  receivedbye = false;
206  byereason = 0;
207  byereasonlen = 0;
208  rtpaddr = 0;
209  rtcpaddr = 0;
210  ownssrc = false;
211  validated = false;
212  processedinrtcp = false;
213  isrtpaddrset = false;
214  isrtcpaddrset = false;
215 }
216 
218 {
219  FlushPackets();
220  if (byereason)
221  delete[] byereason;
222  if (rtpaddr)
223  delete rtpaddr;
224  if (rtcpaddr)
225  delete rtcpaddr;
226 }
227 
229 {
230  if (!SRprevinf.HasInfo())
231  return -1.0;
232 
235  if (t1.IsZero() || t2.IsZero()) // one of the times couldn't be calculated
236  return -1.0;
237 
238  if (t1 <= t2)
239  return -1.0;
240 
241  t1 -= t2; // get the time difference
242 
244 
245  return (t1.GetDouble() / ((double) tsdiff));
246 }
247 
249 {
250  if (!RRinf.HasInfo())
251  return RTPTime(0, 0);
252  if (RRinf.GetDelaySinceLastSR() == 0 && RRinf.GetLastSRTimestamp() == 0)
253  return RTPTime(0, 0);
254 
255  RTPNTPTime recvtime = RRinf.GetReceiveTime().GetNTPTime();
256  uint32_t rtt = ((recvtime.GetMSW() & 0xFFFF) << 16) | ((recvtime.GetLSW() >> 16) & 0xFFFF);
257  rtt -= RRinf.GetLastSRTimestamp();
258  rtt -= RRinf.GetDelaySinceLastSR();
259 
260  double drtt = (((double) rtt) / 65536.0);
261  return RTPTime(drtt);
262 }
263 
264 } // end namespace
265 
double GetDouble() const
uint32_t GetDelaySinceLastSR() const
void ProcessPacket(RTPPacket *pack, const RTPTime &receivetime, double tsunit, bool ownpacket, bool *accept)
uint32_t GetLastSRTimestamp() const
uint32_t GetRTPTimestamp() const
Definition: rtpsourcedata.h:85
RTPTime INF_GetRoundtripTime() const
uint32_t GetMSW() const
unsigned int uint32_t
Definition: rtptypes_win.h:46
#define ACCEPTPACKETCODE
unsigned short uint16_t
Definition: rtptypes_win.h:44
double INF_GetEstimatedTimestampUnit() const
RTCPSenderReportInfo SRprevinf
bool IsZero() const
uint32_t GetExtendedSequenceNumber() const
Definition: rtppacket.h:149
RTPSourceData(uint32_t ssrc)
void SetExtendedSequenceNumber(uint32_t seq)
Definition: rtppacket.h:161
uint32_t GetLSW() const
RTPNTPTime GetNTPTime() const
RTCPSenderReportInfo SRinf
uint32_t GetTimestamp() const
Definition: rtppacket.h:167
RTPNTPTime GetNTPTimestamp() const
Definition: rtpsourcedata.h:81
RTCPReceiverReportInfo RRinf