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/OrderController.java |   49 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/OrderController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/OrderController.java
index 0b6870c..1c062f0 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/OrderController.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/OrderController.java
@@ -2,6 +2,7 @@
 
 import cn.afterturn.easypoi.excel.ExcelExportUtil;
 import cn.afterturn.easypoi.excel.entity.ExportParams;
+import cn.hutool.core.io.FileUtil;
 import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.web.page.PageInfo;
 import com.ruoyi.dataInterchange.api.feignClient.PlaybackMsgClient;
@@ -18,10 +19,13 @@
 import com.ruoyi.system.service.IDriverService;
 import com.ruoyi.system.service.IEnterpriseService;
 import com.ruoyi.system.service.IOrderService;
+import com.ruoyi.system.util.JavaCVStreamUtil;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import org.apache.poi.ss.usermodel.Workbook;
+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;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -30,12 +34,17 @@
 import javax.annotation.Resource;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 import java.io.IOException;
 import java.net.URLEncoder;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author zhibing.pu
@@ -62,6 +71,18 @@
 	
 	@Resource
 	private PlaybackMsgClient playbackMsgClient;
+	
+	@Resource
+	private RedisTemplate redisTemplate;
+	
+	@Value("${live.output.path}")
+	private String outputPath;
+	
+	@Value("${live.ip}")
+	private String liveIp;
+	
+	@Value("${live.port}")
+	private Integer livePort;
 	
 	
 	@GetMapping("/getOrderList")
@@ -155,6 +176,8 @@
 			return R.fail("失败");
 		}
 		Car car = carService.getById(order.getCarId());
+		//手动加一次状态数据,避免定时任务结束任务线程
+		redisTemplate.opsForValue().set("live:" + order.getCarId(), true, 1, TimeUnit.MINUTES);
 		Enterprise enterprise = enterpriseService.getById(car.getEnterpriseId());
 		LocalDateTime dateTime = LocalDateTime.parse(order.getOrderTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
 		long startTime = dateTime.toEpochSecond(ZoneOffset.ofHours(8));
@@ -164,8 +187,30 @@
 		if (200 == startupAckVoR.getCode()) {
 			UPPlaybackMsgStartupAckVo data = startupAckVoR.getData();
 			RealVideoResp resp = new RealVideoResp();
-			resp.setServerIp(data.getServerIP());
-			resp.setServerPort(data.getServerPort());
+			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,
+					new LinkedBlockingQueue<Runnable>());
+			executorService.execute(new Runnable() {
+				@Override
+				public void run() {
+					JavaCVStreamUtil.push_hls(data.getUrl(), path, id, folderPath);
+				}
+			});
+			resp.setServerIp(liveIp);
+			resp.setServerPort(livePort);
 			return R.ok(resp);
 		}
 		return R.fail(startupAckVoR.getMsg());

--
Gitblit v1.7.1