add MediaTuple

This commit is contained in:
Johnny
2023-05-25 16:23:24 +08:00
committed by 夏楚
parent f4ee607feb
commit 0232caf068
41 changed files with 242 additions and 273 deletions

View File

@@ -163,10 +163,10 @@ std::shared_ptr<FILE> HlsMakerImp::makeFile(const string &file, bool setbuf) {
}
void HlsMakerImp::setMediaSource(const string &vhost, const string &app, const string &stream_id) {
_media_src = std::make_shared<HlsMediaSource>(vhost, app, stream_id);
_info.app = app;
_info.stream = stream_id;
_info.vhost = vhost;
_media_src = std::make_shared<HlsMediaSource>(_info);
}
HlsMediaSource::Ptr HlsMakerImp::getMediaSource() const {

View File

@@ -25,8 +25,7 @@ public:
using RingType = toolkit::RingBuffer<std::string>;
using Ptr = std::shared_ptr<HlsMediaSource>;
HlsMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id)
: MediaSource(HLS_SCHEMA, vhost, app, stream_id) {}
HlsMediaSource(const MediaTuple& tuple): MediaSource(HLS_SCHEMA, tuple) {}
~HlsMediaSource() override = default;
/**

View File

@@ -35,8 +35,8 @@ public:
~HlsRecorder() { MpegMuxer::flush(); };
void setMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id) {
_hls->setMediaSource(vhost, app, stream_id);
void setMediaSource(const MediaTuple& tuple) {
_hls->setMediaSource(tuple.vhost, tuple.app, tuple.stream);
}
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) {

View File

@@ -20,17 +20,18 @@ using namespace toolkit;
namespace mediakit {
MP4Reader::MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path) {
MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, const string &file_path) {
//读写文件建议放在后台线程
auto tuple = MediaTuple{vhost, app, stream_id};
_poller = WorkThreadPool::Instance().getPoller();
_file_path = file_path;
if (_file_path.empty()) {
GET_CONFIG(string, recordPath, Protocol::kMP4SavePath);
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
if (enableVhost) {
_file_path = vhost + "/" + app + "/" + stream_id;
_file_path = tuple.shortUrl();
} else {
_file_path = app + "/" + stream_id;
_file_path = tuple.app + "/" + tuple.stream;
}
_file_path = File::absolutePath(_file_path, recordPath);
}
@@ -38,14 +39,14 @@ MP4Reader::MP4Reader(const string &vhost, const string &app, const string &strea
_demuxer = std::make_shared<MP4Demuxer>();
_demuxer->openMP4(_file_path);
if (stream_id.empty()) {
if (tuple.stream.empty()) {
return;
}
ProtocolOption option;
//读取mp4文件并流化时不重复生成mp4/hls文件
option.enable_mp4 = false;
option.enable_hls = false;
_muxer = std::make_shared<MultiMediaSourceMuxer>(vhost, app, stream_id, _demuxer->getDurationMS() / 1000.0f, option);
_muxer = std::make_shared<MultiMediaSourceMuxer>(tuple, _demuxer->getDurationMS() / 1000.0f, option);
auto tracks = _demuxer->getTracks(false);
if (tracks.empty()) {
throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path);

View File

@@ -21,16 +21,16 @@ using namespace toolkit;
namespace mediakit {
string Recorder::getRecordPath(Recorder::type type, const string &vhost, const string &app, const string &stream_id, const string &customized_path) {
string Recorder::getRecordPath(Recorder::type type, const MediaTuple& tuple, const string &customized_path) {
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
switch (type) {
case Recorder::type_hls: {
GET_CONFIG(string, hlsPath, Protocol::kHlsSavePath);
string m3u8FilePath;
if (enableVhost) {
m3u8FilePath = vhost + "/" + app + "/" + stream_id + "/hls.m3u8";
m3u8FilePath = tuple.shortUrl() + "/hls.m3u8";
} else {
m3u8FilePath = app + "/" + stream_id + "/hls.m3u8";
m3u8FilePath = tuple.app + "/" + tuple.stream + "/hls.m3u8";
}
//Here we use the customized file path.
if (!customized_path.empty()) {
@@ -43,9 +43,9 @@ string Recorder::getRecordPath(Recorder::type type, const string &vhost, const s
GET_CONFIG(string, recordAppName, Record::kAppName);
string mp4FilePath;
if (enableVhost) {
mp4FilePath = vhost + "/" + recordAppName + "/" + app + "/" + stream_id + "/";
mp4FilePath = tuple.vhost + "/" + recordAppName + "/" + tuple.app + "/" + tuple.stream + "/";
} else {
mp4FilePath = recordAppName + "/" + app + "/" + stream_id + "/";
mp4FilePath = recordAppName + "/" + tuple.app + "/" + tuple.stream + "/";
}
//Here we use the customized file path.
if (!customized_path.empty()) {
@@ -58,14 +58,14 @@ 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 ProtocolOption &option){
std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const MediaTuple& tuple, 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);
auto path = Recorder::getRecordPath(type, tuple, option.hls_save_path);
GET_CONFIG(bool, enable_vhost, General::kEnableVhost);
auto ret = std::make_shared<HlsRecorder>(path, enable_vhost ? string(VHOST_KEY) + "=" + vhost : "", option);
ret->setMediaSource(vhost, app, stream_id);
auto ret = std::make_shared<HlsRecorder>(path, enable_vhost ? string(VHOST_KEY) + "=" + tuple.vhost : "", option);
ret->setMediaSource(tuple);
return ret;
#else
throw std::invalid_argument("hls相关功能未打开请开启ENABLE_HLS宏后编译再测试");
@@ -75,8 +75,8 @@ std::shared_ptr<MediaSinkInterface> Recorder::createRecorder(type type, const st
case Recorder::type_mp4: {
#if defined(ENABLE_MP4)
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);
auto path = Recorder::getRecordPath(type, tuple, option.mp4_save_path);
return std::make_shared<MP4Recorder>(path, tuple.vhost, tuple.app, tuple.stream, option.mp4_max_second);
#else
throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试");
#endif

View File

@@ -18,7 +18,16 @@ namespace mediakit {
class MediaSinkInterface;
class ProtocolOption;
class RecordInfo {
struct MediaTuple {
std::string vhost;
std::string app;
std::string stream;
std::string shortUrl() const {
return vhost + '/' + app + '/' + stream;
}
};
class RecordInfo: public MediaTuple {
public:
time_t start_time; // GMT 标准时间,单位秒
float time_len; // 录像长度,单位秒
@@ -27,9 +36,6 @@ public:
std::string file_name; // 文件名称
std::string folder; // 文件夹路径
std::string url; // 播放路径
std::string app; // 应用名称
std::string stream; // 流 ID
std::string vhost; // 虚拟主机
};
class Recorder{
@@ -50,7 +56,7 @@ public:
* @param customized_path 录像文件保存自定义根目录,为空则采用配置文件设置
* @return 录制文件绝对路径
*/
static std::string getRecordPath(type type, const std::string &vhost, const std::string &app, const std::string &stream_id,const std::string &customized_path = "");
static std::string getRecordPath(type type, const MediaTuple& tuple, const std::string &customized_path = "");
/**
* 创建录制器对象
@@ -62,7 +68,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 ProtocolOption &option);
static std::shared_ptr<MediaSinkInterface> createRecorder(type type, const MediaTuple& tuple, const ProtocolOption &option);
private:
Recorder() = delete;