Browse Source

流媒体修改

S0025136190 1 year ago
parent
commit
8fda78f3bd

+ 18 - 20
Backend/sample/src/main/java/com/dji/sample/common/smsp/CallSmsp.java

@@ -3,6 +3,7 @@ package com.dji.sample.common.smsp;
 import cn.hutool.crypto.digest.MD5;
 import cn.hutool.crypto.digest.MD5;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONObject;
+import com.dji.sample.configuration.CustomConfiguration;
 import com.dji.sample.manage.model.dto.ManageDeviceLivestreamUrlDTO;
 import com.dji.sample.manage.model.dto.ManageDeviceLivestreamUrlDTO;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -15,9 +16,8 @@ import org.apache.http.util.EntityUtils;
 
 
 import java.time.Instant;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.List;
 
 
-import static com.dji.sample.configuration.CustomConfiguration.smspRtmpUrl;
-import static com.dji.sample.configuration.CustomConfiguration.smspTokenUrl;
 
 
 @Slf4j
 @Slf4j
 public class CallSmsp {
 public class CallSmsp {
@@ -25,7 +25,7 @@ public class CallSmsp {
 //        String pass = MD5.create().digestHex(password);
 //        String pass = MD5.create().digestHex(password);
 //        Instant now = Instant.now();
 //        Instant now = Instant.now();
 //        long currentTimeMillis = now.toEpochMilli();
 //        long currentTimeMillis = now.toEpochMilli();
-        String url = smspTokenUrl+"?username="+username+"&password="+password;
+        String url = CustomConfiguration.smspTokenUrl+"?username="+username+"&password="+password;
         try {
         try {
             String response = sendGetRequest(url);
             String response = sendGetRequest(url);
             log.debug("Response from API: " + response);
             log.debug("Response from API: " + response);
@@ -38,12 +38,12 @@ public class CallSmsp {
         }
         }
     }
     }
 
 
-    public static ManageDeviceLivestreamUrlDTO getRtmp(String token, String deviceSn,String payload) {
-        ManageDeviceLivestreamUrlDTO resultDto = new ManageDeviceLivestreamUrlDTO();
+    public static List<ManageDeviceLivestreamUrlDTO> getRtmp(String token, String deviceSn, String payload) {
+        List<ManageDeviceLivestreamUrlDTO> resultList = new ArrayList<>();
         if(token == null) {
         if(token == null) {
-            return resultDto;
+            return resultList;
         }
         }
-        String url = smspRtmpUrl+"?token="+token;
+        String url = CustomConfiguration.smspRtmpUrl+"?token="+token;
         ObjectMapper objectMapper = new ObjectMapper();
         ObjectMapper objectMapper = new ObjectMapper();
         try {
         try {
             String response = sendGetRequest(url);
             String response = sendGetRequest(url);
@@ -58,25 +58,23 @@ public class CallSmsp {
                     JsonNode childrenNode = deviceNode.get("Children");
                     JsonNode childrenNode = deviceNode.get("Children");
                     // 遍历子节点
                     // 遍历子节点
                     for (JsonNode childNode : childrenNode) {
                     for (JsonNode childNode : childrenNode) {
-                        String channelId = childNode.get("ChannelID").asText();
-                        // 查找特定子设备名
-                        if (payload.equals(channelId)) {
-                            // 获取RTMP和WEBRTC数据
-                            String rtmpUrl = childNode.get("RTMP").asText();
-                            String webRtcUrl = childNode.get("WEBRTC").asText();
-                            Integer sid = childNode.get("Sid").asInt();
-                            resultDto.setChannelId(sid);
-                            resultDto.setRtmpUrl(rtmpUrl);
-                            resultDto.setWebRtcUrl(webRtcUrl);
+                        ManageDeviceLivestreamUrlDTO livestreamUrlDTO = new ManageDeviceLivestreamUrlDTO();
+                        // 获取RTMP和WEBRTC数据
+                        String rtmpUrl = childNode.get("RTMP").asText();
+                        String webRtcUrl = childNode.get("WEBRTC").asText();
+                        Integer channelID = childNode.get("ChannelID").asInt();
+                        livestreamUrlDTO.setChannelId(channelID);
+                        livestreamUrlDTO.setRtmpUrl(rtmpUrl);
+                        livestreamUrlDTO.setWebRtcUrl(webRtcUrl);
 
 
-                        }
+                        resultList.add(livestreamUrlDTO);
                     }
                     }
                 }
                 }
             }
             }
-            return resultDto;
+            return resultList;
         } catch (Exception e) {
         } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
-            return resultDto;
+            return resultList;
         }
         }
     }
     }
 
 

+ 3 - 2
Backend/sample/src/main/java/com/dji/sample/manage/service/impl/LiveStreamServiceImpl.java

@@ -77,12 +77,13 @@ public class LiveStreamServiceImpl implements ILiveStreamService {
         }
         }
         ManageDeviceLivestreamUrlDTO livestreamUrlDTO = new ManageDeviceLivestreamUrlDTO();
         ManageDeviceLivestreamUrlDTO livestreamUrlDTO = new ManageDeviceLivestreamUrlDTO();
         livestreamUrlDTO.setWorkspaceId(responseResult.getData().getWorkspaceId());
         livestreamUrlDTO.setWorkspaceId(responseResult.getData().getWorkspaceId());
-        livestreamUrlDTO.setPayloadIndex(Integer.valueOf(liveParam.getVideoId().getPayloadIndex().toString()));
+        livestreamUrlDTO.setPayload(liveParam.getVideoId().getPayloadIndex().toString());
         livestreamUrlDTO.setDeviceSn(responseResult.getData().getDeviceSn());
         livestreamUrlDTO.setDeviceSn(responseResult.getData().getDeviceSn());
         livestreamUrlDTO = deviceLivestreamUrlService.getUrl(livestreamUrlDTO);
         livestreamUrlDTO = deviceLivestreamUrlService.getUrl(livestreamUrlDTO);
 
 
         ILivestreamUrl url = LiveStreamProperty.get(liveParam.getUrlType());
         ILivestreamUrl url = LiveStreamProperty.get(liveParam.getUrlType());
-        LivestreamRtmpUrl rtmpUrl = (LivestreamRtmpUrl) url.clone();
+
+        LivestreamRtmpUrl rtmpUrl = new LivestreamRtmpUrl();
         rtmpUrl.setUrl(livestreamUrlDTO.getRtmpUrl());
         rtmpUrl.setUrl(livestreamUrlDTO.getRtmpUrl());
         //url = setExt(liveParam.getUrlType(), url, liveParam.getVideoId());
         //url = setExt(liveParam.getUrlType(), url, liveParam.getVideoId());
 
 

+ 76 - 34
Backend/sample/src/main/java/com/dji/sample/manage/service/impl/ManageDeviceLivestreamUrlServiceImpl.java

@@ -11,7 +11,9 @@ import com.dji.sample.manage.model.entity.DevicePayloadEntity;
 import com.dji.sample.manage.model.entity.ManageDeviceLivestreamUrlEntity;
 import com.dji.sample.manage.model.entity.ManageDeviceLivestreamUrlEntity;
 import com.dji.sample.manage.model.entity.UserEntity;
 import com.dji.sample.manage.model.entity.UserEntity;
 import com.dji.sample.manage.model.enums.LiveUrlTypeEnum;
 import com.dji.sample.manage.model.enums.LiveUrlTypeEnum;
+import com.dji.sample.manage.model.enums.UserTypeEnum;
 import com.dji.sample.manage.service.IManageDeviceLivestreamUrlService;
 import com.dji.sample.manage.service.IManageDeviceLivestreamUrlService;
+import com.dji.sdk.cloudapi.device.DeviceSubTypeEnum;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -19,6 +21,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 
 
 import java.util.List;
 import java.util.List;
+import java.util.stream.Collectors;
 
 
 
 
 @Service
 @Service
@@ -42,10 +45,10 @@ public class ManageDeviceLivestreamUrlServiceImpl implements IManageDeviceLivest
 
 
 
 
         QueryWrapper<UserEntity> userWrapper = new QueryWrapper<>();
         QueryWrapper<UserEntity> userWrapper = new QueryWrapper<>();
-        userWrapper.lambda().eq(UserEntity::getUserType,3).eq(UserEntity::getClientId,clientId);
+        userWrapper.lambda().eq(UserEntity::getUserType, UserTypeEnum.API.getVal()).eq(UserEntity::getClientId,clientId);
         UserEntity userEntity = userMapper.selectOne(userWrapper);
         UserEntity userEntity = userMapper.selectOne(userWrapper);
         if (userEntity == null) {
         if (userEntity == null) {
-            log.debug("The user is already null.");
+            log.debug("The user is null.");
             return null;
             return null;
         }
         }
         //密码解密
         //密码解密
@@ -53,11 +56,13 @@ public class ManageDeviceLivestreamUrlServiceImpl implements IManageDeviceLivest
         //获取token
         //获取token
         String token = CallSmsp.getSmspToken(userEntity.getUsername(),password);
         String token = CallSmsp.getSmspToken(userEntity.getUsername(),password);
         if(token == null) {
         if(token == null) {
-            log.debug("The smsp token is already null.");
+            log.debug("The smsp token is null.");
         }
         }
 
 
         //获取rtmp地址
         //获取rtmp地址
-        ManageDeviceLivestreamUrlDTO rtmpDto = CallSmsp.getRtmp(token,deviceLivestreamUrlDTO.getDeviceSn(),deviceLivestreamUrlDTO.getPayload());
+        List<ManageDeviceLivestreamUrlDTO> rtmpList = CallSmsp.getRtmp(token,deviceLivestreamUrlDTO.getDeviceSn(),deviceLivestreamUrlDTO.getPayload());
+        ManageDeviceLivestreamUrlDTO rtmpDto = new ManageDeviceLivestreamUrlDTO();
+        //获取payload信息
         QueryWrapper<DevicePayloadEntity> payloadWrapper = new QueryWrapper<>();
         QueryWrapper<DevicePayloadEntity> payloadWrapper = new QueryWrapper<>();
         payloadWrapper.lambda().eq(DevicePayloadEntity::getPayloadIndex,deviceLivestreamUrlDTO.getPayload())
         payloadWrapper.lambda().eq(DevicePayloadEntity::getPayloadIndex,deviceLivestreamUrlDTO.getPayload())
                 .eq(DevicePayloadEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn());
                 .eq(DevicePayloadEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn());
@@ -65,45 +70,82 @@ public class ManageDeviceLivestreamUrlServiceImpl implements IManageDeviceLivest
 
 
         deviceLivestreamUrlDTO.setPayload(payloadEntity.getPayloadName());
         deviceLivestreamUrlDTO.setPayload(payloadEntity.getPayloadName());
         deviceLivestreamUrlDTO.setPayloadIndex(payloadEntity.getPayloadIndex());
         deviceLivestreamUrlDTO.setPayloadIndex(payloadEntity.getPayloadIndex());
-        deviceLivestreamUrlDTO.setChannelId(rtmpDto.getChannelId());
 
 
-        ManageDeviceLivestreamUrlEntity urlEntity = new ManageDeviceLivestreamUrlEntity();
+
+        //获取数据库中deviceSn对应的url信息
         QueryWrapper<ManageDeviceLivestreamUrlEntity> wrapper = new QueryWrapper<>();
         QueryWrapper<ManageDeviceLivestreamUrlEntity> wrapper = new QueryWrapper<>();
         wrapper.lambda().eq(ManageDeviceLivestreamUrlEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn())
         wrapper.lambda().eq(ManageDeviceLivestreamUrlEntity::getDeviceSn,deviceLivestreamUrlDTO.getDeviceSn())
-                .eq(ManageDeviceLivestreamUrlEntity::getWorkspaceId,deviceLivestreamUrlDTO.getWorkspaceId())
-                .eq(ManageDeviceLivestreamUrlEntity::getPayloadIndex,deviceLivestreamUrlDTO.getPayloadIndex());
+                .eq(ManageDeviceLivestreamUrlEntity::getWorkspaceId,deviceLivestreamUrlDTO.getWorkspaceId());
         List<ManageDeviceLivestreamUrlEntity> urlList = mapper.selectList(wrapper);
         List<ManageDeviceLivestreamUrlEntity> urlList = mapper.selectList(wrapper);
-        if (urlList.size() > 0) {
-            for(int i = 0; i< urlList.size();i++) {
-                urlEntity = urlList.get(i);
-                if (urlEntity.getUrlType().equals(LiveUrlTypeEnum.RTMP.getVal())) {
-                    if(!urlEntity.getUrl().equals(rtmpDto.getRtmpUrl())) {
-                        urlEntity.setUrl(rtmpDto.getRtmpUrl());
-                        mapper.updateById(urlEntity);
+        if(urlList.size() < 0 && rtmpList.size() < 0 ){
+            log.debug("The result is null.");
+            return null;
+        }
+        //过滤payloadIndex
+        List<ManageDeviceLivestreamUrlEntity> filterIndexList = urlList.stream().filter(channelData -> payloadEntity.getPayloadIndex().equals(channelData.getPayloadIndex()))
+                .collect(Collectors.toList());
+        if (filterIndexList.size() > 0) {
+            //过滤channelId
+            List<ManageDeviceLivestreamUrlDTO> channelList = rtmpList.stream().filter(channelData -> filterIndexList.get(0).getChannelId().equals(channelData.getChannelId()))
+                    .collect(Collectors.toList());
+            if (channelList.size() > 0) {
+                //判断url是否一致
+                for(ManageDeviceLivestreamUrlEntity url : filterIndexList) {
+                    if (url.getUrlType().equals(LiveUrlTypeEnum.RTMP.getVal())) {
+                        if(!url.getUrl().equals(channelList.get(0).getRtmpUrl())) {
+                            url.setUrl(channelList.get(0).getRtmpUrl());
+                            mapper.updateById(url);}
+                    } else if(url.getUrlType().equals(LiveUrlTypeEnum.WEBRTC.getVal())) {
+                        if(!url.getUrl().equals(channelList.get(0).getWebRtcUrl())) {
+                            url.setUrl(channelList.get(0).getWebRtcUrl());
+                            mapper.updateById(url);
+                        }
                     }
                     }
-                } else if(urlEntity.getUrlType().equals(LiveUrlTypeEnum.WEBRTC.getVal())) {
-                    if(!urlEntity.getUrl().equals(rtmpDto.getWebRtcUrl())) {
-                        urlEntity.setUrl(rtmpDto.getWebRtcUrl());
-                        mapper.updateById(urlEntity);
+                }
+                rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
+                rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
+            } else {
+                //过滤channelId
+                channelList = rtmpList.stream().filter(channelData -> !urlList.contains(channelData.getChannelId()))
+                        .collect(Collectors.toList());
+                for(ManageDeviceLivestreamUrlEntity url : filterIndexList) {
+                    url.setChannelId(channelList.get(0).getChannelId());
+
+                    if (url.getUrlType().equals(LiveUrlTypeEnum.RTMP.getVal())) {
+                        url.setUrl(channelList.get(0).getRtmpUrl());
+                        mapper.updateById(url);
+                    } else if(url.getUrlType().equals(LiveUrlTypeEnum.WEBRTC.getVal())) {
+                        url.setUrl(channelList.get(0).getWebRtcUrl());
+                        mapper.updateById(url);
                     }
                     }
                 }
                 }
+                rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
+                rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
             }
             }
-            return rtmpDto;
         } else {
         } else {
-            //dto转entity
-            BeanUtils.copyProperties(deviceLivestreamUrlDTO,urlEntity);
-            //存库Rtmp
-            urlEntity.setSubType(1);
-            urlEntity.setUrlType(LiveUrlTypeEnum.RTMP.getVal());
-            urlEntity.setUrl(rtmpDto.getRtmpUrl());
-            mapper.insert(urlEntity);
-            //存库WEBRTC
-            urlEntity.setId(null);
-            urlEntity.setSubType(2);
-            urlEntity.setUrlType(LiveUrlTypeEnum.WEBRTC.getVal());
-            urlEntity.setUrl(rtmpDto.getWebRtcUrl());
-            mapper.insert(urlEntity);
-            return rtmpDto;
+            //过滤channelId
+            List<ManageDeviceLivestreamUrlDTO> channelList = rtmpList.stream().filter(channelData -> !urlList.contains(channelData.getChannelId()))
+                    .collect(Collectors.toList());
+            ManageDeviceLivestreamUrlEntity urlEntity = new ManageDeviceLivestreamUrlEntity();
+            if(channelList.size() > 0) {
+                //dto转entity
+                BeanUtils.copyProperties(deviceLivestreamUrlDTO,urlEntity);
+                //存库Rtmp
+                urlEntity.setSubType(DeviceSubTypeEnum.ONE.getSubType());
+                urlEntity.setUrlType(LiveUrlTypeEnum.RTMP.getVal());
+                urlEntity.setChannelId(channelList.get(0).getChannelId());
+                urlEntity.setUrl(channelList.get(0).getRtmpUrl());
+                mapper.insert(urlEntity);
+                //存库WEBRTC
+                urlEntity.setId(null);
+                urlEntity.setSubType(DeviceSubTypeEnum.TWO.getSubType());
+                urlEntity.setUrlType(LiveUrlTypeEnum.WEBRTC.getVal());
+                urlEntity.setUrl(channelList.get(0).getWebRtcUrl());
+                mapper.insert(urlEntity);
+                rtmpDto.setRtmpUrl(channelList.get(0).getRtmpUrl());
+                rtmpDto.setWebRtcUrl(channelList.get(0).getWebRtcUrl());
+            }
         }
         }
+        return rtmpDto;
     }
     }
 }
 }