goodman

goodman

4个粉丝

30

问答

18

专栏

17

资料

goodman  发布于  2015-01-05 21:26:10
采纳率 0%
30个问答
wooshang@126.com
32189

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了丢个外链吧!

我来回答
回答51个
时间排序
认可量排序

_nucong

2个粉丝

24

问答

0

专栏

4

资料

_nucong 2015-01-05 22:53:21
认可0
写的东西非常有价值,先收藏!

beegeen

0个粉丝

42

问答

0

专栏

1

资料

beegeen 2015-01-07 11:56:22
认可0
学习。感谢LZ分享。

hhzh126

0个粉丝

0

问答

0

专栏

0

资料

hhzh126 2015-03-26 16:06:28
认可0
先收藏,谢谢分享。

ipook168

0个粉丝

3

问答

0

专栏

0

资料

ipook168 2015-03-31 21:37:44
认可0
我们公司把onvif项目外包,花了几十K的键子,已经搞得差不多了。

xy309428529

0个粉丝

0

问答

0

专栏

0

资料

xy309428529 2015-04-10 21:07:46
认可0
目前正接触海思项目,正需要。

goodplayer

0个粉丝

1

问答

0

专栏

0

资料

goodplayer 2015-06-15 13:30:28
认可0
问下楼主,设备端(服务端)加入鉴权怎么加啊?有没有相关资料啊?

iamheimawangzi

0个粉丝

0

问答

0

专栏

0

资料

iamheimawangzi 2015-09-16 18:40:31
认可0
厉害   真厉害   非常有价值  谢谢高手。

sxsong

0个粉丝

12

问答

0

专栏

1

资料

sxsong 2015-09-28 10:23:53
认可0
好东西,楼楼大爱啊

liujiaqi13

0个粉丝

2

问答

0

专栏

0

资料

liujiaqi13 2015-11-23 14:06:06
认可0
弱弱的问一句,onvif是怎么适应丢包率20%以上的呢?   

admin

0个粉丝

6

问答

473

专栏

10

资料

admin 2016-06-30 13:42:08
认可0
66666666666666666

rafael_wl

1个粉丝

12

问答

0

专栏

7

资料

rafael_wl 2016-07-07 19:34:22
认可0
[quote][url=forum.php?mod=redirect&goto=findpost&pid=32882&ptid=4965]admin 发表于 2016-6-30 13:42[/url]
66666666666666666[/quote]

你的账号是admin 这个。。。。。

VVVV88

0个粉丝

0

问答

0

专栏

0

资料

VVVV88 2016-08-31 14:48:34
认可0
先收藏,谢谢分享。

zltkf

0个粉丝

1

问答

0

专栏

0

资料

zltkf 2017-02-04 18:20:42
认可0
感谢楼主分享。很有帮助。

lihu

0个粉丝

0

问答

0

专栏

0

资料

lihu 2017-02-05 10:34:14
认可0
感谢楼主分享

toread

0个粉丝

25

问答

0

专栏

0

资料

toread 2017-04-14 16:56:29
认可0
完整的工程下载:[url]http://pan.baidu.com/s/1bnpQnJh[/url] 附件包含测试视频,超过20M了丢个外链吧!
亲,这个外链已经无效了,可以重新发一个吗?谢谢

lyy111

0个粉丝

27

问答

0

专栏

1

资料

lyy111 2017-05-15 18:18:34
认可0
很不错,参考一下。

hongquan

0个粉丝

0

问答

0

专栏

0

资料

hongquan 2017-05-15 20:01:45
认可0
很有价值的东西

qq328848298

0个粉丝

1

问答

0

专栏

0

资料

qq328848298 2017-06-07 23:22:26
认可0
亲,这个外链已经无效了,可以重新发一个吗?谢谢

hero

0个粉丝

1

问答

0

专栏

0

资料

hero 2017-07-04 15:10:37
认可0

写的东西非常有价值,先收藏!

sbrr123

0个粉丝

0

问答

0

专栏

0

资料

sbrr123 2017-07-19 16:45:08
认可0
onvif 可以直接用 IE直接访问吗?
加载中···
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区