- 收藏
- 点赞
- 分享
- 举报
ONVIF协议实现2:码流地址的获取并在ONVIF Device Manager测试成功
本帖最后由 goodman 于 2015-1-5 21:26 编辑
工作平台及工具: Ubuntu:12.04 + gcc + OnvifTestTool12.12 gsoap下载:http://www.cs.fsu.edu/~engelen/soap.html PC:live555.exe + test.264 目前的最新版本为:gsoap2.8.21 通过设备发现的实现,对ONVIF有了很多的了解,也对其开发步骤有了很好地认识,现在实现码流的获取也不难按如下步骤即可:
1.相关概念和前置知识 Profile:参见http://www.onvif.org/Portals/0/documents/op/ONVIF_Profile_Policy_v2-0.pdf 1.1、GetCapabilities #获取设备能力表 1.2、GetProfiles # 获取设备的Profile 1.3、GetStreamUri #填充rtsp路径如:rtsp://192.168.1.101/test.264 1.4、RTSP服务器 #因为我们的设备端不是摄像机所以要借助live555来模拟 1.5、GetVideoSourceConfiguration #获取视频源配置信息 1.6、GetVideoEncoderConfigurationOptions #获取编码配置选项 下面我们的框架和具体实现围绕这几个函数开实现。
2.生成框架代码 执行如下代码(我的工程里面有个gen.sh也可以生成代码): ./wsdl2h -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 ./soapcpp2 -c onvif.h -x -d ./ -I ${HOME}/workspace/source/gsoap-2.8/gsoap/import -I ${HOME}/workspace/source/gsoap-2.8/gsoap/ 这里我们最小化实现,只包含这个2个其他的都排除在外,这样生成的代码才最小,将生成的代码从tools目录移动到src目录里面(目录结构请下载对用的工程文件) [attach]2081[/attach] 我们看到完整的工程包含这么多的WSDL如果全部实现,光是定义相应的函数就够你受得了。
3.启动一个gsoap服务 main函数里面启动了一个soap_server,这里使用8080端口sevice的的端口可以改成你自己的 [code] int gsoapInit(struct OnvifApp* app) { if (!app->bindPort) app->bindPort = 8080; strcpy(app->hostAddress, "192.168.1.104"); snprintf(app->xAddr, sizeof(app->xAddr), "http://%s:%d/onvif/device_service", app->hostAddress, app->bindPort); fprintf(stderr, "xAddr:%s\n", app->xAddr); snprintf(app->xAddrRoot, sizeof(app->xAddrRoot), "http://%s:%d/onvif", app->hostAddress, app->bindPort);
soap_init2(&app->soap, SOAP_ENC_MTOM | SOAP_ENC_MIME, 0 );
soap_set_namespaces(&app->soap, namespaces);
soap_set_recv_logfile(&app->soap, "./log/recv.xml");
soap_set_sent_logfile(&app->soap, "./log/send.xml");
soap_set_test_logfile(&app->soap, "./log/test.log");
app->soap.bind_flags = SO_REUSEADDR;
app->soap.accept_timeout = 10;
app->soap.recv_timeout = 10;
app->soap.send_timeout = 10;
app->soap.max_keep_alive = 30;
app->masterSocket = soap_bind(&app->soap, "0.0.0.0", app->bindPort, 100);
if (app->masterSocket < 0) {
soap_print_fault(&app->soap, stderr);
return -1;
}
return 0;
}
int main(int argc, char argv[]) { gApp = (struct OnvifApp)calloc(1, sizeof(*gApp)); if(!gApp) { fprintf(stderr, "calloc wsapp failed\n"); return -1; } for(;;) { if(gsoapInit(gApp)!=0) //具体的函数定义请参考工程文件 break; if (WsAppRun(gApp, 1) < 0) break; getchar(); gsoapFini(gApp); free(gApp); return 0; } free(gApp); return -1; } [/code]
4.整理需要实现的函数 上面的框架代码实现好后,一编译就会报一大推的链接错误,我们需要将这些函数实现,我们按命名空间来讲这些函数分类到2个单独的文件离去,分别为 tds_ssom.c 和 trt_ssom.c具体的内容就不贴了比较多,可以参看我工程里的代码。然后再Makefile里面把这2个文件添加上,就能找到这些引用了,这样 做的好处是,每次需要添加第三个WSDL的时候可以保证前面整理的函数都是可用的,这样可以避免很多重复的劳动。
5.实现关键代码 5.1 tdsGetCapabilities函数 [code] SOAP_FMAC5 int SOAP_FMAC6 tdsGetCapabilities(struct soap soap, struct _tds__GetCapabilities tdsGetCapabilities, struct _tdsGetCapabilitiesResponse *tdsGetCapabilitiesResponse) { printf("%s:%d\n",FUNCTION,LINE); enum ttCapabilityCategory CapabilityCategory; if (!tdsGetCapabilities->Category) { CapabilityCategory = ttCapabilityCategoryAll; } else { CapabilityCategory = (tdsGetCapabilities->Category); } if (CapabilityCategory != ttCapabilityCategoryAll && CapabilityCategory != ttCapabilityCategoryAnalytics && CapabilityCategory != ttCapabilityCategoryDevice && CapabilityCategory != ttCapabilityCategoryEvents && CapabilityCategory != ttCapabilityCategoryImaging && CapabilityCategory != ttCapabilityCategoryMedia && CapabilityCategory != ttCapabilityCategoryPTZ) { fprintf(stderr, "The requested WSDL service category is not supported by the NVT"); //return soap_sender_fault_subcode(soap, "env:Receiver/ter:ActionNotSupported/ter:NoSuchService", "The requested WSDL service category is not supported by the NVT", NULL); ; return SOAP_FAULT; } static struct ttCapabilities tCapabilities; soap_default_ttCapabilities(soap, &tCapabilities); tdsGetCapabilitiesResponse->Capabilities = &tCapabilities; / tt:AnalyticsCapabilities */ if ((CapabilityCategory == ttCapabilityCategoryAll) || (CapabilityCategory == ttCapabilityCategoryAnalytics)) { static struct ttAnalyticsCapabilities tAnalytics; soap_default_ttAnalyticsCapabilities(soap, &tAnalytics); tCapabilities.Analytics = &tAnalytics; static char Analytics_Addr[256]; sprintf(Analytics_Addr, "%sanalytics", gApp->xAddrRoot); tAnalytics.XAddr = Analytics_Addr; tAnalytics.RuleSupport = xsdbooleanfalse; tAnalytics.AnalyticsModuleSupport = xsdbooleanfalse; } / tt:DeviceCapabilities / if ((CapabilityCategory == ttCapabilityCategoryAll) || (CapabilityCategory == ttCapabilityCategoryDevice)) { static struct ttDeviceCapabilities tDevice; soap_default_ttDeviceCapabilities(soap, &tDevice); tCapabilities.Device = &tDevice; static char Device_Addr[64]; sprintf(Device_Addr, "%sdevice_service", gApp->xAddrRoot); tDevice.XAddr = Device_Addr; / tt:NetworkCapabilities / static struct ttNetworkCapabilities tNetwork; soap_defaultttNetworkCapabilities(soap, &tNetwork); tDevice.Network = &tNetwork; static enum xsdboolean enIPFilter = xsdbooleantrue; static enum xsdboolean enZeroConfiguration = xsdbooleanfalse_; static enum xsdboolean enIPVersion6 = xsdboolean_false; static enum xsdboolean enDynDNS = xsdbooleantrue_; tNetwork.IPFilter = &enIPFilter; tNetwork.ZeroConfiguration = &enZeroConfiguration; tNetwork.IPVersion6 = &enIPVersion6; tNetwork.DynDNS = &enDynDNS; static struct ttNetworkCapabilitiesExtension tNetworkCapabilitiesExtension; soap_default_ttNetworkCapabilitiesExtension(soap, &tNetworkCapabilitiesExtension); tNetwork.Extension = &tNetworkCapabilitiesExtension; static enum xsdboolean enDot11Configuration; tNetworkCapabilitiesExtension.Dot11Configuration = &enDot11Configuration; / tt:SystemCapabilities / static struct ttSystemCapabilities tSystemCapabilities; soap_default_ttSystemCapabilities(soap, &tSystemCapabilities); tDevice.System = &tSystemCapabilities; tSystemCapabilities.DiscoveryResolve = xsdbooleanfalse; tSystemCapabilities.DiscoveryBye = xsdbooleantrue; tSystemCapabilities.RemoteDiscovery = xsdbooleantrue; tSystemCapabilities.SystemBackup = xsdbooleanfalse; tSystemCapabilities.SystemLogging = xsdbooleanfalse; tSystemCapabilities.FirmwareUpgrade = xsdbooleantrue; tSystemCapabilities.sizeSupportedVersions = 3; static struct ttOnvifVersion tSupportedVersions[3]; tSupportedVersions[0].Major = 2; tSupportedVersions[0].Minor = 2; tSupportedVersions[1].Major = 2; tSupportedVersions[1].Minor = 1; tSupportedVersions[2].Major = 2; tSupportedVersions[2].Minor = 0; tSystemCapabilities.SupportedVersions = tSupportedVersions; static struct ttSystemCapabilitiesExtension tSystemCapabilitiesExtension; soap_defaultttSystemCapabilitiesExtension(soap, &tSystemCapabilitiesExtension); tSystemCapabilities.Extension = &tSystemCapabilitiesExtension; static enum xsdboolean enHttpFirmwareUpgrade = xsdbooleantrue; static enum xsdboolean enHttpSystemBackup = xsdbooleanfalse_; static enum xsdboolean enHttpSystemLogging = xsdboolean_false; static enum xsdboolean enHttpSupportInformation = xsdbooleanfalse_; tSystemCapabilitiesExtension.HttpFirmwareUpgrade = &enHttpFirmwareUpgrade; tSystemCapabilitiesExtension.HttpSystemBackup = &enHttpSystemBackup; tSystemCapabilitiesExtension.HttpSystemLogging = &enHttpSystemLogging; tSystemCapabilitiesExtension.HttpSupportInformation = &enHttpSupportInformation; / tt:IOCapabilities / static struct ttIOCapabilities tIO; soap_default_ttIOCapabilities(soap, &tIO); tDevice.IO = &tIO; static int iInputConnectors = 1; tIO.InputConnectors = &iInputConnectors; static int iRelayOutputs = 1; tIO.RelayOutputs = &iRelayOutputs; static struct ttIOCapabilitiesExtension tIOCapabilitiesExtension; soap_default_ttIOCapabilitiesExtension(soap, &tIOCapabilitiesExtension); tIO.Extension = &tIOCapabilitiesExtension; static enum xsdboolean enAuxiliary = xsdbooleanfalse_; tIOCapabilitiesExtension.Auxiliary = &enAuxiliary; tIOCapabilitiesExtension.sizeAuxiliaryCommands = 1; static char chAuxiliaryCommands[] = "nothing"; static char* p = chAuxiliaryCommands; tIOCapabilitiesExtension.AuxiliaryCommands = &p;//&chAuxiliaryCommands; static struct ttIOCapabilitiesExtension2 tIOCapabilitiesExtension2; soap_default_ttIOCapabilitiesExtension2(soap, &tIOCapabilitiesExtension2); tIOCapabilitiesExtension.Extension = &tIOCapabilitiesExtension2; tDevice.Security = NULL; } tCapabilities.Events = NULL; / tt:ImagingCapabilities / tCapabilities.Imaging = NULL; if ((CapabilityCategory == ttCapabilityCategoryAll) || (CapabilityCategory == ttCapabilityCategoryMedia)) { static struct ttMediaCapabilities tMedia; soap_default_tt__MediaCapabilities(soap, &tMedia); tCapabilities.Media = &tMedia; static char Media_Addr[64]; sprintf(Media_Addr, "%sMedia", gApp->xAddrRoot); tMedia.XAddr = Media_Addr; static struct ttRealTimeStreamingCapabilities tRealTimeStreamingCapabilities; soap_defaultttRealTimeStreamingCapabilities(soap, &tRealTimeStreamingCapabilities); tMedia.StreamingCapabilities = &tRealTimeStreamingCapabilities; static enum xsdboolean enRTPMulticast = xsdbooleanfalse; static enum xsdboolean enRTPUSCORETCP = xsdbooleantrue; static enum xsdboolean enRTP_USCORERTSPUSCORETCP = xsdbooleantrue; tRealTimeStreamingCapabilities.RTPMulticast = &enRTPMulticast; tRealTimeStreamingCapabilities.RTP_USCORETCP = &enRTP_USCORETCP; tRealTimeStreamingCapabilities.RTP_USCORERTSP_USCORETCP = &enRTP_USCORERTSP_USCORETCP; static struct ttMediaCapabilitiesExtension tMediaCapabilitiesExtension; soap_default_ttMediaCapabilitiesExtension(soap, &tMediaCapabilitiesExtension); tMedia.Extension = &tMediaCapabilitiesExtension; static struct ttProfileCapabilities tProfileCapabilities; soap_default_ttProfileCapabilities(soap, &tProfileCapabilities); tMediaCapabilitiesExtension.ProfileCapabilities = &tProfileCapabilities; tProfileCapabilities.MaximumNumberOfProfiles = 3; } tCapabilities.PTZ = NULL; tCapabilities.Extension = NULL; fprintf(stderr, "GetCapabilities ok....\n"); return SOAP_OK; } [/code] 5.2trtGetProfile函数 [code] int trtGetProfile(struct soap soap, struct _trt__GetProfile trt__GetProfile, struct _trtGetProfileResponse *trtGetProfileResponse) {
printf("%s:%d\n",FUNCTION,LINE); return GetProfile(soap, trtGetProfile->ProfileToken, trtGetProfileResponse); } int trtGetProfiles(struct soap soap, struct _trt__GetProfiles trtGetProfiles, struct _trtGetProfilesResponse *trtGetProfilesResponse) { printf("%s:%d\n",FUNCTION,LINE); return GetProfile(soap, NULL, trtGetProfilesResponse); } 5.3trtGetStreamUri函数 int trt__GetStreamUri(struct soap* soap, struct _trtGetStreamUri trt__GetStreamUri, struct _trt__GetStreamUriResponse trt__GetStreamUriResponse) {
printf("%s:%d\n",FUNCTION,LINE); if (trtGetStreamUri->StreamSetup) { if (trtGetStreamUri->StreamSetup->Stream == 1) { return soap_sender_fault_subcode(soap, "ter:InvalidArgVal/ter:InvalidStreamSetup", "Specification of StreamType or Transport part in StreamSetup is not supported.", NULL); } if (trtGetStreamUri->StreamSetup->Transport->Protocol== 3) { return soap_sender_fault_subcode(soap, "ter:InvalidArgVal/ter:InvalidStreamSetup", "The HTTP is not supported.", NULL); } } else { return soap_sender_fault_subcode(soap, "ter:InvalidArgVal/ter:GetStreamUri", "Invalid GetStreamUri.", NULL); } static struct ttMediaUri tMediaUri; static char Dev_Addr[64]; const char* v4_address = "192.168.1.101"; // 修改成你自己的流媒体服务器ip if (strcmp(trtGetStreamUri->ProfileToken, "media_profile1") == 0) { sprintf(Dev_Addr, "rtsp://%s/test.264", v4_address); } else if (strcmp(trt__GetStreamUri->ProfileToken, "media_profile2") == 0) { sprintf(Dev_Addr, "rtsp://%s/live1", v4_address); } else { return SOAP_ERR; } tMediaUri.Uri = Dev_Addr; tMediaUri.InvalidAfterConnect = xsdbooleanfalse; tMediaUri.InvalidAfterReboot = xsdbooleanfalse; static LONG64 SmTimeout; soap_s2xsdduration(soap,"PT100S",&SmTimeout); tMediaUri.Timeout = SmTimeout;//"PT100S"; trtGetStreamUriResponse->MediaUri = &tMediaUri; return SOAP_OK; } [/code] 5.4.其他函数trtGetVideoEncoderConfigurationOptions和trt__GetVideoSourceConfiguration 这2个函数也很重要,你也可以不实现,现实的话视频的信息无法获取,当然我这里是随便填的,详细参见工程文件,代码比较多
6.运行测试机 由于本次我们没有实现设备发现功能,并且我们的测试机器没有流媒体服务器,将live555.exe和test.264解码出来放在同一个目录 运行live555就得到了流媒体地址(这个地址就是trtGetStreamUri里面填的地址): [attach]2082[/attach] 我们需要手工的将ONVIF地址添加进来. [attach]2083[/attach]
点开我们的测试设备,就可以开到视频流了。 [attach]2084[/attach] [attach]2085[/attach]
7.总结 这样基于ONVIF的流媒体地址搜索就实现了,当然过程也耗费了一点时间调试,里面我基本全部使用了静态变量, 应该使用soap_malloc来动态申请,我没有完全搞明白soap的内存管理机制,所以使用保守的做法,至少不会出现内存泄露。 框架生成好再开发ONVIF协议的就是填充结构体,每个命令就要填一大推的结构体。所有的命令在下面可以看到 http://www.onvif.org/onvif/ver20/util/operationIndex.html 点开每一个都有详细的说明,只要按这个说明填好结构体,ONVIF协议的开发就没有什么难度。tds_ssom.c里面包含了一些基本信息网络等函数, 实现他们就可以在Device Manager的信息栏显示了: [attach]2086[/attach] 完整的工程下载:http://pan.baidu.com/s/1bnpQnJh附件包含测试视频,超过20M了丢个外链吧!
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片![alt](url)
-
2015-01-09 21:52:35
-
2017-10-26 10:15:31
-
2020-08-15 15:38:39
-
2017-09-05 11:49:45
-
2016-04-08 19:03:38
-
2017-11-01 10:48:13
-
2019-09-30 18:56:47
-
2023-11-26 11:27:41
-
2014-12-31 21:32:49
-
122024-01-04 16:24:59
-
2013-06-28 22:35:45
-
2020-10-02 12:53:38
-
2013-07-11 12:48:42
-
2019-01-22 11:49:16
-
2018-08-28 13:24:28
-
2015-12-18 15:45:27
-
2015-03-19 15:19:23
-
2016-01-07 10:53:12
-
2018-09-18 10:48:36
-
20帮忙交叉编译个源码
-
5Hi3516CV610 如何使用SD卡升级固件
-
5cat /dev/logmpp 报错 <3>[ vi] [func]:vi_send_frame_node [line]:99 [info]:vi pic queue is full!
-
50如何获取vpss chn的图像修改后发送至vo
-
5FPGA通过Bt1120传YUV422数据过来,vi接收不到数据——3516dv500
-
50SS928 运行PQtools 拼接 推到设备里有一半画面会异常
-
53536AV100的sample_vdec输出到CVBS显示
-
10海思板子mpp怎么在vi阶段改变视频数据尺寸
-
10HI3559AV100 多摄像头同步模式
-
9海思ss928单路摄像头vio中加入opencv处理并显示
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明