ファイル
YMM4-ProxyForge/ZeroDiskProxy/Plugin/ZeroDiskProxyVideoSource.cs
T
routersys ff929da7a0 不要なMF相互運用ファイル削除とコード全体のリファクタリング
未使用のMFインターロップファイルを削除しました。
MfProxyEncoderのトランスコード進捗報告処理をTranscodeProgressReporterクラスに抽出しました。
キャンセル登録の型をIAsyncOperationWithProgressからIAsyncActionWithProgressに修正しました。
MfProxyEncoderFactory・MemoryBudget・PooledMemoryStream・CacheEntryViewModel等をプライマリコンストラクタ構文に変換しました。
ProxyCacheManagerのCacheKeyをrecord structに変換し、Equalsのオーバーライドを整理しました。
PooledBufferをmutableなstructに変更し、Interlocked.Exchangeによるスレッドセーフなディスポーズを実装しました。
FallbackStreamにRead(Span<byte>)・Write(ReadOnlySpan<byte>)・WriteAsyncの各オーバーロードを追加しました。
MemoryBudgetのTickCountを32ビットuintにキャストし、ラッピング演算の整合性を修正しました。
GetAllSnapshotsをList<T>ベースに変更し、配列の事前確保とArray.Resizeを除去しました。
ZeroDiskProxyVideoSourceのUpgrade適用ロジックをTryApplyUpgradeメソッドとして抽出しました。
ProxyGenerationItem・ByteFormatter・CacheEntryViewModelの文字列生成をstring.Createと補間文字列に統一しました。
PluginHostからResourceRegistryへの依存を取り除き、関連するDispose呼び出しを削除しました。
CheckExportWindowにDebug.Assertを追加し、スレッドプールの非同期継続から呼ばれた場合の前提条件違反を検出できるようにしました。
ClearAllのforeachでデコンストラクション構文をkeyプロパティの明示的な参照に変更しました。
CreateCoreメソッドをインスタンスメンバーからstaticメソッドに変更しました。
パターンマッチングをis { IsLoaded: true }・is { IsValid: true }形式に統一しました。
2026-03-24 17:09:37 +09:00

156 行
4.3 KiB
C#

using System.Diagnostics;
using System.Numerics;
using Vortice.Direct2D1;
using Vortice.Direct2D1.Effects;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Plugin.FileSource;
namespace ZeroDiskProxy.Plugin;
internal sealed class ZeroDiskProxyVideoSource : IVideoFileSource
{
private IVideoFileSource _inner;
private readonly Func<IVideoFileSource?>? _tryUpgradeSource;
private readonly AffineTransform2D _switchEffect;
private readonly ID2D1Image _output;
private List<IVideoFileSource>? _retiredSources;
private readonly object _gate = new();
private volatile bool _upgraded;
private volatile IVideoFileSource? _pendingUpgrade;
private int _upgradeFetching;
private long _nextUpgradeAttemptTicks;
private int _disposed;
public TimeSpan Duration
{
get
{
lock (_gate)
return _inner.Duration;
}
}
public ID2D1Image Output => _output;
internal ZeroDiskProxyVideoSource(IVideoFileSource inner, IGraphicsDevicesAndContext devices)
{
_inner = inner;
_switchEffect = new AffineTransform2D(devices.DeviceContext);
_switchEffect.SetInput(0, inner.Output, true);
_switchEffect.TransformMatrix = Matrix3x2.Identity;
_output = _switchEffect.Output;
}
internal ZeroDiskProxyVideoSource(IVideoFileSource inner, IGraphicsDevicesAndContext devices, Func<IVideoFileSource?> tryUpgradeSource)
{
_inner = inner;
_switchEffect = new AffineTransform2D(devices.DeviceContext);
_switchEffect.SetInput(0, inner.Output, true);
_switchEffect.TransformMatrix = Matrix3x2.Identity;
_output = _switchEffect.Output;
_tryUpgradeSource = tryUpgradeSource;
}
public void Update(TimeSpan time)
{
if (!_upgraded && _tryUpgradeSource is not null)
TryApplyUpgrade();
if (_upgraded)
{
if (Volatile.Read(ref _disposed) == 0)
_inner.Update(time);
return;
}
lock (_gate)
{
if (Volatile.Read(ref _disposed) == 0)
_inner.Update(time);
}
}
private void TryApplyUpgrade()
{
var pending = _pendingUpgrade;
if (pending is not null)
{
_pendingUpgrade = null;
lock (_gate)
{
if (!_upgraded && Volatile.Read(ref _disposed) == 0)
{
(_retiredSources ??= []).Add(_inner);
_inner = pending;
_switchEffect.SetInput(0, pending.Output, true);
_upgraded = true;
}
else
{
pending.Dispose();
}
}
}
else
{
var now = Stopwatch.GetTimestamp();
if (now >= Volatile.Read(ref _nextUpgradeAttemptTicks) &&
Interlocked.Exchange(ref _upgradeFetching, 1) == 0)
{
Volatile.Write(ref _nextUpgradeAttemptTicks, now + Stopwatch.Frequency / 2);
ThreadPool.UnsafeQueueUserWorkItem(static s => s.FetchUpgradeAsync(), this, preferLocal: false);
}
}
}
private void FetchUpgradeAsync()
{
try
{
var upgraded = _tryUpgradeSource!();
if (upgraded is not null)
_pendingUpgrade = upgraded;
}
catch { }
finally
{
Volatile.Write(ref _upgradeFetching, 0);
}
}
public int GetFrameIndex(TimeSpan time)
{
lock (_gate)
return _inner.GetFrameIndex(time);
}
public void Dispose()
{
if (Interlocked.Exchange(ref _disposed, 1) != 0)
return;
lock (_gate)
{
_output.Dispose();
_switchEffect.SetInput(0, null, true);
_switchEffect.Dispose();
_inner.Dispose();
if (_retiredSources is { } retired)
{
foreach (var source in retired)
source.Dispose();
retired.Clear();
}
}
var orphan = _pendingUpgrade;
if (orphan is not null)
{
_pendingUpgrade = null;
orphan.Dispose();
}
GC.SuppressFinalize(this);
}
}