ファイル
YMM4-ProxyForge/ZeroDiskProxy/ViewModels/SettingsViewModel.cs
T
routersys 39993b72b2 MediaFoundationベースのプロキシ動画生成・キャッシュ管理プラグイン全コンポーネント新規実装
IProxyEncoderインターフェースを定義し、Windows.Media.TranscodeをラップしたMediaFoundation実装であるMfProxyEncoderを追加しました。
ハードウェアアクセラレーションを優先し、失敗時はソフトウェアエンコードへ自動フォールバックします。
解像度を16アライメントに揃え、ビットレートをフレームサイズとfpsから動的算出します。
エンコード完了後にメモリ予算を確認し、容量が十分な場合はRAMに、不足する場合は一時ファイルとしてディスクに保存します。
ProxyCacheEntryはメモリとディスクの双方を透過的に扱うキャッシュエントリで、OpenReadStream()により格納場所を意識せずストリームを取得できます。
ProxyCacheManagerはConcurrentDictionaryによりスレッドセーフなエントリ管理を行い、生成中タスクの重複起動防止、進捗通知、エラー処理を一括管理します。
完了・キャンセル・失敗の各状態に応じてUIへのDispatchタイミングと自動除去遅延を個別に設定しています。
MemoryBudgetはGlobalMemoryStatusExで実際の空き物理メモリを取得し、指定予約量との差分でインメモリ格納可否を判定します。
FallbackStreamはMemoryStreamとして書き込みを開始し、メモリ残量不足を検知した時点でFileStreamへ透過的に切り替えるハイブリッドストリームです。
BufferPoolはArrayPool<byte>をラップし、大容量バッファの再利用によるGC負荷軽減を行います。
MfNativeMethodsにmfplat.dllおよびmfreadwrite.dllのP/Invoke宣言をまとめました。
MfRuntimeは参照カウント方式でMFStartup/MFShutdownのライフサイクルを安全に管理します。
ManagedIStreamはSystem.StreamをCOM IStreamとして公開するアダプタです。
HResultExtensionsはHRESULTをE_OUTOFMEMORYやE_NOTIMPLなど意味のある例外型にマッピングします。
MfGuidsおよびMfConstantsにMediaFoundation定数を集約しました。
ZeroDiskProxyPluginはIVideoFileSourcePluginを実装し、エクスポート検出・ファイルサイズ閾値判定・プロキシキャッシュヒット確認・自動生成開始の一連の処理フローを担います。
ZeroDiskProxyVideoSourceはDirect2D AffineTransform2Dエフェクトを用いてオリジナル動画ソースとプロキシソースを実行時にホットスワップする機構を持ちます。
ZeroDiskProxyVideoSourceWithScaleはプロキシのスケール差分をAffineTransform2Dで補正し、本来の解像度として上位レイヤに提示します。
PluginHostはシングルトンとしてProxyCacheManager・MemoryBudget・ResourceRegistryを統括し、進捗ポップアップの表示制御とアプリ終了時のクリーンアップを行います。
ExportDetectorはEnumWindowsでプロセス内ウィンドウを列挙し、出力進捗ウィンドウや動画書き出しウィンドウのタイトルを検知してエクスポート中かどうかを判定します。
2秒間のキャッシュにより頻繁な呼び出しでもAPI負荷を抑制します。
ResourceRegistryはCOMポインタとIDisposableを統一的に追跡するIDベースのレジストリです。
ResourceHandleはIDisposableとして登録解除を行うRAIIラッパーです。
ZeroDiskProxySettingsはSettingsBase継承によりJSON永続化と設定UIの自動連携を実現します。
解像度スケール・ビットレート係数・GOPサイズ・メモリ予約量・最大キャッシュメモリ・ハードウェアアクセラレーション・CPUコア数・ディスクフォールバック・自動生成・終了時クリアの各パラメータをClampで安全に管理します。
ZeroDiskProxySettingsViewは基本設定・品質設定・メモリ設定・自動化設定・詳細設定・キャッシュ管理の6セクションをExpanderで構成した設定UIです。
GenerationPopupViewはプロキシ生成中のファイル名・進捗バー・ステータス・格納先を一覧表示する非モーダルウィンドウで、ウィンドウ右下に自動配置されます。
SettingsViewModelはキャッシュ一覧の取得・個別削除・全削除・表示更新コマンドを提供します。
GenerationPopupViewModelはObservableCollectionの変更を監視してHasItemsプロパティを自動更新します。
ProxyGenerationItemはファイル名・進捗率・経過時間・ステータス・格納先種別をBindableとして公開します。
InvertBoolToVisibilityConverterをGenerationPopupViewとSettingsViewの両方で利用するため各ファイルに定義しました。
2026-03-24 09:22:56 +09:00

193 行
6.2 KiB
C#
Raw Blame 履歴

このファイルには曖昧(ambiguous)なUnicode文字が含まれています
このファイルには、他の文字と見間違える可能性があるUnicode文字が含まれています。 それが意図的なものと考えられる場合は、この警告を無視して構いません。 それらの文字を表示するにはエスケープボタンを使用します。
using System.Collections.ObjectModel;
using System.Windows.Input;
using YukkuriMovieMaker.Commons;
using ZeroDiskProxy.Core;
using ZeroDiskProxy.Plugin;
namespace ZeroDiskProxy.ViewModels;
internal sealed class SettingsViewModel : Bindable
{
private string _cacheSummaryText = "読み込み中...";
public string CacheSummaryText
{
get => _cacheSummaryText;
set => Set(ref _cacheSummaryText, value);
}
private string _memoryUsageText = "";
public string MemoryUsageText
{
get => _memoryUsageText;
set => Set(ref _memoryUsageText, value);
}
private bool _hasCacheEntries;
public bool HasCacheEntries
{
get => _hasCacheEntries;
set => Set(ref _hasCacheEntries, value);
}
public ObservableCollection<CacheEntryViewModel> CacheEntries { get; } = [];
public ICommand ClearCacheCommand { get; }
public ICommand RefreshCacheInfoCommand { get; }
public ICommand RemoveCacheEntryCommand { get; }
internal SettingsViewModel()
{
ClearCacheCommand = new ActionCommand(ExecuteClearCache);
RefreshCacheInfoCommand = new ActionCommand(ExecuteRefreshCacheInfo);
RemoveCacheEntryCommand = new ActionCommand<CacheEntryViewModel>(ExecuteRemoveEntry);
ExecuteRefreshCacheInfo();
}
private void ExecuteClearCache()
{
var manager = PluginHost.Instance?.CacheManager;
if (manager is null)
return;
var (count, totalSize) = manager.ClearAll();
if (count == 0)
{
System.Windows.MessageBox.Show(
"削除するキャッシュがありません。",
"ZeroDiskProxy",
System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Information);
}
else
{
System.Windows.MessageBox.Show(
string.Concat(count.ToString("N0"), " 件 (", FormatBytes(totalSize), ") を削除しました。"),
"ZeroDiskProxy",
System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Information);
}
ExecuteRefreshCacheInfo();
}
private void ExecuteRefreshCacheInfo()
{
var manager = PluginHost.Instance?.CacheManager;
if (manager is null)
{
CacheSummaryText = "プラグイン未初期化";
MemoryUsageText = "";
HasCacheEntries = false;
return;
}
var (count, memSize, diskSize, memCount, diskCount) = manager.GetCacheInfo();
CacheSummaryText = string.Concat("キャッシュ数: ", count.ToString("N0"), " 件");
var usageText = string.Concat(
"メモリ: ", memCount.ToString("N0"), " 件 (", FormatBytes(memSize),
") ディスク: ", diskCount.ToString("N0"), " 件 (", FormatBytes(diskSize), ")");
var budget = PluginHost.Instance?.Budget;
if (budget is not null)
usageText = string.Concat(usageText, " 割当済: ", FormatBytes(budget.AllocatedBytes));
MemoryUsageText = usageText;
CacheEntries.Clear();
var snapshots = manager.GetAllSnapshots();
foreach (var snap in snapshots)
CacheEntries.Add(new CacheEntryViewModel(snap));
HasCacheEntries = CacheEntries.Count > 0;
}
private void ExecuteRemoveEntry(CacheEntryViewModel? entry)
{
if (entry is null)
return;
var manager = PluginHost.Instance?.CacheManager;
if (manager is null)
return;
manager.RemoveProxy(entry.OriginalPath, entry.Scale);
ExecuteRefreshCacheInfo();
}
private static string FormatBytes(long bytes)
{
if (bytes <= 0)
return "0 B";
string[] sizes = ["B", "KB", "MB", "GB", "TB"];
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len /= 1024;
}
return string.Concat(len.ToString("0.##"), " ", sizes[order]);
}
}
internal sealed class CacheEntryViewModel : Bindable
{
public string FileName { get; }
public string OriginalPath { get; }
public float Scale { get; }
public string Resolution { get; }
public bool IsInMemory { get; }
public string StorageType { get; }
public long DataSize { get; }
public string DataSizeText { get; }
public string CreatedAtText { get; }
public string LastAccessedText { get; }
public string ScaleText { get; }
internal CacheEntryViewModel(CacheEntrySnapshot snapshot)
{
FileName = snapshot.FileName;
OriginalPath = snapshot.OriginalPath;
Scale = snapshot.Scale;
Resolution = string.Concat(snapshot.ProxyWidth.ToString(), "×", snapshot.ProxyHeight.ToString());
IsInMemory = snapshot.IsInMemory;
StorageType = snapshot.IsInMemory ? "メモリ" : "ディスク";
DataSize = snapshot.DataSize;
DataSizeText = FormatBytes(snapshot.DataSize);
CreatedAtText = snapshot.CreatedAt.ToLocalTime().ToString("HH:mm:ss");
LastAccessedText = snapshot.LastAccessedAt.ToLocalTime().ToString("HH:mm:ss");
ScaleText = string.Concat((snapshot.Scale * 100).ToString("F0"), "%");
}
private static string FormatBytes(long bytes)
{
if (bytes <= 0)
return "0 B";
string[] units = ["B", "KB", "MB", "GB", "TB"];
double value = bytes;
int order = 0;
while (value >= 1024 && order < units.Length - 1)
{
order++;
value /= 1024;
}
return string.Concat(value.ToString("0.##"), " ", units[order]);
}
}
internal sealed class ActionCommand(Action execute) : ICommand
{
public event EventHandler? CanExecuteChanged { add { } remove { } }
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => execute();
}
internal sealed class ActionCommand<T>(Action<T?> execute) : ICommand where T : class
{
public event EventHandler? CanExecuteChanged { add { } remove { } }
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => execute(parameter as T);
}