fix(video): roll back rejected MP4 samples and keep inspector reports read-only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:07:59 +00:00
1 parent 45518e8e4e
commit aba6d629b9
3 files changed
+109 -75

No files matched your search

+14 -8
View File
@@ -113,6 +113,7 @@ public sealed class Mp4Muxer
/// <summary>
/// Appends one access unit (H.264/HEVC: Annex-B or 4-byte length-prefixed NAL units; AV1: a temporal unit of OBUs)
/// in decode order. <paramref name="compositionOffset90k"/> is presentation minus decode time and may be negative.
/// A unit holding only parameter sets is recorded like <see cref="AddParameterSets"/> and adds no sample.
/// </summary>
public void WriteSample(ReadOnlySpan<byte> data, long duration90k, bool isSync, long compositionOffset90k = 0)
{
@@ -122,8 +123,14 @@ public sealed class Mp4Muxer
var start = _position;
if (_stream.Position != start) _stream.Position = start;
var size = _config.Codec == VideoCodec.Av1 ? WriteObus(data) : WriteNalUnits(data);
if (size == 0) throw new ArgumentException("The sample contains no picture data", nameof(data));
var (size, picture) = _config.Codec == VideoCodec.Av1 ? WriteObus(data) : WriteNalUnits(data);
if (size == 0) return; // only parameter sets / delimiters: recorded for the sample entry, no sample
if (!picture)
{
_stream.Position = start; // the next sample overwrites what was written
throw new ArgumentException("The sample contains no picture data", nameof(data));
}
_position += size;
if (_sizes.Count == 0 && !isSync) Log.Warning(LogCategory, "First sample is not a sync sample; players may not start decoding");
if (_chunkSampleCounts.Count == 0 || _chunkTicks >= ChunkTicks || _chunkBytes >= ChunkBytes)
@@ -161,11 +168,12 @@ public sealed class Mp4Muxer
_stream.Write(moov);
if (_config.FastStart) MoveMoovToFront(configuration, moov.Length);
else _position += moov.Length;
if (_stream.Length > _position) _stream.SetLength(_position); // drop bytes of a rejected last sample
_stream.Flush();
_completed = true;
}
private long WriteNalUnits(ReadOnlySpan<byte> data)
private (long Size, bool Picture) WriteNalUnits(ReadOnlySpan<byte> data)
{
Span<byte> length = stackalloc byte[4];
long size = 0;
@@ -180,11 +188,10 @@ public sealed class Mp4Muxer
size += 4 + nal.Length;
picture |= IsPicture(nal[0]);
}
_position += size;
return picture ? size : 0;
return (size, picture);
}
private long WriteObus(ReadOnlySpan<byte> data)
private (long Size, bool Picture) WriteObus(ReadOnlySpan<byte> data)
{
var obus = Av1ObuParser.Split(data);
if (obus.Count == 0 || obus[^1].Offset + obus[^1].TotalLength != data.Length)
@@ -209,8 +216,7 @@ public sealed class Mp4Muxer
_stream.Write(obu.Payload(data));
size += n + obu.PayloadLength;
}
_position += size;
return picture ? size : 0;
return (size, picture);
}
private static int WriteSizedHeader(Span<byte> header, ReadOnlySpan<byte> data, Av1Obu obu)