From 89a9ab4534f10a224f70e546db838423e84a1965 Mon Sep 17 00:00:00 2001
From: 64850858 <648540858@qq.com>
Date: 星期五, 16 七月 2021 16:34:51 +0800
Subject: [PATCH] 添加zlm集群支持

---
 src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java |   66 ++++++++++++++++++++++++---------
 1 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
index 2eb99b8..fe49c3b 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
@@ -2,10 +2,12 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.genersoft.iot.vmp.gb28181.bean.GbStream;
-import com.genersoft.iot.vmp.media.zlm.ZLMServerConfig;
 import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
+import com.genersoft.iot.vmp.media.zlm.dto.IMediaServerItem;
+import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
 import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
 import com.genersoft.iot.vmp.service.IGbStreamService;
+import com.genersoft.iot.vmp.service.IMediaServerService;
 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 import com.genersoft.iot.vmp.storager.dao.GbStreamMapper;
@@ -13,6 +15,8 @@
 import com.genersoft.iot.vmp.storager.dao.StreamProxyMapper;
 import com.genersoft.iot.vmp.service.IStreamProxyService;
 import com.github.pagehelper.PageInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -25,6 +29,8 @@
 @Service
 public class StreamProxyServiceImpl implements IStreamProxyService {
 
+    private final static Logger logger = LoggerFactory.getLogger(StreamProxyServiceImpl.class);
+
     @Autowired
     private IVideoManagerStorager videoManagerStorager;
 
@@ -32,7 +38,7 @@
     private IRedisCatchStorage redisCatchStorage;
 
     @Autowired
-    private ZLMRESTfulUtils zlmresTfulUtils;
+    private ZLMRESTfulUtils zlmresTfulUtils;;
 
     @Autowired
     private StreamProxyMapper streamProxyMapper;
@@ -46,15 +52,28 @@
     @Autowired
     private IGbStreamService gbStreamService;
 
+    @Autowired
+    private IMediaServerService mediaServerService;
+
 
     @Override
     public String save(StreamProxyItem param) {
-        ZLMServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
+        IMediaServerItem mediaInfo;
+        if ("auto".equals(param.getMediaServerId())){
+            mediaInfo = mediaServerService.getMediaServerForMinimumLoad();
+        }else {
+            mediaInfo = mediaServerService.getOne(param.getMediaServerId());
+        }
+        if (mediaInfo == null) {
+            logger.warn("淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM...");
+            return "淇濆瓨澶辫触";
+        }
         String dstUrl = String.format("rtmp://%s:%s/%s/%s", "127.0.0.1", mediaInfo.getRtmpPort(), param.getApp(),
                 param.getStream() );
         param.setDst_url(dstUrl);
         StringBuffer result = new StringBuffer();
         boolean streamLive = false;
+        param.setMediaServerId(mediaInfo.getId());
         // 鏇存柊
         if (videoManagerStorager.queryStreamProxy(param.getApp(), param.getStream()) != null) {
             if (videoManagerStorager.updateStreamProxy(param)) {
@@ -81,6 +100,8 @@
                         videoManagerStorager.updateStreamProxy(param);
                     }
                 }
+            }else {
+                result.append("淇濆瓨澶辫触");
             }
 
         }
@@ -99,11 +120,18 @@
     @Override
     public JSONObject addStreamProxyToZlm(StreamProxyItem param) {
         JSONObject result = null;
+        IMediaServerItem mediaServerItem = null;
+        if (param.getMediaServerId() == null) {
+            logger.warn("娣诲姞浠g悊鏃禡ediaServerId 涓簄ull");
+            return null;
+        }else {
+            mediaServerItem = mediaServerService.getOne(param.getMediaServerId());
+        }
         if ("default".equals(param.getType())){
-            result = zlmresTfulUtils.addStreamProxy(param.getApp(), param.getStream(), param.getUrl(),
+            result = zlmresTfulUtils.addStreamProxy(mediaServerItem, param.getApp(), param.getStream(), param.getUrl(),
                     param.isEnable_hls(), param.isEnable_mp4(), param.getRtp_type());
         }else if ("ffmpeg".equals(param.getType())) {
-            result = zlmresTfulUtils.addFFmpegSource(param.getSrc_url(), param.getDst_url(),
+            result = zlmresTfulUtils.addFFmpegSource(mediaServerItem, param.getSrc_url(), param.getDst_url(),
                     param.getTimeout_ms() + "", param.isEnable_hls(), param.isEnable_mp4(),
                     param.getFfmpeg_cmd_key());
         }
@@ -112,8 +140,9 @@
 
     @Override
     public JSONObject removeStreamProxyFromZlm(StreamProxyItem param) {
-        JSONObject result = zlmresTfulUtils.closeStreams(param.getApp(), param.getStream());
-
+        if (param ==null) return null;
+        IMediaServerItem mediaServerItem = mediaServerService.getOne(param.getMediaServerId());
+        JSONObject result = zlmresTfulUtils.closeStreams(mediaServerItem, param.getApp(), param.getStream());
         return result;
     }
 
@@ -124,17 +153,18 @@
 
     @Override
     public void del(String app, String stream) {
-        StreamProxyItem streamProxyItem = new StreamProxyItem();
-        streamProxyItem.setApp(app);
-        streamProxyItem.setStream(stream);
-        JSONObject jsonObject = removeStreamProxyFromZlm(streamProxyItem);
-        if (jsonObject.getInteger("code") == 0) {
+        StreamProxyItem streamProxyItem = videoManagerStorager.queryStreamProxy(app, stream);
+        if (streamProxyItem != null) {
             videoManagerStorager.deleteStreamProxy(app, stream);
-            // 濡傛灉鍏宠仈浜嗗浗鏍囬偅涔堢Щ闄ゅ叧鑱�
-            gbStreamMapper.del(app, stream);
-            platformGbStreamMapper.delByAppAndStream(app, stream);
-            // TODO 濡傛灉鍏宠仈鐨勬帹娴侊紝 閭d箞鐘舵�佽缃负绂荤嚎
+            JSONObject jsonObject = removeStreamProxyFromZlm(streamProxyItem);
+            if (jsonObject != null && jsonObject.getInteger("code") == 0) {
+                // 濡傛灉鍏宠仈浜嗗浗鏍囬偅涔堢Щ闄ゅ叧鑱�
+                gbStreamMapper.del(app, stream);
+                platformGbStreamMapper.delByAppAndStream(app, stream);
+                // TODO 濡傛灉鍏宠仈鐨勬帹娴侊紝 閭d箞鐘舵�佽缃负绂荤嚎
+            }
         }
+
     }
 
     @Override
@@ -168,9 +198,9 @@
     }
 
     @Override
-    public JSONObject getFFmpegCMDs() {
+    public JSONObject getFFmpegCMDs(IMediaServerItem mediaServerItem) {
         JSONObject result = new JSONObject();
-        JSONObject mediaServerConfigResuly = zlmresTfulUtils.getMediaServerConfig();
+        JSONObject mediaServerConfigResuly = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
         if (mediaServerConfigResuly != null && mediaServerConfigResuly.getInteger("code") == 0
                 && mediaServerConfigResuly.getJSONArray("data").size() > 0){
             JSONObject mediaServerConfig = mediaServerConfigResuly.getJSONArray("data").getJSONObject(0);

--
Gitblit v1.8.0