liusuyi
2026-06-01 3496700a5ba18be8ca0590a79e21a867782d1ea9
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.ard.zlm.service.impl;
 
import com.ard.common.core.constant.Constants;
import com.ard.common.core.constant.SecurityConstants;
import com.ard.common.core.domain.R;
import com.ard.gb28181.api.RemoteGb28181Service;
import com.ard.gb28181.api.domain.Device;
import com.ard.zlm.service.ZlmStreamService;
import com.ard.zlm.domain.StreamChannel;
import com.ard.zlm.api.domain.RTPServerParam;
import com.ard.zlm.api.domain.StreamPullPlay;
import com.ard.zlm.api.domain.ZlmMediaServer;
import com.ard.zlm.api.utils.MediaServerUtils;
import com.ard.zlm.common.InviteSessionType;
import com.ard.zlm.config.UserSetting;
import com.ard.zlm.domain.StreamAuthorityInfo;
import com.ard.zlm.hook.ResultForOnPublish;
import com.ard.zlm.service.IInviteStreamService;
import com.ard.zlm.service.IMediaServerService;
import com.ard.zlm.service.IMediaService;
import com.ard.zlm.service.IRedisCatchStorage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
 
import java.util.Map;
 
@Slf4j
@Service
public class MediaServiceImpl implements IMediaService {
 
    @Autowired
    private ZlmStreamService zlmStreamService;
 
    @Autowired
    private IMediaServerService mediaServerService;
 
    @Autowired
    private IInviteStreamService inviteStreamService;
 
    @Autowired
    private UserSetting userSetting;
 
    @Autowired
    private IRedisCatchStorage redisCatchStorage;
 
    @Autowired
    private RemoteGb28181Service remoteGb28181Service;
 
    @Override
    public boolean closeStreamOnNoneReader(String mediaServerId, String app, String stream, String schema) {
 
//        R<StreamChannel> r = zlmStreamService.getQsDeviceStream(stream, SecurityConstants.INNER);
//        if (r.getCode() != Constants.SUCCESS) {
//            return false;
//        }
//
//        StreamChannel data = r.getData();
//        if (data == null) {
//            return false;
//        }
//
//        // 拉流代理
//        if ("rtsp".equals(app) || "rtmp".equals(app) || "flv".equals(app) || "hls".equals(app)) {
//            if ("1".equals(data.getEnableDisableNoneReader())) {
//                // 无人观看停用
//                // 修改数据
//                StreamPullPlay streamPullPlay = new StreamPullPlay();
//                streamPullPlay.setDeviceId(data.getId());
//                streamPullPlay.setStreamKey(data.getStreamKey());
//                streamPullPlay.setMediaServerId(data.getMediaServerId());
//
//                mediaServerService.stopStreamPullPlay(streamPullPlay);
//                return true;
//            } else {
//                return false;
//            }
//        } else if ("haikang".equals(app) || "haikang_isup".equals(app) || "dahua".equals(app)) {
//            if ("1".equals(data.getEnableDisableNoneReader())) {
//                // 无人观看停用
//                RTPServerParam rtpServerParam = new RTPServerParam();
//                rtpServerParam.setId(data.getId());
//                rtpServerParam.setType(data.getType());
//                rtpServerParam.setStreamId(stream);
//                mediaServerService.stopRtpPlay(rtpServerParam);
//                return true;
//            } else {
//                return false;
//            }
//        } else if ("push".equals(app)) {
//            if ("1".equals(data.getEnableDisableNoneReader())) {
//                return true;
//            } else {
//                return false;
//            }
//        } else if ("gb28181".equals(app)) {
//            if ("1".equals(data.getEnableDisableNoneReader())) {
//                R<Device> deviceR = remoteGb28181Service.getDeviceByDeviceId(data.getGbDeviceId(), SecurityConstants.INNER);
//                if (deviceR.getCode() == Constants.SUCCESS && deviceR.getData() != null) {
//                    mediaServerService.stopGb28181Play(InviteSessionType.PLAY, data, deviceR.getData(), data.getDeviceCode());
//                }
//                return true;
//            } else {
//                return false;
//            }
//        }
        return true;
    }
 
    @Override
    public ResultForOnPublish authenticatePublish(ZlmMediaServer mediaServer, String app, String stream, String params) {
        ResultForOnPublish result = new ResultForOnPublish();
        result.setEnable_audio(true);
 
        // 海康sdk 海康isup 大华sdk
        if ("haikang".equals(app) || "haikang_isup".equals(app) || "dahua".equals(app)) {
            R<StreamChannel> r = zlmStreamService.getQsDeviceStream(stream, SecurityConstants.INNER);
 
            if (r.getCode() != Constants.SUCCESS) {
                result.setEnable_mp4(false);
            } else if (r.getData() == null) {
                result.setEnable_mp4(false);
            } else {
                result.setEnable_mp4("1".equals(r.getData().getEnableMp4()));
            }
        }
        // 推流
        if ("push".equals(app)) {
            R<StreamChannel> r = zlmStreamService.getQsDeviceStream(stream, SecurityConstants.INNER);
 
            if (r.getCode() != Constants.SUCCESS) {
                result.setEnable_mp4(false);
            } else if (r.getData() == null) {
                result.setEnable_mp4(false);
            } else {
                result.setEnable_mp4("1".equals(r.getData().getEnableMp4()));
            }
            if (userSetting.getPushAuthority()) {
                // 对于推流进行鉴权
                Map<String, String> paramMap = MediaServerUtils.urlParamToMap(params);
                // 推流鉴权
                if (params == null) {
                    log.info("推流鉴权失败: 缺少必要参数:sign=md5(user表的pushKey)");
                    throw new RuntimeException("Unauthorized");
                }
 
                String sign = paramMap.get("sign");
                if (sign == null) {
                    log.info("推流鉴权失败: 缺少必要参数:sign=md5");
                    throw new RuntimeException("Unauthorized");
                }
                sign = sign.replaceAll("/$", "");
                // 推流自定义播放鉴权码
                String callId = paramMap.get("callId");
                // 鉴权配置
                String checkStr = callId == null ? userSetting.getPushKey() : (callId + "_" + userSetting.getPushKey());
                String checkSign = DigestUtils.md5DigestAsHex(checkStr.getBytes());
                if (!checkSign.equals(sign)) {
                    log.info("推流鉴权失败: sign 无权限: callId={}. sign={}", callId, sign);
                    throw new RuntimeException("推流鉴权失败: sign 无权限: callId=" + callId + ". sign=" + sign);
                }
 
                StreamAuthorityInfo streamAuthorityInfo = StreamAuthorityInfo.getInstanceByHook(app, stream, mediaServer.getId());
                streamAuthorityInfo.setCallId(callId);
                streamAuthorityInfo.setSign(sign);
                // 鉴权通过
                redisCatchStorage.updateStreamAuthorityInfo(app, stream, streamAuthorityInfo);
            }
        }
 
        return result;
    }
}