mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
RTC: Merge srs_rtp_seq_distance with srs_seq_is_newer
This commit is contained in:
parent
3f5ab8dc63
commit
d603b1580e
5 changed files with 76 additions and 85 deletions
|
@ -34,17 +34,6 @@ class SrsRtpPacket2;
|
||||||
class SrsRtpQueue;
|
class SrsRtpQueue;
|
||||||
class SrsRtpRingBuffer;
|
class SrsRtpRingBuffer;
|
||||||
|
|
||||||
// The "distance" between two uint16 number, for example:
|
|
||||||
// distance(prev_value=3, value=5) === (int16_t)(uint16_t)((uint16_t)3-(uint16_t)5) === -2
|
|
||||||
// distance(prev_value=3, value=65534) === (int16_t)(uint16_t)((uint16_t)3-(uint16_t)65534) === 5
|
|
||||||
// distance(prev_value=65532, value=65534) === (int16_t)(uint16_t)((uint16_t)65532-(uint16_t)65534) === -2
|
|
||||||
// For RTP sequence, it's only uint16 and may flip back, so 3 maybe 3+0xffff.
|
|
||||||
// @see https://mp.weixin.qq.com/s/JZTInmlB9FUWXBQw_7NYqg
|
|
||||||
inline int16_t srs_rtp_seq_distance(const uint16_t& prev_value, const uint16_t& value)
|
|
||||||
{
|
|
||||||
return (int16_t)(value - prev_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// For UDP, the packets sequence may present as bellow:
|
// For UDP, the packets sequence may present as bellow:
|
||||||
// [seq1(done)|seq2|seq3 ... seq10|seq11(lost)|seq12|seq13]
|
// [seq1(done)|seq2|seq3 ... seq10|seq11(lost)|seq12|seq13]
|
||||||
// \___(head_sequence_) \ \___(highest_sequence_)
|
// \___(head_sequence_) \ \___(highest_sequence_)
|
||||||
|
|
|
@ -61,7 +61,7 @@ struct SrsRtcpHeader
|
||||||
|
|
||||||
struct SrsSeqCompareLess {
|
struct SrsSeqCompareLess {
|
||||||
bool operator()(const uint16_t &lhs, const uint16_t &rhs) const {
|
bool operator()(const uint16_t &lhs, const uint16_t &rhs) const {
|
||||||
return SrsSeqIsNewer(rhs, lhs);
|
return srs_seq_is_newer(rhs, lhs);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -33,54 +33,24 @@ using namespace std;
|
||||||
#include <srs_kernel_utility.hpp>
|
#include <srs_kernel_utility.hpp>
|
||||||
#include <srs_kernel_flv.hpp>
|
#include <srs_kernel_flv.hpp>
|
||||||
|
|
||||||
//sn comparison,if current_sn is more(newer) than last_sn,return true,else return false
|
// If value is newer than pre_value,return true; otherwise false
|
||||||
bool SrsSeqIsNewer(uint16_t current_sn, uint16_t last_sn) {
|
bool srs_seq_is_newer(uint16_t value, uint16_t pre_value) {
|
||||||
if(current_sn > last_sn) {
|
return srs_rtp_seq_distance(pre_value, value) > 0;
|
||||||
//current_sn 65533 last_sn 5
|
|
||||||
if(current_sn - last_sn > 0x8000) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//current_sn 2 last_sn 65535
|
|
||||||
if(current_sn - last_sn < -0x8000) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SrsSeqIsRoolback(uint16_t current_sn, uint16_t last_sn)
|
bool srs_seq_is_roolback(uint16_t value, uint16_t pre_value)
|
||||||
{
|
{
|
||||||
if(SrsSeqIsNewer(current_sn, last_sn)) {
|
if(srs_seq_is_newer(value, pre_value)) {
|
||||||
if((last_sn > current_sn)) {
|
if((pre_value > value)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// caculate the difference between sn. If current_sn is more then last_sn, return positive difference, else return negative difference.
|
// If value is newer then pre_value, return positive, otherwise negative.
|
||||||
int32_t SrsSeqDistance(uint16_t current_sn, uint16_t last_sn) {
|
int32_t srs_seq_distance(uint16_t value, uint16_t pre_value) {
|
||||||
if(current_sn > last_sn) {
|
return srs_rtp_seq_distance(pre_value, value);
|
||||||
//current_sn 65535 last_sn 0
|
|
||||||
if(current_sn - last_sn > 0x8000) {
|
|
||||||
return (current_sn - last_sn - 1 - 65535);
|
|
||||||
} else {
|
|
||||||
return (current_sn - last_sn);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//current_sn 0 last_sn 65535
|
|
||||||
if(current_sn - last_sn < -0x8000) {
|
|
||||||
return (current_sn - last_sn + 65535 + 1);
|
|
||||||
} else {
|
|
||||||
return (current_sn - last_sn);
|
|
||||||
// current_sn 15039 last_sn 15042
|
|
||||||
//return last_sn - current_sn;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsRtpHeader::SrsRtpHeader()
|
SrsRtpHeader::SrsRtpHeader()
|
||||||
|
|
|
@ -58,11 +58,22 @@ class SrsRtpRawPayload;
|
||||||
class SrsRtpFUAPayload2;
|
class SrsRtpFUAPayload2;
|
||||||
class SrsSharedPtrMessage;
|
class SrsSharedPtrMessage;
|
||||||
|
|
||||||
// TODO: FIXME: Merge with srs_rtp_seq_distance
|
// The "distance" between two uint16 number, for example:
|
||||||
// @see https://mp.weixin.qq.com/s/JZTInmlB9FUWXBQw_7NYqg
|
// distance(prev_value=3, value=5) === (int16_t)(uint16_t)((uint16_t)3-(uint16_t)5) === -2
|
||||||
bool SrsSeqIsNewer(uint16_t current_sn, uint16_t last_sn);
|
// distance(prev_value=3, value=65534) === (int16_t)(uint16_t)((uint16_t)3-(uint16_t)65534) === 5
|
||||||
bool SrsSeqIsRoolback(uint16_t current_sn, uint16_t last_sn);
|
// distance(prev_value=65532, value=65534) === (int16_t)(uint16_t)((uint16_t)65532-(uint16_t)65534) === -2
|
||||||
int32_t SrsSeqDistance(uint16_t current_sn, uint16_t last_sn);
|
// For RTP sequence, it's only uint16 and may flip back, so 3 maybe 3+0xffff.
|
||||||
|
// @remark Note that srs_rtp_seq_distance(0, 32768)>0 is TRUE by https://mp.weixin.qq.com/s/JZTInmlB9FUWXBQw_7NYqg
|
||||||
|
// but for WebRTC jitter buffer it's FALSE and we follow it.
|
||||||
|
// @remark For srs_rtp_seq_distance(32768, 0)>0, it's FALSE definitely.
|
||||||
|
inline int16_t srs_rtp_seq_distance(const uint16_t& prev_value, const uint16_t& value)
|
||||||
|
{
|
||||||
|
return (int16_t)(value - prev_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool srs_seq_is_newer(uint16_t value, uint16_t pre_value);
|
||||||
|
bool srs_seq_is_roolback(uint16_t value, uint16_t pre_value);
|
||||||
|
int32_t srs_seq_distance(uint16_t value, uint16_t pre_value);
|
||||||
|
|
||||||
class SrsRtpHeader
|
class SrsRtpHeader
|
||||||
{
|
{
|
||||||
|
|
|
@ -55,50 +55,71 @@ VOID TEST(KernelRTCTest, SequenceCompare)
|
||||||
EXPECT_FALSE(srs_rtp_seq_distance(255, 65535) > 0);
|
EXPECT_FALSE(srs_rtp_seq_distance(255, 65535) > 0);
|
||||||
EXPECT_FALSE(srs_rtp_seq_distance(255, 65280) > 0);
|
EXPECT_FALSE(srs_rtp_seq_distance(255, 65280) > 0);
|
||||||
|
|
||||||
// Note that it's TRUE at https://mp.weixin.qq.com/s/JZTInmlB9FUWXBQw_7NYqg
|
// Note that srs_rtp_seq_distance(0, 32768)>0 is TRUE by https://mp.weixin.qq.com/s/JZTInmlB9FUWXBQw_7NYqg
|
||||||
|
// but for WebRTC jitter buffer it's FALSE and we follow it.
|
||||||
EXPECT_FALSE(srs_rtp_seq_distance(0, 32768) > 0);
|
EXPECT_FALSE(srs_rtp_seq_distance(0, 32768) > 0);
|
||||||
// It's FALSE definitely.
|
// It's FALSE definitely.
|
||||||
EXPECT_FALSE(srs_rtp_seq_distance(32768, 0) > 0);
|
EXPECT_FALSE(srs_rtp_seq_distance(32768, 0) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
EXPECT_FALSE(SrsSeqIsNewer(1, 1));
|
EXPECT_FALSE(srs_seq_is_newer(1, 1));
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(65535, 65534));
|
EXPECT_TRUE(srs_seq_is_newer(65535, 65534));
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(1, 0));
|
EXPECT_TRUE(srs_seq_is_newer(1, 0));
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(256, 255));
|
EXPECT_TRUE(srs_seq_is_newer(256, 255));
|
||||||
|
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(0, 65535));
|
EXPECT_TRUE(srs_seq_is_newer(0, 65535));
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(0, 65280));
|
EXPECT_TRUE(srs_seq_is_newer(0, 65280));
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(255, 65535));
|
EXPECT_TRUE(srs_seq_is_newer(255, 65535));
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(255, 65280));
|
EXPECT_TRUE(srs_seq_is_newer(255, 65280));
|
||||||
|
|
||||||
EXPECT_FALSE(SrsSeqIsNewer(65535, 0));
|
EXPECT_FALSE(srs_seq_is_newer(65535, 0));
|
||||||
EXPECT_FALSE(SrsSeqIsNewer(65280, 0));
|
EXPECT_FALSE(srs_seq_is_newer(65280, 0));
|
||||||
EXPECT_FALSE(SrsSeqIsNewer(65535, 255));
|
EXPECT_FALSE(srs_seq_is_newer(65535, 255));
|
||||||
EXPECT_FALSE(SrsSeqIsNewer(65280, 255));
|
EXPECT_FALSE(srs_seq_is_newer(65280, 255));
|
||||||
|
|
||||||
EXPECT_TRUE(SrsSeqIsNewer(32768, 0));
|
EXPECT_FALSE(srs_seq_is_newer(32768, 0));
|
||||||
EXPECT_FALSE(SrsSeqIsNewer(0, 32768));
|
EXPECT_FALSE(srs_seq_is_newer(0, 32768));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
EXPECT_FALSE(SrsSeqDistance(1, 1) > 0);
|
EXPECT_FALSE(srs_seq_distance(1, 1) > 0);
|
||||||
EXPECT_TRUE(SrsSeqDistance(65535, 65534) > 0);
|
EXPECT_TRUE(srs_seq_distance(65535, 65534) > 0);
|
||||||
EXPECT_TRUE(SrsSeqDistance(1, 0) > 0);
|
EXPECT_TRUE(srs_seq_distance(1, 0) > 0);
|
||||||
EXPECT_TRUE(SrsSeqDistance(256, 255) > 0);
|
EXPECT_TRUE(srs_seq_distance(256, 255) > 0);
|
||||||
|
|
||||||
EXPECT_TRUE(SrsSeqDistance(0, 65535) > 0);
|
EXPECT_TRUE(srs_seq_distance(0, 65535) > 0);
|
||||||
EXPECT_TRUE(SrsSeqDistance(0, 65280) > 0);
|
EXPECT_TRUE(srs_seq_distance(0, 65280) > 0);
|
||||||
EXPECT_TRUE(SrsSeqDistance(255, 65535) > 0);
|
EXPECT_TRUE(srs_seq_distance(255, 65535) > 0);
|
||||||
EXPECT_TRUE(SrsSeqDistance(255, 65280) > 0);
|
EXPECT_TRUE(srs_seq_distance(255, 65280) > 0);
|
||||||
|
|
||||||
EXPECT_FALSE(SrsSeqDistance(65535, 0) > 0);
|
EXPECT_FALSE(srs_seq_distance(65535, 0) > 0);
|
||||||
EXPECT_FALSE(SrsSeqDistance(65280, 0) > 0);
|
EXPECT_FALSE(srs_seq_distance(65280, 0) > 0);
|
||||||
EXPECT_FALSE(SrsSeqDistance(65535, 255) > 0);
|
EXPECT_FALSE(srs_seq_distance(65535, 255) > 0);
|
||||||
EXPECT_FALSE(SrsSeqDistance(65280, 255) > 0);
|
EXPECT_FALSE(srs_seq_distance(65280, 255) > 0);
|
||||||
|
|
||||||
EXPECT_TRUE(SrsSeqDistance(32768, 0) > 0);
|
EXPECT_FALSE(srs_seq_distance(32768, 0) > 0);
|
||||||
EXPECT_FALSE(SrsSeqDistance(0, 32768) > 0);
|
EXPECT_FALSE(srs_seq_distance(0, 32768) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(1, 1));
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(65535, 65534));
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(1, 0));
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(256, 255));
|
||||||
|
|
||||||
|
EXPECT_TRUE(srs_seq_is_roolback(0, 65535));
|
||||||
|
EXPECT_TRUE(srs_seq_is_roolback(0, 65280));
|
||||||
|
EXPECT_TRUE(srs_seq_is_roolback(255, 65535));
|
||||||
|
EXPECT_TRUE(srs_seq_is_roolback(255, 65280));
|
||||||
|
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(65535, 0));
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(65280, 0));
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(65535, 255));
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(65280, 255));
|
||||||
|
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(32768, 0));
|
||||||
|
EXPECT_FALSE(srs_seq_is_roolback(0, 32768));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue