PlayerProxy和Mp4Reader构造函数MediaTuple重构

This commit is contained in:
cqm 2024-07-12 17:04:05 +08:00
parent 3e7e1a317e
commit f5a8226bbc
15 changed files with 46 additions and 44 deletions

View File

@ -28,7 +28,8 @@ API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create3(const char *vhost, c
ProtocolOption option;
option.enable_hls = hls_enabled;
option.enable_mp4 = mp4_enabled;
PlayerProxy::Ptr *obj(new PlayerProxy::Ptr(new PlayerProxy(vhost, app, stream, option, retry_count)));
MediaTuple tuple = {vhost, app, stream, ""};
PlayerProxy::Ptr *obj(new PlayerProxy::Ptr(new PlayerProxy(tuple, option, retry_count)));
return (mk_proxy_player)obj;
}
@ -36,7 +37,8 @@ API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create3(const char *vhost, c
API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create4(const char *vhost, const char *app, const char *stream, mk_ini ini, int retry_count) {
assert(vhost && app && stream);
ProtocolOption option(*((mINI *)ini));
PlayerProxy::Ptr *obj(new PlayerProxy::Ptr(new PlayerProxy(vhost, app, stream, option, retry_count)));
MediaTuple tuple = {vhost, app, stream, ""};
PlayerProxy::Ptr *obj(new PlayerProxy::Ptr(new PlayerProxy(tuple, option, retry_count)));
return (mk_proxy_player)obj;
}

View File

@ -376,9 +376,6 @@ static ServiceController<FFmpegSource> s_ffmpeg_src;
static ServiceController<RtpServer> s_rtp_server;
#endif
static inline string getProxyKey(const string &vhost, const string &app, const string &stream) {
return vhost + "/" + app + "/" + stream;
}
static inline string getPusherKey(const string &schema, const string &vhost, const string &app, const string &stream,
const string &dst_url) {
@ -582,17 +579,17 @@ void getStatisticJson(const function<void(Value &val)> &cb) {
#endif
}
void addStreamProxy(const string &vhost, const string &app, const string &stream, const string &url, int retry_count,
void addStreamProxy(const MediaTuple &tuple, const string &url, int retry_count,
const ProtocolOption &option, int rtp_type, float timeout_sec, const mINI &args,
const function<void(const SockException &ex, const string &key)> &cb) {
auto key = getProxyKey(vhost, app, stream);
auto key = tuple.shortUrl();
if (s_player_proxy.find(key)) {
//已经在拉流了
cb(SockException(Err_other, "This stream already exists"), key);
return;
}
//添加拉流代理
auto player = s_player_proxy.make(key, vhost, app, stream, option, retry_count);
auto player = s_player_proxy.make(key, tuple, option, retry_count);
// 先透传拷贝参数
for (auto &pr : args) {
@ -1102,9 +1099,13 @@ void installWebApi() {
ProtocolOption option(allArgs);
auto retry_count = allArgs["retry_count"].empty()? -1: allArgs["retry_count"].as<int>();
addStreamProxy(allArgs["vhost"],
allArgs["app"],
allArgs["stream"],
std::string vhost = DEFAULT_VHOST;
if (!allArgs["vhost"].empty()) {
vhost = allArgs["vhost"];
}
auto tuple = MediaTuple { vhost, allArgs["app"], allArgs["stream"], "" };
addStreamProxy(tuple,
allArgs["url"],
retry_count,
option,
@ -1963,8 +1964,8 @@ void installWebApi() {
option.load(allArgs);
// 强制无人观看时自动关闭
option.auto_close = true;
auto reader = std::make_shared<MP4Reader>(allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["file_path"], option);
auto tuple = MediaTuple{allArgs["vhost"], allArgs["app"], allArgs["stream"], ""};
auto reader = std::make_shared<MP4Reader>(tuple, allArgs["file_path"], option);
// sample_ms设置为0从配置文件加载file_repeat可以指定如果配置文件也指定循环解复用那么强制开启
reader->startReadMP4(0, true, allArgs["file_repeat"]);
});

View File

@ -207,7 +207,7 @@ uint16_t openRtpServer(uint16_t local_port, const mediakit::MediaTuple &tuple, i
Json::Value makeMediaSourceJson(mediakit::MediaSource &media);
void getStatisticJson(const std::function<void(Json::Value &val)> &cb);
void addStreamProxy(const std::string &vhost, const std::string &app, const std::string &stream, const std::string &url, int retry_count,
void addStreamProxy(const mediakit::MediaTuple &tuple, const std::string &url, int retry_count,
const mediakit::ProtocolOption &option, int rtp_type, float timeout_sec, const toolkit::mINI &args,
const std::function<void(const toolkit::SockException &ex, const std::string &key)> &cb);
#endif //ZLMEDIAKIT_WEBAPI_H

View File

@ -301,7 +301,7 @@ static void pullStreamFromOrigin(const vector<string> &urls, size_t index, size_
option.enable_hls = option.enable_hls || (args.schema == HLS_SCHEMA);
option.enable_mp4 = false;
addStreamProxy(args.vhost, args.app, args.stream, url, retry_count, option, Rtsp::RTP_TCP, timeout_sec, mINI{}, [=](const SockException &ex, const string &key) mutable {
addStreamProxy(args, url, retry_count, option, Rtsp::RTP_TCP, timeout_sec, mINI{}, [=](const SockException &ex, const string &key) mutable {
if (!ex) {
return;
}

View File

@ -595,7 +595,8 @@ MediaSource::Ptr MediaSource::createFromMP4(const string &schema, const string &
}
#ifdef ENABLE_MP4
try {
auto reader = std::make_shared<MP4Reader>(vhost, app, stream, file_path);
MediaTuple tuple = {vhost, app, stream, ""};
auto reader = std::make_shared<MP4Reader>(tuple, file_path);
reader->startReadMP4();
return MediaSource::find(schema, vhost, app, stream);
} catch (std::exception &ex) {

View File

@ -24,13 +24,9 @@ using namespace std;
namespace mediakit {
PlayerProxy::PlayerProxy(
const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option, int retry_count,
const MediaTuple &tuple, const ProtocolOption &option, int retry_count,
const EventPoller::Ptr &poller, int reconnect_delay_min, int reconnect_delay_max, int reconnect_delay_step)
: MediaPlayer(poller)
, _option(option) {
_tuple.vhost = vhost;
_tuple.app = app;
_tuple.stream = stream_id;
: MediaPlayer(poller), _tuple(tuple), _option(option) {
_retry_count = retry_count;
setOnClose(nullptr);

View File

@ -66,9 +66,9 @@ public:
// 如果retry_count<0,则一直重试播放否则重试retry_count次数
// 默认一直重试
PlayerProxy(
const std::string &vhost, const std::string &app, const std::string &stream_id, const ProtocolOption &option, int retry_count = -1,
const toolkit::EventPoller::Ptr &poller = nullptr, int reconnect_delay_min = 2, int reconnect_delay_max = 60, int reconnect_delay_step = 3);
PlayerProxy(const MediaTuple &tuple, const ProtocolOption &option, int retry_count = -1,
const toolkit::EventPoller::Ptr &poller = nullptr,
int reconnect_delay_min = 2, int reconnect_delay_max = 60, int reconnect_delay_step = 3);
~PlayerProxy() override;

View File

@ -20,7 +20,7 @@ using namespace toolkit;
namespace mediakit {
MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, const string &file_path,
MP4Reader::MP4Reader(const MediaTuple &tuple, const string &file_path,
toolkit::EventPoller::Ptr poller) {
ProtocolOption option;
// 读取mp4文件并流化时不重复生成mp4/hls文件
@ -29,16 +29,15 @@ MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std
option.enable_hls_fmp4 = false;
// mp4支持多track
option.max_track = 16;
setup(vhost, app, stream_id, file_path, option, std::move(poller));
setup(tuple, file_path, option, std::move(poller));
}
MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, const string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller) {
setup(vhost, app, stream_id, file_path, option, std::move(poller));
MP4Reader::MP4Reader(const MediaTuple &tuple, const string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller) {
setup(tuple, file_path, option, std::move(poller));
}
void MP4Reader::setup(const std::string &vhost, const std::string &app, const std::string &stream_id, const std::string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller) {
void MP4Reader::setup(const MediaTuple &tuple, const std::string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller) {
//读写文件建议放在后台线程
auto tuple = MediaTuple{vhost, app, stream_id, ""};
_poller = poller ? std::move(poller) : WorkThreadPool::Instance().getPoller();
_file_path = file_path;
if (_file_path.empty()) {

View File

@ -28,11 +28,9 @@ public:
* @param stream_id id,,mp4,MediaSource
* @param file_path 使
*/
MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id,
const std::string &file_path = "", toolkit::EventPoller::Ptr poller = nullptr);
MP4Reader(const MediaTuple &tuple, const std::string &file_path = "", toolkit::EventPoller::Ptr poller = nullptr);
MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id,
const std::string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller = nullptr);
MP4Reader(const MediaTuple &tuple, const std::string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller = nullptr);
/**
* MP4文件
@ -69,7 +67,7 @@ private:
void setCurrentStamp(uint32_t stamp);
bool seekTo(uint32_t stamp_seek);
void setup(const std::string &vhost, const std::string &app, const std::string &stream_id, const std::string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller);
void setup(const MediaTuple &tuple, const std::string &file_path, const ProtocolOption &option, toolkit::EventPoller::Ptr poller);
private:
bool _file_repeat = false;

View File

@ -224,7 +224,8 @@ int main(int argc, char *argv[]) {
option.enable_mp4 = false;
option.modify_stamp = (int)ProtocolOption::kModifyStampRelative;
//添加拉流代理
auto proxy = std::make_shared<PlayerProxy>(DEFAULT_VHOST, "app", std::to_string(i), option, -1, nullptr, 1);
auto tuple = MediaTuple{DEFAULT_VHOST, "app", std::to_string(i)};
auto proxy = std::make_shared<PlayerProxy>(tuple, option, -1, nullptr, 1);
//开始拉流代理
proxy->play(input_urls[i]);
proxy_map.emplace(i, std::move(proxy));

View File

@ -137,7 +137,8 @@ int main(int argc, char *argv[]) {
option.enable_mp4 = false;
for (auto i = 0; i < proxy_count; ++i) {
auto stream = to_string(i);
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "live", stream, option));
auto tuple = MediaTuple{DEFAULT_VHOST, "live", stream, ""};
PlayerProxy::Ptr player(new PlayerProxy(tuple, option));
(*player)[Client::kRtpType] = rtp_type;
player->play(in_url);
proxyMap.emplace(stream, player);

View File

@ -148,9 +148,10 @@ int main(int argc, char *argv[]) {
MediaSource::Ptr src = nullptr;
PlayerProxy::Ptr proxy = nullptr;;
auto tuple = MediaTuple{DEFAULT_VHOST, app, stream};
if (end_with(in_url, ".mp4")) {
// create MediaSource from mp4file
auto reader = std::make_shared<MP4Reader>(DEFAULT_VHOST, app, stream, in_url);
auto reader = std::make_shared<MP4Reader>(tuple, in_url);
//mp4 repeat
reader->startReadMP4(0, true, true);
src = MediaSource::find(schema, DEFAULT_VHOST, app, stream, false);
@ -161,7 +162,7 @@ int main(int argc, char *argv[]) {
}
} else {
//添加拉流代理
proxy = std::make_shared<PlayerProxy>(DEFAULT_VHOST, app, stream, option);
proxy = std::make_shared<PlayerProxy>(tuple, option);
//rtsp拉流代理方式
(*proxy)[Client::kRtpType] = rtp_type;
//开始拉流代理

View File

@ -79,7 +79,8 @@ int domain(const string &playUrl, const string &pushUrl) {
ProtocolOption option;
option.enable_hls = false;
option.enable_mp4 = false;
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream", option, -1, poller));
auto tuple = MediaTuple{DEFAULT_VHOST, "app", "stream", ""};
PlayerProxy::Ptr player(new PlayerProxy(tuple, option, -1, poller));
//可以指定rtsp拉流方式支持tcp和udp方式默认tcp
// (*player)[Client::kRtpType] = Rtsp::RTP_UDP;
player->play(playUrl.data());

View File

@ -43,7 +43,8 @@ int domain(const string &file, const string &url) {
mINI::Instance()["protocol.enable_" + schema] = 1;
// 从mp4文件加载生成MediaSource对象
auto reader = std::make_shared<MP4Reader>(DEFAULT_VHOST, "live", "stream", file);
auto tuple = MediaTuple {DEFAULT_VHOST, "live", "stream", ""};
auto reader = std::make_shared<MP4Reader>(tuple, file);
// 开始加载mp4ref_self设置为false这样reader对象设置为nullptr就能注销了file_repeat可以设置为空这样文件读完了就停止推流了
reader->startReadMP4(100, false, true);
auto src = MediaSource::find(schema, DEFAULT_VHOST, "live", "stream", false);

View File

@ -230,8 +230,8 @@ int main(int argc,char *argv[]) {
//http://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
//rtsp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
//rtmp://127.0.0.1/record/live/0/2017-04-11/11-09-38.mp4
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "live", std::string("chn") + to_string(i).data(), ProtocolOption()));
auto tuple = MediaTuple{DEFAULT_VHOST, "live", std::string("chn") + to_string(i).data(), ""};
PlayerProxy::Ptr player(new PlayerProxy(tuple, ProtocolOption()));
//指定RTP over TCP(播放rtsp时有效)
(*player)[Client::kRtpType] = Rtsp::RTP_TCP;
//开始播放,如果播放失败或者播放中止,将会自动重试若干次,重试次数在配置文件中配置,默认一直重试