BililiveRecorder/BililiveRecorder.Flv/Pipeline/Rules/UpdateTimestampOffsetRule.cs

169 lines
7.0 KiB
C#
Raw Normal View History

2021-04-20 20:41:26 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using BililiveRecorder.Flv.Pipeline.Actions;
using StructLinq;
2021-04-20 20:41:26 +08:00
namespace BililiveRecorder.Flv.Pipeline.Rules
{
2022-05-02 00:01:41 +08:00
/// <summary>
/// 修复时间戳错位
/// </summary>
2021-04-20 20:41:26 +08:00
public class UpdateTimestampOffsetRule : ISimpleProcessingRule
{
2022-05-02 00:01:41 +08:00
private static readonly ProcessingComment COMMENT_JumpedWithinGOP = new ProcessingComment(CommentType.Unrepairable, "GOP 内音频或视频时间戳不连续");
private static readonly ProcessingComment COMMENT_CantSolve = new ProcessingComment(CommentType.Unrepairable, "出现了无法计算偏移量的音视频偏移");
2021-04-20 20:41:26 +08:00
public void Run(FlvProcessingContext context, Action next)
{
context.PerActionRun(this.RunPerAction);
next();
}
2022-05-02 00:01:41 +08:00
/// <summary>
/// 检查 Tag 时间戳是否有变小的情况
/// </summary>
private readonly struct IsNextTimestampSmaller : ITwoInputFunction<Tag, bool>
{
2022-05-02 00:01:41 +08:00
public static IsNextTimestampSmaller Instance;
public bool Eval(Tag a, Tag b) => a.Timestamp > b.Timestamp;
}
2021-04-20 20:41:26 +08:00
private IEnumerable<PipelineAction?> RunPerAction(FlvProcessingContext context, PipelineAction action)
{
if (action is PipelineDataAction data)
{
2022-05-02 00:01:41 +08:00
var isNotNormal = data.Tags.Any2(ref IsNextTimestampSmaller.Instance);
if (!isNotNormal)
2021-04-20 20:41:26 +08:00
{
2022-05-02 00:01:41 +08:00
// 没有问题,每个 tag 的时间戳都 >= 上一个 tag 的时间戳。
2021-04-20 20:41:26 +08:00
yield return data;
yield break;
}
2022-05-02 00:01:41 +08:00
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))
2021-04-20 20:41:26 +08:00
{
// 音频或视频自身就有问题,没救了
yield return PipelineDisconnectAction.Instance;
2022-05-02 00:01:41 +08:00
context.AddComment(COMMENT_JumpedWithinGOP);
2021-04-20 20:41:26 +08:00
yield break;
}
else
{
/*
*
*
* 使
* */
var offset = 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void ReduceOffsetRange(ref int maxOffset, ref int minOffset, Tag? leftAudio, Tag? rightAudio, Stack<Tag> tags)
2021-04-20 20:41:26 +08:00
{
while (tags.Count > 0)
2021-04-20 20:41:26 +08:00
{
var video = tags.Pop();
if (leftAudio is not null)
{
var min = leftAudio.Timestamp - video.Timestamp;
if (minOffset < min)
minOffset = min;
}
if (rightAudio is not null)
{
var max = rightAudio.Timestamp - video.Timestamp;
if (maxOffset > max)
maxOffset = max;
}
}
}
Tag? lastAudio = null;
var tags = new Stack<Tag>();
var maxOffset = int.MaxValue;
var minOffset = int.MinValue;
for (var i = 0; i < data.Tags.Count; i++)
{
var tag = data.Tags[i];
if (tag.Type == TagType.Audio)
{
ReduceOffsetRange(ref maxOffset, ref minOffset, lastAudio, tag, tags);
lastAudio = tag;
}
else if (tag.Type == TagType.Video)
{
tags.Push(tag);
}
else
throw new ArgumentException("unexpected tag type");
}
ReduceOffsetRange(ref maxOffset, ref minOffset, lastAudio, null, tags);
if (minOffset == maxOffset)
{ // 理想情况允许偏移范围只有一个值
offset = minOffset;
goto validOffset;
}
else if (minOffset < maxOffset)
{ // 允许偏移的值是一个范围
if (minOffset != int.MinValue)
{
if (maxOffset != int.MaxValue)
{ // 有一个有效范围,取平均值
offset = (int)(((long)minOffset + maxOffset) / 2L);
goto validOffset;
}
else
{ // 无效最大偏移,以最小偏移为准
offset = minOffset + 1;
goto validOffset;
}
}
else
{
if (maxOffset != int.MaxValue)
{ // 无效最小偏移,以最大偏移为准
offset = maxOffset - 1;
goto validOffset;
}
else
{ // 无效结果
goto invalidOffset;
}
}
}
else
{ // 范围无效
goto invalidOffset;
}
validOffset:
if (offset != 0)
{
context.AddComment(new ProcessingComment(CommentType.TimestampOffset, $"音视频时间戳偏移, D: {offset}"));
foreach (var tag in data.Tags)
if (tag.Type == TagType.Video)
tag.Timestamp += offset;
}
yield return data;
yield break;
invalidOffset:
2022-05-02 00:01:41 +08:00
context.AddComment(COMMENT_CantSolve);
yield return PipelineDisconnectAction.Instance;
yield break;
}
}
else
yield return action;
}
2021-04-20 20:41:26 +08:00
}
}