- CameraStreamService enregistré dans le DI + hosted SnapshotPipelineService
qui synchronise les captures avec les caméras assignées (Central = source de
vérité via clés camera.<id>.* dans la config agent ; fallback local Agent:Cameras)
- Découpage du flux MJPEG en frames JPEG (JpegFrameSplitter, marqueurs SOI/EOI)
- Intervalle adaptatif (spec Phase 15) : scène vide 1000ms / mouvement 500ms /
objet détecté 200ms, doublé si CPU > 80% ; état de scène piloté par les
résultats de l'analyseur + proxy de mouvement (FrameMotionDetector, pixel diff
réel en Phase 16)
- Channel borné 100 snapshots (DropOldest) — backpressure + compteur de drops
et log rate-limité (alerte admin via remontée des logs agent)
- Frames réellement transmises à FrameAnalyzerService (compteur AnalyzedFrameCount)
- Central : GET /api/settings/agent/{id} embarque les caméras assignées
(name/rtsp/sub_rtsp/enabled) ; ConfigReceiverService les applique à chaud
- Tests : JpegFrameSplitter, intervalle adaptatif, tracker de scène, motion
detector, CameraConfigStore, pipeline (snapshot → analyseur), handler config
117 lines
4.0 KiB
C#
117 lines
4.0 KiB
C#
using Heimdall.Agent.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
public class SnapshotPipelineServiceTests
|
|
{
|
|
private const string FfmpegNotFound = "/usr/bin/nonexistent-ffmpeg";
|
|
|
|
private static (SnapshotPipelineService Service, AgentState State, CameraConfigStore Store, CameraStreamService Streams, FrameAnalyzerService Analyzer) CreatePipeline(
|
|
List<AgentCameraOptions>? cameras = null)
|
|
{
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "test-key" };
|
|
var options = Options.Create(new AgentOptions
|
|
{
|
|
CentralUrl = "http://localhost:5000",
|
|
SiteToken = "test",
|
|
Cameras = cameras ?? [],
|
|
});
|
|
var store = new CameraConfigStore();
|
|
var streams = new CameraStreamService(Substitute.For<ILogger<CameraStreamService>>(), FfmpegNotFound);
|
|
var analyzer = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
var service = new SnapshotPipelineService(
|
|
state, options, store, streams, analyzer, new SceneStateTracker(),
|
|
Substitute.For<ILogger<SnapshotPipelineService>>());
|
|
return (service, state, store, streams, analyzer);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartStop_WithoutRegistration_StopsGracefully()
|
|
{
|
|
var (service, state, _, _, _) = CreatePipeline();
|
|
state.AgentId = Guid.Empty; // never registered
|
|
|
|
await service.StartAsync(CancellationToken.None);
|
|
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
|
|
await service.StopAsync(cts.Token);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartStop_WithNoCameras_DoesNotStartStreams()
|
|
{
|
|
var (service, _, _, streams, _) = CreatePipeline();
|
|
|
|
await service.StartAsync(CancellationToken.None);
|
|
try
|
|
{
|
|
await Task.Delay(200);
|
|
streams.ActiveStreamCount.ShouldBe(0);
|
|
}
|
|
finally
|
|
{
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartStop_WithLocalCameras_PopulatesStoreFallback()
|
|
{
|
|
var cameras = new List<AgentCameraOptions>
|
|
{
|
|
new() { Name = "Cam 1", RtspUrl = "rtsp://cam1/stream" },
|
|
};
|
|
var (service, _, store, streams, _) = CreatePipeline(cameras);
|
|
|
|
await service.StartAsync(CancellationToken.None);
|
|
try
|
|
{
|
|
await Task.Delay(200);
|
|
// Local fallback applied to the store…
|
|
var camera = store.Cameras.ShouldHaveSingleItem();
|
|
camera.Name.ShouldBe("Cam 1");
|
|
camera.EffectiveRtspUrl.ShouldBe("rtsp://cam1/stream");
|
|
// …and capture attempted (ffmpeg missing → no active stream, no crash).
|
|
streams.ActiveStreamCount.ShouldBe(0);
|
|
}
|
|
finally
|
|
{
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SnapshotWrittenToChannel_IsForwardedToAnalyzer()
|
|
{
|
|
var (service, _, _, streams, analyzer) = CreatePipeline();
|
|
|
|
await service.StartAsync(CancellationToken.None);
|
|
try
|
|
{
|
|
await streams.SnapshotChannel.Writer.WriteAsync(
|
|
new Snapshot(Guid.NewGuid(), DateTime.UtcNow, [1, 2, 3]));
|
|
|
|
var deadline = DateTime.UtcNow.AddSeconds(5);
|
|
while (service.SnapshotsProcessed == 0 && DateTime.UtcNow < deadline)
|
|
await Task.Delay(50);
|
|
|
|
// The frame reached the analyzer — real call, not just a counter.
|
|
service.SnapshotsProcessed.ShouldBe(1);
|
|
analyzer.AnalyzedFrameCount.ShouldBe(1);
|
|
}
|
|
finally
|
|
{
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StopAsync_WithoutStart_DoesNotThrow()
|
|
{
|
|
var (service, _, _, _, _) = CreatePipeline();
|
|
await service.StopAsync(CancellationToken.None);
|
|
}
|
|
}
|