全面整理转协议相关配置文件

This commit is contained in:
xiongziliang
2022-11-12 23:54:35 +08:00
parent 946945ce7b
commit 9bb6a2f828
20 changed files with 373 additions and 298 deletions

View File

@@ -20,11 +20,13 @@ class HlsRecorder final : public MediaSourceEventInterceptor, public MpegMuxer,
public:
using Ptr = std::shared_ptr<HlsRecorder>;
HlsRecorder(const std::string &m3u8_file, const std::string &params) : MpegMuxer(false) {
HlsRecorder(const std::string &m3u8_file, const std::string &params, const ProtocolOption &option) : MpegMuxer(false) {
GET_CONFIG(uint32_t, hlsNum, Hls::kSegmentNum);
GET_CONFIG(bool, hlsKeep, Hls::kSegmentKeep);
GET_CONFIG(uint32_t, hlsBufSize, Hls::kFileBufSize);
GET_CONFIG(float, hlsDuration, Hls::kSegmentDuration);
_option = option;
_hls = std::make_shared<HlsMakerImp>(m3u8_file, params, hlsBufSize, hlsDuration, hlsNum, hlsKeep);
//清空上次的残余文件
_hls->clearCache();
@@ -44,10 +46,9 @@ public:
int readerCount() { return _hls->getMediaSource()->readerCount(); }
void onReaderChanged(MediaSource &sender, int size) override {
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
// hls保留切片个数为0时代表为hls录制(不删除切片)那么不管有无观看者都一直生成hls
_enabled = hls_demand ? (_hls->isLive() ? size : true) : true;
if (!size && _hls->isLive() && hls_demand) {
_enabled = _option.hls_demand ? (_hls->isLive() ? size : true) : true;
if (!size && _hls->isLive() && _option.hls_demand) {
// hls直播时如果无人观看就删除视频缓存目的是为了防止视频跳跃
_clear_cache = true;
}
@@ -55,23 +56,21 @@ public:
}
bool inputFrame(const Frame::Ptr &frame) override {
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
if (_clear_cache && hls_demand) {
if (_clear_cache && _option.hls_demand) {
_clear_cache = false;
//清空旧的m3u8索引文件于ts切片
_hls->clearCache();
_hls->getMediaSource()->setIndexFile("");
}
if (_enabled || !hls_demand) {
if (_enabled || !_option.hls_demand) {
return MpegMuxer::inputFrame(frame);
}
return false;
}
bool isEnabled() {
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
//缓存尚未清空时还允许触发inputFrame函数以便及时清空缓存
return hls_demand ? (_clear_cache ? true : _enabled) : true;
return _option.hls_demand ? (_clear_cache ? true : _enabled) : true;
}
private:
@@ -86,6 +85,7 @@ private:
private:
bool _enabled = true;
bool _clear_cache = false;
ProtocolOption _option;
std::shared_ptr<HlsMakerImp> _hls;
};
}//namespace mediakit

View File

@@ -24,7 +24,7 @@ MP4Reader::MP4Reader(const string &vhost, const string &app, const string &strea
_poller = WorkThreadPool::Instance().getPoller();
_file_path = file_path;
if (_file_path.empty()) {
GET_CONFIG(string, recordPath, Record::kFilePath);
GET_CONFIG(string, recordPath, Protocol::kMP4SavePath);
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
if (enableVhost) {
_file_path = vhost + "/" + app + "/" + stream_id;

View File

@@ -28,8 +28,8 @@ MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &
_info.stream = stream_id;
_info.vhost = vhost;
_info.folder = path;
GET_CONFIG(size_t ,recordSec,Record::kFileSecond);
_max_second = max_second ? max_second : recordSec;
GET_CONFIG(uint32_t, s_max_second, Protocol::kMP4MaxSecond);
_max_second = max_second ? max_second : s_max_second;
}
MP4Recorder::~MP4Recorder() {

View File

@@ -23,7 +23,7 @@ string Recorder::getRecordPath(Recorder::type type, const string &vhost, const s
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
switch (type) {
case Recorder::type_hls: {
GET_CONFIG(string, hlsPath, Hls::kFilePath);
GET_CONFIG(string, hlsPath, Protocol::kHlsSavePath);
string m3u8FilePath;
if (enableVhost) {
m3u8FilePath = vhost + "/" + app + "/" + stream_id + "/hls.m3u8";
@@ -37,7 +37,7 @@ string Recorder::getRecordPath(Recorder::type type, const string &vhost, const s
return File::absolutePath(m3u8FilePath, hlsPath);
}
case Recorder::type_mp4: {
GET_CONFIG(string, recordPath, Record::kFilePath);
GET_CONFIG(string, recordPath, Protocol::kMP4SavePath);
GET_CONFIG(string, recordAppName, Record::kAppName);
string mp4FilePath;
if (enableVhost) {
@@ -56,13 +56,13 @@ string Recorder::getRecordPath(Recorder::type type, const string &vhost, const s
}
}
std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path, size_t max_second){
auto path = Recorder::getRecordPath(type, vhost, app, stream_id, customized_path);
std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option){
switch (type) {
case Recorder::type_hls: {
#if defined(ENABLE_HLS)
auto path = Recorder::getRecordPath(type, vhost, app, stream_id, option.hls_save_path);
GET_CONFIG(bool, enable_vhost, General::kEnableVhost);
auto ret = std::make_shared<HlsRecorder>(path, enable_vhost ? string(VHOST_KEY) + "=" + vhost : "");
auto ret = std::make_shared<HlsRecorder>(path, enable_vhost ? string(VHOST_KEY) + "=" + vhost : "", option);
ret->setMediaSource(vhost, app, stream_id);
return ret;
#else
@@ -73,7 +73,8 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const st
case Recorder::type_mp4: {
#if defined(ENABLE_MP4)
return std::make_shared<MP4Recorder>(path, vhost, app, stream_id, max_second);
auto path = Recorder::getRecordPath(type, vhost, app, stream_id, option.mp4_save_path);
return std::make_shared<MP4Recorder>(path, vhost, app, stream_id, option.mp4_max_second);
#else
throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试");
#endif

View File

@@ -10,11 +10,13 @@
#ifndef SRC_MEDIAFILE_RECORDER_H_
#define SRC_MEDIAFILE_RECORDER_H_
#include <memory>
#include <string>
namespace mediakit {
class MediaSinkInterface;
class ProtocolOption;
class RecordInfo {
public:
@@ -60,7 +62,7 @@ public:
* @param max_second mp4录制最大切片时间单位秒置0则采用配置文件配置
* @return 对象指针可能为nullptr
*/
static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const std::string &vhost, const std::string &app, const std::string &stream_id, const std::string &customized_path = "", size_t max_second = 0);
static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const std::string &vhost, const std::string &app, const std::string &stream_id, const ProtocolOption &option);
private:
Recorder() = delete;