mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
修改mp4录制相关代码风格
This commit is contained in:
parent
7c016ad14b
commit
2422285829
@ -20,17 +20,13 @@ using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
MP4Recorder::MP4Recorder(const string &strPath,
|
||||
const string &strVhost,
|
||||
const string &strApp,
|
||||
const string &strStreamId,
|
||||
size_t max_second) {
|
||||
_strPath = strPath;
|
||||
MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second) {
|
||||
_folder_path = path;
|
||||
/////record 业务逻辑//////
|
||||
_info.app = strApp;
|
||||
_info.stream = strStreamId;
|
||||
_info.vhost = strVhost;
|
||||
_info.folder = strPath;
|
||||
_info.app = app;
|
||||
_info.stream = stream_id;
|
||||
_info.vhost = vhost;
|
||||
_info.folder = path;
|
||||
GET_CONFIG(size_t ,recordSec,Record::kFileSecond);
|
||||
_max_second = max_second ? max_second : recordSec;
|
||||
}
|
||||
@ -40,31 +36,27 @@ MP4Recorder::~MP4Recorder() {
|
||||
|
||||
void MP4Recorder::createFile() {
|
||||
closeFile();
|
||||
auto strDate = getTimeStr("%Y-%m-%d");
|
||||
auto strTime = getTimeStr("%H-%M-%S");
|
||||
auto strFileTmp = _strPath + strDate + "/." + strTime + ".mp4";
|
||||
auto strFile = _strPath + strDate + "/" + strTime + ".mp4";
|
||||
auto date = getTimeStr("%Y-%m-%d");
|
||||
auto time = getTimeStr("%H-%M-%S");
|
||||
auto full_path_tmp = _folder_path + date + "/." + time + ".mp4";
|
||||
auto full_path = _folder_path + date + "/" + time + ".mp4";
|
||||
|
||||
/////record 业务逻辑//////
|
||||
_info.start_time = ::time(NULL);
|
||||
_info.file_name = strTime + ".mp4";
|
||||
_info.file_path = strFile;
|
||||
_info.file_name = time + ".mp4";
|
||||
_info.file_path = full_path;
|
||||
GET_CONFIG(string, appName, Record::kAppName);
|
||||
_info.url = appName + "/"
|
||||
+ _info.app + "/"
|
||||
+ _info.stream + "/"
|
||||
+ strDate + "/"
|
||||
+ strTime + ".mp4";
|
||||
_info.url = appName + "/" + _info.app + "/" + _info.stream + "/" + date + "/" + time + ".mp4";
|
||||
|
||||
try {
|
||||
_muxer = std::make_shared<MP4Muxer>();
|
||||
_muxer->openMP4(strFileTmp);
|
||||
_muxer->openMP4(full_path_tmp);
|
||||
for (auto &track :_tracks) {
|
||||
//添加track
|
||||
_muxer->addTrack(track);
|
||||
}
|
||||
_strFileTmp = strFileTmp;
|
||||
_strFile = strFile;
|
||||
_full_path_tmp = full_path_tmp;
|
||||
_full_path = full_path;
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << ex.what();
|
||||
}
|
||||
@ -72,10 +64,10 @@ void MP4Recorder::createFile() {
|
||||
|
||||
void MP4Recorder::asyncClose() {
|
||||
auto muxer = _muxer;
|
||||
auto strFileTmp = _strFileTmp;
|
||||
auto strFile = _strFile;
|
||||
auto full_path_tmp = _full_path_tmp;
|
||||
auto full_path = _full_path;
|
||||
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执行时间
|
||||
info.time_len = (float) (::time(NULL) - info.start_time);
|
||||
//关闭mp4非常耗时,所以要放在后台线程执行
|
||||
@ -83,15 +75,15 @@ void MP4Recorder::asyncClose() {
|
||||
|
||||
//获取文件大小
|
||||
struct stat fileData;
|
||||
stat(strFileTmp.data(), &fileData);
|
||||
stat(full_path_tmp.data(), &fileData);
|
||||
info.file_size = fileData.st_size;
|
||||
if (fileData.st_size < 1024) {
|
||||
//录像文件太小,删除之
|
||||
File::delete_file(strFileTmp.data());
|
||||
File::delete_file(full_path_tmp.data());
|
||||
return;
|
||||
}
|
||||
//临时文件名改成正式文件名,防止mp4未完成时被访问
|
||||
rename(strFileTmp.data(),strFile.data());
|
||||
rename(full_path_tmp.data(), full_path.data());
|
||||
|
||||
/////record 业务逻辑//////
|
||||
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4, info);
|
||||
@ -106,17 +98,18 @@ void MP4Recorder::closeFile() {
|
||||
}
|
||||
|
||||
bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
|
||||
if (_baseSec == 0) {
|
||||
_baseSec = frame->dts();
|
||||
if (_last_dts == 0) {
|
||||
//极少情况下dts时间戳可能回退
|
||||
_last_dts = frame->dts();
|
||||
}
|
||||
|
||||
auto duration = frame->dts() - _baseSec;
|
||||
if (!_muxer || ((duration > _max_second * 1000) && (!_haveVideo || (_haveVideo && frame->keyFrame())))) {
|
||||
auto duration = frame->dts() - _last_dts;
|
||||
if (!_muxer || ((duration > _max_second * 1000) && (!_have_video || (_have_video && frame->keyFrame())))) {
|
||||
//成立条件
|
||||
//1、_muxer为空
|
||||
//2、到了切片时间,并且只有音频
|
||||
//3、到了切片时间,有视频并且遇到视频的关键帧
|
||||
_baseSec = 0;
|
||||
_last_dts = 0;
|
||||
createFile();
|
||||
}
|
||||
|
||||
@ -131,7 +124,7 @@ bool MP4Recorder::addTrack(const Track::Ptr & track){
|
||||
//保存所有的track,为创建MP4MuxerFile做准备
|
||||
_tracks.emplace_back(track);
|
||||
if (track->getTrackType() == TrackVideo) {
|
||||
_haveVideo = true;
|
||||
_have_video = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -139,10 +132,9 @@ bool MP4Recorder::addTrack(const Track::Ptr & track){
|
||||
void MP4Recorder::resetTracks() {
|
||||
closeFile();
|
||||
_tracks.clear();
|
||||
_haveVideo = false;
|
||||
_have_video = false;
|
||||
}
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
|
||||
#endif //ENABLE_MP4
|
@ -28,14 +28,10 @@ namespace mediakit {
|
||||
#ifdef ENABLE_MP4
|
||||
class MP4Recorder : public MediaSinkInterface {
|
||||
public:
|
||||
typedef std::shared_ptr<MP4Recorder> Ptr;
|
||||
using Ptr = std::shared_ptr<MP4Recorder>;
|
||||
|
||||
MP4Recorder(const string &strPath,
|
||||
const string &strVhost,
|
||||
const string &strApp,
|
||||
const string &strStreamId,
|
||||
size_t max_second);
|
||||
virtual ~MP4Recorder();
|
||||
MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second);
|
||||
~MP4Recorder() override;
|
||||
|
||||
/**
|
||||
* 重置所有Track
|
||||
@ -56,16 +52,17 @@ private:
|
||||
void createFile();
|
||||
void closeFile();
|
||||
void asyncClose();
|
||||
|
||||
private:
|
||||
bool _haveVideo = false;
|
||||
bool _have_video = false;
|
||||
size_t _max_second;
|
||||
string _strPath;
|
||||
string _strFile;
|
||||
string _strFileTmp;
|
||||
string _folder_path;
|
||||
string _full_path;
|
||||
string _full_path_tmp;
|
||||
RecordInfo _info;
|
||||
MP4Muxer::Ptr _muxer;
|
||||
list<Track::Ptr> _tracks;
|
||||
uint64_t _baseSec = 0;
|
||||
uint64_t _last_dts = 0;
|
||||
};
|
||||
|
||||
#endif ///ENABLE_MP4
|
||||
|
Loading…
Reference in New Issue
Block a user