| | |
| | | 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; |
| | |
| | | * @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(); |
| | | //正常信息和错误信息合并输出 |
| | | builder.redirectErrorStream(true); |
| | | builder.command(command); |
| | | //开始执行命令 |
| | | Process process = null; |
| | | try { |
| | | process = builder.start(); |
| | | // //如果你想获取到执行完后的信息,那么下面的代码也是需要的 |
| | | // 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("销毁webrtc-streamer"); |
| | | if (CmdUtils.isProcessRunning(processName)) { |
| | | // 进程已经在运行,结束该进程 |
| | | CmdUtils.stopProcess(processName); |
| | | } |
| | | } |
| | | } |
| | | } |