From efc3e78ba03ae5b93ea905c211af9139304a4e08 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Tue, 5 Apr 2022 19:32:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8emplace=5Fback=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81=20(#1542)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 少了一次构造调用,为什么不用呢? --- src/Record/HlsMaker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Record/HlsMaker.cpp b/src/Record/HlsMaker.cpp index be1c9f01..091667c0 100644 --- a/src/Record/HlsMaker.cpp +++ b/src/Record/HlsMaker.cpp @@ -137,7 +137,7 @@ void HlsMaker::flushLastSegment(bool eof){ if (seg_dur <= 0) { seg_dur = 100; } - _seg_dur_list.push_back(std::make_tuple(seg_dur, std::move(_last_file_name))); + _seg_dur_list.emplace_back(seg_dur, std::move(_last_file_name)); delOldSegment(); //先flush ts切片,否则可能存在ts文件未写入完毕就被访问的情况 onFlushLastSegment(seg_dur); From e4262222f44be871d68e8c53861af21c593721c8 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Tue, 5 Apr 2022 19:49:22 +0800 Subject: [PATCH 2/3] =?UTF-8?q?hls=E6=8B=89=E6=B5=81=E6=97=B6=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E9=80=82=E5=BD=93=E9=87=8D=E8=AF=95=20=20(#1541)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * hls拉流时应该适当重试 hls拉取索引文件失败时, 不应该直接上报,而应该内部适当重试几次. 避免由于网络抖动造成的客户端播放不流畅. * Update HlsPlayer.cpp --- src/Http/HlsPlayer.cpp | 17 ++++++++++++++++- src/Http/HlsPlayer.h | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Http/HlsPlayer.cpp b/src/Http/HlsPlayer.cpp index 42e0d9d7..352bd7f2 100644 --- a/src/Http/HlsPlayer.cpp +++ b/src/Http/HlsPlayer.cpp @@ -42,7 +42,22 @@ void HlsPlayer::teardown_l(const SockException &ex) { _play_result = true; onPlayResult(ex); } else { - onShutdown(ex); + //如果不是主动关闭的,则重新拉取索引文件 + if (ex.getErrCode() != Err_shutdown) { + // 当切片列表已空, 且没有正在下载的切片并且重试次数已经达到最大次数时, 则认为失败关闭播放器 + if (_ts_list.empty() && !(_http_ts_player && _http_ts_player->waitResponse()) + && _try_fetch_index_times >= MAX_TRY_FETCH_INDEX_TIMES) { + onShutdown(ex); + } else { + _try_fetch_index_times += 1; + shutdown(ex); + WarnL << "重新尝试拉取索引文件[" << _try_fetch_index_times << "]:" << _play_url; + fetchIndexFile(); + return; + } + } else { + onShutdown(ex); + } } _timer.reset(); _timer_ts.reset(); diff --git a/src/Http/HlsPlayer.h b/src/Http/HlsPlayer.h index 4ffb143c..75610dcb 100644 --- a/src/Http/HlsPlayer.h +++ b/src/Http/HlsPlayer.h @@ -19,6 +19,7 @@ #define MIN_TIMEOUT_MULTIPLE 2 #define MAX_TIMEOUT_MULTIPLE 5 +#define MAX_TRY_FETCH_INDEX_TIMES 5 namespace mediakit { @@ -105,6 +106,7 @@ private: std::set _ts_url_cache; HttpTSPlayer::Ptr _http_ts_player; int _timeout_multiple = MIN_TIMEOUT_MULTIPLE; + int _try_fetch_index_times = 0; }; class HlsPlayerImp : public PlayerImp, private TrackListener { From 75b17b3a6af5d5c8529ef2b6e5d111c66fed1e64 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Wed, 6 Apr 2022 14:16:48 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=81=BF=E5=85=8Dm3u8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=BAchunked=E6=97=B6=E5=B4=A9=E6=BA=83=20(#1543)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update HttpChunkedSplitter.h * 避免m3u8文件为chunked时崩溃 具体问题见 https://github.com/ZLMediaKit/ZLMediaKit/issues/1407 当数据最后小于2个字节时, 应该放弃回调. 做个保险, 避免导致溢出后崩溃. 这个bug很难出现, 但是的确存在. 一些特殊的服务器采用chunked返回的m3u8文件解析时, 有可能会遇到. * Update HttpChunkedSplitter.cpp Co-authored-by: 夏楚 <771730766@qq.com> --- src/Http/HttpChunkedSplitter.cpp | 21 ++++++++++++--------- src/Http/HttpChunkedSplitter.h | 8 +++----- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Http/HttpChunkedSplitter.cpp b/src/Http/HttpChunkedSplitter.cpp index 3083a67f..2b8b3d84 100644 --- a/src/Http/HttpChunkedSplitter.cpp +++ b/src/Http/HttpChunkedSplitter.cpp @@ -9,29 +9,32 @@ */ #include +#include "Common/macros.h" #include "HttpChunkedSplitter.h" using namespace std; +//[chunk size][\r\n][chunk data][\r\n][chunk size][\r\n][chunk data][\r\n][chunk size = 0][\r\n][\r\n] + namespace mediakit{ - + const char *HttpChunkedSplitter::onSearchPacketTail(const char *data, size_t len) { - auto pos = strstr(data,"\r\n"); - if(!pos){ + auto pos = strstr(data, "\r\n"); + if (!pos) { return nullptr; } return pos + 2; } void HttpChunkedSplitter::onRecvContent(const char *data, size_t len) { - onRecvChunk(data,len - 2); + onRecvChunk(data, len - 2); } ssize_t HttpChunkedSplitter::onRecvHeader(const char *data, size_t len) { - string str(data,len - 2); - int ret; - sscanf(str.data(),"%X",&ret); - return ret + 2; + int size; + CHECK(sscanf(data, "%X", &size) == 1 && size >= 0); + //包括后面\r\n两个字节 + return size + 2; } -}//namespace mediakit \ No newline at end of file +}//namespace mediakit diff --git a/src/Http/HttpChunkedSplitter.h b/src/Http/HttpChunkedSplitter.h index ae16c58d..6d8561f1 100644 --- a/src/Http/HttpChunkedSplitter.h +++ b/src/Http/HttpChunkedSplitter.h @@ -21,12 +21,10 @@ public: /** * len == 0时代表结束 */ - typedef std::function onChunkData; + using onChunkData = std::function; - HttpChunkedSplitter(const onChunkData &cb){ - _onChunkData = cb; - }; - ~HttpChunkedSplitter() override {} ; + HttpChunkedSplitter(const onChunkData &cb) { _onChunkData = cb; }; + ~HttpChunkedSplitter() override { _onChunkData = nullptr; }; protected: ssize_t onRecvHeader(const char *data,size_t len) override;