liusuyi
2026-06-01 a2e7e8ff9cfaa69b001d483710bddbda50d55c91
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
package com.ard.zlm.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.ard.zlm.config.UserSetting;
import com.ard.zlm.constants.VideoManagerConstants;
import com.ard.zlm.api.domain.MediaInfo;
import com.ard.zlm.domain.StreamAuthorityInfo;
import com.ard.zlm.api.domain.ZlmMediaServer;
import com.ard.zlm.service.IRedisCatchStorage;
import com.ard.zlm.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * @FileName RedisCatchStorageImpl
 * @Description
 * @Author fengcheng
 * @date 2026-04-01
 **/
@SuppressWarnings("rawtypes")
@Slf4j
@Component
public class RedisCatchStorageImpl implements IRedisCatchStorage {
 
    @Autowired
    private UserSetting userSetting;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
 
    @Override
    public void addStream(ZlmMediaServer mediaServerItem, String type, String app, String streamId, MediaInfo mediaInfo) {
        // 查找是否使用了callID
        StreamAuthorityInfo streamAuthorityInfo = getStreamAuthorityInfo(app, streamId);
        String key = VideoManagerConstants.ZLM_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_" + type.toUpperCase() + "_" + app + "_" + streamId + "_" + mediaServerItem.getId();
        if (streamAuthorityInfo != null) {
            mediaInfo.setCallId(streamAuthorityInfo.getCallId());
        }
        redisTemplate.opsForValue().set(key, JSON.toJSONString(mediaInfo));
    }
 
    @Override
    public void removeStream(String mediaServerId, String type, String app, String streamId) {
        String key = VideoManagerConstants.ZLM_SERVER_STREAM_PREFIX + userSetting.getServerId() + "_" + type.toUpperCase() + "_"  + app + "_" + streamId + "_" + mediaServerId;
        redisTemplate.delete(key);
    }
 
    /**
     * 获取流信息
     *
     * @param app
     * @param streamId
     * @param mediaServerId
     * @return
     */
    @Override
    public MediaInfo getStreamInfo(String app, String streamId, String mediaServerId) {
        String scanKey = VideoManagerConstants.ZLM_SERVER_STREAM_PREFIX  + userSetting.getServerId() + "_*_" + app + "_" + streamId + "_" + mediaServerId;
 
        MediaInfo result = null;
        List<Object> keys = RedisUtil.scan(redisTemplate, scanKey);
        if (keys.size() > 0) {
            String key = (String) keys.get(0);
            String mediaInfoJson = (String)redisTemplate.opsForValue().get(key);
            result = JSON.parseObject(mediaInfoJson, MediaInfo.class);
        }
 
        return result;
    }
 
    public StreamAuthorityInfo getStreamAuthorityInfo(String app, String stream) {
        String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY;
        String objectKey = app+ "_" + stream;
        return (StreamAuthorityInfo)redisTemplate.opsForHash().get(key, objectKey);
    }
 
    /**
     * 存储推流的鉴权信息
     *
     * @param app 应用名
     * @param stream 流
     * @param streamAuthorityInfo 鉴权信息
     */
    @Override
    public void updateStreamAuthorityInfo(String app, String stream, StreamAuthorityInfo streamAuthorityInfo) {
        String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY;
        String objectKey = app+ "_" + stream;
        redisTemplate.opsForHash().put(key, objectKey, streamAuthorityInfo);
    }
 
    /**
     * 添加推流列表信息到redis
     *
     * @param app
     * @param stream
     * @param mediaInfo
     */
    @Override
    public void addPushListItem(String app, String stream, MediaInfo mediaInfo) {
        String key = VideoManagerConstants.PUSH_STREAM_LIST + app + "_" + stream;
        redisTemplate.opsForValue().set(key, mediaInfo);
    }
 
    /**
     * 获取推流列表信息从redis
     *
     * @param app
     * @param stream
     * @return
     */
    @Override
    public MediaInfo getPushListItem(String app, String stream) {
        String key = VideoManagerConstants.PUSH_STREAM_LIST + app + "_" + stream;
        return (MediaInfo)redisTemplate.opsForValue().get(key);
    }
 
    /**
     * 移除推流的鉴权信息
     *
     * @param app
     * @param stream
     * @param id
     */
    @Override
    public void removePushListItem(String app, String stream, String id) {
        String key = VideoManagerConstants.PUSH_STREAM_LIST + app + "_" + stream;
        MediaInfo param = (MediaInfo)redisTemplate.opsForValue().get(key);
        if (param != null) {
            redisTemplate.delete(key);
        }
    }
}