From 5f7d23f12345a4ade34b1a6c7b1de11a6e819960 Mon Sep 17 00:00:00 2001 From: "Alex.CR" Date: Wed, 12 Feb 2020 07:03:27 -0600 Subject: [PATCH] add srt parameter configure (#1599) * if there isn't srt connect, it needn't epoll wait * solve repush srt bugs * change two thread to one thread * mpegts discard header is not 0x47 * add srt_epoll_clear_usocks * add srt parameter configure --- trunk/conf/srt.conf | 5 ++ trunk/src/app/srs_app_config.cpp | 145 ++++++++++++++++++++++++++++++- trunk/src/app/srs_app_config.hpp | 20 +++++ trunk/src/srt/srt_handle.cpp | 11 +-- trunk/src/srt/srt_server.cpp | 50 +++++++++++ trunk/src/srt/srt_server.hpp | 2 + 6 files changed, 227 insertions(+), 6 deletions(-) diff --git a/trunk/conf/srt.conf b/trunk/conf/srt.conf index d648b16b3..b9fcc9d32 100644 --- a/trunk/conf/srt.conf +++ b/trunk/conf/srt.conf @@ -9,6 +9,7 @@ http_api { enabled on; listen 1985; } + http_server { enabled on; listen 8080; @@ -18,6 +19,10 @@ http_server { srt_server { enabled on; listen 10080; + maxbw 1000000000; + connect_timeout 4000; + peerlatency 300; + recvlatency 300; } # @doc https://github.com/ossrs/srs/issues/1147#issuecomment-577607026 diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 585e1d6f0..1cea608db 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -3525,7 +3525,10 @@ srs_error_t SrsConfig::check_normal_config() SrsConfDirective* conf = root->get("srt_server"); for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; - if (n != "enabled" && n != "listen") { + if (n != "enabled" && n != "listen" && n != "maxbw" + && n != "mss" && n != "latency" && n != "recvlatency" + && n != "peerlatency" && n != "tlpkdrop" && n != "connect_timeout" + && n != "sendbuf" && n != "recvbuf" && n != "payloadsize") { return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal srt_stream.%s", n.c_str()); } } @@ -6701,6 +6704,146 @@ unsigned short SrsConfig::get_srt_listen_port() return (unsigned short)atoi(conf->arg0().c_str()); } +int SrsConfig::get_srto_maxbw() { + static int64_t DEFAULT = -1; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("maxbw"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +int SrsConfig::get_srto_mss() { + static int DEFAULT = 1500; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("mms"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +int SrsConfig::get_srto_latency() { + static int DEFAULT = 120; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("latency"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +int SrsConfig::get_srto_recv_latency() { + static int DEFAULT = 120; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("recvlatency"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +int SrsConfig::get_srto_peer_latency() { + static int DEFAULT = 120; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("peerlatency"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +bool SrsConfig::get_srto_tlpkdrop() { + static bool DEFAULT = true; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("tlpkdrop"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return SRS_CONF_PERFER_TRUE(conf->arg0()); +} + +int SrsConfig::get_srto_conntimeout() { + static int DEFAULT = 3000; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("connect_timeout"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +int SrsConfig::get_srto_sendbuf() { + static int64_t DEFAULT = 8192 * (1500-28); + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("sendbuf"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +int SrsConfig::get_srto_recvbuf() { + static int64_t DEFAULT = 8192 * (1500-28); + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("recvbuf"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + +int SrsConfig::get_srto_payloadsize() { + static int DEFAULT = 1316; + SrsConfDirective* conf = root->get("srt_server"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("payloadsize"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return atoi(conf->arg0().c_str()); +} + bool SrsConfig::get_http_stream_enabled() { SrsConfDirective* conf = root->get("http_server"); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 197c5decb..90a57e0c3 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -595,6 +595,26 @@ public: virtual bool get_srt_enabled(); // Get the srt service listen port virtual unsigned short get_srt_listen_port(); + // Get the srt SRTO_MAXBW, max bandwith, default is -1. + virtual int get_srto_maxbw(); + // Get the srt SRTO_MSS, Maximum Segment Size, default is 1500. + virtual int get_srto_mss(); + // Get the srt SRTO_LATENCY, latency, default is 0 which means peer/recv latency is 120ms. + virtual int get_srto_latency(); + // Get the srt SRTO_RCVLATENCY, recv latency, default is 120ms. + virtual int get_srto_recv_latency(); + // Get the srt SRTO_PEERLATENCY, peer latency, default is 0.. + virtual int get_srto_peer_latency(); + // Get the srt SRTO_TLPKDROP, Too-late Packet Drop, default is true. + virtual bool get_srto_tlpkdrop(); + // Get the srt SRTO_CONNTIMEO, connection timeout, default is 3000ms. + virtual int get_srto_conntimeout(); + // Get the srt SRTO_SNDBUF, send buffer, default is 8192 × (1500-28). + virtual int get_srto_sendbuf(); + // Get the srt SRTO_RCVBUF, recv buffer, default is 8192 × (1500-28). + virtual int get_srto_recvbuf(); + // SRTO_PAYLOADSIZE + virtual int get_srto_payloadsize(); // http_hooks section private: diff --git a/trunk/src/srt/srt_handle.cpp b/trunk/src/srt/srt_handle.cpp index dfba60cd1..4592785e3 100644 --- a/trunk/src/srt/srt_handle.cpp +++ b/trunk/src/srt/srt_handle.cpp @@ -131,13 +131,14 @@ void srt_handle::add_newconn(SRT_CONN_PTR conn_ptr, int events) { int val_i; int opt_len = sizeof(int); - val_i = 1000; - srt_setsockopt(conn_ptr->get_conn(), 0, SRTO_LATENCY, &val_i, opt_len); - val_i = 2048; - srt_setsockopt(conn_ptr->get_conn(), 0, SRTO_MAXBW, &val_i, opt_len); - srt_getsockopt(conn_ptr->get_conn(), 0, SRTO_LATENCY, &val_i, &opt_len); srs_trace("srto SRTO_LATENCY=%d", val_i); + + srt_getsockopt(conn_ptr->get_conn(), 0, SRTO_PEERLATENCY, &val_i, &opt_len); + srs_trace("srto SRTO_PEERLATENCY=%d", val_i); + srt_getsockopt(conn_ptr->get_conn(), 0, SRTO_RCVLATENCY, &val_i, &opt_len); + srs_trace("srto SRTO_RCVLATENCY=%d", val_i); + srt_getsockopt(conn_ptr->get_conn(), 0, SRTO_SNDBUF, &val_i, &opt_len); srs_trace("srto SRTO_SNDBUF=%d", val_i); srt_getsockopt(conn_ptr->get_conn(), 0, SRTO_RCVBUF, &val_i, &opt_len); diff --git a/trunk/src/srt/srt_server.cpp b/trunk/src/srt/srt_server.cpp index ae4918873..06660135c 100644 --- a/trunk/src/srt/srt_server.cpp +++ b/trunk/src/srt/srt_server.cpp @@ -25,6 +25,54 @@ srt_server::~srt_server() } +int srt_server::init_srt_parameter() { + const int DEF_LATENCY = 188*7; + + int opt_len = sizeof(int); + + if (_server_socket == -1) { + return -1; + } + int maxbw = _srs_config->get_srto_maxbw(); + srt_setsockopt(_server_socket, 0, SRTO_MAXBW, &maxbw, opt_len); + int mss = _srs_config->get_srto_mss(); + srt_setsockopt(_server_socket, 0, SRTO_MSS, &mss, opt_len); + + bool tlpkdrop = _srs_config->get_srto_tlpkdrop(); + int tlpkdrop_i = tlpkdrop ? 1 : 0; + srt_setsockopt(_server_socket, 0, SRTO_TLPKTDROP, &tlpkdrop_i, opt_len); + + int connection_timeout = _srs_config->get_srto_conntimeout(); + srt_setsockopt(_server_socket, 0, SRTO_CONNTIMEO, &connection_timeout, opt_len); + + int send_buff = _srs_config->get_srto_sendbuf(); + srt_setsockopt(_server_socket, 0, SRTO_SNDBUF, &send_buff, opt_len); + int recv_buff = _srs_config->get_srto_recvbuf(); + srt_setsockopt(_server_socket, 0, SRTO_RCVBUF, &recv_buff, opt_len); + int payload_size = _srs_config->get_srto_payloadsize(); + srt_setsockopt(_server_socket, 0, SRTO_PAYLOADSIZE, &payload_size, opt_len); + + int latency = _srs_config->get_srto_latency(); + if (DEF_LATENCY != latency) { + srt_setsockopt(_server_socket, 0, SRTO_LATENCY, &latency, opt_len); + } + + int recv_latency = _srs_config->get_srto_recv_latency(); + if (DEF_LATENCY != recv_latency) { + srt_setsockopt(_server_socket, 0, SRTO_RCVLATENCY, &recv_latency, opt_len); + } + + int peer_latency = _srs_config->get_srto_peer_latency(); + if (DEF_LATENCY != peer_latency) { + srt_setsockopt(_server_socket, 0, SRTO_PEERLATENCY, &recv_latency, opt_len); + } + + srs_trace("init srt parameter, maxbw:%d, mss:%d, tlpkdrop:%d, connect timeout:%d, \ +send buff:%d, recv buff:%d, payload size:%d, latency:%d, recv latency:%d, peer latency:%d", + maxbw, mss, tlpkdrop, connection_timeout, send_buff, recv_buff, payload_size, + latency, recv_latency, peer_latency); + return 0; +} int srt_server::init_srt() { if (_server_socket != -1) { return -1; @@ -54,6 +102,8 @@ int srt_server::init_srt() { return -2; } + init_srt_parameter(); + _pollid = srt_epoll_create(); if (_pollid < -1) { srs_error("srt server srt_epoll_create error, port=%d", _listen_port); diff --git a/trunk/src/srt/srt_server.hpp b/trunk/src/srt/srt_server.hpp index 1234b891a..0ef244b4b 100644 --- a/trunk/src/srt/srt_server.hpp +++ b/trunk/src/srt/srt_server.hpp @@ -21,6 +21,8 @@ public: private: //init srt socket and srt epoll int init_srt(); + int init_srt_parameter(); + //srt main epoll loop void on_work(); //accept new srt connection