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";