完善按需转协议(包括hls)

This commit is contained in:
xiongziliang
2020-09-12 19:20:18 +08:00
parent 268a7fec10
commit be1e872f0c
13 changed files with 225 additions and 124 deletions

View File

@@ -11,6 +11,7 @@
#include "HlsMakerImp.h"
#include "Util/util.h"
#include "Util/uv_errno.h"
using namespace toolkit;
namespace mediakit {
@@ -24,37 +25,44 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file,
_path_hls = m3u8_file;
_params = params;
_buf_size = bufSize;
_file_buf.reset(new char[bufSize],[](char *ptr){
_file_buf.reset(new char[bufSize], [](char *ptr) {
delete[] ptr;
});
}
HlsMakerImp::~HlsMakerImp() {
clearCache();
}
void HlsMakerImp::clearCache() {
//录制完了
flushLastSegment(true);
if(isLive()){
if (isLive()) {
//hls直播才删除文件
clear();
_file = nullptr;
_segment_file_paths.clear();
File::delete_file(_path_prefix.data());
}
}
string HlsMakerImp::onOpenSegment(int index) {
string segment_name , segment_path;
string segment_name, segment_path;
{
auto strDate = getTimeStr("%Y-%m-%d");
auto strHour = getTimeStr("%H");
auto strTime = getTimeStr("%M-%S");
segment_name = StrPrinter << strDate + "/" + strHour + "/" + strTime << "_" << index << ".ts";
segment_path = _path_prefix + "/" + segment_name;
if(isLive()){
_segment_file_paths.emplace(index,segment_path);
segment_path = _path_prefix + "/" + segment_name;
if (isLive()) {
_segment_file_paths.emplace(index, segment_path);
}
}
_file = makeFile(segment_path, true);
if(!_file){
WarnL << "create file falied," << segment_path << " " << get_uv_errmsg();
if (!_file) {
WarnL << "create file failed," << segment_path << " " << get_uv_errmsg();
}
if(_params.empty()){
if (_params.empty()) {
return std::move(segment_name);
}
return std::move(segment_name + "?" + _params);
@@ -62,7 +70,7 @@ string HlsMakerImp::onOpenSegment(int index) {
void HlsMakerImp::onDelSegment(int index) {
auto it = _segment_file_paths.find(index);
if(it == _segment_file_paths.end()){
if (it == _segment_file_paths.end()) {
return;
}
File::delete_file(it->second.data());
@@ -77,27 +85,27 @@ void HlsMakerImp::onWriteSegment(const char *data, int len) {
void HlsMakerImp::onWriteHls(const char *data, int len) {
auto hls = makeFile(_path_hls);
if(hls){
fwrite(data,len,1,hls.get());
if (hls) {
fwrite(data, len, 1, hls.get());
hls.reset();
if(_media_src){
if (_media_src) {
_media_src->registHls();
}
} else{
WarnL << "create hls file falied," << _path_hls << " " << get_uv_errmsg();
} else {
WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg();
}
//DebugL << "\r\n" << string(data,len);
}
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file,bool setbuf) {
std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file, bool setbuf) {
auto file_buf = _file_buf;
auto ret= shared_ptr<FILE>(File::create_file(file.data(), "wb"), [file_buf](FILE *fp) {
auto ret = shared_ptr<FILE>(File::create_file(file.data(), "wb"), [file_buf](FILE *fp) {
if (fp) {
fclose(fp);
}
});
if(ret && setbuf){
if (ret && setbuf) {
setvbuf(ret.get(), _file_buf.get(), _IOFBF, _buf_size);
}
return ret;
@@ -107,7 +115,7 @@ void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const s
_media_src = std::make_shared<HlsMediaSource>(vhost, app, stream_id);
}
MediaSource::Ptr HlsMakerImp::getMediaSource() const{
HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const {
return _media_src;
}