From 573449f1b0d36c996aab50afa6b04c9dbe7c7de7 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 4 Apr 2020 15:36:35 +0800 Subject: [PATCH] For #307, refine RTC latency from 600ms to 200ms. 4.0.20 --- README.md | 1 + trunk/conf/full.conf | 14 +++++++++++-- trunk/src/app/srs_app_config.cpp | 26 ++++++++++++++++++------- trunk/src/app/srs_app_config.hpp | 6 +++--- trunk/src/app/srs_app_rtc_conn.cpp | 18 +++++++++++++---- trunk/src/core/srs_core_performance.hpp | 1 + trunk/src/core/srs_core_version4.hpp | 2 +- 7 files changed, 51 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index faee9efea..32b378f22 100755 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ For previous versions, please read: ## V4 changes +* v4.0, 2020-04-04, For [#307][bug #307], refine RTC latency from 600ms to 200ms. 4.0.20 * v4.0, 2020-04-03, For [#307][bug #307], build SRTP with openssl to improve performance. 4.0.19 * v4.0, 2020-03-31, For [#1500][bug #1500], support push stream by GB28181. 4.0.18 * v4.0, 2020-03-31, Play stream by WebRTC on iOS/Android/PC browser. 4.0.17 diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index f8dc90c96..454df82ab 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -421,6 +421,14 @@ vhost rtc.vhost.srs.com { # default: transcode aac transcode; } + # whether enable min delay mode for vhost. + # For RTC, we recommend to set to on. + min_latency on; + play { + # set the MW(merged-write) latency in ms. + # For RTC, we recommend lower value, such as 0. + mw_latency 0; + } } ############################################################################################# @@ -444,7 +452,8 @@ vhost scope.vhost.srs.com { # 1. disable the publish.mr for vhost. # 2. use timeout for cond wait for consumer queue. # @see https://github.com/ossrs/srs/issues/257 - # default: off + # default: off (for RTMP/HTTP-FLV) + # default: on (for WebRTC) min_latency off; # whether enable the TCP_NODELAY @@ -645,7 +654,8 @@ vhost play.srs.com { # SRS always set mw on, so we just set the latency value. # the latency of stream >= mw_latency + mr_latency # the value recomment is [300, 1800] - # default: 350 + # default: 350 (for RTMP/HTTP-FLV) + # default: 0 (for WebRTC) mw_latency 350; # the minimal packets send interval in ms, diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 5a579c83b..47bfe7d85 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -5126,9 +5126,12 @@ srs_utime_t SrsConfig::get_mr_sleep(string vhost) return (srs_utime_t)(::atoi(conf->arg0().c_str()) * SRS_UTIME_MILLISECONDS); } -srs_utime_t SrsConfig::get_mw_sleep(string vhost) +srs_utime_t SrsConfig::get_mw_sleep(string vhost, bool is_rtc) { - static srs_utime_t DEFAULT = SRS_PERF_MW_SLEEP; + static srs_utime_t SYS_DEFAULT = SRS_PERF_MW_SLEEP; + static srs_utime_t RTC_DEFAULT = 0; + + srs_utime_t DEFAULT = is_rtc? RTC_DEFAULT : SYS_DEFAULT; SrsConfDirective* conf = get_vhost(vhost); if (!conf) { @@ -5148,19 +5151,28 @@ srs_utime_t SrsConfig::get_mw_sleep(string vhost) return (srs_utime_t)(::atoi(conf->arg0().c_str()) * SRS_UTIME_MILLISECONDS); } -bool SrsConfig::get_realtime_enabled(string vhost) +bool SrsConfig::get_realtime_enabled(string vhost, bool is_rtc) { + static bool SYS_DEFAULT = SRS_PERF_MIN_LATENCY_ENABLED; + static bool RTC_DEFAULT = true; + + bool DEFAULT = is_rtc? RTC_DEFAULT : SYS_DEFAULT; + SrsConfDirective* conf = get_vhost(vhost); if (!conf) { - return SRS_PERF_MIN_LATENCY_ENABLED; + return DEFAULT; } conf = conf->get("min_latency"); if (!conf || conf->arg0().empty()) { - return SRS_PERF_MIN_LATENCY_ENABLED; + return DEFAULT; + } + + if (is_rtc) { + return SRS_CONF_PERFER_TRUE(conf->arg0()); + } else { + return SRS_CONF_PERFER_FALSE(conf->arg0()); } - - return SRS_CONF_PERFER_FALSE(conf->arg0()); } bool SrsConfig::get_tcp_nodelay(string vhost) diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index f130b9a3b..ad6670de6 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -605,14 +605,14 @@ public: // @param vhost, the vhost to get the mr sleep time. // TODO: FIXME: add utest for mr config. virtual srs_utime_t get_mr_sleep(std::string vhost); - // Get the mw sleep time in srs_utime_t for vhost. + // Get the mw_latency, mw sleep time in srs_utime_t for vhost. // @param vhost, the vhost to get the mw sleep time. // TODO: FIXME: add utest for mw config. - virtual srs_utime_t get_mw_sleep(std::string vhost); + virtual srs_utime_t get_mw_sleep(std::string vhost, bool is_rtc = false); // Whether min latency mode enabled. // @param vhost, the vhost to get the min_latency. // TODO: FIXME: add utest for min_latency. - virtual bool get_realtime_enabled(std::string vhost); + virtual bool get_realtime_enabled(std::string vhost, bool is_rtc = false); // Whether enable tcp nodelay for all clients of vhost. virtual bool get_tcp_nodelay(std::string vhost); // The minimal send interval in srs_utime_t. diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index b213093a0..6b464f173 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -507,21 +507,31 @@ srs_error_t SrsRtcSenderThread::cycle() rtc_session->request.get_stream_url().c_str(), ::getpid(), source->source_id()); SrsConsumer* consumer = NULL; + SrsAutoFree(SrsConsumer, consumer); if ((err = source->create_consumer(NULL, consumer)) != srs_success) { return srs_error_wrap(err, "rtc create consumer, source url=%s", rtc_session->request.get_stream_url().c_str()); } - SrsAutoFree(SrsConsumer, consumer); + // TODO: FIXME: Support reload. + SrsRequest* req = &rtc_session->request; + bool realtime = _srs_config->get_realtime_enabled(req->vhost, true); + srs_utime_t mw_sleep = _srs_config->get_mw_sleep(req->vhost, true); + + SrsMessageArray msgs(SRS_PERF_MW_MSGS); while (true) { if ((err = trd->pull()) != srs_success) { return srs_error_wrap(err, "rtc sender thread"); } - SrsMessageArray msgs(SRS_PERF_MW_MSGS); - #ifdef SRS_PERF_QUEUE_COND_WAIT - consumer->wait(0, SRS_PERF_MW_SLEEP); + if (realtime) { + // for realtime, min required msgs is 0, send when got one+ msgs. + consumer->wait(0, mw_sleep); + } else { + // for no-realtime, got some msgs then send. + consumer->wait(SRS_PERF_MW_MIN_MSGS_FOR_RTC, mw_sleep); + } #endif int msg_count = 0; diff --git a/trunk/src/core/srs_core_performance.hpp b/trunk/src/core/srs_core_performance.hpp index bd962bbd4..e265c373b 100644 --- a/trunk/src/core/srs_core_performance.hpp +++ b/trunk/src/core/srs_core_performance.hpp @@ -124,6 +124,7 @@ #define SRS_PERF_QUEUE_COND_WAIT #ifdef SRS_PERF_QUEUE_COND_WAIT #define SRS_PERF_MW_MIN_MSGS 8 + #define SRS_PERF_MW_MIN_MSGS_FOR_RTC 0 #endif /** * the default value of vhost for diff --git a/trunk/src/core/srs_core_version4.hpp b/trunk/src/core/srs_core_version4.hpp index 52f8e2196..9cf915bee 100644 --- a/trunk/src/core/srs_core_version4.hpp +++ b/trunk/src/core/srs_core_version4.hpp @@ -24,6 +24,6 @@ #ifndef SRS_CORE_VERSION4_HPP #define SRS_CORE_VERSION4_HPP -#define SRS_VERSION4_REVISION 19 +#define SRS_VERSION4_REVISION 20 #endif