test(windows): MediaPlayer and source-reader playback as separate tests
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
This commit is contained in:
2 files changed
+150
-49
No files matched your search
@@ -67,37 +67,34 @@ public sealed class MediaFoundationEncoderTests(TestContext context)
|
||||
|
||||
/// <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; a Media Foundation source reader decodes its first frame to the picture that was
|
||||
/// encoded, which is the whole check where Windows Media Player is not installed (Windows Server, "N" editions).
|
||||
/// 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 SoftwareH264OutputPlaysBack()
|
||||
public void MediaPlayerOpensTheSoftwareH264Output()
|
||||
{
|
||||
RequireEncoder(VideoCodec.H264, hardware: false, tenBit: false);
|
||||
var dir = context.CreateTempDirectory();
|
||||
var settings = Settings(Path.Combine(dir, "playback.mp4"), VideoCodec.H264, 8) with { PreferHardware = false };
|
||||
using (var session = new MediaFoundationEncoderProvider().Open(settings))
|
||||
{
|
||||
WriteFrames(session, 60, 8);
|
||||
session.Complete();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
if (Mp4Playback.HasMediaPlayer)
|
||||
{
|
||||
var opened = Mp4Playback.OpenWithMediaPlayer(settings.OutputPath, TimeSpan.FromSeconds(60));
|
||||
context.Log($"MediaPlayer: {opened}");
|
||||
Assert.Equal(1280, opened.Width);
|
||||
Assert.Equal(720, opened.Height);
|
||||
Assert.Approximately(2.0, opened.Duration.TotalSeconds, 0.05);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Log("Windows Media Player is not installed: the source reader decode is the playback check");
|
||||
}
|
||||
|
||||
var frame = Mp4Playback.DecodeFirstFrame(settings.OutputPath);
|
||||
/// <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);
|
||||
@@ -107,6 +104,22 @@ public sealed class MediaFoundationEncoderTests(TestContext context)
|
||||
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)]
|
||||
@@ -181,6 +194,42 @@ public sealed class MediaFoundationEncoderTests(TestContext context)
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)");
|
||||
|
||||
Reference in new issue
Block a user