All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahuatech.icc.multiinone.admin.RealTimeSDK Maven / Gradle / Ivy

The newest version!
package com.dahuatech.icc.multiinone.admin;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.admin.enums.PlayBackFormatEnum;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.accesscontrol.AccessBaseAuthSDK;
import com.dahuatech.icc.multiinone.admin.constant.AdminConstant;
import com.dahuatech.icc.multiinone.admin.vo.RealTimeRequest;
import com.dahuatech.icc.multiinone.admin.vo.RealTimeResponse;
import com.dahuatech.icc.multiinone.exception.BusinessException;
import com.dahuatech.icc.multiinone.utils.StringUtils;
import com.dahuatech.icc.oauth.http.IccClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;

import java.util.ArrayList;
import java.util.List;

public class RealTimeSDK extends AccessBaseAuthSDK {

    /**  ********************* 手动拼接地址废弃 ***********************
    private final String httpHlsTemplate = "http://%s:7086/live/cameraid/%s$%s/substream/%s.m3u8";
    private final String httpsHlsTemplate = "https://%s:7096/live/cameraid/%s$%s/substream/%s.m3u8";
    private final String httpFlvTemplate = "http://%s:7886/live/cameraid/%s$%s/substream/%s.flv";
    private final String httpsFLVTemplate = "https://%s:7896/live/cameraid/%s$%s/substream/%s.flv";
    private final String rtmpTemplate = "rtmp://%s:1975/live/cameraid=%s$%s;substream=%s";
    private final String rtspTemplate = "rtsp://%s:9090/dss/monitor/param?cameraid=%s$%s&substream=%s";
     **/

    /**************************新版本SDK:获取视频实时播放地址,根据参数类型区分RTSP和HLS/FLV/RTMP**************************/
    public RealTimeResponse getRealTimeUrl(RealTimeRequest realTimeRequest){
        logger.info("RealTimeSDK,getRealTimeUrl,param:{}.",new JSONObject(realTimeRequest).toJSONString(0));
        RealTimeResponse realTimeResponse = new RealTimeResponse();

        try{
            realTimeRequest.valid();
            if(PlayBackFormatEnum.RTSP.getCode().equals(realTimeRequest.getType())){
                realTimeResponse.setUrlList(getRTSPRealTimeUrl(realTimeRequest));
            }else{
                realTimeResponse.setUrlList(getHLSFLVRTMPRealTimeUrl(realTimeRequest));
            }
            realTimeResponse.setCode(Constants.SUCESS_CODE);
            realTimeResponse.setErrMsg(Constants.SUCCESS_MSG);
        }catch (BusinessException businessException){
            logger.error("Acquire real time stream's URL fail,errorMsg:{}.",businessException,businessException.getMessage());
            realTimeResponse.setCode(businessException.code);
            realTimeResponse.setErrMsg(businessException.msg);
            return realTimeResponse;
        }catch (Exception e){
            logger.error("Acquire real time stream's URL fail,errorMsg:{}.",e,e.getMessage());
            realTimeResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            realTimeResponse.setCode(Constants.SYSTEMERROR_CODE);
            return realTimeResponse;
        }
        return realTimeResponse;
    }

    private List getHLSFLVRTMPRealTimeUrl(RealTimeRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = AdminConstant.HLS_FLV_RTMP_URL;
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject data = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("channelId",request.getChannelId());
            jsonObject.put("streamType",request.getStreamType());
            jsonObject.put("type",request.getType());
            data.put("data",jsonObject);
            generalRequest.setBody(data.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("Acquire HLS/FLV/RTMP stream'URL fail,errorMsg:{}.",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!"1000".equals(generalResponse.getCode())){
            logger.error("Acquire HLS/FLV/RTMP stream'URL fail,code:{},errorMsg:{}.",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        //对结果进行处理
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        if(jsonObject.getJSONObject("data") == null){
            throw new BusinessException("Acquired HLS/FLV/RTMP stream's URL is empty.");
        }
        String rtspUrls = jsonObject.getJSONObject("data").getStr("url");
        String token = jsonObject.getJSONObject("data").getStr("token");
        String[] rtspUrlArr = rtspUrls.split("\\|");
        List list = new ArrayList();
        if(rtspUrlArr == null || rtspUrlArr.length == 0){
            throw new BusinessException("Acquired HLS/FLV/RTMP stream's URL is empty.");
        }
        for(String item : rtspUrlArr){
            if(!StringUtils.isEmpty(token)){
                list.add(item + "?token=" + token);
            } else{
                list.add(item);
            }
        }

        /**********************************统一用接口返回的HLS/FLV/RTMP视频地址,手动拼接的地址废弃************************************/
        /**
         String[] arr = request.getChannelId().split("\\$");
         if(PlayBackFormatEnum.HLS.getCode().equals(request.getType())){
         list.add(String.format(httpHlsTemplate,request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost(),arr[0],arr[3],request.getStreamType()).replaceAll("\\$","%24"));
         list.add(String.format(httpsHlsTemplate,request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost(),arr[0],arr[3],request.getStreamType()).replaceAll("\\$","%24"));
         }else if(PlayBackFormatEnum.FLV.code.equals(request.getType())) {
         list.add(String.format(httpFlvTemplate, request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost(), arr[0], arr[3], request.getStreamType()).replaceAll("\\$","%24"));
         list.add(String.format(httpsFLVTemplate, request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost(), arr[0], arr[3], request.getStreamType()).replaceAll("\\$","%24"));
         }else {
         list.add(String.format(rtmpTemplate,request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost(),arr[0],arr[3],request.getStreamType()).replaceAll("\\$","%24"));
         }**/
        return list;
    }

    /**
     * 获取RTSP实时流
     * **/
    private List getRTSPRealTimeUrl(RealTimeRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = AdminConstant.RTSP_REQUEST_URL;
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject data = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("channelId",request.getChannelId());
            jsonObject.put("streamType",request.getStreamType());
            jsonObject.put("dataType","1");
            data.put("data",jsonObject);
            generalRequest.setBody(data.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("Acquire RTSP stream'URL fail,errMsg:{}",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!"1000".equals(generalResponse.getCode())){
            logger.error("Acquire RTSP stream'URL fail,code:{},errorMsg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        //对结果进行处理
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        if(jsonObject.getJSONObject("data") == null){
            throw new BusinessException("Acquired RTSP stream address is Empty.");
        }
        String rtspUrls = jsonObject.getJSONObject("data").getStr("url");
        String token = jsonObject.getJSONObject("data").getStr("token");
        String[] rtspUrlArr = rtspUrls.split("\\|");
        List list = new ArrayList();
        if(rtspUrlArr == null || rtspUrlArr.length == 0){
            throw new BusinessException("Acquired RTSP stream address is Empty.");
        }
        for(String item : rtspUrlArr){
            list.add(item + "?token=" + token);
        }
        /**********************************统一用接口返回的RTSP视频地址,手动拼接的地址废弃************************************/
        /**  String[] arr = request.getChannelId().split("\\$");
             list.add(String.format(rtspTemplate,request.getOauthConfigBaseInfo().getHttpConfigInfo().getHost(),arr[0],arr[3],request.getStreamType()).replaceAll("\\$","%24"));
         **/
        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy