Toolbox: Add data hashes to exported xml.gz

This commit is contained in:
genteure 2021-11-20 13:19:23 +08:00
parent 8ad4962cff
commit ccdba838f8
10 changed files with 59 additions and 12 deletions

View File

@ -28,6 +28,6 @@ namespace BililiveRecorder.Core
//}; //};
} }
public Stream CreateMemoryStream(string tag) => this.manager.GetStream(tag); public MemoryStream CreateMemoryStream(string tag) => this.manager.GetStream(tag);
} }
} }

View File

@ -8,6 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FastHashes" Version="2.4.0" />
<PackageReference Include="JsonSubTypes" Version="1.8.0" /> <PackageReference Include="JsonSubTypes" Version="1.8.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" /> <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />

View File

@ -4,6 +4,6 @@ namespace BililiveRecorder.Flv
{ {
public class DefaultMemoryStreamProvider : IMemoryStreamProvider public class DefaultMemoryStreamProvider : IMemoryStreamProvider
{ {
public Stream CreateMemoryStream(string tag) => new MemoryStream(); public MemoryStream CreateMemoryStream(string tag) => new MemoryStream();
} }
} }

View File

@ -84,6 +84,12 @@ namespace BililiveRecorder.Flv
/// </summary> /// </summary>
[XmlAttribute] [XmlAttribute]
public H264NaluType Type { get; set; } public H264NaluType Type { get; set; }
/// <summary>
/// nal_unit data hash
/// </summary>
[XmlAttribute]
public string? NaluHash { get; set; }
} }
/// <summary> /// <summary>

View File

@ -4,6 +4,6 @@ namespace BililiveRecorder.Flv
{ {
public interface IMemoryStreamProvider public interface IMemoryStreamProvider
{ {
Stream CreateMemoryStream(string tag); MemoryStream CreateMemoryStream(string tag);
} }
} }

View File

@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;
using System.Xml.Serialization; using System.Xml.Serialization;
using BililiveRecorder.Flv.Amf; using BililiveRecorder.Flv.Amf;
using FastHashes;
namespace BililiveRecorder.Flv namespace BililiveRecorder.Flv
{ {
@ -26,8 +28,11 @@ namespace BililiveRecorder.Flv
[XmlAttribute] [XmlAttribute]
public int Timestamp { get; set; } public int Timestamp { get; set; }
[XmlAttribute]
public string? DataHash { get; set; }
[XmlIgnore] [XmlIgnore]
public Stream? BinaryData { get; set; } public MemoryStream? BinaryData { get; set; }
[XmlElement] [XmlElement]
public ScriptTagBody? ScriptData { get; set; } public ScriptTagBody? ScriptData { get; set; }
@ -43,7 +48,7 @@ namespace BililiveRecorder.Flv
: BinaryConvertUtilities.StreamToHexString(this.BinaryData); : BinaryConvertUtilities.StreamToHexString(this.BinaryData);
set set
{ {
Stream? new_stream = null; MemoryStream? new_stream = null;
if (value != null) if (value != null)
new_stream = BinaryConvertUtilities.HexStringToMemoryStream(value); new_stream = BinaryConvertUtilities.HexStringToMemoryStream(value);
@ -52,10 +57,13 @@ namespace BililiveRecorder.Flv
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ShouldSerializeBinaryDataForSerializationUseOnly() => 0 != (this.Flag & TagFlag.Header); public bool ShouldSerializeBinaryDataForSerializationUseOnly() => 0 != (this.Flag & TagFlag.Header);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ShouldSerializeScriptData() => this.Type == TagType.Script; public bool ShouldSerializeScriptData() => this.Type == TagType.Script;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ShouldSerializeNalus() => this.Type == TagType.Video && (0 == (this.Flag & TagFlag.Header)); public bool ShouldSerializeNalus() => this.Type == TagType.Video && (0 == (this.Flag & TagFlag.Header));
public override string ToString() => this.DebuggerDisplay; public override string ToString() => this.DebuggerDisplay;
@ -64,7 +72,7 @@ namespace BililiveRecorder.Flv
public Tag Clone() => this.Clone(null); public Tag Clone() => this.Clone(null);
public Tag Clone(IMemoryStreamProvider? provider = null) public Tag Clone(IMemoryStreamProvider? provider = null)
{ {
Stream? binaryData = null; MemoryStream? binaryData = null;
if (this.BinaryData != null) if (this.BinaryData != null)
{ {
binaryData = provider?.CreateMemoryStream(nameof(Tag) + ":" + nameof(Clone)) ?? new MemoryStream(); binaryData = provider?.CreateMemoryStream(nameof(Tag) + ":" + nameof(Clone)) ?? new MemoryStream();
@ -88,12 +96,37 @@ namespace BililiveRecorder.Flv
Size = this.Size, Size = this.Size,
Index = this.Index, Index = this.Index,
Timestamp = this.Timestamp, Timestamp = this.Timestamp,
DataHash = this.DataHash,
BinaryData = binaryData, BinaryData = binaryData,
ScriptData = scriptData, ScriptData = scriptData,
Nalus = this.Nalus is null ? null : new List<H264Nalu>(this.Nalus), Nalus = this.Nalus is null ? null : new List<H264Nalu>(this.Nalus),
}; };
} }
private static readonly FarmHash64 farmHash64 = new FarmHash64();
public string? UpdateDataHash()
{
if (this.BinaryData is null)
{
this.DataHash = null;
}
else
{
var buffer = this.BinaryData.GetBuffer();
this.DataHash = BinaryConvertUtilities.ByteArrayToHexString(farmHash64.ComputeHash(buffer, (int)this.BinaryData.Length));
if (this.Nalus?.Count > 0)
{
foreach (var nalu in this.Nalus)
{
nalu.NaluHash = BinaryConvertUtilities.ByteArrayToHexString(farmHash64.ComputeHash(buffer, nalu.StartPosition, (int)nalu.FullSize));
}
}
}
return this.DataHash;
}
private string DebuggerDisplay => string.Format("{0}, {1}{2}{3}, TS={4}, Size={5}", private string DebuggerDisplay => string.Format("{0}, {1}{2}{3}, TS={4}, Size={5}",
this.Type switch this.Type switch
{ {
@ -158,7 +191,7 @@ namespace BililiveRecorder.Flv
return new string(result); return new string(result);
} }
internal static Stream HexStringToMemoryStream(string hex) internal static MemoryStream HexStringToMemoryStream(string hex)
{ {
var stream = new MemoryStream(hex.Length / 2); var stream = new MemoryStream(hex.Length / 2);
for (var i = 0; i < hex.Length; i += 2) for (var i = 0; i < hex.Length; i += 2)

View File

@ -28,6 +28,6 @@ namespace BililiveRecorder.ToolBox
//}; //};
} }
public Stream CreateMemoryStream(string tag) => this.manager.GetStream(tag); public MemoryStream CreateMemoryStream(string tag) => this.manager.GetStream(tag);
} }
} }

View File

@ -66,11 +66,17 @@ namespace BililiveRecorder.ToolBox.Tool.Export
var count = 0; var count = 0;
var tags = new List<Tag>(); var tags = new List<Tag>();
var memoryStreamProvider = new RecyclableMemoryStreamProvider(); var memoryStreamProvider = new RecyclableMemoryStreamProvider();
using var reader = new FlvTagPipeReader(PipeReader.Create(inputStream), memoryStreamProvider, skipData: true, logger: logger); using var reader = new FlvTagPipeReader(PipeReader.Create(inputStream), memoryStreamProvider, skipData: false, logger: logger);
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
var tag = await reader.ReadTagAsync(cancellationToken).ConfigureAwait(false); var tag = await reader.ReadTagAsync(cancellationToken).ConfigureAwait(false);
if (tag is null) break; if (tag is null)
break;
tag.UpdateDataHash();
if (!tag.ShouldSerializeBinaryDataForSerializationUseOnly())
tag.BinaryData?.Dispose();
tags.Add(tag); tags.Add(tag);
if (count++ % 300 == 0 && progress is not null) if (count++ % 300 == 0 && progress is not null)

View File

@ -31,6 +31,7 @@ namespace BililiveRecorder.Flv.Tests.FlvTests
Assert.Equal(source.Index, result.Index); Assert.Equal(source.Index, result.Index);
Assert.Equal(source.Size, result.Size); Assert.Equal(source.Size, result.Size);
Assert.Equal(source.Timestamp, result.Timestamp); Assert.Equal(source.Timestamp, result.Timestamp);
Assert.Equal(source.DataHash, result.DataHash);
Assert.NotSame(source.BinaryData, result.BinaryData); Assert.NotSame(source.BinaryData, result.BinaryData);
Assert.Equal(source.BinaryDataForSerializationUseOnly, result.BinaryDataForSerializationUseOnly); Assert.Equal(source.BinaryDataForSerializationUseOnly, result.BinaryDataForSerializationUseOnly);

View File

@ -12,6 +12,6 @@ namespace BililiveRecorder.Flv.Tests
MaximumFreeLargePoolBytes = 64 * 1024 * 32, MaximumFreeLargePoolBytes = 64 * 1024 * 32,
}; };
public Stream CreateMemoryStream(string tag) => manager.GetStream(tag); public MemoryStream CreateMemoryStream(string tag) => manager.GetStream(tag);
} }
} }