轻松实现其他协议到Gb28181协议的转换网关(onvif转GB28181)

       为了实现视频网络的互联互通,打破信息孤岛,国家慢慢要求各行各业视频监控支持GB28181协议,但是有很多以前就已经安装部署的老摄像头,不支持GB28181协议,怎么办呢?

       我们可以采用视频媒体和协议的转换网关,实现视频源设备非国标到GB28181的转换。是的,没有看错,只需要一个软件网关。onvif作为国际标准协议,很多老摄像头是支持onvif协议的,我们本章以onvif协议的设备为例,讲一下如何转gb28181。

       一.实现onvif设备的查找

           onvif设备的查找是标准的onvif协议的部分,我们使用gsoap库和onvif项目的wdsl进行编译,编译方法网上很多,我会把编译好的onvif协议的c++代码工程文件发布到本站的下载站上,这里我们还是只讲重点和核心。首先初始化soap协议,代码如下:

SYONVIF_API void* SoapInit(char* szIP)
{
	struct soap *tsoap;
	tsoap = soap_new();
	if (!tsoap)
	{
		return tsoap;
	}

	// 想绑定的IP地址
	struct in_addr if_req;
	if_req.s_addr = inet_addr(szIP);    
	tsoap->ipv4_multicast_if = (char*)soap_malloc(tsoap, sizeof(in_addr));
	memset(tsoap->ipv4_multicast_if, 0, sizeof(in_addr));
	memcpy(tsoap->ipv4_multicast_if, (char*)&if_req, sizeof(if_req));

	//超时时间,当达到10秒没有发现设备时,自动返回
	tsoap->recv_timeout = 5;
	soap_set_namespaces(tsoap, namespaces);

	return tsoap;
}

szIP是onvif网内的ip,注意可能会有多网卡,那么你需要传入对应局域网的IP,初始化完成后是设备的发现,onvif设备的发现原理是发送广播,代码如下:


//查找onvif设备
SYONVIF_API int onvif_DiscoveryDevice(void* isoap, vector& onvif_urls)
{
	int result = 0;

	struct wsdd__ProbeType req;
	struct __wsdd__ProbeMatches resp;
	struct wsdd__ScopesType sScope;
	struct SOAP_ENV__Header header;

	struct soap *tsoap = (struct soap *)isoap;

	soap_default_SOAP_ENV__Header(tsoap, &header);

	//生成唯一的messageid
	uuid_t uuid;
	unsigned char *guid_string = NULL;    
	UuidCreate(&uuid);
	UuidToStringA(&uuid, &guid_string);
	header.wsa__MessageID = (char *)malloc(100);
	sprintf_s(header.wsa__MessageID, 100, "uuid:%s", (char *)guid_string);
	header.wsa__To = "urn:schemas-xmlsoap-org:ws:2005:04:discovery";
	header.wsa__Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";
	tsoap->header = &header;

	soap_default_wsdd__ScopesType(tsoap, &sScope);
	sScope.__item = ""; 
	soap_default_wsdd__ProbeType(tsoap, &req);
	req.Scopes = &sScope;

	req.Types = "";   
	int i = 0;
	//发送UDP广播
	result = soap_send___wsdd__Probe(tsoap, "soap.udp://239.255.255.250:3702", NULL, &req);
	//接收数据
	if (result == SOAP_OK)
	{
		result = soap_recv___wsdd__ProbeMatches(tsoap, &resp);
		if (result == SOAP_OK)
		{
			if (tsoap->error)
			{

				result = tsoap->error;
			}
			else
			{
				//发现设备
				for (i = 0; i < resp.wsdd__ProbeMatches->__sizeProbeMatch; i++)
				{
					onvif_urls.push_back(resp.wsdd__ProbeMatches->ProbeMatch[i].XAddrs);
				}
				result = resp.wsdd__ProbeMatches->__sizeProbeMatch;
			}
		}
		else if (tsoap->error)
		{
			result = tsoap->error;
		}
	}
	free(header.wsa__MessageID);
	return result;
}

 设备发现后,需要获取每个设备的媒体信息,或者就是rtsp或http的url,代码如下:

int onvif_GetUri(void* isoap, char* capa_url, char* sztoken, char* user, char* pwd, char* szuri)
{
	int result = 0;

	struct soap *tsoap = (struct soap *)isoap;

	struct _trt__GetStreamUri trt__GetStreamUri;
	struct _trt__GetStreamUriResponse trt__GetStreamUriResponse;
	tt__MediaUri MediaUri;
	tt__StreamSetup StreamSetup;
	tt__Transport Transport;

	memset(&trt__GetStreamUri, 0, sizeof(_trt__GetStreamUri));
	memset(&trt__GetStreamUriResponse, 0, sizeof(_trt__GetStreamUriResponse));
	memset(&MediaUri, 0, sizeof(tt__MediaUri));
	memset(&StreamSetup, 0, sizeof(tt__StreamSetup));
	memset(&Transport, 0, sizeof(tt__Transport));

	trt__GetStreamUriResponse.MediaUri = &MediaUri;
	StreamSetup.Stream = tt__StreamType__RTP_Unicast;
	Transport.Protocol = tt__TransportProtocol__RTSP;
	StreamSetup.Transport = &Transport;
	trt__GetStreamUri.StreamSetup = &StreamSetup;
	trt__GetStreamUri.ProfileToken = sztoken;//nMediaType=0 主码流	nMediaType=1 子码流


	soap_wsse_add_UsernameTokenDigest(tsoap, "user", user, pwd);
	soap_call___trt__GetStreamUri(tsoap, capa_url, NULL, &trt__GetStreamUri, &trt__GetStreamUriResponse);

	if (tsoap->error && tsoap->status != 200)
	{
		return -1;
	}
	else
	{
		sprintf(szuri,"%s", trt__GetStreamUriResponse.MediaUri->Uri);
		return 0;
	}

}

       上面这个函数最后一个参数返回的就是onvif摄像头的rtsp地址,到这里我们完成了第一步的目的。

 

二.注册gb28181服务

      之前我们获取了onvif设备的媒体地址(rtsp地址),我们可以获取该设备的音视频媒体流,那么我们需要以gb28181协议注册到服务器上即可,然后再响应客户端的invite协议,心跳协议等。

     对于gb28181我们采用开源resip库进行封装,主要代码如下:

	gbt::GbtDeviceAgent& agent = getAgent();
	agent.setMessageHandler(this);
	agent.setSink(this);
	agent.setPlaybackHandler(this);

	agent.setRegTime(3600);
	agent.setUserAgent("SyGbtDevice");
	agent.setHeartBeat(m_heartBeatInterval, m_heartBeatCount);

	GBT_Catalog catalog = GBT_Catalog();
	comn::copyStr(catalog.name, "onvif转GB28181网关");
	//comn::copyStr(catalog.deviceID, m_server.username);
	agent.setCatalog(catalog);

	int ret = agent.open(m_ip.c_str(), m_port, m_server);

 注册上去后,客户端就可以看到设备在线,可以发送invite进行点播了,还是发一张图,这样可以让大家看的更加直观:

轻松实现其他协议到Gb28181协议的转换网关(onvif转GB28181)_第1张图片

上图是我搜索的安讯士的onvif摄像机,然后注册到国标上,为了方便直观专门编一个windows版的。

如果遇到类似场景,可以到到此下载地址下载和交流。

你可能感兴趣的:(GB28181)