录制接口支持指定切片时间大小:#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 stream id
* @param customized_path null则自动生成
* @param max_second mp4录制最大切片时间0
* @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);
}
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);
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){

View File

@ -818,7 +818,14 @@
{
"key": "customized_path",
"value": null,
"disabled": true,
"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);
src->setListener(shared_from_this());
if (_enable_hls) {
src->setupRecord(Recorder::type_hls, true, "");
src->setupRecord(Recorder::type_hls, true, "", 0);
}
if (_enable_mp4) {
src->setupRecord(Recorder::type_mp4, true, "");
src->setupRecord(Recorder::type_mp4, true, "", 0);
}
}
}

View File

@ -877,10 +877,11 @@ void installWebApi() {
CHECK_SECRET();
CHECK_ARGS("type","vhost","app","stream");
auto result = Recorder::startRecord((Recorder::type) allArgs["type"].as<int>(),
allArgs["vhost"],
allArgs["app"],
allArgs["stream"],
allArgs["customized_path"]);
allArgs["vhost"],
allArgs["app"],
allArgs["stream"],
allArgs["customized_path"],
allArgs["max_second"].as<size_t>());
val["result"] = result;
val["code"] = result ? API::Success : API::OtherFailed;
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();
if (!listener) {
WarnL << "未设置MediaSource的事件监听者setupRecord失败:" << getSchema() << "/" << getVhost() << "/" << getApp() << "/" << getId();
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){
@ -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();
if (!listener) {
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) {

View File

@ -77,7 +77,7 @@ public:
////////////////////////仅供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; };
// 获取所有track相关信息
@ -109,7 +109,7 @@ public:
int totalReaderCount(MediaSource &sender) override;
void onReaderChanged(MediaSource &sender, int size) 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;
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;
@ -252,7 +252,7 @@ public:
// 该流观看人数变化
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);
// 开始发送ps-rtp

View File

@ -107,8 +107,8 @@ int MultiMuxerPrivate::totalReaderCount() const {
(hls ? hls->readerCount() : 0);
}
static std::shared_ptr<MediaSinkInterface> makeRecorder(const vector<Track::Ptr> &tracks, Recorder::type type, const string &custom_path, MediaSource &sender){
auto recorder = Recorder::createRecorder(type, sender.getVhost(), sender.getApp(), sender.getId(), custom_path);
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, max_second);
for (auto &track : tracks) {
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) {
case Recorder::type_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) {
//设置HlsMediaSource的事件监听器
hls->setListener(_listener);
@ -136,7 +136,7 @@ bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bo
case Recorder::type_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) {
//停止录制
_mp4 = nullptr;
@ -326,8 +326,8 @@ int MultiMediaSourceMuxer::totalReaderCount(MediaSource &sender) {
return listener->totalReaderCount(sender);
}
bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) {
return _muxer->setupRecord(sender, type, start, 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, max_second);
}
bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) {

View File

@ -44,7 +44,7 @@ private:
int totalReaderCount() const;
void setTimeStamp(uint32_t stamp);
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 isEnabled();
void onTrackReady(const Track::Ptr & track) override;
@ -125,7 +125,7 @@ public:
* @param custom_path
* @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 {
MP4Recorder::MP4Recorder(const string& strPath,
const string &strVhost,
const string &strApp,
const string &strStreamId) {
MP4Recorder::MP4Recorder(const string &strPath,
const string &strVhost,
const string &strApp,
const string &strStreamId,
size_t max_second) {
_strPath = strPath;
/////record 业务逻辑//////
_info.app = strApp;
_info.stream = strStreamId;
_info.vhost = strVhost;
_info.folder = strPath;
GET_CONFIG(size_t ,recordSec,Record::kFileSecond);
_max_second = max_second ? max_second : recordSec;
}
MP4Recorder::~MP4Recorder() {
closeFile();
@ -104,8 +107,7 @@ void MP4Recorder::closeFile() {
}
void MP4Recorder::inputFrame(const Frame::Ptr &frame) {
GET_CONFIG(uint32_t,recordSec,Record::kFileSecond);
if(!_muxer || ((_createFileTicker.elapsedTime() > recordSec * 1000) &&
if(!_muxer || ((_createFileTicker.elapsedTime() > _max_second * 1000) &&
(!_haveVideo || (_haveVideo && frame->keyFrame()))) ){
//成立条件
//1、_muxer为空

View File

@ -33,7 +33,8 @@ public:
MP4Recorder(const string &strPath,
const string &strVhost,
const string &strApp,
const string &strStreamId);
const string &strStreamId,
size_t max_second);
virtual ~MP4Recorder();
/**
@ -55,12 +56,13 @@ private:
void closeFile();
void asyncClose();
private:
bool _haveVideo = false;
size_t _max_second;
string _strPath;
string _strFile;
string _strFileTmp;
Ticker _createFileTicker;
RecordInfo _info;
bool _haveVideo = false;
MP4Muxer::Ptr _muxer;
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);
switch (type) {
case Recorder::type_hls: {
@ -72,7 +72,7 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const st
case Recorder::type_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
throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试");
#endif
@ -90,13 +90,13 @@ bool Recorder::isRecording(type type, const string &vhost, const string &app, co
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);
if (!src) {
WarnL << "未找到相关的MediaSource,startRecord失败:" << vhost << "/" << app << "/" << stream_id;
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){
@ -104,7 +104,7 @@ bool Recorder::stopRecord(type type, const string &vhost, const string &app, con
if(!src){
return false;
}
return src->setupRecord(type, false, "");
return src->setupRecord(type, false, "", 0);
}
} /* namespace mediakit */

View File

@ -57,9 +57,10 @@ public:
* @param app
* @param stream_id id
* @param customized_path
* @param max_second mp4录制最大切片时间0
* @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
* @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);
/**
*