diff --git a/postman/ZLMediaKit.postman_collection.json b/postman/ZLMediaKit.postman_collection.json index 784bb60c..b8bbd0f7 100644 --- a/postman/ZLMediaKit.postman_collection.json +++ b/postman/ZLMediaKit.postman_collection.json @@ -2048,6 +2048,142 @@ } }, "response": [] + }, + { + "name": "点播mp4文件(loadMP4File)", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{ZLMediaKit_URL}}/index/api/loadMP4File?secret={{ZLMediaKit_secret}}&vhost={{defaultVhost}}&app=live&stream=test&file_path=/path/to/mp4/file.mp4", + "host": [ + "{{ZLMediaKit_URL}}" + ], + "path": [ + "index", + "api", + "loadMP4File" + ], + "query": [ + { + "key": "secret", + "value": "{{ZLMediaKit_secret}}", + "description": "api操作密钥(配置文件配置)" + }, + { + "key": "vhost", + "value": "{{defaultVhost}}", + "description": "添加的流的虚拟主机,例如__defaultVhost__" + }, + { + "key": "app", + "value": "live", + "description": "添加的流的应用名,例如live" + }, + { + "key": "stream", + "value": "test", + "description": "添加的流的id名,例如test" + }, + { + "key": "file_path", + "value": "/path/to/mp4/file.mp4", + "description": "mp4文件绝对路径" + }, + { + "key": "file_repeat", + "value": "1", + "description": "是否循环点播mp4文件,如果配置文件已经开启循环点播,此参数无效", + "disabled": true + }, + { + "key": "enable_hls", + "value": "", + "description": "是否转hls-ts", + "disabled": true + }, + { + "key": "enable_hls_fmp4", + "value": "", + "description": "是否转hls-fmp4", + "disabled": true + }, + { + "key": "enable_mp4", + "value": "", + "description": "是否mp4录制,默认不开启(覆盖配置文件)", + "disabled": true + }, + { + "key": "enable_rtsp", + "value": "1", + "description": "是否转协议为rtsp/webrtc", + "disabled": true + }, + { + "key": "enable_rtmp", + "value": "1", + "description": "是否转协议为rtmp/flv", + "disabled": true + }, + { + "key": "enable_ts", + "value": "1", + "description": "是否转协议为http-ts/ws-ts", + "disabled": true + }, + { + "key": "enable_fmp4", + "value": "1", + "description": "是否转协议为http-fmp4/ws-fmp4", + "disabled": true + }, + { + "key": "enable_audio", + "value": "1", + "description": "转协议是否开启音频", + "disabled": true + }, + { + "key": "add_mute_audio", + "value": "1", + "description": "转协议无音频时,是否添加静音aac音频", + "disabled": true + }, + { + "key": "mp4_save_path", + "value": "", + "description": "mp4录制保存根目录,置空使用默认目录", + "disabled": true + }, + { + "key": "mp4_max_second", + "value": "1800", + "description": "mp4录制切片大小,单位秒", + "disabled": true + }, + { + "key": "hls_save_path", + "value": "", + "description": "hls保存根目录,置空使用默认目录", + "disabled": true + }, + { + "key": "modify_stamp", + "value": "", + "description": "是否修改原始时间戳,默认值2;取值范围:0.采用源视频流绝对时间戳,不做任何改变;1.采用zlmediakit接收数据时的系统时间戳(有平滑处理);2.采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正", + "disabled": true + }, + { + "key": "auto_close", + "value": "", + "description": "无人观看时,是否直接关闭(而不是通过on_none_reader hook返回close);强制开启,此参数不生效", + "disabled": true + } + ] + } + }, + "response": [] } ], "event": [ diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 63b4d5e3..8bd2f416 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -11,44 +11,52 @@ #include #include #include -#include -#include -#include "Util/util.h" -#include "Util/logger.h" -#include "Util/onceToken.h" -#include "Util/NoticeCenter.h" -#include "Util/File.h" -#ifdef ENABLE_MYSQL -#include "Util/SqlPool.h" -#endif //ENABLE_MYSQL -#include "Common/config.h" -#include "Common/MediaSource.h" -#include "Http/HttpRequester.h" -#include "Http/HttpSession.h" -#include "Network/TcpServer.h" -#include "Network/UdpServer.h" -#include "Player/PlayerProxy.h" -#include "Pusher/PusherProxy.h" -#include "Util/MD5.h" -#include "WebApi.h" -#include "WebHook.h" -#include "Thread/WorkThreadPool.h" -#include "Rtp/RtpSelector.h" -#include "FFmpegSource.h" -#if defined(ENABLE_RTPPROXY) -#include "Rtp/RtpServer.h" -#endif -#ifdef ENABLE_WEBRTC -#include "../webrtc/WebRtcPlayer.h" -#include "../webrtc/WebRtcPusher.h" -#include "../webrtc/WebRtcEchoTest.h" -#endif + #ifdef _WIN32 #include #include #include #endif // _WIN32 +#include +#include +#include "Util/MD5.h" +#include "Util/util.h" +#include "Util/File.h" +#include "Util/logger.h" +#include "Util/onceToken.h" +#include "Util/NoticeCenter.h" +#include "Network/TcpServer.h" +#include "Network/UdpServer.h" +#include "Thread/WorkThreadPool.h" + +#ifdef ENABLE_MYSQL +#include "Util/SqlPool.h" +#endif //ENABLE_MYSQL + +#include "WebApi.h" +#include "WebHook.h" +#include "FFmpegSource.h" + +#include "Common/config.h" +#include "Common/MediaSource.h" +#include "Http/HttpSession.h" +#include "Http/HttpRequester.h" +#include "Player/PlayerProxy.h" +#include "Pusher/PusherProxy.h" +#include "Rtp/RtpSelector.h" +#include "Record/MP4Reader.h" + +#if defined(ENABLE_RTPPROXY) +#include "Rtp/RtpServer.h" +#endif + +#ifdef ENABLE_WEBRTC +#include "../webrtc/WebRtcPlayer.h" +#include "../webrtc/WebRtcPusher.h" +#include "../webrtc/WebRtcEchoTest.h" +#endif + #if defined(ENABLE_VERSION) #include "version.h" #endif @@ -1777,6 +1785,23 @@ void installWebApi() { }); #endif + api_regist("/index/api/loadMP4File", [](API_ARGS_MAP) { + CHECK_SECRET(); + CHECK_ARGS("vhost", "app", "stream", "file_path"); + + ProtocolOption option; + // 默认解复用mp4不生成mp4 + option.enable_mp4 = false; + // 但是如果参数明确指定开启mp4, 那么也允许之 + option.load(allArgs); + // 强制无人观看时自动关闭 + option.auto_close = true; + + auto reader = std::make_shared(allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["file_path"], option); + // sample_ms设置为0,从配置文件加载;file_repeat可以指定,如果配置文件也指定循环解复用,那么强制开启 + reader->startReadMP4(0, true, allArgs["file_repeat"]); + }); + ////////////以下是注册的Hook API//////////// api_regist("/index/hook/on_publish",[](API_ARGS_JSON){ //开始推流事件 diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 73306385..9721210a 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -202,6 +202,11 @@ public: template ProtocolOption(const MAP &allArgs) : ProtocolOption() { + load(allArgs); + } + + template + void load(const MAP &allArgs) { #define GET_OPT_VALUE(key) getArgsValue(allArgs, #key, key) GET_OPT_VALUE(modify_stamp); GET_OPT_VALUE(enable_audio); diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index 2def3781..d1f408dd 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -21,6 +21,20 @@ using namespace toolkit; namespace mediakit { MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, const string &file_path) { + ProtocolOption option; + // 读取mp4文件并流化时,不重复生成mp4/hls文件 + option.enable_mp4 = false; + option.enable_hls = false; + option.enable_hls_fmp4 = false; + + setup(vhost, app, stream_id, file_path, option); +} + +MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, const string &file_path, const ProtocolOption &option) { + setup(vhost, app, stream_id, file_path, option); +} + +void MP4Reader::setup(const std::string &vhost, const std::string &app, const std::string &stream_id, const std::string &file_path, const ProtocolOption &option) { //读写文件建议放在后台线程 auto tuple = MediaTuple{vhost, app, stream_id}; _poller = WorkThreadPool::Instance().getPoller(); @@ -42,10 +56,7 @@ MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std if (tuple.stream.empty()) { return; } - ProtocolOption option; - //读取mp4文件并流化时,不重复生成mp4/hls文件 - option.enable_mp4 = false; - option.enable_hls = false; + _muxer = std::make_shared(tuple, _demuxer->getDurationMS() / 1000.0f, option); auto tracks = _demuxer->getTracks(false); if (tracks.empty()) { diff --git a/src/Record/MP4Reader.h b/src/Record/MP4Reader.h index b2cb87ed..ac07e9e8 100644 --- a/src/Record/MP4Reader.h +++ b/src/Record/MP4Reader.h @@ -28,7 +28,12 @@ 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 = ""); + MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, + const std::string &file_path = ""); + + MP4Reader(const std::string &vhost, const std::string &app, const std::string &stream_id, + const std::string &file_path, const ProtocolOption &option); + ~MP4Reader() override = default; /** @@ -66,6 +71,8 @@ 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); + private: bool _file_repeat = false; bool _have_video = false;