|
|
@@ -4,9 +4,11 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.hswebframework.web.authorization.annotation.Authorize;
|
|
|
+import org.jetlinks.community.media.config.UserSetup;
|
|
|
import org.jetlinks.community.media.contanst.VideoManagerConstants;
|
|
|
import org.jetlinks.community.media.entity.GbStream;
|
|
|
import org.jetlinks.community.media.bean.StreamInfo;
|
|
|
@@ -51,24 +53,30 @@ public class ZLMHttpHookListener {
|
|
|
private final LocalGbStreamService gbStreamService;
|
|
|
private final ClusterEventBus clusterEventBus;
|
|
|
private final String serverId="";
|
|
|
+ private final UserSetup userSetup;
|
|
|
/**
|
|
|
* 服务器定时上报时间,上报间隔可配置,默认10s上报一次
|
|
|
*
|
|
|
*/
|
|
|
@ResponseBody
|
|
|
@PostMapping(value = "/on_server_keepalive", produces = "application/json;charset=UTF-8")
|
|
|
- public Mono<ResponseEntity<String>> onServerKeepalive(@RequestBody JSONObject json){
|
|
|
+ public Mono<ResponseEntity<String>> onServerKeepalive(@RequestBody JSONObject json) throws Exception {
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
log.debug("[ ZLM HOOK ]on_server_keepalive API调用,参数:" + json.toString());
|
|
|
}
|
|
|
+ String mediaServerId = json.getStr("mediaServerId");
|
|
|
|
|
|
+ List<ZLMHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(ZLMHttpHookSubscribe.HookType.on_server_keepalive);
|
|
|
+ if (subscribes != null && subscribes.size() > 0) {
|
|
|
+ for (ZLMHttpHookSubscribe.Event subscribe : subscribes) {
|
|
|
+ subscribe.accept(null, json);
|
|
|
+ }
|
|
|
+ }
|
|
|
JSONObject ret = new JSONObject()
|
|
|
- .append("code", 0)
|
|
|
- .append("msg", "success");
|
|
|
- return eventBus.
|
|
|
- publish(ZLMHttpHookSubscribe.HookType.on_server_keepalive.name(),json)
|
|
|
- .thenReturn(ResponseEntity.ok(ret.toString()));
|
|
|
+ .putOpt("code", 0)
|
|
|
+ .putOpt("msg", "success");
|
|
|
+ return Mono.just(ResponseEntity.ok(ret.toString()));
|
|
|
}
|
|
|
|
|
|
// /**
|
|
|
@@ -89,52 +97,52 @@ public class ZLMHttpHookListener {
|
|
|
// return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
// }
|
|
|
//
|
|
|
- /**
|
|
|
- * 访问http文件服务器上hls之外的文件时触发。
|
|
|
- *
|
|
|
- */
|
|
|
- @ResponseBody
|
|
|
- @PostMapping(value = "/on_http_access", produces = "application/json;charset=UTF-8")
|
|
|
- public ResponseEntity<String> onHttpAccess(@RequestBody JSONObject json){
|
|
|
+ /**
|
|
|
+ * 访问http文件服务器上hls之外的文件时触发。
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @ResponseBody
|
|
|
+ @PostMapping(value = "/on_http_access", produces = "application/json;charset=UTF-8")
|
|
|
+ public ResponseEntity<String> onHttpAccess(@RequestBody JSONObject json){
|
|
|
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
log.debug("[ ZLM HOOK ]on_http_access API 调用,参数:" + json.toString());
|
|
|
- }
|
|
|
- String mediaServerId = json.getStr("mediaServerId");
|
|
|
- JSONObject ret = new JSONObject();
|
|
|
- ret.put("code", 0);
|
|
|
- ret.put("err", "");
|
|
|
- ret.put("path", "");
|
|
|
- ret.put("second", 600);
|
|
|
- return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
- }
|
|
|
+ }
|
|
|
+ String mediaServerId = json.getStr("mediaServerId");
|
|
|
+ JSONObject ret = new JSONObject()
|
|
|
+ .putOpt("code", 0)
|
|
|
+ .putOpt("err", "")
|
|
|
+ .putOpt("path", "")
|
|
|
+ .putOpt("second", 600);
|
|
|
+ return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 播放器鉴权事件,rtsp/rtmp/http-flv/ws-flv/hls的播放都将触发此鉴权事件。
|
|
|
- *
|
|
|
- */
|
|
|
- @ResponseBody
|
|
|
- @PostMapping(value = "/on_play", produces = "application/json;charset=UTF-8")
|
|
|
- public ResponseEntity<String> onPlay(@RequestBody JSONObject json){
|
|
|
+ /**
|
|
|
+ * 播放器鉴权事件,rtsp/rtmp/http-flv/ws-flv/hls的播放都将触发此鉴权事件。
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @ResponseBody
|
|
|
+ @PostMapping(value = "/on_play", produces = "application/json;charset=UTF-8")
|
|
|
+ public ResponseEntity<String> onPlay(@RequestBody JSONObject json){
|
|
|
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
log.debug("[ ZLM HOOK ]on_play API调用,参数:" + json.toString());
|
|
|
- }
|
|
|
- String mediaServerId = json.getStr("mediaServerId");
|
|
|
- ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_play, json);
|
|
|
- if (subscribe != null ) {
|
|
|
- MediaServerItem mediaInfo = mediaServerItemService.getOneByServerId(mediaServerId);
|
|
|
- //todo
|
|
|
+ }
|
|
|
+ String mediaServerId = json.getStr("mediaServerId");
|
|
|
+ ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_play, json);
|
|
|
+ if (subscribe != null ) {
|
|
|
+ MediaServerItem mediaInfo = mediaServerItemService.getOneByServerId(mediaServerId);
|
|
|
+ //todo
|
|
|
// if (mediaInfo != null) {
|
|
|
// subscribe.response(mediaInfo, json);
|
|
|
// }
|
|
|
|
|
|
- }
|
|
|
- JSONObject ret = new JSONObject();
|
|
|
- ret.put("code", 0);
|
|
|
- ret.put("msg", "success");
|
|
|
- return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
- }
|
|
|
+ }
|
|
|
+ JSONObject ret = new JSONObject()
|
|
|
+ .putOpt("code", 0)
|
|
|
+ .putOpt("msg", "success");
|
|
|
+ return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* rtsp/rtmp/rtp推流鉴权事件。
|
|
|
@@ -145,32 +153,32 @@ public class ZLMHttpHookListener {
|
|
|
public ResponseEntity<String> onPublish(@RequestBody JSONObject json) {
|
|
|
|
|
|
log.debug("[ ZLM HOOK ]on_publish API调用,参数:" + json.toString());
|
|
|
- JSONObject ret = new JSONObject();
|
|
|
- ret.put("code", 0);
|
|
|
- ret.put("msg", "success");
|
|
|
- ret.put("enableHls", true);
|
|
|
-// ret.put("enableMP4", userSetup.isRecordPushLive());
|
|
|
-// String mediaServerId = json.getString("mediaServerId");
|
|
|
-// ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, json);
|
|
|
-// if (subscribe != null) {
|
|
|
-// MediaServerItem mediaInfo = mediaServerService.getOneByServerId(mediaServerId);
|
|
|
-// if (mediaInfo != null) {
|
|
|
-// subscribe.response(mediaInfo, json);
|
|
|
-// }else {
|
|
|
-// ret.put("code", 1);
|
|
|
-// ret.put("msg", "zlm not register");
|
|
|
-// }
|
|
|
-// }
|
|
|
-// String app = json.getString("app");
|
|
|
-// String stream = json.getString("stream");
|
|
|
-// StreamInfo streamInfo = redisCatchStorage.queryPlaybackByStreamId(stream);
|
|
|
+ JSONObject ret = new JSONObject()
|
|
|
+ .putOpt("code", 0)
|
|
|
+ .putOpt("msg", "success")
|
|
|
+ .putOpt("enableHls", true)
|
|
|
+ .putOpt("enableMP4", userSetup.isRecordPushLive());
|
|
|
+// String mediaServerId = json.getStr("mediaServerId");
|
|
|
+// ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, json);
|
|
|
+// if (subscribe != null) {
|
|
|
+// MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
|
|
|
+// if (mediaInfo != null) {
|
|
|
+// subscribe.accept(mediaInfo, json);
|
|
|
+// }else {
|
|
|
+// ret.put("code", 1);
|
|
|
+// ret.put("msg", "zlm not register");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// String app = json.getStr("app");
|
|
|
+// String stream = json.getStr("stream");
|
|
|
+// StreamInfo streamInfo = redisCatchStorage.queryPlaybackByStreamId(stream);
|
|
|
//
|
|
|
-// // 录像回放时不进行录像下载
|
|
|
-// if (streamInfo != null) {
|
|
|
-// ret.put("enableMP4", false);
|
|
|
-// }else {
|
|
|
-// ret.put("enableMP4", userSetup.isRecordPushLive());
|
|
|
-// }
|
|
|
+// // 录像回放时不进行录像下载
|
|
|
+// if (streamInfo != null) {
|
|
|
+// ret.put("enableMP4", false);
|
|
|
+// }else {
|
|
|
+// ret.put("enableMP4", userSetup.isRecordPushLive());
|
|
|
+// }
|
|
|
|
|
|
return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
}
|
|
|
@@ -193,23 +201,23 @@ public class ZLMHttpHookListener {
|
|
|
// return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
// }
|
|
|
|
|
|
- /**
|
|
|
- * rtsp专用的鉴权事件,先触发on_rtsp_realm事件然后才会触发on_rtsp_auth事件。
|
|
|
- *
|
|
|
- */
|
|
|
- @ResponseBody
|
|
|
- @PostMapping(value = "/on_rtsp_realm", produces = "application/json;charset=UTF-8")
|
|
|
- public ResponseEntity<String> onRtspRealm(@RequestBody JSONObject json){
|
|
|
+ /**
|
|
|
+ * rtsp专用的鉴权事件,先触发on_rtsp_realm事件然后才会触发on_rtsp_auth事件。
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @ResponseBody
|
|
|
+ @PostMapping(value = "/on_rtsp_realm", produces = "application/json;charset=UTF-8")
|
|
|
+ public ResponseEntity<String> onRtspRealm(@RequestBody JSONObject json){
|
|
|
|
|
|
- if (log.isDebugEnabled()) {
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
log.debug("[ ZLM HOOK ]on_rtsp_realm API调用,参数:" + json.toString());
|
|
|
- }
|
|
|
- String mediaServerId = json.getStr("mediaServerId");
|
|
|
- JSONObject ret = new JSONObject();
|
|
|
- ret.put("code", 0);
|
|
|
- ret.put("realm", "");
|
|
|
- return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
- }
|
|
|
+ }
|
|
|
+ String mediaServerId = json.getStr("mediaServerId");
|
|
|
+ JSONObject ret = new JSONObject()
|
|
|
+ .putOpt("code", 0)
|
|
|
+ .putOpt("realm", "");
|
|
|
+ return new ResponseEntity<String>(ret.toString(), HttpStatus.OK);
|
|
|
+ }
|
|
|
//
|
|
|
//
|
|
|
// /**
|
|
|
@@ -265,7 +273,7 @@ public class ZLMHttpHookListener {
|
|
|
* rtsp/rtmp流注册或注销时触发此事件;此事件对回复不敏感。
|
|
|
*
|
|
|
*/
|
|
|
- @PostMapping(value = "/on_stream_changed")
|
|
|
+ @PostMapping(value = "/on_stream_changed", produces = "application/json;charset=UTF-8")
|
|
|
public Mono<ResponseEntity<String>> onStreamChanged(@RequestBody MediaItem item){
|
|
|
if (log.isDebugEnabled()) {
|
|
|
log.debug("[ ZLM HOOK ]on_stream_changed API调用,参数:" + JSONUtil.toJsonStr(item));
|
|
|
@@ -278,11 +286,8 @@ public class ZLMHttpHookListener {
|
|
|
//rtsp或rtmp
|
|
|
String schema = item.getSchema();
|
|
|
JSONObject json = JSONUtil.parseObj(item);
|
|
|
- //todo 这里订阅/发布采用eventBus 模式进行
|
|
|
- //on_stream_changed+{mediaServerId}+{streamId}+app
|
|
|
- Mono<Long> result = Mono.fromRunnable(()->clusterEventBus.publish(ZlmSubscribeTopic.getOnStreamChanged(app, streamId, mediaServerId), json)).thenReturn(1L);
|
|
|
- // 流消失移除redis play
|
|
|
|
|
|
+ Mono<Long> result = Mono.fromRunnable(()->{}).thenReturn(1L);
|
|
|
|
|
|
boolean regist = item.isRegist();
|
|
|
if ("rtmp".equals(schema)){
|
|
|
@@ -369,11 +374,11 @@ public class ZLMHttpHookListener {
|
|
|
if (type != null) {
|
|
|
// 发送流变化redis消息
|
|
|
JSONObject jsonObject = new JSONObject()
|
|
|
- .append("serverId", serverId)
|
|
|
- .append("app", app)
|
|
|
- .append("stream", streamId)
|
|
|
- .append("register", regist)
|
|
|
- .append("mediaServerId", mediaServerId);
|
|
|
+ .putOpt("serverId", serverId)
|
|
|
+ .putOpt("app", app)
|
|
|
+ .putOpt("stream", streamId)
|
|
|
+ .putOpt("register", regist)
|
|
|
+ .putOpt("mediaServerId", mediaServerId);
|
|
|
if(mono!=null){
|
|
|
String originType=type;
|
|
|
mono=mono.doOnNext(___->clusterEventBus.publish(VideoManagerConstants.WVP_SERVER_STREAM_PREFIX+"_"+originType,jsonObject));
|
|
|
@@ -385,11 +390,25 @@ public class ZLMHttpHookListener {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
JSONObject ret = new JSONObject()
|
|
|
- .append("code", 0)
|
|
|
- .append("msg", "success");
|
|
|
+ .putOpt("code", 0)
|
|
|
+ .putOpt("msg", "success");
|
|
|
return result
|
|
|
- .thenReturn(ResponseEntity.ok(ret.toString()));
|
|
|
+ .mergeWith(Mono.fromRunnable(()->{
|
|
|
+ ZLMHttpHookSubscribe.Event subscribe = this.subscribe.getSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, json);
|
|
|
+ if(subscribe!=null){
|
|
|
+ MediaServerItem mediaServerItem = redisCatchStorage.getMediaServerItem(mediaServerId);
|
|
|
+ if(mediaServerItem!=null){
|
|
|
+ try {
|
|
|
+ subscribe.accept(mediaServerItem,json);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("回调失败,",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }))
|
|
|
+ .then(Mono.just(ResponseEntity.ok(ret.toString())));
|
|
|
}
|
|
|
//
|
|
|
// /**
|
|
|
@@ -504,17 +523,21 @@ public class ZLMHttpHookListener {
|
|
|
*/
|
|
|
@ResponseBody
|
|
|
@PostMapping(value = "/on_server_started", produces = "application/json;charset=UTF-8")
|
|
|
- public Mono<ResponseEntity<String>> onServerStarted(ServerWebExchange exchange, @RequestBody JSONObject jsonObject){
|
|
|
+ public Mono<ResponseEntity<String>> onServerStarted(ServerWebExchange exchange, @RequestBody JSONObject jsonObject) throws Exception {
|
|
|
if (log.isDebugEnabled()) {
|
|
|
log.debug("[ ZLM HOOK ]on_server_started API调用,参数:" + jsonObject.toString());
|
|
|
}
|
|
|
String remoteAddr = exchange.getRequest().getRemoteAddress().getAddress().toString();
|
|
|
- jsonObject.append("ip", remoteAddr);
|
|
|
+ jsonObject.putOpt("ip", remoteAddr);
|
|
|
JSONObject ret = new JSONObject()
|
|
|
- .append("code", 0)
|
|
|
- .append("msg", "success");
|
|
|
- return eventBus.
|
|
|
- publish(ZLMHttpHookSubscribe.HookType.on_server_started.name(),jsonObject)
|
|
|
- .thenReturn(ResponseEntity.ok(ret.toString()));
|
|
|
+ .putOpt("code", 0)
|
|
|
+ .putOpt("msg", "success");
|
|
|
+ List<ZLMHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(ZLMHttpHookSubscribe.HookType.on_server_started);
|
|
|
+ if (subscribes != null && subscribes.size() > 0) {
|
|
|
+ for (ZLMHttpHookSubscribe.Event subscribe : subscribes) {
|
|
|
+ subscribe.accept(null, jsonObject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Mono.just(ResponseEntity.ok(ret.toString()));
|
|
|
}
|
|
|
}
|