物联网(java版本)臻识摄像头+本地实时显示视频

 

部分代码显示

package com.xdnnaovideo.videoservice.listenermessage;


import com.google.gson.Gson;
import com.xdnnaovideo.videoservice.entity.AddAndUpdateRtspVo;
import com.xdnnaovideo.videoservice.entity.AjaxResult;
import com.xdnnaovideo.videoservice.rtsp.MediaConstant;
import com.xdnnaovideo.videoservice.rtsp.MediaStart;
import org.apache.commons.lang3.StringUtils;
import org.bytedeco.javacv.FrameGrabber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.TextMessage;

@Component
public class ActivemqListener {
    @Autowired
    MediaStart mediaStart;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${webserviceluwairstp}")
    String webserviceluwairstp;

    @JmsListener(destination = "${webserviceluwairstp}")
    public void webserviceluwairstp(TextMessage message) {
        try {
            AddAndUpdateRtspVo addAndUpdateRtspVo = new Gson().fromJson(message.getText(), AddAndUpdateRtspVo.class);
            String type = addAndUpdateRtspVo.getType();
            if("start".equals(type)){
                convertexsetting(addAndUpdateRtspVo);
                logger.info("设置数据rstp进来:" + addAndUpdateRtspVo);
            }else if("stop".equals(type)){
               stopconvertexsetting(addAndUpdateRtspVo);
                logger.info("设置数据rstp进来:" + addAndUpdateRtspVo);
            }

            message.acknowledge();

        } catch (Exception error) {
            logger.error("发生错误:{}", error.getMessage());
        }

    }

    private void convertexsetting(AddAndUpdateRtspVo addAndUpdateRtspVo) {
        String linkId = addAndUpdateRtspVo.getLinkIdDeviceInfo();
        String rtspPath = addAndUpdateRtspVo.getRtspPath();


        boolean haveSameFFmpegFrameGrabberCache = MediaConstant.isHaveSameFFmpegFrameGrabberCache(linkId.trim(), rtspPath.trim());
        boolean haveSamertspPath = MediaConstant.isHaveSamertspPath(rtspPath.trim());
        String linkIdCopy = MediaConstant.getLinkId(rtspPath.trim());
        // 当要修改的rtspPath地址不是在存在准备修改的linkId上,而是存在其他linkId上,则不允许修改,这样做是避免同时解析两个相同的rtsp地址导致流错乱
        if (haveSameFFmpegFrameGrabberCache && haveSamertspPath) {
            logger.info("输入参数存在有误,要更改的rtspPath地址已经存在于其他链路上,不允许在此链路修改,请移步先将链路:[" + linkIdCopy + "]的rtsp地址更改后再来更改");
            return;
        }
        MediaConstant.FFmpegFrameGrabberCache fFmpegFrameGrabberCache1 = MediaConstant.getFFmpegFrameGrabberCache(linkId.trim());
        String message = "";
        // 如果为空,则说明没有添加过该链路的FFmpegFrameGrabberCache
        if (fFmpegFrameGrabberCache1 == null) {
            MediaConstant.FFmpegFrameGrabberCache fFmpegFrameGrabberCache =
                    new MediaConstant.FFmpegFrameGrabberCache()
                            .setLinkId(linkId.trim())
                            .setRtspPath(rtspPath.trim());
            MediaConstant.fFmpegFrameGrabberCaches.put(linkId.trim(), fFmpegFrameGrabberCache);
            message = "添加" + linkId + ":rtsp地址成功";
        } else { // 如果不为空,则要将该链路对应的数据更改为初始化状态,添加新的信息
            try {
                fFmpegFrameGrabberCache1.getGrabber().stop();
            } catch (FrameGrabber.Exception e) {
                logger.error("FrameGrabber stop exception: ", e);
            }
            fFmpegFrameGrabberCache1.setRtspPath(rtspPath)
                    .setLinkId(linkId)
                    .setGrabber(null)
                    .setIsStart(false);
            message = "修改" + linkId + ":rtsp地址成功";
        }
        //启动当前链路线程
        mediaStart.addLinkThreadPool(linkId.trim());
    }

    private void stopconvertexsetting(AddAndUpdateRtspVo addAndUpdateRtspVo) {
        String linkId = addAndUpdateRtspVo.getLinkIdDeviceInfo();
        String rtspPath = addAndUpdateRtspVo.getRtspPath();
        MediaConstant.FFmpegFrameGrabberCache fFmpegFrameGrabberCache1 = MediaConstant.getFFmpegFrameGrabberCache(linkId.trim());
        // 如果为空,则说明没有添加过该链路的FFmpegFrameGrabberCache
        if (fFmpegFrameGrabberCache1 != null) {
            try {
                fFmpegFrameGrabberCache1.getGrabber().stop();
            } catch (FrameGrabber.Exception e) {
                logger.error("FrameGrabber stop exception: ", e);
            }
            fFmpegFrameGrabberCache1.setRtspPath(rtspPath)
                    .setLinkId(linkId)
                    .setGrabber(null)
                    .setIsStart(false);
        }
        MediaConstant.removeisHaveSameFFmpegFrameGrabberCache(linkId.trim());
        MediaConstant.removeisHaveSameFFmpegFrameGrabberCache(linkId.trim());
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值