Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions src/winapp-CLI/WinApp.Cli.Tests/RealRecordingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Text.Json;
using Windows.Win32.Foundation;
using WinApp.Cli.Services;
Expand Down Expand Up @@ -602,6 +603,96 @@ public async Task RecordAsync_TruncatedFrameBundleReturnsActionableWarning()
warning => warning.Contains("only the indexed frame prefix was retained", StringComparison.Ordinal)));
}

[TestMethod]
public async Task RecordAsync_FirstFrameArrivesAfterTheRequestedDuration_StillCapturesAFrame()
{
// The elapsed-duration check used to run before any frame had been committed, so a capture
// source that took longer than --duration to deliver its first frame into the capture loop
// ended the recording with zero frames: an empty MP4 and, with --frames-dir, a zero-frame
// manifest. Elapsed time may only end a recording that has already captured something.
using var fx = new UiaTestFixture();
var capture = new FakeWindowCapture();
var svc = NewAutomation();
var recording = NewRecordingService(svc, capture);
var uiTarget = SessionFor(fx);
await ResolveAsync(svc, uiTarget, "btnInvoke");

var root = Path.Join(AppContext.BaseDirectory, "coverage-scratch", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(root);
var output = Path.Join(root, "slow-first-frame.mp4");
var frame = Enumerable.Repeat((byte)0x33, 64 * 64 * 4).ToArray();

capture.Supported = true;
capture.StartGrabberCallback = (_, _) =>
new SlowStartingFrameGrabber(frame, 64, 64, TimeSpan.FromMilliseconds(1_500));
Mp4SinkWriterEncoder.s_createNoClobber =
(path, width, height, _, _) => new FakeVideoEncoder(path, width, height);

var result = await recording.RecordAsync(uiTarget, null, new RecordOptions
{
OutputPath = output,
DurationSec = 1,
Fps = 2,
MaxEdge = 64,
}, CancellationToken.None);

Assert.IsTrue(
result.Frames >= 1,
$"a recording whose capture source starts late must still capture a frame, got {result.Frames}");
}

[TestMethod]
public async Task RecordAsync_SlowFrameArtifactSetup_IsNotChargedToTheRequestedDuration()
{
// Frame artifact setup creates a staging directory and opens the manifest and index writers.
// The capture clock used to start before that work, so on a loaded machine a short recording
// could spend its whole --duration budget on filesystem setup and capture almost nothing.
using var fx = new UiaTestFixture();
var capture = new FakeWindowCapture();
var svc = NewAutomation();
var recording = NewRecordingService(svc, capture);
var uiTarget = SessionFor(fx);
await ResolveAsync(svc, uiTarget, "btnInvoke");

var root = Path.Join(AppContext.BaseDirectory, "coverage-scratch", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(root);
var output = Path.Join(root, "slow-setup.mp4");
var setupDelay = TimeSpan.FromMilliseconds(1_500);

capture.Supported = true;
capture.StartGrabberCallback = (_, _) =>
new FakeFrameGrabber(new byte[64 * 64 * 4], 64, 64);
Mp4SinkWriterEncoder.s_createNoClobber =
(path, width, height, _, _) => new FakeVideoEncoder(path, width, height);
RecordFrameBundleWriter.s_create = _ =>
{
Thread.Sleep(setupDelay);
return new FakeFrameSink();
};

var wallClock = Stopwatch.StartNew();
var result = await recording.RecordAsync(uiTarget, null, new RecordOptions
{
OutputPath = output,
FramesDirectory = Path.Join(root, "slow-setup.frames"),
DurationSec = 1,
Fps = 4,
MaxEdge = 64,
}, CancellationToken.None);
wallClock.Stop();

Assert.IsTrue(result.Frames >= 1, "the recording must capture something");

// Wall time covers setup and capture; the reported elapsed time must cover capture only, so
// the setup delay has to show up as the difference. Comparing the two rather than asserting
// an absolute elapsed time keeps this immune to a slow machine stretching the capture loop.
var excluded = wallClock.ElapsedMilliseconds - result.ElapsedMs;
Assert.IsTrue(
excluded >= setupDelay.TotalMilliseconds * 0.8,
$"reported elapsed time must measure capture, not setup: only {excluded}ms of the " +
$"{setupDelay.TotalMilliseconds}ms setup was excluded from {result.ElapsedMs}ms elapsed");
}

private sealed class FakeFrameGrabber(byte[] pixels, int width, int height) : IFrameGrabber
{
private long _version;
Expand All @@ -618,6 +709,48 @@ private sealed class FakeFrameGrabber(byte[] pixels, int width, int height) : IF
public void Dispose() => Disposed = true;
}

/// <summary>
/// Reports one frame to the recorder's setup read, then yields nothing for the given silence
/// window before delivering frames again — the shape of a capture source that is slow to start
/// feeding the capture loop.
/// </summary>
private sealed class SlowStartingFrameGrabber(
byte[] pixels, int width, int height, TimeSpan silence) : IFrameGrabber
{
private readonly Stopwatch _silenceSince = new();
private int _reads;
private long _version;

public bool IsClosed => false;

public (byte[] Pixels, int Width, int Height, long Version)? TryGetLatest()
{
// Recorder setup dereferences its read without a null check, so it always gets a frame.
if (Interlocked.Increment(ref _reads) == 1)
{
return Latest();
}

// Time the silence from the capture loop's first read, so the window the recorder sees
// does not shrink by however long the recorder spent between the two.
if (!_silenceSince.IsRunning)
{
_silenceSince.Start();
}

return _silenceSince.Elapsed < silence ? null : Latest();
}

public Task<bool> WaitForFirstFrameAsync(TimeSpan timeout, CancellationToken ct) => Task.FromResult(true);

public void Dispose()
{
}

private (byte[] Pixels, int Width, int Height, long Version) Latest()
=> (pixels, width, height, Interlocked.Increment(ref _version));
}

private sealed class FakeVideoEncoder(string path, int width, int height) : IVideoEncoder
{
public int Width { get; } = width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ private RecordFrameBundleConfiguration CreateFrameConfiguration(
{
FinalDirectory = finalDirectory,
VideoPath = Path.Join(_tempDirectory.FullName, "video.mp4"),
StartedUtc = DateTimeOffset.Parse("2026-01-01T00:00:00Z"),
Width = 64,
Height = 64,
MaximumBundleBytes = maximumBundleBytes,
Expand All @@ -476,6 +475,7 @@ private static RecordFrameCompletion Completion(int videoFrameCount = 3)
{
Status = "complete",
StopReason = "cancelled",
StartedUtc = DateTimeOffset.Parse("2026-01-01T00:00:00Z"),
ElapsedMs = 300,
AchievedFps = 10,
CadenceRatio = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ internal sealed class RecordFrameBundleConfiguration

public required string FinalDirectory { get; init; }
public required string VideoPath { get; init; }
public required DateTimeOffset StartedUtc { get; init; }
public required int Width { get; init; }
public required int Height { get; init; }
public required RecordFrameRequestManifest Requested { get; init; }
Expand All @@ -38,6 +37,12 @@ internal sealed class RecordFrameCompletion
{
public required string Status { get; init; }
public required string StopReason { get; init; }

/// <summary>
/// Wall-clock instant capture began. Supplied at completion because it is the same instant the
/// capture clock starts, which is only known once artifact setup is done.
/// </summary>
public required DateTimeOffset StartedUtc { get; init; }
public required long ElapsedMs { get; init; }
public required double AchievedFps { get; init; }
public required double CadenceRatio { get; init; }
Expand Down Expand Up @@ -213,7 +218,7 @@ public async Task<RecordFrameArtifactResult> CompleteAsync(RecordFrameCompletion
Status = completion.Status == "complete" && IsTruncated
? "truncated"
: completion.Status,
StartedUtc = _configuration.StartedUtc,
StartedUtc = completion.StartedUtc,
CompletedUtc = DateTimeOffset.UtcNow,
StopReason = completion.StopReason,
Requested = _configuration.Requested,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ internal sealed partial class UiRecordingService
private sealed class RecordFrameArtifactSetup
{
public required RecordOptions Options { get; init; }
public required DateTimeOffset StartedUtc { get; init; }
public required int EncoderWidth { get; init; }
public required int EncoderHeight { get; init; }
}
Expand All @@ -29,7 +28,6 @@ private RecordFrameArtifactCoordinator CreateRecordFrameArtifactCoordinator(
{
FinalDirectory = setup.Options.FramesDirectory!,
VideoPath = setup.Options.OutputPath,
StartedUtc = setup.StartedUtc,
Width = setup.EncoderWidth,
Height = setup.EncoderHeight,
Requested = new RecordFrameRequestManifest
Expand Down
20 changes: 16 additions & 4 deletions src/winapp-CLI/WinApp.UIAutomation.Recording/UiRecordingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,6 @@ public async Task<RecordCaptureResult> RecordAsync(UiTarget uiTarget, string? el

var frameDurationHns = 10_000_000L / options.Fps;
var totalFrames = options.DurationSec > 0 ? (long)options.DurationSec * options.Fps : (long?)null;
var stopwatch = Stopwatch.StartNew();
var startedUtc = DateTimeOffset.UtcNow;
var frameIndex = 0;
long lastEncodedVersion = -1;
var startedSignaled = false;
Expand All @@ -330,12 +328,19 @@ public async Task<RecordCaptureResult> RecordAsync(UiTarget uiTarget, string? el
frameOutput = CreateRecordFrameArtifactCoordinator(new RecordFrameArtifactSetup
{
Options = options,
StartedUtc = startedUtc,
EncoderWidth = encoderW,
EncoderHeight = encoderH,
});
}

// The capture clock starts here, after frame artifact setup has created its staging
// directory and opened the manifest and index writers. Starting it before that setup
// charged the filesystem work to --duration, so on a loaded machine a short recording
// could spend its whole budget before capturing anything. startedUtc marks the same
// instant, so a frame's elapsedMs is an offset from the manifest's startedUtc.
var stopwatch = Stopwatch.StartNew();
var startedUtc = DateTimeOffset.UtcNow;

async ValueTask CommitFrameAsync(byte[] processedFrame)
{
var sampleIndex = frameIndex;
Expand Down Expand Up @@ -375,7 +380,12 @@ async ValueTask CommitFrameAsync(byte[] processedFrame)
break;
}

if (totalFrames.HasValue && stopwatch.Elapsed.TotalSeconds >= options.DurationSec)
// Elapsed time only ends a recording that has already captured something.
// Reaching the requested duration before the first frame is committed means
// capture was slow to start, not that the recording is done; ending here handed
// the caller an empty MP4 and a zero-frame manifest instead of a recording.
// Cancellation is unaffected and still ends the loop at zero frames.
if (totalFrames.HasValue && frameIndex > 0 && stopwatch.Elapsed.TotalSeconds >= options.DurationSec)
{
break;
}
Expand Down Expand Up @@ -518,6 +528,7 @@ async ValueTask CommitFrameAsync(byte[] processedFrame)
{
Status = "partial",
StopReason = stopReason,
StartedUtc = startedUtc,
ElapsedMs = elapsedMs,
AchievedFps = frameAchievedFps,
CadenceRatio = frameCadenceRatio,
Expand Down Expand Up @@ -557,6 +568,7 @@ async ValueTask CommitFrameAsync(byte[] processedFrame)
{
Status = "complete",
StopReason = stopReason,
StartedUtc = startedUtc,
ElapsedMs = elapsedMs,
AchievedFps = achievedFps,
CadenceRatio = cadenceRatio,
Expand Down
Loading