linux c 读取摄像头,Linux下onvif客户端获取ipc摄像头 获取能力:GetCapabilities
getcapabilities:獲取能力,主要目的獲取設(shè)備能力信息(獲取媒體服務(wù)地址)
鑒權(quán):但是在調(diào)用獲取設(shè)備能力之前是需要鑒權(quán)的。onvif協(xié)議規(guī)定,部分接口需要鑒權(quán),部分接口不需要鑒權(quán),在調(diào)用需要鑒權(quán)的接口時不使用鑒權(quán),會導(dǎo)致接口調(diào)用失敗。實現(xiàn)鑒權(quán)的方式之一可以調(diào)用gsoap源碼中的?soap_wsse_add_usernametokendigest()函數(shù)。要安裝依賴庫openssl
實現(xiàn)代碼:
1 #include
2 #include
3 #include
4 #include
5
6 #include "soaph.h"
7 #include "stdsoap2.h"
8 #include "soapstub.h"
9 #include "wsseapi.h"
10
11 #include "wsdd.nsmap" //命名空間
12
13 static struct soap* onvif_initsoap(struct soap_env__header *header, const char *was_to, const char *was_action, int timeout)
14 {
15 struct soap *soap = null; // soap環(huán)境變量
16 unsigned char macaddr[6];
17 char _hwid[1024];
18 unsigned int flagrand;
19
20 soap = soap_new();
21 if(soap == null)
22 {
23 printf("[%d]soap = null\n", __line__);
24 return null;
25 }
26
27 soap_set_namespaces(soap, namespaces); // 設(shè)置soap的namespaces,即設(shè)置命名空間
28
29 // 設(shè)置超時(超過指定時間沒有數(shù)據(jù)就退出)
30 if(timeout > 0)
31 {
32 soap->recv_timeout = timeout;
33 soap->send_timeout = timeout;
34 soap->connect_timeout = timeout;
35 }
36 else
37 {
38 //maximum waittime : 20s
39 soap->recv_timeout = 20;
40 soap->send_timeout = 20;
41 soap->connect_timeout = 20;
42 }
43
44 soap_default_soap_env__header(soap, header);
45
46 //create sessionid randomly,生成uuid(windows下叫g(shù)uid,linux下叫uuid),格式為urn:uuid:8-4-4-4-12,由系統(tǒng)隨機產(chǎn)生
47 srand((int)time(0));
48 flagrand = rand()%9000 + 8888;
49 macaddr[0] = 0x1;
50 macaddr[1] = 0x2;
51 macaddr[2] = 0x3;
52 macaddr[3] = 0x4;
53 macaddr[4] = 0x5;
54 macaddr[5] = 0x6;
55 sprintf(_hwid, "urn:uuid:%ud68a-1dd2-11b2-a105-%02x%02x%02x%02x%02x%02x", flagrand, macaddr[0], macaddr[1], macaddr[2],macaddr[3],macaddr[4],macaddr[5]);
56 header->wsa__messageid = (char *)malloc(100);
57 memset(header->wsa__messageid, 0, 100);
58 strncpy(header->wsa__messageid, _hwid, strlen(_hwid)); //wsa__messageid存放的是uuid
59
60 if(was_action != null)
61 {
62 header->wsa__action = (char*)malloc(1024);
63 memset(header->wsa__action, '\0', 1024);
64 strncpy(header->wsa__action, was_action, 1024); //
65 }
66 if(was_to != null)
67 {
68 header->wsa__to = (char *)malloc(1024);
69 memset(header->wsa__to, '\0', 1024);
70 strncpy(header->wsa__to, was_to, 1024);//"urn:schemas-xmlsoap-org:ws:2005:04:discovery";
71 }
72 soap->header = header;
73 return soap;
74 }
75
76
77 //釋放函數(shù)
78 void onvif_soap_delete(struct soap *soap)
79 {
80 soap_destroy(soap); // remove deserialized class instances (c++ only)
81 soap_end(soap); // clean up deserialized data (except class instances) and temporary data
82 soap_free(soap); // reset and deallocate the context created with soap_new or soap_copy
83 }
84
85
86 //鑒權(quán)
87 static int onvif_setauthinfo(struct soap *soap, const char *username, const char *password)
88 {
89 int result = 0;
90 if((null != username) || (null != password)){
91 soap_wsse_add_usernametokendigest(soap, null, username, password);
92 }else{
93 printf("un etauth\n");
94 result = -1;
95 }
96
97 return result;
98 }
99
100
101
102 int main(int argc,char *argv[])
103 {
104 int ret = 0;
105 char sercer_addr[] = "http://172.168.0.211/onvif/device_service"; //設(shè)備搜索得到的地址
106
107 struct soap_env__header header;
108 struct soap* soap = onvif_initsoap(&header, null, null, 5);
109
110 struct _tds__getcapabilities *req;
111 struct _tds__getcapabilitiesresponse *response;
112
113 if(null == (req = (struct _tds__getcapabilities *)calloc(1,sizeof(struct _tds__getcapabilities))))
114 {
115 printf("calloc is error \n");
116 ret = -1;
117 return ret;
118 }else{
119 req->__sizecategory = 1;
120 req->category = (enum tt__capabilitycategory *)soap_malloc(soap, sizeof(int));
121 *(req->category) = (enum tt__capabilitycategory)5; //5表示:tt__capabilitycategory__media
122
123 onvif_setauthinfo(soap,"admin","hk123456"); //鑒權(quán),輸入攝像頭的用戶名、密碼
124 ret = soap_call___tds__getcapabilities(soap, sercer_addr, null,req, response);
125 if(soap->error){
126 ret = -1;
127 printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
128 return ret;
129 }else{;
130 if(null != response->capabilities)
131 {
132 if (response->capabilities->media != null){
133 if (response->capabilities->media->xaddr != null){
134 printf(" media_addr: %s \n", response->capabilities->media->xaddr);
135 }
136 }
137 }
138 }
139 }
140
141 if(null != req)
142 {
143 free(req);
144 req = null;
145 }
146
147 onvif_soap_delete(soap);
148 return ret;
149 }
在編譯之前要把:stdsoap2.c ?soapc.c ?md5.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapclient.c? ?把這些.c 還有有些配套的.h? 拷貝到當(dāng)前工作目錄下。
如果出現(xiàn)以下錯誤要在編譯的時候加:-dwith_openssl
/tmp/cccao7u5.o:在函數(shù)‘soap_mec_init’中:
mecevp.c:(.text+0xe):對‘soap_ssl_init’未定義的引用
/tmp/ccv2yl3q.o:在函數(shù)‘soap_smd_init’中:
smdevp.c:(.text+0x24c):對‘soap_ssl_init’未定義的引用
編譯:gcc -o test get_uri_test.c stdsoap2.c soapc.c md5.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapclient.c -i import/ -dwith_openssl -lssl -lcrypto -ldl -pthread
結(jié)果:成功得到我們想要的媒體地址
總結(jié)
以上是生活随笔為你收集整理的linux c 读取摄像头,Linux下onvif客户端获取ipc摄像头 获取能力:GetCapabilities的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python实现模糊综合评价法(FCE)
- 下一篇: linux 其他常用命令