The segmenter behind the media player's stream feed. Plans cue-point ranges from a video WebM and an audio WebM (with an optional VP9 switch source once the AV1 byte budget runs out), downloads each range with pooled reads, muxes video clusters and matching audio on a worker thread, and writes standalone files into FileSystem.Data. Also handles audio-only sources, reports refusals, and deletes segment files on release and dispose.
using System.IO;
using System.Threading;
namespace Bimp.Resolver.Media;
/// <summary>
/// A finished segment file, ready to play.
/// </summary>
public sealed class MediaSegment
{
public int Seq;
/// <summary> Path in <see cref="FileSystem.Data"/>. </summary>
public string Path;
/// <summary> Media time (seconds) of the first frame. </summary>
public float Start;
/// <summary> Media time (seconds) at which the next segment starts. </summary>
public float End;
public bool Last;
/// <summary> Live: stream time (seconds) at which this segment's clock reads 0. </summary>
public double StreamStart;
/// <summary> Live: when the file was finished (<see cref="RealTime.NowDouble"/>). </summary>
public double ReadyAt;
/// <summary>
/// Live: how much of the next segment the file carries after <see cref="End"/> - it plays on into it while the
/// next player comes up. 0 = it ends at End (low latency H.264, ending in skip frames).
/// </summary>
public float Overlap;
}
/// <summary>
/// Turns a separate video WebM + audio WebM into a series of standalone WebM segment files in
/// <see cref="FileSystem.Data"/>, starting at a given time. Each segment is a run of whole video clusters (cue
/// point to cue point) plus the audio for the same span, downloaded with range requests - so playback starts
/// after the first few seconds are fetched, not the whole file. Timestamps stay absolute.
/// <para>
/// Audio only (no video source) writes one file from the cue point before the start time to the end.
/// </para>
/// </summary>
public sealed class WebmSegmenter : ISegmentFeed, IDisposable
{
/// <summary> The first segment is short so playback starts quickly. </summary>
const float FirstSegmentSeconds = 4;
const float SegmentSeconds = 40;
/// <summary> Audio only files use short clusters, which is also their seek granularity. </summary>
const long AudioOnlyClusterMs = 5000;
/// <summary>
/// A segment's audio runs this far past its end, so it keeps playing while the next segment's player starts - that
/// one stays silent until its first frame is up: ~0.1 s at 1080p, 0.4-2.6 s for 4K VP9 (measured).
/// </summary>
const double AudioTailSeconds = 3.0;
/// <summary>
/// On subsequent segments (seq > 0), include 1-2 preceding audio blocks before t0 (in a pre-cluster) so no
/// straddling Opus packet is dropped at the boundary and the Opus decoder converges before t0.
/// </summary>
const double AudioLeadSeconds = 0.04;
/// <summary>
/// A segment's video runs this far into the next one, so its player shows real frames while the next player comes
/// up. A new player's clock is in step when it's opened ~50 ms early, but its first frame at 1440p/4K comes 150-520 ms
/// after that (measured) - without this the picture held for that long at every swap.
/// </summary>
const double VideoOverlapSeconds = 1.0;
/// <summary>
/// YouTube's AV1 urls stop answering after ~88 MB for the clients that get them (documented, not measured here - no
/// measurement is recorded): plan the AV1 part of playback to stay under this (counting what the session already
/// downloaded), then carry on with the switch video. The switch video is FormatSelector.SwitchLike: the tallest SDR
/// VP9 below 4K at the same frame rate, as the engine's VP9 decoder keeps up with 1440p60 but not 4K60.
/// </summary>
const long Av1BudgetBytes = 80L * 1024 * 1024;
readonly WebmSource video;
readonly WebmSource audio;
readonly WebmSource switchVideo;
readonly string directory;
readonly CancellationTokenSource cts = new();
/// <summary>
/// Each segment: its video source, its cue range, and - for the last AV1 one - the time it ends at instead of its
/// last cue (the switch video's first keyframe; the file runs on past it while that one's player comes up).
/// </summary>
readonly List<(WebmSource src, int first, int end, long? endAt)> ranges = new();
readonly Dictionary<int, Task<MediaSegment>> building = new();
readonly double duration;
bool disposed;
/// <summary> Something went wrong building a segment. </summary>
public string Error { get; private set; }
/// <summary> A build was refused (403/410) even after the url was refreshed - only a fresh session, from another client, helps. </summary>
public bool Refused { get; private set; }
/// <summary> The client that minted the url of the file that was refused (see <see cref="Refused"/>), if it's known. </summary>
public string RefusedClient { get; private set; }
public int Count => video is null ? 1 : ranges.Count;
bool ISegmentFeed.IsLive => false;
int ISegmentFeed.Newest => building.Where( kv => kv.Value.IsCompletedSuccessfully && kv.Value.Result is not null ).Select( kv => kv.Key ).DefaultIfEmpty( -1 ).Max();
void ISegmentFeed.Want( int seq ) => GetAsync( seq ).FireAndForget( $"build segment {seq}" );
/// <param name="maxLagSeconds">Start at the cue point before <paramref name="startSeconds"/> if it's at most this far behind, else at the next one.</param>
/// <param name="switchTo">Carry on with this video once the AV1 budget is used (see <see cref="Av1BudgetBytes"/>).</param>
public WebmSegmenter( WebmSource video, WebmSource audio, float startSeconds, string directory, float maxLagSeconds = float.MaxValue, WebmSource switchTo = null )
{
if ( audio is null ) throw new ArgumentNullException( nameof( audio ) );
if ( video is not null && video.TimecodeScale != audio.TimecodeScale )
throw new MergeException( "video and audio use different timecode scales" );
this.video = video;
this.audio = audio;
switchVideo = switchTo is not null && switchTo.TimecodeScale == audio.TimecodeScale ? switchTo : null;
this.directory = directory;
duration = Math.Max( video?.Duration ?? 0, audio.Duration );
FileSystem.Data.CreateDirectory( directory );
if ( video is not null ) PlanRanges( startSeconds, maxLagSeconds );
else ranges.Add( (null, audio.IndexAt( audio.TimeOf( startSeconds ) ), audio.Cues.Count, null) );
}
void PlanRanges( float startSeconds, float maxLagSeconds )
{
var src = video;
var cues = video.Cues;
var i = video.IndexAt( video.TimeOf( startSeconds ) );
if ( i + 1 < cues.Count && startSeconds - video.SecondsOf( cues[i].Time ) > maxLagSeconds ) i++;
var target = FirstSegmentSeconds;
// the allowance is YouTube's AV1 limit: a VP9 video keeps its switch video for a failure only (see SwitchFrom)
var budget = switchVideo is null || !video.IsMp4 ? long.MaxValue : Av1BudgetBytes - video.File.BytesRead;
// what a segment downloads, with its overlap into the next one
long Cost( int i, int j ) => VideoOverlap( video, i, j, j < video.Cues.Count ? video.Cues[j].Time : long.MaxValue, j >= video.Cues.Count ).readEnd - video.ClusterStart( i );
while ( i < cues.Count )
{
var j = i + 1;
while ( j < cues.Count && src.SecondsOf( cues[j].Time - cues[i].Time ) < target ) j++;
if ( src == video && switchVideo is not null && Cost( i, j ) > budget )
{
// as much AV1 as still fits, then the switch video from its first keyframe at or after there
while ( j > i + 1 && Cost( i, j ) > budget ) j--;
if ( Cost( i, j ) <= budget )
{
budget -= Cost( i, j );
ranges.Add( (src, i, j, null) );
i = j;
if ( i >= cues.Count ) break;
}
var k = SwitchIndex( cues[i].Time );
if ( k < 0 ) break;
EndAtSwitch( switchVideo.Cues[k].Time );
(src, cues, i, target) = (switchVideo, switchVideo.Cues, k, FirstSegmentSeconds);
Log.Info( ranges.Count == 0
? "[bimp] this session's AV1 allowance is used up (YouTube stops serving it after ~88 MB) - playing the VP9 copy"
: $"[bimp] AV1 until {video.SecondsOf( cues[k].Time ):0.0}s (YouTube stops serving it after ~88 MB), then the VP9 copy" );
continue;
}
if ( src == video ) budget -= Cost( i, j );
ranges.Add( (src, i, j, null) );
i = j;
target = SegmentSeconds;
}
}
/// <summary> The switch video's first keyframe at or after a time, or the last one before it; -1 if it has none. </summary>
int SwitchIndex( long time )
{
var k = switchVideo.FirstIndexFrom( time );
if ( k >= switchVideo.Cues.Count ) k = switchVideo.Cues.Count - 1;
return k;
}
/// <summary>
/// The last AV1 segment ends where the switch video starts: its file runs on past that like any other (see
/// <see cref="VideoOverlap"/>), but its clock hands over there.
/// </summary>
void EndAtSwitch( long switchTime )
{
if ( ranges.Count == 0 || ranges[^1].src != video ) return;
var (src, first, end, _) = ranges[^1];
ranges[^1] = (src, first, end, switchTime);
}
/// <summary>
/// The video a segment ending at <paramref name="t1"/> carries: entries <paramref name="first"/> to kEnd, cut at
/// cut (<see cref="VideoOverlapSeconds"/> past t1), and the bytes to read for them. Only the start of the cluster
/// the cut falls in is read (its keyframe plus a margin), not the whole keyframe interval.
/// </summary>
static (int kEnd, long cut, long readEnd) VideoOverlap( WebmSource src, int first, int end, long t1, bool last )
{
var cues = src.Cues;
if ( last || t1 == long.MaxValue ) return (end, long.MaxValue, src.ClusterEnd( end - 1 ));
var cut = t1 + src.TimeOf( VideoOverlapSeconds );
var k = Math.Max( end - 1, src.IndexAt( cut - 1 ) );
var kEnd = k + 1;
var clusterEnd = k + 1 < cues.Count ? cues[k + 1].Time : long.MaxValue;
if ( clusterEnd <= cut ) return (kEnd, cut, src.ClusterEnd( k ));
var span = clusterEnd == long.MaxValue ? src.TimeOf( SegmentSeconds ) : clusterEnd - cues[k].Time;
var bytes = src.ClusterEnd( k ) - src.ClusterStart( k );
var fraction = Math.Clamp( (cut - cues[k].Time) / (double)Math.Max( span, 1 ), 0, 1 );
var readEnd = src.ClusterStart( k ) + (long)(bytes * fraction * 1.5) + 512 * 1024;
return (kEnd, cut, Math.Min( readEnd, src.ClusterEnd( k ) ));
}
/// <summary>
/// An AV1 segment couldn't be downloaded (the url stopped answering early): it and everything after it come from
/// the switch video instead, from its keyframe at or before the segment's start.
/// </summary>
bool SwitchFrom( int seq )
{
if ( switchVideo is null || seq >= ranges.Count || ranges[seq].src != video ) return false;
var from = video.Cues[ranges[seq].first].Time;
ranges.RemoveRange( seq, ranges.Count - seq );
var cues = switchVideo.Cues;
var i = switchVideo.IndexAt( from );
var target = FirstSegmentSeconds;
while ( i < cues.Count )
{
var j = i + 1;
while ( j < cues.Count && switchVideo.SecondsOf( cues[j].Time - cues[i].Time ) < target ) j++;
ranges.Add( (switchVideo, i, j, null) );
i = j;
target = SegmentSeconds;
}
Log.Info( $"[bimp] AV1 video stopped answering - carrying on with VP9 from {switchVideo.SecondsOf( cues[ranges[seq].first].Time ):0.0}s" );
return true;
}
/// <summary>
/// The segment, building it (and everything before it) if needed. Also starts on the one after it.
/// </summary>
public Task<MediaSegment> GetAsync( int seq )
{
if ( seq < 0 || seq >= Count ) return Task.FromResult<MediaSegment>( null );
var task = Build( seq );
// the look-ahead is nobody's await: keep its fault observed (FireAndForget), it reports via Error/Refused
if ( seq + 1 < Count ) Build( seq + 1 ).FireAndForget( $"build segment {seq + 1}" );
return task;
}
/// <summary> The segment if it's finished, else null. </summary>
public MediaSegment TryGet( int seq )
{
return building.TryGetValue( seq, out var t ) && t.IsCompletedSuccessfully ? t.Result : null;
}
Task<MediaSegment> Build( int seq )
{
if ( building.TryGetValue( seq, out var existing ) ) return existing;
// one at a time, in order - the network is the bottleneck, and the next one is needed first
var previous = seq > 0 && building.TryGetValue( seq - 1, out var p ) ? p : Task.FromResult<MediaSegment>( null );
var task = BuildAfter( previous, seq );
building[seq] = task;
return task;
}
async Task<MediaSegment> BuildAfter( Task previous, int seq )
{
try { await previous; } catch { /* reported by its own task */ }
try
{
var segment = video is null ? await BuildAudioOnly() : await BuildMergedOrSwitch( seq );
if ( disposed ) { Delete( segment.Path ); return null; }
return segment;
}
catch ( OperationCanceledException )
{
return null;
}
catch ( Exception e )
{
if ( e is FetchException { IsRefused: true } refusal )
{
Refused = true;
RefusedClient ??= refusal.ClientKey;
}
Error ??= e.Message;
Log.Warning( $"[bimp] Couldn't build segment {seq}: {e.Message}" );
throw;
}
}
async Task<MediaSegment> BuildMergedOrSwitch( int seq )
{
try
{
return await BuildMerged( seq );
}
catch ( FetchException e ) when ( SwitchFrom( seq ) )
{
Log.Trace( $"[bimp] segment {seq}: {e.Message}" );
return await BuildMerged( seq );
}
}
async Task<MediaSegment> BuildMerged( int seq )
{
var ct = cts.Token;
var (video, first, end, endAt) = ranges[seq];
var cues = video.Cues;
var t0 = cues[first].Time;
var t1 = endAt ?? (end < cues.Count ? cues[end].Time : long.MaxValue);
var last = end >= cues.Count && endAt is null;
// the video clusters, byte for byte, running on into the next segment (see VideoOverlapSeconds)
var (kEnd, cut, vEnd) = VideoOverlap( video, first, end, t1, last );
var vStart = video.ClusterStart( first );
// the audio clusters covering [lead, t1 + tail)
var lead = seq > 0 ? Math.Max( 0L, t0 - audio.TimeOf( AudioLeadSeconds ) ) : t0;
var tail = t1 == long.MaxValue ? long.MaxValue : t1 + audio.TimeOf( AudioTailSeconds );
var ai = audio.IndexAt( lead );
var aj = tail == long.MaxValue ? audio.Cues.Count : audio.FirstIndexFrom( tail );
var aStart = audio.ClusterStart( ai );
var aEnd = aj < audio.Cues.Count ? audio.ClusterStart( aj ) : audio.SegmentEnd;
#if BIMP_DEV
#endif
var videoTask = video.File.ReadPooledAsync( vStart, vEnd, ct );
var audioTask = audio.File.ReadPooledAsync( aStart, aEnd, ct );
// The downloads sit in pooled buffers (see PooledRange): they go back in the finally, once the mux has written
// the file - the video blocks are slices of the buffer until then - or on any failure, a cancelled build included.
// The try starts right after the downloads do, so nothing between them can skip giving the buffers back.
#if BIMP_DEV
#endif
try
{
#if BIMP_DEV
#endif
// A mass refusal (403) fails both downloads at once: awaiting them one by one orphaned the second task,
// which resurfaced later as an unobserved-exception error. Await both, report the first failure.
Exception firstFailure = null;
foreach ( var t in new[] { videoTask, audioTask } )
{
try { await t; }
catch ( OperationCanceledException ) when ( ct.IsCancellationRequested ) { throw; }
catch ( Exception e ) { firstFailure ??= e; }
}
if ( firstFailure is not null ) throw firstFailure;
// the buffers can be larger than what was downloaded: only Length bytes are the range
var videoBytes = videoTask.Result.Data;
var videoLength = videoTask.Result.Length;
var audioBytes = audioTask.Result.Data;
var audioLength = audioTask.Result.Length;
#if BIMP_DEV
#endif
// Parsing the audio and assembling the entries copies megabytes of clusters - done on a worker thread with
// the write, so the main thread (and every player's Present()) never waits on it.
var vtrack = video.TrackNumber;
ulong atrack = vtrack != 1 ? 1UL : 2UL;
var path = $"{directory}/seg_{seq}.webm";
long firstBlockTime = t0;
long lastVideo = t0;
await GameTask.RunInThreadAsync( () =>
{
#if BIMP_DEV
#endif
var blocks = WebmMux.ParseBlocks( audioBytes, 0, audioLength );
if ( blocks.Count > 0 ) firstBlockTime = blocks[0].Time;
// audio from before the first video cue goes in front of it in its own pre-cluster (non-negative
// cluster/block timecodes): on first == 0 everything before t0, and on seq > 0 the AudioLeadSeconds
// lead-in so no straddling Opus packet is dropped and the decoder converges before t0.
byte[] pre = null;
if ( first == 0 )
{
var early = blocks.Where( b => b.Time >= 0 && b.Time < t0 ).ToList();
if ( early.Count > 0 ) pre = Ebml.Concat( WebmMux.Clusters( early, atrack ).Select( c => c.cluster ) );
}
else if ( seq > 0 )
{
var early = blocks.Where( b => b.Time >= lead && b.Time < t0 ).ToList();
if ( early.Count > 0 ) pre = Ebml.Concat( WebmMux.Clusters( early, atrack ).Select( c => c.cluster ) );
}
// Each entry: the video clusters from a cue point, with the audio for the same time woven in among their blocks.
// The engine reads a file in order and only holds a few seconds of packets: audio written after a whole
// keyframe interval of video (5-7 s, up to 18 MB at 4K) reached it too late, and it went silent for 1-3 s at a
// time (measured). Clusters still start at keyframes - the engine's forward seek lands on any cluster.
// The clusters stay descriptions (slices of the download): WebmMux.Write streams them out, nothing the size of
// a cluster is built in memory.
var entries = new List<MuxEntry>();
var bi = 0;
while ( bi < blocks.Count && blocks[bi].Time < t0 ) bi++;
for ( int k = first; k < kEnd; k++ )
{
var entry = new MuxEntry { Time = cues[k].Time };
var from = (int)(video.ClusterStart( k ) - vStart);
var to = (int)Math.Min( video.ClusterEnd( k ) - vStart, videoLength );
// the last entry's audio runs on past the end (see AudioTailSeconds)
var next = k + 1 < cues.Count ? cues[k + 1].Time : long.MaxValue;
var until = k + 1 < kEnd ? next : tail;
var group = new List<MediaBlock>();
while ( bi < blocks.Count && blocks[bi].Time < until ) group.Add( blocks[bi++] );
// AV1 from a fragmented MP4: its fragments become clusters first (the last may be cut off)
var clusters = video.IsMp4
? WebmMux.InterleaveStreamed( video.ClusterBlocksFromMp4( videoBytes, from, to, vStart, partial: k == kEnd - 1 ), Math.Min( next, cut ), group, atrack, cut, ref lastVideo )
: WebmMux.InterleaveStreamed( videoBytes, from, to, Math.Min( next, cut ), group, atrack, cut, ref lastVideo );
if ( clusters.Count == 0 ) continue;
entry.Clusters.AddRange( clusters );
entries.Add( entry );
}
var tracks = Ebml.Make( Ebml.Tracks, Ebml.Concat( video.TrackEntry, WebmMux.FixMonoOpus( WebmMux.RenumberTrack( audio.TrackEntry, atrack, 0x6A696D6200000000UL | atrack ) ) ) );
#if BIMP_DEV
#endif
using var file = FileSystem.Data.OpenWrite( path );
WebmMux.Write( file, video.EbmlHeader, video.TimecodeScale, duration, tracks, vtrack, entries, pre );
} );
ct.ThrowIfCancellationRequested();
#if BIMP_DEV
#endif
return new MediaSegment
{
Seq = seq,
Path = path,
Start = (float)video.SecondsOf( first == 0 ? Math.Min( t0, firstBlockTime ) : t0 ),
End = (float)(t1 == long.MaxValue ? video.SecondsOf( (long)duration ) : video.SecondsOf( t1 )),
Last = last,
Overlap = last ? 0 : (float)Math.Max( video.SecondsOf( lastVideo - t1 ), 0 ),
};
}
finally
{
#if BIMP_DEV
#endif
ReturnBuffer( videoTask );
ReturnBuffer( audioTask );
}
}
/// <summary> Give a download's buffer back, whether the build is done with it or never got that far (see <see cref="PooledRange"/>). </summary>
static void ReturnBuffer( Task<PooledRange> download )
{
#if BIMP_DEV
#endif
if ( download.IsCompletedSuccessfully ) download.Result.Dispose();
// Still running, or failed: the continuation runs either way (at once if it's already done), and decides from the
// task's final state - so a download that finishes between two checks here still gives its buffer back, and a
// late fault (a 403) is observed instead of resurfacing from the finalizer as an unobserved-exception error.
else download.ContinueWith( t => { if ( t.IsCompletedSuccessfully ) t.Result.Dispose(); else _ = t.Exception; } );
}
/// <summary> How much audio a limited client's file is read for (it serves ~61-80 s, measured 2026-10-16). </summary>
const double LimitedAudioSeconds = 50;
async Task<MediaSegment> BuildAudioOnly()
{
var ct = cts.Token;
var (_, first, _, _) = ranges[0];
var start = audio.ClusterStart( first );
// a client that only serves the first minute or so (see StreamPlan.Limited) refuses a read that runs past it, so the whole
// rest of the file would give nothing: take the start, inside what it serves
var end = audio.SegmentEnd;
if ( audio.File.Limited )
{
var cut = audio.FirstIndexFrom( audio.Cues[first].Time + audio.TimeOf( LimitedAudioSeconds ) );
if ( cut < audio.Cues.Count ) end = Math.Min( end, audio.ClusterStart( cut ) );
}
// a pooled buffer, given back when the file is written (or the build fails) - only data.Length bytes are the audio
using var data = await audio.File.ReadPooledAsync( start, end, ct );
var track = audio.TrackNumber;
var path = $"{directory}/audio.webm";
long firstTime = 0;
await GameTask.RunInThreadAsync( () =>
{
#if BIMP_DEV
#endif
var blocks = WebmMux.ParseBlocks( data.Data, 0, data.Length );
if ( blocks.Count == 0 ) throw new MergeException( "no audio blocks" );
firstTime = blocks[0].Time;
var entries = WebmMux.Clusters( blocks, track, AudioOnlyClusterMs )
.Select( c => new MuxEntry { Time = c.time, Parts = { c.cluster } } )
.ToList();
var tracks = Ebml.Make( Ebml.Tracks, WebmMux.FixMonoOpus( audio.TrackEntry ) );
#if BIMP_DEV
#endif
using var file = FileSystem.Data.OpenWrite( path );
WebmMux.Write( file, audio.EbmlHeader, audio.TimecodeScale, duration, tracks, track, entries, null );
} );
ct.ThrowIfCancellationRequested();
return new MediaSegment
{
Seq = 0,
Path = path,
Start = (float)audio.SecondsOf( firstTime ),
End = (float)audio.SecondsOf( (long)duration ),
Last = true,
};
}
/// <summary>
/// Done with a segment - delete its file.
/// </summary>
public void Release( int seq )
{
var s = TryGet( seq );
if ( s is not null ) Delete( s.Path );
}
static void Delete( string path )
{
#if BIMP_DEV
#endif
try
{
if ( FileSystem.Data.FileExists( path ) ) FileSystem.Data.DeleteFile( path );
}
catch ( Exception e )
{
// still open in a player - the cache is wiped on the next start anyway
Log.Trace( $"[bimp] couldn't delete {path}: {e.Message}" );
}
}
public void Dispose()
{
if ( disposed ) return;
disposed = true;
cts.Cancel();
foreach ( var t in building.Values )
if ( t.IsCompletedSuccessfully && t.Result is not null ) Delete( t.Result.Path );
try
{
if ( FileSystem.Data.DirectoryExists( directory ) ) FileSystem.Data.DeleteDirectory( directory, true );
}
catch ( Exception e )
{
Log.Trace( $"[bimp] couldn't delete {directory}: {e.Message}" );
}
}
}