mirror of
https://gitee.com/xia-chu/ZLMediaKit.git
synced 2026-05-23 10:07:50 +08:00
hls/mp4录制放置在后台线程
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
Linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Windows / build (push) Has been cancelled
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
Linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Windows / build (push) Has been cancelled
This commit is contained in:
parent
479a3fb9bb
commit
69e6362f48
@ -245,6 +245,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 void *web_api_tag = nullptr;
|
static void *web_api_tag = nullptr;
|
||||||
|
|
||||||
static inline void addHttpListener(){
|
static inline void addHttpListener(){
|
||||||
@ -587,7 +591,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 *) {
|
||||||
@ -626,6 +630,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
|
||||||
|
|||||||
26
src/Record/HlsRecorder.cpp
Normal file
26
src/Record/HlsRecorder.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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"
|
||||||
|
using namespace toolkit;
|
||||||
|
|
||||||
|
namespace mediakit {
|
||||||
|
|
||||||
|
ThreadPool &getHlsThread() {
|
||||||
|
static ThreadPool ret(1, ThreadPool::PRIORITY_LOWEST, true);
|
||||||
|
static onceToken s_token([]() {
|
||||||
|
ret.async([]() {
|
||||||
|
setThreadName("hls thread");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
}//namespace mediakit
|
||||||
@ -15,9 +15,12 @@
|
|||||||
#include "MPEG.h"
|
#include "MPEG.h"
|
||||||
#include "MP4Muxer.h"
|
#include "MP4Muxer.h"
|
||||||
#include "Common/config.h"
|
#include "Common/config.h"
|
||||||
|
#include "Thread/ThreadPool.h"
|
||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
|
toolkit::ThreadPool& getHlsThread();
|
||||||
|
|
||||||
template <typename Muxer>
|
template <typename Muxer>
|
||||||
class HlsRecorderBase : public MediaSourceEventInterceptor, public Muxer, public std::enable_shared_from_this<HlsRecorderBase<Muxer> > {
|
class HlsRecorderBase : public MediaSourceEventInterceptor, public Muxer, public std::enable_shared_from_this<HlsRecorderBase<Muxer> > {
|
||||||
public:
|
public:
|
||||||
@ -58,6 +61,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool inputFrame(const Frame::Ptr &frame) override {
|
bool inputFrame(const Frame::Ptr &frame) override {
|
||||||
|
auto ptr = this->shared_from_this();
|
||||||
|
auto cached_frame = Frame::getCacheAbleFrame(frame);
|
||||||
|
getHlsThread().async([ptr, cached_frame]() {
|
||||||
|
ptr->inputFrame_l(cached_frame);
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool inputFrame_l(const Frame::Ptr &frame) {
|
||||||
if (_clear_cache && _option.hls_demand) {
|
if (_clear_cache && _option.hls_demand) {
|
||||||
_clear_cache = false;
|
_clear_cache = false;
|
||||||
// 清空旧的m3u8索引文件于ts切片 [AUTO-TRANSLATED:a4ce0664]
|
// 清空旧的m3u8索引文件于ts切片 [AUTO-TRANSLATED:a4ce0664]
|
||||||
|
|||||||
@ -22,6 +22,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 MediaTuple &tuple, const string &path, size_t max_second) {
|
MP4Recorder::MP4Recorder(const MediaTuple &tuple, const string &path, size_t max_second) {
|
||||||
_folder_path = path;
|
_folder_path = path;
|
||||||
// ///record 业务逻辑////// [AUTO-TRANSLATED:2e78931a]
|
// ///record 业务逻辑////// [AUTO-TRANSLATED:2e78931a]
|
||||||
@ -78,7 +88,7 @@ void MP4Recorder::asyncClose() {
|
|||||||
auto full_path = _full_path;
|
auto full_path = _full_path;
|
||||||
auto info = _info;
|
auto info = _info;
|
||||||
TraceL << "Start close tmp mp4 file: " << full_path_tmp;
|
TraceL << "Start close tmp mp4 file: " << full_path_tmp;
|
||||||
WorkThreadPool::Instance().getExecutor()->async([muxer, full_path_tmp, full_path, info]() mutable {
|
getMP4Thread().async([muxer, full_path_tmp, full_path, info]() mutable {
|
||||||
info.time_len = muxer->getDuration() / 1000.0f;
|
info.time_len = muxer->getDuration() / 1000.0f;
|
||||||
// 关闭mp4可能非常耗时,所以要放在后台线程执行 [AUTO-TRANSLATED:a7378a11]
|
// 关闭mp4可能非常耗时,所以要放在后台线程执行 [AUTO-TRANSLATED:a7378a11]
|
||||||
// Closing mp4 can be very time-consuming, so it should be executed in the background thread
|
// Closing mp4 can be very time-consuming, so it should be executed in the background thread
|
||||||
@ -153,7 +163,12 @@ bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
|
|||||||
if (_muxer) {
|
if (_muxer) {
|
||||||
// 生成mp4文件 [AUTO-TRANSLATED:76a8d77c]
|
// 生成mp4文件 [AUTO-TRANSLATED:76a8d77c]
|
||||||
// Generate mp4 file
|
// Generate mp4 file
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user