Reported as "the default video and the green screen video are playing at a hideously slow rate… I thought the playback timing issues were resolved?!" They were not: two independent faults, both in the shared clip pipe. 1. QUADRATIC COPY IN THE FRAME READER. `RawVideoFrameReader` re-concatenated the whole pending buffer on every `Feed`, and the pipe feeds it 64 KB at a time. A 1080p BGRA frame is 8 MB, so one frame cost ~127 copies of a growing buffer — roughly half a gigabyte of memcpy per frame. Measured on a 30 fps clip: 6 frames in 3 seconds (2 fps). The reader now fills one preallocated frame buffer and hands that array to the VideoFrame, so a frame is copied exactly once. Pipe reads are 256 KB. Same clip: 29 fps. 2. PACING SLEPT ON TOP OF THE WORK. A fixed 1/fps sleep after each frame adds the frame's own cost, so 20 ms of work on a 30 fps clip yields 1/(33+20) ≈ 19 fps — measured 20.9. Pacing is now deadline-based (`Helpers/FramePacer.cs`): wait only what is left of the frame's interval, resync when a frame overruns instead of bursting, never sleep longer than one interval. The alert clip shares this pipe, so it gained both fixes. Tests: `FramePacerTests` (subtract work, no drift, late → resync, zero duration, reset); `ChromaKeyRealClipTests.RealClip_PlaysAtItsNativeRate` (throughput floor of 15 fps on the real 30 fps clip — the old code sat at 2); the two pacing assertions now check "at most one interval" rather than an exact sleep, which is what deadline pacing means. Full suite: 574 passed, 0 failed, clean build.
81 lines
3.3 KiB
C#
81 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
using ytLive.Services;
|
|
using ytLive.Services.Encoder;
|
|
|
|
namespace ytLive.Tests;
|
|
|
|
/// <summary>
|
|
/// TASK 47 Good Dog: the alert clip decoder paces VIDEO to real time via the
|
|
/// frame-rate probe. The 2026-09-26 live failure was a healthy decoder whose
|
|
/// frames all dumped in the first ~1-2s (no probe = no pacing), leaving the box
|
|
/// frozen on the last frame while the audio ran its ~8s — read as "no video
|
|
/// plays". This drives the REAL <see cref="AlertClipDecoder"/> against a fake
|
|
/// process + probe and records the pacing delay it requests per frame (the same
|
|
/// seam the media path's decoder test uses — the repo never runs a real codec).
|
|
/// </summary>
|
|
public class AlertClipDecoderTests
|
|
{
|
|
[Fact]
|
|
public async Task AlertClipDecoder_PacesVideoFramesToTheProbedFrameRate()
|
|
{
|
|
const int w = 1, h = 1; // frame bytes = 4
|
|
var payload = new byte[12]; // three frames
|
|
var delays = new List<TimeSpan>();
|
|
|
|
using var decoder = new AlertClipDecoder(
|
|
"clip.mp4", w, h,
|
|
new FakeLocator(),
|
|
() => new FakeDecodeProcess(payload),
|
|
frameRateProbe: new FakeFrameRateProbe(100.0),
|
|
delay: (t, _) => { delays.Add(t); return Task.CompletedTask; });
|
|
|
|
var forwarded = 0;
|
|
var completed = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
decoder.FrameAvailable += _ => forwarded++;
|
|
decoder.Completed += () => completed.TrySetResult(true);
|
|
|
|
decoder.Start();
|
|
await completed.Task.WaitAsync(TimeSpan.FromSeconds(5));
|
|
|
|
Assert.Equal(3, forwarded);
|
|
// One pacing delay per emitted frame, each AT MOST one interval: pacing targets a deadline,
|
|
// so the frame's own work is subtracted from the wait (2026-11-05 — the old version slept a
|
|
// full interval on top of the work, which is what made 1080p playback crawl).
|
|
Assert.NotEmpty(delays); // the decoder paces at all
|
|
foreach (var d in delays)
|
|
Assert.True(d > TimeSpan.Zero && d <= TimeSpan.FromMilliseconds(10),
|
|
$"expected a wait of at most 10ms, got {d.TotalMilliseconds}ms");
|
|
}
|
|
|
|
private sealed class FakeDecodeProcess : IDecodeProcess
|
|
{
|
|
private readonly MemoryStream _stream;
|
|
public FakeDecodeProcess(byte[] bytes) => _stream = new MemoryStream(bytes);
|
|
public void Start(ProcessStartInfo startInfo) { }
|
|
public Stream StandardOutput => _stream;
|
|
public bool HasExited => _stream.Position >= _stream.Length;
|
|
public int ExitCode => 0;
|
|
public void Kill() { }
|
|
public Task WaitForExitAsync(CancellationToken ct = default) => Task.CompletedTask;
|
|
public void Dispose() => _stream.Dispose();
|
|
}
|
|
|
|
private sealed class FakeFrameRateProbe : IFrameRateProbe
|
|
{
|
|
private readonly double _fps;
|
|
public FakeFrameRateProbe(double fps) => _fps = fps;
|
|
public Task<double?> ProbeAsync(string path, CancellationToken ct = default)
|
|
=> Task.FromResult<double?>(_fps);
|
|
}
|
|
|
|
private sealed class FakeLocator : IFfmpegLocator
|
|
{
|
|
public Task<string> LocateAsync(CancellationToken ct = default) => Task.FromResult("ffmpeg.exe");
|
|
}
|
|
} |