-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSeqManager.cpp
More file actions
233 lines (194 loc) · 5.76 KB
/
Copy pathSeqManager.cpp
File metadata and controls
233 lines (194 loc) · 5.76 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#define MS_CLASS "RTC::SeqManager"
// #define MS_LOG_DEV_LEVEL 3
#include "RTC/SeqManager.hpp"
#include "Logger.hpp"
#include "Utils.hpp"
#include <iterator>
namespace RTC
{
template<typename T, uint8_t N>
bool SeqManager<T, N>::SeqLowerThan::operator()(T lhs, T rhs) const
{
return Utils::Number::IsLowerThan<T, N>(lhs, rhs);
}
template<typename T, uint8_t N>
bool SeqManager<T, N>::SeqHigherThan::operator()(T lhs, T rhs) const
{
return Utils::Number::IsHigherThan<T, N>(lhs, rhs);
}
template<typename T, uint8_t N>
bool SeqManager<T, N>::IsSeqHigherThan(T lhs, T rhs)
{
return Utils::Number::IsHigherThan<T, N>(lhs, rhs);
}
template<typename T, uint8_t N>
bool SeqManager<T, N>::IsSeqLowerThan(T lhs, T rhs)
{
return Utils::Number::IsLowerThan<T, N>(lhs, rhs);
}
template<typename T, uint8_t N>
SeqManager<T, N>::SeqManager(T initialOutput) : initialOutput(initialOutput)
{
MS_TRACE();
}
template<typename T, uint8_t N>
void SeqManager<T, N>::Sync(T input)
{
MS_TRACE();
// Update base.
this->base = (this->maxOutput - input) & SeqManager::MaxValue;
// Update maxInput.
this->maxInput = input;
// Clear dropped set.
this->dropped.clear();
}
template<typename T, uint8_t N>
void SeqManager<T, N>::Drop(T input)
{
MS_TRACE();
// Mark as dropped if 'input' is higher than anyone already processed.
if (SeqManager<T, N>::IsSeqHigherThan(input, this->maxInput))
{
this->maxInput = input;
this->maxDropped = input;
// Insert input in sorted order, if not present.
const SeqLowerThan seqLowerThan;
const auto it =
std::lower_bound(this->dropped.begin(), this->dropped.end(), input, seqLowerThan);
if (it == this->dropped.end() || *it != input)
{
this->dropped.insert(it, input);
}
ClearDropped();
}
// Mark as dropped if no input was forwarded after the last dropped one and
// 'input' is higher than the last forwarded input 'this->maxForwarded'.
// Allows for properly accounting for out of order drops until an input is forwarded.
else if (this->maxInput == this->maxDropped && SeqManager<T, N>::IsSeqHigherThan(input, this->maxForwarded))
{
// Insert input in sorted order, if not present.
const SeqLowerThan seqLowerThan;
const auto it =
std::lower_bound(this->dropped.begin(), this->dropped.end(), input, seqLowerThan);
if (it == this->dropped.end() || *it != input)
{
this->dropped.insert(it, input);
}
ClearDropped();
}
}
template<typename T, uint8_t N>
bool SeqManager<T, N>::Input(T input, T& output)
{
MS_TRACE();
auto base = this->base;
// No dropped inputs to consider.
if (this->dropped.empty())
{
goto done;
}
// Dropped inputs present, cleanup and update base.
else
{
// Set 'maxInput' here if needed before calling ClearDropped().
if (this->started && SeqManager<T, N>::IsSeqHigherThan(input, this->maxInput))
{
this->maxInput = input;
this->maxForwarded = input;
}
ClearDropped();
base = this->base;
}
// No dropped inputs to consider after cleanup.
if (this->dropped.empty())
{
goto done;
}
else
{
const SeqLowerThan seqLowerThan;
const auto it =
std::lower_bound(this->dropped.begin(), this->dropped.end(), input, seqLowerThan);
if (it != this->dropped.end() && *it == input)
{
MS_DEBUG_DEV("trying to send a dropped input");
return false;
}
// There are dropped inputs, calculate 'base' for this input.
auto droppedCount = std::distance(
this->dropped.begin(),
std::lower_bound(this->dropped.begin(), this->dropped.end(), input, SeqLowerThan()));
base = (this->base - droppedCount) & SeqManager::MaxValue;
}
done:
output = (input + base) & SeqManager::MaxValue;
if (!this->started)
{
this->started = true;
this->maxInput = input;
this->maxForwarded = input;
this->maxOutput = output;
}
else
{
// New input is higher than the maximum seen.
if (SeqManager<T, N>::IsSeqHigherThan(input, this->maxInput))
{
this->maxInput = input;
this->maxForwarded = input;
}
// New output is higher than the maximum seen.
if (SeqManager<T, N>::IsSeqHigherThan(output, this->maxOutput))
{
this->maxOutput = output;
}
}
output = (output + this->initialOutput) & SeqManager::MaxValue;
return true;
}
template<typename T, uint8_t N>
T SeqManager<T, N>::GetMaxInput() const
{
return this->maxInput;
}
template<typename T, uint8_t N>
T SeqManager<T, N>::GetMaxOutput() const
{
// 'maxOutput' is stored in the offset-less space (as 'base' and comparisons
// use it), so apply 'initialOutput' here, just like `Input()` does before
// returning the output to the caller.
return (this->maxOutput + this->initialOutput) & SeqManager::MaxValue;
}
/*
* Delete droped inputs greater than maxInput, which belong to a previous
* cycle.
*/
template<typename T, uint8_t N>
void SeqManager<T, N>::ClearDropped()
{
MS_TRACE();
// Cleanup dropped values.
if (this->dropped.empty())
{
return;
}
const size_t previousDroppedSize = this->dropped.size();
// Cleanup dropped values.
this->dropped.erase(
this->dropped.begin(),
std::find_if(
this->dropped.begin(),
this->dropped.end(),
[this](T value)
{
return !SeqManager<T, N>::IsSeqHigherThan(value, this->maxInput);
}));
// Adapt base.
this->base = (this->base - (previousDroppedSize - this->dropped.size())) & SeqManager::MaxValue;
}
// Explicit instantiation to have all SeqManager definitions in this file.
template class SeqManager<uint8_t>; // For codecs.
template class SeqManager<uint8_t, 3>; // For testing.
template class SeqManager<uint16_t>; // For RTP sequence numbers.
template class SeqManager<uint16_t, 15>; // For PictureID (15 bits).
} // namespace RTC