Onvif¶
Onvif is an HTTP XML protocol between the camera and the client, allowing to discover RTSP URLs on the camera and configure the camera.
Alongside Onvif, there exists a WS-Discovery mechanism that allows finding a camera in the local network, but it is not an integral part of Onvif. Instead, other proprietary, often poorly described mechanisms can be used.
Onvif also extends to more complex VMS systems and video analytics, but in practice, these applications of Onvif are significantly rarer, less standardized, and used less frequently. And even less often implemented according to standards.
How to work with Onvif¶
To start working with Onvif, you need to know on which port the device has an Onvif server. Since most Onvif implementations are written using the gSOAP library (which is GPL), it's easier to run it as a separate daemon on a separate port rather than integrating it into the main web server. Therefore, it's common for a camera to show its web interface on port 80, while machine settings are accessed through some other port, like 8899.
In protocols for finding cameras like WD-Discovery, the port for Onvif is usually explicitly stated.
Onvif itself is a SOAP protocol and perhaps the only example of a SOAP protocol on the internet outside of the enterprise world.
Its WSDL (i.e., a rigidly structured protocol description) is available on the Onvif website along with textual descriptions.
In theory, the use of such a rigid protocol as SOAP should, if not guarantee order in the data, at least guarantee order in their formatting. In practice, everything is much worse because there are very few libraries for working with SOAP, the protocol is very complex, and in the end, it all comes down to the fact that live code on the camera simply contains templates into which data are inserted without validation, generating invalid XML.
How to play video via Onvif?¶
A very common question with two answers.
The first, formal answer: not possible. Onvif is a textual protocol for configuring the camera, and it can't play video.
The second, substantive answer: use the Onvif Profile S Specification to obtain URLs for playing video from the camera via RTSP.
Usually, when contrasting "playing via RTSP" and "playing via Onvif," it's implied that in the first case, there's no Onvif involved, and one needs to somehow find out the URLs the camera responds to. In the second case, the URL can be discovered through a standardized protocol.
How to configure a camera via Onvif?¶
Onvif offers extensive possibilities for camera configuration, but it's important to understand that with each camera model, you'll need to figure out on-site how the standard is interpreted.
For example, Onvif allows for the creation of multiple profiles (qualities, streams, tracks, etc.), their creation, deletion, and naming. Practically all cameras will allow you to change the bitrate of a profile, but only a few will let you delete profiles or rename them as you wish.
This means that in your code for configuring different cameras, you won't be able to delete all profiles and recreate them from scratch according to a single standard. Instead, you'll need to carefully describe for each type of camera exactly how to upload data to that specific model.
How to receive motion events via Onvif?¶
Onvif allows connecting to a camera and receiving motion detector events from it.
In theory, long polling should be supported, meaning if there's no motion at the moment of connection, the camera should wait up to 60 seconds for any motion to occur. This mechanism is very good in principle, but many cameras don't have it; they immediately return a no-motion response and ask to reconnect.
On the client side, this needs to be handled by reducing the frequency of requests.
Onvif and Politics¶
Onvif is a trademark of the corresponding organization, which is subject to American laws. In 2019, Hikvision and Dahua were expelled from this organization and were prohibited from claiming support for Onvif. Frankly, it's a bit unclear who then was supposed to remain in this organization, but they know best.
One can imagine the future of this standard, whose authors are trying to ban more than 80% of the global market, on their own.
Alternatives to Onvif¶
PSIA¶
Against the backdrop of the immense complexity of SOAP and the presence of only one available implementation in the form of gSOAP, the PSIA standard appeared and did not gain widespread adoption. It's a great idea that boils down to agreements on HTTP and RTSP URLs and significantly simpler exchanges. However, it didn't take off and is rarely used in cameras.
GBT28181¶
In China, GB/T 28181 is widespread. Its status isn't entirely clear, but an increase in the number of regional standards is likely a norm that we can expect in the future.
ISAPI¶
Analogous to PSIA but from Hikvision and actually used. ISAPI allows doing everything that Hikvision cameras can do and appears to be the most advanced. The problem with it is one: it's unclear whether Dahua and others will ever support it.