ZLMediaKit/src/Record/MP4Recorder.cpp

134 lines
3.9 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* 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.
2017-09-27 16:20:30 +08:00
*/
2017-04-01 16:35:56 +08:00
2020-04-03 20:45:58 +08:00
#ifdef ENABLE_MP4
2019-07-26 09:10:27 +08:00
#include <ctime>
2017-04-01 16:35:56 +08:00
#include <sys/stat.h>
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
2019-08-22 16:25:19 +08:00
#include "MP4Recorder.h"
2019-04-03 11:09:50 +08:00
#include "Thread/WorkThreadPool.h"
2018-10-30 14:59:42 +08:00
using namespace toolkit;
namespace mediakit {
2019-08-22 16:25:19 +08:00
MP4Recorder::MP4Recorder(const string& strPath,
2020-03-20 11:51:24 +08:00
const string &strVhost,
const string &strApp,
const string &strStreamId) {
_strPath = strPath;
/////record 业务逻辑//////
_info.app = strApp;
_info.stream = strStreamId;
_info.vhost = strVhost;
_info.folder = strPath;
2017-04-01 16:35:56 +08:00
}
2019-08-22 16:25:19 +08:00
MP4Recorder::~MP4Recorder() {
2020-03-20 11:51:24 +08:00
closeFile();
2017-04-01 16:35:56 +08:00
}
2019-08-22 16:25:19 +08:00
void MP4Recorder::createFile() {
2020-03-20 11:51:24 +08:00
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";
/////record 业务逻辑//////
_info.start_time = ::time(NULL);
_info.file_name = strTime + ".mp4";
_info.file_path = strFile;
2019-05-28 17:14:36 +08:00
GET_CONFIG(string,appName,Record::kAppName);
_info.url = appName + "/"
+ _info.app + "/"
+ _info.stream + "/"
+ strDate + "/"
+ strTime + ".mp4";
2018-02-09 11:42:55 +08:00
2020-03-20 11:51:24 +08:00
try {
2020-09-20 19:45:04 +08:00
_muxer = std::make_shared<MP4Muxer>();
_muxer->openMP4(strFileTmp);
for (auto &track :_tracks) {
//添加track
_muxer->addTrack(track);
2020-03-20 11:51:24 +08:00
}
_strFileTmp = strFileTmp;
_strFile = strFile;
_createFileTicker.resetTime();
2020-09-20 19:45:04 +08:00
} catch (std::exception &ex) {
2020-03-20 11:51:24 +08:00
WarnL << ex.what();
}
2017-04-01 16:35:56 +08:00
}
2019-08-22 16:25:19 +08:00
void MP4Recorder::asyncClose() {
2020-03-20 11:51:24 +08:00
auto muxer = _muxer;
auto strFileTmp = _strFileTmp;
auto strFile = _strFile;
auto info = _info;
WorkThreadPool::Instance().getExecutor()->async([muxer,strFileTmp,strFile,info]() {
//获取文件录制时间放在关闭mp4之前是为了忽略关闭mp4执行时间
const_cast<RecordInfo&>(info).time_len = ::time(NULL) - info.start_time;
2020-03-20 11:51:24 +08:00
//关闭mp4非常耗时所以要放在后台线程执行
2020-07-16 10:40:30 +08:00
muxer->closeMP4();
2020-03-20 11:51:24 +08:00
//临时文件名改成正式文件名防止mp4未完成时被访问
rename(strFileTmp.data(),strFile.data());
//获取文件大小
struct stat fileData;
stat(strFile.data(), &fileData);
const_cast<RecordInfo&>(info).file_size = fileData.st_size;
2020-03-20 11:51:24 +08:00
/////record 业务逻辑//////
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,info);
});
2019-04-03 11:09:50 +08:00
}
2019-08-22 16:25:19 +08:00
void MP4Recorder::closeFile() {
2020-03-20 11:51:24 +08:00
if (_muxer) {
asyncClose();
_muxer = nullptr;
}
2017-04-01 16:35:56 +08:00
}
2019-12-03 12:32:57 +08:00
void MP4Recorder::inputFrame(const Frame::Ptr &frame) {
2020-03-20 11:51:24 +08:00
GET_CONFIG(uint32_t,recordSec,Record::kFileSecond);
if(!_muxer || ((_createFileTicker.elapsedTime() > recordSec * 1000) &&
(!_haveVideo || (_haveVideo && frame->keyFrame()))) ){
//成立条件
//1、_muxer为空
//2、到了切片时间并且只有音频
//3、到了切片时间有视频并且遇到视频的关键帧
createFile();
}
if(_muxer){
//生成mp4文件
_muxer->inputFrame(frame);
}
2018-10-28 00:15:27 +08:00
}
2019-12-03 12:32:57 +08:00
void MP4Recorder::addTrack(const Track::Ptr & track){
2020-03-20 11:51:24 +08:00
//保存所有的track为创建MP4MuxerFile做准备
_tracks.emplace_back(track);
if(track->getTrackType() == TrackVideo){
_haveVideo = true;
}
2018-10-28 00:15:27 +08:00
}
void MP4Recorder::resetTracks() {
2020-03-20 11:51:24 +08:00
closeFile();
_tracks.clear();
_haveVideo = false;
_createFileTicker.resetTime();
}
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00
2020-04-03 20:45:58 +08:00
#endif //ENABLE_MP4