From 0c036e04350bed53651bfe6552032df29e845be9 Mon Sep 17 00:00:00 2001 From: "Alex.CR" Date: Sat, 29 Jan 2022 01:07:01 -0600 Subject: [PATCH] SRT: Reduce the SRT bug by limit the max times for retry. --- trunk/src/srt/srt_conn.cpp | 9 ++++++++- trunk/src/srt/srt_conn.hpp | 2 ++ trunk/src/srt/srt_handle.cpp | 13 +++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/trunk/src/srt/srt_conn.cpp b/trunk/src/srt/srt_conn.cpp index bbcf96244..dc2e97cac 100644 --- a/trunk/src/srt/srt_conn.cpp +++ b/trunk/src/srt/srt_conn.cpp @@ -122,7 +122,8 @@ bool get_streamid_info(const std::string& streamid, int& mode, std::string& url_ } srt_conn::srt_conn(SRTSOCKET conn_fd, const std::string& streamid):_conn_fd(conn_fd), - _streamid(streamid) { + _streamid(streamid), + write_fail_cnt_(0) { get_streamid_info(streamid, _mode, _url_subpath); _update_timestamp = now_ms(); @@ -195,7 +196,13 @@ int srt_conn::write(unsigned char* data, int len) { ret = srt_send(_conn_fd, (char*)data, len); if (ret <= 0) { srt_log_error("srt write error:%d, socket fd:%d", ret, _conn_fd); + write_fail_cnt_++; return ret; } + write_fail_cnt_ = 0; return ret; } + +int srt_conn::get_write_fail_count() { + return write_fail_cnt_; +} \ No newline at end of file diff --git a/trunk/src/srt/srt_conn.hpp b/trunk/src/srt/srt_conn.hpp index 110e45913..44fa9bd17 100644 --- a/trunk/src/srt/srt_conn.hpp +++ b/trunk/src/srt/srt_conn.hpp @@ -44,6 +44,7 @@ public: void update_timestamp(long long now_ts); long long get_last_ts(); + int get_write_fail_count(); private: SRTSOCKET _conn_fd; @@ -52,6 +53,7 @@ private: std::string _vhost; int _mode; long long _update_timestamp; + int write_fail_cnt_; }; typedef std::shared_ptr SRT_CONN_PTR; diff --git a/trunk/src/srt/srt_handle.cpp b/trunk/src/srt/srt_handle.cpp index dcb1a007c..20547d673 100644 --- a/trunk/src/srt/srt_handle.cpp +++ b/trunk/src/srt/srt_handle.cpp @@ -26,6 +26,7 @@ static long long MONITOR_TIMEOUT = 5000; const unsigned int DEF_DATA_SIZE = 188*7; const long long CHECK_ALIVE_INTERVAL = 5*1000; const long long CHECK_ALIVE_TIMEOUT = 5*1000; +static const int SRT_WRTIE_FAIL_MAX = 10; long long srt_now_ms = 0; @@ -216,6 +217,7 @@ void srt_handle::handle_push_data(SRT_SOCKSTATUS status, const std::string& subp srt_log_info("receive data size(%d) from pusher(%d) to pullers, count:%d", ret, conn_fd, streamid_iter->second.size()); + std::vector remove_vec; for (auto puller_iter = streamid_iter->second.begin(); puller_iter != streamid_iter->second.end(); puller_iter++) { @@ -228,6 +230,17 @@ void srt_handle::handle_push_data(SRT_SOCKSTATUS status, const std::string& subp srt_log_info("send data size(%d) to puller fd:%d", write_ret, puller_iter->first); if (write_ret > 0) { puller_iter->second->update_timestamp(srt_now_ms); + } else { + if (player_conn->get_write_fail_count() > SRT_WRTIE_FAIL_MAX) { + remove_vec.push_back(puller_iter->first); + } + } + } + + for (auto item : remove_vec) { + streamid_iter->second.erase(item); + if (streamid_iter->second.empty()) { + _streamid_map.erase(streamid_iter); } }