2021-04-20 20:41:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-04-29 23:51:06 +08:00
|
|
|
|
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-06-17 17:42:50 +08:00
|
|
|
|
private static readonly ProcessingComment COMMENT_JumpedWithinGOP = new ProcessingComment(CommentType.Unrepairable, true, "GOP 内音频或视频时间戳不连续");
|
|
|
|
|
private static readonly ProcessingComment COMMENT_CantSolve = new ProcessingComment(CommentType.Unrepairable, true, "出现了无法计算偏移量的音视频偏移");
|
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>
|
2021-04-29 23:51:06 +08:00
|
|
|
|
{
|
2022-05-02 00:01:41 +08:00
|
|
|
|
public static IsNextTimestampSmaller Instance;
|
2021-04-29 23:51:06 +08:00
|
|
|
|
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);
|
2021-04-29 23:51:06 +08:00
|
|
|
|
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
|
|
|
|
{
|
2021-04-22 22:40:40 +08:00
|
|
|
|
// 音频或视频自身就有问题,没救了
|
2022-07-24 21:44:58 +08:00
|
|
|
|
yield return new PipelineDisconnectAction("GOP 内音频或视频时间戳不连续");
|
2022-05-02 00:01:41 +08:00
|
|
|
|
context.AddComment(COMMENT_JumpedWithinGOP);
|
2021-04-20 20:41:26 +08:00
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
/*
|
|
|
|
|
* 设定做调整的为视频帧,参照每个视频帧左右(左为前、右为后)的音频帧的时间戳
|
|
|
|
|
* 计算出最多和最少能符合“不小于前面的帧并且不大于后面的帧”的要求的偏移量
|
|
|
|
|
* 如果当前偏移量比总偏移量要求更严,则使用当前偏移量范围作为总偏移量范围
|
|
|
|
|
* */
|
|
|
|
|
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
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
while (tags.Count > 0)
|
2021-04-20 20:41:26 +08:00
|
|
|
|
{
|
2021-04-29 23:51:06 +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;
|
|
|
|
|
}
|
2021-04-22 22:40:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
|
Tag? lastAudio = null;
|
|
|
|
|
var tags = new Stack<Tag>();
|
|
|
|
|
var maxOffset = int.MaxValue;
|
|
|
|
|
var minOffset = int.MinValue;
|
2021-04-22 22:40:40 +08:00
|
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
|
for (var i = 0; i < data.Tags.Count; i++)
|
2021-04-27 21:30:49 +08:00
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
var tag = data.Tags[i];
|
|
|
|
|
if (tag.Type == TagType.Audio)
|
2021-04-27 21:30:49 +08:00
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
ReduceOffsetRange(ref maxOffset, ref minOffset, lastAudio, tag, tags);
|
|
|
|
|
lastAudio = tag;
|
2021-04-27 21:30:49 +08:00
|
|
|
|
}
|
2021-04-29 23:51:06 +08:00
|
|
|
|
else if (tag.Type == TagType.Video)
|
2021-04-27 21:30:49 +08:00
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
tags.Push(tag);
|
2021-04-27 21:30:49 +08:00
|
|
|
|
}
|
2021-04-29 23:51:06 +08:00
|
|
|
|
else
|
|
|
|
|
throw new ArgumentException("unexpected tag type");
|
2021-04-27 21:30:49 +08:00
|
|
|
|
}
|
2021-04-29 23:51:06 +08:00
|
|
|
|
|
|
|
|
|
ReduceOffsetRange(ref maxOffset, ref minOffset, lastAudio, null, tags);
|
|
|
|
|
|
2022-01-12 18:20:56 +08:00
|
|
|
|
if (minOffset == maxOffset)
|
2021-04-29 23:51:06 +08:00
|
|
|
|
{ // 理想情况允许偏移范围只有一个值
|
|
|
|
|
offset = minOffset;
|
|
|
|
|
goto validOffset;
|
|
|
|
|
}
|
|
|
|
|
else if (minOffset < maxOffset)
|
|
|
|
|
{ // 允许偏移的值是一个范围
|
|
|
|
|
if (minOffset != int.MinValue)
|
2021-04-27 21:30:49 +08:00
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
if (maxOffset != int.MaxValue)
|
|
|
|
|
{ // 有一个有效范围,取平均值
|
|
|
|
|
offset = (int)(((long)minOffset + maxOffset) / 2L);
|
|
|
|
|
goto validOffset;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ // 无效最大偏移,以最小偏移为准
|
|
|
|
|
offset = minOffset + 1;
|
|
|
|
|
goto validOffset;
|
|
|
|
|
}
|
2021-04-27 21:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
if (maxOffset != int.MaxValue)
|
|
|
|
|
{ // 无效最小偏移,以最大偏移为准
|
|
|
|
|
offset = maxOffset - 1;
|
|
|
|
|
goto validOffset;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ // 无效结果
|
|
|
|
|
goto invalidOffset;
|
|
|
|
|
}
|
2021-04-27 21:30:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 23:51:06 +08:00
|
|
|
|
else
|
|
|
|
|
{ // 范围无效
|
|
|
|
|
goto invalidOffset;
|
2021-04-22 22:40:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
|
validOffset:
|
|
|
|
|
if (offset != 0)
|
2021-04-22 22:40:40 +08:00
|
|
|
|
{
|
2022-06-17 17:42:50 +08:00
|
|
|
|
context.AddComment(new ProcessingComment(CommentType.TimestampOffset, true, $"音视频时间戳偏移, D: {offset}"));
|
2021-04-29 23:51:06 +08:00
|
|
|
|
|
|
|
|
|
foreach (var tag in data.Tags)
|
|
|
|
|
if (tag.Type == TagType.Video)
|
|
|
|
|
tag.Timestamp += offset;
|
2021-04-22 22:40:40 +08:00
|
|
|
|
}
|
2021-04-29 23:51:06 +08:00
|
|
|
|
|
|
|
|
|
yield return data;
|
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
|
|
invalidOffset:
|
2022-05-02 00:01:41 +08:00
|
|
|
|
context.AddComment(COMMENT_CantSolve);
|
2022-07-24 21:44:58 +08:00
|
|
|
|
yield return new PipelineDisconnectAction("出现无法计算的音视频时间戳错位");
|
2021-04-29 23:51:06 +08:00
|
|
|
|
yield break;
|
2021-04-22 22:40:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 23:51:06 +08:00
|
|
|
|
else
|
|
|
|
|
yield return action;
|
2021-04-22 22:40:40 +08:00
|
|
|
|
}
|
2021-04-20 20:41:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|