录制接口支持指定切片时间大小:#747

This commit is contained in:
xiongziliang 2021-03-07 10:41:57 +08:00
parent daedbed737
commit c0f4899950
13 changed files with 54 additions and 40 deletions

View File

@ -63,9 +63,10 @@ API_EXPORT int API_CALL mk_recorder_is_recording(int type, const char *vhost, co
* @param app * @param app
* @param stream id * @param stream id
* @param customized_path null则自动生成 * @param customized_path null则自动生成
* @param max_second mp4录制最大切片时间0
* @return 10 * @return 10
*/ */
API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream, const char *customized_path); API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream, const char *customized_path, size_t max_second);
/** /**
* *

View File

@ -41,9 +41,9 @@ API_EXPORT int API_CALL mk_recorder_is_recording(int type, const char *vhost, co
return Recorder::isRecording((Recorder::type)type,vhost,app,stream); return Recorder::isRecording((Recorder::type)type,vhost,app,stream);
} }
API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream,const char *customized_path){ API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream,const char *customized_path, size_t max_second){
assert(vhost && app && stream); assert(vhost && app && stream);
return Recorder::startRecord((Recorder::type)type,vhost,app,stream,customized_path ? customized_path : ""); return Recorder::startRecord((Recorder::type)type,vhost,app,stream,customized_path ? customized_path : "", max_second);
} }
API_EXPORT int API_CALL mk_recorder_stop(int type, const char *vhost, const char *app, const char *stream){ API_EXPORT int API_CALL mk_recorder_stop(int type, const char *vhost, const char *app, const char *stream){

View File

@ -818,7 +818,14 @@
{ {
"key": "customized_path", "key": "customized_path",
"value": null, "value": null,
"disabled": true,
"description": "录像文件保存自定义根目录,为空则采用配置文件设置" "description": "录像文件保存自定义根目录,为空则采用配置文件设置"
},
{
"key": "max_second",
"value": "1000",
"disabled": true,
"description": "MP4录制的切片时间大小单位秒"
} }
] ]
} }

View File

@ -280,10 +280,10 @@ void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) {
setDelegate(listener); setDelegate(listener);
src->setListener(shared_from_this()); src->setListener(shared_from_this());
if (_enable_hls) { if (_enable_hls) {
src->setupRecord(Recorder::type_hls, true, ""); src->setupRecord(Recorder::type_hls, true, "", 0);
} }
if (_enable_mp4) { if (_enable_mp4) {
src->setupRecord(Recorder::type_mp4, true, ""); src->setupRecord(Recorder::type_mp4, true, "", 0);
} }
} }
} }

View File

@ -880,7 +880,8 @@ void installWebApi() {
allArgs["vhost"], allArgs["vhost"],
allArgs["app"], allArgs["app"],
allArgs["stream"], allArgs["stream"],
allArgs["customized_path"]); allArgs["customized_path"],
allArgs["max_second"].as<size_t>());
val["result"] = result; val["result"] = result;
val["code"] = result ? API::Success : API::OtherFailed; val["code"] = result ? API::Success : API::OtherFailed;
val["msg"] = result ? "success" : "start record failed"; val["msg"] = result ? "success" : "start record failed";

View File

@ -171,13 +171,13 @@ void MediaSource::onReaderChanged(int size) {
} }
} }
bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path){ bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second){
auto listener = _listener.lock(); auto listener = _listener.lock();
if (!listener) { if (!listener) {
WarnL << "未设置MediaSource的事件监听者setupRecord失败:" << getSchema() << "/" << getVhost() << "/" << getApp() << "/" << getId(); WarnL << "未设置MediaSource的事件监听者setupRecord失败:" << getSchema() << "/" << getVhost() << "/" << getApp() << "/" << getId();
return false; return false;
} }
return listener->setupRecord(*this, type, start, custom_path); return listener->setupRecord(*this, type, start, custom_path, max_second);
} }
bool MediaSource::isRecording(Recorder::type type){ bool MediaSource::isRecording(Recorder::type type){
@ -626,12 +626,12 @@ void MediaSourceEventInterceptor::onRegist(MediaSource &sender, bool regist) {
} }
} }
bool MediaSourceEventInterceptor::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) { bool MediaSourceEventInterceptor::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) {
auto listener = _listener.lock(); auto listener = _listener.lock();
if (!listener) { if (!listener) {
return false; return false;
} }
return listener->setupRecord(sender, type, start, custom_path); return listener->setupRecord(sender, type, start, custom_path, max_second);
} }
bool MediaSourceEventInterceptor::isRecording(MediaSource &sender, Recorder::type type) { bool MediaSourceEventInterceptor::isRecording(MediaSource &sender, Recorder::type type) {

View File

@ -77,7 +77,7 @@ public:
////////////////////////仅供MultiMediaSourceMuxer对象继承//////////////////////// ////////////////////////仅供MultiMediaSourceMuxer对象继承////////////////////////
// 开启或关闭录制 // 开启或关闭录制
virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) { return false; }; virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) { return false; };
// 获取录制状态 // 获取录制状态
virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; }; virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; };
// 获取所有track相关信息 // 获取所有track相关信息
@ -109,7 +109,7 @@ public:
int totalReaderCount(MediaSource &sender) override; int totalReaderCount(MediaSource &sender) override;
void onReaderChanged(MediaSource &sender, int size) override; void onReaderChanged(MediaSource &sender, int size) override;
void onRegist(MediaSource &sender, bool regist) override; void onRegist(MediaSource &sender, bool regist) override;
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) override; bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) override;
bool isRecording(MediaSource &sender, Recorder::type type) override; bool isRecording(MediaSource &sender, Recorder::type type) override;
vector<Track::Ptr> getTracks(MediaSource &sender, bool trackReady = true) const override; vector<Track::Ptr> getTracks(MediaSource &sender, bool trackReady = true) const override;
void startSendRtp(MediaSource &sender, const string &dst_url, uint16_t dst_port, const string &ssrc, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb) override; void startSendRtp(MediaSource &sender, const string &dst_url, uint16_t dst_port, const string &ssrc, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb) override;
@ -252,7 +252,7 @@ public:
// 该流观看人数变化 // 该流观看人数变化
void onReaderChanged(int size); void onReaderChanged(int size);
// 开启或关闭录制 // 开启或关闭录制
bool setupRecord(Recorder::type type, bool start, const string &custom_path); bool setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second);
// 获取录制状态 // 获取录制状态
bool isRecording(Recorder::type type); bool isRecording(Recorder::type type);
// 开始发送ps-rtp // 开始发送ps-rtp

View File

@ -107,8 +107,8 @@ int MultiMuxerPrivate::totalReaderCount() const {
(hls ? hls->readerCount() : 0); (hls ? hls->readerCount() : 0);
} }
static std::shared_ptr<MediaSinkInterface> makeRecorder(const vector<Track::Ptr> &tracks, Recorder::type type, const string &custom_path, MediaSource &sender){ static std::shared_ptr<MediaSinkInterface> makeRecorder(MediaSource &sender, const vector<Track::Ptr> &tracks, Recorder::type type, const string &custom_path, size_t max_second){
auto recorder = Recorder::createRecorder(type, sender.getVhost(), sender.getApp(), sender.getId(), custom_path); auto recorder = Recorder::createRecorder(type, sender.getVhost(), sender.getApp(), sender.getId(), custom_path, max_second);
for (auto &track : tracks) { for (auto &track : tracks) {
recorder->addTrack(track); recorder->addTrack(track);
} }
@ -116,12 +116,12 @@ static std::shared_ptr<MediaSinkInterface> makeRecorder(const vector<Track::Ptr>
} }
//此函数可能跨线程调用 //此函数可能跨线程调用
bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path){ bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second){
switch (type) { switch (type) {
case Recorder::type_hls : { case Recorder::type_hls : {
if (start && !_hls) { if (start && !_hls) {
//开始录制 //开始录制
auto hls = dynamic_pointer_cast<HlsRecorder>(makeRecorder(getTracks(true), type, custom_path, sender)); auto hls = dynamic_pointer_cast<HlsRecorder>(makeRecorder(sender, getTracks(true), type, custom_path, max_second));
if (hls) { if (hls) {
//设置HlsMediaSource的事件监听器 //设置HlsMediaSource的事件监听器
hls->setListener(_listener); hls->setListener(_listener);
@ -136,7 +136,7 @@ bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bo
case Recorder::type_mp4 : { case Recorder::type_mp4 : {
if (start && !_mp4) { if (start && !_mp4) {
//开始录制 //开始录制
_mp4 = makeRecorder(getTracks(true), type, custom_path, sender); _mp4 = makeRecorder(sender, getTracks(true), type, custom_path, max_second);
} else if (!start && _mp4) { } else if (!start && _mp4) {
//停止录制 //停止录制
_mp4 = nullptr; _mp4 = nullptr;
@ -326,8 +326,8 @@ int MultiMediaSourceMuxer::totalReaderCount(MediaSource &sender) {
return listener->totalReaderCount(sender); return listener->totalReaderCount(sender);
} }
bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) { bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) {
return _muxer->setupRecord(sender, type, start, custom_path); return _muxer->setupRecord(sender, type, start, custom_path, max_second);
} }
bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) { bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) {

View File

@ -44,7 +44,7 @@ private:
int totalReaderCount() const; int totalReaderCount() const;
void setTimeStamp(uint32_t stamp); void setTimeStamp(uint32_t stamp);
void setTrackListener(Listener *listener); void setTrackListener(Listener *listener);
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path); bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second);
bool isRecording(MediaSource &sender, Recorder::type type); bool isRecording(MediaSource &sender, Recorder::type type);
bool isEnabled(); bool isEnabled();
void onTrackReady(const Track::Ptr & track) override; void onTrackReady(const Track::Ptr & track) override;
@ -125,7 +125,7 @@ public:
* @param custom_path * @param custom_path
* @return * @return
*/ */
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) override; bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) override;
/** /**
* *

View File

@ -20,16 +20,19 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
MP4Recorder::MP4Recorder(const string& strPath, MP4Recorder::MP4Recorder(const string &strPath,
const string &strVhost, const string &strVhost,
const string &strApp, const string &strApp,
const string &strStreamId) { const string &strStreamId,
size_t max_second) {
_strPath = strPath; _strPath = strPath;
/////record 业务逻辑////// /////record 业务逻辑//////
_info.app = strApp; _info.app = strApp;
_info.stream = strStreamId; _info.stream = strStreamId;
_info.vhost = strVhost; _info.vhost = strVhost;
_info.folder = strPath; _info.folder = strPath;
GET_CONFIG(size_t ,recordSec,Record::kFileSecond);
_max_second = max_second ? max_second : recordSec;
} }
MP4Recorder::~MP4Recorder() { MP4Recorder::~MP4Recorder() {
closeFile(); closeFile();
@ -104,8 +107,7 @@ void MP4Recorder::closeFile() {
} }
void MP4Recorder::inputFrame(const Frame::Ptr &frame) { void MP4Recorder::inputFrame(const Frame::Ptr &frame) {
GET_CONFIG(uint32_t,recordSec,Record::kFileSecond); if(!_muxer || ((_createFileTicker.elapsedTime() > _max_second * 1000) &&
if(!_muxer || ((_createFileTicker.elapsedTime() > recordSec * 1000) &&
(!_haveVideo || (_haveVideo && frame->keyFrame()))) ){ (!_haveVideo || (_haveVideo && frame->keyFrame()))) ){
//成立条件 //成立条件
//1、_muxer为空 //1、_muxer为空

View File

@ -33,7 +33,8 @@ public:
MP4Recorder(const string &strPath, MP4Recorder(const string &strPath,
const string &strVhost, const string &strVhost,
const string &strApp, const string &strApp,
const string &strStreamId); const string &strStreamId,
size_t max_second);
virtual ~MP4Recorder(); virtual ~MP4Recorder();
/** /**
@ -55,12 +56,13 @@ private:
void closeFile(); void closeFile();
void asyncClose(); void asyncClose();
private: private:
bool _haveVideo = false;
size_t _max_second;
string _strPath; string _strPath;
string _strFile; string _strFile;
string _strFileTmp; string _strFileTmp;
Ticker _createFileTicker; Ticker _createFileTicker;
RecordInfo _info; RecordInfo _info;
bool _haveVideo = false;
MP4Muxer::Ptr _muxer; MP4Muxer::Ptr _muxer;
list<Track::Ptr> _tracks; list<Track::Ptr> _tracks;
}; };

View File

@ -55,7 +55,7 @@ string Recorder::getRecordPath(Recorder::type type, const string &vhost, const s
} }
} }
std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path){ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path, size_t max_second){
auto path = Recorder::getRecordPath(type, vhost, app, stream_id, customized_path); auto path = Recorder::getRecordPath(type, vhost, app, stream_id, customized_path);
switch (type) { switch (type) {
case Recorder::type_hls: { case Recorder::type_hls: {
@ -72,7 +72,7 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const st
case Recorder::type_mp4: { case Recorder::type_mp4: {
#if defined(ENABLE_MP4) #if defined(ENABLE_MP4)
return std::make_shared<MP4Recorder>(path, vhost, app, stream_id); return std::make_shared<MP4Recorder>(path, vhost, app, stream_id, max_second);
#else #else
throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试"); throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试");
#endif #endif
@ -90,13 +90,13 @@ bool Recorder::isRecording(type type, const string &vhost, const string &app, co
return src->isRecording(type); return src->isRecording(type);
} }
bool Recorder::startRecord(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path){ bool Recorder::startRecord(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path, size_t max_second){
auto src = MediaSource::find(vhost, app, stream_id); auto src = MediaSource::find(vhost, app, stream_id);
if (!src) { if (!src) {
WarnL << "未找到相关的MediaSource,startRecord失败:" << vhost << "/" << app << "/" << stream_id; WarnL << "未找到相关的MediaSource,startRecord失败:" << vhost << "/" << app << "/" << stream_id;
return false; return false;
} }
return src->setupRecord(type, true, customized_path); return src->setupRecord(type, true, customized_path, max_second);
} }
bool Recorder::stopRecord(type type, const string &vhost, const string &app, const string &stream_id){ bool Recorder::stopRecord(type type, const string &vhost, const string &app, const string &stream_id){
@ -104,7 +104,7 @@ bool Recorder::stopRecord(type type, const string &vhost, const string &app, con
if(!src){ if(!src){
return false; return false;
} }
return src->setupRecord(type, false, ""); return src->setupRecord(type, false, "", 0);
} }
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -57,9 +57,10 @@ public:
* @param app * @param app
* @param stream_id id * @param stream_id id
* @param customized_path * @param customized_path
* @param max_second mp4录制最大切片时间0
* @return nullptr * @return nullptr
*/ */
static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path = ""); static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path = "", size_t max_second = 0);
/** /**
* *
@ -80,7 +81,7 @@ public:
* @param customized_path * @param customized_path
* @return * @return
*/ */
static bool startRecord(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path); static bool startRecord(type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path, size_t max_second);
/** /**
* *