新增mp4点播接口loadMP4File (#3018 #2972)

This commit is contained in:
夏楚 2023-11-24 10:44:08 +08:00 committed by GitHub
parent 92e7d8837e
commit db0818c8d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 221 additions and 37 deletions

View File

@ -2048,6 +2048,142 @@
} }
}, },
"response": [] "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": [ "event": [

View File

@ -11,44 +11,52 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <math.h> #include <math.h>
#include <signal.h> #include <signal.h>
#include <functional>
#include <unordered_map>
#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 #ifdef _WIN32
#include <io.h> #include <io.h>
#include <iostream> #include <iostream>
#include <tchar.h> #include <tchar.h>
#endif // _WIN32 #endif // _WIN32
#include <functional>
#include <unordered_map>
#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) #if defined(ENABLE_VERSION)
#include "version.h" #include "version.h"
#endif #endif
@ -1777,6 +1785,23 @@ void installWebApi() {
}); });
#endif #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<MP4Reader>(allArgs["vhost"], allArgs["app"], allArgs["stream"], allArgs["file_path"], option);
// sample_ms设置为0从配置文件加载file_repeat可以指定如果配置文件也指定循环解复用那么强制开启
reader->startReadMP4(0, true, allArgs["file_repeat"]);
});
////////////以下是注册的Hook API//////////// ////////////以下是注册的Hook API////////////
api_regist("/index/hook/on_publish",[](API_ARGS_JSON){ api_regist("/index/hook/on_publish",[](API_ARGS_JSON){
//开始推流事件 //开始推流事件

View File

@ -202,6 +202,11 @@ public:
template <typename MAP> template <typename MAP>
ProtocolOption(const MAP &allArgs) : ProtocolOption() { ProtocolOption(const MAP &allArgs) : ProtocolOption() {
load(allArgs);
}
template <typename MAP>
void load(const MAP &allArgs) {
#define GET_OPT_VALUE(key) getArgsValue(allArgs, #key, key) #define GET_OPT_VALUE(key) getArgsValue(allArgs, #key, key)
GET_OPT_VALUE(modify_stamp); GET_OPT_VALUE(modify_stamp);
GET_OPT_VALUE(enable_audio); GET_OPT_VALUE(enable_audio);

View File

@ -21,6 +21,20 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std::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) {
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}; auto tuple = MediaTuple{vhost, app, stream_id};
_poller = WorkThreadPool::Instance().getPoller(); _poller = WorkThreadPool::Instance().getPoller();
@ -42,10 +56,7 @@ MP4Reader::MP4Reader(const std::string &vhost, const std::string &app, const std
if (tuple.stream.empty()) { if (tuple.stream.empty()) {
return; return;
} }
ProtocolOption option;
//读取mp4文件并流化时不重复生成mp4/hls文件
option.enable_mp4 = false;
option.enable_hls = false;
_muxer = std::make_shared<MultiMediaSourceMuxer>(tuple, _demuxer->getDurationMS() / 1000.0f, option); _muxer = std::make_shared<MultiMediaSourceMuxer>(tuple, _demuxer->getDurationMS() / 1000.0f, option);
auto tracks = _demuxer->getTracks(false); auto tracks = _demuxer->getTracks(false);
if (tracks.empty()) { if (tracks.empty()) {

View File

@ -28,7 +28,12 @@ public:
* @param stream_id id,,mp4,MediaSource * @param stream_id id,,mp4,MediaSource
* @param file_path 使 * @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; ~MP4Reader() override = default;
/** /**
@ -66,6 +71,8 @@ private:
void setCurrentStamp(uint32_t stamp); void setCurrentStamp(uint32_t stamp);
bool seekTo(uint32_t stamp_seek); 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: private:
bool _file_repeat = false; bool _file_repeat = false;
bool _have_video = false; bool _have_video = false;