Introduction
Once connected to an ONVIF device, you often need to retrieve its video/audio stream URI to consume the media (e.g., via RTSP player or decoder). This guide shows two methods to get the stream URI using the Happytimesoft ONVIF Client Library.
Method 1: Get the default stream URI (usually RTP over RTSP).
Method 2: Request a stream URI with a specific transport protocol (e.g., RTP over UDP or HTTP).
Method 1: Get Default Stream URI (RTP over RTSP)
Use the GetStreamUris function to retrieve the pre-configured stream URI, typically using RTP over RTSP.
// Call GetStreamUris to get the default RTP over RTSP stream address
if (!GetStreamUris(&g_device))
{
printf("%s, GetStreamUris failed\r\n", g_device.binfo.XAddr.host);
}
The resulting URI is typically in the format: rtsp://[IP]:[PORT]/[STREAM_PATH]
Method 2: Get Stream URI with Custom Protocol
Use the onvif_GetStreamUri function to request a stream using a specific transport protocol, such as RTP over UDP or HTTP tunneling.
Step 1: Define the Request
Specify the desired transport protocol and stream type (unicast/multicast).
ONVIF_Profile * p_profile = g_device.profiles;
while (p_profile)
{
GetStreamUri_REQ req;
GetStreamUri_RES res;
memset(&req, 0, sizeof(req));
memset(&res, 0, sizeof(res));
// Set transport protocol: UDP, RTSP (default), or HTTP
req.StreamSetup.Transport.Protocol = TransportProtocol_UDP;
// Options: TransportProtocol_UDP, TransportProtocol_RTSP, TransportProtocol_HTTP
// Set stream type: Unicast or Multicast
req.StreamSetup.Stream = StreamType_RTP_Unicast;
// Options: StreamType_RTP_Unicast, StreamType_RTP_Multicast
strcpy(req.ProfileToken, p_profile->token);
Step 2: Execute the Request
Call the API and store the returned URI.
if (onvif_GetStreamUri(&g_device, &req, &res))
{
strcpy(p_profile->stream_uri, res.Uri);
// URI is now stored in p_profile->stream_uri
}
p_profile = p_profile->next;
}
Protocol Comparison
- RTP over RTSP (Method 1): Standard method, uses RTSP for control and RTP for media. Most common for live viewing.
- RTP over UDP (Method 2): Lower overhead, direct UDP transmission. May be blocked by firewalls.
- RTP over HTTP (Method 2): HTTP tunneling, useful for traversing restrictive firewalls.
Key Notes
- The
ProfileTokenidentifies the video profile (e.g., main or sub stream). - Always check the return value of API calls for error handling.
- The resulting URI can be used in media players (VLC) or libraries (FFmpeg) to decode the stream.