hls/mp4录制放置在后台线程

This commit is contained in:
ziyue 2021-12-30 20:52:38 +08:00
parent 84f3aa0748
commit 9875083114
4 changed files with 61 additions and 4 deletions

View File

@ -219,6 +219,10 @@ extern std::vector<size_t> getBlockTypeSize();
extern uint64_t getTotalMemBlockByType(int type); extern uint64_t getTotalMemBlockByType(int type);
extern uint64_t getThisThreadMemBlockByType(int type) ; extern uint64_t getThisThreadMemBlockByType(int type) ;
namespace mediakit {
extern ThreadPool &getMP4Thread();
extern ThreadPool &getHlsThread();
}
static inline void addHttpListener(){ static inline void addHttpListener(){
GET_CONFIG(bool, api_debug, API::kApiDebug); GET_CONFIG(bool, api_debug, API::kApiDebug);
//注册监听kBroadcastHttpRequest事件 //注册监听kBroadcastHttpRequest事件
@ -403,7 +407,7 @@ void getStatisticJson(const function<void(Value &val)> &cb) {
val["totalMemBlockTypeCount"] = str; val["totalMemBlockTypeCount"] = str;
} }
auto thread_size = EventPollerPool::Instance().getExecutorSize() + WorkThreadPool::Instance().getExecutorSize(); auto thread_size = 2 + EventPollerPool::Instance().getExecutorSize() + WorkThreadPool::Instance().getExecutorSize();
std::shared_ptr<vector<Value> > thread_mem_info = std::make_shared<vector<Value> >(thread_size); std::shared_ptr<vector<Value> > thread_mem_info = std::make_shared<vector<Value> >(thread_size);
shared_ptr<void> finished(nullptr, [thread_mem_info, cb, obj](void *) { shared_ptr<void> finished(nullptr, [thread_mem_info, cb, obj](void *) {
@ -441,6 +445,8 @@ void getStatisticJson(const function<void(Value &val)> &cb) {
}; };
EventPollerPool::Instance().for_each(lam1); EventPollerPool::Instance().for_each(lam1);
WorkThreadPool::Instance().for_each(lam1); WorkThreadPool::Instance().for_each(lam1);
lam0(getMP4Thread());
lam0(getHlsThread());
#else #else
cb(*obj); cb(*obj);
#endif #endif

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include "HlsRecorder.h"
namespace mediakit {
ThreadPool &getHlsThread() {
static ThreadPool ret(1, ThreadPool::PRIORITY_LOWEST, true);
static onceToken s_token([]() {
ret.async([]() {
setThreadName("hls thread");
});
});
return ret;
}
bool HlsRecorder::inputFrame(const Frame::Ptr &frame) {
auto ptr = shared_from_this();
auto cached_frame = Frame::getCacheAbleFrame(frame);
getHlsThread().async([ptr, cached_frame]() {
ptr->inputFrame_l(cached_frame);
});
return true;
}
}//namespace mediakit

View File

@ -63,7 +63,9 @@ public:
return hls_demand ? (_clear_cache ? true : _enabled) : true; return hls_demand ? (_clear_cache ? true : _enabled) : true;
} }
bool inputFrame(const Frame::Ptr &frame) override { bool inputFrame(const Frame::Ptr &frame) override;
bool inputFrame_l(const Frame::Ptr &frame) {
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;

View File

@ -20,6 +20,16 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
ThreadPool &getMP4Thread() {
static ThreadPool ret(1, ThreadPool::PRIORITY_LOWEST, true);
static onceToken s_token([]() {
ret.async([]() {
setThreadName("mp4 thread");
});
});
return ret;
}
MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second) { MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second) {
_folder_path = path; _folder_path = path;
/////record 业务逻辑////// /////record 业务逻辑//////
@ -67,7 +77,7 @@ void MP4Recorder::asyncClose() {
auto full_path_tmp = _full_path_tmp; auto full_path_tmp = _full_path_tmp;
auto full_path = _full_path; auto full_path = _full_path;
auto info = _info; auto info = _info;
WorkThreadPool::Instance().getExecutor()->async([muxer, full_path_tmp, full_path, info]() mutable { getMP4Thread().async([muxer, full_path_tmp, full_path, info]() mutable {
//获取文件录制时间放在关闭mp4之前是为了忽略关闭mp4执行时间 //获取文件录制时间放在关闭mp4之前是为了忽略关闭mp4执行时间
info.time_len = (float) (::time(NULL) - info.start_time); info.time_len = (float) (::time(NULL) - info.start_time);
//关闭mp4非常耗时所以要放在后台线程执行 //关闭mp4非常耗时所以要放在后台线程执行
@ -112,7 +122,12 @@ bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
if (_muxer) { if (_muxer) {
//生成mp4文件 //生成mp4文件
return _muxer->inputFrame(frame); auto muxer = _muxer;
auto cached_frame = Frame::getCacheAbleFrame(frame);
getMP4Thread().async([muxer, cached_frame]() {
return muxer->inputFrame(cached_frame);
});
return true;
} }
return false; return false;
} }