From 67e1b1f194c79db9d6666bc8f7f1a298d94efbe7 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Thu, 8 Apr 2021 17:34:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AA=92=E4=BD=93=E6=B3=A8=E5=86=8Chook?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=96=E7=A0=81=E7=AD=89=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 117 ++++++++++++++++++++++----------------------- server/WebApi.h | 2 +- server/WebHook.cpp | 13 +++-- 3 files changed, 68 insertions(+), 64 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index a70b12d1..ad0c7bd8 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -247,6 +247,61 @@ static inline string getProxyKey(const string &vhost,const string &app,const str return vhost + "/" + app + "/" + stream; } +Value makeMediaSourceJson(MediaSource &media){ + Value item; + item["schema"] = media.getSchema(); + item["vhost"] = media.getVhost(); + item["app"] = media.getApp(); + item["stream"] = media.getId(); + item["createStamp"] = (Json::UInt64) media.getCreateStamp(); + item["aliveSecond"] = (Json::UInt64) media.getAliveSecond(); + item["bytesSpeed"] = media.getBytesSpeed(); + item["readerCount"] = media.readerCount(); + item["totalReaderCount"] = media.totalReaderCount(); + item["originType"] = (int) media.getOriginType(); + item["originTypeStr"] = getOriginTypeString(media.getOriginType()); + item["originUrl"] = media.getOriginUrl(); + auto originSock = media.getOriginSock(); + if (originSock) { + item["originSock"]["local_ip"] = originSock->get_local_ip(); + item["originSock"]["local_port"] = originSock->get_local_port(); + item["originSock"]["peer_ip"] = originSock->get_peer_ip(); + item["originSock"]["peer_port"] = originSock->get_peer_port(); + item["originSock"]["identifier"] = originSock->getIdentifier(); + } else { + item["originSock"] = Json::nullValue; + } + + for(auto &track : media.getTracks()){ + Value obj; + auto codec_type = track->getTrackType(); + obj["codec_id"] = track->getCodecId(); + obj["codec_id_name"] = track->getCodecName(); + obj["ready"] = track->ready(); + obj["codec_type"] = codec_type; + switch(codec_type){ + case TrackAudio : { + auto audio_track = dynamic_pointer_cast(track); + obj["sample_rate"] = audio_track->getAudioSampleRate(); + obj["channels"] = audio_track->getAudioChannel(); + obj["sample_bit"] = audio_track->getAudioSampleBit(); + break; + } + case TrackVideo : { + auto video_track = dynamic_pointer_cast(track); + obj["width"] = video_track->getVideoWidth(); + obj["height"] = video_track->getVideoHeight(); + obj["fps"] = round(video_track->getVideoFps()); + break; + } + default: + break; + } + item["tracks"].append(obj); + } + return item; +} + /** * 安装api接口 * 所有api都支持GET和POST两种方式 @@ -370,62 +425,6 @@ void installWebApi() { }); #endif//#if !defined(_WIN32) - - static auto makeMediaSourceJson = [](const MediaSource::Ptr &media){ - Value item; - item["schema"] = media->getSchema(); - item["vhost"] = media->getVhost(); - item["app"] = media->getApp(); - item["stream"] = media->getId(); - item["createStamp"] = (Json::UInt64) media->getCreateStamp(); - item["aliveSecond"] = (Json::UInt64) media->getAliveSecond(); - item["bytesSpeed"] = media->getBytesSpeed(); - item["readerCount"] = media->readerCount(); - item["totalReaderCount"] = media->totalReaderCount(); - item["originType"] = (int) media->getOriginType(); - item["originTypeStr"] = getOriginTypeString(media->getOriginType()); - item["originUrl"] = media->getOriginUrl(); - auto originSock = media->getOriginSock(); - if (originSock) { - item["originSock"]["local_ip"] = originSock->get_local_ip(); - item["originSock"]["local_port"] = originSock->get_local_port(); - item["originSock"]["peer_ip"] = originSock->get_peer_ip(); - item["originSock"]["peer_port"] = originSock->get_peer_port(); - item["originSock"]["identifier"] = originSock->getIdentifier(); - } else { - item["originSock"] = Json::nullValue; - } - - for(auto &track : media->getTracks()){ - Value obj; - auto codec_type = track->getTrackType(); - obj["codec_id"] = track->getCodecId(); - obj["codec_id_name"] = track->getCodecName(); - obj["ready"] = track->ready(); - obj["codec_type"] = codec_type; - switch(codec_type){ - case TrackAudio : { - auto audio_track = dynamic_pointer_cast(track); - obj["sample_rate"] = audio_track->getAudioSampleRate(); - obj["channels"] = audio_track->getAudioChannel(); - obj["sample_bit"] = audio_track->getAudioSampleBit(); - break; - } - case TrackVideo : { - auto video_track = dynamic_pointer_cast(track); - obj["width"] = video_track->getVideoWidth(); - obj["height"] = video_track->getVideoHeight(); - obj["fps"] = round(video_track->getVideoFps()); - break; - } - default: - break; - } - item["tracks"].append(obj); - } - return item; - }; - //获取流列表,可选筛选参数 //测试url0(获取所有流) http://127.0.0.1/index/api/getMediaList //测试url1(获取虚拟主机为"__defaultVost__"的流) http://127.0.0.1/index/api/getMediaList?vhost=__defaultVost__ @@ -446,7 +445,7 @@ void installWebApi() { if (!allArgs["stream"].empty() && allArgs["stream"] != media->getId()) { return; } - val["data"].append(makeMediaSourceJson(media)); + val["data"].append(makeMediaSourceJson(*media)); }); }); @@ -466,7 +465,7 @@ void installWebApi() { val["online"] = false; return; } - val = makeMediaSourceJson(src); + val = makeMediaSourceJson(*src); val["online"] = true; val["code"] = API::Success; }); @@ -1083,7 +1082,7 @@ void installWebApi() { val["passwd"] = allArgs["user_name"].data(); }); - api_regist("/index/hook/on_stream_changed",[](API_ARGS_MAP){ + api_regist("/index/hook/on_stream_changed",[](API_ARGS_JSON){ //媒体注册或反注册事件 }); diff --git a/server/WebApi.h b/server/WebApi.h index 286fde41..0cf2ec39 100755 --- a/server/WebApi.h +++ b/server/WebApi.h @@ -124,5 +124,5 @@ bool checkArgs(Args &&args, First &&first, KeyTypes &&...keys) { void installWebApi(); void unInstallWebApi(); - +Value makeMediaSourceJson(MediaSource &media); #endif //ZLMEDIAKIT_WEBAPI_H diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 16f8ddf3..1bd47ffb 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -19,6 +19,7 @@ #include "Rtsp/RtspSession.h" #include "Http/HttpSession.h" #include "WebHook.h" +#include "WebApi.h" using namespace toolkit; using namespace mediakit; @@ -301,10 +302,14 @@ void installWebHook(){ } ArgsType body; body["regist"] = bRegist; - body["schema"] = sender.getSchema(); - body["vhost"] = sender.getVhost(); - body["app"] = sender.getApp(); - body["stream"] = sender.getId(); + if (bRegist) { + body = makeMediaSourceJson(sender); + } else { + body["schema"] = sender.getSchema(); + body["vhost"] = sender.getVhost(); + body["app"] = sender.getApp(); + body["stream"] = sender.getId(); + } //执行hook do_http_hook(hook_stream_chaned,body, nullptr); });