From c447c60bd5e27627aa77e56f985dea536be05764 Mon Sep 17 00:00:00 2001
From: zhaowenyu <35589726@qq.com>
Date: Sat, 08 Nov 2025 13:05:41 +0800
Subject: [PATCH] 2025-11-8 zwy 修改websocket视频接口的逻辑
---
ard-sample/src/main/java/com/ard/sample/component/websocket/model/Payload.java | 5 -
ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java | 7 +
ard-admin/src/main/java/com/ard/web/controller/sample/LiveStreamController.java | 19 ++++
ard-common/src/main/java/com/ard/common/core/redis/RedisConst.java | 2
ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java | 174 +++++++++++++++++++++---------------------
5 files changed, 112 insertions(+), 95 deletions(-)
diff --git a/ard-admin/src/main/java/com/ard/web/controller/sample/LiveStreamController.java b/ard-admin/src/main/java/com/ard/web/controller/sample/LiveStreamController.java
index 03a960e..1e0f8f1 100644
--- a/ard-admin/src/main/java/com/ard/web/controller/sample/LiveStreamController.java
+++ b/ard-admin/src/main/java/com/ard/web/controller/sample/LiveStreamController.java
@@ -4,8 +4,10 @@
import com.ard.common.utils.SecurityUtils;
import com.ard.sample.common.model.ResponseResult;
import com.ard.sample.component.mqtt.model.ChannelName;
+import com.ard.sample.component.websocket.handler.LiveStreamHandler;
import com.ard.sample.manage.model.dto.CapacityDeviceDTO;
import com.ard.sample.manage.model.dto.LiveTypeDTO;
+import com.ard.sample.manage.model.param.RemoteDebugParam;
import com.ard.sample.manage.model.receiver.LiveCapacityReceiver;
import com.ard.sample.manage.service.ILiveStreamService;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -37,6 +39,9 @@
private ILiveStreamService liveStreamService;
@Autowired
+ private LiveStreamHandler _liveStreamHandler;
+
+ @Autowired
private ObjectMapper mapper;
/**
@@ -66,6 +71,20 @@
return ResponseResult.success(liveCapacity);
}
+ @ApiOperation("获取video_id")
+ @PostMapping("/videoid/{sn}")
+ public String getVideoId(@PathVariable String sn) {
+ String videoId = "";
+ try{
+ videoId = _liveStreamHandler.getLiveParam(sn).getVideoId();
+ }
+ catch (Exception e){
+ videoId = e.getMessage();
+ e.printStackTrace();
+ }
+ return videoId;
+ }
+
/**
* Live streaming according to the parameters passed in from the web side.
*
diff --git a/ard-common/src/main/java/com/ard/common/core/redis/RedisConst.java b/ard-common/src/main/java/com/ard/common/core/redis/RedisConst.java
index b408979..e1101c7 100644
--- a/ard-common/src/main/java/com/ard/common/core/redis/RedisConst.java
+++ b/ard-common/src/main/java/com/ard/common/core/redis/RedisConst.java
@@ -19,7 +19,7 @@
public static final Integer WEBSOCKET_ALIVE_SECOND = 60 * 60 * 24;
- public static final String WEBSOCKET_VideoId_PREFIX = "videoid" + DELIMITER;
+ public static final String WEBSOCKET_VIDEO_ADDRESS_PREFIX = "videoaddress" + DELIMITER;
public static final String DEVICE_ONLINE_PREFIX = "online" + DELIMITER;
diff --git a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java
index 89ccdd4..df43823 100644
--- a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java
+++ b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java
@@ -87,7 +87,6 @@
private void handleSubscription(String userId, String type, Payload payload) {
String sn = payload.getSn();
boolean subscribe = payload.isSubscribe();
- String videoType = payload.getVideotype();
String subscriptionKey = getSubscriptionKey(sn, type); // 生成唯一订阅标识
if (subscribe) {
@@ -99,7 +98,7 @@
// 开启直播
// 开启直播逻辑
System.out.println("开启直播");
- _liveStreamService.StartLiveStreaming(sn, videoType);
+ _liveStreamService.StartLiveStreaming(sn);
}
} else {
// 停止直播
@@ -140,6 +139,10 @@
*/
private void removeUserAllSubscriptions(String userId) {
for (Map.Entry<String, Set<String>> entry : subscriptions.entrySet()) {
+ if (entry.getKey().contains(MessageType.LIVESTREAM.getType())) {
+ String sn = entry.getKey().split(":")[0];
+ _liveStreamService.StopLiveStreaming(sn);
+ }
entry.getValue().remove(userId);
if (entry.getValue().isEmpty()) {
subscriptions.remove(entry.getKey());
diff --git a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
index 3a0453d..6468eaf 100644
--- a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
+++ b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
@@ -20,110 +20,68 @@
@Component
public class LiveStreamHandler {
@Autowired
+ protected ObjectMapper _objectMapper;
+ @Autowired
private IDeviceService _deviceService;
-
@Autowired
private ILiveStreamService _liveStreamService;
-
@Autowired
private DockWebSocketHandler _dockWebSocketHandler;
-
- @Autowired
- protected ObjectMapper _objectMapper;
/**
* 开启直播
*/
- public void StartLiveStreaming(String sn, String videotype ) {
- // 检查设备是否在线
- if (!_deviceService.checkDeviceOnline(sn)) {
- String jsonString = getResult(sn, "设备不在线,请检查网络连接!");
- log.info("SN: {}-设备不在线,请检查网络连接!", sn);
- _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), jsonString);
- return;
- }
- // 读取直播能力拼接参数
- List<CapacityDeviceDTO> devicesList = _liveStreamService.getLiveCapacity();
- if (devicesList == null || devicesList.isEmpty()) {
- String jsonString = getResult(sn, "未获得设备直播能力!");
- log.info("SN: {}-未获得设备直播能力!", sn);
- _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), jsonString);
- return;
- }
- for (CapacityDeviceDTO device : devicesList) {
- if (!device.getSn().equals(sn)) {
- continue;
- }
- List<CapacityCameraDTO> camerasList = device.getCamerasList();
- for (CapacityCameraDTO camera : camerasList) {
- if (camera.getName()==null || camera.getName().isEmpty()) {
- continue;
- }
- LiveTypeDTO param = new LiveTypeDTO();
- String videoid = device.getSn() + "/" + camera.getIndex();
- List<CapacityVideoDTO> videosList = camera.getVideosList();
- for (CapacityVideoDTO video : videosList) {
- videoid = videoid + "/" + video.getIndex();
- param.setUrlType(1);
- param.setUrl("");
- param.setVideoId(videoid);
- param.setVideoQuality(1);
- param.setVideoType(videotype);
- //调用开始直播
- ResponseResult rlive = null;
- String resultLive = "";
- try {
- rlive = _liveStreamService.liveStart(param);
- if (rlive != null) {
-// if (rlive.getCode() == 513003) {
- while (rlive.getCode() == 513003) {
- //Thread.sleep(200);
- _liveStreamService.liveStop(param.getVideoId());
- //System.out.println("相机已经在直播中,关闭直播后重启直播!");
- log.info("SN: {}-相机已经在直播中,关闭直播后重启直播!", sn);
- rlive = _liveStreamService.liveStart(param);
- }
- if(rlive.getCode() == 0){
- // 直播成功后,在redis中存一个标记,将videoid保留,留给停止直播使用
- //System.out.println("开启直播成功!");
- log.info("SN: {}-开启直播成功!", sn);
- RedisOpsUtils.setWithExpire(RedisConst.WEBSOCKET_VideoId_PREFIX + sn, param.getVideoId(), RedisConst.WEBSOCKET_ALIVE_SECOND);
- }
- rlive.setGateway(sn);
- rlive.setType(MessageType.LIVESTREAM.getType());
- resultLive = _objectMapper.writeValueAsString(rlive);
- _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
- log.info("SN: {}-直播地址:{}", sn, resultLive);
- break;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
+ public void StartLiveStreaming(String sn) {
+ //调用开始直播
+ ResponseResult rlive = null;
+ String resultLive = "";
+ try {
+ // 获取直播参数
+ LiveTypeDTO param = getLiveParam(sn);
+ // 如果存在直播则读取直播地址返回
+ if (RedisOpsUtils.checkExist(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn)) {
+ resultLive = String.valueOf(RedisOpsUtils.get(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn));
+ } else {
+ log.info("SN: {}-redis中未找到相机的播放地址信息!停止直播后再开启直播", sn);
+ StopLiveStreaming(sn);
+ // 如果不存在直播,则开始直播,并返回直播地址
+ rlive = _liveStreamService.liveStart(param);
+ if (rlive.getCode() == 0) {
+ // 直播成功后,在redis中存一个标记,将videoid保留,留给停止直播使用
+ log.info("SN: {}-开启直播成功!", sn);
+ rlive.setGateway(sn);
+ rlive.setType(MessageType.LIVESTREAM.getType());
+ resultLive = _objectMapper.writeValueAsString(rlive);
+ RedisOpsUtils.set(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn, resultLive);
}
}
+ } catch (Exception e) {
+ log.info("SN: {}-开启直播失败!原因:{}", sn, e.getMessage());
+ String jsonString = getResult(sn, "开启直播失败!原因:" + e.getMessage());
+ _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), jsonString);
+ e.printStackTrace();
}
+
+ _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
+ log.info("SN: {}-直播地址:{}", sn, resultLive);
}
public void StopLiveStreaming(String sn) {
- // 停止直播逻辑
ResponseResult rlive = null;
- String resultLive = "";
-// System.out.println("关闭直播");
- String videoid = "";
- if(!RedisOpsUtils.checkExist(RedisConst.WEBSOCKET_VideoId_PREFIX + sn)){
- resultLive = getResult(sn, "未找到正在直播的相机!");
+// String resultLive = "";
+ try {
+ // 获取直播参数
+ LiveTypeDTO param = getLiveParam(sn);
+ // 停止直播逻辑
+ rlive = _liveStreamService.liveStop(param.getVideoId());
+// rlive.setGateway(sn);
+// rlive.setType(MessageType.LIVESTREAM.getType());
+// resultLive = _objectMapper.writeValueAsString(rlive);
+ RedisOpsUtils.del(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn);
+ } catch (Exception e) {
+ e.printStackTrace();
}
- else {
- videoid = String.valueOf(RedisOpsUtils.get(RedisConst.WEBSOCKET_VideoId_PREFIX + sn));
- try {
- rlive = _liveStreamService.liveStop(videoid);
- rlive.setGateway(sn);
- resultLive = _objectMapper.writeValueAsString(rlive);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
+ //_dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
}
public String getResult(String sn, String msg) {
@@ -143,4 +101,46 @@
return jsonString;
}
+
+ /**
+ * 获取直播参数
+ */
+ public LiveTypeDTO getLiveParam(String sn) throws Exception {
+ LiveTypeDTO param = new LiveTypeDTO();
+ // 检查设备是否在线
+ if (!_deviceService.checkDeviceOnline(sn)) {
+ log.info("SN: {}-设备不在线,请检查网络连接!", sn);
+ // 抛弃出异常,给调用者处理
+ throw new Exception("设备不在线,请检查网络连接!");
+ }
+ // 读取直播能力拼接参数
+ List<CapacityDeviceDTO> devicesList = _liveStreamService.getLiveCapacity();
+ if (devicesList == null || devicesList.isEmpty()) {
+ log.info("SN: {}-未获得设备直播能力!", sn);
+ // 抛弃出异常,给调用者处理
+ throw new Exception("未获得设备直播能力!");
+ }
+ for (CapacityDeviceDTO device : devicesList) {
+ if (!device.getSn().equals(sn)) {
+ continue;
+ }
+ List<CapacityCameraDTO> camerasList = device.getCamerasList();
+ for (CapacityCameraDTO camera : camerasList) {
+ if (camera.getName() == null || camera.getName().isEmpty()) {
+ continue;
+ }
+ String videoid = device.getSn() + "/" + camera.getIndex();
+ List<CapacityVideoDTO> videosList = camera.getVideosList();
+ for (CapacityVideoDTO video : videosList) {
+ videoid = videoid + "/" + video.getIndex();
+ param.setUrlType(1);
+ param.setUrl("");
+ param.setVideoId(videoid);
+ param.setVideoQuality(1);
+ param.setVideoType(video.getSwitchVideoTypes().get(0));
+ }
+ }
+ }
+ return param;
+ }
}
diff --git a/ard-sample/src/main/java/com/ard/sample/component/websocket/model/Payload.java b/ard-sample/src/main/java/com/ard/sample/component/websocket/model/Payload.java
index 34c8d78..cbe293f 100644
--- a/ard-sample/src/main/java/com/ard/sample/component/websocket/model/Payload.java
+++ b/ard-sample/src/main/java/com/ard/sample/component/websocket/model/Payload.java
@@ -21,9 +21,4 @@
* 是否订阅
* */
private boolean subscribe;
-
- /**
- * 直播视频流镜头类型 enum_string {"ir":"红外","normal":"默认","wide":"广角","zoom":"变焦"}
- * */
- private String videotype;
}
--
Gitblit v1.9.3