Overview
OnvifServer is a C++ library that provides an implementation of the ONVIF server. The library supports Linux only. The library is designed to be used in applications that require ONVIF server functionality, such as video surveillance systems, IP cameras, and other devices that support the ONVIF standard. OnvifServer library has dependencies: nlohmann JSON library (source code included in the library), zlib (linked, ZLIB license), and openssl (linked, Apache 2.0 license). The library repository include template for full support of Onvif Profile S. The library was tested for full support of Onvif Profile S. Profile S template requires additional dependencies: VStreamerMediaMtx (Provides RTSP video server. Source code included), CppHttpLib (Provides HTTP server required for ONVIF Profile S, v3.14.0 source code included, MIT license), and ChildProcess (Provides function tu run external processes). This dependencies provide full functional video management and streaming capabilities of Onvif Profile S template. The library manages ONVIF requests and responses by using a callback mechanism. The user defines a global callback and the library invokes this callback with request/response type parameter. Also, configuration of OnvifServer is done by JSON string. The OnvifServer library is supported on Linux only and requires C++17 standard.
Documentation
Documentation: GO TO DOCUMENTATION
Simple interface
class OnvifServer
{
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 "OnvifServer.h"
#include "OnvifGlobalHandler.h"
#include "DefaultConfigContent.h"
/// ONVIF callback function. This function handles ONVIF messages.
ONVIF_RET onvifCallback(ONVIF_DEVICE *p_device, ONVIF_MSG_TYPE msg_type, ...)
{
va_list args;
va_start(args, msg_type);
// Unless request is rejected explicitly, 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.
req->ImagingSettings.Brightness;
req->ImagingSettings.Contrast;
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::OnvifServer onvifServer;
// Init ONVIF server with the JSON configuration and callback function.
if (!onvifServer.init(g_defaultConfigContent, onvifCallback))
return -1;
// Start ONVIF server.
if (!onvifServer.start())
return -1;
// Wait key press to stop the server.
std::cout << "ONVIF server started. Press Enter to stop..." << std::endl;
std::cin.get();
onvifServer.stop();
return 0;
}