2021-02-08 16:51:19 +08:00
|
|
|
using System;
|
|
|
|
using BililiveRecorder.Flv.Pipeline.Rules;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
namespace BililiveRecorder.Flv.Pipeline
|
|
|
|
{
|
|
|
|
public static class IProcessingPipelineBuilderExtensions
|
|
|
|
{
|
2022-06-25 17:31:21 +08:00
|
|
|
public static IProcessingPipelineBuilder ConfigureServices(this IProcessingPipelineBuilder builder, Action<IServiceCollection> configure)
|
|
|
|
{
|
|
|
|
configure?.Invoke(builder.ServiceCollection);
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IProcessingPipelineBuilder AddRule<T>(this IProcessingPipelineBuilder builder) where T : IProcessingRule =>
|
|
|
|
builder.AddRule((next, services) => ActivatorUtilities.GetServiceOrCreateInstance<T>(services) switch
|
2021-02-08 16:51:19 +08:00
|
|
|
{
|
2021-03-09 00:50:13 +08:00
|
|
|
ISimpleProcessingRule simple => context => simple.Run(context, () => next(context)),
|
|
|
|
IFullProcessingRule full => context => full.Run(context, next),
|
2021-02-08 16:51:19 +08:00
|
|
|
_ => throw new ArgumentException($"Type ({typeof(T).FullName}) does not ISimpleProcessingRule or IFullProcessingRule")
|
|
|
|
});
|
|
|
|
|
2022-06-25 17:31:21 +08:00
|
|
|
public static IProcessingPipelineBuilder AddRule<T>(this IProcessingPipelineBuilder builder, T instance) where T : IProcessingRule =>
|
2021-02-08 16:51:19 +08:00
|
|
|
instance switch
|
|
|
|
{
|
2022-06-25 17:31:21 +08:00
|
|
|
ISimpleProcessingRule simple => builder.AddRule((next, services) => context => simple.Run(context, () => next(context))),
|
|
|
|
IFullProcessingRule full => builder.AddRule((next, services) => context => full.Run(context, next)),
|
2021-02-08 16:51:19 +08:00
|
|
|
_ => throw new ArgumentException($"Type ({typeof(T).FullName}) does not ISimpleProcessingRule or IFullProcessingRule")
|
|
|
|
};
|
|
|
|
|
2022-06-25 17:31:21 +08:00
|
|
|
public static IProcessingPipelineBuilder AddDefaultRules(this IProcessingPipelineBuilder builder) =>
|
2021-02-08 16:51:19 +08:00
|
|
|
builder
|
2022-06-25 17:31:21 +08:00
|
|
|
.AddRule<HandleEndTagRule>()
|
|
|
|
.AddRule<HandleDelayedAudioHeaderRule>()
|
2022-07-21 00:06:18 +08:00
|
|
|
// .AddRule<UpdateTimestampOffsetRule>()
|
2022-06-25 17:31:21 +08:00
|
|
|
.AddRule<UpdateTimestampJumpRule>()
|
|
|
|
.AddRule<HandleNewScriptRule>()
|
|
|
|
.AddRule<HandleNewHeaderRule>()
|
|
|
|
.AddRule<RemoveDuplicatedChunkRule>()
|
2021-02-08 16:51:19 +08:00
|
|
|
;
|
|
|
|
|
2022-06-25 17:31:21 +08:00
|
|
|
public static IProcessingPipelineBuilder AddRemoveFillerDataRule(this IProcessingPipelineBuilder builder) =>
|
|
|
|
builder.AddRule<RemoveFillerDataRule>();
|
2021-02-08 16:51:19 +08:00
|
|
|
}
|
|
|
|
}
|