merge: MP4/AVI/GIF/APNG writers, bitstream parsers, MP4 inspector, Media Foundation encoder
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
36 files changed
+8175
No files matched your search
@@ -0,0 +1,60 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.Marshalling;
|
||||
|
||||
namespace Titano.Core.Windows.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// Lifetime helpers for source-generated COM interop. Interface pointers received as <c>out nint</c> are wrapped in
|
||||
/// unique runtime-callable wrappers so they can be released deterministically (Media Foundation encoders recycle a
|
||||
/// small pool of samples and stall when references linger until finalization).
|
||||
/// </summary>
|
||||
internal static class ComObjects
|
||||
{
|
||||
private static readonly StrategyBasedComWrappers Wrappers = new();
|
||||
|
||||
/// <summary>Wraps an owned interface pointer; the caller's reference is released (the wrapper holds its own).</summary>
|
||||
public static T Attach<T>(nint pointer) where T : class
|
||||
{
|
||||
if (pointer == 0) throw new ArgumentNullException(nameof(pointer));
|
||||
try
|
||||
{
|
||||
return (T)Wrappers.GetOrCreateObjectForComInstance(pointer, CreateObjectFlags.UniqueInstance);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.Release(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Like <see cref="Attach{T}"/> but returns null for a null pointer.</summary>
|
||||
public static T? AttachOrNull<T>(nint pointer) where T : class => pointer == 0 ? null : Attach<T>(pointer);
|
||||
|
||||
/// <summary>An AddRef'ed <typeparamref name="T"/> pointer of a wrapper, for structures; release it with <see cref="Marshal.Release"/>.</summary>
|
||||
public static unsafe nint GetPointer<T>(T wrapper) where T : class => (nint)ComInterfaceMarshaller<T>.ConvertToUnmanaged(wrapper);
|
||||
|
||||
/// <summary>Releases the COM object behind a wrapper created by <see cref="Attach{T}"/> now rather than at finalization.</summary>
|
||||
public static void Release(object? wrapper)
|
||||
{
|
||||
if (wrapper is ComObject com) com.FinalRelease();
|
||||
}
|
||||
|
||||
/// <summary>Throws a <see cref="COMException"/> naming the operation when <paramref name="hr"/> is a failure code.</summary>
|
||||
public static void Check(int hr, string operation)
|
||||
{
|
||||
if (hr < 0) throw new COMException($"{operation} failed with HRESULT 0x{hr:X8}", hr);
|
||||
}
|
||||
|
||||
/// <summary>Reads and frees a CoTaskMem-allocated UTF-16 string.</summary>
|
||||
public static string? TakeString(nint pointer)
|
||||
{
|
||||
if (pointer == 0) return null;
|
||||
try
|
||||
{
|
||||
return Marshal.PtrToStringUni(pointer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeCoTaskMem(pointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user