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

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

There is a newer version: 1.0.13.7
Show newest version
package com.dahuatech.icc.multiinone.admin;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONArray;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.admin.enums.PlayBackFormatEnum;
import com.dahuatech.icc.admin.enums.RecordSourceEnum;
import com.dahuatech.icc.admin.enums.RecordTypeEnum;
import com.dahuatech.icc.admin.enums.SSRecordTypeEnum;
import com.dahuatech.icc.common.ParamValidEnum;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.admin.constant.AdminConstant;
import com.dahuatech.icc.multiinone.admin.domain.RecordInfo;
import com.dahuatech.icc.multiinone.admin.vo.AlarmPlayBackRequest;
import com.dahuatech.icc.multiinone.admin.vo.CommonPlayBackRequest;
import com.dahuatech.icc.multiinone.admin.vo.PlayBackResponse;
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 com.dahuatech.icc.oauth.model.v202010.OauthConfigBaseInfo;
import com.dahuatech.icc.util.CollectionUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PlayBackSDK {

    private final Log logger = LogFactory.get();

    private final String httpCenterHlsTemplate = "http://%s:7086/vod/center/cameraid/%s$%s/substream/%s/recordtype/%s/totallength/%s/begintime/%s/endtime/%s.m3u8";
    private final String httpsCenterHlsTemplate = "https://%s:7096/vod/center/cameraid/%s$%s/substream/%s/recordtype/%s/totallength/%s/begintime/%s/endtime/%s.m3u8";
    private final String httpDeviceHlsTemplate = "http://%s:7086/vod/device/cameraid/%s$%s/substream/%s/recordtype/%s/totallength/%s/begintime/%s/endtime/%s.m3u8";
    private final String httpsDeviceHlsTemplate = "https://%s:7096/vod/device/cameraid/%s$%s/substream/%s/recordtype/%s/totallength/%s/begintime/%s/endtime/%s.m3u8";

    private final String rtspTemplate = "rtsp://%s:9090/dss/playback/param?cameraid=%s$%s&substream=%s&type=%s&recordType=%s&begintime=%s&endtime=%s";

    /**
     * 获取普通录像URL
     * @param request
     * @return
     */
    public PlayBackResponse getCommonPlayBackUrl(CommonPlayBackRequest request){
        logger.info("PlayBackSDK,getCommonPlayBackUrl,param:{}",new JSONObject(request).toJSONString(0));
        PlayBackResponse playBackResponse = new PlayBackResponse();
        try{
            request.valid();
            //1.查询普通录像信息
            List list = queryCommonPlayBackInfo(request);
            if(CollectionUtil.isEmpty(list)){
                logger.info("No general recording information found.");
                playBackResponse.setCode(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode());
                playBackResponse.setErrMsg("No general recording information found.");
                playBackResponse.setSuccess(true);
                return playBackResponse;
            }
            //HLS类型处理
            if(PlayBackFormatEnum.HLS.getCode().equals(request.getType())){
                //playBackResponse.setUrlList(getHlsUrlList(list,request.getOauthConfigBaseInfo()));
                playBackResponse.setUrlList(getCommonHlsUrlList(list, request));
            }else{
                //flv/rtmp/rtsp处理,从查询结果中找到HLS对应的url,设置到结果体中
                List rtspUrlList = getCommonRTSPUrlList(list,request);
                playBackResponse.setUrlList(rtspUrlList);
            }
            playBackResponse.setCode(Constants.SUCESS_CODE);
            playBackResponse.setErrMsg(Constants.SUCCESS_MSG);
            return playBackResponse;
        }catch (BusinessException businessException){
            logger.error("Failed to get playback stream,URL:{}.",businessException,businessException.getMessage());
            playBackResponse.setCode(businessException.getCode());
            playBackResponse.setErrMsg(businessException.getMsg());
        }catch (Exception e){
            logger.error("An exception occurred while getting the playback stream,errorMsg:{}",e,e.getMessage());
            playBackResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            playBackResponse.setCode(Constants.SYSTEMERROR_CODE);
        }
        return playBackResponse;
    }

    /**
     * 获取报警录像URL
     * @param request
     * @return
     */
    public PlayBackResponse getAlarmPlayBackUrl(AlarmPlayBackRequest request){
        logger.info("PlayBackSDK,getAlarmPlayBackUrl,param:{}",new JSONObject(request).toJSONString(0));
        PlayBackResponse playBackResponse = new PlayBackResponse();

        //HLS不支持报警录像回放
        if(PlayBackFormatEnum.HLS.getCode().equals(request.getType())){
            logger.info("HLS does not support alarm video playback.");
            playBackResponse.setCode(ParamValidEnum.PARAM_FORMAT_ERROR.getCode());
            playBackResponse.setErrMsg("HLS does not support alarm video playback.");
            return playBackResponse;
        }
        try{
            request.valid();
            List list = queryAlarmPlayBackInfo(request);
            if(CollectionUtil.isEmpty(list)){
                logger.info("No alarm recording information found.");
                playBackResponse.setCode(ParamValidEnum.PARAM_NOT_EMPTY_ERROR.getCode());
                playBackResponse.setErrMsg("No alarm recording information found.");
                return playBackResponse;
            }
            //RTSP报警录像回放接口和普通录像回放接口一致
            List rtspUrlList = getAlarmRTSPUrlList(list,request);
            playBackResponse.setUrlList(rtspUrlList);
            playBackResponse.setCode(Constants.SUCESS_CODE);
            playBackResponse.setErrMsg(Constants.SUCCESS_MSG);
            return playBackResponse;
        }catch (BusinessException businessException){
            logger.error("Failed to get playback stream,errorMsg:{}.",businessException,businessException.getMessage());
            playBackResponse.setCode(businessException.getCode());
            playBackResponse.setErrMsg(businessException.getMessage());
        }catch (Exception e){
            logger.error("Get playback stream URL exception,errorMsg:{}.",e,e.getMessage());
            playBackResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            playBackResponse.setCode(Constants.SYSTEMERROR_CODE);
        }
        return playBackResponse;
    }

    private List queryCommonPlayBackInfo(CommonPlayBackRequest request) throws ClientException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = AdminConstant.PLAY_INFO_GET;
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            JSONObject data = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("channelId",request.getChannelId());
            jsonObject.put("startTime", dateFormat.parse(request.getStartTime()).getTime()/1000);
            jsonObject.put("endTime",dateFormat.parse(request.getEndTime()).getTime()/1000);
            jsonObject.put("recordSource",request.getRecordSource());
            jsonObject.put("streamType","0");
            jsonObject.put("recordType","0");
            data.put("data",jsonObject);
            generalRequest.setBody(data.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("An exception occurs when obtaining common play back information,errorMsg:{}.",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(generalResponse.getCode() != null && !generalResponse.getCode().equals("1000")){
            logger.error("An exception occurs when obtaining common play back information,code:{},errorMsg:{}.",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        if(jsonObject.getJSONObject("data") == null){
            return new ArrayList();
        }
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("records");
        if(jsonArray == null){
            return new ArrayList();
        }
        List list = jsonArray.toList(RecordInfo.class);
        return list;
    }

    private List queryAlarmPlayBackInfo(AlarmPlayBackRequest request) throws ClientException,BusinessException {
        IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
        String url = AdminConstant.AlARM_RECORD_GET;
        GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject data = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("alarmCode",request.getAlarmCode());
            data.put("data",jsonObject);
            generalRequest.setBody(data.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("An exception occurs when obtaining alarm play back information,errorMsg:{}.",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(generalResponse.getCode() != null && !generalResponse.getCode().equals("1000")){
            logger.error("An exception occurs when obtaining alarm play back information,code:{},errorMsg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        if(jsonObject.getJSONObject("data") == null){
            return new ArrayList();
        }
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("records");
        if(jsonArray == null){
            return new ArrayList();
        }
        List list = jsonArray.toList(RecordInfo.class);
        return list;
    }

    private List startPlaybackByTime(RecordInfo recordInfo, OauthConfigBaseInfo oauthConfigBaseInfo) throws ClientException {
        IccClient iccClient = new IccClient(oauthConfigBaseInfo);
        String url = AdminConstant.PLAY_BACK_BY_TIME;
        GeneralRequest generalRequest = new GeneralRequest(oauthConfigBaseInfo.getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject data = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("channelId",recordInfo.getChannelId());
            jsonObject.put("startTime", recordInfo.getStartTime());
            jsonObject.put("endTime",recordInfo.getEndTime());
            jsonObject.put("recordSource",recordInfo.getRecordSource());
            jsonObject.put("streamType",recordInfo.getStreamType());
            jsonObject.put("recordType","0");
            data.put("data",jsonObject);
            generalRequest.setBody(data.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("按时间回放获取流地址接口异常{}",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!"1000".equals(generalResponse.getCode())){
            logger.error("按时间回放获取流地址接口失败code:{}-msg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        JSONObject jsonObject = JSONUtil.parseObj(generalResponse.getResult());
        if(jsonObject.getJSONObject("data") == null){
            return new ArrayList();
        }
        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){
            return new ArrayList();
        }
        for(String item : rtspUrlArr){
            list.add(item + "?token=" + token);
        }
        return list;
    }

    private List startPlaybackByFile(RecordInfo recordInfo, OauthConfigBaseInfo oauthConfigBaseInfo) throws ClientException {
        IccClient iccClient = new IccClient(oauthConfigBaseInfo);
        String url = AdminConstant.PLAY_BACK_BY_FILE;
        GeneralRequest generalRequest = new GeneralRequest(oauthConfigBaseInfo.getHttpConfigInfo(),url, Method.POST);
        GeneralResponse generalResponse = null;
        try{
            JSONObject data = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("channelId",recordInfo.getChannelId());
            jsonObject.put("startTime", recordInfo.getStartTime());
            jsonObject.put("endTime",recordInfo.getEndTime());
            jsonObject.put("recordSource",recordInfo.getRecordSource());
            jsonObject.put("streamType",recordInfo.getStreamType());
            jsonObject.put("fileName",recordInfo.getRecordName());
            jsonObject.put("recordType",recordInfo.getRecordType());
            jsonObject.put("ssId",recordInfo.getSsId());
            jsonObject.put("diskId",recordInfo.getDiskId());
            jsonObject.put("streamId",recordInfo.getStreamId());
            data.put("data",jsonObject);
            generalRequest.setBody(data.toString());
            generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
        }catch (Exception e){
            logger.error("按文件回放获取流地址接口异常{}",e,e.getMessage());
            throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
        }
        if(!"1000".equals(generalResponse.getCode())){
            logger.error("按文件回放获取流地址接口失败code:{}-msg:{}",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("获取RTSP流地址为空");
        }
        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("获取RTSP流地址为空");
        }
        for(String item : rtspUrlArr){
            list.add(item + "?token=" + token);
        }
        return list;
    }

    private List getHlsUrlList(List list,OauthConfigBaseInfo oauthConfigBaseInfo){
        List rtspUrlList = new ArrayList();
        String host = oauthConfigBaseInfo.getHttpConfigInfo().getHost();
        String [] arr = null;
        String recordType = null;
        Long totalLength = 0l;
        for(RecordInfo recordInfo : list){
            arr = recordInfo.getChannelId().split("\\$");
            totalLength = recordInfo.getEndTime() - recordInfo.getStartTime();

            //录像来源为:中心
            if(RecordSourceEnum.SERVICE.getCode().equals(recordInfo.getRecordSource())){
                if(SSRecordTypeEnum.ALARM.getCode().equals(recordInfo.getRecordType())){
                    recordType = RecordTypeEnum.OTHER.getCode();
                }else if(SSRecordTypeEnum.TIME.getCode().equals(recordInfo.getRecordType())){
                    recordType = RecordTypeEnum.COMMON.getCode();
                }else if(SSRecordTypeEnum.REMOTE.getCode().equals(recordInfo.getRecordType())){
                    recordType = RecordTypeEnum.COMMON.getCode();
                }
                rtspUrlList.add(recordInfo);
                //rtspUrlList.add(String.format(httpCenterHlsTemplate,host,arr[0],arr[3],recordType,recordInfo.getStreamType(),totalLength,recordInfo.getStartTime(),recordInfo.getEndTime()).replaceAll("\\$","%24"));
                //rtspUrlList.add(String.format(httpsCenterHlsTemplate,host,arr[0],arr[3],recordType,recordInfo.getStreamType(),totalLength,recordInfo.getStartTime(),recordInfo.getEndTime()).replaceAll("\\$","%24"));

            }else{//录像来源为:设备
                if(SSRecordTypeEnum.ALARM.getCode().equals(recordInfo.getRecordType())){
                    recordType = "2";
                }else if(SSRecordTypeEnum.TIME.getCode().equals(recordInfo.getRecordType())){
                    recordType = "6";
                }else if(SSRecordTypeEnum.REMOTE.getCode().equals(recordInfo.getRecordType())){
                    recordType = "3";
                }
                rtspUrlList.add(recordInfo);
                //rtspUrlList.add(String.format(httpDeviceHlsTemplate,host,arr[0],arr[3],recordType,recordInfo.getStreamType(),totalLength,recordInfo.getStartTime(),recordInfo.getEndTime()).replaceAll("\\$","%24"));
                //rtspUrlList.add(String.format(httpsDeviceHlsTemplate,host,arr[0],arr[3],recordType,recordInfo.getStreamType(),totalLength,recordInfo.getStartTime(),recordInfo.getEndTime()).replaceAll("\\$","%24"));
            }
        }
        return rtspUrlList;
    }

    private List getRTSPUrlList(List list,OauthConfigBaseInfo oauthConfigBaseInfo) throws ClientException {
        List rtspUrlList = new ArrayList();
        String host = oauthConfigBaseInfo.getHttpConfigInfo().getHost();
        String [] arr = null;
        String recordType = null;
        for(RecordInfo recordInfo : list){
            arr = recordInfo.getChannelId().split("\\$");

            //录像来源为:设备
            if(RecordSourceEnum.DEVICE.getCode().equals(recordInfo.getRecordSource())){
                rtspUrlList.add(recordInfo);
                //rtspUrlList.addAll(startPlaybackByTime(recordInfo,oauthConfigBaseInfo));
            }else{
                rtspUrlList.add(recordInfo);
                //rtspUrlList.addAll(startPlaybackByFile(recordInfo,oauthConfigBaseInfo));
                if(SSRecordTypeEnum.ALARM.getCode().equals(recordInfo.getRecordType())){
                    recordType = "2";
                }else if(SSRecordTypeEnum.TIME.getCode().equals(recordInfo.getRecordType())){
                    recordType = RecordTypeEnum.COMMON.getCode();
                }else if(SSRecordTypeEnum.REMOTE.getCode().equals(recordInfo.getRecordType())){
                    recordType = RecordTypeEnum.COMMON.getCode();
                }
            }
            //rtspUrlList.add(recordInfo);
            //rtspUrlList.add(String.format(rtspTemplate,host,arr[0],arr[3],recordInfo.getStreamType(),recordInfo.getRecordSource(),recordType,recordInfo.getStartTime(),recordInfo.getEndTime()).replaceAll("\\$","%24"));
        }
        return rtspUrlList;
    }

    /*************************SDK新版本:统一采用掉接口的方式获取录像回放地址*********************************/
    /**
     * 获取HLS/RTMP普通录像回放地址
     * **/
    private List getCommonHlsUrlList(List list, CommonPlayBackRequest request)throws ClientException,BusinessException {
        if(CollectionUtil.isEmpty(list)){
            logger.info("Hls play back record is empty.");
            return list;
        }

        for(RecordInfo recordInfo : list){
            IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
            String url = AdminConstant.HLS_RTMP_STRREAM_ADDRESS_GET;
            GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
            GeneralResponse generalResponse = null;
            SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

            try{
                JSONObject data = new JSONObject();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("channelId",request.getChannelId());
                jsonObject.put("beginTime", formatter.format(formatter.parse(formatter.format(recordInfo.getStartTime() * 1000))));
                jsonObject.put("endTime",formatter.format(formatter.parse(formatter.format(recordInfo.getEndTime() * 1000))));
                jsonObject.put("recordSource",request.getRecordSource());
                jsonObject.put("recordType","1");
                jsonObject.put("streamType",recordInfo.getStreamType());
                jsonObject.put("type",request.getType());
                data.put("data",jsonObject);
                generalRequest.body(data.toString());
                generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
            }catch (Exception e){
                logger.error("Acquire HLS/RTMP stream address fail.errorMsg:{}.",e,e.getMessage());
                throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
            }
            if(!"1000".equals(generalResponse.getCode())){
                logger.error("Acquire HLS/RTMP stream address 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(generalResponse.getCode(),"The obtained HLS/RTMP address is empty.");
            }
            String rtspUrls = jsonObject.getJSONObject("data").getStr("url");
            String token = jsonObject.getJSONObject("data").getStr("token");
            String[] rtspUrlArr = rtspUrls.split("\\|");

            if(rtspUrlArr == null || rtspUrlArr.length == 0){
                throw new BusinessException(generalResponse.getCode(),"The obtained HLS/RTMP address is empty.");
            }

            List urls = new ArrayList();
            for(int i = 0; i < rtspUrlArr.length; i++){
                if(!StringUtils.isEmpty(token)) {
                    urls.add(rtspUrlArr[i] + "?token=" + token);
                } else {
                    urls.add(rtspUrlArr[i]);
                }
            }
            recordInfo.setRtspUrl(urls);
        }

        return list;
    }

    /**
     * 获取RTSP普通录像回放地址
     * **/
    private List getCommonRTSPUrlList (List list, CommonPlayBackRequest request)throws ClientException,BusinessException {
        if(CollectionUtil.isEmpty(list)){
            logger.info("RTSP play back record is empty.");
            return list;
        }

        for(RecordInfo recordInfo : list){
            //录像来源为:中心,以文件形式回放录像
            String url = AdminConstant.PLAY_BACK_BY_FILE;
            if(RecordSourceEnum.DEVICE.getCode().equals(recordInfo.getRecordSource())){
                //录像来源为:设备,以时间形式回放录像
                url = AdminConstant.PLAY_BACK_BY_TIME;
            }
            IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
            GeneralRequest generalRequest = new GeneralRequest(request.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
            GeneralResponse generalResponse = null;
            SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

            try{
                JSONObject data = new JSONObject();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("channelId",request.getChannelId());
                jsonObject.put("startTime", recordInfo.getStartTime());
                jsonObject.put("endTime",recordInfo.getEndTime());
                jsonObject.put("recordSource",request.getRecordSource());
                jsonObject.put("recordType","0");
                jsonObject.put("streamType","0");
                if(!RecordSourceEnum.DEVICE.getCode().equals(list.get(0).getRecordSource())){
                    jsonObject.put("ssId", recordInfo.getSsId());
                    jsonObject.put("streamId",recordInfo.getStreamId());
                    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 RTSP stream address fail,errorMsg:{}.",e,e.getMessage());
                throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
            }
            if(!"1000".equals(generalResponse.getCode())){
                logger.error("Acquire RTSP stream address 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(generalResponse.getCode(),"RTSP stream address is empty.");
            }
            String rtspUrls = jsonObject.getJSONObject("data").getStr("url");
            String token = jsonObject.getJSONObject("data").getStr("token");
            String[] rtspUrlArr = rtspUrls.split("\\|");

            if(rtspUrlArr == null || rtspUrlArr.length == 0){
                throw new BusinessException(generalResponse.getCode(),"RTSP stream address is empty.");
            }

            List urls = new ArrayList();
            for(int i = 0; i < rtspUrlArr.length; i++){
                if(!StringUtils.isEmpty(token)) {
                    urls.add(rtspUrlArr[i] + "?token=" + token);
                } else {
                    urls.add(rtspUrlArr[i]);
                }
            }
            recordInfo.setRtspUrl(urls);
        }

        return list;
    }

    /**
     * 获取RTSP报警录像回放地址
     * **/
    private List getAlarmRTSPUrlList (List list, AlarmPlayBackRequest request)throws ClientException,BusinessException{
        if(CollectionUtil.isEmpty(list)){
            logger.info("RTSP play back record is empty.");
            return list;
        }

        for(RecordInfo recordInfo : list){
            //录像来源为:中心,以文件形式回放录像
            String url = AdminConstant.PLAY_BACK_BY_FILE;
            if(RecordSourceEnum.DEVICE.getCode().equals(recordInfo.getRecordSource())){
                //录像来源为:设备,以时间形式回放录像
                url = AdminConstant.PLAY_BACK_BY_TIME;
            }
            //录像来源为:设备,以时间形式回放录像
            IccClient iccClient = new IccClient(request.getOauthConfigBaseInfo());
            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",recordInfo.getChannelId());
                jsonObject.put("startTime", recordInfo.getStartTime());
                jsonObject.put("endTime",recordInfo.getEndTime());
                jsonObject.put("recordSource",recordInfo.getRecordSource());
                jsonObject.put("recordType","0");
                jsonObject.put("streamType","0");
                if(!RecordSourceEnum.DEVICE.getCode().equals(list.get(0).getRecordSource())){
                    jsonObject.put("ssId", recordInfo.getSsId());
                    jsonObject.put("streamId",recordInfo.getStreamId());
                    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 RTSP stream address fail,errorMsg:{}.",e,e.getMessage());
                throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
            }
            if(!"1000".equals(generalResponse.getCode())){
                logger.error("Acquire RTSP stream address 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(generalResponse.getCode(),"RTSP stream address is empty.");
            }
            String rtspUrls = jsonObject.getJSONObject("data").getStr("url");
            String token = jsonObject.getJSONObject("data").getStr("token");
            String[] rtspUrlArr = rtspUrls.split("\\|");

            if(rtspUrlArr == null || rtspUrlArr.length == 0){
                throw new BusinessException(generalResponse.getCode(),"RTSP stream address is empty.");
            }
            List urls = new ArrayList();
            for(int i = 0; i < rtspUrlArr.length; i++){
                if(!StringUtils.isEmpty(token)) {
                    urls.add(rtspUrlArr[i] + "?token=" + token);
                } else {
                    urls.add(rtspUrlArr[i]);
                }
            }
            recordInfo.setRtspUrl(urls);
        }

        return list;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy