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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
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>
|
2020-12-05 12:25:52 +08:00
|
|
|
|
#include "Util/File.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
|
|
|
|
|
2019-08-01 11:44:16 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2021-03-07 10:41:57 +08:00
|
|
|
|
MP4Recorder::MP4Recorder(const string &strPath,
|
|
|
|
|
const string &strVhost,
|
|
|
|
|
const string &strApp,
|
|
|
|
|
const string &strStreamId,
|
|
|
|
|
size_t max_second) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
_strPath = strPath;
|
|
|
|
|
/////record 业务逻辑//////
|
2020-09-20 11:40:42 +08:00
|
|
|
|
_info.app = strApp;
|
|
|
|
|
_info.stream = strStreamId;
|
|
|
|
|
_info.vhost = strVhost;
|
|
|
|
|
_info.folder = strPath;
|
2021-03-07 10:41:57 +08:00
|
|
|
|
GET_CONFIG(size_t ,recordSec,Record::kFileSecond);
|
|
|
|
|
_max_second = max_second ? max_second : recordSec;
|
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 业务逻辑//////
|
2020-09-20 11:40:42 +08:00
|
|
|
|
_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);
|
2020-09-20 11:40:42 +08:00
|
|
|
|
_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) {
|
2019-08-02 10:53:00 +08:00
|
|
|
|
//添加track
|
|
|
|
|
_muxer->addTrack(track);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
_strFileTmp = strFileTmp;
|
|
|
|
|
_strFile = strFile;
|
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;
|
2021-03-14 10:29:17 +08:00
|
|
|
|
WorkThreadPool::Instance().getExecutor()->async([muxer,strFileTmp,strFile,info]() mutable{
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//获取文件录制时间,放在关闭mp4之前是为了忽略关闭mp4执行时间
|
2021-03-14 10:29:17 +08:00
|
|
|
|
info.time_len = (float)(::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-12-05 12:25:52 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//获取文件大小
|
|
|
|
|
struct stat fileData;
|
2020-12-05 12:25:52 +08:00
|
|
|
|
stat(strFileTmp.data(), &fileData);
|
2021-03-14 10:29:17 +08:00
|
|
|
|
info.file_size = fileData.st_size;
|
2020-12-05 12:25:52 +08:00
|
|
|
|
if (fileData.st_size < 1024) {
|
|
|
|
|
//录像文件太小,删除之
|
|
|
|
|
File::delete_file(strFileTmp.data());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//临时文件名改成正式文件名,防止mp4未完成时被访问
|
|
|
|
|
rename(strFileTmp.data(),strFile.data());
|
|
|
|
|
|
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) {
|
2021-08-12 16:07:31 +08:00
|
|
|
|
if (_baseSec == 0) {
|
|
|
|
|
_baseSec = frame->dts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto duration = frame->dts() - _baseSec;
|
|
|
|
|
if (!_muxer || ((duration > _max_second * 1000) && (!_haveVideo || (_haveVideo && frame->keyFrame())))) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//成立条件
|
|
|
|
|
//1、_muxer为空
|
|
|
|
|
//2、到了切片时间,并且只有音频
|
|
|
|
|
//3、到了切片时间,有视频并且遇到视频的关键帧
|
2021-08-09 18:28:43 +08:00
|
|
|
|
_baseSec = 0;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
createFile();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
if (_muxer) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//生成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
|
|
|
|
}
|
|
|
|
|
|
2019-10-16 11:10:20 +08:00
|
|
|
|
void MP4Recorder::resetTracks() {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
closeFile();
|
|
|
|
|
_tracks.clear();
|
|
|
|
|
_haveVideo = false;
|
2019-10-16 11:10:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
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
|