From ea4f9a0c4a7f8a46718cd1de60bc0e21f4f25ce5 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 3 Apr 2019 11:09:50 +0800 Subject: [PATCH] =?UTF-8?q?mp4close=E6=94=BE=E5=9C=A8=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- src/Common/config.cpp | 4 ++-- src/Common/config.h | 4 ++-- src/MediaFile/Mp4Maker.cpp | 37 +++++++++++++++++++++++-------------- src/MediaFile/Mp4Maker.h | 1 + 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index e79d24b5..b6bca50b 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit e79d24b51051ed9b3bd9c66b9d8ace3c76a0411a +Subproject commit b6bca50b1213de39b460b6a87f91d4bec5d95f7e diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 8139c467..fc4457c3 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -227,8 +227,8 @@ const char kAppName[] = RECORD_FIELD"appName"; #define RECORD_SAMPLE_MS 100 const char kSampleMS[] = RECORD_FIELD"sampleMS"; -//MP4文件录制大小,不能太大,否则MP4Close函数执行事件太长 -#define RECORD_FILE_SECOND (10*60) +//MP4文件录制大小,默认一个小时 +#define RECORD_FILE_SECOND (60*60) const char kFileSecond[] = RECORD_FIELD"fileSecond"; //录制文件路径 diff --git a/src/Common/config.h b/src/Common/config.h index 080f94e4..57134e7f 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -73,7 +73,7 @@ extern const char kBroadcastMediaChanged[]; //录制mp4文件成功后广播 extern const char kBroadcastRecordMP4[]; -#define BroadcastRecordMP4Args const Mp4Info &info,Mp4Maker &sender +#define BroadcastRecordMP4Args const Mp4Info &info //收到http api请求广播 extern const char kBroadcastHttpRequest[]; @@ -216,7 +216,7 @@ namespace Record { extern const char kAppName[]; //每次流化MP4文件的时长,单位毫秒 extern const char kSampleMS[]; -//MP4文件录制大小,不能太大,否则MP4Close函数执行事件太长 +//MP4文件录制大小,默认一个小时 extern const char kFileSecond[]; //录制文件路径 extern const char kFilePath[]; diff --git a/src/MediaFile/Mp4Maker.cpp b/src/MediaFile/Mp4Maker.cpp index 82f4beb2..6da70143 100644 --- a/src/MediaFile/Mp4Maker.cpp +++ b/src/MediaFile/Mp4Maker.cpp @@ -36,6 +36,7 @@ #include "Util/NoticeCenter.h" #include "Extension/H264.h" #include "Extension/AAC.h" +#include "Thread/WorkThreadPool.h" using namespace toolkit; @@ -212,25 +213,33 @@ void Mp4Maker::createFile() { } } +void Mp4Maker::asyncClose() { + auto hMp4 = _hMp4; + auto strFileTmp = _strFileTmp; + auto strFile = _strFile; + auto info = _info; + WorkThreadPool::Instance().getExecutor()->async([hMp4,strFileTmp,strFile,info]() { + //获取文件录制时间,放在MP4Close之前是为了忽略MP4Close执行时间 + const_cast(info).ui64TimeLen = ::time(NULL) - info.ui64StartedTime; + //MP4Close非常耗时,所以要放在后台线程执行 + MP4Close(hMp4,MP4_CLOSE_DO_NOT_COMPUTE_BITRATE); + //临时文件名改成正式文件名,防止mp4未完成时被访问 + rename(strFileTmp.data(),strFile.data()); + //获取文件大小 + struct stat fileData; + stat(strFile.data(), &fileData); + const_cast(info).ui64FileSize = fileData.st_size; + /////record 业务逻辑////// + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,info); + }); +} + void Mp4Maker::closeFile() { if (_hMp4 != MP4_INVALID_FILE_HANDLE) { - { - TimeTicker(); - MP4Close(_hMp4,MP4_CLOSE_DO_NOT_COMPUTE_BITRATE); - } - rename(_strFileTmp.data(),_strFile.data()); + asyncClose(); _hMp4 = MP4_INVALID_FILE_HANDLE; _hVideo = MP4_INVALID_TRACK_ID; _hAudio = MP4_INVALID_TRACK_ID; - - /////record 业务逻辑////// - _info.ui64TimeLen = ::time(NULL) - _info.ui64StartedTime; - //获取文件大小 - struct stat fileData; - stat(_strFile.data(), &fileData); - _info.ui64FileSize = fileData.st_size; - //----record 业务逻辑----// - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastRecordMP4,_info,*this); } } diff --git a/src/MediaFile/Mp4Maker.h b/src/MediaFile/Mp4Maker.h index 29e810e5..15a0d6fc 100644 --- a/src/MediaFile/Mp4Maker.h +++ b/src/MediaFile/Mp4Maker.h @@ -79,6 +79,7 @@ private: private: void createFile(); void closeFile(); + void asyncClose(); //时间戳:参考频率1000 void inputH264(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp);