The MediaPlayer check ran on an STA thread with no exception handling: a WPF NotSupportedException from new MediaPlayer(), an exception from Open or from an event handler inside Dispatcher.Run ended the whole Titano.Core.Windows.Tests process. Every outcome now goes through one Finish that closes the player and stops the dispatcher once, and the exception is rethrown on the test thread with its own type and stack trace. Where Windows Media Player is missing the playback test passed with only a log line. It is split into MediaPlayerOpensTheSoftwareH264Output, skipped with the reason where Windows Media Player is missing or unusable, and SourceReaderDecodesTheSoftwareH264Output, which runs on every Windows with Media Foundation. MediaPlayerOpensTheHardwareHevcMain10Output also opens the hardware HEVC Main10 output where a hardware encoder and an HEVC decoder exist. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
245 lines
11 KiB
C#
245 lines
11 KiB
C#
using Titano.Core.Imaging;
|
||
using Titano.Core.Video;
|
||
using Titano.Core.Windows.MediaFoundation;
|
||
using Titano.Testing;
|
||
|
||
namespace Titano.Core.Windows.Tests.MediaFoundation;
|
||
|
||
[Serial] // encoders share GPU sessions and the MF runtime
|
||
public sealed class MediaFoundationEncoderTests(TestContext context)
|
||
{
|
||
private const long Frame30 = 3000; // 1/30 s at 90 kHz
|
||
|
||
[Test]
|
||
public void VendorIdsMapToNames()
|
||
{
|
||
Assert.Equal("NVIDIA", MediaFoundationEncoderProvider.VendorName("VEN_10DE"));
|
||
Assert.Equal("Intel", MediaFoundationEncoderProvider.VendorName("ven_8086"));
|
||
Assert.Equal("AMD", MediaFoundationEncoderProvider.VendorName("VEN_1002"));
|
||
Assert.Equal("VEN_ABCD", MediaFoundationEncoderProvider.VendorName("VEN_ABCD"));
|
||
Assert.Null(MediaFoundationEncoderProvider.VendorName(null));
|
||
}
|
||
|
||
[Test]
|
||
[WindowsOnly]
|
||
public void EnumeratesEncodersAndHidesSoftwareHevcAndAv1()
|
||
{
|
||
RequireMediaFoundation();
|
||
var encoders = MediaFoundationEncoders.Enumerate();
|
||
foreach (var e in encoders) context.Log($"{e.Codec} \"{e.Name}\" hardware={e.IsHardware} async={e.IsAsync} vendor={e.VendorId} 10-bit={e.SupportsTenBit}");
|
||
Assert.True(encoders.All(e => !string.IsNullOrWhiteSpace(e.Name)));
|
||
|
||
var capabilities = new MediaFoundationEncoderProvider().Capabilities;
|
||
Assert.True(capabilities.All(c => c.Hardware || c.Codec == VideoCodec.H264), "HEVC/AV1 are hardware-only");
|
||
Assert.True(capabilities.All(c => !c.SupportsTenBit || c.Codec != VideoCodec.H264));
|
||
Assert.Equal(encoders.Count(e => e.IsHardware || e.Codec == VideoCodec.H264), capabilities.Count);
|
||
}
|
||
|
||
[Test]
|
||
[WindowsOnly]
|
||
[Timeout(60_000)]
|
||
public void SoftwareH264EncodeProducesAValidMp4()
|
||
{
|
||
RequireEncoder(VideoCodec.H264, hardware: false, tenBit: false);
|
||
var dir = context.CreateTempDirectory();
|
||
var settings = Settings(Path.Combine(dir, "software.mp4"), VideoCodec.H264, 8) with { PreferHardware = false };
|
||
var provider = new MediaFoundationEncoderProvider();
|
||
Assert.True(provider.CanEncode(settings));
|
||
using (var session = provider.Open(settings))
|
||
{
|
||
Assert.False(session.IsHardware);
|
||
context.Log("encoder: " + session.EncoderName);
|
||
WriteFrames(session, 60, 8);
|
||
session.Complete();
|
||
}
|
||
|
||
var report = Mp4Inspector.Inspect(settings.OutputPath);
|
||
context.Log(report.ToString());
|
||
Assert.True(report.IsValid, string.Join("; ", report.Errors));
|
||
Assert.Equal(VideoCodec.H264, report.Codec);
|
||
Assert.Equal(1280, report.Width);
|
||
Assert.Equal(720, report.Height);
|
||
Assert.Equal(60, report.FrameCount);
|
||
Assert.Approximately(2.0, report.Duration.TotalSeconds, 0.034);
|
||
Assert.True(report.FastStart);
|
||
Assert.Count(1, Directory.GetFiles(dir)); // no temporary file left behind
|
||
}
|
||
|
||
/// <summary>
|
||
/// BRIEF §20.2: what the encoder writes plays in Windows. MediaPlayer (Windows Media Player's pipeline) opens it at
|
||
/// the encoded size and length. Where Windows Media Player is missing or unusable (Windows Server, "N" editions) the
|
||
/// test is skipped with that reason, and <see cref="SourceReaderDecodesTheSoftwareH264Output"/> is the check.
|
||
/// </summary>
|
||
[Test]
|
||
[WindowsOnly]
|
||
[Timeout(120_000)]
|
||
public void MediaPlayerOpensTheSoftwareH264Output()
|
||
{
|
||
RequireEncoder(VideoCodec.H264, hardware: false, tenBit: false);
|
||
RequireMediaPlayer();
|
||
var opened = OpenWithMediaPlayer(Encoded("playback.mp4", VideoCodec.H264, 8, hardware: false));
|
||
context.Log($"MediaPlayer: {opened}");
|
||
Assert.Equal(1280, opened.Width);
|
||
Assert.Equal(720, opened.Height);
|
||
Assert.Approximately(2.0, opened.Duration.TotalSeconds, 0.05);
|
||
}
|
||
|
||
/// <summary>
|
||
/// BRIEF §20.2: a Media Foundation source reader decodes the encoder's first frame to the picture that was encoded.
|
||
/// It needs no Windows Media Player, so it runs on every Windows with Media Foundation, Server editions included.
|
||
/// </summary>
|
||
[Test]
|
||
[WindowsOnly]
|
||
[Timeout(60_000)]
|
||
public void SourceReaderDecodesTheSoftwareH264Output()
|
||
{
|
||
RequireEncoder(VideoCodec.H264, hardware: false, tenBit: false);
|
||
var frame = Mp4Playback.DecodeFirstFrame(Encoded("decode.mp4", VideoCodec.H264, 8, hardware: false));
|
||
context.Log($"source reader: {frame.Width}×{frame.Height}, {frame.Duration}, luma square {frame.LumaAt(100, 360)}, corner {frame.LumaAt(4, 4)}");
|
||
Assert.Equal(1280, frame.Width);
|
||
Assert.Equal(720, frame.Height);
|
||
Assert.Approximately(2.0, frame.Duration.TotalSeconds, 0.05);
|
||
// Frame 0 as WriteFrames drew it: the white square centred at (100, 360) on a dark blue corner.
|
||
Assert.Greater(frame.LumaAt(100, 360), 200);
|
||
Assert.Less(frame.LumaAt(4, 4), 60);
|
||
}
|
||
|
||
/// <summary>The hardware HEVC Main10 output opens in MediaPlayer at its size and length, where Windows can decode HEVC.</summary>
|
||
[Test]
|
||
[WindowsOnly]
|
||
[Timeout(120_000)]
|
||
public void MediaPlayerOpensTheHardwareHevcMain10Output()
|
||
{
|
||
RequireEncoder(VideoCodec.Hevc, hardware: true, tenBit: true);
|
||
RequireMediaPlayer();
|
||
if (!Mp4Playback.HasDecoder(VideoCodec.Hevc)) TestContext.Skip("No HEVC decoder on this machine (HEVC Video Extensions)");
|
||
var opened = OpenWithMediaPlayer(Encoded("main10.mp4", VideoCodec.Hevc, 10, hardware: true));
|
||
context.Log($"MediaPlayer: {opened}");
|
||
Assert.Equal(1280, opened.Width);
|
||
Assert.Equal(720, opened.Height);
|
||
Assert.Approximately(2.0, opened.Duration.TotalSeconds, 0.05);
|
||
}
|
||
|
||
[Test]
|
||
[WindowsOnly]
|
||
[Timeout(60_000)]
|
||
public void HardwareHevcMain10EncodeWhenAvailable()
|
||
{
|
||
RequireEncoder(VideoCodec.Hevc, hardware: true, tenBit: true);
|
||
var dir = context.CreateTempDirectory();
|
||
var settings = Settings(Path.Combine(dir, "main10.mp4"), VideoCodec.Hevc, 10);
|
||
using (var session = new MediaFoundationEncoderProvider().Open(settings))
|
||
{
|
||
Assert.True(session.IsHardware);
|
||
context.Log("encoder: " + session.EncoderName);
|
||
WriteFrames(session, 60, 10);
|
||
session.Complete();
|
||
}
|
||
var report = Mp4Inspector.Inspect(settings.OutputPath);
|
||
context.Log(report.ToString());
|
||
Assert.True(report.IsValid, string.Join("; ", report.Errors));
|
||
Assert.Equal(VideoCodec.Hevc, report.Codec);
|
||
Assert.Equal(10, report.BitDepth);
|
||
Assert.Equal(60, report.FrameCount);
|
||
}
|
||
|
||
[Test]
|
||
[WindowsOnly]
|
||
[Timeout(60_000)]
|
||
public void DisposeWithoutCompleteLeavesNoFile()
|
||
{
|
||
RequireEncoder(VideoCodec.H264, hardware: false, tenBit: false);
|
||
var dir = context.CreateTempDirectory();
|
||
var settings = Settings(Path.Combine(dir, "cancelled.mp4"), VideoCodec.H264, 8) with { PreferHardware = false };
|
||
using (var session = new MediaFoundationEncoderProvider().Open(settings))
|
||
{
|
||
WriteFrames(session, 5, 8);
|
||
}
|
||
Assert.Empty(Directory.GetFiles(dir));
|
||
}
|
||
|
||
private static EncodeSettings Settings(string path, VideoCodec codec, int bitDepth) => new()
|
||
{
|
||
OutputPath = path,
|
||
Codec = codec,
|
||
Width = 1280,
|
||
Height = 720,
|
||
FrameRate = new Rational(30, 1),
|
||
BitDepth = bitDepth,
|
||
Profile = codec == VideoCodec.Hevc ? (bitDepth == 10 ? CodecProfile.HevcMain10 : CodecProfile.HevcMain) : CodecProfile.H264High,
|
||
BitRate = 4_000_000,
|
||
GopLength = 30,
|
||
};
|
||
|
||
/// <summary>A moving colour ramp with a bright square, distinct per frame.</summary>
|
||
private static void WriteFrames(IVideoEncoderSession session, int count, int bitDepth)
|
||
{
|
||
using var frame = new ImageBuffer(1280, 720);
|
||
for (var f = 0; f < count; f++)
|
||
{
|
||
for (var y = 0; y < 720; y++)
|
||
{
|
||
var r = frame.Row(0, y);
|
||
var g = frame.Row(1, y);
|
||
var b = frame.Row(2, y);
|
||
for (var x = 0; x < 1280; x++)
|
||
{
|
||
var square = Math.Abs(x - 100 - f * 15) < 60 && Math.Abs(y - 360) < 60;
|
||
r[x] = square ? 1f : (float)ColorSpace.SrgbToLinear((x + f * 7) % 1280 / 1280.0);
|
||
g[x] = square ? 1f : (float)ColorSpace.SrgbToLinear(y / 720.0);
|
||
b[x] = (float)ColorSpace.SrgbToLinear(bitDepth == 10 ? 0.5 + 0.4 * Math.Sin((x + y) * 0.01) : 0.3);
|
||
}
|
||
}
|
||
session.WriteFrame(frame, Frame30);
|
||
}
|
||
}
|
||
|
||
/// <summary>Two seconds of <see cref="WriteFrames"/> (60 frames at 30 fps, 1280×720) in a new temporary file.</summary>
|
||
private string Encoded(string name, VideoCodec codec, int bitDepth, bool hardware)
|
||
{
|
||
var settings = Settings(Path.Combine(context.CreateTempDirectory(), name), codec, bitDepth) with { PreferHardware = hardware };
|
||
using (var session = new MediaFoundationEncoderProvider().Open(settings))
|
||
{
|
||
Assert.Equal(hardware, session.IsHardware);
|
||
context.Log("encoder: " + session.EncoderName);
|
||
WriteFrames(session, 60, bitDepth);
|
||
session.Complete();
|
||
}
|
||
return settings.OutputPath;
|
||
}
|
||
|
||
/// <summary>
|
||
/// <see cref="Mp4Playback.OpenWithMediaPlayer"/>, skipped when WPF finds Windows Media Player present but unusable
|
||
/// (its <see cref="NotSupportedException"/>, "version 10 or later is required").
|
||
/// </summary>
|
||
private static Mp4Playback.Opened OpenWithMediaPlayer(string path)
|
||
{
|
||
try
|
||
{
|
||
return Mp4Playback.OpenWithMediaPlayer(path, TimeSpan.FromSeconds(60));
|
||
}
|
||
catch (NotSupportedException ex)
|
||
{
|
||
TestContext.Skip("Windows Media Player cannot be used: " + ex.Message);
|
||
throw;
|
||
}
|
||
}
|
||
|
||
private static void RequireMediaPlayer()
|
||
{
|
||
if (!Mp4Playback.HasMediaPlayer) TestContext.Skip("Windows Media Player is not installed (Windows Server, \"N\" editions); the source reader test checks playback");
|
||
}
|
||
|
||
private static void RequireMediaFoundation()
|
||
{
|
||
if (!MediaFoundationRuntime.IsAvailable) TestContext.Skip("Media Foundation is not installed (Windows N or Server without Media Foundation)");
|
||
}
|
||
|
||
private static void RequireEncoder(VideoCodec codec, bool hardware, bool tenBit)
|
||
{
|
||
RequireMediaFoundation();
|
||
if (!MediaFoundationEncoders.Enumerate().Any(e => e.Codec == codec && e.IsHardware == hardware && (!tenBit || e.SupportsTenBit)))
|
||
TestContext.Skip($"No {(hardware ? "hardware" : "software")} {codec}{(tenBit ? " 10-bit" : string.Empty)} encoder on this machine");
|
||
}
|
||
}
|