2026-08-11 20:47:27 +02:00
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Discovery;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shared camera stream URL templates used by BOTH the Central (to build
|
|
|
|
|
/// candidate URLs for a discovery result) and the Agent (to build + test the
|
|
|
|
|
/// URLs on the camera LAN, since only the agent can reach the cameras).
|
|
|
|
|
/// Placeholders: {IP}, {PORT}, {CHANNEL}, {USERNAME}, {PASSWORD}.
|
2026-08-11 22:04:10 +02:00
|
|
|
/// Each template declares the protocol default port; <see cref="BuildUrls"/>
|
|
|
|
|
/// applies a per-protocol port filter so URLs only carry plausible ports
|
|
|
|
|
/// (open ports, or the protocol default as a fallback).
|
2026-08-11 20:47:27 +02:00
|
|
|
/// </summary>
|
|
|
|
|
public static class CameraStreamTemplates
|
|
|
|
|
{
|
|
|
|
|
/// <summary>URL templates for stream building. {IP}, {PORT}, {CHANNEL}, {USERNAME}, {PASSWORD}.</summary>
|
|
|
|
|
public static readonly Dictionary<string, string[]> StreamTemplates = new()
|
|
|
|
|
{
|
|
|
|
|
["p:onvif"] = ["rtsp://{IP}:{PORT}/onvif/media?profile=1"],
|
|
|
|
|
["p:rtsp-h264"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/stream1",
|
|
|
|
|
"rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/Streaming/Channels/{CHANNEL}01"],
|
|
|
|
|
["p:rtsp-h265"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/stream2",
|
|
|
|
|
"rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/Streaming/Channels/{CHANNEL}02"],
|
|
|
|
|
["p:mjpeg"] = ["http://{IP}:{PORT}/mjpeg", "http://{IP}:{PORT}/video.mjpg"],
|
2026-08-11 22:04:10 +02:00
|
|
|
["p:dvrrip"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/cam/realmonitor?channel={CHANNEL}&subtype=0",
|
|
|
|
|
"dvrip://{USERNAME}:{PASSWORD}@{IP}:{PORT}?channel={CHANNEL}&subtype=0"],
|
|
|
|
|
["p:rtmp"] = ["rtmp://{IP}:{PORT}/live/{CHANNEL}"],
|
|
|
|
|
["b:hikvision"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/Streaming/Channels/{CHANNEL}01",
|
|
|
|
|
"rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/Streaming/Channels/{CHANNEL}02"],
|
|
|
|
|
["b:dahua"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/cam/realmonitor?channel={CHANNEL}&subtype=0",
|
|
|
|
|
"dvrip://{USERNAME}:{PASSWORD}@{IP}:{PORT}?channel={CHANNEL}&subtype=0"],
|
2026-08-11 20:47:27 +02:00
|
|
|
// Imou (Dahua consumer brand): Dahua-style RTSP + HTTP snapshot/MJPEG CGI
|
2026-08-11 22:04:10 +02:00
|
|
|
["b:imou"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/cam/realmonitor?channel={CHANNEL}&subtype=0",
|
|
|
|
|
"http://{IP}:{PORT}/video.mjpg?oids=1&username={USERNAME}&password={PASSWORD}",
|
|
|
|
|
"http://{IP}:{PORT}/cgi-bin/snapshot.cgi?loginuse={USERNAME}&loginpas={PASSWORD}"],
|
|
|
|
|
["b:axis"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/axis-media/media.amp",
|
|
|
|
|
"http://{IP}:{PORT}/axis-cgi/mjpg/video.cgi"],
|
|
|
|
|
["b:vivotek"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/live.sdp"],
|
|
|
|
|
["b:amcrest"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/cam/realmonitor?channel={CHANNEL}&subtype=0"],
|
|
|
|
|
// Uniview / Bubble protocol (Uniview IPC/NVR expose bubble:// on port 80)
|
|
|
|
|
["b:uniview"] = ["bubble://{USERNAME}:{PASSWORD}@{IP}:{PORT}/bubble/live?ch={CHANNEL}&stream=0",
|
|
|
|
|
"rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/unicast/c{CHANNEL}/s0/live"],
|
|
|
|
|
// Reolink: ONVIF + RTSP (port 554) — very common on the LAN
|
|
|
|
|
["b:reolink"] = ["rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/h264Preview_01_main",
|
|
|
|
|
"rtsp://{USERNAME}:{PASSWORD}@{IP}:{PORT}/h264Preview_01_sub"],
|
2026-08-11 20:47:27 +02:00
|
|
|
};
|
|
|
|
|
|
2026-08-11 22:04:10 +02:00
|
|
|
/// <summary>Protocol default ports — the fallback port used for an URL when no open port matches.</summary>
|
|
|
|
|
public static readonly Dictionary<string, int> ProtocolDefaultPorts = new(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
{
|
|
|
|
|
["rtsp"] = 554,
|
|
|
|
|
["rtsps"] = 554,
|
|
|
|
|
["rtmp"] = 1935,
|
|
|
|
|
["rtmps"] = 1935,
|
|
|
|
|
["http"] = 80,
|
|
|
|
|
["https"] = 443,
|
|
|
|
|
["bubble"] = 80,
|
|
|
|
|
["dvrip"] = 34567,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// True when a port is plausible for a stream protocol. Used by the
|
|
|
|
|
/// port filter: candidate URLs only carry ports that are either open on
|
|
|
|
|
/// the device or the protocol default.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool IsPortCompatible(string scheme, int port)
|
|
|
|
|
{
|
|
|
|
|
if (ProtocolDefaultPorts.TryGetValue(scheme, out var def) && port == def)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return scheme.ToLowerInvariant() switch
|
|
|
|
|
{
|
|
|
|
|
"rtsp" or "rtsps" => port is 554 or 8554 or 10554 or 5544 or 7070 or 7447 or 8000 or 8001 or 8080 or 8081 or 8090 or 81 or 88,
|
|
|
|
|
"rtmp" or "rtmps" => port == 1935,
|
|
|
|
|
"http" or "https" => port is 80 or 81 or 88 or 8080 or 8081 or 8090 or 8000 or 8001 or 443,
|
|
|
|
|
"bubble" => port is 80 or 8080,
|
|
|
|
|
"dvrip" => port is 34567 or 34568 or 37777,
|
|
|
|
|
_ => false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 20:47:27 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Build candidate stream URLs for a set of template ids. Falls back to a
|
|
|
|
|
/// generic RTSP URL when no template matches.
|
|
|
|
|
/// </summary>
|
2026-08-11 22:04:10 +02:00
|
|
|
/// <remarks>
|
|
|
|
|
/// Port selection per URL: when <paramref name="portFilter"/> is provided,
|
|
|
|
|
/// the protocol default port is used when present in the filter; otherwise
|
|
|
|
|
/// the first compatible open ports (max 2 per template) are used. URLs whose
|
|
|
|
|
/// protocol has no compatible open port are skipped. When no filter is
|
|
|
|
|
/// provided the protocol default port is used.
|
|
|
|
|
/// </remarks>
|
2026-08-11 20:47:27 +02:00
|
|
|
public static List<string> BuildUrls(
|
|
|
|
|
string[] ids, string ip, string? username, string? password, int channel = 0, int[]? portFilter = null)
|
|
|
|
|
{
|
|
|
|
|
var urls = new List<string>();
|
2026-08-11 22:04:10 +02:00
|
|
|
var filter = portFilter is { Length: > 0 }
|
|
|
|
|
? portFilter.Distinct().Order().ToArray()
|
|
|
|
|
: null;
|
2026-08-11 20:47:27 +02:00
|
|
|
|
|
|
|
|
foreach (var id in ids)
|
|
|
|
|
{
|
|
|
|
|
if (!StreamTemplates.TryGetValue(id, out var templates))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach (var template in templates)
|
|
|
|
|
{
|
2026-08-11 22:04:10 +02:00
|
|
|
var scheme = template[..template.IndexOf("://", StringComparison.Ordinal)].ToLowerInvariant();
|
|
|
|
|
var defaultPort = ProtocolDefaultPorts.TryGetValue(scheme, out var dp) ? dp : 554;
|
2026-08-11 20:47:27 +02:00
|
|
|
|
2026-08-11 22:04:10 +02:00
|
|
|
int[] ports;
|
|
|
|
|
if (filter is null)
|
|
|
|
|
{
|
|
|
|
|
ports = [defaultPort];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Prefer the protocol default when open, else compatible open ports.
|
|
|
|
|
var compatible = filter.Where(p => IsPortCompatible(scheme, p)).ToArray();
|
|
|
|
|
if (compatible.Contains(defaultPort))
|
|
|
|
|
ports = [defaultPort];
|
|
|
|
|
else if (compatible.Length > 0)
|
|
|
|
|
ports = compatible.Take(2).ToArray();
|
|
|
|
|
else
|
|
|
|
|
continue; // no plausible port for this protocol → skip URL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var port in ports)
|
|
|
|
|
{
|
|
|
|
|
var url = template
|
|
|
|
|
.Replace("{IP}", ip)
|
|
|
|
|
.Replace("{PORT}", port.ToString(CultureInfo.InvariantCulture))
|
|
|
|
|
.Replace("{CHANNEL}", channel.ToString(CultureInfo.InvariantCulture))
|
|
|
|
|
.Replace("{USERNAME}", username ?? "admin")
|
|
|
|
|
.Replace("{PASSWORD}", password ?? "");
|
|
|
|
|
|
|
|
|
|
if (!urls.Contains(url))
|
|
|
|
|
urls.Add(url);
|
|
|
|
|
}
|
2026-08-11 20:47:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (urls.Count == 0)
|
2026-08-11 22:04:10 +02:00
|
|
|
{
|
2026-08-20 14:13:06 +02:00
|
|
|
const int fallbackPort = 554;
|
2026-08-11 22:04:10 +02:00
|
|
|
urls.Add($"rtsp://{ip}:{fallbackPort}/stream1");
|
|
|
|
|
}
|
2026-08-11 20:47:27 +02:00
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
|
}
|
|
|
|
|
}
|