Set Up ONVIF Event Subscription

Learn how to subscribe to real-time events from an ONVIF device (e.g., motion detection, PTZ) using the Happytimesoft Client Library in C.

Introduction

ONVIF Event Subscription allows your application to receive real-time notifications from a camera or device when specific events occur, such as motion detection, door opening, or PTZ movement.

This guide walks you through setting up the subscription and handling incoming events using callback functions.

Step 1: Initialize Event System

Before subscribing, initialize the event handling system. This sets up an internal HTTP server to receive event notifications from the device.

C Code: Initialize Event System
// Init event handler
// Bind the internal HTTP server to 0.0.0.0:30100
onvif_event_init(0, 30100, MAX_DEV_NUMS); 

// Set event notification callback
onvif_set_event_notify_cb(eventNotifyCallback, 0); 

// Set subscription disconnect callback
onvif_set_subscribe_disconnect_cb(subscribeDisconnectCallback, 0);

Note: The HTTP server (port 30100) listens for callbacks from the ONVIF device. Ensure this port is accessible.

Step 2: Configure and Connect to Device

Set up the ONVIF device connection with IP, port, credentials, etc. (Same as other tutorials).

C Code: Device Setup
// Define an ONVIF_DEVICE variable
ONVIF_DEVICE g_device; 

// Open log file
log_init("log.txt"); 
log_set_level(LOG_DBG);  

// Init system and HTTP buffers
sys_buf_init(10 * MAX_DEV_NUMS);  
http_msg_buf_init(10 * MAX_DEV_NUMS);  

// Initialize the device structure
memset(&g_device, 0, sizeof(g_device)); 

// Set device address
g_device.binfo.XAddr.https = 0; 
g_device.binfo.XAddr.port = 8000; 
strcpy(g_device.binfo.XAddr.host, "192.168.1.3"); 
strcpy(g_device.binfo.XAddr.url, "/onvif/device_service"); 

// Set login credentials
onvif_SetAuthInfo(&g_device, "admin", "admin");
onvif_SetAuthMethod(&g_device, AuthMethod_UsernameToken);
onvif_SetReqTimeout(&g_device, 5000); 

// Get device capabilities
if (!GetCapabilities(&g_device))
{   
    printf("%s, GetCapabilities failed\r\n", g_device.binfo.XAddr.host);
}

Step 3: Subscribe to Events

After connecting, subscribe to the device's event service if supported.

C Code: Subscribe to Events
// Check if device supports ONVIF events
if (g_device.Capabilities.events.support == 1)
{   
    if (Subscribe(&g_device, getDeviceIndex(&g_device)))   
    {       
        printf("Subscribe successful!\r\n");   
    }   
    else   
    {       
        printf("Subscribe failed!\r\n");   
    }
}

Handling Events: Callback Functions

When an event occurs, the device sends a notification to your internal HTTP server, which triggers the callback functions you defined.

Event Notification Callback

Handles incoming event data (e.g., motion detection).

C Code: Event Notification Callback
void eventNotifyCallback(Notify_REQ * p_req, void * p_data)
{
    // Process the event notification
    // p_req contains the event data (e.g., topic, message)
    // Implement your event handling logic here
    printf("Event received: %s\r\n", p_req->Event.Topic); 
}

Subscription Disconnect Callback

Handles subscription loss (e.g., network issue).

C Code: Disconnect Callback
void subscribeDisconnectCallback(ONVIF_DEVICE * p_dev, void * p_data)
{
    // Handle subscription disconnection
    // e.g., attempt to re-subscribe
    printf("Subscription lost for device: %s\r\n", p_dev->binfo.XAddr.host);
}

Key Concepts

  • Pull Point Subscription: Your application pulls events from the device's event service.
  • Callback Mechanism: The device pushes notifications to your HTTP server, which invokes your callback functions.
  • Common Events: Motion detection, PTZ control, audio level, door sensors.
  • Port 30100: Must be open and accessible for the device to send notifications.