From 5fe5dd9de60f44fb6e6644b9c263fe331fd43f80 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 12 Dec 2019 22:58:31 +0800 Subject: [PATCH] =?UTF-8?q?hls=E4=BF=9D=E5=AD=98ts=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=97=B6=E6=A0=B9=E6=8D=AE=E6=97=A5=E6=9C=9F=E3=80=81=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- src/Record/HlsMakerImp.cpp | 29 ++++++++++++++++++----------- src/Record/HlsMakerImp.h | 2 +- src/Record/MP4Recorder.cpp | 22 ++-------------------- 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index 628d3b25..ecb38246 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit 628d3b2527f63b54a5eb38b9e9973254d4a2192b +Subproject commit ecb382460ffa2f6f5cdf1e11b394797e3396b2d0 diff --git a/src/Record/HlsMakerImp.cpp b/src/Record/HlsMakerImp.cpp index 8fa9d4fd..88cd724f 100644 --- a/src/Record/HlsMakerImp.cpp +++ b/src/Record/HlsMakerImp.cpp @@ -56,21 +56,31 @@ HlsMakerImp::~HlsMakerImp() { } string HlsMakerImp::onOpenSegment(int index) { - auto full_path = fullPath(index); - _file = makeFile(full_path, true); + string segment_name , segment_path; + { + auto strDate = getTimeStr("%Y-%m-%d"); + auto strTime = getTimeStr("%H-%M-%S"); + segment_name = StrPrinter << strDate + "/" + strTime << "_" << index << ".ts"; + segment_path = _path_prefix + "/" + segment_name; + _segment_file_paths.emplace(index,segment_path); + } + _file = makeFile(segment_path, true); if(!_file){ - WarnL << "create file falied," << full_path << " " << get_uv_errmsg(); + WarnL << "create file falied," << segment_path << " " << get_uv_errmsg(); } - //DebugL << index << " " << full_path; if(_params.empty()){ - return StrPrinter << index << ".ts"; + return std::move(segment_name); } - return StrPrinter << index << ".ts" << "?" << _params; + return std::move(segment_name + "?" + _params); } void HlsMakerImp::onDelSegment(int index) { - //WarnL << index; - File::delete_file(fullPath(index).data()); + auto it = _segment_file_paths.find(index); + if(it == _segment_file_paths.end()){ + return; + } + File::delete_file(it->second.data()); + _segment_file_paths.erase(it); } void HlsMakerImp::onWriteSegment(const char *data, int len) { @@ -90,9 +100,6 @@ void HlsMakerImp::onWriteHls(const char *data, int len) { //DebugL << "\r\n" << string(data,len); } -string HlsMakerImp::fullPath(int index) { - return StrPrinter << _path_prefix << "/" << index << ".ts"; -} std::shared_ptr HlsMakerImp::makeFile(const string &file,bool setbuf) { auto ret= shared_ptr(File::createfile_file(file.data(), "wb"), [](FILE *fp) { diff --git a/src/Record/HlsMakerImp.h b/src/Record/HlsMakerImp.h index 55637fd8..ece0213a 100644 --- a/src/Record/HlsMakerImp.h +++ b/src/Record/HlsMakerImp.h @@ -49,9 +49,9 @@ protected: void onWriteSegment(const char *data, int len) override; void onWriteHls(const char *data, int len) override; private: - string fullPath(int index); std::shared_ptr makeFile(const string &file,bool setbuf = false); private: + map _segment_file_paths; std::shared_ptr _file; std::shared_ptr _file_buf; string _path_prefix; diff --git a/src/Record/MP4Recorder.cpp b/src/Record/MP4Recorder.cpp index e0332a56..a92f3fde 100644 --- a/src/Record/MP4Recorder.cpp +++ b/src/Record/MP4Recorder.cpp @@ -29,30 +29,12 @@ #include #include "Common/config.h" #include "MP4Recorder.h" -#include "Util/util.h" -#include "Util/NoticeCenter.h" #include "Thread/WorkThreadPool.h" using namespace toolkit; namespace mediakit { -string timeStr(const char *fmt) { - std::tm tm_snapshot; - auto time = ::time(NULL); -#if defined(_WIN32) - localtime_s(&tm_snapshot, &time); // thread-safe -#else - localtime_r(&time, &tm_snapshot); // POSIX -#endif - const size_t size = 1024; - char buffer[size]; - auto success = std::strftime(buffer, size, fmt, &tm_snapshot); - if (0 == success) - return string(fmt); - return buffer; -} - MP4Recorder::MP4Recorder(const string& strPath, const string &strVhost, const string &strApp, @@ -70,8 +52,8 @@ MP4Recorder::~MP4Recorder() { void MP4Recorder::createFile() { closeFile(); - auto strDate = timeStr("%Y-%m-%d"); - auto strTime = timeStr("%H-%M-%S"); + auto strDate = getTimeStr("%Y-%m-%d"); + auto strTime = getTimeStr("%H-%M-%S"); auto strFileTmp = _strPath + strDate + "/." + strTime + ".mp4"; auto strFile = _strPath + strDate + "/" + strTime + ".mp4";