修改mp4录制相关代码风格

This commit is contained in:
ziyue 2021-10-20 17:37:19 +08:00
parent 7c016ad14b
commit 2422285829
2 changed files with 46 additions and 57 deletions

View File

@ -20,17 +20,13 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
MP4Recorder::MP4Recorder(const string &strPath, MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second) {
const string &strVhost, _folder_path = path;
const string &strApp,
const string &strStreamId,
size_t max_second) {
_strPath = strPath;
/////record 业务逻辑////// /////record 业务逻辑//////
_info.app = strApp; _info.app = app;
_info.stream = strStreamId; _info.stream = stream_id;
_info.vhost = strVhost; _info.vhost = vhost;
_info.folder = strPath; _info.folder = path;
GET_CONFIG(size_t ,recordSec,Record::kFileSecond); GET_CONFIG(size_t ,recordSec,Record::kFileSecond);
_max_second = max_second ? max_second : recordSec; _max_second = max_second ? max_second : recordSec;
} }
@ -40,31 +36,27 @@ MP4Recorder::~MP4Recorder() {
void MP4Recorder::createFile() { void MP4Recorder::createFile() {
closeFile(); closeFile();
auto strDate = getTimeStr("%Y-%m-%d"); auto date = getTimeStr("%Y-%m-%d");
auto strTime = getTimeStr("%H-%M-%S"); auto time = getTimeStr("%H-%M-%S");
auto strFileTmp = _strPath + strDate + "/." + strTime + ".mp4"; auto full_path_tmp = _folder_path + date + "/." + time + ".mp4";
auto strFile = _strPath + strDate + "/" + strTime + ".mp4"; auto full_path = _folder_path + date + "/" + time + ".mp4";
/////record 业务逻辑////// /////record 业务逻辑//////
_info.start_time = ::time(NULL); _info.start_time = ::time(NULL);
_info.file_name = strTime + ".mp4"; _info.file_name = time + ".mp4";
_info.file_path = strFile; _info.file_path = full_path;
GET_CONFIG(string,appName,Record::kAppName); GET_CONFIG(string, appName, Record::kAppName);
_info.url = appName + "/" _info.url = appName + "/" + _info.app + "/" + _info.stream + "/" + date + "/" + time + ".mp4";
+ _info.app + "/"
+ _info.stream + "/"
+ strDate + "/"
+ strTime + ".mp4";
try { try {
_muxer = std::make_shared<MP4Muxer>(); _muxer = std::make_shared<MP4Muxer>();
_muxer->openMP4(strFileTmp); _muxer->openMP4(full_path_tmp);
for (auto &track :_tracks) { for (auto &track :_tracks) {
//添加track //添加track
_muxer->addTrack(track); _muxer->addTrack(track);
} }
_strFileTmp = strFileTmp; _full_path_tmp = full_path_tmp;
_strFile = strFile; _full_path = full_path;
} catch (std::exception &ex) { } catch (std::exception &ex) {
WarnL << ex.what(); WarnL << ex.what();
} }
@ -72,29 +64,29 @@ void MP4Recorder::createFile() {
void MP4Recorder::asyncClose() { void MP4Recorder::asyncClose() {
auto muxer = _muxer; auto muxer = _muxer;
auto strFileTmp = _strFileTmp; auto full_path_tmp = _full_path_tmp;
auto strFile = _strFile; auto full_path = _full_path;
auto info = _info; auto info = _info;
WorkThreadPool::Instance().getExecutor()->async([muxer,strFileTmp,strFile,info]() mutable{ WorkThreadPool::Instance().getExecutor()->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非常耗时所以要放在后台线程执行
muxer->closeMP4(); muxer->closeMP4();
//获取文件大小 //获取文件大小
struct stat fileData; struct stat fileData;
stat(strFileTmp.data(), &fileData); stat(full_path_tmp.data(), &fileData);
info.file_size = fileData.st_size; info.file_size = fileData.st_size;
if (fileData.st_size < 1024) { if (fileData.st_size < 1024) {
//录像文件太小,删除之 //录像文件太小,删除之
File::delete_file(strFileTmp.data()); File::delete_file(full_path_tmp.data());
return; return;
} }
//临时文件名改成正式文件名防止mp4未完成时被访问 //临时文件名改成正式文件名防止mp4未完成时被访问
rename(strFileTmp.data(),strFile.data()); rename(full_path_tmp.data(), full_path.data());
/////record 业务逻辑////// /////record 业务逻辑//////
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,info); NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4, info);
}); });
} }
@ -106,17 +98,18 @@ void MP4Recorder::closeFile() {
} }
bool MP4Recorder::inputFrame(const Frame::Ptr &frame) { bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
if (_baseSec == 0) { if (_last_dts == 0) {
_baseSec = frame->dts(); //极少情况下dts时间戳可能回退
_last_dts = frame->dts();
} }
auto duration = frame->dts() - _baseSec; auto duration = frame->dts() - _last_dts;
if (!_muxer || ((duration > _max_second * 1000) && (!_haveVideo || (_haveVideo && frame->keyFrame())))) { if (!_muxer || ((duration > _max_second * 1000) && (!_have_video || (_have_video && frame->keyFrame())))) {
//成立条件 //成立条件
//1、_muxer为空 //1、_muxer为空
//2、到了切片时间并且只有音频 //2、到了切片时间并且只有音频
//3、到了切片时间有视频并且遇到视频的关键帧 //3、到了切片时间有视频并且遇到视频的关键帧
_baseSec = 0; _last_dts = 0;
createFile(); createFile();
} }
@ -127,11 +120,11 @@ bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
return false; return false;
} }
bool MP4Recorder::addTrack(const Track::Ptr & track){ bool MP4Recorder::addTrack(const Track::Ptr &track) {
//保存所有的track为创建MP4MuxerFile做准备 //保存所有的track为创建MP4MuxerFile做准备
_tracks.emplace_back(track); _tracks.emplace_back(track);
if(track->getTrackType() == TrackVideo){ if (track->getTrackType() == TrackVideo) {
_haveVideo = true; _have_video = true;
} }
return true; return true;
} }
@ -139,10 +132,9 @@ bool MP4Recorder::addTrack(const Track::Ptr & track){
void MP4Recorder::resetTracks() { void MP4Recorder::resetTracks() {
closeFile(); closeFile();
_tracks.clear(); _tracks.clear();
_haveVideo = false; _have_video = false;
} }
} /* namespace mediakit */ } /* namespace mediakit */
#endif //ENABLE_MP4 #endif //ENABLE_MP4

View File

@ -26,16 +26,12 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
#ifdef ENABLE_MP4 #ifdef ENABLE_MP4
class MP4Recorder : public MediaSinkInterface{ class MP4Recorder : public MediaSinkInterface {
public: public:
typedef std::shared_ptr<MP4Recorder> Ptr; using Ptr = std::shared_ptr<MP4Recorder>;
MP4Recorder(const string &strPath, MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second);
const string &strVhost, ~MP4Recorder() override;
const string &strApp,
const string &strStreamId,
size_t max_second);
virtual ~MP4Recorder();
/** /**
* Track * Track
@ -56,16 +52,17 @@ private:
void createFile(); void createFile();
void closeFile(); void closeFile();
void asyncClose(); void asyncClose();
private: private:
bool _haveVideo = false; bool _have_video = false;
size_t _max_second; size_t _max_second;
string _strPath; string _folder_path;
string _strFile; string _full_path;
string _strFileTmp; string _full_path_tmp;
RecordInfo _info; RecordInfo _info;
MP4Muxer::Ptr _muxer; MP4Muxer::Ptr _muxer;
list<Track::Ptr> _tracks; list<Track::Ptr> _tracks;
uint64_t _baseSec = 0; uint64_t _last_dts = 0;
}; };
#endif ///ENABLE_MP4 #endif ///ENABLE_MP4