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
|
|
|
|
|
{
|
|
|
|
|
public class UpdateTimestampOffsetRule : ISimpleProcessingRule
|
|
|
|
|
{
|
|
|
|
|
private static readonly ProcessingComment comment1 = new ProcessingComment(CommentType.Unrepairable, "GOP 内音频或视频时间戳不连续");
|
2021-04-22 22:40:40 +08:00
|
|
|
|
private static readonly ProcessingComment comment2 = new ProcessingComment(CommentType.Unrepairable, "出现了无法计算偏移量的音视频偏移");
|
2021-04-20 20:41:26 +08:00
|
|
|
|
|
|
|
|
|
public void Run(FlvProcessingContext context, Action next)
|
|
|
|
|
{
|
|
|
|
|
context.PerActionRun(this.RunPerAction);
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
|
private readonly struct IsNotNormal : ITwoInputFunction<Tag, bool>
|
|
|
|
|
{
|
|
|
|
|
public static IsNotNormal 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)
|
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
var isNotNormal = data.Tags.Any2(ref IsNotNormal.Instance);
|
|
|
|
|
if (!isNotNormal)
|
2021-04-20 20:41:26 +08:00
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
// 没有问题
|
2021-04-20 20:41:26 +08:00
|
|
|
|
yield return data;
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 23:51:06 +08:00
|
|
|
|
if (data.Tags.Where(x => x.Type == TagType.Audio).Any2(ref IsNotNormal.Instance) || data.Tags.Where(x => x.Type == TagType.Video).Any2(ref IsNotNormal.Instance))
|
2021-04-20 20:41:26 +08:00
|
|
|
|
{
|
2021-04-22 22:40:40 +08:00
|
|
|
|
// 音频或视频自身就有问题,没救了
|
|
|
|
|
yield return PipelineDisconnectAction.Instance;
|
2021-04-20 20:41:26 +08:00
|
|
|
|
context.AddComment(comment1);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (minOffset == maxOffset)
|
|
|
|
|
{ // 理想情况允许偏移范围只有一个值
|
|
|
|
|
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
|
|
|
|
{
|
2021-04-29 23:51:06 +08:00
|
|
|
|
context.AddComment(new ProcessingComment(CommentType.TimestampOffset, $"音视频时间戳偏移, D: {offset}"));
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
context.AddComment(comment2);
|
|
|
|
|
yield return PipelineDisconnectAction.Instance;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|