diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 7c0006db..ead00000 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -219,6 +219,10 @@ extern std::vector getBlockTypeSize(); extern uint64_t getTotalMemBlockByType(int type); extern uint64_t getThisThreadMemBlockByType(int type) ; +namespace mediakit { +extern ThreadPool &getMP4Thread(); +extern ThreadPool &getHlsThread(); +} static inline void addHttpListener(){ GET_CONFIG(bool, api_debug, API::kApiDebug); //注册监听kBroadcastHttpRequest事件 @@ -403,7 +407,7 @@ void getStatisticJson(const function &cb) { 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 > thread_mem_info = std::make_shared >(thread_size); shared_ptr finished(nullptr, [thread_mem_info, cb, obj](void *) { @@ -441,6 +445,8 @@ void getStatisticJson(const function &cb) { }; EventPollerPool::Instance().for_each(lam1); WorkThreadPool::Instance().for_each(lam1); + lam0(getMP4Thread()); + lam0(getHlsThread()); #else cb(*obj); #endif diff --git a/src/Record/HlsRecorder.cpp b/src/Record/HlsRecorder.cpp new file mode 100644 index 00000000..76d8ca3b --- /dev/null +++ b/src/Record/HlsRecorder.cpp @@ -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 diff --git a/src/Record/HlsRecorder.h b/src/Record/HlsRecorder.h index 4be987a4..d0416f5e 100644 --- a/src/Record/HlsRecorder.h +++ b/src/Record/HlsRecorder.h @@ -63,7 +63,9 @@ public: 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); if (_clear_cache && hls_demand) { _clear_cache = false; diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index 5ae7d807..72973ceb 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -20,6 +20,16 @@ using namespace toolkit; 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) { _folder_path = path; /////record 业务逻辑////// @@ -67,7 +77,7 @@ void MP4Recorder::asyncClose() { auto full_path_tmp = _full_path_tmp; auto full_path = _full_path; 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执行时间 info.time_len = (float) (::time(NULL) - info.start_time); //关闭mp4非常耗时,所以要放在后台线程执行 @@ -112,7 +122,12 @@ bool MP4Recorder::inputFrame(const Frame::Ptr &frame) { if (_muxer) { //生成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; }