Browse Source

Merge branch 'usky-zyj' of uskycloud/usky-modules into server-165

hanzhengyi 1 tuần trước cách đây
mục cha
commit
7f8e4cba0b

+ 8 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/VcDeviceController.java

@@ -69,6 +69,14 @@ public class VcDeviceController {
         return ApiResult.success(vcDeviceService.getVcDeviceVideo(deviceUuid));
     }
 
+    /**
+     * 停止拉取摄像头视频流
+     */
+    @GetMapping("stopDeviceVideo")
+    public void stopDeviceVideo(){
+        vcDeviceService.stopDeviceVideo();
+    }
+
     /**
      * 新增
      * @param vcDevice

+ 2 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/VcDeviceService.java

@@ -23,6 +23,8 @@ public interface VcDeviceService extends CrudService<VcDevice> {
 
     String getVcDeviceVideo(String deviceUuid);
 
+    void stopDeviceVideo();
+
     boolean add(VcDevice vcDevice);
 
     void update(VcDevice vcDevice);

+ 0 - 46
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/config/VcVideo.java

@@ -1,46 +0,0 @@
-package com.usky.iot.service.config;
-
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.scheduling.annotation.Async;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-
-@Slf4j
-@Configuration
-public class VcVideo {
-    @Async
-    public void processVcVideo(String command) {
-        try {
-            log.info("【begin】实时拉取摄像头数据流 "+command);
-            // 创建 ProcessBuilder 实例
-            ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
-            // 启动进程
-            Process process = processBuilder.start();
-
-            // 获取进程的输入流
-            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-            String line;
-            while ((line = reader.readLine()) != null) {
-                System.out.println(line);
-            }
-
-            // 获取进程的错误流
-            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
-            while ((line = errorReader.readLine()) != null) {
-                System.err.println(line);
-            }
-
-            // 等待进程执行完成并获取退出码
-            int exitCode = process.waitFor();
-            System.out.println("命令执行完成,退出码: " + exitCode);
-            log.info("【end】实时拉取摄像头数据流 "+command+ "命令执行完成,退出码: " + exitCode);
-
-        } catch (IOException | InterruptedException e) {
-            e.printStackTrace();
-        }
-    }
-
-}

+ 42 - 10
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/VcDeviceServiceImpl.java

@@ -8,14 +8,11 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.usky.common.core.bean.CommonPage;
 import com.usky.common.core.exception.BusinessException;
 import com.usky.common.security.utils.SecurityUtils;
-import com.usky.iot.domain.BaseFacilityDevice;
-import com.usky.iot.domain.BaseGgpFacility;
 import com.usky.iot.domain.VcDevice;
 import com.usky.iot.mapper.VcDeviceMapper;
 import com.usky.iot.service.VcDeviceService;
 import com.usky.common.mybatis.core.AbstractCrudService;
-import com.usky.iot.service.config.VcVideo;
-import com.usky.iot.service.vo.BaseFacilityDeviceVO;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -34,14 +31,14 @@ import java.util.List;
  * @author han
  * @since 2024-10-22
  */
+@Slf4j
 @Service
 public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcDevice> implements VcDeviceService {
 
     @Value("${vc.rtmp-ip}")
     private String rtmpIp;
 
-    @Autowired
-    private VcVideo vcVideo;
+    Process process = null;
 
     @Override
     public List<VcDevice> VcDeviceList(Integer groupId){
@@ -78,13 +75,48 @@ public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcD
         String videoPassword = one.getVideoPassword();
         String videoIp = one.getVideoIp();
         String videoPort = one.getVideoPort().toString();
-        String command = "ffmpeg -i rtsp://"+accountNumber+":"+videoPassword+"@"+videoIp+":"+videoPort+" -vcodec copy -acodec aac -ar 44100 -c:v libx264 -strict -2 -ac 1 -f flv -r 30 -s 1920x1080 -q 10 -f flv rtmp://"+rtmpIp+":1935/hls/"+deviceUuid;
+        String command = "ffmpeg -i rtsp://"+accountNumber+":"+videoPassword+"@"+videoIp+":"+videoPort+" -vcodec copy -acodec aac -ar 44100 -c:v libx264 -strict -2 -ac 1 -r 30 -s 1920x1080 -q 10 -f flv rtmp://"+rtmpIp+":1935/hls/"+deviceUuid;
         System.out.println(command);
-        vcVideo.processVcVideo(command);
 
-        String repDeviceVideo = rtmpIp+":80/hls/"+deviceUuid+".m3u8";
+        try {
+            log.info("【begin】实时拉取摄像头数据流 "+command);
+            // 创建 ProcessBuilder 实例
+            ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
+            // 启动进程
+            process = processBuilder.start();
 
-        return repDeviceVideo;
+            new Thread(() -> {
+                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        System.out.println(line);
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }).start();
+
+            String repDeviceVideo = rtmpIp+":80/hls/"+deviceUuid+".m3u8";
+            return repDeviceVideo;
+
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            return null;
+        }
+
+    }
+
+    @Override
+    public void stopDeviceVideo() {
+        if (process != null && process.isAlive()) {
+            process.destroy();
+            try {
+                process.waitFor();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
     }
 
     @Override