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

This commit is contained in:
xiongziliang 2019-12-12 22:58:31 +08:00
parent ee66d945e8
commit 5fe5dd9de6
4 changed files with 22 additions and 33 deletions

@ -1 +1 @@
Subproject commit 628d3b2527f63b54a5eb38b9e9973254d4a2192b Subproject commit ecb382460ffa2f6f5cdf1e11b394797e3396b2d0

View File

@ -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) {
//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) {

View File

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

View File

@ -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() {
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";