一、设备搜索原理
Onvif协议要求实现Onvif协议服务的设备支持设备发现以及探测,也就是discovery和probe;discovery,客户端向网段内的组播地址为239.255.255.250,端口为3702发送广播消息,等待网内Onvif协议设备响应响应消息的设备会返回自己的IP、UUID、EP Address、Type等,最重要的会返回设备的Device Service Address,格如:http://192.168.100.123:8099/onvif/device_service ,此地址就是为设备提供Onvif服务的服务地址;
二、设备搜索实现
客户端首先发起ws-discovery,查找所在网络段内的所有的ipcam。Ipcam在接收到ws-discovery之后可以进行响应;
1、创建组播用的udp socket;
2、绑定组播地址为239.255.255.250,端口为3702,因为ws-discovery的组播地址和端口就是为239.255.255.250和3702;
3、向socket发送(send)搜索数据,下面探测消息(Probe)的内容,是ONVIF Device Test Tool工具搜索IPC时通过Wireshark抓包工具抓包到的:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:dn="http://www.onvif.org/ver10/network/wsdl"
xmlns="http://www.w3.org/2003/05/soap-envelope">
<Header>
<wsa:MessageID xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
uuid:25862dc4-4866-42a9-87a7-edcf2918ab10
</wsa:MessageID>
<wsa:To xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
urn:schemas-xmlsoap-org:ws:2005:04:discovery
</wsa:To>
<wsa:Action xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe
</wsa:Action>
</Header>
<Body>
<Probe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/ws/2005/04/discovery">
<Types>dn:NetworkVideoTransmitter</Types>
<Scopes />
</Probe>
</Body>
</Envelope>
4、接受(recv)数据
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2000/10/XMLSchema"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-
secext-1.0.xsd"
xmlns:wsa5="http://www.w3.org/2005/08/addressing"
xmlns:xop="http://www.w3.org/2004/08/xop/include"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:tt="http://www.onvif.org/ver10/schema" xmlns:ns1="http://www.w3.org/2005/05/xmlmime"
xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
xmlns:ns7="http://docs.oasis-open.org/wsrf/r-2"
xmlns:ns2="http://docs.oasis-open.org/wsrf/bf-2"
xmlns:dndl="http://www.onvif.org/ver10/network/wsdl/DiscoveryLookupBinding"
xmlns:dnrd="http://www.onvif.org/ver10/network/wsdl/RemoteDiscoveryBinding"
xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery"
xmlns:dn="http://www.onvif.org/ver10/network/wsdl"
xmlns:ns10="http://www.onvif.org/ver10/replay/wsdl"
xmlns:ns11="http://www.onvif.org/ver10/search/wsdl"
xmlns:ns13="http://www.onvif.org/ver20/analytics/wsdl/RuleEngineBinding"
xmlns:ns14="http://www.onvif.org/ver20/analytics/wsdl/AnalyticsEngineBinding"
xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl"
xmlns:ns15="http://www.onvif.org/ver10/events/wsdl/PullPointSubscriptionBinding"
xmlns:ns16="http://www.onvif.org/ver10/events/wsdl/EventBinding"
xmlns:tev="http://www.onvif.org/ver10/events/wsdl"
xmlns:ns17="http://www.onvif.org/ver10/events/wsdl/SubscriptionManagerBinding"
xmlns:ns18="http://www.onvif.org/ver10/events/wsdl/NotificationProducerBinding"
xmlns:ns19="http://www.onvif.org/ver10/events/wsdl/NotificationConsumerBinding"
xmlns:ns20="http://www.onvif.org/ver10/events/wsdl/PullPointBinding"
xmlns:ns21="http://www.onvif.org/ver10/events/wsdl/CreatePullPointBinding"
xmlns:ns22="http://www.onvif.org/ver10/events/wsdl/PausableSubscriptionManagerBind
ing"
xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
xmlns:ns3="http://www.onvif.org/ver10/analyticsdevice/wsdl"
xmlns:ns4="http://www.onvif.org/ver10/deviceIO/wsdl"
xmlns:ns5="http://www.onvif.org/ver10/display/wsdl"
xmlns:ns8="http://www.onvif.org/ver10/receiver/wsdl"
xmlns:ns9="http://www.onvif.org/ver10/recording/wsdl"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl"
xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl"
xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl"
xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
xmlns:trt2="http://www.onvif.org/ver20/media/wsdl"
xmlns:ter="http://www.onvif.org/ver10/error" xmlns:tns1="http://www.onvif.org/ver10/topics"
xmlns:tnsn="http://www.eventextension.com/2011/event/topics">
<SOAP-ENV:Header>
<wsa:MessageID>urn:uuid:327b23c6-5566-7788-99ac-001217d7bf0f
</wsa:MessageID>
<wsa:RelatesTo>uuid:25862dc4-4866-42a9-87a7-edcf2918ab10
</wsa:RelatesTo>
<wsa:To SOAP-ENV:mustUnderstand="true">
http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
</wsa:To>
<wsa:Action SOAP-ENV:mustUnderstand="true">
http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches
</wsa:Action>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<d:ProbeMatches>
<d:ProbeMatch>
<wsa:EndpointReference><wsa:Address>
urn:uuid:b12dca78-5566-7788-99aa-001217d7bf0f</wsa:Address>
</wsa:EndpointReference>
<d:Types>dn:NetworkVideoTransmitter
</d:Types>
<d:Scopes>onvif://www.onvif.org/type/video_encoder onvif://www.onvif.org/type/audio_encoder
onvif://www.onvif.org/hardware/IPC-model onvif://www.onvif.org/location/country/china
onvif://www.onvif.org/name/NVT onvif://www.onvif.org/Profile/Streaming
</d:Scopes>
<d:XAddrs>
http://192.168.100.123:8099/onvif/device_service
</d:XAddrs>
<d:MetadataVersion>10</d:MetadataVersion>
</d:ProbeMatch>
</d:ProbeMatches>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
5、解析上面接受到的数据,得到IPC摄像头的「设备服务地址」,即XAddrs字段,
<d:XAddrs>http://192.168.100.123:8099/onvif/device_service</d:XAddrs>,后续调用其他ONVIF接口,都需要用到「设备服务地址」 http://192.168.100.123:8099/onvif/device_service。