From e94cb6da040c03cbb33d96c0fb438c76bc20c6ac Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: 星期一, 14 八月 2023 17:11:45 +0800
Subject: [PATCH] 优化拉流列表和协议

---
 ard-work/src/main/java/com/ruoyi/media/service/impl/WebrtcService.java |   66 ++++++++++++++++++--------------
 1 files changed, 37 insertions(+), 29 deletions(-)

diff --git a/ard-work/src/main/java/com/ruoyi/media/service/impl/WebrtcService.java b/ard-work/src/main/java/com/ruoyi/media/service/impl/WebrtcService.java
index 299d2e3..e8b45ba 100644
--- a/ard-work/src/main/java/com/ruoyi/media/service/impl/WebrtcService.java
+++ b/ard-work/src/main/java/com/ruoyi/media/service/impl/WebrtcService.java
@@ -1,9 +1,13 @@
 package com.ruoyi.media.service.impl;
 
+import com.ruoyi.utils.tools.CmdUtils;
 import com.sun.jna.Platform;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
@@ -19,40 +23,44 @@
  * @Version: 1.0
  **/
 @Component
+@Slf4j(topic = "cmd")
 public class WebrtcService {
+    String processName = "webrtc-streamer.exe";
+    @Value("${webrtc.host}")
+    String webrtcHost;
+    @Value("${webrtc.enabled}")
+    Boolean webrtcEnabled;
+
     @PostConstruct
     public void init() {
-        if (Platform.isWindows()) {
-            // String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "webrtc" + File.separator + "setup.bat";
-            // String[] cmd = {"cmd", "/c", "start", exePath};
-            String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "webrtc" + File.separator + "webrtc-streamer.exe";
-            List<String> cmd = new ArrayList<>();
-            cmd.add(exePath);
-            cmd.add("-o");
-            cmd.add("-H127.0.0.1:8000");
-            commandStart(cmd);
+        if (webrtcEnabled) {
+            if (Platform.isWindows()) {
+                String exePath = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "webrtc" + File.separator + processName;
+                List<String> cmd = new ArrayList<>();
+                cmd.add(exePath);
+                cmd.add("-H" + webrtcHost);
+                cmd.add("-o");
+                if (CmdUtils.isProcessRunning(processName)) {
+                    // 杩涚▼宸茬粡鍦ㄨ繍琛岋紝缁撴潫璇ヨ繘绋�
+                    CmdUtils.stopProcess(processName);
+                }
+                // 鍚姩鍚庡彴杩涚▼
+                CmdUtils.commandStart(processName, cmd, null);
+                // 鍚姩cmd绐楀彛
+//            String[] command = {"cmd", "/c", "start", exePath, "-H127.0.0.1:8000", "-o"};
+//            CmdUtils.commandStart(command);
+            }
         }
     }
-    public static void commandStart(List<String> command) {
-        command.forEach(v -> System.out.print(v + " "));
-        System.out.println();
-        System.out.println();
-        ProcessBuilder builder = new ProcessBuilder();
-        //姝e父淇℃伅鍜岄敊璇俊鎭悎骞惰緭鍑�
-        builder.redirectErrorStream(true);
-        builder.command(command);
-        //寮�濮嬫墽琛屽懡浠�
-        Process process = null;
-        try {
-           process = builder.start();
-//            //濡傛灉浣犳兂鑾峰彇鍒版墽琛屽畬鍚庣殑淇℃伅锛岄偅涔堜笅闈㈢殑浠g爜涔熸槸闇�瑕佺殑
-//            String line = "";
-//            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
-//            while ((line = br.readLine()) != null) {
-//                System.out.println(line);
-//            }
-       } catch (IOException e) {
-            e.printStackTrace();
+
+    @PreDestroy
+    public void destroyMediaMtx() {
+        if (webrtcEnabled) {
+            log.info("閿�姣亀ebrtc-streamer");
+            if (CmdUtils.isProcessRunning(processName)) {
+                // 杩涚▼宸茬粡鍦ㄨ繍琛岋紝缁撴潫璇ヨ繘绋�
+                CmdUtils.stopProcess(processName);
+            }
         }
     }
 }

--
Gitblit v1.9.3