ファイル
YMM4-ProxyForge/ZeroDiskProxy/Plugin/ZeroDiskProxyVideoSource.cs
T
routersys e2505b4608 エンコーダ設定の外部化とスレッド安全性・メモリ上限の改善
EncoderConfigを新設し、ハードウェアアクセラレーションとディスクフォールバックの有効/無効をエンコーダ生成ごとに動的に取得できるよう外部化しました。
MemoryBudgetにMaxCacheMemoryMbの上限を追加し、物理メモリ空き容量だけでなくキャッシュ総量にも上限を設けました。
ProxyCacheEntryのDataSizeをInterlocked.Readによるキャッシュ済みサイズに変更し、ディスクI/Oなしで安全に参照できるようにしました。
GetOrCreateTempFilePathにlockを追加し、複数スレッドからの同時呼び出しによる多重ファイル書き込みを防止しました。
LastAccessedAtをInterlocked経由で更新するよう変更し、プロパティへの非アトミックな書き込みを排除しました。
ProxyCacheManagerのキャッシュキーをOrdinalIgnoreCaseに変更し、パスの大文字小文字差異による二重キャッシュを防止しました。
キャッシュ登録をGetOrAddに変更し、競合時に重複エントリが生じた場合は後勝ちエントリを即座に解放するよう修正しました。
DispatchInvokeの汎用版を廃止してラムダキャプチャ版に統一し、ボックス化コストを除去しつつ可読性を向上しました。
ExportDetectorのStringBuilderをchar[]とThreadStaticフィールドに変更し、ウィンドウ列挙ごとのヒープアロケーションを削減しました。
ZeroDiskProxyVideoSourceのプロキシアップグレードをTask.Runによる非同期バックグラウンドフェッチに変更し、Update呼び出し元スレッドのブロッキングを解消しました。
廃止されたCpuCoreCount設定とその表示UIを削除しました。
2026-03-24 11:37:32 +09:00

143 行
4.1 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)
{
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);
Task.Run(FetchUpgradeAsync);
}
}
}
lock (_gate)
_inner.Update(time);
}
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);
}
}