Skip to content

Commit

Permalink
FLV: Disconnect stream when continuous duplicated data is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
Genteure committed Jul 24, 2022
1 parent fae7000 commit f9f0dec
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
4 changes: 2 additions & 2 deletions BililiveRecorder.Core/Recording/StandardRecordTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ private async Task RecordingLoopAsync()
}
this.ioDiskStopwatch.Reset();

if (this.context.Actions.Any(x => x is PipelineDisconnectAction))
if (this.context.Actions.FirstOrDefault(x => x is PipelineDisconnectAction) is PipelineDisconnectAction disconnectAction)
{
this.logger.Information("根据修复逻辑的要求结束录制");
this.logger.Information("修复系统断开录制:{Reason}", disconnectAction.Reason);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ namespace BililiveRecorder.Flv.Pipeline.Actions
{
public class PipelineDisconnectAction : PipelineAction
{
public static readonly PipelineDisconnectAction Instance = new PipelineDisconnectAction();
public string Reason { get; set; } = string.Empty;

public override PipelineAction Clone() => Instance;
public PipelineDisconnectAction(string reason)
{
this.Reason = reason;
}

public override PipelineAction Clone() => new PipelineDisconnectAction(reason: this.Reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Run(FlvProcessingContext context, Action next)
if (shouldReportError)
{
context.AddComment(comment1);
yield return PipelineDisconnectAction.Instance;
yield return new PipelineDisconnectAction("直播音频数据中间出现音频头");
yield return PipelineNewFileAction.Instance;
yield return null;
yield break;
Expand Down
25 changes: 25 additions & 0 deletions BililiveRecorder.Flv/Pipeline/Rules/RemoveDuplicatedChunkRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class RemoveDuplicatedChunkRule : ISimpleProcessingRule
{
private const int MAX_HISTORY = 16;
private const string QUEUE_KEY = "DeDuplicationQueue";
private const string DUPLICATED_COUNT_KEY = "DuplicatedFlvDataCount";

private static readonly FarmHash64 farmHash64 = new();
private static readonly ProcessingComment comment = new ProcessingComment(CommentType.RepeatingData, true, "重复数据");
Expand Down Expand Up @@ -75,6 +76,27 @@ public void Run(FlvProcessingContext context, Action next)
{
// 重复数据
context.AddComment(comment);

// 判断连续收到的重复数据数量
if (context.SessionItems.ContainsKey(DUPLICATED_COUNT_KEY) && context.SessionItems[DUPLICATED_COUNT_KEY] is int count)
{
count += 1;
}
else
{
count = 1;
}

const int DisconnectOnDuplicatedDataCount = 10;
if (count > DisconnectOnDuplicatedDataCount)
{
yield return new PipelineDisconnectAction($"连续收到了 {DisconnectOnDuplicatedDataCount} 段重复数据");
context.SessionItems.Remove(DUPLICATED_COUNT_KEY);
}
else
{
context.SessionItems[DUPLICATED_COUNT_KEY] = count;
}
}
else
{
Expand All @@ -84,11 +106,14 @@ public void Run(FlvProcessingContext context, Action next)
while (hashHistory.Count > MAX_HISTORY)
hashHistory.Dequeue();

context.SessionItems.Remove(DUPLICATED_COUNT_KEY);
yield return action;
}
}
else
{
yield return action;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Run(FlvProcessingContext context, Action next)
if (data.Tags.Where(x => x.Type == TagType.Audio).Any2(ref IsNextTimestampSmaller.Instance) || data.Tags.Where(x => x.Type == TagType.Video).Any2(ref IsNextTimestampSmaller.Instance))
{
// 音频或视频自身就有问题,没救了
yield return PipelineDisconnectAction.Instance;
yield return new PipelineDisconnectAction("GOP 内音频或视频时间戳不连续");
context.AddComment(COMMENT_JumpedWithinGOP);
yield break;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ static void ReduceOffsetRange(ref int maxOffset, ref int minOffset, Tag? leftAud

invalidOffset:
context.AddComment(COMMENT_CantSolve);
yield return PipelineDisconnectAction.Instance;
yield return new PipelineDisconnectAction("出现无法计算的音视频时间戳错位");
yield break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,8 @@ namespace BililiveRecorder.Flv.Pipeline.Actions
}
public class PipelineDisconnectAction : BililiveRecorder.Flv.Pipeline.Actions.PipelineAction
{
public static readonly BililiveRecorder.Flv.Pipeline.Actions.PipelineDisconnectAction Instance;
public PipelineDisconnectAction() { }
public PipelineDisconnectAction(string reason) { }
public string Reason { get; set; }
public override BililiveRecorder.Flv.Pipeline.Actions.PipelineAction Clone() { }
}
public class PipelineEndAction : BililiveRecorder.Flv.Pipeline.Actions.PipelineAction
Expand Down

0 comments on commit f9f0dec

Please sign in to comment.