FLV: UpdateTimestampOffsetRule is now functional

This commit is contained in:
Genteure 2021-04-22 22:40:40 +08:00
parent 5f4c9633bd
commit afd8f7a2d3
13 changed files with 259 additions and 148 deletions

View File

@ -4,16 +4,20 @@ using Microsoft.IO;
namespace BililiveRecorder.Core
{
public class RecyclableMemoryStreamProvider : IMemoryStreamProvider
internal class RecyclableMemoryStreamProvider : IMemoryStreamProvider
{
private readonly RecyclableMemoryStreamManager manager = new RecyclableMemoryStreamManager(32 * 1024, 64 * 1024, 64 * 1024 * 32)
{
MaximumFreeSmallPoolBytes = 64 * 1024 * 1024,
MaximumFreeLargePoolBytes = 64 * 1024 * 32,
};
private readonly RecyclableMemoryStreamManager manager;
public RecyclableMemoryStreamProvider()
{
const int K = 1024;
const int M = K * K;
this.manager = new RecyclableMemoryStreamManager(32 * K, 64 * K, 64 * K * 32)
{
MaximumFreeSmallPoolBytes = 32 * M,
MaximumFreeLargePoolBytes = 64 * K * 32,
};
//manager.StreamFinalized += () =>
//{
// Debug.WriteLine("TestRecyclableMemoryStreamProvider: Stream Finalized");

View File

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BililiveRecorder.Flv.Pipeline;
@ -11,54 +10,26 @@ namespace BililiveRecorder.Flv.Grouping.Rules
public bool AppendWith(Tag tag, LinkedList<Tag> tags, out LinkedList<Tag>? leftover)
{
var flag = tag.IsNonKeyframeData()
|| (tag.IsKeyframeData() && tags.All(x => x.IsNonKeyframeData()))
var shouldAppend =
// Tag 是非关键帧数据
tag.IsNonKeyframeData()
// 或是音频头,并且之前未出现过音频数据
|| (tag.Type == TagType.Audio && tag.Flag == TagFlag.Header && tags.All(x => x.Type != TagType.Audio || x.Flag == TagFlag.Header));
// || (tag.IsKeyframeData() && tags.All(x => x.IsNonKeyframeData()))
if (flag)
if (shouldAppend)
{
tags.AddLast(tag);
leftover = null;
return true;
}
else
{
var ts = tag.Timestamp;
var lastAudio = tags.LastOrDefault(x => x.Type == TagType.Audio);
bool predicate(Tag x) => x.Type == TagType.Audio && x.Timestamp >= ts;
if (tag.IsKeyframeData() && lastAudio is not null && Math.Abs(tag.Timestamp - lastAudio.Timestamp) <= 50 && tags.Any(predicate))
{
{
leftover = new LinkedList<Tag>();
foreach (var item in tags.Where(predicate))
leftover.AddLast(item);
leftover.AddLast(tag);
}
// tags.RemoveAll(predicate);
{
var node = tags.First;
while (node != null)
{
var next = node.Next;
if (predicate(node.Value))
tags.Remove(node);
node = next;
}
}
return false;
}
else
{
leftover = new LinkedList<Tag>();
leftover.AddLast(tag);
return false;
}
}
}
public PipelineAction CreatePipelineAction(LinkedList<Tag> tags) => new PipelineDataAction(new List<Tag>(tags));
}

View File

@ -6,9 +6,8 @@ namespace BililiveRecorder.Flv.Pipeline.Rules
{
public class UpdateTimestampOffsetRule : ISimpleProcessingRule
{
private const int MAX_ALLOWED_DIFF = 1000 * 10; // 10 seconds
private static readonly ProcessingComment comment1 = new ProcessingComment(CommentType.Unrepairable, "GOP 内音频或视频时间戳不连续");
private static readonly ProcessingComment comment2 = new ProcessingComment(CommentType.Unrepairable, "出现了无法计算偏移量的音视频偏移");
public void Run(FlvProcessingContext context, Action next)
{
@ -29,39 +28,121 @@ namespace BililiveRecorder.Flv.Pipeline.Rules
yield break;
}
// 这个问题可能不能稳定修复,如果是在录直播最好还是断开重连,获取正常的直播流
// TODO 确认修复效果
yield return PipelineDisconnectAction.Instance;
if (!(this.CheckIfNormal(data.Tags.Where(x => x.Type == TagType.Audio)) && this.CheckIfNormal(data.Tags.Where(x => x.Type == TagType.Video))))
{
// 音频或视频自身就有问题,没救了
yield return PipelineDisconnectAction.Instance;
context.AddComment(comment1);
yield break;
}
else
{
var audio = data.Tags.First(x => x.Type == TagType.Audio);
var video = data.Tags.First(x => x.Type == TagType.Video);
var oc = new OffsetCalculator();
var diff = audio.Timestamp - video.Timestamp;
foreach (var tag in data.Tags)
oc.AddTag(tag);
if (diff > 50)
if (oc.Calculate(out var videoOffset))
{
context.AddComment(new ProcessingComment(CommentType.TimestampOffset, $"音视频时间戳偏移, A: {audio.Timestamp}, V: {video.Timestamp}, D: {diff}"));
foreach (var tag in data.Tags.Where(x => x.Type == TagType.Audio))
if (videoOffset != 0)
{
tag.Timestamp -= diff;
}
}
context.AddComment(new ProcessingComment(CommentType.TimestampOffset, $"音视频时间戳偏移, D: {videoOffset}"));
// 因为上面已经检查了音频或视频单独不存在时间戳跳变问题,所以可以进行排序
data.Tags = data.Tags.OrderBy(x => x.Timestamp).ToList();
foreach (var tag in data.Tags)
if (tag.Type == TagType.Video)
tag.Timestamp += videoOffset;
}
yield return data;
yield break;
}
else
{
context.AddComment(comment2);
yield return PipelineDisconnectAction.Instance;
yield break;
}
}
}
else
yield return action;
}
/// <summary>
/// 音视频偏差量计算
/// </summary>
private class OffsetCalculator
{
/*
*
*
* 使
* */
private Tag? lastAudio = null;
private readonly Stack<Tag> tags = new Stack<Tag>();
private int maxOffset = int.MaxValue;
private int minOffset = int.MinValue;
public void AddTag(Tag tag)
{
if (tag.Type == TagType.Audio)
{
this.ReduceOffsetRange(this.lastAudio, tag);
this.lastAudio = tag;
}
else if (tag.Type == TagType.Video)
{
this.tags.Push(tag);
}
else
throw new ArgumentException("unexpected tag type");
}
public bool Calculate(out int offset)
{
this.ReduceOffsetRange(this.lastAudio, null);
this.lastAudio = null;
if (this.minOffset == this.maxOffset)
{
offset = this.minOffset;
return true;
}
else if (this.minOffset <= this.maxOffset)
{
offset = (this.minOffset + this.maxOffset) / 2;
return true;
}
else
{
offset = 0;
return false;
}
}
private void ReduceOffsetRange(Tag? leftAudio, Tag? rightAudio)
{
while (this.tags.Count > 0)
{
var video = this.tags.Pop();
if (leftAudio is not null)
{
var min = leftAudio.Timestamp - video.Timestamp;
if (this.minOffset < min)
this.minOffset = min;
}
if (rightAudio is not null)
{
var max = rightAudio.Timestamp - video.Timestamp;
if (this.maxOffset > max)
this.maxOffset = max;
}
}
}
}
}
}

View File

@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="1.4.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
</ItemGroup>

View File

@ -50,7 +50,7 @@ namespace BililiveRecorder.ToolBox.Commands
FileStream? flvFileStream = null;
try
{
var memoryStreamProvider = new DefaultMemoryStreamProvider();
var memoryStreamProvider = new RecyclableMemoryStreamProvider();
var comments = new List<ProcessingComment>();
var context = new FlvProcessingContext();
var session = new Dictionary<object, object?>();

View File

@ -65,7 +65,8 @@ namespace BililiveRecorder.ToolBox.Commands
{
var count = 0;
var tags = new List<Tag>();
using var reader = new FlvTagPipeReader(PipeReader.Create(inputStream), new DefaultMemoryStreamProvider(), skipData: true, logger: logger);
var memoryStreamProvider = new RecyclableMemoryStreamProvider();
using var reader = new FlvTagPipeReader(PipeReader.Create(inputStream), memoryStreamProvider, skipData: true, logger: logger);
while (true)
{
var tag = await reader.ReadTagAsync(default).ConfigureAwait(false);

View File

@ -53,7 +53,7 @@ namespace BililiveRecorder.ToolBox.Commands
FileStream? flvFileStream = null;
try
{
var memoryStreamProvider = new DefaultMemoryStreamProvider();
var memoryStreamProvider = new RecyclableMemoryStreamProvider();
var comments = new List<ProcessingComment>();
var context = new FlvProcessingContext();
var session = new Dictionary<object, object?>();

View File

@ -0,0 +1,33 @@
using System.IO;
using BililiveRecorder.Flv;
using Microsoft.IO;
namespace BililiveRecorder.ToolBox
{
internal class RecyclableMemoryStreamProvider : IMemoryStreamProvider
{
private readonly RecyclableMemoryStreamManager manager;
public RecyclableMemoryStreamProvider()
{
const int K = 1024;
const int M = K * K;
this.manager = new RecyclableMemoryStreamManager(32 * K, 64 * K, 64 * K * 32)
{
MaximumFreeSmallPoolBytes = 32 * M,
MaximumFreeLargePoolBytes = 64 * K * 32,
};
//manager.StreamFinalized += () =>
//{
// Debug.WriteLine("TestRecyclableMemoryStreamProvider: Stream Finalized");
//};
//manager.StreamDisposed += () =>
//{
// // Debug.WriteLine("TestRecyclableMemoryStreamProvider: Stream Disposed");
//};
}
public Stream CreateMemoryStream(string tag) => this.manager.GetStream(tag);
}
}

View File

@ -16,7 +16,8 @@
mc:Ignorable="d" DataContext="{x:Null}"
d:DesignHeight="600" d:DesignWidth="900"
Title="ToolboxAutoFixPage">
<Grid Margin="20">
<Border Background="Transparent" AllowDrop="True" Drop="FileNameTextBox_Drop">
<Grid Margin="20" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
@ -56,7 +57,7 @@
</StackPanel>
</Button>
<Button VerticalAlignment="Bottom" DockPanel.Dock="Right" Margin="0,0,5,0" Content="选择..." Click="SelectFile_Button_Click"/>
<TextBox ui:ControlHelper.PlaceholderText="FLV 文件" ui:TextBoxHelper.IsDeleteButtonVisible="False" x:Name="FileNameTextBox"/>
<TextBox ui:ControlHelper.PlaceholderText="FLV 文件" ui:TextBoxHelper.IsDeleteButtonVisible="False" x:Name="FileNameTextBox" />
</DockPanel>
<Border Grid.Row="1" BorderThickness="1" CornerRadius="5" x:Name="analyzeResultDisplayArea" DataContext="{x:Null}"
Background="{DynamicResource SystemControlBackgroundAltHighBrush}"
@ -84,7 +85,7 @@
<StackPanel Grid.Row="2" HorizontalAlignment="Center"
Visibility="{Binding Unrepairable,Converter={StaticResource BooleanToVisibilityCollapsedConverter}}">
<TextBlock HorizontalAlignment="Center" FontSize="20" Text="文件内存在录播姬无法自动修复的问题" Foreground="Red"/>
<TextBlock HorizontalAlignment="Center" Text="请点击“修复失败?”按钮并反馈本问题"/>
<TextBlock HorizontalAlignment="Center" Text="请点击 修复失败? 按钮并反馈本问题"/>
</StackPanel>
<StackPanel Grid.Row="3" HorizontalAlignment="Center" Margin="10">
<TextBlock Text="{Binding OutputFileCount,StringFormat=修复将会输出 {0} 个文件}" Margin="0,0,0,5"/>
@ -102,4 +103,5 @@
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource SelectorTemplate}"/>
</Border>
</Grid>
</Border>
</ui:Page>

View File

@ -275,5 +275,19 @@ namespace BililiveRecorder.WPF.Pages
catch (Exception) { }
}
}
private void FileNameTextBox_Drop(object sender, DragEventArgs e)
{
try
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
this.FileNameTextBox.Text = files[0];
}
}
catch (Exception)
{ }
}
}
}

View File

@ -104,7 +104,7 @@ namespace BililiveRecorder.Flv.RuleTests.Integrated
Assert.Equal(0, tags[2].Timestamp);
Assert.Equal(TagFlag.Header, tags[2].Flag);
Assert.Equal(0, tags[3].Timestamp);
Assert.InRange(tags[3].Timestamp, 0, 50);
}
protected void AssertTagsAlmostEqual(List<Tag> expected, List<Tag> actual)

View File

@ -1,4 +1,7 @@
{
"AllowedComments": {
"TimestampOffset": 105
},
"Files": [
{
"VideoHeaderData": "17000000000164001F030100176764001FACB402802DD0800000030080000018478C195001000468EF3CB0",

View File

@ -2,6 +2,7 @@
"AlternativeHeaderCount": 0,
"AllowedComments": {
"DecodingHeader": 1,
"TimestampOffset": 1,
"TimestampJump": 1
},
"Files": [