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
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
2019-08-01 11:44:16 +08:00
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2021-10-20 17:37:19 +08:00
|
|
|
|
MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &app, const string &stream_id, size_t max_second) {
|
|
|
|
|
_folder_path = path;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/////record 业务逻辑//////
|
2021-10-20 17:37:19 +08:00
|
|
|
|
_info.app = app;
|
|
|
|
|
_info.stream = stream_id;
|
|
|
|
|
_info.vhost = vhost;
|
|
|
|
|
_info.folder = path;
|
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
|
|
|
|
}
|
2021-12-29 20:48:15 +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();
|
2021-10-20 17:37:19 +08:00
|
|
|
|
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";
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
/////record 业务逻辑//////
|
2020-09-20 11:40:42 +08:00
|
|
|
|
_info.start_time = ::time(NULL);
|
2021-10-20 17:37:19 +08:00
|
|
|
|
_info.file_name = time + ".mp4";
|
|
|
|
|
_info.file_path = full_path;
|
|
|
|
|
GET_CONFIG(string, appName, Record::kAppName);
|
|
|
|
|
_info.url = appName + "/" + _info.app + "/" + _info.stream + "/" + date + "/" + time + ".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>();
|
2021-10-20 17:37:19 +08:00
|
|
|
|
_muxer->openMP4(full_path_tmp);
|
2020-09-20 19:45:04 +08:00
|
|
|
|
for (auto &track :_tracks) {
|
2019-08-02 10:53:00 +08:00
|
|
|
|
//添加track
|
|
|
|
|
_muxer->addTrack(track);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-10-20 17:37:19 +08:00
|
|
|
|
_full_path_tmp = full_path_tmp;
|
|
|
|
|
_full_path = full_path;
|
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
|
|
|
|
}
|
2022-09-07 11:06:39 +08:00
|
|
|
|
|
2019-08-22 16:25:19 +08:00
|
|
|
|
void MP4Recorder::asyncClose() {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
auto muxer = _muxer;
|
2021-10-20 17:37:19 +08:00
|
|
|
|
auto full_path_tmp = _full_path_tmp;
|
|
|
|
|
auto full_path = _full_path;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
auto info = _info;
|
2021-10-20 17:37:19 +08:00
|
|
|
|
WorkThreadPool::Instance().getExecutor()->async([muxer, full_path_tmp, full_path, info]() mutable {
|
2022-10-16 16:40:21 +08:00
|
|
|
|
info.time_len = muxer->getDuration() / 1000.0f;
|
|
|
|
|
// 关闭mp4可能非常耗时,所以要放在后台线程执行
|
2020-07-16 10:40:30 +08:00
|
|
|
|
muxer->closeMP4();
|
2022-10-16 16:40:21 +08:00
|
|
|
|
if (!full_path_tmp.empty()) {
|
|
|
|
|
// 获取文件大小
|
2022-06-19 17:27:03 +08:00
|
|
|
|
info.file_size = File::fileSize(full_path_tmp.data());
|
|
|
|
|
if (info.file_size < 1024) {
|
2022-10-16 16:40:21 +08:00
|
|
|
|
// 录像文件太小,删除之
|
2022-06-19 17:27:03 +08:00
|
|
|
|
File::delete_file(full_path_tmp.data());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-16 16:40:21 +08:00
|
|
|
|
// 临时文件名改成正式文件名,防止mp4未完成时被访问
|
2022-06-19 17:27:03 +08:00
|
|
|
|
rename(full_path_tmp.data(), full_path.data());
|
2020-12-05 12:25:52 +08:00
|
|
|
|
}
|
2022-10-16 16:40:21 +08:00
|
|
|
|
//触发mp4录制切片生成事件
|
2021-10-20 17:37:19 +08:00
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4, info);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
});
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
|
2022-03-20 14:52:15 +08:00
|
|
|
|
if (!(_have_video && frame->getTrackType() == TrackAudio)) {
|
|
|
|
|
//如果有视频且输入的是音频,那么应该忽略切片逻辑
|
|
|
|
|
if (_last_dts == 0 || _last_dts > frame->dts()) {
|
|
|
|
|
//极少情况下dts时间戳可能回退
|
|
|
|
|
_last_dts = frame->dts();
|
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
2022-03-20 14:52:15 +08:00
|
|
|
|
auto duration = frame->dts() - _last_dts;
|
|
|
|
|
if (!_muxer || ((duration > _max_second * 1000) && (!_have_video || (_have_video && frame->keyFrame())))) {
|
|
|
|
|
//成立条件
|
|
|
|
|
// 1、_muxer为空
|
|
|
|
|
// 2、到了切片时间,并且只有音频
|
|
|
|
|
// 3、到了切片时间,有视频并且遇到视频的关键帧
|
|
|
|
|
_last_dts = 0;
|
|
|
|
|
createFile();
|
|
|
|
|
}
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
if (_muxer) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//生成mp4文件
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return _muxer->inputFrame(frame);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2018-10-28 00:15:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 17:37:19 +08:00
|
|
|
|
bool MP4Recorder::addTrack(const Track::Ptr &track) {
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//保存所有的track,为创建MP4MuxerFile做准备
|
|
|
|
|
_tracks.emplace_back(track);
|
2021-10-20 17:37:19 +08:00
|
|
|
|
if (track->getTrackType() == TrackVideo) {
|
|
|
|
|
_have_video = true;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return 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();
|
2021-10-20 17:37:19 +08:00
|
|
|
|
_have_video = 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
|
|
|
|
|
2022-06-19 17:27:03 +08:00
|
|
|
|
#endif //ENABLE_MP4
|