648540858
2023-07-17 5c49627012996aedb802b81e64c4776f9874f0a7
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
package com.genersoft.iot.vmp.media.zlm;
 
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
import com.genersoft.iot.vmp.media.zlm.dto.MediaSendRtpPortInfo;
import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class SendRtpPortManager {
 
    private final static Logger logger = LoggerFactory.getLogger(SendRtpPortManager.class);
 
    @Autowired
    private UserSetting userSetting;
 
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
 
    private final String KEY = "VM_MEDIA_SEND_RTP_PORT_RANGE_";
 
 
    public void initServerPort(String mediaServerId, int startPort, int endPort){
        String key = KEY + userSetting.getServerId() + "_" +  mediaServerId;
        MediaSendRtpPortInfo mediaSendRtpPortInfo = new MediaSendRtpPortInfo(startPort, endPort, mediaServerId);
        redisTemplate.opsForValue().set(key, mediaSendRtpPortInfo);
    }
 
    public int getNextPort(String mediaServerId) {
        String sendIndexKey = KEY + userSetting.getServerId() + "_" +  mediaServerId;
        MediaSendRtpPortInfo mediaSendRtpPortInfo = (MediaSendRtpPortInfo)redisTemplate.opsForValue().get(sendIndexKey);
        if (mediaSendRtpPortInfo == null) {
            logger.warn("[发送端口管理] 获取{}的发送端口时未找到端口信息", mediaSendRtpPortInfo);
            return 0;
        }
 
        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX
                + userSetting.getServerId() + "_*";
        List<Object> queryResult = RedisUtil.scan(redisTemplate, key);
        Map<Integer, SendRtpItem> sendRtpItemMap = new HashMap<>();
 
        for (Object o : queryResult) {
            SendRtpItem sendRtpItem = (SendRtpItem) redisTemplate.opsForValue().get(o);
            if (sendRtpItem != null) {
                sendRtpItemMap.put(sendRtpItem.getLocalPort(), sendRtpItem);
            }
        }
 
        int port = getPort(mediaSendRtpPortInfo.getCurrent(),
                mediaSendRtpPortInfo.getStart(),
                mediaSendRtpPortInfo.getEnd(), checkPort -> sendRtpItemMap.get(checkPort) == null);
 
        mediaSendRtpPortInfo.setCurrent(port);
        redisTemplate.opsForValue().set(sendIndexKey, mediaSendRtpPortInfo);
        return port;
    }
 
    interface CheckPortCallback{
        boolean check(int port);
    }
 
    private int getPort(int current, int start, int end, CheckPortCallback checkPortCallback) {
        int port;
        if (current %2 != 0) {
            port = current + 1;
        }else {
            port = current + 2;
        }
        if (port > end) {
            if (start %2 != 0) {
                port = start + 1;
            }else {
                port = start;
            }
        }
        if (!checkPortCallback.check(port)) {
            return getPort(port, start, end, checkPortCallback);
        }
        return port;
    }
}