zhaowenyu
2025-11-06 12affd880c748ef01a38ee65d0e7e5212945a38d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.ard.sample.component.websocket.handler;
 
import com.ard.common.core.redis.RedisConst;
import com.ard.common.core.redis.RedisOpsUtils;
import com.ard.sample.common.model.ResponseResult;
import com.ard.sample.component.websocket.enums.MessageType;
import com.ard.sample.manage.model.dto.*;
import com.ard.sample.manage.service.IDeviceService;
import com.ard.sample.manage.service.ILiveStreamService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Component
public class LiveStreamHandler {
    @Autowired
    private IDeviceService _deviceService;
 
    @Autowired
    private ILiveStreamService _liveStreamService;
 
    @Autowired
    private DockWebSocketHandler _dockWebSocketHandler;
 
    @Autowired
    protected ObjectMapper _objectMapper;
 
    /**
     * 开启直播
     */
    public void StartLiveStreaming(String sn) {
        // 检查设备是否在线
        if (!_deviceService.checkDeviceOnline(sn)) {
            String jsonString = getResult(sn, "设备不在线,请检查网络连接!");
            _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), jsonString);
            return;
        }
        // 读取直播能力拼接参数
        List<CapacityDeviceDTO> devicesList = _liveStreamService.getLiveCapacity();
        if (devicesList == null || devicesList.isEmpty()) {
            String jsonString = getResult(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(video.getSwitchVideoTypes().get(0));
                    //调用开始直播
                    ResponseResult rlive = null;
                    String resultLive = "";
                    try {
                        rlive = _liveStreamService.liveStart(param);
                        if (rlive != null) {
                            if (rlive.getCode() == 513003) {
                                _liveStreamService.liveStop(param.getVideoId());
                                System.out.println("相机已经在直播中,关闭直播后重启直播!");
                                rlive = _liveStreamService.liveStart(param);
                            }
                            if(rlive.getCode() == 0){
                                // 直播成功后,在redis中存一个标记,将videoid保留,留给停止直播使用
                                System.out.println("开启直播成功!");
                                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);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
                    break;
                }
            }
        }
    }
 
    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, "未找到正在直播的相机!");
        }
        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);
    }
 
    public String getResult(String sn, String msg) {
        Map<String, String> result = new HashMap<>();
        result.put("gateway", sn);
        result.put("code", "1");
        result.put("message", msg);
        result.put("data", null);
        result.put("type", MessageType.LIVESTREAM.getType());
        // 将Map转换为JSON字符串
        String jsonString = "";
        try {
            jsonString = _objectMapper.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return jsonString;
    }
}