使用RTSP判断摄像机设备是否在线以及快照抓取

是否在线

使用Onvif/RTSP协议接入摄像机的应用中非常重要的一环就是设备的在线状态判断和快照抓取

设备在线状态判断有很多种方法又没有什么方法

说有很多种方法指的是,我们可以使用ICMP协议对设备的进行ping操作,或者可以对设备的特定端口进行连接获取判断连接是否一次,再或者使用RTSP协议流程进行判定,但是都有个问题–不靠谱,禁ping,端口未知,RTSP协议实现不标准,都会导致–不靠谱

所以说,没什么方法

那就是–走RTSP流程拉流

拉流都成功了,你肯定在线,否则你不在线,不然在线联网又有什么用?我又不能用

快照抓取

快照抓取这件事就很简单了

int DecoderHelper::WriteJPEG(const char* out_file) const
{
	// 输出文件路径  
	if (!out_file)
		return -1;

	// 分配AVFormatContext对象  
	AVFormatContext* avFormatContext = avformat_alloc_context();

	// 设置输出文件格式  
	avFormatContext->oformat = av_guess_format("mjpeg", NULL, NULL);
	// 创建并初始化一个和该url相关的AVIOContext  
	if (avio_open(&avFormatContext->pb, out_file, AVIO_FLAG_READ_WRITE) < 0)
	{
		printf("Couldn't open output file.");
		return -2;
	}

	// 构建一个新stream  
	AVStream* avStream = avformat_new_stream(avFormatContext, NULL);
	if (!avStream)
	{
		return -1;
	}

	// Begin Output some information  
	//av_dump_format(pFormatCtx, 0, out_file, 1);
	// End Output some information  

	AVCodecParameters* codecpar = avStream->codecpar;
	codecpar->codec_id = avFormatContext->oformat->video_codec;

	// 查找解码器  
	AVCodec* codec = avcodec_find_encoder(codecpar->codec_id);
	if (!codec)
	{
		printf("Codec not found.");
		return -1;
	}

	// 设置该stream的信息  
	AVCodecContext* avCodecContext = avcodec_alloc_context3(codec);
	avcodec_parameters_to_context(avCodecContext, codecpar);

	avCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
	avCodecContext->pix_fmt = AV_PIX_FMT_YUVJ420P;
	avCodecContext->width = width_;
	avCodecContext->height = height_;
	avCodecContext->time_base.num = 1;
	avCodecContext->time_base.den = 25;

	// 设置pCodecCtx的解码器为pCodec  
	int ret = avcodec_open2(avCodecContext, codec, NULL);
	if (ret < 0)
	{
		printf("Could not open codec.ret=%d", ret);
		return -1;
	}

	//Write Header  
	avformat_write_header(avFormatContext, NULL);

	//Encode  
	// 给AVPacket分配足够大的空间  
	AVPacketWrapper packetWrapper(width_ * height_ * 3);
	AVPacket* packet = packetWrapper.getPacket();

	ret = avcodec_send_frame(avCodecContext, avFrame_);
	if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
	{
		return -3;
	}
	ret = avcodec_receive_packet(avCodecContext, packet);
	if (ret < 0 && ret != AVERROR_EOF)
	{
		return -3;
	}

	av_write_frame(avFormatContext, packet);

	//Write Trailer  
	av_write_trailer(avFormatContext);

	avio_close(avFormatContext->pb);
	avcodec_free_context(&avCodecContext);
	avformat_free_context(avFormatContext);

	return 0;
}

好的配合

我们何不把快照抓取和在线状态判定做到一起呢?

是的,抓取到快照我们判断为在线,否则为离线

这样,摄像机的在线状态是“真正的”在线状态

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值