From 0d2e0352152c4d004272f991a6f96fef83fb98dc Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Wed, 19 Oct 2022 14:20:53 +0800 Subject: [PATCH 1/2] rtsp and rtp h264 lowlatency mode config --- conf/config.ini | 5 ++++- src/Common/MediaSource.h | 6 ++++++ src/Common/config.cpp | 6 ++++++ src/Common/config.h | 5 +++++ src/Extension/H264Rtp.cpp | 16 ++++++++++++---- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index a60fa4cc..1fcfcdd2 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -264,6 +264,8 @@ audioMtuSize=600 videoMtuSize=1400 #rtp包最大长度限制,单位KB,主要用于识别TCP上下文破坏时,获取到错误的rtp rtpMaxSize=10 +# rtp 打包时,低延迟开关,默认关闭(为0),h264存在一帧多个slice(NAL)的情况,在这种情况下,如果开启可能会导致画面花屏 +lowLatency=0 [rtp_proxy] #导出调试数据(包括rtp/ps/h264)至该目录,置空则关闭数据导出 @@ -352,7 +354,8 @@ keepAliveSecond=15 port=554 #rtsps服务器监听地址 sslport=0 - +#rtsp 转发是否使用低延迟模式,当开启时,不会缓存rtp包,来提高并发,可以降低一帧的延迟 +lowLatency=0 [shell] #调试telnet服务器接受最大bufffer大小 maxReqSize=1024 diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 8fbff060..9266a936 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -398,6 +398,12 @@ private: //但是却对性能提升很大,这样做还是比较划算的 GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS); + + GET_CONFIG(int, rtspLowLatency, Rtsp::KLowLatency); + if(std::is_same::value && rtspLowLatency){ + return true; + } + return std::is_same::value ? false : (mergeWriteMS <= 0); } diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 4520f798..97174e75 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -173,6 +173,7 @@ const string kAuthBasic = RTSP_FIELD "authBasic"; const string kHandshakeSecond = RTSP_FIELD "handshakeSecond"; const string kKeepAliveSecond = RTSP_FIELD "keepAliveSecond"; const string kDirectProxy = RTSP_FIELD "directProxy"; +const string KLowLatency = RTSP_FIELD"lowLatency"; static onceToken token([]() { // 默认Md5方式认证 @@ -180,6 +181,7 @@ static onceToken token([]() { mINI::Instance()[kHandshakeSecond] = 15; mINI::Instance()[kKeepAliveSecond] = 15; mINI::Instance()[kDirectProxy] = 1; + mINI::Instance()[KLowLatency] = 0; }); } // namespace Rtsp @@ -206,10 +208,14 @@ const string kAudioMtuSize = RTP_FIELD "audioMtuSize"; // rtp包最大长度限制,单位是KB const string kRtpMaxSize = RTP_FIELD "rtpMaxSize"; +const string KLowLatency = RTP_FIELD "lowLatency"; + static onceToken token([]() { mINI::Instance()[kVideoMtuSize] = 1400; mINI::Instance()[kAudioMtuSize] = 600; mINI::Instance()[kRtpMaxSize] = 10; + mINI::Instance()[KLowLatency] = 0; + }); } // namespace Rtp diff --git a/src/Common/config.h b/src/Common/config.h index 65176b5e..7b64e02e 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -251,6 +251,9 @@ extern const std::string kKeepAliveSecond; // 假定您的拉流源地址不是264或265或AAC,那么你可以使用直接代理的方式来支持rtsp代理 // 默认开启rtsp直接代理,rtmp由于没有这些问题,是强制开启直接代理的 extern const std::string kDirectProxy; + +// rtsp 转发是否使用低延迟模式,当开启时,不会缓存rtp包,来提高并发,可以降低一帧的延迟 +extern const std::string KLowLatency; } // namespace Rtsp ////////////RTMP服务器配置/////////// @@ -271,6 +274,8 @@ extern const std::string kVideoMtuSize; extern const std::string kAudioMtuSize; // rtp包最大长度限制, 单位KB extern const std::string kRtpMaxSize; +// rtp 打包时,低延迟开关,默认关闭(为0),h264存在一帧多个slice(NAL)的情况,在这种情况下,如果开启可能会导致画面花屏 +extern const std::string KLowLatency; } // namespace Rtp ////////////组播配置/////////// diff --git a/src/Extension/H264Rtp.cpp b/src/Extension/H264Rtp.cpp index a5331e26..f5187658 100644 --- a/src/Extension/H264Rtp.cpp +++ b/src/Extension/H264Rtp.cpp @@ -283,11 +283,19 @@ bool H264RtpEncoder::inputFrame(const Frame::Ptr &frame) { default: break; } - if (_last_frame) { - //如果时间戳发生了变化,那么markbit才置true - inputFrame_l(_last_frame, _last_frame->pts() != frame->pts()); + GET_CONFIG(int,lowLatency,Rtp::KLowLatency); + if (lowLatency) { // 低延迟模式 + if (_last_frame) { + flush(); + } + inputFrame_l(frame, true); + } else { + if (_last_frame) { + //如果时间戳发生了变化,那么markbit才置true + inputFrame_l(_last_frame, _last_frame->pts() != frame->pts()); + } + _last_frame = Frame::getCacheAbleFrame(frame); } - _last_frame = Frame::getCacheAbleFrame(frame); return true; } From 0a71e06d4abd4f0b27e348b3ceb4a5658a021569 Mon Sep 17 00:00:00 2001 From: xiongguangjie Date: Wed, 19 Oct 2022 14:55:39 +0800 Subject: [PATCH 2/2] united some code style --- src/Common/MediaSource.h | 2 +- src/Common/config.cpp | 8 ++++---- src/Common/config.h | 4 ++-- src/Extension/H264Rtp.cpp | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 9266a936..fe61d685 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -399,7 +399,7 @@ private: GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS); - GET_CONFIG(int, rtspLowLatency, Rtsp::KLowLatency); + GET_CONFIG(int, rtspLowLatency, Rtsp::kLowLatency); if(std::is_same::value && rtspLowLatency){ return true; } diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 926dd9cc..9f5976e2 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -173,7 +173,7 @@ const string kAuthBasic = RTSP_FIELD "authBasic"; const string kHandshakeSecond = RTSP_FIELD "handshakeSecond"; const string kKeepAliveSecond = RTSP_FIELD "keepAliveSecond"; const string kDirectProxy = RTSP_FIELD "directProxy"; -const string KLowLatency = RTSP_FIELD"lowLatency"; +const string kLowLatency = RTSP_FIELD"lowLatency"; static onceToken token([]() { // 默认Md5方式认证 @@ -181,7 +181,7 @@ static onceToken token([]() { mINI::Instance()[kHandshakeSecond] = 15; mINI::Instance()[kKeepAliveSecond] = 15; mINI::Instance()[kDirectProxy] = 1; - mINI::Instance()[KLowLatency] = 0; + mINI::Instance()[kLowLatency] = 0; }); } // namespace Rtsp @@ -208,13 +208,13 @@ const string kAudioMtuSize = RTP_FIELD "audioMtuSize"; // rtp包最大长度限制,单位是KB const string kRtpMaxSize = RTP_FIELD "rtpMaxSize"; -const string KLowLatency = RTP_FIELD "lowLatency"; +const string kLowLatency = RTP_FIELD "lowLatency"; static onceToken token([]() { mINI::Instance()[kVideoMtuSize] = 1400; mINI::Instance()[kAudioMtuSize] = 600; mINI::Instance()[kRtpMaxSize] = 10; - mINI::Instance()[KLowLatency] = 0; + mINI::Instance()[kLowLatency] = 0; }); } // namespace Rtp diff --git a/src/Common/config.h b/src/Common/config.h index 7b64e02e..312878b6 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -253,7 +253,7 @@ extern const std::string kKeepAliveSecond; extern const std::string kDirectProxy; // rtsp 转发是否使用低延迟模式,当开启时,不会缓存rtp包,来提高并发,可以降低一帧的延迟 -extern const std::string KLowLatency; +extern const std::string kLowLatency; } // namespace Rtsp ////////////RTMP服务器配置/////////// @@ -275,7 +275,7 @@ extern const std::string kAudioMtuSize; // rtp包最大长度限制, 单位KB extern const std::string kRtpMaxSize; // rtp 打包时,低延迟开关,默认关闭(为0),h264存在一帧多个slice(NAL)的情况,在这种情况下,如果开启可能会导致画面花屏 -extern const std::string KLowLatency; +extern const std::string kLowLatency; } // namespace Rtp ////////////组播配置/////////// diff --git a/src/Extension/H264Rtp.cpp b/src/Extension/H264Rtp.cpp index f5187658..1130102f 100644 --- a/src/Extension/H264Rtp.cpp +++ b/src/Extension/H264Rtp.cpp @@ -283,7 +283,7 @@ bool H264RtpEncoder::inputFrame(const Frame::Ptr &frame) { default: break; } - GET_CONFIG(int,lowLatency,Rtp::KLowLatency); + GET_CONFIG(int,lowLatency,Rtp::kLowLatency); if (lowLatency) { // 低延迟模式 if (_last_frame) { flush();