Files
Chronos/tests/Chronos.Core.Windows.Tests/MediaFoundation/Mp4Playback.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

290 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Windows.Media;
using System.Windows.Threading;
using Chronos.Core.Video;
using Chronos.Core.Windows.Interop;
using Chronos.Core.Windows.MediaFoundation;
namespace Chronos.Core.Windows.Tests.MediaFoundation;
/// <summary>
/// How Windows plays a written MP4 back (BRIEF §20.2): WPF's <see cref="MediaPlayer"/>, which runs on Windows Media
/// Player's pipeline, and a Media Foundation source reader that decodes the first frame, which also works where Windows
/// Media Player is not installed (Windows Server, "N" editions).
/// </summary>
internal static partial class Mp4Playback
{
/// <summary>What a player reports once the file is open.</summary>
public sealed record Opened(int Width, int Height, TimeSpan Duration);
/// <summary>The first decoded frame: its size, the file's duration and the luma plane (8 bits, top-down, no padding).</summary>
public sealed record FirstFrame(int Width, int Height, TimeSpan Duration, byte[] Luma)
{
public byte LumaAt(int x, int y) => Luma[y * Width + x];
}
/// <summary>True when <see cref="MediaPlayer"/> can work: it needs Windows Media Player (wmp.dll).</summary>
public static bool HasMediaPlayer
{
get
{
if (!NativeLibrary.TryLoad("wmp.dll", out var handle)) return false;
NativeLibrary.Free(handle);
return true;
}
}
/// <summary>
/// True when Media Foundation has a decoder for <paramref name="codec"/>: always for H.264, only with the HEVC Video
/// Extensions for HEVC, which MediaPlayer needs as much as the source reader does.
/// </summary>
public static unsafe bool HasDecoder(VideoCodec codec)
{
using var runtime = MediaFoundationRuntime.Acquire();
var input = new MftRegisterTypeInfo { MajorType = MfGuids.MediaTypeVideo, Subtype = MediaFoundationEncoders.Subtype(codec) };
const uint flags = MfNative.EnumHardware | MfNative.EnumAsync | MfNative.EnumSync | MfNative.EnumSortAndFilter;
if (MfNative.MFTEnumEx(VideoDecoderCategory, flags, &input, null, out var array, out var count) < 0) return false;
try
{
for (var i = 0; i < count; i++) Marshal.Release(Marshal.ReadIntPtr(array, i * IntPtr.Size));
}
finally
{
Marshal.FreeCoTaskMem(array);
}
return count > 0;
}
/// <summary>
/// Opens <paramref name="path"/> with <see cref="MediaPlayer"/> on its own dispatcher thread and returns what it
/// reports at <see cref="MediaPlayer.MediaOpened"/>. Throws, on the calling thread, when it raises
/// <see cref="MediaPlayer.MediaFailed"/>, when neither event comes within <paramref name="timeout"/>, and with WPF's
/// own exception when the player cannot be made or opened at all (a <see cref="NotSupportedException"/> where Windows
/// Media Player 10 or later is not usable): nothing escapes the dispatcher thread, where it would end the test program.
/// </summary>
public static Opened OpenWithMediaPlayer(string path, TimeSpan timeout)
{
Opened? opened = null;
Exception? failure = null;
var thread = new Thread(() =>
{
var dispatcher = Dispatcher.CurrentDispatcher;
MediaPlayer? player = null;
DispatcherTimer? deadline = null;
var finished = false;
// The first outcome counts; the player is closed and the dispatcher stopped once.
void Finish(Opened? result, Exception? error)
{
if (finished) return;
finished = true;
(opened, failure) = (result, error);
deadline?.Stop();
try
{
player?.Close();
}
catch (Exception ex)
{
failure ??= ex;
}
dispatcher.InvokeShutdown();
}
try
{
player = new MediaPlayer();
player.MediaOpened += (_, _) =>
{
// A handler that threw would leave Dispatcher.Run with the player still open.
try
{
Finish(new Opened(player.NaturalVideoWidth, player.NaturalVideoHeight,
player.NaturalDuration.HasTimeSpan ? player.NaturalDuration.TimeSpan : TimeSpan.Zero), null);
}
catch (Exception ex)
{
Finish(null, ex);
}
};
player.MediaFailed += (_, e) =>
Finish(null, new InvalidDataException($"MediaPlayer could not open '{path}': {e.ErrorException?.Message}", e.ErrorException));
// This constructor starts the timer.
deadline = new DispatcherTimer(timeout, DispatcherPriority.Normal, (_, _) =>
Finish(null, new TimeoutException($"MediaPlayer neither opened nor failed on '{path}' within {timeout.TotalSeconds:0} s")), dispatcher);
player.Open(new Uri(path, UriKind.Absolute));
Dispatcher.Run();
}
catch (Exception ex)
{
if (finished) failure ??= ex;
else Finish(null, ex);
}
})
{
IsBackground = true,
Name = "MediaPlayer check",
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
// Rethrown with the dispatcher thread's stack trace and its own type: a NotSupportedException stays one.
if (failure is not null) ExceptionDispatchInfo.Throw(failure);
return opened ?? throw new InvalidOperationException("MediaPlayer stopped without a result");
}
/// <summary>Decodes the first video frame of <paramref name="path"/> to NV12 with a Media Foundation source reader.</summary>
public static unsafe FirstFrame DecodeFirstFrame(string path)
{
using var runtime = MediaFoundationRuntime.Acquire();
ComObjects.Check(MFCreateSourceReaderFromURL(path, 0, out var readerPointer), "MFCreateSourceReaderFromURL");
var reader = ComObjects.Attach<IMFSourceReader>(readerPointer);
try
{
var duration = TimeSpan.Zero;
var variant = stackalloc byte[PropVariantSize];
new Span<byte>(variant, PropVariantSize).Clear();
if (reader.GetPresentationAttribute(MediaSource, PresentationDuration, (nint)variant) >= 0)
{
if (*(ushort*)variant == VtUi8) duration = TimeSpan.FromTicks((long)*(ulong*)(variant + 8));
PropVariantClear((nint)variant);
}
// The reader inserts the H.264/HEVC decoder itself once NV12 is asked for.
ComObjects.Check(MfNative.MFCreateMediaType(out var typePointer), "MFCreateMediaType");
var type = ComObjects.Attach<IMFMediaType>(typePointer);
try
{
ComObjects.Check(type.SetGUID(MfGuids.MajorType, MfGuids.MediaTypeVideo), "MF_MT_MAJOR_TYPE");
ComObjects.Check(type.SetGUID(MfGuids.Subtype, MfGuids.FormatNv12), "MF_MT_SUBTYPE");
var native = ComObjects.GetPointer(type);
try
{
ComObjects.Check(reader.SetCurrentMediaType(FirstVideoStream, 0, native), "SetCurrentMediaType(NV12)");
}
finally
{
Marshal.Release(native);
}
}
finally
{
ComObjects.Release(type);
}
var sample = ReadFirstSample(reader);
try
{
ComObjects.Check(reader.GetCurrentMediaType(FirstVideoStream, out var currentPointer), "GetCurrentMediaType");
var current = ComObjects.Attach<IMFMediaType>(currentPointer);
ulong size;
try
{
ComObjects.Check(current.GetUINT64(MfGuids.FrameSize, out size), "MF_MT_FRAME_SIZE");
}
finally
{
ComObjects.Release(current);
}
var (width, height) = ((int)(size >> 32), (int)(size & 0xFFFF_FFFF));
return new FirstFrame(width, height, duration, CopyLuma(sample, width, height));
}
finally
{
ComObjects.Release(sample);
}
}
finally
{
ComObjects.Release(reader);
}
}
/// <summary>Reads until the first video sample; stream ticks and format changes come without one.</summary>
private static IMFSample ReadFirstSample(IMFSourceReader reader)
{
for (var attempt = 0; attempt < 100; attempt++)
{
ComObjects.Check(reader.ReadSample(FirstVideoStream, 0, out _, out var flags, out _, out var samplePointer), "ReadSample");
if (samplePointer != 0) return ComObjects.Attach<IMFSample>(samplePointer);
if ((flags & (ReaderError | ReaderEndOfStream)) != 0) throw new InvalidDataException($"The source reader returned no frame (flags 0x{flags:X})");
}
throw new InvalidDataException("The source reader returned no frame in 100 reads");
}
/// <summary>Copies the Y plane out of the sample's buffer, honouring the decoder's row pitch.</summary>
private static unsafe byte[] CopyLuma(IMFSample sample, int width, int height)
{
ComObjects.Check(sample.ConvertToContiguousBuffer(out var bufferPointer), "ConvertToContiguousBuffer");
var buffer = ComObjects.Attach<IMFMediaBuffer>(bufferPointer);
try
{
var luma = new byte[width * height];
if (buffer is IMF2DBuffer twoD && twoD.Lock2D(out var scanline0, out var pitch) >= 0)
{
try
{
for (var y = 0; y < height; y++) new ReadOnlySpan<byte>((byte*)scanline0 + (long)y * pitch, width).CopyTo(luma.AsSpan(y * width));
}
finally
{
twoD.Unlock2D();
}
return luma;
}
ComObjects.Check(buffer.Lock(out var data, out _, out var length), "IMFMediaBuffer.Lock");
try
{
// A contiguous NV12 frame: the Y rows, then half as many interleaved chroma rows, all of one pitch.
var stride = (int)(length / (uint)(height + height / 2));
if (stride < width) throw new InvalidDataException($"An NV12 buffer of {length} bytes is too small for {width}×{height}");
for (var y = 0; y < height; y++) new ReadOnlySpan<byte>((byte*)data + (long)y * stride, width).CopyTo(luma.AsSpan(y * width));
return luma;
}
finally
{
buffer.Unlock();
}
}
finally
{
ComObjects.Release(buffer);
}
}
private const uint FirstVideoStream = 0xFFFF_FFFC; // MF_SOURCE_READER_FIRST_VIDEO_STREAM
private const uint MediaSource = 0xFFFF_FFFF; // MF_SOURCE_READER_MEDIASOURCE
private const uint ReaderError = 0x1, ReaderEndOfStream = 0x2; // MF_SOURCE_READERF_ERROR, _ENDOFSTREAM
private const ushort VtUi8 = 21;
private const int PropVariantSize = 24; // PROPVARIANT on 64-bit Windows
/// <summary>MFT_CATEGORY_VIDEO_DECODER.</summary>
private static readonly Guid VideoDecoderCategory = new("d6c02d4b-6833-45b4-971a-05a4b04bab91");
/// <summary>MF_PD_DURATION: the presentation's length in 100 ns units.</summary>
private static readonly Guid PresentationDuration = new("6c990d33-bb8e-477a-8598-0d5d96fcd88a");
[LibraryImport("mfreadwrite.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial int MFCreateSourceReaderFromURL(string url, nint attributes, out nint reader);
[LibraryImport("ole32.dll")]
private static partial int PropVariantClear(nint variant);
}
/// <summary>IMFSourceReader (mfreadwrite.h) in vtable order.</summary>
[GeneratedComInterface]
[Guid("70ae66f2-c809-4e4f-8915-bdcb406b7993")]
internal partial interface IMFSourceReader
{
[PreserveSig] int GetStreamSelection(uint streamIndex, out int selected);
[PreserveSig] int SetStreamSelection(uint streamIndex, int selected);
[PreserveSig] int GetNativeMediaType(uint streamIndex, uint mediaTypeIndex, out nint mediaType);
[PreserveSig] int GetCurrentMediaType(uint streamIndex, out nint mediaType);
[PreserveSig] int SetCurrentMediaType(uint streamIndex, nint reserved, nint mediaType);
[PreserveSig] int SetCurrentPosition(in Guid timeFormat, nint position);
[PreserveSig] int ReadSample(uint streamIndex, uint controlFlags, out uint actualStreamIndex, out uint streamFlags, out long timestamp, out nint sample);
[PreserveSig] int Flush(uint streamIndex);
[PreserveSig] int GetServiceForStream(uint streamIndex, in Guid service, in Guid iid, out nint instance);
[PreserveSig] int GetPresentationAttribute(uint streamIndex, in Guid attribute, nint value);
}