chore: auto-commit 2026-08-11 22:57 [Heimdall] src/Heimdall.Agent/Services/NetworkCameraScanner.cs, src/Heimdall.Application/DTOs/SiteCredentialDto.cs, src/Heimdall.Application/Services/ICameraDiscoveryService.cs, src/Heimdall.Infrastructure/Services/Discovery/CameraDiscoveryService.cs, src/Heimdall.Infrastructure/Services/Discovery/StreamTesterService.cs…

This commit is contained in:
Arnaud Dartois committed 2026-08-11 22:57:42 +02:00
1 parent ac1871e8d9
commit 6df44cb9e0
6 files changed
+444 -9

No files matched your search

@@ -40,6 +40,8 @@ public sealed class NetworkCameraScanner
private static readonly TimeSpan OnvifUdpTimeout = TimeSpan.FromMilliseconds(500);
private static readonly TimeSpan ReverseDnsTimeout = TimeSpan.FromMilliseconds(500);
private static readonly TimeSpan DvripTimeout = TimeSpan.FromSeconds(3);
private static readonly TimeSpan ScreenshotTimeout = TimeSpan.FromSeconds(6);
private const int MaxScreenshotBytes = 512 * 1024; // 512 KB max per frame
// ReSharper disable once InconsistentNaming
private static readonly XNamespace SoapNs = "http://www.w3.org/2003/05/soap-envelope";
@@ -180,14 +182,26 @@ public sealed class NetworkCameraScanner
if (device.StreamUrls is { Length: > 0 })
{
var alive = await TestUrlsAsync(device.StreamUrls, ct).ConfigureAwait(false);
return device with { StreamUrls = alive.Count > 0 ? [.. alive] : device.StreamUrls };
var screenshot = device.ScreenshotBase64 is null && alive.Count > 0
? await CaptureScreenshotAsync(alive[0], ct).ConfigureAwait(false)
: device.ScreenshotBase64;
return device with
{
StreamUrls = alive.Count > 0 ? [.. alive] : device.StreamUrls,
ScreenshotBase64 = screenshot,
};
}
// Build + test candidate stream URLs (the agent is on the camera LAN,
// the Central is not — this is where the live stream is proven).
var tested = await TestStreamUrlsAsync(device, credentialPool, ct).ConfigureAwait(false);
if (tested.Count > 0)
return device with { StreamUrls = [.. tested] };
{
var screenshot = device.ScreenshotBase64 is null
? await CaptureScreenshotAsync(tested[0], ct).ConfigureAwait(false)
: device.ScreenshotBase64;
return device with { StreamUrls = [.. tested], ScreenshotBase64 = screenshot };
}
if (HasDefinitiveCameraEvidence(device))
{
@@ -1409,6 +1423,102 @@ public sealed class NetworkCameraScanner
}
}
// ─── Screenshot capture ──────────────────────────────────────────────
/// <summary>
/// Capture one JPEG frame from a stream URL with ffmpeg (the agent image
/// ships ffmpeg). Best-effort: returns null on any failure (no ffmpeg,
/// timeout, unreadable stream) — the discovery must never fail because of
/// a screenshot. ArgumentList is used (never Arguments) to avoid RTSP
/// argument injection.
/// </summary>
private async Task<string?> CaptureScreenshotAsync(string url, CancellationToken ct)
{
try
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
cts.CancelAfter(ScreenshotTimeout);
var psi = new System.Diagnostics.ProcessStartInfo
{
FileName = "ffmpeg",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
psi.ArgumentList.Add("-hide_banner");
psi.ArgumentList.Add("-loglevel");
psi.ArgumentList.Add("error");
psi.ArgumentList.Add("-rtsp_transport");
psi.ArgumentList.Add("tcp");
psi.ArgumentList.Add("-i");
psi.ArgumentList.Add(url);
psi.ArgumentList.Add("-frames:v");
psi.ArgumentList.Add("1");
psi.ArgumentList.Add("-f");
psi.ArgumentList.Add("image2pipe");
psi.ArgumentList.Add("-vcodec");
psi.ArgumentList.Add("mjpeg");
psi.ArgumentList.Add("pipe:1");
using var process = System.Diagnostics.Process.Start(psi);
if (process is null)
return null;
using var ms = new MemoryStream();
var copyTask = process.StandardOutput.BaseStream.CopyToAsync(ms, cts.Token);
var stderrTask = process.StandardError.ReadToEndAsync(cts.Token);
var completed = await Task.WhenAny(copyTask, Task.Delay(ScreenshotTimeout, ct)).ConfigureAwait(false);
if (completed != copyTask)
{
TryKill(process);
return null;
}
await copyTask.ConfigureAwait(false);
if (!process.HasExited)
{
var exitTask = process.WaitForExitAsync(cts.Token);
var exited = await Task.WhenAny(exitTask, Task.Delay(TimeSpan.FromSeconds(2), ct)).ConfigureAwait(false);
if (exited != exitTask)
{
TryKill(process);
return null;
}
}
var bytes = ms.ToArray();
if (bytes.Length is 0 or > MaxScreenshotBytes)
return null;
return Convert.ToBase64String(bytes);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
return null;
}
catch (Exception ex)
{
_logger?.LogTrace(ex, "Screenshot capture failed for {Url}", url);
return null;
}
}
private static void TryKill(System.Diagnostics.Process process)
{
try
{
if (!process.HasExited)
process.Kill(entireProcessTree: true);
}
catch
{
// best effort
}
}
// ─── Network helpers ────────────────────────────────────────────────
private static (string? LocalIp, string? GatewayIp) GetLocalNetworkInfo()