From 0f9ecda1919beb1e8fbbca0f669ac86badf70806 Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期四, 10 四月 2025 14:47:42 +0800
Subject: [PATCH] 优化推流和拉流功能及关闭逻辑

---
 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CarController.java |   72 +++++++++++++++++++++++++++++++-----
 1 files changed, 62 insertions(+), 10 deletions(-)

diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CarController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CarController.java
index b0f5363..f5d89b4 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CarController.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CarController.java
@@ -1,5 +1,6 @@
 package com.ruoyi.system.controller;
 
+import cn.hutool.core.io.FileUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.utils.StringUtils;
@@ -24,6 +25,7 @@
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -31,6 +33,8 @@
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
@@ -48,7 +52,14 @@
 @RequestMapping("/car")
 public class CarController {
 	
-	private String outputUrl = "F:\\nginx 1.7.11.3 Gryphon\\html\\";
+	@Value("${live.output.path}")
+	private String outputPath;
+	
+	@Value("${live.ip}")
+	private String liveIp;
+	
+	@Value("${live.port}")
+	private Integer livePort;
 	@Resource
 	private ICarService carService;
 	@Resource
@@ -106,12 +117,25 @@
 		if (null == car) {
 			return R.fail("失败");
 		}
+		//手动加一次状态数据,避免定时任务结束任务线程
+		redisTemplate.opsForValue().set("live:" + id, true, 1, TimeUnit.MINUTES);
 		Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
 		R<UPRealvideoMsgStartupAckVo> msgStartupAckVoR = realVideoMsgClient.startupRealVideo(Integer.valueOf(enterprise.getCode()), car.getVehicleNumber());
 		if (200 == msgStartupAckVoR.getCode()) {
+			String path = outputPath + "hls\\" + car.getVehicleNumber() + "\\live.m3u8";
+			String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
+			FileUtil.mkParentDirs(path);
+			File file = new File(path);
+			if (!file.exists()) {
+				try {
+					file.createNewFile();
+				} catch (IOException e) {
+					throw new RuntimeException(e);
+				}
+			}
+			
 			UPRealvideoMsgStartupAckVo data = msgStartupAckVoR.getData();
 			RealVideoResp resp = new RealVideoResp();
-			String url = "rtmp://192.168.110.85:1935/flv-live/" + car.getVehicleNumber();
 			//执行拉流和推流
 			ExecutorService executorService = new ThreadPoolExecutor(1, 1,
 					0L, TimeUnit.MILLISECONDS,
@@ -119,10 +143,11 @@
 			executorService.execute(new Runnable() {
 				@Override
 				public void run() {
-					JavaCVStreamUtil.push_flv(data.getUrl(), url, id);
+					JavaCVStreamUtil.push_hls(data.getUrl(), path, id, folderPath);
 				}
 			});
-			resp.setUrl(url);
+			resp.setServerIp(liveIp);
+			resp.setServerPort(livePort);
 			return R.ok(resp);
 		}
 		
@@ -135,13 +160,26 @@
 	@ApiImplicitParams({
 			@ApiImplicitParam(value = "车辆id", name = "id", required = true)
 	})
-	public R closeRealVideo(@PathVariable("id") Integer id){
-		JavaCVStreamUtil.close(id);
+	public R closeRealVideo(@PathVariable("id") Integer id) {
+		Car car = carService.getById(id);
+		if (null == car) {
+			return R.fail("失败");
+		}
+		String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
+		JavaCVStreamUtil.close(id, folderPath);
 		return R.ok();
 	}
 	
 	
-	
+	@GetMapping("/playDetection/{id}")
+	@ApiOperation(value = "播放检测", tags = {"车辆管理"})
+	@ApiImplicitParams({
+			@ApiImplicitParam(value = "车辆id", name = "id", required = true)
+	})
+	public R playDetection(@PathVariable("id") Integer id) {
+		redisTemplate.opsForValue().set("live:" + id, true, 1, TimeUnit.MINUTES);
+		return R.ok();
+	}
 	
 	
 	@GetMapping("/getPlaybackVideo")
@@ -151,13 +189,26 @@
 		if (null == car) {
 			return R.fail("失败");
 		}
+		//手动加一次状态数据,避免定时任务结束任务线程
+		redisTemplate.opsForValue().set("live:" + req.getId(), true, 1, TimeUnit.MINUTES);
 		Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
 		R<UPPlaybackMsgStartupAckVo> startupAckVoR = playbackMsgClient.playbackMsgStartup(Integer.valueOf(enterprise.getCode()), car.getVehicleNumber(),
 				req.getStartTime(), req.getEndTime());
 		if (200 == startupAckVoR.getCode()) {
 			UPPlaybackMsgStartupAckVo data = startupAckVoR.getData();
 			RealVideoResp resp = new RealVideoResp();
-			String url = "rtmp://192.168.110.85:1935/flv-live/" + car.getVehicleNumber();
+			String path = outputPath + "hls\\" + car.getVehicleNumber() + "\\live.m3u8";
+			String folderPath = outputPath + "hls\\" + car.getVehicleNumber();
+			FileUtil.mkParentDirs(path);
+			File file = new File(path);
+			if (!file.exists()) {
+				try {
+					file.createNewFile();
+				} catch (IOException e) {
+					throw new RuntimeException(e);
+				}
+			}
+			
 			//执行拉流和推流
 			ExecutorService executorService = new ThreadPoolExecutor(1, 1,
 					0L, TimeUnit.MILLISECONDS,
@@ -165,10 +216,11 @@
 			executorService.execute(new Runnable() {
 				@Override
 				public void run() {
-					JavaCVStreamUtil.push_flv(data.getUrl(), url, req.getId());
+					JavaCVStreamUtil.push_hls(data.getUrl(), path, req.getId(), folderPath);
 				}
 			});
-			resp.setUrl(url);
+			resp.setServerIp(liveIp);
+			resp.setServerPort(livePort);
 			return R.ok(resp);
 		}
 		return R.fail(startupAckVoR.getMsg());

--
Gitblit v1.7.1