forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
201 lines (161 loc) · 6.29 KB
/
window.cpp
File metadata and controls
201 lines (161 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2018 Haivision Systems Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
/*****************************************************************************
Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the
above copyright notice, this list of conditions
and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Illinois
nor the names of its contributors may be used to
endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu, last updated 01/22/2011
modified by
Haivision Systems Inc.
*****************************************************************************/
#include "platform_sys.h"
#include <cmath>
#include <cstring>
#include "common.h"
#include "window.h"
#include <algorithm>
using namespace std;
using namespace srt::sync;
namespace srt
{
namespace ACKWindowTools
{
void store(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t ack)
{
r_aSeq[r_iHead].iACKSeqNo = seq;
r_aSeq[r_iHead].iACK = ack;
r_aSeq[r_iHead].tsTimeStamp = steady_clock::now();
r_iHead = (r_iHead + 1) % size;
// overwrite the oldest ACK since it is not likely to be acknowledged
if (r_iHead == r_iTail)
r_iTail = (r_iTail + 1) % size;
}
int acknowledge(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t& r_ack, const steady_clock::time_point& currtime)
{
// Head has not exceeded the physical boundary of the window
if (r_iHead >= r_iTail)
{
for (int i = r_iTail, n = r_iHead; i < n; ++ i)
{
// Looking for an identical ACK Seq. No.
if (seq == r_aSeq[i].iACKSeqNo)
{
// Return the Data ACK it carried
r_ack = r_aSeq[i].iACK;
// Calculate RTT estimate
const int rtt = (int)count_microseconds(currtime - r_aSeq[i].tsTimeStamp);
if (i + 1 == r_iHead)
{
r_iTail = r_iHead = 0;
r_aSeq[0].iACKSeqNo = SRT_SEQNO_NONE;
}
else
r_iTail = (i + 1) % size;
return rtt;
}
}
// The record about ACK is not found in the buffer, RTT can not be calculated
return -1;
}
// Head has exceeded the physical window boundary, so it is behind tail
for (int j = r_iTail, n = r_iHead + (int)size; j < n; ++ j)
{
// Looking for an identical ACK Seq. No.
if (seq == r_aSeq[j % size].iACKSeqNo)
{
// Return the Data ACK it carried
j %= size;
r_ack = r_aSeq[j].iACK;
// Calculate RTT estimate
const int rtt = (int)count_microseconds(currtime - r_aSeq[j].tsTimeStamp);
if (j == r_iHead)
{
r_iTail = r_iHead = 0;
r_aSeq[0].iACKSeqNo = -1;
}
else
r_iTail = (j + 1) % size;
return rtt;
}
}
// The record about ACK is not found in the buffer, RTT can not be calculated
return -1;
}
} // namespace AckTools
////////////////////////////////////////////////////////////////////////////////
void CPktTimeWindowTools::initializeWindowArrays(int* r_pktWindow, int* r_probeWindow, int* r_bytesWindow, size_t asize, size_t psize, size_t max_payload_size)
{
for (size_t i = 0; i < asize; ++ i)
r_pktWindow[i] = 1000000; //1 sec -> 1 pkt/sec
for (size_t k = 0; k < psize; ++ k)
r_probeWindow[k] = 1000; //1 msec -> 1000 pkts/sec
for (size_t i = 0; i < asize; ++ i)
r_bytesWindow[i] = int(max_payload_size); //based on 1 pkt/sec set in r_pktWindow[i]
}
int CPktTimeWindowTools::ceilPerMega(double value, double count)
{
static const double MEGA = 1000.0 * 1000.0;
return int(::ceil(MEGA / (value / count)));
}
int CPktTimeWindowTools::getPktRcvSpeed_in(const int* window, int* replica, const int* abytes, size_t asize, size_t hdr_size, int& w_bytesps)
{
PassFilter<int> filter = GetPeakRange(window, replica, asize);
unsigned count = 0;
int sum = 0;
w_bytesps = 0;
unsigned long bytes = 0;
AccumulatePassFilterParallel(window, asize, filter, abytes, (sum), (count), (bytes));
// calculate speed, or return 0 if not enough valid value
if (count <= (asize/2))
{
w_bytesps = 0;
return 0;
}
bytes += (unsigned long)(hdr_size * count); //Add protocol headers to bytes received
w_bytesps = ceilPerMega(sum, bytes);
return ceilPerMega(sum, count);
}
int CPktTimeWindowTools::getBandwidth_in(const int* window, int* replica, size_t psize)
{
PassFilter<int> filter = GetPeakRange(window, replica, psize);
int sum, count;
Tie(sum, count) = AccumulatePassFilter(window, psize, filter);
sum += filter.median;
count += 1;
return ceilPerMega(sum, count);
}
} // namespace srt