From a53dce38c2f58c0bfd37203e46dc57aa56025f45 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 17 十一月 2022 18:22:24 +0800
Subject: [PATCH] Merge pull request #673 from 648540858/wvp-28181-2.0-multi-network

---
 src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java |  119 ++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 61 insertions(+), 58 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 40c37c2..3183e3d 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
@@ -1,16 +1,15 @@
 package com.genersoft.iot.vmp.service.impl;
 
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
 import com.genersoft.iot.vmp.common.StreamInfo;
 import com.genersoft.iot.vmp.conf.UserSetting;
+import com.genersoft.iot.vmp.conf.exception.ControllerException;
 import com.genersoft.iot.vmp.gb28181.bean.GbStream;
-import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
-import com.genersoft.iot.vmp.gb28181.bean.TreeType;
 import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
 import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
 import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
-import com.genersoft.iot.vmp.media.zlm.dto.MediaItem;
+import com.genersoft.iot.vmp.media.zlm.dto.hook.OnStreamChangedHookParam;
 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;
@@ -24,7 +23,8 @@
 import com.genersoft.iot.vmp.storager.dao.StreamProxyMapper;
 import com.genersoft.iot.vmp.service.IStreamProxyService;
 import com.genersoft.iot.vmp.utils.DateUtil;
-import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
+import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
+import com.genersoft.iot.vmp.vmanager.bean.ResourceBaceInfo;
 import com.github.pagehelper.PageInfo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -33,9 +33,8 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.TransactionDefinition;
 import org.springframework.transaction.TransactionStatus;
-import org.springframework.util.StringUtils;
+import org.springframework.util.ObjectUtils;
 
-import java.net.InetAddress;
 import java.util.*;
 
 /**
@@ -93,25 +92,21 @@
 
 
     @Override
-    public WVPResult<StreamInfo> save(StreamProxyItem param) {
+    public StreamInfo save(StreamProxyItem param) {
         MediaServerItem mediaInfo;
-        WVPResult<StreamInfo> wvpResult = new WVPResult<>();
-        wvpResult.setCode(0);
-        if (param.getMediaServerId() == null || "auto".equals(param.getMediaServerId())){
+        if (ObjectUtils.isEmpty(param.getMediaServerId()) || "auto".equals(param.getMediaServerId())){
             mediaInfo = mediaServerService.getMediaServerForMinimumLoad();
         }else {
             mediaInfo = mediaServerService.getOne(param.getMediaServerId());
         }
         if (mediaInfo == null) {
             logger.warn("淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM...");
-            wvpResult.setMsg("淇濆瓨澶辫触");
-            return wvpResult;
+            throw new ControllerException(ErrorCode.ERROR100.getCode(), "淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM");
         }
-
         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();
+        StringBuffer resultMsg = new StringBuffer();
         boolean streamLive = false;
         param.setMediaServerId(mediaInfo.getId());
         boolean saveResult;
@@ -121,43 +116,47 @@
         }else { // 鏂板
             saveResult = addStreamProxy(param);
         }
-        if (saveResult) {
-            result.append("淇濆瓨鎴愬姛");
-            if (param.isEnable()) {
-                JSONObject jsonObject = addStreamProxyToZlm(param);
-                if (jsonObject == null || jsonObject.getInteger("code") != 0) {
-                    streamLive = false;
-                    result.append(", 浣嗘槸鍚敤澶辫触锛岃妫�鏌ユ祦鍦板潃鏄惁鍙敤");
-                    param.setEnable(false);
-                    // 鐩存帴绉婚櫎
-                    if (param.isEnable_remove_none_reader()) {
-                        del(param.getApp(), param.getStream());
-                    }else {
-                        updateStreamProxy(param);
-                    }
-
-                }else {
-                    streamLive = true;
-                    StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
-                            mediaInfo, param.getApp(), param.getStream(), null, null);
-                    wvpResult.setData(streamInfo);
-
-                }
-            }
-        }else {
-            result.append("淇濆瓨澶辫触");
+        if (!saveResult) {
+            throw new ControllerException(ErrorCode.ERROR100.getCode(),"淇濆瓨澶辫触");
         }
-        if ( !StringUtils.isEmpty(param.getPlatformGbId()) && streamLive) {
+        StreamInfo resultForStreamInfo = null;
+        resultMsg.append("淇濆瓨鎴愬姛");
+        if (param.isEnable()) {
+            JSONObject jsonObject = addStreamProxyToZlm(param);
+            if (jsonObject == null || jsonObject.getInteger("code") != 0) {
+                streamLive = false;
+                resultMsg.append(", 浣嗘槸鍚敤澶辫触锛岃妫�鏌ユ祦鍦板潃鏄惁鍙敤");
+                param.setEnable(false);
+                // 鐩存帴绉婚櫎
+                if (param.isEnable_remove_none_reader()) {
+                    del(param.getApp(), param.getStream());
+                }else {
+                    updateStreamProxy(param);
+                }
+
+
+            }else {
+                streamLive = true;
+                resultForStreamInfo = mediaService.getStreamInfoByAppAndStream(
+                        mediaInfo, param.getApp(), param.getStream(), null, null);
+
+            }
+        }
+        if ( !ObjectUtils.isEmpty(param.getPlatformGbId()) && streamLive) {
             List<GbStream> gbStreams = new ArrayList<>();
             gbStreams.add(param);
             if (gbStreamService.addPlatformInfo(gbStreams, param.getPlatformGbId(), param.getCatalogId())){
-                result.append(",  鍏宠仈鍥芥爣骞冲彴[ " + param.getPlatformGbId() + " ]鎴愬姛");
+                return resultForStreamInfo;
             }else {
-                result.append(",  鍏宠仈鍥芥爣骞冲彴[ " + param.getPlatformGbId() + " ]澶辫触");
+                resultMsg.append(",  鍏宠仈鍥芥爣骞冲彴[ " + param.getPlatformGbId() + " ]澶辫触");
+                throw new ControllerException(ErrorCode.ERROR100.getCode(), resultMsg.toString());
+            }
+        }else {
+            if (!streamLive) {
+                throw new ControllerException(ErrorCode.ERROR100.getCode(), resultMsg.toString());
             }
         }
-        wvpResult.setMsg(result.toString());
-        return wvpResult;
+        return resultForStreamInfo;
     }
 
     /**
@@ -174,7 +173,7 @@
         streamProxyItem.setCreateTime(now);
         try {
             if (streamProxyMapper.add(streamProxyItem) > 0) {
-                if (!StringUtils.isEmpty(streamProxyItem.getGbId())) {
+                if (!ObjectUtils.isEmpty(streamProxyItem.getGbId())) {
                     if (gbStreamMapper.add(streamProxyItem) < 0) {
                         //浜嬪姟鍥炴粴
                         dataSourceTransactionManager.rollback(transactionStatus);
@@ -209,7 +208,7 @@
         streamProxyItem.setStreamType("proxy");
         try {
             if (streamProxyMapper.update(streamProxyItem) > 0) {
-                if (!StringUtils.isEmpty(streamProxyItem.getGbId())) {
+                if (!ObjectUtils.isEmpty(streamProxyItem.getGbId())) {
                     if (gbStreamMapper.updateByAppAndStream(streamProxyItem) == 0) {
                         //浜嬪姟鍥炴粴
                         dataSourceTransactionManager.rollback(transactionStatus);
@@ -293,12 +292,11 @@
     public boolean start(String app, String stream) {
         boolean result = false;
         StreamProxyItem streamProxy = videoManagerStorager.queryStreamProxy(app, stream);
-        if (!streamProxy.isEnable() ) {
+        if (streamProxy != null && !streamProxy.isEnable() ) {
             JSONObject jsonObject = addStreamProxyToZlm(streamProxy);
             if (jsonObject == null) {
                 return false;
             }
-            System.out.println(jsonObject);
             if (jsonObject.getInteger("code") == 0) {
                 result = true;
                 streamProxy.setEnable(true);
@@ -386,18 +384,18 @@
         String type = "PULL";
 
         // 鍙戦�乺edis娑堟伅
-        List<MediaItem> mediaItems = redisCatchStorage.getStreams(mediaServerId, type);
-        if (mediaItems.size() > 0) {
-            for (MediaItem mediaItem : mediaItems) {
+        List<OnStreamChangedHookParam> onStreamChangedHookParams = redisCatchStorage.getStreams(mediaServerId, type);
+        if (onStreamChangedHookParams.size() > 0) {
+            for (OnStreamChangedHookParam onStreamChangedHookParam : onStreamChangedHookParams) {
                 JSONObject jsonObject = new JSONObject();
                 jsonObject.put("serverId", userSetting.getServerId());
-                jsonObject.put("app", mediaItem.getApp());
-                jsonObject.put("stream", mediaItem.getStream());
+                jsonObject.put("app", onStreamChangedHookParam.getApp());
+                jsonObject.put("stream", onStreamChangedHookParam.getStream());
                 jsonObject.put("register", false);
                 jsonObject.put("mediaServerId", mediaServerId);
                 redisCatchStorage.sendStreamChangeMsg(type, jsonObject);
                 // 绉婚櫎redis鍐呮祦鐨勪俊鎭�
-                redisCatchStorage.removeStream(mediaServerId, type, mediaItem.getApp(), mediaItem.getStream());
+                redisCatchStorage.removeStream(mediaServerId, type, onStreamChangedHookParam.getApp(), onStreamChangedHookParam.getStream());
             }
         }
     }
@@ -415,7 +413,7 @@
     private void syncPullStream(String mediaServerId){
         MediaServerItem mediaServer = mediaServerService.getOne(mediaServerId);
         if (mediaServer != null) {
-            List<MediaItem> allPullStream = redisCatchStorage.getStreams(mediaServerId, "PULL");
+            List<OnStreamChangedHookParam> allPullStream = redisCatchStorage.getStreams(mediaServerId, "PULL");
             if (allPullStream.size() > 0) {
                 zlmresTfulUtils.getMediaList(mediaServer, jsonObject->{
                     Map<String, StreamInfo> stringStreamInfoMap = new HashMap<>();
@@ -424,7 +422,7 @@
                         if(data != null && data.size() > 0) {
                             for (int i = 0; i < data.size(); i++) {
                                 JSONObject streamJSONObj = data.getJSONObject(i);
-                                if ("rtmp".equals(streamJSONObj.getString("schema"))) {
+                                if ("rtsp".equals(streamJSONObj.getString("schema"))) {
                                     StreamInfo streamInfo = new StreamInfo();
                                     String app = streamJSONObj.getString("app");
                                     String stream = streamJSONObj.getString("stream");
@@ -452,4 +450,9 @@
         }
 
     }
+
+    @Override
+    public ResourceBaceInfo getOverview() {
+        return streamProxyMapper.getOverview();
+    }
 }

--
Gitblit v1.8.0