RTC: Add utest for srs_rtp_seq_distance

pull/1809/head
忘篱 5 years ago
parent be1b0bf941
commit 3f5ab8dc63

@ -134,7 +134,7 @@ function show_help() {
Presets:
--x86-64, --x86-x64 [default] For x86/x64 cpu, common pc and servers.
--arm Enable crossbuild for ARM, should also set bellow toolchain options.
--mips Enable crossbuild for MIPS
--osx Enable build for OSX/Darwin AppleOS.
Features:
-h, --help Print this message and exit 0.
@ -175,7 +175,6 @@ Performance: @see https://blog.csdn.net/win_lin/article/details/5
Toolchain options: @see https://github.com/ossrs/srs/issues/1547#issuecomment-576078411
--static Whether add '-static' to link options.
--arm Enable crossbuild for ARM.
--mips Enable crossbuild for MIPS.
--cc=<CC> Use c compiler CC, default is gcc.
--cxx=<CXX> Use c++ compiler CXX, default is g++.
--ar=<AR> Use archive tool AR, default is ar.

2
trunk/configure vendored

@ -407,7 +407,7 @@ fi
if [ $SRS_UTEST = YES ]; then
MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_protocol" "srs_utest_kernel" "srs_utest_core"
"srs_utest_config" "srs_utest_rtmp" "srs_utest_http" "srs_utest_avc" "srs_utest_reload"
"srs_utest_mp4" "srs_utest_service" "srs_utest_app")
"srs_utest_mp4" "srs_utest_service" "srs_utest_app" "srs_utest_rtc")
ModuleLibIncs=(${SRS_OBJS_DIR} ${LibSTRoot} ${LibSSLRoot})
if [[ $SRS_RTC == YES ]]; then
ModuleLibIncs+=("${LibFfmpegRoot[*]}" ${LibSrtpRoot})

@ -151,8 +151,8 @@ SrsRtpNackInfo::SrsRtpNackInfo()
req_nack_count_ = 0;
}
bool SrsRtpNackForReceiver::SeqComp::operator()(const uint16_t& low, const uint16_t& high) const {
return srs_rtp_seq_distance(low, high) > 0;
bool SrsRtpNackForReceiver::SeqComp::operator()(const uint16_t& pre_value, const uint16_t& value) const {
return srs_rtp_seq_distance(pre_value, value) > 0;
}
SrsRtpNackForReceiver::SrsRtpNackForReceiver(SrsRtpRingBuffer* rtp, size_t queue_size)

@ -35,13 +35,14 @@ class SrsRtpQueue;
class SrsRtpRingBuffer;
// The "distance" between two uint16 number, for example:
// distance(low=3, high=5) === (int16_t)(uint16_t)((uint16_t)3-(uint16_t)5) === -2
// distance(low=3, high=65534) === (int16_t)(uint16_t)((uint16_t)3-(uint16_t)65534) === 5
// distance(low=65532, high=65534) === (int16_t)(uint16_t)((uint16_t)65532-(uint16_t)65534) === -2
// 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.
inline int16_t srs_rtp_seq_distance(const uint16_t& low, const uint16_t& high)
// @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)(high - low);
return (int16_t)(value - prev_value);
}
// For UDP, the packets sequence may present as bellow:
@ -123,7 +124,7 @@ class SrsRtpNackForReceiver
{
private:
struct SeqComp {
bool operator()(const uint16_t& low, const uint16_t& high) const;
bool operator()(const uint16_t& pre_value, const uint16_t& value) const;
};
private:
// Nack queue, seq order, oldest to newest.

@ -48,7 +48,6 @@ enum SrsRtcpType {
SrsRtcpType_xr = 207,
};
// RTCP Header, @see http://tools.ietf.org/html/rfc3550#section-6.1
struct SrsRtcpHeader
{

@ -59,6 +59,7 @@ class SrsRtpFUAPayload2;
class SrsSharedPtrMessage;
// TODO: FIXME: Merge with srs_rtp_seq_distance
// @see https://mp.weixin.qq.com/s/JZTInmlB9FUWXBQw_7NYqg
bool SrsSeqIsNewer(uint16_t current_sn, uint16_t last_sn);
bool SrsSeqIsRoolback(uint16_t current_sn, uint16_t last_sn);
int32_t SrsSeqDistance(uint16_t current_sn, uint16_t last_sn);

@ -350,9 +350,12 @@ srs_error_t SrsHttpMessage::set_url(string url, bool allow_jsonp)
// use server public ip when host not specified.
// to make telnet happy.
std::string host = _header.get("Host");
// If no host in header, we use local discovered IP, IPv4 first.
if (host.empty()) {
host= srs_get_public_internet_address();
host = srs_get_public_internet_address(true);
}
if (!host.empty()) {
uri = "http://" + host + _url;
}

@ -139,6 +139,26 @@ bool srs_net_device_is_internet(const sockaddr* addr)
if (IN6_IS_ADDR_SITELOCAL(&a6->sin6_addr)) {
return false;
}
// Others.
if (IN6_IS_ADDR_MULTICAST(&a6->sin6_addr)) {
return false;
}
if (IN6_IS_ADDR_MC_NODELOCAL(&a6->sin6_addr)) {
return false;
}
if (IN6_IS_ADDR_MC_LINKLOCAL(&a6->sin6_addr)) {
return false;
}
if (IN6_IS_ADDR_MC_SITELOCAL(&a6->sin6_addr)) {
return false;
}
if (IN6_IS_ADDR_MC_ORGLOCAL(&a6->sin6_addr)) {
return false;
}
if (IN6_IS_ADDR_MC_GLOBAL(&a6->sin6_addr)) {
return false;
}
}
return true;
@ -287,7 +307,7 @@ vector<SrsIPAddress*>& srs_get_local_ips()
std::string _public_internet_address;
string srs_get_public_internet_address()
string srs_get_public_internet_address(bool ipv4_only)
{
if (!_public_internet_address.empty()) {
return _public_internet_address;
@ -301,6 +321,9 @@ string srs_get_public_internet_address()
if (!ip->is_internet) {
continue;
}
if (ipv4_only && !ip->is_ipv4) {
continue;
}
srs_warn("use public address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
_public_internet_address = ip->ip;
@ -313,6 +336,9 @@ string srs_get_public_internet_address()
if (ip->is_loopback) {
continue;
}
if (ipv4_only && !ip->is_ipv4) {
continue;
}
srs_warn("use private address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
_public_internet_address = ip->ip;

@ -67,7 +67,7 @@ struct SrsIPAddress
extern std::vector<SrsIPAddress*>& srs_get_local_ips();
// Get local public ip, empty string if no public internet address found.
extern std::string srs_get_public_internet_address();
extern std::string srs_get_public_internet_address(bool ipv4_only = false);
// Detect whether specified device is internet public address.
extern bool srs_net_device_is_internet(std::string ifname);

@ -5999,15 +5999,17 @@ VOID TEST(ProtocolHTTPTest, HTTPParser)
VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
{
srs_error_t err = srs_success;
if (true) {
MockBufferIO bio;
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\nContent-Length: 5\r\n\r\nHello");
EXPECT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST, false));
ISrsHttpMessage* req = NULL;
ASSERT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
// We should read body, or next parsing message will fail.
// @see https://github.com/ossrs/srs/issues/1181
@ -6019,7 +6021,7 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
// Should fail because there is body which not read.
// @see https://github.com/ossrs/srs/issues/1181
ASSERT_FALSE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_FAILED(hp.parse_message(&bio, &req));
srs_freep(req);
}
@ -6028,11 +6030,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET");
EXPECT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST, false));
// Should fail if not completed message.
ISrsHttpMessage* req = NULL;
ASSERT_FALSE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_FAILED(hp.parse_message(&bio, &req));
srs_freep(req);
}
@ -6041,14 +6043,14 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\nContent-Length: 5\r\n\r\nHello");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST, false));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
ASSERT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
char v[64] = {0};
EXPECT_TRUE(0 == req->body_reader()->read(v, sizeof(v), NULL));
HELPER_ASSERT_SUCCESS(req->body_reader()->read(v, sizeof(v), NULL));
EXPECT_TRUE(string("Hello") == string(v));
EXPECT_TRUE(req->body_reader()->eof());
@ -6059,11 +6061,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\nContent-Length: 0\r\n\r\n");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST, false));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
EXPECT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
}
if (true) {
@ -6071,11 +6073,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\n\r\n");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST, false));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
EXPECT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
}
if (true) {
@ -6083,11 +6085,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\n\r\n");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST, false));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
EXPECT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
}
}

@ -0,0 +1,104 @@
/*
The MIT License (MIT)
Copyright (c) 2013-2020 Winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_utest_rtc.hpp>
#include <srs_kernel_error.hpp>
#include <srs_core_autofree.hpp>
#include <srs_app_rtc_queue.hpp>
#include <srs_kernel_rtc_rtp.hpp>
VOID TEST(KernelRTCTest, SequenceCompare)
{
if (true) {
EXPECT_EQ(0, srs_rtp_seq_distance(0, 0));
EXPECT_EQ(0, srs_rtp_seq_distance(1, 1));
EXPECT_EQ(0, srs_rtp_seq_distance(3, 3));
EXPECT_EQ(1, srs_rtp_seq_distance(0, 1));
EXPECT_EQ(-1, srs_rtp_seq_distance(1, 0));
EXPECT_EQ(1, srs_rtp_seq_distance(65535, 0));
}
if (true) {
EXPECT_FALSE(srs_rtp_seq_distance(1, 1) > 0);
EXPECT_TRUE(srs_rtp_seq_distance(65534, 65535) > 0);
EXPECT_TRUE(srs_rtp_seq_distance(0, 1) > 0);
EXPECT_TRUE(srs_rtp_seq_distance(255, 256) > 0);
EXPECT_TRUE(srs_rtp_seq_distance(65535, 0) > 0);
EXPECT_TRUE(srs_rtp_seq_distance(65280, 0) > 0);
EXPECT_TRUE(srs_rtp_seq_distance(65535, 255) > 0);
EXPECT_TRUE(srs_rtp_seq_distance(65280, 255) > 0);
EXPECT_FALSE(srs_rtp_seq_distance(0, 65535) > 0);
EXPECT_FALSE(srs_rtp_seq_distance(0, 65280) > 0);
EXPECT_FALSE(srs_rtp_seq_distance(255, 65535) > 0);
EXPECT_FALSE(srs_rtp_seq_distance(255, 65280) > 0);
// Note that it's TRUE at https://mp.weixin.qq.com/s/JZTInmlB9FUWXBQw_7NYqg
EXPECT_FALSE(srs_rtp_seq_distance(0, 32768) > 0);
// It's FALSE definitely.
EXPECT_FALSE(srs_rtp_seq_distance(32768, 0) > 0);
}
if (true) {
EXPECT_FALSE(SrsSeqIsNewer(1, 1));
EXPECT_TRUE(SrsSeqIsNewer(65535, 65534));
EXPECT_TRUE(SrsSeqIsNewer(1, 0));
EXPECT_TRUE(SrsSeqIsNewer(256, 255));
EXPECT_TRUE(SrsSeqIsNewer(0, 65535));
EXPECT_TRUE(SrsSeqIsNewer(0, 65280));
EXPECT_TRUE(SrsSeqIsNewer(255, 65535));
EXPECT_TRUE(SrsSeqIsNewer(255, 65280));
EXPECT_FALSE(SrsSeqIsNewer(65535, 0));
EXPECT_FALSE(SrsSeqIsNewer(65280, 0));
EXPECT_FALSE(SrsSeqIsNewer(65535, 255));
EXPECT_FALSE(SrsSeqIsNewer(65280, 255));
EXPECT_TRUE(SrsSeqIsNewer(32768, 0));
EXPECT_FALSE(SrsSeqIsNewer(0, 32768));
}
if (true) {
EXPECT_FALSE(SrsSeqDistance(1, 1) > 0);
EXPECT_TRUE(SrsSeqDistance(65535, 65534) > 0);
EXPECT_TRUE(SrsSeqDistance(1, 0) > 0);
EXPECT_TRUE(SrsSeqDistance(256, 255) > 0);
EXPECT_TRUE(SrsSeqDistance(0, 65535) > 0);
EXPECT_TRUE(SrsSeqDistance(0, 65280) > 0);
EXPECT_TRUE(SrsSeqDistance(255, 65535) > 0);
EXPECT_TRUE(SrsSeqDistance(255, 65280) > 0);
EXPECT_FALSE(SrsSeqDistance(65535, 0) > 0);
EXPECT_FALSE(SrsSeqDistance(65280, 0) > 0);
EXPECT_FALSE(SrsSeqDistance(65535, 255) > 0);
EXPECT_FALSE(SrsSeqDistance(65280, 255) > 0);
EXPECT_TRUE(SrsSeqDistance(32768, 0) > 0);
EXPECT_FALSE(SrsSeqDistance(0, 32768) > 0);
}
}

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2013-2020 Winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SRS_UTEST_RTC_HPP
#define SRS_UTEST_RTC_HPP
/*
#include <srs_utest_rtc.hpp>
*/
#include <srs_utest.hpp>
#endif

@ -110,7 +110,7 @@ VOID TEST(TCPServerTest, PingPong)
SrsTcpClient c(_srs_tmp_host, _srs_tmp_port, _srs_tmp_timeout);
HELPER_EXPECT_SUCCESS(c.connect());
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
EXPECT_TRUE(h.fd != NULL);
}
@ -123,7 +123,7 @@ VOID TEST(TCPServerTest, PingPong)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
@ -145,7 +145,7 @@ VOID TEST(TCPServerTest, PingPong)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
@ -169,7 +169,7 @@ VOID TEST(TCPServerTest, PingPong)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
@ -204,7 +204,7 @@ VOID TEST(TCPServerTest, PingPongWithTimeout)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
@ -226,7 +226,7 @@ VOID TEST(TCPServerTest, PingPongWithTimeout)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
@ -248,7 +248,7 @@ VOID TEST(TCPServerTest, PingPongWithTimeout)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
@ -428,7 +428,7 @@ VOID TEST(TCPServerTest, WritevIOVC)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
@ -458,7 +458,7 @@ VOID TEST(TCPServerTest, WritevIOVC)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif

Loading…
Cancel
Save