From d0c7863c9d1d640a4761d84a83f86816437539d1 Mon Sep 17 00:00:00 2001 From: genteure Date: Fri, 24 Jun 2022 21:29:10 +0800 Subject: [PATCH] Fix FFmpeg detection rule --- .../Pipeline/Rules/FfmpegDetectionRule.cs | 31 ++++++++++++++ .../ProcessingRules/FfmpegDetectionRule.cs | 41 ------------------- .../Tool/Analyze/AnalyzeHandler.cs | 1 + .../Tool/Fix/FixHandler.cs | 1 + .../PublicApi.HasNoChangesAsync.verified.txt | 7 ++++ .../RuleTests/IntegratedTestBase.cs | 3 +- 6 files changed, 42 insertions(+), 42 deletions(-) create mode 100644 BililiveRecorder.Flv/Pipeline/Rules/FfmpegDetectionRule.cs delete mode 100644 BililiveRecorder.ToolBox/ProcessingRules/FfmpegDetectionRule.cs diff --git a/BililiveRecorder.Flv/Pipeline/Rules/FfmpegDetectionRule.cs b/BililiveRecorder.Flv/Pipeline/Rules/FfmpegDetectionRule.cs new file mode 100644 index 0000000..2179ecd --- /dev/null +++ b/BililiveRecorder.Flv/Pipeline/Rules/FfmpegDetectionRule.cs @@ -0,0 +1,31 @@ +using System; +using System.Linq; +using BililiveRecorder.Flv.Amf; +using BililiveRecorder.Flv.Pipeline.Actions; + +namespace BililiveRecorder.Flv.Pipeline.Rules +{ + public class FfmpegDetectionRule : ISimpleProcessingRule + { + public bool EndTagDetected { get; private set; } + public bool LavfEncoderDetected { get; private set; } + + public void Run(FlvProcessingContext context, Action next) + { + if (!this.EndTagDetected && context.Actions.Any(x => x is PipelineEndAction)) + this.EndTagDetected = true; + + if (!this.LavfEncoderDetected && context.Actions.Any(action => + { + if (action is PipelineScriptAction scriptAction + && (scriptAction?.Tag?.ScriptData?.GetMetadataValue()?.Value?.TryGetValue("encoder", out var encoderValue) ?? false) + && encoderValue is ScriptDataString encoder) + return encoder.Value.StartsWith("Lavf", StringComparison.Ordinal); + return false; + })) + this.LavfEncoderDetected = true; + + next(); + } + } +} diff --git a/BililiveRecorder.ToolBox/ProcessingRules/FfmpegDetectionRule.cs b/BililiveRecorder.ToolBox/ProcessingRules/FfmpegDetectionRule.cs deleted file mode 100644 index 1507a3e..0000000 --- a/BililiveRecorder.ToolBox/ProcessingRules/FfmpegDetectionRule.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Linq; -using BililiveRecorder.Flv.Amf; -using BililiveRecorder.Flv.Pipeline; -using BililiveRecorder.Flv.Pipeline.Actions; - -namespace BililiveRecorder.ToolBox.ProcessingRules -{ - public class FfmpegDetectionRule : ISimpleProcessingRule - { - public bool EndTagDetected { get; private set; } - public bool LavfEncoderDetected { get; private set; } - - public void Run(FlvProcessingContext context, Action next) - { - if (!this.EndTagDetected && context.Actions.Any(x => x is PipelineEndAction)) - { - this.EndTagDetected = true; - } - - if (!this.LavfEncoderDetected) - { - if (context.Actions.Any(action => - { - if (action is PipelineScriptAction scriptAction) - { - var encoder = scriptAction?.Tag?.ScriptData?.GetMetadataValue()?.Value?["encoder"] as ScriptDataString; - if (encoder is not null) - { - return encoder.Value.StartsWith("Lavf", StringComparison.Ordinal); - } - } - return false; - })) - { - this.LavfEncoderDetected = true; - } - } - } - } -} diff --git a/BililiveRecorder.ToolBox/Tool/Analyze/AnalyzeHandler.cs b/BililiveRecorder.ToolBox/Tool/Analyze/AnalyzeHandler.cs index 2d2b51b..cf57104 100644 --- a/BililiveRecorder.ToolBox/Tool/Analyze/AnalyzeHandler.cs +++ b/BililiveRecorder.ToolBox/Tool/Analyze/AnalyzeHandler.cs @@ -13,6 +13,7 @@ using BililiveRecorder.Flv.Grouping; using BililiveRecorder.Flv.Parser; using BililiveRecorder.Flv.Pipeline; using BililiveRecorder.Flv.Pipeline.Actions; +using BililiveRecorder.Flv.Pipeline.Rules; using BililiveRecorder.Flv.Writer; using BililiveRecorder.Flv.Xml; using BililiveRecorder.ToolBox.ProcessingRules; diff --git a/BililiveRecorder.ToolBox/Tool/Fix/FixHandler.cs b/BililiveRecorder.ToolBox/Tool/Fix/FixHandler.cs index a1989d1..5b1b4ad 100644 --- a/BililiveRecorder.ToolBox/Tool/Fix/FixHandler.cs +++ b/BililiveRecorder.ToolBox/Tool/Fix/FixHandler.cs @@ -13,6 +13,7 @@ using BililiveRecorder.Flv.Grouping; using BililiveRecorder.Flv.Parser; using BililiveRecorder.Flv.Pipeline; using BililiveRecorder.Flv.Pipeline.Actions; +using BililiveRecorder.Flv.Pipeline.Rules; using BililiveRecorder.Flv.Writer; using BililiveRecorder.Flv.Xml; using BililiveRecorder.ToolBox.ProcessingRules; diff --git a/test/BililiveRecorder.Flv.Tests/Expectations/PublicApi.HasNoChangesAsync.verified.txt b/test/BililiveRecorder.Flv.Tests/Expectations/PublicApi.HasNoChangesAsync.verified.txt index 44e28db..ce0332d 100644 --- a/test/BililiveRecorder.Flv.Tests/Expectations/PublicApi.HasNoChangesAsync.verified.txt +++ b/test/BililiveRecorder.Flv.Tests/Expectations/PublicApi.HasNoChangesAsync.verified.txt @@ -762,6 +762,13 @@ namespace BililiveRecorder.Flv.Pipeline } namespace BililiveRecorder.Flv.Pipeline.Rules { + public class FfmpegDetectionRule : BililiveRecorder.Flv.Pipeline.IProcessingRule, BililiveRecorder.Flv.Pipeline.ISimpleProcessingRule + { + public FfmpegDetectionRule() { } + public bool EndTagDetected { get; } + public bool LavfEncoderDetected { get; } + public void Run(BililiveRecorder.Flv.Pipeline.FlvProcessingContext context, System.Action next) { } + } public class HandleDelayedAudioHeaderRule : BililiveRecorder.Flv.Pipeline.IProcessingRule, BililiveRecorder.Flv.Pipeline.ISimpleProcessingRule { public HandleDelayedAudioHeaderRule() { } diff --git a/test/BililiveRecorder.Flv.Tests/RuleTests/IntegratedTestBase.cs b/test/BililiveRecorder.Flv.Tests/RuleTests/IntegratedTestBase.cs index b5e105a..a892152 100644 --- a/test/BililiveRecorder.Flv.Tests/RuleTests/IntegratedTestBase.cs +++ b/test/BililiveRecorder.Flv.Tests/RuleTests/IntegratedTestBase.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using BililiveRecorder.Flv.Grouping; using BililiveRecorder.Flv.Pipeline; +using BililiveRecorder.Flv.Pipeline.Rules; using BililiveRecorder.Flv.Writer; using BililiveRecorder.Flv.Xml; using Microsoft.Extensions.DependencyInjection; @@ -17,7 +18,7 @@ namespace BililiveRecorder.Flv.Tests.RuleTests var writer = new FlvProcessingContextWriter(tagWriter: output, allowMissingHeader: true, disableKeyframes: true, logger: null); var session = new Dictionary(); var context = new FlvProcessingContext(); - var pipeline = new ProcessingPipelineBuilder(new ServiceCollection().BuildServiceProvider()).AddDefault().AddRemoveFillerData().Build(); + var pipeline = new ProcessingPipelineBuilder(new ServiceCollection().BuildServiceProvider()).Add().AddDefault().AddRemoveFillerData().Build(); while (true) {