新增支持hls-fmp4直播(#2603 #977 #1965)

同时主要优化点包括:
1、编译宏特性开关优化。
2、转协议复用器相关创建代码移动至Recorder类。
3、转协议复用器onAllTrackReady函数修改为addTrackCompleted。
4、startRecord/stopRecord/isRecording接口新增支持ts/fmp4/hls-fmp4协议。

Co-authored-by: xia-chu <771730766@qq.com>
Co-authored-by: linxiaoyan87 <linxiaoyan87@foxmail.com>
This commit is contained in:
XiaoYan Lin
2023-07-02 12:02:33 +08:00
committed by GitHub
parent 6aa4b741a3
commit cb0579a16d
27 changed files with 413 additions and 224 deletions

View File

@@ -21,21 +21,14 @@ using namespace toolkit;
namespace mediakit {
HlsMakerImp::HlsMakerImp(const string &m3u8_file,
const string &params,
uint32_t bufSize,
float seg_duration,
uint32_t seg_number,
bool seg_keep):HlsMaker(seg_duration, seg_number, seg_keep) {
HlsMakerImp::HlsMakerImp(bool is_fmp4, const string &m3u8_file, const string &params, uint32_t bufSize, float seg_duration,
uint32_t seg_number, bool seg_keep) : HlsMaker(is_fmp4, seg_duration, seg_number, seg_keep) {
_poller = EventPollerPool::Instance().getPoller();
_path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/'));
_path_hls = m3u8_file;
_params = params;
_buf_size = bufSize;
_file_buf.reset(new char[bufSize], [](char *ptr) {
delete[] ptr;
});
_file_buf.reset(new char[bufSize], [](char *ptr) { delete[] ptr; });
_info.folder = _path_prefix;
}
@@ -53,9 +46,9 @@ void HlsMakerImp::clearCache() {
}
void HlsMakerImp::clearCache(bool immediately, bool eof) {
//录制完了
// 录制完了
flushLastSegment(eof);
if (!isLive()||isKeep()) {
if (!isLive() || isKeep()) {
return;
}
@@ -63,7 +56,7 @@ void HlsMakerImp::clearCache(bool immediately, bool eof) {
_file = nullptr;
_segment_file_paths.clear();
//hls直播才删除文件
// hls直播才删除文件
GET_CONFIG(uint32_t, delay, Hls::kDeleteDelaySec);
if (!delay || immediately) {
File::delete_file(_path_prefix.data());
@@ -82,7 +75,7 @@ string HlsMakerImp::onOpenSegment(uint64_t index) {
auto strDate = getTimeStr("%Y-%m-%d");
auto strHour = getTimeStr("%H");
auto strTime = getTimeStr("%M-%S");
segment_name = StrPrinter << strDate + "/" + strHour + "/" + strTime << "_" << index << ".ts";
segment_name = StrPrinter << strDate + "/" + strHour + "/" + strTime << "_" << index << (isFmp4() ? ".mp4" : ".ts");
segment_path = _path_prefix + "/" + segment_name;
if (isLive()) {
_segment_file_paths.emplace(index, segment_path);
@@ -90,14 +83,14 @@ string HlsMakerImp::onOpenSegment(uint64_t index) {
}
_file = makeFile(segment_path, true);
//保存本切片的元数据
// 保存本切片的元数据
_info.start_time = ::time(NULL);
_info.file_name = segment_name;
_info.file_path = segment_path;
_info.url = _info.app + "/" + _info.stream + "/" + segment_name;
if (!_file) {
WarnL << "create file failed," << segment_path << " " << get_uv_errmsg();
WarnL << "Create file failed," << segment_path << " " << get_uv_errmsg();
}
if (_params.empty()) {
return segment_name;
@@ -114,6 +107,18 @@ void HlsMakerImp::onDelSegment(uint64_t index) {
_segment_file_paths.erase(it);
}
void HlsMakerImp::onWriteInitSegment(const char *data, size_t len) {
string init_seg_path = _path_prefix + "/init.mp4";
_file = makeFile(init_seg_path, true);
if (_file) {
fwrite(data, len, 1, _file.get());
_file = nullptr;
} else {
WarnL << "Create file failed," << init_seg_path << " " << get_uv_errmsg();
}
}
void HlsMakerImp::onWriteSegment(const char *data, size_t len) {
if (_file) {
fwrite(data, len, 1, _file.get());
@@ -132,13 +137,12 @@ void HlsMakerImp::onWriteHls(const std::string &data) {
_media_src->setIndexFile(data);
}
} else {
WarnL << "create hls file failed," << _path_hls << " " << get_uv_errmsg();
WarnL << "Create hls file failed," << _path_hls << " " << get_uv_errmsg();
}
//DebugL << "\r\n" << string(data,len);
}
void HlsMakerImp::onFlushLastSegment(uint64_t duration_ms) {
//关闭并flush文件到磁盘
// 关闭并flush文件到磁盘
_file = nullptr;
GET_CONFIG(bool, broadcastRecordTs, Hls::kBroadcastRecordTs);
@@ -166,11 +170,11 @@ void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const s
_info.app = app;
_info.stream = stream_id;
_info.vhost = vhost;
_media_src = std::make_shared<HlsMediaSource>(_info);
_media_src = std::make_shared<HlsMediaSource>(isFmp4() ? HLS_FMP4_SCHEMA : HLS_SCHEMA, _info);
}
HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const {
return _media_src;
}
}//namespace mediakit
} // namespace mediakit