Skip to content

Commit

Permalink
FLV: Refactor ProcessingComment
Browse files Browse the repository at this point in the history
  • Loading branch information
Genteure committed Jun 24, 2022
1 parent 97154b5 commit 46cde48
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 50 deletions.
2 changes: 2 additions & 0 deletions BililiveRecorder.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using BililiveRecorder.Core.Config;
using BililiveRecorder.Core.Config.V3;
using BililiveRecorder.DependencyInjection;
using BililiveRecorder.Flv.Pipeline;
using BililiveRecorder.ToolBox;
using BililiveRecorder.Web;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -409,6 +410,7 @@ private static Logger BuildLogger(LogEventLevel logLevel, LogEventLevel logFileL
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.Destructure.AsScalar<IPAddress>()
.Destructure.AsScalar<ProcessingComment>()
.Destructure.ByTransforming<Flv.Xml.XmlFlvFile.XmlFlvFileMeta>(x => new
{
x.Version,
Expand Down
4 changes: 2 additions & 2 deletions BililiveRecorder.Core/ProcessingRules/SplitRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal class SplitRule : ISimpleProcessingRule
private const int FLAG_BEFORE = 1;
private const int FLAG_AFTER = 2;

private static readonly ProcessingComment comment_before = new ProcessingComment(CommentType.Logging, "New file before data by split rule");
private static readonly ProcessingComment comment_after = new ProcessingComment(CommentType.Logging, "New file after data by split rule");
private static readonly ProcessingComment comment_before = new ProcessingComment(CommentType.Logging, false, "New file before data by split rule");
private static readonly ProcessingComment comment_after = new ProcessingComment(CommentType.Logging, false, "New file after data by split rule");

public void Run(FlvProcessingContext context, System.Action next)
{
Expand Down
18 changes: 12 additions & 6 deletions BililiveRecorder.Flv/Pipeline/ProcessingComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ namespace BililiveRecorder.Flv.Pipeline
{
public class ProcessingComment
{
public ProcessingComment(CommentType t, string c)
public ProcessingComment(CommentType type, bool actionRequired, string comment)
{
this.T = t;
this.C = c ?? throw new ArgumentNullException(nameof(c));
this.Type = type;
this.ActionRequired = actionRequired;
this.Comment = comment ?? throw new ArgumentNullException(nameof(comment));
}

/// <summary>
/// Type
/// </summary>
public CommentType T { get; }
public CommentType Type { get; }

/// <summary>
/// Action Required
/// </summary>
public bool ActionRequired { get; }

/// <summary>
/// Comment
/// </summary>
public string C { get; }
public string Comment { get; }

public override string ToString() => $"{this.T} {this.C}";
public override string ToString() => $"({this.Type},{(this.ActionRequired ? "A" : "C")}): {this.Comment}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace BililiveRecorder.Flv.Pipeline.Rules
/// </summary>
public class HandleDelayedAudioHeaderRule : ISimpleProcessingRule
{
private static readonly ProcessingComment comment1 = new ProcessingComment(CommentType.Unrepairable, "音频数据出现在音频头之前");
private static readonly ProcessingComment comment2 = new ProcessingComment(CommentType.DecodingHeader, "检测到延后收到的音频头");
private static readonly ProcessingComment comment1 = new ProcessingComment(CommentType.Unrepairable, true, "音频数据出现在音频头之前");
private static readonly ProcessingComment comment2 = new ProcessingComment(CommentType.DecodingHeader, true, "检测到延后收到的音频头");

public void Run(FlvProcessingContext context, Action next)
{
Expand Down
2 changes: 1 addition & 1 deletion BililiveRecorder.Flv/Pipeline/Rules/HandleEndTagRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BililiveRecorder.Flv.Pipeline.Rules
/// </summary>
public class HandleEndTagRule : ISimpleProcessingRule
{
private static readonly ProcessingComment comment = new ProcessingComment(CommentType.Logging, "因收到 End Tag 分段");
private static readonly ProcessingComment comment = new ProcessingComment(CommentType.Logging, false, "因收到 End Tag 分段");

public void Run(FlvProcessingContext context, Action next)
{
Expand Down
4 changes: 2 additions & 2 deletions BililiveRecorder.Flv/Pipeline/Rules/HandleNewHeaderRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class HandleNewHeaderRule : ISimpleProcessingRule
private const string VIDEO_HEADER_KEY = "HandleNewHeaderRule_VideoHeader";
private const string AUDIO_HEADER_KEY = "HandleNewHeaderRule_AudioHeader";

private static readonly ProcessingComment MultipleHeaderComment = new ProcessingComment(CommentType.DecodingHeader, "收到了连续多个 Header,新建文件");
private static readonly ProcessingComment SplitFileComment = new ProcessingComment(CommentType.DecodingHeader, "因为 Header 问题新建文件");
private static readonly ProcessingComment MultipleHeaderComment = new ProcessingComment(CommentType.DecodingHeader, true, "收到了连续多个 Header,新建文件");
private static readonly ProcessingComment SplitFileComment = new ProcessingComment(CommentType.DecodingHeader, true, "因为 Header 问题新建文件");

public void Run(FlvProcessingContext context, Action next)
{
Expand Down
6 changes: 3 additions & 3 deletions BililiveRecorder.Flv/Pipeline/Rules/HandleNewScriptRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class HandleNewScriptRule : ISimpleProcessingRule
{
private const string STORE_KEY = "HandleNewScriptRule_MetaDataReceived";
private const string onMetaData = "onMetaData";
private static readonly ProcessingComment comment_onmetadata = new ProcessingComment(CommentType.OnMetaData, "收到了 onMetaData");
private static readonly ProcessingComment comment_onmetadata = new ProcessingComment(CommentType.OnMetaData, false, "收到了 onMetaData");

public void Run(FlvProcessingContext context, Action next)
{
Expand Down Expand Up @@ -95,12 +95,12 @@ public void Run(FlvProcessingContext context, Action next)
{
// 记录信息,不输出到文件,不对文件进行分段。
var message = $"重复收到 onMetaData, onMetaData 内容: {data?.ToJson() ?? "(null)"}";
context.AddComment(new ProcessingComment(CommentType.OnMetaData, message));
context.AddComment(new ProcessingComment(CommentType.OnMetaData, false, message));
yield return new PipelineLogMessageWithLocationAction(Serilog.Events.LogEventLevel.Warning, "重复收到 onMetaData");
yield break;
}
notOnMetaData:
context.AddComment(new ProcessingComment(CommentType.Logging, "收到了非 onMetaData 的 Script Tag: " + (data?.ToJson() ?? "(null)")));
context.AddComment(new ProcessingComment(CommentType.Logging, false, "收到了非 onMetaData 的 Script Tag: " + (data?.ToJson() ?? "(null)")));
yield break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RemoveDuplicatedChunkRule : ISimpleProcessingRule
private const string QUEUE_KEY = "DeDuplicationQueue";

private static readonly FarmHash64 farmHash64 = new();
private static readonly ProcessingComment comment = new ProcessingComment(CommentType.RepeatingData, "重复数据");
private static readonly ProcessingComment comment = new ProcessingComment(CommentType.RepeatingData, true, "重复数据");

public void Run(FlvProcessingContext context, Action next)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public void Run(FlvProcessingContext context, Action next)
var diff = currentTimestamp - ts.LastOriginal;
if (diff < 0)
{
context.AddComment(new ProcessingComment(CommentType.TimestampJump, $"时间戳变小, curr: {currentTimestamp}, diff: {diff}"));
context.AddComment(new ProcessingComment(CommentType.TimestampJump, true, $"时间戳变小, curr: {currentTimestamp}, diff: {diff}"));
ts.CurrentOffset = currentTimestamp - ts.NextTimestampTarget;
}
else if (diff > JUMP_THRESHOLD)
{
context.AddComment(new ProcessingComment(CommentType.TimestampJump, $"时间戳间隔过大, curr: {currentTimestamp}, diff: {diff}"));
context.AddComment(new ProcessingComment(CommentType.TimestampJump, true, $"时间戳间隔过大, curr: {currentTimestamp}, diff: {diff}"));
ts.CurrentOffset = currentTimestamp - ts.NextTimestampTarget;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace BililiveRecorder.Flv.Pipeline.Rules
/// </summary>
public class UpdateTimestampOffsetRule : ISimpleProcessingRule
{
private static readonly ProcessingComment COMMENT_JumpedWithinGOP = new ProcessingComment(CommentType.Unrepairable, "GOP 内音频或视频时间戳不连续");
private static readonly ProcessingComment COMMENT_CantSolve = new ProcessingComment(CommentType.Unrepairable, "出现了无法计算偏移量的音视频偏移");
private static readonly ProcessingComment COMMENT_JumpedWithinGOP = new ProcessingComment(CommentType.Unrepairable, true, "GOP 内音频或视频时间戳不连续");
private static readonly ProcessingComment COMMENT_CantSolve = new ProcessingComment(CommentType.Unrepairable, true, "出现了无法计算偏移量的音视频偏移");

public void Run(FlvProcessingContext context, Action next)
{
Expand Down Expand Up @@ -145,7 +145,7 @@ static void ReduceOffsetRange(ref int maxOffset, ref int minOffset, Tag? leftAud
validOffset:
if (offset != 0)
{
context.AddComment(new ProcessingComment(CommentType.TimestampOffset, $"音视频时间戳偏移, D: {offset}"));
context.AddComment(new ProcessingComment(CommentType.TimestampOffset, true, $"音视频时间戳偏移, D: {offset}"));

foreach (var tag in data.Tags)
if (tag.Type == TagType.Video)
Expand Down
16 changes: 8 additions & 8 deletions BililiveRecorder.ToolBox/Tool/Analyze/AnalyzeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,25 @@ await Task.Run(async () =>
{
var (videoStats, audioStats) = statsRule.GetStats();

var countableComments = comments.Where(x => x.T != CommentType.Logging).ToArray();
var countableComments = comments.Where(x => x.Type != CommentType.Logging).ToArray();
return new AnalyzeResponse
{
InputPath = inputPath,

NeedFix = tagWriter.OutputFileCount != 1 || countableComments.Any(),
Unrepairable = countableComments.Any(x => x.T == CommentType.Unrepairable),
Unrepairable = countableComments.Any(x => x.Type == CommentType.Unrepairable),

OutputFileCount = tagWriter.OutputFileCount,

VideoStats = videoStats,
AudioStats = audioStats,

IssueTypeOther = countableComments.Count(x => x.T == CommentType.Other),
IssueTypeUnrepairable = countableComments.Count(x => x.T == CommentType.Unrepairable),
IssueTypeTimestampJump = countableComments.Count(x => x.T == CommentType.TimestampJump),
IssueTypeTimestampOffset = countableComments.Count(x => x.T == CommentType.TimestampOffset),
IssueTypeDecodingHeader = countableComments.Count(x => x.T == CommentType.DecodingHeader),
IssueTypeRepeatingData = countableComments.Count(x => x.T == CommentType.RepeatingData)
IssueTypeOther = countableComments.Count(x => x.Type == CommentType.Other),
IssueTypeUnrepairable = countableComments.Count(x => x.Type == CommentType.Unrepairable),
IssueTypeTimestampJump = countableComments.Count(x => x.Type == CommentType.TimestampJump),
IssueTypeTimestampOffset = countableComments.Count(x => x.Type == CommentType.TimestampOffset),
IssueTypeDecodingHeader = countableComments.Count(x => x.Type == CommentType.DecodingHeader),
IssueTypeRepeatingData = countableComments.Count(x => x.Type == CommentType.RepeatingData)
};
});

Expand Down
16 changes: 8 additions & 8 deletions BililiveRecorder.ToolBox/Tool/Fix/FixHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,25 @@ await Task.Run(() =>
{
var (videoStats, audioStats) = statsRule.GetStats();

var countableComments = comments.Where(x => x.T != CommentType.Logging).ToArray();
var countableComments = comments.Where(x => x.Type != CommentType.Logging).ToArray();
return new FixResponse
{
InputPath = inputPath,
OutputPaths = outputPaths.ToArray(),
OutputFileCount = outputPaths.Count,

NeedFix = outputPaths.Count != 1 || countableComments.Any(),
Unrepairable = countableComments.Any(x => x.T == CommentType.Unrepairable),
Unrepairable = countableComments.Any(x => x.Type == CommentType.Unrepairable),

VideoStats = videoStats,
AudioStats = audioStats,

IssueTypeOther = countableComments.Count(x => x.T == CommentType.Other),
IssueTypeUnrepairable = countableComments.Count(x => x.T == CommentType.Unrepairable),
IssueTypeTimestampJump = countableComments.Count(x => x.T == CommentType.TimestampJump),
IssueTypeTimestampOffset = countableComments.Count(x => x.T == CommentType.TimestampOffset),
IssueTypeDecodingHeader = countableComments.Count(x => x.T == CommentType.DecodingHeader),
IssueTypeRepeatingData = countableComments.Count(x => x.T == CommentType.RepeatingData)
IssueTypeOther = countableComments.Count(x => x.Type == CommentType.Other),
IssueTypeUnrepairable = countableComments.Count(x => x.Type == CommentType.Unrepairable),
IssueTypeTimestampJump = countableComments.Count(x => x.Type == CommentType.TimestampJump),
IssueTypeTimestampOffset = countableComments.Count(x => x.Type == CommentType.TimestampOffset),
IssueTypeDecodingHeader = countableComments.Count(x => x.Type == CommentType.DecodingHeader),
IssueTypeRepeatingData = countableComments.Count(x => x.Type == CommentType.RepeatingData)
};
});

Expand Down
2 changes: 2 additions & 0 deletions BililiveRecorder.WPF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using BililiveRecorder.Flv.Pipeline;
using BililiveRecorder.ToolBox;
using Esprima;
using Jint.Runtime;
Expand Down Expand Up @@ -225,6 +226,7 @@ private static Logger BuildLogger()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.Destructure.AsScalar<IPAddress>()
.Destructure.AsScalar<ProcessingComment>()
.Destructure.ByTransforming<Flv.Xml.XmlFlvFile.XmlFlvFileMeta>(x => new
{
x.Version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,10 @@ namespace BililiveRecorder.Flv.Pipeline
}
public class ProcessingComment
{
public ProcessingComment(BililiveRecorder.Flv.Pipeline.CommentType t, string c) { }
public string C { get; }
public BililiveRecorder.Flv.Pipeline.CommentType T { get; }
public ProcessingComment(BililiveRecorder.Flv.Pipeline.CommentType type, bool actionRequired, string comment) { }
public bool ActionRequired { get; }
public string Comment { get; }
public BililiveRecorder.Flv.Pipeline.CommentType Type { get; }
public override string ToString() { }
}
public delegate void ProcessingDelegate(BililiveRecorder.Flv.Pipeline.FlvProcessingContext context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace BililiveRecorder.Flv.Tests.RuleTests
[ExpectationPath("Bad")]
public class IntegratedBadTests : IntegratedTestBase
{
[Theory(Skip = "魔改版,不测试")]
[Theory()]
[Expectation("TestBadSamples")]
[SampleFileTestData("../data/flv/TestData/Bad", "*.xml")]
public async Task TestBadSamples(string path)
Expand All @@ -35,12 +35,12 @@ public async Task TestBadSamples(string path)
await RunPipeline(reader, flvTagListWriter, comments).ConfigureAwait(false);

// Assert
comments.RemoveAll(x => x.T == CommentType.Logging);
comments.RemoveAll(x => !x.ActionRequired);

var outputResult = new OutputResult
{
AlternativeHeaders = flvTagListWriter.AlternativeHeaders.Select(x => x.BinaryDataForSerializationUseOnly).ToArray(),
Comments = comments.GroupBy(x => x.T).Select(x => new CommentCount(x.Key, x.Count())).ToArray(),
Comments = comments.GroupBy(x => x.Type).Select(x => new CommentCount(x.Key, x.Count())).ToArray(),
TagCounts = flvTagListWriter.Files.Select(x => x.Count).ToArray()
};

Expand Down
10 changes: 5 additions & 5 deletions test/BililiveRecorder.Flv.Tests/RuleTests/IntegratedGoodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace BililiveRecorder.Flv.Tests.RuleTests
[ExpectationPath("Good")]
public class IntegratedGoodTests : IntegratedTestBase
{
[Theory(Skip = "魔改版,不测试")]
[Theory()]
[Expectation("StandardTest")]
[SampleFileTestData("../data/flv/TestData/Good", "*.xml")]
public async Task StrictTestsAsync(string path)
Expand All @@ -29,7 +29,7 @@ public async Task StrictTestsAsync(string path)
await RunPipeline(reader, flvTagListWriter, comments).ConfigureAwait(false);

// Assert
comments.RemoveAll(x => x.T == CommentType.Logging);
comments.RemoveAll(x => !x.ActionRequired);

Assert.Empty(comments);

Expand All @@ -49,7 +49,7 @@ public async Task StrictTestsAsync(string path)
await Verifier.Verify(xmlStr).UseExtension("xml").UseParameters(path);
}

[Theory(Skip = "魔改版,不测试")]
[Theory()]
[Expectation("WithOffsetTest")]
[SampleFileTestData("../data/flv/TestData/Good", "*.xml")]
public async Task StrictWithArtificalOffsetTestsAsync(string path)
Expand All @@ -74,8 +74,8 @@ public async Task StrictWithArtificalOffsetTestsAsync(string path)
await RunPipeline(reader, output, comments).ConfigureAwait(false);

// Assert
comments.RemoveAll(x => x.T == CommentType.Logging);
Assert.Equal(CommentType.TimestampJump, Assert.Single(comments).T);
comments.RemoveAll(x => !x.ActionRequired);
Assert.Equal(CommentType.TimestampJump, Assert.Single(comments).Type);

Assert.Empty(output.AlternativeHeaders);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected static async Task AssertTagsByRerunPipeline(List<Tag> tags)
await RunPipeline(reader, output, comments).ConfigureAwait(false);

// 忽略 ignore Logging
comments.RemoveAll(x => x.T == CommentType.Logging);
comments.RemoveAll(x => !x.ActionRequired);
// 不应该有任何问题 Shouldn't have any problems
Assert.Empty(comments);
// 不应该有多个 Header Shouldn't have multiple headers
Expand Down

0 comments on commit 46cde48

Please sign in to comment.