Disclaimer
ONVIF® is a registered trademark of ONVIF, Inc. The ONVIF name is used here only in a descriptive manner to indicate that this library is intended to implement aspects of the ONVIF® specifications for interoperability. This project is not affiliated with, endorsed by, or sponsored by ONVIF, Inc. or its members. No ONVIF® conformance or certification is claimed. Any conformance claim can only be made by a finished product that has successfully completed ONVIF’s official conformance process and received a valid Declaration of Conformance (DoC) from ONVIF. No claim is made to the exclusive right to use “ONVIF” apart from the technical reference shown in this library’s documentation and code.
Overview
OServer is a C++ library that provides a server implementation based on the ONVIF® specifications. The library supports Linux only. It is designed for applications that require server functionality implementing the ONVIF® specifications, such as video surveillance systems, IP cameras, and other devices. The OServer library has the following dependencies: nlohmann JSON library (source code included), zlib (linked, ZLIB license), and openssl (linked, Apache 2.0 license). The repository also includes a template intended to help implement Profile S features. The Profile S template requires additional dependencies: VStreamerMediaMtx (provides an RTSP video server, source code included), CppHttpLib (provides an HTTP server required for ONVIF® Profile S, v3.14.0 source code included, MIT license), and ChildProcess (provides functions to run external processes). These dependencies provide video management and streaming capabilities used by the Profile S template. The library manages ONVIF® requests and responses using a callback mechanism. The user defines a global callback, and the library invokes this callback with request/response type parameters. Configuration of OServer is done via a JSON string. The OServer library requires the C++17 standard. Note: Claiming ONVIF® conformance for a product requires completion of ONVIF’s official conformance process and a valid DoC.
Documentation
Documentation: GO TO DOCUMENTATION
Simple interface
class OServer
{
public:
/// Type definition for the ONVIF® message handler callback function.
using onvif_handler_t = ONVIF_RET (*)(ONVIF_DEVICE *, ONVIF_MSG_TYPE, ...);
/// Get string of current library version.
static std::string getVersion();
/// Initialize the ONVIF® server with the JSON configuration string
/// and a global handler callback.
bool init(const std::string& jsonConfig, onvif_handler_t callback);
/// Initialize the ONVIF® server with the function pointers to initialize
/// the supported analytics rules and modules. This method is OPTIONAL.
void initAnalytics(void (*rulesInit)(onvif_SupportedRules *),
void (*modulesInit)(onvif_SupportedAnalyticsModules *));
/// Start the ONVIF® server.
bool start();
/// Stop the ONVIF® server.
bool stop();
/// Set the streaming capabilities of server device that onvif server will use.
void setStreamingCapabilities(bool rtpMulticast, bool rtpRtspTcp);
};
Simple example
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include "OnvifImage.h"
#include "OServer.h"
#include "DefaultConfigContent.h"
/// ONVIF® callback function. This function handles ONVIF® messages.
ONVIF_RET oCallback(ONVIF_DEVICE *p_device, ONVIF_MSG_TYPE msg_type, ...)
{
va_list args;
va_start(args, msg_type);
// Unless the request is explicitly rejected, it's better to return ONVIF_OK
// for many requests for test tool compatibility.
ONVIF_RET ret = ONVIF_OK;
// Check the message type and handle it accordingly.
switch (msg_type)
{
// Handle SetImagingSettings request.
case SetImagingSettings:
{
auto vsl = va_arg(args, VideoSourceList *);
auto req = va_arg(args, img_SetImagingSettings_REQ *);
// Get token. Camera token is defined in config file.
std::string cameraName(vsl->VideoSource.token);
// Some logic depends on camera name if we have multiple cameras.
// Apply these settings to your camera.
// Example: yourCamera.setBrightness(req->ImagingSettings.Brightness);
// Example: yourCamera.setContrast(req->ImagingSettings.Contrast);
// Example: yourCamera.setColorSaturation(req->ImagingSettings.ColorSaturation);
break;
}
// Handle GetImagingSettings request.
case GetImagingSettings:
{
auto vsl = va_arg(args, VideoSourceList *);
auto req = va_arg(args, img_GetImagingSettings_REQ *);
// Get token. Camera token is defined in config file.
std::string cameraName(vsl->VideoSource.token);
// Some logic depends on camera name if we have multiple cameras.
// Fill image settings.
auto imageSettings = &vsl->ImagingSettings;
imageSettings->Brightness = 25.0f; // Example value.
imageSettings->Contrast = 50.0f; // Example value.
imageSettings->ColorSaturation = 75.0f; // Example value.
break;
}
// ... Other cases for handling different ONVIF® messages ...
}
va_end(args);
// Return the result of the operation.
return ret;
}
/// Entry point.
int main(int argc, char **argv)
{
// Create ONVIF® server object.
cr::onvif::OServer oServer;
// Initialize ONVIF® server with the JSON configuration and callback function.
if (!oServer.init(g_defaultConfigContent, oCallback))
return -1;
// Start ONVIF® server.
if (!oServer.start())
return -1;
// Wait key press to stop the server.
std::cout << "ONVIF® server started. Press Enter to stop..." << std::endl;
std::cin.get();
oServer.stop();
return 0;
}