ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)
Posted DoubleLi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)相关的知识,希望对你有一定的参考价值。
前言及鸣谢:
感谢guog先生,快活林高先生,onvif全国交流群的的酷夏先生在开发过程中给予的巨大支持,没有你们的帮助开发过程将异常艰难啊。谢谢了!
ONVIF介绍:
流程总览:
搜索:Probe : 发现网络摄像头,获取 webserver 地址
http://192.168.15.240/onvif/device_service
能力获取:GetCapabilities :获取设备能力文件,从中识别出媒体信息地址 URI: http://192.168.15.240/onvif/Media
媒体信息获取:GetProfiles : 获取媒体信息文件,识别主通道、子通道的视频编码分辨率
RTSP地址获取:GetStreamUri :获取指定通道的流媒体地址 rtsp://192.168.15.240:554/Streaming/Channels/2?transportmode=unicast
Gsoap及开发框架生成:
命令:
wsdl2h -o onvif.h -c -s -t ./typemap.dat devicemgmt.wsdl media.wsdl event.wsdl display.wsdl deviceio.wsdl imaging.wsdl ptz.wsdl receiver.wsdl recording.wsdl search.wsdl remotediscovery.wsdl replay.wsdl analytics.wsdl analyticsdevice.wsdl actionengine.wsdl accesscontrol.wsdl doorcontrol.wsdl
离线文件在:
http://download.csdn.net/detail/u011597695/5875143wsdl2h -o onvif.h -c -s -t ./typemap.dat http://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl http://www.onvif.org/onvif/ver10/event/wsdl/event.wsdl http://www.onvif.org/onvif/ver10/display.wsdl http://www.onvif.org/onvif/ver10/deviceio.wsdl http://www.onvif.org/onvif/ver20/imaging/wsdl/imaging.wsdl http://www.onvif.org/onvif/ver20/ptz/wsdl/ptz.wsdl http://www.onvif.org/onvif/ver10/receiver.wsdl http://www.onvif.org/onvif/ver10/recording.wsdl http://www.onvif.org/onvif/ver10/search.wsdl http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl http://www.onvif.org/onvif/ver10/replay.wsdl http://www.onvif.org/onvif/ver20/analytics/wsdl/analytics.wsdl http://www.onvif.org/onvif/ver10/analyticsdevice.wsdl http://www.onvif.org/ver10/actionengine.wsdl http://www.onvif.org/ver10/pacs/accesscontrol.wsdl http://www.onvif.org/ver10/pacs/doorcontrol.wsdl
(记得拷贝gsoap的typemap文件至生成目录下,wsdl2h命令需要这个。)wsdl2h -o onvif.h -c -s -t ./typemap.dat devicemgmt.wsdl media.wsdl event.wsdl display.wsdl deviceio.wsdl imaging.wsdl ptz.wsdl receiver.wsdl recording.wsdl search.wsdl remotediscovery.wsdl replay.wsdl analytics.wsdl analyticsdevice.wsdl actionengine.wsdl accesscontrol.wsdl doorcontrol.wsdl
现在可以开始生成了:如下:如果直接生成对应C的库文件会发生重复定义错误,可以修改该文件。
wsa5.h(288): **ERROR**: remote method name clash: struct/class ‘SOAP_ENV__Fault‘ already declared at line 274
打开文件gsoap_2.8.16/gsoap-2.8/gsoap/import/ wsa5.h
将277行int SOAP_ENV__Fault修改为int SOAP_ENV__Fault_alex
笔者没有使用这种方法,是将这个结构体直接注释的方式,最后的结果是,都可以使用。同时上一步生成的onvif.h文件中没有打开wsse.h, 导致最后生成代码中SOAP_ENV__Header 结构体中缺少定义 wsse__Security数据段,无法进行鉴权命令。
即:添加对openssl的支持,在上一步生成的onvif.h中添加(可选)
#import "wsse.h"
随后使用命令生成:
soapcpp2 -c onvif.h -x -I/root/Tools/Gsoap/gsoap-2.8/gsoap/import -I/root/Tools/Gsoap/gsoap-2.8/gsoap/ -I/root/Tools/Gsoap/gsoap-2.8/gsoap/custom -I/root/Tools/Gsoap/gsoap-2.8/gsoap/extras -I/root/Tools/Gsoap/gsoap-2.8/gsoap/plugin
到此为止,基于 C 的客户端和服务器的Onvif开发框架及已经搭建完成。
设备搜索原理及编程技巧:
struct soap* NewSoap(struct SOAP_ENV__Header *header,struct soap* soap,
wsdd__ProbeType *req_,
wsdd__ScopesType *sScope_)
{
soap = soap_new();
if(NULL == soap )
{
printf("sopa new error\r\n");
return NULL;
}
soap->recv_timeout = 5;
soap_set_namespaces(soap, namespaces);
soap_default_SOAP_ENV__Header(soap, header);
uuid_t uuid;
char guid_string[100];
uuid_generate(uuid);
uuid_unparse(uuid, guid_string);
header->wsa__MessageID = 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";
soap->header = header;
soap_default_wsdd__ScopesType(soap, sScope_);
sScope_->__item = "";
soap_default_wsdd__ProbeType(soap, req_);
req_->Scopes = sScope_;
req_->Types = ""; //"dn:NetworkVideoTransmitter";
return soap ;
}
int i = 0;
result = soap_send___wsdd__Probe(soap, MULTICAST_ADDRESS, NULL, &req);
while(result == SOAP_OK)
{
result = soap_recv___wsdd__ProbeMatches(soap, &resp);
if(result == SOAP_OK)
{
if(soap->error)
{
printf("soap error 1: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
}
else
{
printf("Onvif Device detected *********************************************\r\n");
for(i = 0; i < resp.wsdd__ProbeMatches->__sizeProbeMatch; i++)
{
printf("__sizeProbeMatch : %d\r\n", resp.wsdd__ProbeMatches->__sizeProbeMatch);
printf("wsa__EndpointReference : %p\r\n", resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference);
printf("Target EP Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference.Address);
printf("Target Type : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->Types);
printf("Target Service Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->XAddrs);
printf("Target Metadata Version : %d\r\n", resp.wsdd__ProbeMatches->ProbeMatch->MetadataVersion);
if(resp.wsdd__ProbeMatches->ProbeMatch->Scopes)
{
printf("Target Scopes Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->Scopes->__item);
}
}
break;
}
}
else if (soap->error)
{
printf("[%d] soap error 2: %d, %s, %s\n", __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
}
}
设备鉴权:
soap_wsse_add_UsernameTokenDigest(soap,"user", ONVIF_USER, ONVIF_PASSWORD);
原理也很容易明白其实,就是讲http的soap消息加入对应header中xml的元素而已,然后敏感消息digest MD5加密编码。获取能力:
void UserGetCapabilities(struct soap *soap ,struct __wsdd__ProbeMatches *resp,
struct _tds__GetCapabilities *capa_req,struct _tds__GetCapabilitiesResponse *capa_resp)
{
capa_req->Category = (enum tt__CapabilityCategory *)soap_malloc(soap, sizeof(int));
capa_req->__sizeCategory = 1;
*(capa_req->Category) = (enum tt__CapabilityCategory)(tt__CapabilityCategory__Media);
capa_resp->Capabilities = (struct tt__Capabilities*)soap_malloc(soap,sizeof(struct tt__Capabilities)) ;
soap_wsse_add_UsernameTokenDigest(soap,"user", ONVIF_USER, ONVIF_PASSWORD);
printf("\n--------------------Now Gettting Capabilities NOW --------------------\n\n");
int result = soap_call___tds__GetCapabilities(soap, resp->wsdd__ProbeMatches->ProbeMatch->XAddrs, NULL, capa_req, capa_resp);
if (soap->error)
{
printf("[%s][%d]--->>> soap error: %d, %s, %s\n", __func__, __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
int retval = soap->error;
exit(-1) ;
}
else
{
printf(" \n--------------------GetCapabilities OK! result=%d--------------\n \n",result);
if(capa_resp->Capabilities==NULL)
{
printf(" GetCapabilities failed! result=%d \n",result);
}
else
{
printf(" Media->XAddr=%s \n", capa_resp->Capabilities->Media->XAddr);
}
}
}
获取媒体信息Profile:
void UserGetProfiles(struct soap *soap,struct _trt__GetProfiles *trt__GetProfiles,
struct _trt__GetProfilesResponse *trt__GetProfilesResponse ,struct _tds__GetCapabilitiesResponse *capa_resp)
{
int result=0 ;
printf("\n-------------------Getting Onvif Devices Profiles--------------\n\n");
soap_wsse_add_UsernameTokenDigest(soap,"user", ONVIF_USER, ONVIF_PASSWORD);
result = soap_call___trt__GetProfiles(soap, capa_resp->Capabilities->Media->XAddr, NULL, trt__GetProfiles, trt__GetProfilesResponse);
if (result==-1)
//NOTE: it may be regular if result isn‘t SOAP_OK.Because some attributes aren‘t supported by server.
//any question email [email protected]
{
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
exit(-1);
}
else{
printf("\n-------------------Profiles Get OK--------------\n\n");
if(trt__GetProfilesResponse->Profiles!=NULL)
{
if(trt__GetProfilesResponse->Profiles->Name!=NULL){
printf("Profiles Name:%s \n",trt__GetProfilesResponse->Profiles->Name);
}
if(trt__GetProfilesResponse->Profiles->token!=NULL){
printf("Profiles Taken:%s\n",trt__GetProfilesResponse->Profiles->token);
}
}
else{
printf("Profiles Get inner Error\n");
}
}
printf("Profiles Get Procedure over\n");
}
获取RTSP的URI:
void UserGetUri(struct soap *soap,struct _trt__GetStreamUri *trt__GetStreamUri,struct _trt__GetStreamUriResponse *trt__GetStreamUriResponse,
struct _trt__GetProfilesResponse *trt__GetProfilesResponse,struct _tds__GetCapabilitiesResponse *capa_resp)
{
int result=0 ;
trt__GetStreamUri->StreamSetup = (struct tt__StreamSetup*)soap_malloc(soap,sizeof(struct tt__StreamSetup));//初始化,分配空间
trt__GetStreamUri->StreamSetup->Stream = 0;//stream type
trt__GetStreamUri->StreamSetup->Transport = (struct tt__Transport *)soap_malloc(soap, sizeof(struct tt__Transport));//初始化,分配空间
trt__GetStreamUri->StreamSetup->Transport->Protocol = 0;
trt__GetStreamUri->StreamSetup->Transport->Tunnel = 0;
trt__GetStreamUri->StreamSetup->__size = 1;
trt__GetStreamUri->StreamSetup->__any = NULL;
trt__GetStreamUri->StreamSetup->__anyAttribute =NULL;
trt__GetStreamUri->ProfileToken = trt__GetProfilesResponse->Profiles->token ;
printf("\n\n---------------Getting Uri----------------\n\n");
soap_wsse_add_UsernameTokenDigest(soap,"user", ONVIF_USER, ONVIF_PASSWORD);
soap_call___trt__GetStreamUri(soap, capa_resp->Capabilities->Media->XAddr, NULL, trt__GetStreamUri, trt__GetStreamUriResponse);
if (soap->error) {
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
}
else{
printf("!!!!NOTE: RTSP Addr Get Done is :%s \n",trt__GetStreamUriResponse->MediaUri->Uri);
}
}
开发注意事项:(必读)
soap通信的命名空间如果错误则不能检索到设备:编译好的wsdd.nsmap文件需要修改命名空间,如下:
如果要正常开发,被检索到,或者发现其他设备需要nsmap修改如下:1.1换1.2
以下命名空间表示SOAP1.1版本:
{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/*/soap-envelope", NULL},
{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL}, //1.1
以下命名空间表示SOAP1.2版本:
{"SOAP-ENV", "http://www.w3.org/2003/05/soap-envelope", "http://schemas.xmlsoap.org/soap/envelope/", NULL},
{"SOAP-ENC", "http://www.w3.org/2003/05/soap-encoding", "http://schemas.xmlsoap.org/soap/encoding/", NULL}, //1.2
另外存在的客户端搜索不到设备情况:
1.是否有vpn,存在的话,本机IP会产生变化导致不能搜到?抓包可以看到,3702端口包的数据源地址改变。
2.uuid是否已经赋值。
3.有时,windows宿主机装有虚拟机,也可能造成onvif客户端的ip获取错误。故搜索不到。
这些问题,在交换机或者路由支持本地局域网跨网段数据UDP交互时,均不会产生。
调试技巧:
fsend/ frecv 打印出发送和接收到的报文。使用xml编辑器分析。当然也可以直接用浏览器看。
1、打开onvif调试开关,以便让onvif打印一些可用的调试信息。
在Makefile中添加调试宏定义如: CC = gcc -DDEBUG
2、打开调试宏后,默认在程序运行的目录产生三个文件:
RECV.log
SENT.log
TEST.log
RECV.log是onvif接收到的SOAP数据,没接收一条,都会在RECV.log中记录
SENT.log是onvif发送出去的SOAP数据,没发送一套,也会在SENT.log中生成记录
最后是TEST.log,如果说RECV和SENT可以用wireshark工具抓包代替,那么TEST.log是谁也替代不了的,TEST.log记录了onvif的实时的工作状态。
尤其当出现segmentation fault错误,TEST.log就成了唯一一个能够定位到具体内存出错的地方了。
SOAP_TYPE返回soap->error=4的错误说明
关于数据正确(抓包可收到数据),但soap返回错误,为4 及 SOAP_TYPE 的问题:
GetCapabilities的过程错误时。
多次调试后得出结论,是tt__CapabilityCategory 的设置问题,有的设备不具备全部功能,而请求全部或请求没有的功能就可能造成这种问题,推荐写5(tt__CapabilityCategory__Media) 这是大多数设置有的能力,而且最常用。
GetProfile时错误:
其实数据在抓包过程中也能完全抓到,多次调试后,发现结构体需要的Name以及token关键字被赋值。其他的没有,说明本点返回与服务器的支持性有很大关系。及,开发过程中需要对应自己的需求,根据实际的需要和返回错误,读取返回结构体数据。
资源:
ONVIFDEVICEMANAGER下载地址:
http://pan.baidu.com/share/link?shareid=1967805400&uk=70662920&fid=3981296515
ONVIFTESTTOOL下载地址:
http://www.cr173.com/soft/66448.html
官网开发者向导资料下载地址:
http://www.onvif.org/Resources/WhitePapers.aspx
参考文章:
http://blog.csdn.net/ghostyu/article/details/8162280
http://blog.csdn.net/max_min_go/article/details/17964643
linux设备上的Onvif 实现10:获取支持通道的RTSP地址
http://gaohtao.blog.163.com/blog/static/58241823201381113214599/
http://blog.csdn.net/max_min_go/article/details/17617057
ONVIF协议开发资源
http://www.csdn.net/tag/onvif%252520%2525E5%25258D%25258F%2525E8%2525AE%2525AE
http://blog.csdn.net/love_xjhu/article/details/11821037
http://blog.csdn.net/u012084827/article/details/19031969
http://blog.csdn.net/love_xjhu/article/details/9772361
http://blog.csdn.net/zsl461975543/article/details/8971143
代码框架生成之Onvif开发
http://www.yc-edu.org/C__peixun/6655.html
linux设备上的Onvif 实现4:成功编译gsoap 2.8.15
http://blog.csdn.net/u012084827/article/details/12202133
onvif规范的实现:onvif开发常用调试方法 和常见的segmentation fault错误
http://blog.csdn.net/ghostyu/article/details/8432760
linux设备上的Onvif 实现6:获取摄像头的流媒体地址完整流程
http://blog.csdn.net/u012084827/article/details/12201997
S?O?A?P? ?错?误?代?码?表
以上是关于ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)的主要内容,如果未能解决你的问题,请参考以下文章
Onvif/RTSP网络安防摄像机网页无插件直播方案EasyNVR如何判断前端设备的类型?
RTSP_ONVIF协议视频流媒体网页无插件直播服务器EasyNVR配置海康大华等安防摄像头通道地址的方法