2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-08-01 11:44:16 +08:00
|
|
|
|
#ifdef ENABLE_MP4RECORD
|
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
|
|
|
|
|
2019-08-01 11:44:16 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2019-08-22 16:25:19 +08:00
|
|
|
|
MP4Recorder::MP4Recorder(const string& strPath,
|
2018-02-02 18:06:08 +08:00
|
|
|
|
const string &strVhost,
|
|
|
|
|
const string &strApp,
|
2018-10-28 00:15:27 +08:00
|
|
|
|
const string &strStreamId) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strPath = strPath;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
/////record 业务逻辑//////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_info.strAppName = strApp;
|
|
|
|
|
_info.strStreamId = strStreamId;
|
|
|
|
|
_info.strVhost = strVhost;
|
|
|
|
|
_info.strFolder = strPath;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2019-08-22 16:25:19 +08:00
|
|
|
|
MP4Recorder::~MP4Recorder() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
closeFile();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:25:19 +08:00
|
|
|
|
void MP4Recorder::createFile() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
closeFile();
|
2019-12-12 22:58:31 +08:00
|
|
|
|
auto strDate = getTimeStr("%Y-%m-%d");
|
2020-01-20 15:02:45 +08:00
|
|
|
|
auto strDay = getTimeStr("%Y-%m-%d");
|
2019-12-12 22:58:31 +08:00
|
|
|
|
auto strTime = getTimeStr("%H-%M-%S");
|
2020-01-20 15:02:45 +08:00
|
|
|
|
auto strFileTmp = _strPath + strDate + "/" + strDay + "/." + strTime + ".mp4";
|
|
|
|
|
auto strFile = _strPath + strDate + "/" + strDay + "/" + strTime + ".mp4";
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
/////record 业务逻辑//////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_info.ui64StartedTime = ::time(NULL);
|
|
|
|
|
_info.strFileName = strTime + ".mp4";
|
|
|
|
|
_info.strFilePath = strFile;
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(string,appName,Record::kAppName);
|
2019-05-29 09:24:02 +08:00
|
|
|
|
_info.strUrl = appName + "/"
|
|
|
|
|
+ _info.strAppName + "/"
|
|
|
|
|
+ _info.strStreamId + "/"
|
|
|
|
|
+ strDate + "/"
|
2020-01-20 15:02:45 +08:00
|
|
|
|
+ strDay + "/"
|
2019-05-29 09:24:02 +08:00
|
|
|
|
+ strTime + ".mp4";
|
2018-02-09 11:42:55 +08:00
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
try {
|
|
|
|
|
_muxer = std::make_shared<MP4MuxerFile>(strFileTmp.data());
|
|
|
|
|
for(auto &track :_tracks){
|
2019-08-02 10:53:00 +08:00
|
|
|
|
//添加track
|
|
|
|
|
_muxer->addTrack(track);
|
2018-10-28 00:15:27 +08:00
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
_strFileTmp = strFileTmp;
|
|
|
|
|
_strFile = strFile;
|
|
|
|
|
_createFileTicker.resetTime();
|
|
|
|
|
}catch(std::exception &ex) {
|
|
|
|
|
WarnL << ex.what();
|
2018-10-28 00:15:27 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:25:19 +08:00
|
|
|
|
void MP4Recorder::asyncClose() {
|
2019-08-01 18:49:04 +08:00
|
|
|
|
auto muxer = _muxer;
|
2019-04-03 11:09:50 +08:00
|
|
|
|
auto strFileTmp = _strFileTmp;
|
|
|
|
|
auto strFile = _strFile;
|
|
|
|
|
auto info = _info;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
WorkThreadPool::Instance().getExecutor()->async([muxer,strFileTmp,strFile,info]() {
|
|
|
|
|
//获取文件录制时间,放在关闭mp4之前是为了忽略关闭mp4执行时间
|
2019-12-04 18:36:30 +08:00
|
|
|
|
const_cast<MP4Info&>(info).ui64TimeLen = ::time(NULL) - info.ui64StartedTime;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
//关闭mp4非常耗时,所以要放在后台线程执行
|
|
|
|
|
const_cast<MP4MuxerFile::Ptr &>(muxer).reset();
|
2019-04-03 11:09:50 +08:00
|
|
|
|
//临时文件名改成正式文件名,防止mp4未完成时被访问
|
|
|
|
|
rename(strFileTmp.data(),strFile.data());
|
|
|
|
|
//获取文件大小
|
|
|
|
|
struct stat fileData;
|
|
|
|
|
stat(strFile.data(), &fileData);
|
2019-12-04 18:36:30 +08:00
|
|
|
|
const_cast<MP4Info&>(info).ui64FileSize = fileData.st_size;
|
2019-04-03 11:09:50 +08:00
|
|
|
|
/////record 业务逻辑//////
|
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,info);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:25:19 +08:00
|
|
|
|
void MP4Recorder::closeFile() {
|
2019-08-01 18:49:04 +08:00
|
|
|
|
if (_muxer) {
|
2019-08-01 11:44:16 +08:00
|
|
|
|
asyncClose();
|
2019-08-01 18:49:04 +08:00
|
|
|
|
_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) {
|
2019-08-01 18:49:04 +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){
|
2019-08-01 18:49:04 +08:00
|
|
|
|
//保存所有的track,为创建MP4MuxerFile做准备
|
2019-08-01 22:07:40 +08:00
|
|
|
|
_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() {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
#endif //ENABLE_MP4RECORD
|