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) {