Qt writes security video surveillance system 34-onvif event subscription

I. Introduction

Event subscription is a recently added function, mainly because more and more application scenarios are encountered, which can receive alarm events from cameras. For example, almost all cameras will have an alarm input and output interface behind it. If the user connects an external alarm input, then When the alarm is triggered, the corresponding event will also be sent out through onvif, which is equivalent to being compatible with the alarm event reception of all onvif camera manufacturers. In some application systems, this function is also very common.

Event subscription process

  1. Send getEvent(CreatePullPointSubscription) to subscribe to the event service.
  2. After the subscription service is successful, the PullMessages subscription event is sent.
  3. If there is an event, the data will be replied immediately, and after the data is processed, the PullMessages subscription event will be resent.
  4. In this way, once there is an event, the data will be replied after the request, and the request is a long connection by default.
  5. There is a timeout when sending PullMessages. Once the timeout period is reached, PullMessages need to be resent.

Main functions of onvif

  1. Search for devices and obtain device information such as manufacturer, model, etc.
  2. Get multiple profile s of the device.
  3. Get the video stream address rtsp of the corresponding configuration file, and parameters such as resolution.
  4. PTZ control, move up and down, left and right, zoom in and out of focus, relative and absolute movement.
  5. Get the preset position information and trigger the preset position.
  6. Subscribe to events and receive various messages from the device, especially alarm events such as IO port alarms.
  7. Snapshot to get the current picture of the device.
  8. Get, create, delete user information.
  9. Obtain and device network configuration information such as IP address.
  10. Get and set NTP time synchronization.
  11. Get and set the device time.
  12. Reboot the device.

The processing flow of onvif

  1. Bind the multicast IP ( and port (3702), and send data in fixed xml format to search for the device.
  2. The received data in xml format is parsed to obtain the Onvif address of the device.
  3. Send the corresponding data to the Onvif address, and retrieve the corresponding node data after receiving the data.
  4. Request the Onvif address to obtain the Media address and Ptz address. The Media address is used to obtain detailed configuration files, and the Ptz address is used for PTZ control.
  5. The ptz control is to send the corresponding data to the Ptz address.
  6. If user authentication is set, it is necessary to organize the user token information to be sent together, and authentication processing is required each time.
  7. The received data is not standard xml data and cannot be processed according to normal node parsing. It can only be done with QXmlQuery.
  8. The data returned by each manufacturer's equipment may not be exactly the same, basically inconsistent, and it is necessary to perform a fuzzy search for node values.
  9. The underlying protocol parsing is deliberately used, because soap is too bloated and the function name is too alternative, and it is specially made to be lightweight.
  10. Two must-have tools, Onvif Device Manager and Onvif Device Test Tool.

2. Features

  1. Support multi-screen switching, full-screen switching, etc., including 1+4+6+8+9+13+16+25+36+64 screen switching.
  2. Support alt+enter full screen, esc exit full screen.
  3. Custom information box + error box + inquiry box + prompt box in the lower right corner (including multiple formats).
  4. 17 sets of skin styles can be replaced at will, and all styles are unified, including menus, etc.
  5. Move the mouse on the gimbal dashboard to highlight, and the eight directions can be accurately identified.
  6. The bottom screen toolbar (screen split switching + screenshot sound and other settings) is moved up to highlight.
  7. You can change the logo in the upper left corner + Chinese software name + English software name in the configuration file.
  8. It encapsulates Baidu map, view switching, motion track, device point, mouse press to obtain latitude and longitude, etc.
  9. Support picture map, the device button can be freely dragged on the picture map to automatically save the location information.
  10. On Baidu Maps and Picture Maps, double-click the video to preview the live video of the camera.
  11. Stack form, each form is a separate qwidget, easy to write your own code.
  12. The right mouse button menu at the top can dynamically control the display and hide of the time CPU + upper left panel + lower left panel + upper right panel + lower right panel, and supports restoring the default layout.
  13. The toolbar can place multiple small icons and close icons.
  14. The left and right sides can be dragged and stretched, and the width and height positions are automatically memorized and restored after restarting.
  15. Double-click the camera node to automatically play the video, double-click the node to automatically add videos in sequence, and automatically jump to the next one, and double-click the parent node to automatically add all the videos under the node.
  16. Drag the camera node to the corresponding window to play the video, and support dragging local files to play directly.
  17. The video screen form supports dragging and swapping, and responds instantly.
  18. Double-click the node + drag the node + drag the form to exchange the position, and the url.txt is automatically updated.
  19. Support loading channel video playback from url.txt, automatically memorize the video corresponding to the last channel, and automatically open and play after the software is started.
  20. The volume bar control in the lower right corner is automatically hidden when the focus is lost, and the volume bar has a mute icon.
  21. Integrate Baidu online map and offline map, you can add the corresponding location of the device, automatically generate the map, support zooming and adding overlays, etc.
  22. Drag the video outside the channel form to automatically delete the video.
  23. The right mouse button can delete the current + all videos, screenshot the current + all videos.
  24. Recorder management, camera management, can add, delete, modify, import, export and print information, and immediately apply new device information to generate a tree list without restarting.
  25. In the pro file, you can freely turn on whether to load the map.
  26. Video playback can choose 2 kinds of kernels to switch freely, vlc+ffmpeg, which can be set in pro.
  27. 1+4+9+16 screen polling can be set, polling interval and polling stream type can be set, etc., directly click the start polling button on the right side of the toolbar at the bottom of the main interface, and click again to stop polling .
  28. By default, the mouse pointer is automatically hidden if there is no operation for more than 10 seconds.
  29. Support onvif search equipment, support any onvif camera, including but not limited to Hikvision Dahua Yushi Tiandi Weiye Huawei, etc.
  30. Support onvif PTZ control, can move the PTZ camera up, down, left and right, including reset and focus adjustment.
  31. At the same time support sqlite, mysql, postsql and other databases.
  32. Video can be saved, optional timing storage or single file storage, optional storage interval.
  33. You can set the video stream communication mode tcp+udp, and you can set the video decoding to be speed priority, quality priority, equalization, etc.
  34. The hard decoding type can be set, supporting qsv, dxva2, d3d11va, etc.
  35. By default, opengl is used to draw video, which has ultra-low CPU resource consumption and supports both yuyv and nv12 formats for drawing, which is very powerful.
  36. Highly customizable, users can easily derive their own functions on this basis, supporting linux and mac systems.

3. Effect picture

Fourth, the core code

QString OnvifDevice::getEvent(const QString &timeout)
    QString file = OnvifHelper::getFile(":/send/getEvent.xml");
    file = file.arg(request->getUserToken()).arg(timeout);
    QByteArray dataSend = file.toUtf8();
    QNetworkReply *reply = request->post(deviceUrl, dataSend);
    emit sendData(dataSend, deviceUrl);

    QByteArray dataReceive;
    bool ok = checkData(reply, dataReceive, "Subscribe to the event service");
    if (ok) {
        OnvifQuery query;
        eventUrl = query.getEventUrl();

        //Start a timer to request event content
        if (!timerPullMessage->isActive()) {
            QTimer::singleShot(100, this, SLOT(pullMessage()));

    return eventUrl;

QString OnvifDevice::pullMessage(const QString &timeout)
    QString message;
    if (eventUrl.isEmpty()) {
        return message;

    emit receiveInfo("Request event content");

    //There are two types of messages, sent one by one
    QString file;
    if (messageIndex == 0) {
        //messageIndex = 1;
        file = OnvifHelper::getFile(":/send/pullMessage.xml");
    } else if (messageIndex == 1) {
        messageIndex = 0;
        file = OnvifHelper::getFile(":/send/pullMessage2.xml");

    QString uuid = OnvifHelper::getUuid();
    file = file.arg(request->getUserToken()).arg(uuid).arg(eventUrl).arg(timeout);
    QByteArray dataSend = file.toUtf8();
    QNetworkReply *reply = request->post(eventUrl, dataSend);
    emit sendData(dataSend, eventUrl);

    QByteArray dataReceive;
    bool ok = checkData(reply, dataReceive, "Request event content");
    if (ok) {
        OnvifQuery query;
        message = query.getEventMessage();
        if (!message.isEmpty()) {
            emit receiveInfo(message);
            //Received the event data and responded to the continuation request, because it is possible that the event data will be required until it is finished.

    return message;

Posted by Iank1968 on Tue, 31 May 2022 14:11:11 +0530