Commit 1ae6cdcb9a88eabda091b1ea8476acd05c108331

Authored by chenjunyu_1481036421
1 parent b68ce5d0

feat:萤石增加多人观看策略

@@ -92,6 +92,17 @@ public class TkVideoController extends BaseController { @@ -92,6 +92,17 @@ public class TkVideoController extends BaseController {
92 return getCameraURL(entityId, null); 92 return getCameraURL(entityId, null);
93 } 93 }
94 94
  95 + @GetMapping("outVideo/{entityId}")
  96 + @ApiOperation("退出视频流播放")
  97 + public void outVideo(
  98 + @PathVariable("entityId") String entityId) throws ThingsboardException {
  99 + videoService.outVideo(
  100 + getCurrentUser().isTenantAdmin(),
  101 + getCurrentUser().getCurrentUserId(),
  102 + getCurrentUser().getCurrentTenantId(),
  103 + entityId);
  104 + }
  105 +
95 @GetMapping("url/{entityId}/{protocolType}") 106 @GetMapping("url/{entityId}/{protocolType}")
96 @ApiOperation("获取平台视频流播放地址") 107 @ApiOperation("获取平台视频流播放地址")
97 public ResponseResult<Map<String, String>> getCameraPreviewURL( 108 public ResponseResult<Map<String, String>> getCameraPreviewURL(
@@ -9,6 +9,7 @@ import org.thingsboard.server.common.data.yunteng.utils.HttpClientUtils; @@ -9,6 +9,7 @@ import org.thingsboard.server.common.data.yunteng.utils.HttpClientUtils;
9 import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil; 9 import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
10 10
11 import java.io.IOException; 11 import java.io.IOException;
  12 +import java.text.SimpleDateFormat;
12 import java.util.Date; 13 import java.util.Date;
13 import java.util.HashMap; 14 import java.util.HashMap;
14 import java.util.Map; 15 import java.util.Map;
@@ -21,6 +22,11 @@ public class EzvizUtils { @@ -21,6 +22,11 @@ public class EzvizUtils {
21 private static Long expireTime; 22 private static Long expireTime;
22 23
23 public static String getCameraPreviewURL(String appKey, String appSecret, String sn) throws IOException { 24 public static String getCameraPreviewURL(String appKey, String appSecret, String sn) throws IOException {
  25 + //查询缓存中是否已有该设备的直播url
  26 + String videoUrl = VideoNumbersUtils.getUrl(appKey+sn);
  27 + if(!StringUtils.isEmpty(videoUrl)){
  28 + return videoUrl;
  29 + }
24 getAccessTokenByUrl(appKey,appSecret); 30 getAccessTokenByUrl(appKey,appSecret);
25 String cameraPreviewURL = url+"v2/live/address/get?accessToken="+accessToken+ 31 String cameraPreviewURL = url+"v2/live/address/get?accessToken="+accessToken+
26 "&deviceSerial="+sn+"&protocol=4&quality=1"; 32 "&deviceSerial="+sn+"&protocol=4&quality=1";
@@ -38,6 +44,8 @@ public class EzvizUtils { @@ -38,6 +44,8 @@ public class EzvizUtils {
38 } 44 }
39 throw new TkDataValidationException(message); 45 throw new TkDataValidationException(message);
40 } 46 }
  47 + //将获取到的rul放入缓存
  48 + VideoNumbersUtils.putVideoUrl(appKey+sn,json.get("url").textValue());
41 return json.get("url").textValue(); 49 return json.get("url").textValue();
42 }else{ 50 }else{
43 String errorCode = json.get("code").asText(); 51 String errorCode = json.get("code").asText();
@@ -85,4 +93,11 @@ public class EzvizUtils { @@ -85,4 +93,11 @@ public class EzvizUtils {
85 } 93 }
86 } 94 }
87 95
  96 + public static void main(String[] args) {
  97 + Date date = new Date(1703235281);
  98 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  99 +
  100 + System.out.println(sdf.format(date));
  101 + }
  102 +
88 } 103 }
  1 +package org.thingsboard.server.common.data.yunteng.utils.tools;
  2 +
  3 +import java.util.HashMap;
  4 +import java.util.Map;
  5 +
  6 +public class VideoNumbersUtils {
  7 +
  8 + private static Map<String, Map<String,Object>> number = new HashMap<>();
  9 +
  10 +
  11 + public static void putVideoUrl(String key,String url) {
  12 + Map<String,Object> newVideo = new HashMap<>();
  13 + newVideo.put("url",url);
  14 + newVideo.put("number",1);
  15 + number.put(key,newVideo);
  16 + }
  17 +
  18 + public static String getUrl(String key) {
  19 + //获取已有直播链接并加入人数
  20 + Map<String,Object> oldVideo = number.get(key);
  21 + if(oldVideo==null){
  22 + return null;
  23 + }
  24 + oldVideo.put("number",(int)oldVideo.get("number")+1);
  25 + return oldVideo.get("url").toString();
  26 + }
  27 +
  28 + public static void deleteVideoNumber(String key) {
  29 + //减少直播间人数
  30 + Map<String,Object> oldVideo = number.get(key);
  31 + int videoNumber = (int) oldVideo.get("number");
  32 + //只有一个人并且需要减少则直接删除
  33 + if(videoNumber==1){
  34 + number.remove(key);
  35 + return;
  36 + }
  37 + oldVideo.put("number",(int)oldVideo.get("number")-1);
  38 + }
  39 +
  40 +
  41 +}
@@ -15,10 +15,7 @@ import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidatio @@ -15,10 +15,7 @@ import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidatio
15 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; 15 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
16 import org.thingsboard.server.common.data.yunteng.dto.*; 16 import org.thingsboard.server.common.data.yunteng.dto.*;
17 import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil; 17 import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
18 -import org.thingsboard.server.common.data.yunteng.utils.tools.EzvizUtils;  
19 -import org.thingsboard.server.common.data.yunteng.utils.tools.HikVisionArtemisPostUtils;  
20 -import org.thingsboard.server.common.data.yunteng.utils.tools.ProtocolType;  
21 -import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData; 18 +import org.thingsboard.server.common.data.yunteng.utils.tools.*;
22 import org.thingsboard.server.dao.yunteng.entities.TkVideoEntity; 19 import org.thingsboard.server.dao.yunteng.entities.TkVideoEntity;
23 import org.thingsboard.server.dao.yunteng.mapper.OrganizationMapper; 20 import org.thingsboard.server.dao.yunteng.mapper.OrganizationMapper;
24 import org.thingsboard.server.dao.yunteng.mapper.TkVideoMapper; 21 import org.thingsboard.server.dao.yunteng.mapper.TkVideoMapper;
@@ -175,6 +172,21 @@ public class TkVideoServiceImpl extends AbstractBaseService<TkVideoMapper, TkVid @@ -175,6 +172,21 @@ public class TkVideoServiceImpl extends AbstractBaseService<TkVideoMapper, TkVid
175 return null; 172 return null;
176 } 173 }
177 174
  175 + @Override
  176 + public void outVideo(boolean isTenantAdmin, String customerUserId, String tenantId, String entityId) {
  177 + if (isTenantAdmin) {
  178 + customerUserId = null;
  179 + }
  180 + //通过流媒体方式获取视频流的列表
  181 + List<TkVideoDTO> videoDTOList = baseMapper.getVideoInfosByTenantIdOrAccessModeOrId(tenantId, entityId,
  182 + FastIotConstants.MagicNumber.ONE, customerUserId);
  183 + if (videoDTOList.size() == 0) {
  184 + throw new TkDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  185 + }
  186 + TkVideoDTO videoDTO = videoDTOList.get(0);
  187 + VideoNumbersUtils.deleteVideoNumber(videoDTO.getVideoPlatformDTO().getAppKey()+videoDTO.getSn());
  188 + }
  189 +
178 private String HikVisionArtemis(TkVideoDTO videoDTO, ProtocolType protocolType){ 190 private String HikVisionArtemis(TkVideoDTO videoDTO, ProtocolType protocolType){
179 if(protocolType!=null && protocolType.equals(ProtocolType.HLS)){ 191 if(protocolType!=null && protocolType.equals(ProtocolType.HLS)){
180 protocolType = videoDTO.getPlayProtocol() == 0 ? ProtocolType.HLS : ProtocolType.HLSS; 192 protocolType = videoDTO.getPlayProtocol() == 0 ? ProtocolType.HLS : ProtocolType.HLSS;
@@ -104,4 +104,9 @@ public interface TkVideoService extends BaseService<TkVideoEntity> { @@ -104,4 +104,9 @@ public interface TkVideoService extends BaseService<TkVideoEntity> {
104 * @return 视频列表 104 * @return 视频列表
105 */ 105 */
106 List<TkVideoDTO> getVideoList(String organizationId, String tenantId); 106 List<TkVideoDTO> getVideoList(String organizationId, String tenantId);
  107 +
  108 + void outVideo( boolean isTenantAdmin,
  109 + String customerUserId,
  110 + String tenantId,
  111 + String entityId);
107 } 112 }