|  |  | 
 |  |  | package com.genersoft.iot.vmp.gb28181.session;
 | 
 |  |  | 
 | 
 |  |  | import com.genersoft.iot.vmp.common.InviteSessionType;
 | 
 |  |  | import com.genersoft.iot.vmp.common.VideoManagerConstants;
 | 
 |  |  | import com.genersoft.iot.vmp.conf.UserSetting;
 | 
 |  |  | import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo;
 | 
 |  |  | 
 |  |  | import com.genersoft.iot.vmp.utils.redis.RedisUtil;
 | 
 |  |  | import gov.nist.javax.sip.message.SIPResponse;
 | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
 |  |  | import org.springframework.data.redis.core.RedisTemplate;
 | 
 |  |  | import org.springframework.stereotype.Component;
 | 
 |  |  | import org.springframework.util.ObjectUtils;
 | 
 |  |  | 
 | 
 |  |  | import java.util.ArrayList;
 | 
 |  |  | import java.util.List;
 | 
 |  |  | 
 | 
 |  |  | /**     | 
 |  |  |  * @description:视频流session管理器,管理视频预览、预览回放的通信句柄  | 
 |  |  |  * @author: swwheihei
 | 
 |  |  |  * @date:   2020年5月13日 下午4:03:02      | 
 |  |  | /**
 | 
 |  |  |  * 视频流session管理器,管理视频预览、预览回放的通信句柄
 | 
 |  |  |  */
 | 
 |  |  | @Component
 | 
 |  |  | public class VideoStreamSessionManager {
 | 
 |  |  | 
 |  |  |    @Autowired
 | 
 |  |  |    private UserSetting userSetting;
 | 
 |  |  | 
 | 
 |  |  |    public enum SessionType {
 | 
 |  |  |       play,
 | 
 |  |  |       playback,
 | 
 |  |  |       download
 | 
 |  |  |    }
 | 
 |  |  |    @Autowired
 | 
 |  |  |    private RedisTemplate<Object, Object> redisTemplate;
 | 
 |  |  | 
 | 
 |  |  |    /**
 | 
 |  |  |     * 添加一个点播/回放的事务信息
 | 
 |  |  | 
 |  |  |     * @param mediaServerId 所使用的流媒体ID
 | 
 |  |  |     * @param response 回复
 | 
 |  |  |     */
 | 
 |  |  |    public void put(String deviceId, String channelId, String callId, String stream, String ssrc, String mediaServerId, SIPResponse response, SessionType type){
 | 
 |  |  |    public void put(String deviceId, String channelId, String callId, String stream, String ssrc, String mediaServerId, SIPResponse response, InviteSessionType type){
 | 
 |  |  |       SsrcTransaction ssrcTransaction = new SsrcTransaction();
 | 
 |  |  |       ssrcTransaction.setDeviceId(deviceId);
 | 
 |  |  |       ssrcTransaction.setChannelId(channelId);
 | 
 |  |  | 
 |  |  |       ssrcTransaction.setMediaServerId(mediaServerId);
 | 
 |  |  |       ssrcTransaction.setType(type);
 | 
 |  |  | 
 | 
 |  |  |       RedisUtil.set(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId()
 | 
 |  |  |       redisTemplate.opsForValue().set(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId()
 | 
 |  |  |             + "_" +  deviceId + "_" + channelId + "_" + callId + "_" + stream, ssrcTransaction);
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  | 
 |  |  |          stream ="*";
 | 
 |  |  |       }
 | 
 |  |  |       String key = VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_" + deviceId + "_" + channelId + "_" + callId+ "_" + stream;
 | 
 |  |  |       List<Object> scanResult = RedisUtil.scan(key);
 | 
 |  |  |       List<Object> scanResult = RedisUtil.scan(redisTemplate, key);
 | 
 |  |  |       if (scanResult.size() == 0) {
 | 
 |  |  |          return null;
 | 
 |  |  |       }
 | 
 |  |  |       return (SsrcTransaction)RedisUtil.get((String) scanResult.get(0));
 | 
 |  |  |       return (SsrcTransaction)redisTemplate.opsForValue().get(scanResult.get(0));
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    public List<SsrcTransaction> getSsrcTransactionForAll(String deviceId, String channelId, String callId, String stream){
 | 
 |  |  | 
 |  |  |          stream ="*";
 | 
 |  |  |       }
 | 
 |  |  |       String key = VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_" + deviceId + "_" + channelId + "_" + callId+ "_" + stream;
 | 
 |  |  |       List<Object> scanResult = RedisUtil.scan(key);
 | 
 |  |  |       List<Object> scanResult = RedisUtil.scan(redisTemplate, key);
 | 
 |  |  |       if (scanResult.size() == 0) {
 | 
 |  |  |          return null;
 | 
 |  |  |       }
 | 
 |  |  |       List<SsrcTransaction> result = new ArrayList<>();
 | 
 |  |  |       for (Object keyObj : scanResult) {
 | 
 |  |  |          result.add((SsrcTransaction)RedisUtil.get((String) keyObj));
 | 
 |  |  |          result.add((SsrcTransaction)redisTemplate.opsForValue().get(keyObj));
 | 
 |  |  |       }
 | 
 |  |  |       return result;
 | 
 |  |  |    }
 | 
 |  |  | 
 |  |  |       if (ssrcTransaction == null) {
 | 
 |  |  |          return;
 | 
 |  |  |       }
 | 
 |  |  |       RedisUtil.del(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_"
 | 
 |  |  |       redisTemplate.delete(VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX + userSetting.getServerId() + "_"
 | 
 |  |  |             +  deviceId + "_" + channelId + "_" + ssrcTransaction.getCallId() + "_" + ssrcTransaction.getStream());
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  | 
 | 
 |  |  |    public List<SsrcTransaction> getAllSsrc() {
 | 
 |  |  |       List<Object> ssrcTransactionKeys = RedisUtil.scan(String.format("%s_*_*_*_*", VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX+ userSetting.getServerId()));
 | 
 |  |  |       List<Object> ssrcTransactionKeys = RedisUtil.scan(redisTemplate, String.format("%s_*_*_*_*", VideoManagerConstants.MEDIA_TRANSACTION_USED_PREFIX+ userSetting.getServerId()));
 | 
 |  |  |       List<SsrcTransaction> result= new ArrayList<>();
 | 
 |  |  |       for (int i = 0; i < ssrcTransactionKeys.size(); i++) {
 | 
 |  |  |          String key = (String)ssrcTransactionKeys.get(i);
 | 
 |  |  |          SsrcTransaction ssrcTransaction = JsonUtil.redisJsonToObject(key, SsrcTransaction.class);
 | 
 |  |  |       for (Object ssrcTransactionKey : ssrcTransactionKeys) {
 | 
 |  |  |          String key = (String) ssrcTransactionKey;
 | 
 |  |  |          SsrcTransaction ssrcTransaction = JsonUtil.redisJsonToObject(redisTemplate, key, SsrcTransaction.class);
 | 
 |  |  |          result.add(ssrcTransaction);
 | 
 |  |  |       }
 | 
 |  |  |       return result;
 |