Browse Source

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

james 2 weeks ago
parent
commit
f197bf950d

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

+ 33 - 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,15 +31,13 @@ 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;
-
     @Override
     public List<VcDevice> VcDeviceList(Integer groupId){
         LambdaQueryWrapper<VcDevice> queryWrapper = Wrappers.lambdaQuery();
@@ -80,11 +75,39 @@ public class VcDeviceServiceImpl extends AbstractCrudService<VcDeviceMapper, VcD
         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;
         System.out.println(command);
-        vcVideo.processVcVideo(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);
+            }
+
+            // 等待进程执行完成并获取退出码
+            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;
+            }
+
+        } catch (IOException | InterruptedException e) {
+            e.printStackTrace();
+            return null;
+        }
+
 
-        String repDeviceVideo = rtmpIp+":80/hls/"+deviceUuid+".m3u8";
 
-        return repDeviceVideo;
     }
 
     @Override