一个基于Gsoap 的ONVIF C++ 库

https://github.com/xsmart/onvifcpplib

这个库支持ProfileS 和ProfileG,目前还在开发当中,现在已经支持Event

下面是一个客户端的例子

int _tmain(int argc, _TCHAR* argv[])
{
	int ret;
	/* 192.168.1.1 is the NVT, 192.168.1.234 is the NVC */
	string url = "http://192.168.1.1/onvif/device_service";
	/* Below is where to receive the event */
	string eventNotify = "http://192.168.1.234:9090/subscription-2";
	string user = "admin";
	string pass =  "admin";
	OnvifClientDevice onvifDevice(url, user, pass);
	_tds__GetCapabilitiesResponse capabilitiesResponse;

	onvifDevice.GetCapabilities();

	OnvifClientMedia media(onvifDevice);
	_trt__GetProfilesResponse profiles;
	media.GetProfiles(profiles);

	OnvifClientEvent onvifEvent(onvifDevice);

	onvifEvent.Subscribe(eventNotify);
	
	OnvifClientEventNotify notify(soap_new());
	
    if ((ret = soap_bind(&notify, NULL, 9090, 100) == SOAP_INVALID_SOCKET ) ) 
	{
        printf("OnvifClientEventNotify::soap_bind Binding on %d port failed", 9090);
        return 0;
    }

	//Loop to recevie the event
	while(1)
	{
        if( (ret = soap_accept(&notify)) == SOAP_INVALID_SOCKET) {
            printf("soap_accept accepting failed");
            return 0;
        }

        if ( (soap_begin_serve(&notify)) != SOAP_OK) {
            printf("soap_begin_serve serve %d failed", ret);
            continue;
        }

        ret = notify.dispatch();
        continue;
	}
	return 0;
}

ONVIF协议是用于网络视频设备的标准化协议,它定义了一套通用接口,使得不同厂家的设备可以进行互操作。设备发现是ONVIF协议中非常重要的部分,它可以帮助用户快速地找到网络中的ONVIF设备并获取设备的基本信息。 以下是基于c++实现ONVIF设备发现工具的示例代码: ```c++ // onvif_device_discovery.cpp #include <iostream> #include <string> #include <vector> #include <wsdd.h> #include <stdio.h> #include <stdlib.h> #include <cstring> using namespace std; // 回调函数 void discovery_callback(struct soap *soap, const char *endpoint, const char *type, const char *version, const char *xaddrs, const char *metadata, void *arg) { vector<string> *discovered_devices = static_cast<vector<string> *>(arg); discovered_devices->push_back(endpoint); } int main(int argc, char **argv) { // 初始化soap环境 struct soap *soap = soap_new(); // 设置超时时间为5秒 soap->recv_timeout = 5; soap->send_timeout = 5; // 发送搜索消息 soap_wsdd_init_Ext(soap, NULL, NULL, NULL, 0, 0, 0); soap_wsdd_listen(soap, -1); soap_wsdd_search(soap, NULL, "dn:NetworkVideoTransmitter", NULL); // 等待搜索响应 int cnt = 0; while (cnt++ < 5) { soap_wsdd_poll(soap, 1000); } // 获取发现的设备地址 vector<string> discovered_devices; soap_wsdd_get_discovered_devices(soap, &discovered_devices); if (!discovered_devices.empty()) { cout << "Discovered devices:" << endl; for (auto &device_addr : discovered_devices) { cout << device_addr << endl; } } else { cout << "No device found." << endl; } // 释放soap环境 soap_destroy(soap); soap_end(soap); soap_free(soap); return 0; } ``` 该示例程序使用了ONVIF协议中的WS-Discovery模块进行设备发现。在程序执行时,先初始化soap环境,然后发送搜索消息,等待搜索响应,并获取发现的设备地址。最后,释放soap环境,程序结束。 需要注意的是,该示例程序需要使用到开源gSOAPlibwsdd,需要先安装这两个。可以通过以下命令安装: ```bash sudo apt-get install gsoap libwsdd ``` 安装完成后,使用以下命令编译程序: ```bash g++ onvif_device_discovery.cpp -o onvif_device_discovery -lgsoap++ -lwsdd ``` 编译完成后,即可执行程序进行设备发现: ```bash ./onvif_device_discovery ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值