From 191a9cb6a141fb39037c41d8c13e78bf0c1b1cde Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: 星期三, 19 七月 2023 16:00:20 +0800
Subject: [PATCH] 优化流媒体转码增删改查

---
 ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java |   61 ++++++++------
 ard-work/src/main/java/com/ruoyi/utils/forest/MediaClient.java         |   40 +++++----
 ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml        |    3 
 ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java      |    5 
 ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java  |  103 +++++++++++++++++--------
 5 files changed, 128 insertions(+), 84 deletions(-)

diff --git a/ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java b/ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java
index 088a8b5..52adb77 100644
--- a/ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java
+++ b/ard-work/src/main/java/com/ruoyi/media/controller/MediaController.java
@@ -1,9 +1,13 @@
 package com.ruoyi.media.controller;
 
 import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
+import com.ruoyi.common.annotation.Anonymous;
+import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.page.TableDataInfo;
+import com.ruoyi.common.enums.BusinessType;
+import com.ruoyi.device.camera.domain.ArdCameras;
 import com.ruoyi.media.domain.StreamInfo;
 import com.ruoyi.media.service.IMediaService;
 import io.swagger.annotations.Api;
@@ -23,27 +27,48 @@
 @RestController
 @Api(tags = "娴佸獟浣撴帴鍙�")
 @RequestMapping("/media/stream")
+@Anonymous
 public class MediaController extends BaseController {
     @Resource
     private IMediaService mediaService;
 
-    @PostMapping("/add")
+    @PostMapping()
     @ApiOperation("澧炲姞杞爜")
-    @ApiOperationSupport(includeParameters = {"streamInfo.name","streamInfo.rtspSource"})
+    @PreAuthorize("@ss.hasPermi('media:stream:add')")
+    @ApiOperationSupport(includeParameters = {"streamInfo.name", "streamInfo.rtspSource", "streamInfo.mode"})
     public AjaxResult addPath(@RequestBody StreamInfo streamInfo) {
-        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource());
+        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode());
+        return AjaxResult.success(rtsp);
+    }
+    /**
+     * 鑾峰彇杞爜璇︾粏淇℃伅
+     */
+    @ApiOperation("鑾峰彇杞爜璇︾粏淇℃伅")
+    @GetMapping(value = "/{name}")
+    public AjaxResult getInfo(@PathVariable("name") String name) {
+        return success(mediaService.getPathInfo(name));
+    }
+    /**
+     * 淇敼杞爜
+     */
+    @ApiOperation("淇敼杞爜")
+    @PreAuthorize("@ss.hasPermi('media:stream:edit')")
+    @PutMapping
+    public AjaxResult edit(@RequestBody StreamInfo streamInfo) {
+        mediaService.removePath(new String[]{streamInfo.getName()});
+        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode());
         return AjaxResult.success(rtsp);
     }
 
-    @PostMapping("/remove")
+    @DeleteMapping("/path/{names}")
+    @PreAuthorize("@ss.hasPermi('media:stream:remove')")
     @ApiOperation("绉婚櫎杞爜")
-    @ApiOperationSupport(includeParameters = {"streamInfo.name"})
-    public AjaxResult removePath(@RequestBody StreamInfo streamInfo) {
-        mediaService.removePath(streamInfo.getName());
+    public AjaxResult removePath( @PathVariable String[] names) {
+        mediaService.removePath(names);
         return AjaxResult.success();
     }
 
-    @GetMapping("/getPaths")
+    @GetMapping("/path/list")
     @ApiOperation("鑾峰彇褰撳墠杞爜鍒楄〃")
     public TableDataInfo getPaths() {
         startPage();
@@ -87,25 +112,7 @@
         startPage();
         return getDataTable(mediaService.getPushStreamList());
     }
-    /**
-     * 鏂板娴佸獟浣撴帹娴�
-     */
-    @PreAuthorize("@ss.hasPermi('media:stream:add')")
-    @PostMapping
-    @ApiOperationSupport(includeParameters = {"streamInfo.name"})
-    public AjaxResult add(@RequestBody StreamInfo streamInfo) throws InterruptedException {
-        String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource());
-        return AjaxResult.success(rtsp);
-    }
-    /**
-     * 鍒犻櫎娴佸獟浣撴帹娴�
-     */
-    @PreAuthorize("@ss.hasPermi('media:stream:remove')")
-    @DeleteMapping("/path/{name}")
-    public AjaxResult remove(@PathVariable String name) {
-        mediaService.removePath(name);
-        return AjaxResult.success();
-    }
+
     /**
      * 鍒犻櫎娴佸獟浣撴媺娴�
      */
diff --git a/ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java b/ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java
index 910ad97..099c8c6 100644
--- a/ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java
+++ b/ard-work/src/main/java/com/ruoyi/media/service/IMediaService.java
@@ -7,8 +7,9 @@
 
 public interface IMediaService {
 
-    public String addPath(String name, String rtspPath);
-    public void removePath(String name);
+    public String addPath(String name, String rtspPath,String mode);
+    StreamInfo getPathInfo(String name);
+    public void removePath(String[] names);
     public List<StreamInfo>paths();
     public List<Items> rtspconns();
     public List<Items> rtspsessions();
diff --git a/ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java b/ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java
index 51fd458..baa5569 100644
--- a/ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java
+++ b/ard-work/src/main/java/com/ruoyi/media/service/impl/MediaService.java
@@ -28,13 +28,10 @@
 
     @Resource
     MediaClient mediaClient;
-
     @Value("${mediamtx.host}")
     String mediamtxHost;
-
     @Override
-    public String addPath(String name, String rtspPath) {
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
+    public String addPath(String name, String rtspPath,String mode) {
         String rtspUrl = "rtsp://" + mediamtxHost + ":8554/";
         Conf mediaInfo = new Conf();
         //-vcodec libx264 //鎸囧畾瑙嗛缂栫爜鍣ㄤ负 libx264锛屼娇鐢� H.264 缂栫爜鏍煎紡杩涜瑙嗛鍘嬬缉
@@ -48,24 +45,60 @@
         String cmd = "ffmpeg -rtsp_transport tcp -i \"" + rtspPath + "\" -vcodec libx264 -preset:v ultrafast -r 25 -threads 4  -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
         //GPU纭В鐮佺紪鐮� -hwaccel cuvid -c:v h264_cuvid  浣跨敤cuda瑙g爜   -c:v h264_nvenc 浣跨敤cuda缂栫爜
         //String cmd = "ffmpeg -hwaccel cuvid -c:v h264_cuvid  -rtsp_transport udp  -i \"" + rtspPath + "\" -c:v h264_nvenc  -r 25 -threads 4  -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH";
-//        mediaInfo.setRunoninit(cmd);
-//        mediaInfo.setRunoninitrestart(true);
-        mediaInfo.setRunondemand(cmd);
-        mediaInfo.setRunondemandrestart(true);
-        mediaClient.addPath(apiUrl, name, mediaInfo);
+
+        if(mode.equals("1"))
+        {
+            mediaInfo.setRunondemand(cmd);
+            mediaInfo.setRunondemandrestart(true);
+        }
+       else{
+           mediaInfo.setRunoninit(cmd);
+           mediaInfo.setRunoninitrestart(true);
+        }
+        mediaClient.addPath(name, mediaInfo);
         return rtspUrl + name;
     }
 
     @Override
-    public void removePath(String name) {
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        mediaClient.removePath(apiUrl, name);
+    public StreamInfo getPathInfo(String name) {
+        Items item = mediaClient.getPathInfo(name);
+        StreamInfo info = new StreamInfo();
+        //ID
+        info.setName(name);
+        String runoninit;
+        String runondemand = item.getConf().getRunondemand();
+        if (StringUtils.isNotEmpty(runondemand))
+        {
+            runoninit= item.getConf().getRunondemand();
+            info.setMode("1");
+        }
+        else
+        {
+            runoninit = item.getConf().getRunoninit();
+            info.setMode("2");
+        }
+        //RTSP婧愬湴鍧�
+        String regex = "rtsp://[^\\s\"]+";
+        Pattern pattern = Pattern.compile(regex);
+        Matcher matcher = pattern.matcher(runoninit);
+        if (matcher.find()) {
+            info.setRtspSource(matcher.group());
+        }
+        return info;
+    }
+
+    @Override
+    public void removePath(String[] names) {
+        for(String name:names)
+        {
+            mediaClient.removePath(name);
+        }
     }
 
     @Override
     public List<StreamInfo> paths() {
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.paths(apiUrl);
+
+        String list = mediaClient.paths();
         JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
         List<Items> items = jsonsRoot.getItems();
         List<StreamInfo> pathInfoList = new ArrayList<>();
@@ -79,15 +112,14 @@
             if (StringUtils.isNotEmpty(runondemand))
             {
                 runoninit= item.getConf().getRunondemand();
-                info.setMode("鎸夐渶杞爜");
+                info.setMode("1");
             }
             else
             {
                 runoninit = item.getConf().getRunoninit();
-                info.setMode("瀹炴椂杞爜");
+                info.setMode("2");
             }
             //RTSP婧愬湴鍧�
-            runoninit = item.getConf().getRunondemand();
             String regex = "rtsp://[^\\s\"]+";
             Pattern pattern = Pattern.compile(regex);
             Matcher matcher = pattern.matcher(runoninit);
@@ -109,24 +141,21 @@
 
     @Override
     public List<Items> rtspconns() {
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.rtspconns(apiUrl);
+        String list = mediaClient.rtspconns();
         JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
         return jsonsRoot.getItems();
     }
 
     @Override
     public List<Items> rtspsessions() {
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.rtspsessions(apiUrl);
+        String list = mediaClient.rtspsessions();
         JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
         return jsonsRoot.getItems();
     }
 
     @Override
     public RtspSession getRtspSessionById(String sessionId) {
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.getRtspsessionById(apiUrl, sessionId);
+        String list = mediaClient.getRtspsessionById(sessionId);
         RtspSession rtspSession = JSONObject.parseObject(list, RtspSession.class);
         return rtspSession;
     }
@@ -134,8 +163,8 @@
     @Override
     public List<RtspSession> getPushStreams() {
         List<RtspSession> rtspSessions = new ArrayList<>();
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.paths(apiUrl);
+
+        String list = mediaClient.paths();
         JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
         List<Items> items = jsonsRoot.getItems();
         for (Items item : items) {
@@ -150,8 +179,7 @@
     @Override
     public List<RtspSession> getPullStreams() {
         List<RtspSession> rtspSessions = new ArrayList<>();
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.paths(apiUrl);
+        String list = mediaClient.paths();
         JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
         List<Items> items = jsonsRoot.getItems();
         for (Items item : items) {
@@ -168,8 +196,7 @@
     @Override
     public List<StreamInfo> getPushStreamList() {
         List<StreamInfo> PushStreamInfoList = new ArrayList<>();
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.paths(apiUrl);
+        String list = mediaClient.paths();
         JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
         List<Items> items = jsonsRoot.getItems();
         for (Items item : items) {
@@ -199,8 +226,16 @@
             String formatSentSize = ArdTool.formatFileSize(bytesSent);
             info.setDownTraffic(formatSentSize);
             //RTSP婧愬湴鍧�
-            //String runoninit = item.getConf().getRunoninit();
-            String runoninit = item.getConf().getRunondemand();
+            String runondemand = item.getConf().getRunondemand();
+            String runoninit;
+            if(StringUtils.isNotEmpty(runondemand))
+            {
+                 runoninit = item.getConf().getRunondemand();
+            }
+            else
+            {
+                 runoninit = item.getConf().getRunoninit();
+            }
             String regex = "rtsp://[^\\s\"]+";
             Pattern pattern = Pattern.compile(regex);
             Matcher matcher = pattern.matcher(runoninit);
@@ -228,8 +263,7 @@
     @Override
     public List<StreamInfo> getPullStreamList() {
         List<StreamInfo> PullStreamInfoList = new ArrayList<>();
-        String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-        String list = mediaClient.paths(apiUrl);
+        String list = mediaClient.paths();
         JsonsRoot jsonsRoot = JSONObject.parseObject(list, JsonsRoot.class);
         List<Items> items = jsonsRoot.getItems();
         for (Items item : items) {
@@ -275,8 +309,7 @@
     @Override
     public Boolean kickRtspSession(String sessionId) {
         try {
-            String apiUrl = "http://" + mediamtxHost + ":9997/v2";
-            mediaClient.kick(apiUrl, sessionId);
+            mediaClient.kick(sessionId);
             return true;
         } catch (Exception ex) {
             return false;
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/MediaClient.java b/ard-work/src/main/java/com/ruoyi/utils/forest/MediaClient.java
index f3e0480..e87f76c 100644
--- a/ard-work/src/main/java/com/ruoyi/utils/forest/MediaClient.java
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/MediaClient.java
@@ -1,10 +1,8 @@
 package com.ruoyi.utils.forest;
 
-import com.dtflys.forest.annotation.Get;
-import com.dtflys.forest.annotation.JSONBody;
-import com.dtflys.forest.annotation.Post;
-import com.dtflys.forest.annotation.Var;
+import com.dtflys.forest.annotation.*;
 import com.ruoyi.media.domain.Conf;
+import com.ruoyi.media.domain.Items;
 
 /**
  * @Description: mediamtx娴佸獟浣撳鎴风
@@ -13,47 +11,53 @@
  * @Date: 2023骞�07鏈�06鏃�9:51
  * @Version: 1.0
  **/
+@BaseRequest(baseURL = "http://#{mediamtx.host}:9997/v2")
 public interface MediaClient {
     /**
      * 澧炲姞璺緞
      */
-    @Post("{apiUrl}/config/paths/add/{name}")
-    String addPath(@Var("apiUrl") String apiUrl, @Var("name") String name, @JSONBody Conf body);
+    @Post("/config/paths/add/{name}")
+    String addPath( @Var("name") String name, @JSONBody Conf body);
 
     /**
      * 绉婚櫎璺緞
      */
-    @Post("{apiUrl}/config/paths/remove/{name}")
-    String removePath(@Var("apiUrl") String apiUrl, @Var("name") String name);
+    @Post("/config/paths/remove/{name}")
+    String removePath(@Var("name") String name);
 
+    /**
+     * 鑾峰彇璺緞璇︽儏
+     */
+    @Get("/paths/get/{name}")
+    Items getPathInfo(@Var("name") String name);
     /**
      * 鏌ヨ鎵�鏈夎矾寰�
      */
-    @Get("{apiUrl}/paths/list")
-    String paths(@Var("apiUrl") String apiUrl);
+    @Get("/paths/list")
+    String paths();
 
     /**
      * 鏌ヨ鎵�鏈塺tsp浼氳瘽
      */
-    @Get("{apiUrl}/rtspsessions/list")
-    String rtspsessions(@Var("apiUrl") String apiUrl);
+    @Get("/rtspsessions/list")
+    String rtspsessions();
 
     /**
      * 鏌ヨ鎵�鏈塺tsp杩炴帴
      */
-    @Get("{apiUrl}/rtspconns/list")
-    String rtspconns(@Var("apiUrl") String apiUrl);
+    @Get("/rtspconns/list")
+    String rtspconns();
 
     /**
      * 鎸塻essionId鏌ヨrtsp浼氳瘽
      */
-    @Get("{apiUrl}/rtspsessions/get/{sessionId}")
-    String getRtspsessionById(@Var("apiUrl") String apiUrl, @Var("sessionId") String sessionId);
+    @Get("/rtspsessions/get/{sessionId}")
+    String getRtspsessionById(@Var("sessionId") String sessionId);
 
     /**
      * 鎸塻essionId鍒犻櫎rtsp浼氳瘽
      */
-    @Post("{apiUrl}/rtspsessions/kick/{sessionId}")
-    String kick(@Var("apiUrl") String apiUrl, @Var("sessionId") String sessionId);
+    @Post("/rtspsessions/kick/{sessionId}")
+    String kick(@Var("sessionId") String sessionId);
 
 }
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
index 7557ac3..e41ea2c 100644
--- a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
+++ b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
@@ -88,8 +88,7 @@
 
     <select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
 --         select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.sex, u.status,
---         u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.camera_priority,u.rong_cloud_token, d.dept_name, d.leader from sys_user
---         u
+--         u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.camera_priority,u.rong_cloud_token, d.dept_name, d.leader from sys_user u
 --         left join sys_dept d on u.dept_id = d.dept_id
         <include refid="selectUserVo"/>
         where u.del_flag = '0'

--
Gitblit v1.9.3