生成の待ち行列へUIスレッドへの委譲を注入する
このコミットが含まれているのは:
8ファイル変更
+78
-39
検索条件に一致するファイルはありません
@@ -39,7 +39,7 @@ public sealed class ProxyGenerationQueueTests : IDisposable
|
||||
}
|
||||
|
||||
ProxyGenerationQueue CreateQueue(ProxyEncodeFunction encode)
|
||||
=> new(cache, encode, () => options, () => phase, reported.Add)
|
||||
=> new(cache, encode, () => options, () => phase, reported.Add, TestUiThread.Post)
|
||||
{
|
||||
CompletedRetention = TimeSpan.FromMilliseconds(50),
|
||||
CancelledRetention = TimeSpan.FromMilliseconds(50),
|
||||
@@ -54,16 +54,16 @@ public sealed class ProxyGenerationQueueTests : IDisposable
|
||||
|
||||
static async Task WaitUntilAsync(Func<bool> condition)
|
||||
{
|
||||
for (var attempt = 0; attempt < 200 && !condition(); attempt++)
|
||||
for (var attempt = 0; attempt < 200 && !TestUiThread.Read(condition); attempt++)
|
||||
await Task.Delay(10, TestContext.Current.CancellationToken);
|
||||
Assert.True(condition());
|
||||
Assert.True(TestUiThread.Read(condition));
|
||||
}
|
||||
|
||||
static async Task<ProxyGenerationItem> EnqueueAsync(ProxyGenerationQueue queue, SourceIdentity source, int scale)
|
||||
{
|
||||
Assert.True(queue.TryEnqueue(source, scale));
|
||||
await WaitUntilAsync(() => queue.Items.Count == 1);
|
||||
return queue.Items[0];
|
||||
return TestUiThread.Read(() => queue.Items[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -126,7 +126,7 @@ public sealed class ProxyGenerationQueueTests : IDisposable
|
||||
|
||||
queue.TryEnqueue(source, 50);
|
||||
await started.Task;
|
||||
var item = Assert.Single(queue.Items);
|
||||
var item = TestUiThread.Read(() => Assert.Single(queue.Items));
|
||||
Assert.Equal(source.Path, item.SourcePath);
|
||||
Assert.Equal("source.mp4", item.FileName);
|
||||
Assert.Equal(50, item.Scale);
|
||||
@@ -223,7 +223,7 @@ public sealed class ProxyGenerationQueueTests : IDisposable
|
||||
queue.TryEnqueue(source, 50);
|
||||
await Task.Delay(ProxyGenerationQueue.ExportPollInterval * 3, TestContext.Current.CancellationToken);
|
||||
Assert.False(encoded);
|
||||
Assert.Equal(ProxyGenerationStatus.Waiting, Assert.Single(queue.Items).Status);
|
||||
Assert.Equal(ProxyGenerationStatus.Waiting, TestUiThread.Read(() => Assert.Single(queue.Items).Status));
|
||||
|
||||
phase = ExportPhase.Idle;
|
||||
await queue.WhenIdleAsync();
|
||||
|
||||
@@ -27,7 +27,7 @@ public sealed class ProxySourceProviderTests : IDisposable
|
||||
Directory.CreateDirectory(root);
|
||||
context = devices.CreateContext();
|
||||
cache = new ProxyCache(Path.Combine(root, "cache"));
|
||||
queue = new ProxyGenerationQueue(cache, Encode, () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { })
|
||||
queue = new ProxyGenerationQueue(cache, Encode, () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { }, TestUiThread.Post)
|
||||
{
|
||||
CompletedRetention = TimeSpan.FromMilliseconds(50),
|
||||
FailedRetention = TimeSpan.FromMilliseconds(50),
|
||||
@@ -219,7 +219,7 @@ public sealed class ProxySourceProviderTests : IDisposable
|
||||
{
|
||||
var path = CreateFile();
|
||||
refuseAll = true;
|
||||
var failing = new ProxyGenerationQueue(cache, (_, _, _) => throw new ProxyEncodeException(ProxyEncodeFailure.FFmpegFailed, "boom"), () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { })
|
||||
var failing = new ProxyGenerationQueue(cache, (_, _, _) => throw new ProxyEncodeException(ProxyEncodeFailure.FFmpegFailed, "boom"), () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { }, TestUiThread.Post)
|
||||
{
|
||||
FailedRetention = TimeSpan.FromMilliseconds(50),
|
||||
};
|
||||
@@ -265,7 +265,7 @@ public sealed class ProxySourceProviderTests : IDisposable
|
||||
{
|
||||
await release.Task;
|
||||
return await Encode(request, null, CancellationToken.None);
|
||||
}, () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { })
|
||||
}, () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { }, TestUiThread.Post)
|
||||
{
|
||||
CompletedRetention = TimeSpan.FromMilliseconds(50),
|
||||
};
|
||||
|
||||
@@ -23,7 +23,7 @@ public sealed class ProxyUpgradeLoaderTests : IDisposable
|
||||
Directory.CreateDirectory(root);
|
||||
context = devices.CreateContext();
|
||||
cache = new ProxyCache(Path.Combine(root, "cache"));
|
||||
queue = new ProxyGenerationQueue(cache, Encode, () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { })
|
||||
queue = new ProxyGenerationQueue(cache, Encode, () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { }, TestUiThread.Post)
|
||||
{
|
||||
CompletedRetention = TimeSpan.FromMilliseconds(50),
|
||||
};
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace ProxyForge.Tests;
|
||||
|
||||
static class TestUiThread
|
||||
{
|
||||
static readonly Lock gate = new();
|
||||
|
||||
public static void Post(Action action)
|
||||
{
|
||||
using (gate.EnterScope())
|
||||
action();
|
||||
}
|
||||
|
||||
public static T Read<T>(Func<T> read)
|
||||
{
|
||||
using (gate.EnterScope())
|
||||
return read();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace ProxyForge.Tests;
|
||||
|
||||
public sealed class UiThreadTests
|
||||
{
|
||||
[Fact]
|
||||
public void WithoutAnApplicationTheActionRunsInlineOnTheCallingThread()
|
||||
{
|
||||
var threadId = 0;
|
||||
|
||||
UiThread.Post(() => threadId = Environment.CurrentManagedThreadId);
|
||||
|
||||
Assert.Equal(Environment.CurrentManagedThreadId, threadId);
|
||||
}
|
||||
}
|
||||
@@ -255,7 +255,7 @@ public sealed class ProxyForgeSettingsViewModelTests : IDisposable
|
||||
{
|
||||
Directory.CreateDirectory(root);
|
||||
cache = new ProxyCache(Path.Combine(root, "cache"));
|
||||
queue = new ProxyGenerationQueue(cache, (_, _, _) => throw new ProxyEncodeException(ProxyEncodeFailure.FFmpegFailed, "boom"), () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { });
|
||||
queue = new ProxyGenerationQueue(cache, (_, _, _) => throw new ProxyEncodeException(ProxyEncodeFailure.FFmpegFailed, "boom"), () => new ProxyEncodeOptions(50, 30, false), () => ExportPhase.Idle, _ => { }, TestUiThread.Post);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using ProxyForge.Cache;
|
||||
using ProxyForge.Export;
|
||||
using ProxyForge.Sources;
|
||||
@@ -17,7 +16,8 @@ internal sealed class ProxyGenerationQueue(
|
||||
ProxyEncodeFunction encode,
|
||||
Func<ProxyEncodeOptions> options,
|
||||
Func<ExportPhase> exportPhase,
|
||||
Action<Exception> reportUnexpected)
|
||||
Action<Exception> reportUnexpected,
|
||||
Action<Action> onUi)
|
||||
{
|
||||
public static readonly TimeSpan ExportPollInterval = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
@@ -34,9 +34,9 @@ internal sealed class ProxyGenerationQueue(
|
||||
=> HashCode.Combine(StringComparer.OrdinalIgnoreCase.GetHashCode(key.Source.Path), key.Source.Length, key.Source.WriteTimeTicks, key.Scale);
|
||||
}
|
||||
|
||||
sealed class ItemProgress(ProxyGenerationItem item) : IProgress<double>
|
||||
sealed class ItemProgress(ProxyGenerationItem item, Action<Action> onUi) : IProgress<double>
|
||||
{
|
||||
public void Report(double value) => OnUi(() => item.Progress = value);
|
||||
public void Report(double value) => onUi(() => item.Progress = value);
|
||||
}
|
||||
|
||||
readonly Lock gate = new();
|
||||
@@ -49,7 +49,8 @@ internal sealed class ProxyGenerationQueue(
|
||||
(request, progress, cancellationToken) => new ProxyEncoder(FFmpegRuntime.Executables, VideoSourceLoader.Load).EncodeAsync(request, progress, cancellationToken),
|
||||
() => new ProxyEncodeOptions(ProxyForgeSettings.Default.BitrateScale, ProxyForgeSettings.Default.KeyFrameInterval, ProxyForgeSettings.Default.UsesHardwareEncoder),
|
||||
() => ExportDetector.Shared.Phase,
|
||||
ProxyForgeTelemetry.Report);
|
||||
ProxyForgeTelemetry.Report,
|
||||
UiThread.Post);
|
||||
|
||||
public ObservableCollection<ProxyGenerationItem> Items { get; } = [];
|
||||
|
||||
@@ -130,7 +131,7 @@ internal sealed class ProxyGenerationQueue(
|
||||
{
|
||||
var token = cancellation.Token;
|
||||
var retention = CompletedRetention;
|
||||
OnUi(() => Items.Add(item));
|
||||
onUi(() => Items.Add(item));
|
||||
|
||||
try
|
||||
{
|
||||
@@ -138,9 +139,9 @@ internal sealed class ProxyGenerationQueue(
|
||||
try
|
||||
{
|
||||
await WaitForExportToEndAsync(token).ConfigureAwait(false);
|
||||
OnUi(() => item.Status = ProxyGenerationStatus.Generating);
|
||||
onUi(() => item.Status = ProxyGenerationStatus.Generating);
|
||||
var entry = await GenerateAsync(key, item, token).ConfigureAwait(false);
|
||||
OnUi(() =>
|
||||
onUi(() =>
|
||||
{
|
||||
item.Progress = 1d;
|
||||
item.Status = ProxyGenerationStatus.Completed;
|
||||
@@ -155,14 +156,14 @@ internal sealed class ProxyGenerationQueue(
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
retention = CancelledRetention;
|
||||
OnUi(() => item.Status = ProxyGenerationStatus.Cancelled);
|
||||
onUi(() => item.Status = ProxyGenerationStatus.Cancelled);
|
||||
}
|
||||
catch (ProxyEncodeException exception)
|
||||
{
|
||||
retention = FailedRetention;
|
||||
RememberFailure(key, exception);
|
||||
Log.Default.Write($"ProxyForge: プロキシを生成できませんでした。{key.Source.Path}", exception);
|
||||
OnUi(() =>
|
||||
onUi(() =>
|
||||
{
|
||||
item.Failure = exception.Failure;
|
||||
item.Status = ProxyGenerationStatus.Failed;
|
||||
@@ -174,7 +175,7 @@ internal sealed class ProxyGenerationQueue(
|
||||
RememberFailure(key, null);
|
||||
Log.Default.Write($"ProxyForge: プロキシの生成で想定していない例外が起きました。{key.Source.Path}", exception);
|
||||
reportUnexpected(exception);
|
||||
OnUi(() => item.Status = ProxyGenerationStatus.Failed);
|
||||
onUi(() => item.Status = ProxyGenerationStatus.Failed);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -184,7 +185,7 @@ internal sealed class ProxyGenerationQueue(
|
||||
}
|
||||
|
||||
await Task.Delay(retention, CancellationToken.None).ConfigureAwait(false);
|
||||
OnUi(() => Items.Remove(item));
|
||||
onUi(() => Items.Remove(item));
|
||||
}
|
||||
|
||||
async Task WaitForExportToEndAsync(CancellationToken token)
|
||||
@@ -209,7 +210,7 @@ internal sealed class ProxyGenerationQueue(
|
||||
ProxyEncodeResult result;
|
||||
try
|
||||
{
|
||||
result = await encode(request, new ItemProgress(item), token).ConfigureAwait(false);
|
||||
result = await encode(request, new ItemProgress(item, onUi), token).ConfigureAwait(false);
|
||||
var latest = SourceIdentity.Of(key.Source.Path);
|
||||
if (latest is null || !latest.Value.Matches(key.Source.Path, key.Source.Length, key.Source.WriteTimeTicks))
|
||||
throw new ProxyEncodeException(ProxyEncodeFailure.SourceChanged, "The source file changed while the proxy was being generated.");
|
||||
@@ -259,19 +260,4 @@ internal sealed class ProxyGenerationQueue(
|
||||
Log.Default.Write($"ProxyForge: 途中まで書き出したファイルを削除できませんでした。{path}", exception);
|
||||
}
|
||||
}
|
||||
|
||||
static void OnUi(Action action)
|
||||
{
|
||||
var dispatcher = Application.Current?.Dispatcher;
|
||||
if (dispatcher is null || dispatcher.CheckAccess())
|
||||
{
|
||||
action();
|
||||
return;
|
||||
}
|
||||
|
||||
if (dispatcher.HasShutdownStarted)
|
||||
return;
|
||||
|
||||
dispatcher.InvokeAsync(action);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace ProxyForge;
|
||||
|
||||
internal static class UiThread
|
||||
{
|
||||
public static void Post(Action action)
|
||||
{
|
||||
var dispatcher = Application.Current?.Dispatcher;
|
||||
if (dispatcher is null || dispatcher.CheckAccess())
|
||||
{
|
||||
action();
|
||||
return;
|
||||
}
|
||||
|
||||
if (dispatcher.HasShutdownStarted)
|
||||
return;
|
||||
|
||||
dispatcher.InvokeAsync(action);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする