Commit 5fe5dd9d by xiongziliang

hls保存ts文件时根据日期、时间保存

parent ee66d945
ZLToolKit @ ecb38246
Subproject commit 628d3b2527f63b54a5eb38b9e9973254d4a2192b Subproject commit ecb382460ffa2f6f5cdf1e11b394797e3396b2d0
...@@ -56,21 +56,31 @@ HlsMakerImp::~HlsMakerImp() { ...@@ -56,21 +56,31 @@ HlsMakerImp::~HlsMakerImp() {
} }
string HlsMakerImp::onOpenSegment(int index) { string HlsMakerImp::onOpenSegment(int index) {
auto full_path = fullPath(index); string segment_name , segment_path;
_file = makeFile(full_path, true); {
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){ 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()){ 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) { void HlsMakerImp::onDelSegment(int index) {
//WarnL << index; auto it = _segment_file_paths.find(index);
File::delete_file(fullPath(index).data()); 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) { void HlsMakerImp::onWriteSegment(const char *data, int len) {
...@@ -90,9 +100,6 @@ void HlsMakerImp::onWriteHls(const char *data, int len) { ...@@ -90,9 +100,6 @@ void HlsMakerImp::onWriteHls(const char *data, int len) {
//DebugL << "\r\n" << string(data,len); //DebugL << "\r\n" << string(data,len);
} }
string HlsMakerImp::fullPath(int index) {
return StrPrinter << _path_prefix << "/" << index << ".ts";
}
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) { std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) {
auto ret= shared_ptr<FILE>(File::createfile_file(file.data(), "wb"), [](FILE *fp) { auto ret= shared_ptr<FILE>(File::createfile_file(file.data(), "wb"), [](FILE *fp) {
......
...@@ -49,9 +49,9 @@ protected: ...@@ -49,9 +49,9 @@ protected:
void onWriteSegment(const char *data, int len) override; void onWriteSegment(const char *data, int len) override;
void onWriteHls(const char *data, int len) override; void onWriteHls(const char *data, int len) override;
private: private:
string fullPath(int index);
std::shared_ptr<FILE> makeFile(const string &file,bool setbuf = false); std::shared_ptr<FILE> makeFile(const string &file,bool setbuf = false);
private: private:
map<int /*index*/,string/*file_path*/> _segment_file_paths;
std::shared_ptr<FILE> _file; std::shared_ptr<FILE> _file;
std::shared_ptr<char> _file_buf; std::shared_ptr<char> _file_buf;
string _path_prefix; string _path_prefix;
......
...@@ -29,30 +29,12 @@ ...@@ -29,30 +29,12 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "Common/config.h" #include "Common/config.h"
#include "MP4Recorder.h" #include "MP4Recorder.h"
#include "Util/util.h"
#include "Util/NoticeCenter.h"
#include "Thread/WorkThreadPool.h" #include "Thread/WorkThreadPool.h"
using namespace toolkit; using namespace toolkit;
namespace mediakit { 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, MP4Recorder::MP4Recorder(const string& strPath,
const string &strVhost, const string &strVhost,
const string &strApp, const string &strApp,
...@@ -70,8 +52,8 @@ MP4Recorder::~MP4Recorder() { ...@@ -70,8 +52,8 @@ MP4Recorder::~MP4Recorder() {
void MP4Recorder::createFile() { void MP4Recorder::createFile() {
closeFile(); closeFile();
auto strDate = timeStr("%Y-%m-%d"); auto strDate = getTimeStr("%Y-%m-%d");
auto strTime = timeStr("%H-%M-%S"); auto strTime = getTimeStr("%H-%M-%S");
auto strFileTmp = _strPath + strDate + "/." + strTime + ".mp4"; auto strFileTmp = _strPath + strDate + "/." + strTime + ".mp4";
auto strFile = _strPath + strDate + "/" + strTime + ".mp4"; auto strFile = _strPath + strDate + "/" + strTime + ".mp4";
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论