提交 ffb4b514 authored 作者: José Luis Millán's avatar José Luis Millán

SeqManager

上级 8268259a
#ifndef RTC_SEQ_MANAGER_HPP
#define RTC_SEQ_MANAGER_HPP
#include "common.hpp"
#include <limits> // std::numeric_limits.
#include <set>
namespace RTC
{
template<typename T>
class SeqManager
{
public:
static constexpr T MaxValue = std::numeric_limits<T>::max();
private:
struct SeqLowerThan
{
bool operator()(const T& lhs, const T& rhs) const;
};
struct SeqHigherThan
{
bool operator()(const T& lhs, const T& rhs) const;
};
private:
static const SeqLowerThan isSeqLowerThan;
static const SeqHigherThan isSeqHigherThan;
public:
static bool IsSeqLowerThan(const T& lhs, const T& rhs);
static bool IsSeqHigherThan(const T& lhs, const T& rhs);
public:
SeqManager();
public:
void Sync(T input);
void Drop(T input);
bool Input(const T input, T& output);
private:
T base{ 0 };
T maxOutput{ 0 };
T maxInput{ 0 };
std::set<T, SeqLowerThan> dropped;
};
// Tell the compiler not to bother instantiating this template. It is done in the .cpp file.
extern template class SeqManager<uint16_t>;
extern template class SeqManager<uint32_t>;
} // namespace RTC
#endif
......@@ -35,6 +35,7 @@
'src/RTC/RtpStreamRecv.cpp',
'src/RTC/RtpStreamSend.cpp',
'src/RTC/RtpDataCounter.cpp',
'src/RTC/SeqManager.cpp',
'src/RTC/SrtpSession.cpp',
'src/RTC/StunMessage.cpp',
'src/RTC/TcpConnection.cpp',
......@@ -127,6 +128,7 @@
'include/RTC/RtpStreamRecv.hpp',
'include/RTC/RtpStreamSend.hpp',
'include/RTC/RtpDataCounter.hpp',
'include/RTC/SeqManager.hpp',
'include/RTC/SrtpSession.hpp',
'include/RTC/StunMessage.hpp',
'include/RTC/TcpConnection.hpp',
......@@ -265,6 +267,7 @@
'test/test-rtcp.cpp',
'test/test-bitrate.cpp',
'test/test-rtpstreamrecv.cpp',
'test/test-seqmanager.cpp',
# C++ include files
'test/catch.hpp',
'test/helpers.hpp'
......
#define MS_CLASS "RTC::SeqManager"
// #define MS_LOG_DEV
#include "RTC/SeqManager.hpp"
#include "Logger.hpp"
namespace RTC
{
template<typename T>
bool SeqManager<T>::SeqLowerThan::operator()(const T& lhs, const T& rhs) const
{
return ((rhs > lhs) && (rhs - lhs <= MaxValue / 2)) ||
((lhs > rhs) && (lhs - rhs > MaxValue / 2));
}
template<typename T>
bool SeqManager<T>::SeqHigherThan::operator()(const T& lhs, const T& rhs) const
{
return ((lhs > rhs) && (lhs - rhs <= MaxValue / 2)) ||
((rhs > lhs) && (rhs - lhs > MaxValue / 2));
}
template<typename T>
const typename SeqManager<T>::SeqLowerThan SeqManager<T>::isSeqLowerThan;
template<typename T>
const typename SeqManager<T>::SeqHigherThan SeqManager<T>::isSeqHigherThan;
template<typename T>
bool SeqManager<T>::IsSeqLowerThan(const T& lhs, const T& rhs)
{
return isSeqLowerThan(lhs, rhs);
}
template<typename T>
bool SeqManager<T>::IsSeqHigherThan(const T& lhs, const T& rhs)
{
return isSeqHigherThan(lhs, rhs);
}
template<typename T>
SeqManager<T>::SeqManager()
{
}
template<typename T>
void SeqManager<T>::Sync(T input)
{
// Update base.
this->base = this->maxOutput - input + 1;
// Update maxInput.
this->maxInput = input;
// Clear dropped set.
this->dropped.clear();
}
template<typename T>
void SeqManager<T>::Drop(T input)
{
// Mark as dropped if 'input' is higher than anyone already processed.
if (SeqManager<T>::IsSeqHigherThan(input, this->maxInput))
this->dropped.insert(input);
}
template<typename T>
bool SeqManager<T>::Input(const T input, T& output)
{
auto base = this->base;
// There are dropped inputs. Synchronize.
if (!this->dropped.empty())
{
// Delete dropped inputs older than input - MaxValue/2.
auto it = this->dropped.lower_bound(input - MaxValue / 2);
this->dropped.erase(this->dropped.begin(), it);
// Check whether this input was dropped.
it = this->dropped.find(input);
if (it != this->dropped.end())
{
MS_WARN_TAG(rtp, "trying to send a dropped input");
return false;
}
// Count dropped entries before 'input' in order to adapt the base.
size_t dropped = std::count_if(
this->dropped.begin(), this->dropped.end(), [&input](T i) { return i < input; });
base -= dropped;
}
output = input + base;
T idelta = input - this->maxInput;
T odelta = output - this->maxOutput;
// New input is higher than the maximum seen. But less than acceptable units higher.
// Keep it as the maximum seen. See Drop().
if (idelta < MaxValue / 2)
{
this->maxInput = input;
}
else if (idelta != 0)
{
MS_WARN_TAG(rtp, "trying to send a too new input");
return false;
}
// New output is higher than the maximum seen. But less than acceptable units higher.
// Keep it as the maximum seen. See Sync().
if (odelta < MaxValue / 2)
this->maxOutput = output;
return true;
}
// Explicit instantiation to have all SeqManager definitions in this file.
template class SeqManager<uint8_t>;
template class SeqManager<uint16_t>;
template class SeqManager<uint32_t>;
} // namespace RTC
#include "include/catch.hpp"
#include "common.hpp"
#include <vector>
#include "RTC/SeqManager.hpp"
using namespace RTC;
struct Input
{
Input(uint16_t input, uint16_t output, bool sync=false, bool drop=false)
: input(input), output(output), sync(sync), drop(drop)
{}
uint16_t input{ 0 };
uint16_t output{ 0 };
bool sync{ false };
bool drop{ false };
};
void validate(RTC::SeqManager<uint16_t>& seqManager, std::vector<Input>& inputs)
{
for (auto element : inputs)
{
if (element.sync)
seqManager.Sync(element.input);
if (element.drop)
{
seqManager.Drop(element.input);
}
else
{
uint16_t output;
seqManager.Input(element.input, output);
REQUIRE(output == element.output);
}
}
}
SCENARIO("RTC::SeqManager", "[rtc]")
{
SECTION("0 is greater than 65000")
{
REQUIRE(RTC::SeqManager<uint16_t>::IsSeqHigherThan(0, 65000) == true);
}
SECTION("receive ordered numbers, no sync, no drop")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 1, 1, false, false },
{ 2, 2, false, false },
{ 3, 3, false, false },
{ 4, 4, false, false },
{ 5, 5, false, false },
{ 6, 6, false, false },
{ 7, 7, false, false },
{ 8, 8, false, false },
{ 9, 9, false, false },
{ 10, 10, false, false },
{ 11, 11, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("receive ordered numbers, sync, no drop")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 1, 1, false, false },
{ 2, 2, false, false },
{ 80, 3, true, false },
{ 81, 4, false, false },
{ 82, 5, false, false },
{ 83, 6, false, false },
{ 84, 7, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("receive ordered numbers, sync, drop")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 1, 1, false, false },
{ 2, 2, false, false },
{ 3, 3, false, false },
{ 4, 4, true, false }, // sync.
{ 5, 5, false, false },
{ 6, 6, false, false },
{ 7, 7, true, false }, // sync.
{ 8, 0, false, true }, // drop.
{ 9, 8, false, false },
{ 11, 0, false, true }, // drop.
{ 10, 9, false, false },
{ 12, 10, false, false },
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("receive ordered wrapped numbers")
{
std::vector<Input> inputs =
{
{ 65533, 65533, false, false },
{ 65534, 65534, false, false },
{ 65535, 65535, false, false },
{ 0, 0, false, false },
{ 1, 1, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("receive sequence numbers with a big jump")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 1, 1, false, false },
{ 1000, 1000, false, false },
{ 1001, 1001, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("receive mixed numbers with a big jump, drop before jump")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 1, 0, false, true }, // drop.
{ 100, 99, false, false },
{ 100, 99, false, false },
{ 103, 0, false, true }, // drop.
{ 101, 100, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("receive mixed numbers with a big jump, drop after jump")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 1, 1, false, false },
{ 100, 0, false, true }, // drop.
{ 103, 0, false, true }, // drop.
{ 101, 100, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("drop, receive numbers newer and older than the one dropped")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 2, 0, false, true }, // drop.
{ 3, 2, false, false },
{ 4, 3, false, false },
{ 1, 1, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
SECTION("receive mixed numbers, sync, drop")
{
std::vector<Input> inputs =
{
{ 0, 0, false, false },
{ 1, 1, false, false },
{ 2, 2, false, false },
{ 3, 3, false, false },
{ 7, 7, false, false },
{ 6, 0, false, true }, // drop.
{ 8, 8, false, false },
{ 10, 10, false, false },
{ 9, 9, false, false },
{ 11, 11, false, false },
{ 0, 12, true, false }, // sync.
{ 2, 14, false, false },
{ 3, 15, false, false },
{ 4, 16, false, false },
{ 5, 17, false, false },
{ 6, 18, false, false },
{ 7, 19, false, false },
{ 8, 20, false, false },
{ 9, 21, false, false },
{ 10, 22, false, false },
{ 9, 0, false, true }, // drop.
{ 61, 23, true, false }, // sync.
{ 62, 24, false, false },
{ 63, 25, false, false },
{ 64, 26, false, false },
{ 65, 27, false, false },
{ 11, 28, true, false }, // sync.
{ 12, 29, false, false },
{ 13, 30, false, false },
{ 14, 31, false, false },
{ 15, 32, false, false },
{ 1, 33, true, false }, // sync.
{ 2, 34, false, false },
{ 3, 35, false, false },
{ 4, 36, false, false },
{ 5, 37, false, false },
{ 65533, 38, true, false }, // sync.
{ 65534, 39, false, false },
{ 65535, 40, false, false },
{ 0, 41, true, false }, // sync.
{ 1, 42, false, false },
{ 3, 0, false, true }, // drop.
{ 4, 44, false, false },
{ 5, 45, false, false },
{ 6, 46, false, false },
{ 7, 47, false, false }
};
RTC::SeqManager<uint16_t> seqManager;
validate(seqManager, inputs);
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论