From b77515d21710108b5d1f4406db51d8380ee0ba8a Mon Sep 17 00:00:00 2001 From: voidon Date: Sat, 6 Jan 2024 16:15:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhls=E6=8C=89=E9=9C=80?= =?UTF-8?q?=E6=8B=89=E6=B5=81=E4=B8=80=E6=AC=A1=E6=92=AD=E6=94=BE=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=20(#3153?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/HlsMediaSource.cpp | 4 ++++ src/Record/HlsMediaSource.h | 7 +++++++ src/Record/HlsRecorder.h | 2 ++ 3 files changed, 13 insertions(+) diff --git a/src/Record/HlsMediaSource.cpp b/src/Record/HlsMediaSource.cpp index 382e2779..c7cfe5db 100644 --- a/src/Record/HlsMediaSource.cpp +++ b/src/Record/HlsMediaSource.cpp @@ -87,7 +87,11 @@ void HlsMediaSource::setIndexFile(std::string index_file) strongSelf->onReaderChanged(size); }; _ring = std::make_shared(0, std::move(lam)); + } + if (!_registered && !_clear_cache) { + //在清空缓存期间不进行注册,否则可能播放中的ts文件会被清空 regist(); + _registered = true; } //赋值m3u8索引文件内容 diff --git a/src/Record/HlsMediaSource.h b/src/Record/HlsMediaSource.h index f9c4c56d..511be4cf 100644 --- a/src/Record/HlsMediaSource.h +++ b/src/Record/HlsMediaSource.h @@ -62,9 +62,16 @@ public: _ring->getInfoList(cb, on_change); } + /** + * 设置清空缓存的标志位 + */ + void setClearCache(bool clear_cache) { _clear_cache = clear_cache; }; + private: RingType::Ptr _ring; std::string _index_file; + bool _clear_cache = false; + bool _registered = false; mutable std::mutex _mtx_index; toolkit::List> _list_cb; }; diff --git a/src/Record/HlsRecorder.h b/src/Record/HlsRecorder.h index f6e84bfb..d43b8520 100644 --- a/src/Record/HlsRecorder.h +++ b/src/Record/HlsRecorder.h @@ -49,6 +49,7 @@ public: _enabled = _option.hls_demand ? (_hls->isLive() ? size : true) : true; if (!size && _hls->isLive() && _option.hls_demand) { // hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃 + _hls->getMediaSource()->setClearCache(true); _clear_cache = true; } MediaSourceEventInterceptor::onReaderChanged(sender, size); @@ -59,6 +60,7 @@ public: _clear_cache = false; //清空旧的m3u8索引文件于ts切片 _hls->clearCache(); + _hls->getMediaSource()->setClearCache(false); _hls->getMediaSource()->setIndexFile(""); } if (_enabled || !_option.hls_demand) {