From 781708f0a1b38640fdf481175fb030132f2fac39 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sun, 27 Dec 2020 21:48:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=85=BC=E5=AE=B9=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 111 ++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 63 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 9bf6eddc..fac9bd9d 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -145,9 +145,6 @@ static void api_regist(const string &api_path, FUNC &&func) { s_map_api.emplace(api_path, toApi(std::move(func))); } -#define api_regist1 api_regist -#define api_regist2 api_regist - //获取HTTP请求中url参数、content参数 static ApiArgsType getAllArgs(const Parser &parser) { @@ -288,7 +285,7 @@ void installWebApi() { //获取线程负载 //测试url http://127.0.0.1/index/api/getThreadsLoad - api_regist2("/index/api/getThreadsLoad",[](API_ARGS2){ + api_regist("/index/api/getThreadsLoad",[](API_ARGS2){ EventPollerPool::Instance().getExecutorDelay([invoker, headerOut](const vector &vecDelay) { Value val; auto vec = EventPollerPool::Instance().getExecutorLoad(); @@ -306,7 +303,7 @@ void installWebApi() { //获取后台工作线程负载 //测试url http://127.0.0.1/index/api/getWorkThreadsLoad - api_regist2("/index/api/getWorkThreadsLoad", [](API_ARGS2){ + api_regist("/index/api/getWorkThreadsLoad", [](API_ARGS2){ WorkThreadPool::Instance().getExecutorDelay([invoker, headerOut](const vector &vecDelay) { Value val; auto vec = WorkThreadPool::Instance().getExecutorLoad(); @@ -324,7 +321,7 @@ void installWebApi() { //获取服务器配置 //测试url http://127.0.0.1/index/api/getServerConfig - api_regist1("/index/api/getServerConfig",[](API_ARGS1){ + api_regist("/index/api/getServerConfig",[](API_ARGS1){ CHECK_SECRET(); Value obj; for (auto &pr : mINI::Instance()) { @@ -336,7 +333,7 @@ void installWebApi() { //设置服务器配置 //测试url(比如关闭http api调试) http://127.0.0.1/index/api/setServerConfig?api.apiDebug=0 //你也可以通过http post方式传参,可以通过application/x-www-form-urlencoded或application/json方式传参 - api_regist1("/index/api/setServerConfig",[](API_ARGS1){ + api_regist("/index/api/setServerConfig",[](API_ARGS1){ CHECK_SECRET(); auto &ini = mINI::Instance(); int changed = API::Success; @@ -369,20 +366,20 @@ void installWebApi() { //获取服务器api列表 //测试url http://127.0.0.1/index/api/getApiList - api_regist1("/index/api/getApiList",[](API_ARGS1){ + api_regist("/index/api/getApiList",[](API_ARGS1){ s_get_api_list(API_ARGS_VALUE1); }); //获取服务器api列表 //测试url http://127.0.0.1/index/ - api_regist1("/index/",[](API_ARGS1){ + api_regist("/index/",[](API_ARGS1){ s_get_api_list(API_ARGS_VALUE1); }); #if !defined(_WIN32) //重启服务器,只有Daemon方式才能重启,否则是直接关闭! //测试url http://127.0.0.1/index/api/restartServer - api_regist1("/index/api/restartServer",[](API_ARGS1){ + api_regist("/index/api/restartServer",[](API_ARGS1){ CHECK_SECRET(); EventPollerPool::Instance().getPoller()->doDelayTask(1000,[](){ //尝试正常退出 @@ -460,7 +457,7 @@ void installWebApi() { //测试url0(获取所有流) http://127.0.0.1/index/api/getMediaList //测试url1(获取虚拟主机为"__defaultVost__"的流) http://127.0.0.1/index/api/getMediaList?vhost=__defaultVost__ //测试url2(获取rtsp类型的流) http://127.0.0.1/index/api/getMediaList?schema=rtsp - api_regist1("/index/api/getMediaList",[](API_ARGS1){ + api_regist("/index/api/getMediaList",[](API_ARGS1){ CHECK_SECRET(); //获取所有MediaSource列表 MediaSource::for_each_media([&](const MediaSource::Ptr &media){ @@ -478,14 +475,14 @@ void installWebApi() { }); //测试url http://127.0.0.1/index/api/isMediaOnline?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs - api_regist1("/index/api/isMediaOnline",[](API_ARGS1){ + api_regist("/index/api/isMediaOnline",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("schema","vhost","app","stream"); val["online"] = (bool) (MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"])); }); //测试url http://127.0.0.1/index/api/getMediaInfo?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs - api_regist1("/index/api/getMediaInfo",[](API_ARGS1){ + api_regist("/index/api/getMediaInfo",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("schema","vhost","app","stream"); auto src = MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"]); @@ -500,7 +497,7 @@ void installWebApi() { //主动关断流,包括关断拉流、推流 //测试url http://127.0.0.1/index/api/close_stream?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1 - api_regist1("/index/api/close_stream",[](API_ARGS1){ + api_regist("/index/api/close_stream",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("schema","vhost","app","stream"); //踢掉推流器 @@ -522,7 +519,7 @@ void installWebApi() { //批量主动关断流,包括关断拉流、推流 //测试url http://127.0.0.1/index/api/close_streams?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1 - api_regist1("/index/api/close_streams",[](API_ARGS1){ + api_regist("/index/api/close_streams",[](API_ARGS1){ CHECK_SECRET(); //筛选命中个数 int count_hit = 0; @@ -558,7 +555,7 @@ void installWebApi() { //获取所有TcpSession列表信息 //可以根据本地端口和远端ip来筛选 //测试url(筛选某端口下的tcp会话) http://127.0.0.1/index/api/getAllSession?local_port=1935 - api_regist1("/index/api/getAllSession",[](API_ARGS1){ + api_regist("/index/api/getAllSession",[](API_ARGS1){ CHECK_SECRET(); Value jsession; uint16_t local_port = allArgs["local_port"].as(); @@ -583,7 +580,7 @@ void installWebApi() { //断开tcp连接,比如说可以断开rtsp、rtmp播放器等 //测试url http://127.0.0.1/index/api/kick_session?id=123456 - api_regist1("/index/api/kick_session",[](API_ARGS1){ + api_regist("/index/api/kick_session",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("id"); //踢掉tcp会话 @@ -597,7 +594,7 @@ void installWebApi() { //批量断开tcp连接,比如说可以断开rtsp、rtmp播放器等 //测试url http://127.0.0.1/index/api/kick_sessions?local_port=1935 - api_regist1("/index/api/kick_sessions",[](API_ARGS1){ + api_regist("/index/api/kick_sessions",[](API_ARGS1){ CHECK_SECRET(); uint16_t local_port = allArgs["local_port"].as(); string &peer_ip = allArgs["peer_ip"]; @@ -661,7 +658,7 @@ void installWebApi() { //动态添加rtsp/rtmp拉流代理 //测试url http://127.0.0.1/index/api/addStreamProxy?vhost=__defaultVhost__&app=proxy&enable_rtsp=1&enable_rtmp=1&stream=0&url=rtmp://127.0.0.1/live/obs - api_regist2("/index/api/addStreamProxy",[](API_ARGS2){ + api_regist("/index/api/addStreamProxy",[](API_ARGS2){ CHECK_SECRET(); CHECK_ARGS("vhost","app","stream","url"); addStreamProxy(allArgs["vhost"], @@ -684,7 +681,7 @@ void installWebApi() { //关闭拉流代理 //测试url http://127.0.0.1/index/api/delStreamProxy?key=__defaultVhost__/proxy/0 - api_regist1("/index/api/delStreamProxy",[](API_ARGS1){ + api_regist("/index/api/delStreamProxy",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("key"); lock_guard lck(s_proxyMapMtx); @@ -724,7 +721,7 @@ void installWebApi() { //动态添加rtsp/rtmp拉流代理 //测试url http://127.0.0.1/index/api/addFFmpegSource?src_url=http://live.hkstv.hk.lxdns.com/live/hks2/playlist.m3u8&dst_url=rtmp://127.0.0.1/live/hks2&timeout_ms=10000 - api_regist2("/index/api/addFFmpegSource",[](API_ARGS2){ + api_regist("/index/api/addFFmpegSource",[](API_ARGS2){ CHECK_SECRET(); CHECK_ARGS("src_url","dst_url","timeout_ms"); auto src_url = allArgs["src_url"]; @@ -745,34 +742,24 @@ void installWebApi() { }); }); - - static auto api_delFFmpegSource = [](API_ARGS1){ + //关闭拉流代理 + //测试url http://127.0.0.1/index/api/delFFmepgSource?key=key + api_regist("/index/api/delFFmpegSource",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("key"); lock_guard lck(s_ffmpegMapMtx); val["data"]["flag"] = s_ffmpegMap.erase(allArgs["key"]) == 1; - }; - - //关闭拉流代理 - //测试url http://127.0.0.1/index/api/delFFmepgSource?key=key - api_regist1("/index/api/delFFmpegSource",[](API_ARGS1){ - api_delFFmpegSource(API_ARGS_VALUE1); - }); - - //此处为了兼容之前的拼写错误 - api_regist1("/index/api/delFFmepgSource",[](API_ARGS1){ - api_delFFmpegSource(API_ARGS_VALUE1); }); //新增http api下载可执行程序文件接口 //测试url http://127.0.0.1/index/api/downloadBin - api_regist2("/index/api/downloadBin",[](API_ARGS2){ + api_regist("/index/api/downloadBin",[](API_ARGS2){ CHECK_SECRET(); invoker.responseFile(headerIn,StrCaseMap(),exePath()); }); #if defined(ENABLE_RTPPROXY) - api_regist1("/index/api/getRtpInfo",[](API_ARGS1){ + api_regist("/index/api/getRtpInfo",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("stream_id"); @@ -788,7 +775,7 @@ void installWebApi() { val["local_ip"] = process->get_local_ip(); }); - api_regist1("/index/api/openRtpServer",[](API_ARGS1){ + api_regist("/index/api/openRtpServer",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("port", "enable_tcp", "stream_id"); @@ -814,7 +801,7 @@ void installWebApi() { val["port"] = server->getPort(); }); - api_regist1("/index/api/closeRtpServer",[](API_ARGS1){ + api_regist("/index/api/closeRtpServer",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("stream_id"); @@ -829,7 +816,7 @@ void installWebApi() { val["hit"] = 1; }); - api_regist1("/index/api/listRtpServer",[](API_ARGS1){ + api_regist("/index/api/listRtpServer",[](API_ARGS1){ CHECK_SECRET(); lock_guard lck(s_rtpServerMapMtx); @@ -841,7 +828,7 @@ void installWebApi() { } }); - api_regist2("/index/api/startSendRtp",[](API_ARGS2){ + api_regist("/index/api/startSendRtp",[](API_ARGS2){ CHECK_SECRET(); CHECK_ARGS("vhost", "app", "stream", "ssrc", "dst_url", "dst_port", "is_udp"); @@ -860,7 +847,7 @@ void installWebApi() { }); }); - api_regist1("/index/api/stopSendRtp",[](API_ARGS1){ + api_regist("/index/api/stopSendRtp",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("vhost", "app", "stream"); @@ -875,7 +862,7 @@ void installWebApi() { } }); - api_regist1("/index/api/pauseRtpCheck", [](API_ARGS1) { + api_regist("/index/api/pauseRtpCheck", [](API_ARGS1) { CHECK_SECRET(); CHECK_ARGS("stream_id"); //只是暂停流的检查,流媒体服务器做为流负载服务,收流就转发,RTSP/RTMP有自己暂停协议 @@ -890,7 +877,7 @@ void installWebApi() { val["hit"] = 1; }); - api_regist1("/index/api/resumeRtpCheck", [](API_ARGS1) { + api_regist("/index/api/resumeRtpCheck", [](API_ARGS1) { CHECK_SECRET(); CHECK_ARGS("stream_id"); @@ -908,7 +895,7 @@ void installWebApi() { #endif//ENABLE_RTPPROXY // 开始录制hls或MP4 - api_regist1("/index/api/startRecord",[](API_ARGS1){ + api_regist("/index/api/startRecord",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("type","vhost","app","stream"); auto result = Recorder::startRecord((Recorder::type) allArgs["type"].as(), @@ -922,7 +909,7 @@ void installWebApi() { }); // 停止录制hls或MP4 - api_regist1("/index/api/stopRecord",[](API_ARGS1){ + api_regist("/index/api/stopRecord",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("type","vhost","app","stream"); auto result = Recorder::stopRecord((Recorder::type) allArgs["type"].as(), @@ -935,7 +922,7 @@ void installWebApi() { }); // 获取hls或MP4录制状态 - api_regist1("/index/api/isRecording",[](API_ARGS1){ + api_regist("/index/api/isRecording",[](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("type","vhost","app","stream"); val["status"] = Recorder::isRecording((Recorder::type) allArgs["type"].as(), @@ -946,7 +933,7 @@ void installWebApi() { //获取录像文件夹列表或mp4文件列表 //http://127.0.0.1/index/api/getMp4RecordFile?vhost=__defaultVhost__&app=live&stream=ss&period=2020-01 - api_regist1("/index/api/getMp4RecordFile", [](API_ARGS1){ + api_regist("/index/api/getMp4RecordFile", [](API_ARGS1){ CHECK_SECRET(); CHECK_ARGS("vhost", "app", "stream"); auto record_path = Recorder::getRecordPath(Recorder::type_mp4, allArgs["vhost"], allArgs["app"],allArgs["stream"]); @@ -1001,7 +988,7 @@ void installWebApi() { //获取截图缓存或者实时截图 //http://127.0.0.1/index/api/getSnap?url=rtmp://127.0.0.1/record/robot.mp4&timeout_sec=10&expire_sec=3 - api_regist2("/index/api/getSnap", [](API_ARGS2){ + api_regist("/index/api/getSnap", [](API_ARGS2){ CHECK_SECRET(); CHECK_ARGS("url", "timeout_sec", "expire_sec"); GET_CONFIG(string, snap_root, API::kSnapRoot); @@ -1064,7 +1051,7 @@ void installWebApi() { }); ////////////以下是注册的Hook API//////////// - api_regist1("/index/hook/on_publish",[](API_ARGS1){ + api_regist("/index/hook/on_publish",[](API_ARGS1){ //开始推流事件 //转换成rtsp或rtmp val["enableRtxp"] = true; @@ -1074,21 +1061,21 @@ void installWebApi() { val["enableMP4"] = false; }); - api_regist1("/index/hook/on_play",[](API_ARGS1){ + api_regist("/index/hook/on_play",[](API_ARGS1){ //开始播放事件 }); - api_regist1("/index/hook/on_flow_report",[](API_ARGS1){ + api_regist("/index/hook/on_flow_report",[](API_ARGS1){ //流量统计hook api }); - api_regist1("/index/hook/on_rtsp_realm",[](API_ARGS1){ + api_regist("/index/hook/on_rtsp_realm",[](API_ARGS1){ //rtsp是否需要鉴权,默认需要鉴权 val["code"] = API::Success; val["realm"] = "zlmediakit_reaml"; }); - api_regist1("/index/hook/on_rtsp_auth",[](API_ARGS1){ + api_regist("/index/hook/on_rtsp_auth",[](API_ARGS1){ //rtsp鉴权密码,密码等于用户名 //rtsp可以有双重鉴权!后面还会触发on_play事件 CHECK_ARGS("user_name"); @@ -1097,13 +1084,13 @@ void installWebApi() { val["passwd"] = allArgs["user_name"].data(); }); - api_regist1("/index/hook/on_stream_changed",[](API_ARGS1){ + api_regist("/index/hook/on_stream_changed",[](API_ARGS1){ //媒体注册或反注册事件 }); #if !defined(_WIN32) - api_regist2("/index/hook/on_stream_not_found_ffmpeg",[](API_ARGS2){ + api_regist("/index/hook/on_stream_not_found_ffmpeg",[](API_ARGS2){ //媒体未找到事件,我们都及时拉流hks作为替代品,目的是为了测试按需拉流 CHECK_SECRET(); CHECK_ARGS("vhost","app","stream"); @@ -1135,7 +1122,7 @@ void installWebApi() { }); #endif//!defined(_WIN32) - api_regist2("/index/hook/on_stream_not_found",[](API_ARGS2){ + api_regist("/index/hook/on_stream_not_found",[](API_ARGS2){ //媒体未找到事件,我们都及时拉流hks作为替代品,目的是为了测试按需拉流 CHECK_SECRET(); CHECK_ARGS("vhost","app","stream"); @@ -1159,15 +1146,15 @@ void installWebApi() { }); }); - api_regist1("/index/hook/on_record_mp4",[](API_ARGS1){ + api_regist("/index/hook/on_record_mp4",[](API_ARGS1){ //录制mp4分片完毕事件 }); - api_regist1("/index/hook/on_shell_login",[](API_ARGS1){ + api_regist("/index/hook/on_shell_login",[](API_ARGS1){ //shell登录调试事件 }); - api_regist1("/index/hook/on_stream_none_reader",[](API_ARGS1){ + api_regist("/index/hook/on_stream_none_reader",[](API_ARGS1){ //无人观看流默认关闭 val["close"] = true; }); @@ -1177,7 +1164,7 @@ void installWebApi() { return true; }; - api_regist1("/index/hook/on_http_access",[](API_ARGS1){ + api_regist("/index/hook/on_http_access",[](API_ARGS1){ //在这里根据allArgs["params"](url参数)来判断该http客户端是否有权限访问该文件 if(!checkAccess(allArgs["params"])){ //无访问权限 @@ -1198,11 +1185,9 @@ void installWebApi() { }); - api_regist1("/index/hook/on_server_started",[](API_ARGS1){ + api_regist("/index/hook/on_server_started",[](API_ARGS1){ //服务器重启报告 }); - - } void unInstallWebApi(){