Browse Source

优化实时拉取摄像头视频流接口,实现界面停止调用后ffmpeg拉流停止的逻辑

james 2 weeks ago
parent
commit
82616a1d8d

+ 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);

+ 30 - 22
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/VcDeviceServiceImpl.java

@@ -38,6 +38,8 @@ public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcD
     @Value("${vc.rtmp-ip}")
     private String rtmpIp;
 
+    Process process = null;
+
     @Override
     public List<VcDevice> VcDeviceList(Integer groupId){
         LambdaQueryWrapper<VcDevice> queryWrapper = Wrappers.lambdaQuery();
@@ -73,9 +75,9 @@ 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);
-        Process process = null;
+
         try {
             log.info("【begin】实时拉取摄像头数据流 "+command);
             // 创建 ProcessBuilder 实例
@@ -83,26 +85,22 @@ public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcD
             // 启动进程
             process = processBuilder.start();
 
-            // 获取进程的输入流
-//            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-//            String line;
-//            while ((line = reader.readLine()) != null) {
-//                System.out.println(line);
-//            }
-
-            // 等待进程执行完成并获取退出码
-            int exitCode = process.waitFor();
-            log.info("【end】实时拉取摄像头数据流 "+command+ "命令执行完成,退出码: " + exitCode);
-            if (exitCode == 0) {
-                System.out.println("FFmpeg 命令执行成功");
-                String repDeviceVideo = rtmpIp+":80/hls/"+deviceUuid+".m3u8";
-                return repDeviceVideo;
-            } else {
-                System.out.println("FFmpeg 命令执行失败,退出码: " + exitCode);
-                return null;
-            }
+            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 | InterruptedException e) {
+        } catch (IOException e) {
             e.printStackTrace();
             return null;
         } finally {
@@ -111,8 +109,18 @@ public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcD
             }
         }
 
+    }
 
-
+    @Override
+    public void stopDeviceVideo() {
+        if (process != null && process.isAlive()) {
+            process.destroy();
+            try {
+                process.waitFor();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
     }
 
     @Override