Commit e37982ad by 夏楚 Committed by GitHub

优化hls媒体流注册与无人观看清理机制 (#1421)

* 优化hls媒体流注册与无人观看清理机制

* 优化hls cookie管理

* bug fixed
parent 2fbd8de7
...@@ -117,7 +117,7 @@ void HlsMakerImp::onWriteHls(const std::string &data) { ...@@ -117,7 +117,7 @@ void HlsMakerImp::onWriteHls(const std::string &data) {
fwrite(data.data(), data.size(), 1, hls.get()); fwrite(data.data(), data.size(), 1, hls.get());
hls.reset(); hls.reset();
if (_media_src) { if (_media_src) {
_media_src->registHls(data); _media_src->setIndexFile(data);
} }
} else { } else {
WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg(); WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg();
......
...@@ -23,7 +23,7 @@ HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockIn ...@@ -23,7 +23,7 @@ HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockIn
void HlsCookieData::addReaderCount() { void HlsCookieData::addReaderCount() {
if (!*_added) { if (!*_added) {
auto src = std::dynamic_pointer_cast<HlsMediaSource>(MediaSource::find(HLS_SCHEMA, _info._vhost, _info._app, _info._streamid)); auto src = getMediaSource();
if (src) { if (src) {
*_added = true; *_added = true;
_ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller()); _ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller());
......
...@@ -39,12 +39,10 @@ public: ...@@ -39,12 +39,10 @@ public:
int readerCount() override { return _ring ? _ring->readerCount() : 0; } int readerCount() override { return _ring ? _ring->readerCount() : 0; }
/** /**
* 生成m3u8文件时触发 * 设置或清空m3u8索引文件内容
* @param index_file m3u8文件内容
*/ */
void registHls(std::string index_file) { void setIndexFile(std::string index_file) {
if (!_is_regist) { if (!_ring) {
_is_regist = true;
std::weak_ptr<HlsMediaSource> weakSelf = std::dynamic_pointer_cast<HlsMediaSource>(shared_from_this()); std::weak_ptr<HlsMediaSource> weakSelf = std::dynamic_pointer_cast<HlsMediaSource>(shared_from_this());
auto lam = [weakSelf](int size) { auto lam = [weakSelf](int size) {
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
...@@ -58,19 +56,19 @@ public: ...@@ -58,19 +56,19 @@ public:
regist(); regist();
} }
if (index_file.empty()) {
//没产生索引文件, 只是为了触发媒体注册
return;
}
//赋值m3u8索引文件内容 //赋值m3u8索引文件内容
std::lock_guard<std::mutex> lck(_mtx_index); std::lock_guard<std::mutex> lck(_mtx_index);
_index_file = std::move(index_file); _index_file = std::move(index_file);
_list_cb.for_each([&](const std::function<void(const std::string &str)> &cb) { cb(_index_file); }); if (!_index_file.empty()) {
_list_cb.clear(); _list_cb.for_each([&](const std::function<void(const std::string &str)> &cb) { cb(_index_file); });
_list_cb.clear();
}
} }
/**
* 异步获取m3u8文件
*/
void getIndexFile(std::function<void(const std::string &str)> cb) { void getIndexFile(std::function<void(const std::string &str)> cb) {
std::lock_guard<std::mutex> lck(_mtx_index); std::lock_guard<std::mutex> lck(_mtx_index);
if (!_index_file.empty()) { if (!_index_file.empty()) {
...@@ -81,6 +79,9 @@ public: ...@@ -81,6 +79,9 @@ public:
_list_cb.emplace_back(std::move(cb)); _list_cb.emplace_back(std::move(cb));
} }
/**
* 同步获取m3u8文件
*/
std::string getIndexFile() const { std::string getIndexFile() const {
std::lock_guard<std::mutex> lck(_mtx_index); std::lock_guard<std::mutex> lck(_mtx_index);
return _index_file; return _index_file;
...@@ -89,12 +90,10 @@ public: ...@@ -89,12 +90,10 @@ public:
void onSegmentSize(size_t bytes) { _speed[TrackVideo] += bytes; } void onSegmentSize(size_t bytes) { _speed[TrackVideo] += bytes; }
private: private:
bool _is_regist = false;
RingType::Ptr _ring; RingType::Ptr _ring;
std::string _index_file; std::string _index_file;
mutable std::mutex _mtx_index; mutable std::mutex _mtx_index;
toolkit::List<std::function<void(const std::string &)> > _list_cb; toolkit::List<std::function<void(const std::string &)>> _list_cb;
}; };
class HlsCookieData { class HlsCookieData {
......
...@@ -38,36 +38,28 @@ public: ...@@ -38,36 +38,28 @@ public:
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) { void setListener(const std::weak_ptr<MediaSourceEvent> &listener) {
setDelegate(listener); setDelegate(listener);
_hls->getMediaSource()->setListener(shared_from_this()); _hls->getMediaSource()->setListener(shared_from_this());
//先注册媒体流,后续可以按需生成
_hls->getMediaSource()->registHls("");
} }
int readerCount() { int readerCount() { return _hls->getMediaSource()->readerCount(); }
return _hls->getMediaSource()->readerCount();
}
void onReaderChanged(MediaSource &sender, int size) override { void onReaderChanged(MediaSource &sender, int size) override {
GET_CONFIG(bool, hls_demand, General::kHlsDemand); GET_CONFIG(bool, hls_demand, General::kHlsDemand);
//hls保留切片个数为0时代表为hls录制(不删除切片),那么不管有无观看者都一直生成hls // hls保留切片个数为0时代表为hls录制(不删除切片),那么不管有无观看者都一直生成hls
_enabled = hls_demand ? (_hls->isLive() ? size : true) : true; _enabled = hls_demand ? (_hls->isLive() ? size : true) : true;
if (!size && _hls->isLive() && hls_demand) { if (!size && _hls->isLive() && hls_demand) {
//hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃 // hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃
_clear_cache = true; _clear_cache = true;
} }
MediaSourceEventInterceptor::onReaderChanged(sender, size); MediaSourceEventInterceptor::onReaderChanged(sender, size);
} }
bool isEnabled() {
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
//缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
return hls_demand ? (_clear_cache ? true : _enabled) : true;
}
bool inputFrame(const Frame::Ptr &frame) override { bool inputFrame(const Frame::Ptr &frame) override {
GET_CONFIG(bool, hls_demand, General::kHlsDemand); GET_CONFIG(bool, hls_demand, General::kHlsDemand);
if (_clear_cache && hls_demand) { if (_clear_cache && hls_demand) {
_clear_cache = false; _clear_cache = false;
//清空旧的m3u8索引文件于ts切片
_hls->clearCache(); _hls->clearCache();
_hls->getMediaSource()->setIndexFile("");
} }
if (_enabled || !hls_demand) { if (_enabled || !hls_demand) {
return MpegMuxer::inputFrame(frame); return MpegMuxer::inputFrame(frame);
...@@ -75,6 +67,12 @@ public: ...@@ -75,6 +67,12 @@ public:
return false; return false;
} }
bool isEnabled() {
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
//缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
return hls_demand ? (_clear_cache ? true : _enabled) : true;
}
private: private:
void onWrite(std::shared_ptr<toolkit::Buffer> buffer, uint32_t timestamp, bool key_pos) override { void onWrite(std::shared_ptr<toolkit::Buffer> buffer, uint32_t timestamp, bool key_pos) override {
if (!buffer) { if (!buffer) {
...@@ -85,8 +83,7 @@ private: ...@@ -85,8 +83,7 @@ private:
} }
private: private:
//默认不生成hls文件,有播放器时再生成 bool _enabled = true;
bool _enabled = false;
bool _clear_cache = false; bool _clear_cache = false;
std::shared_ptr<HlsMakerImp> _hls; std::shared_ptr<HlsMakerImp> _hls;
}; };
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论