Phase 21 Direction B: bound network memory via Range-segmented forward fetch

Replace the open-ended forward GET with sequential bounded bytes=start-end
segments, the next fetched only when the scheduler drains below low-water,
so the browser holds ~one segment regardless of file size. Seek converges
on the same loop. Strip BP-DIAG.
This commit is contained in:
daniel-c-harvey
2026-06-24 13:20:37 -04:00
parent def297e7d9
commit 11faf8888f
6 changed files with 687 additions and 279 deletions
+2 -28
View File
@@ -269,19 +269,13 @@ export class AudioPlayer {
const headerParsed = decoder.ready;
const canStart = headerParsed && this.scheduler.hasMinimumBuffers(this.minBuffersForPlayback);
// [BP-DIAG] Phase 21.4 — value of productionPaused actually placed on the Opus chunk
// result handed to C#. Confirms the flag is populated on THIS path (not just the WAV
// path). TEMPORARY — strip once confirmed.
const opusPaused = this.scheduler.evaluateProductionPause();
this.bpDiagLogChunkResult('opus', canStart, opusPaused);
return {
success: true,
canStartStreaming: canStart,
headerParsed,
bufferCount: this.scheduler.getBufferCount(),
duration: this.duration,
productionPaused: opusPaused
productionPaused: this.scheduler.evaluateProductionPause()
};
} catch (error) {
return { success: false, error: (error as Error).message };
@@ -320,18 +314,13 @@ export class AudioPlayer {
const canStart = this.streamDecoder.headerParsed &&
this.scheduler.hasMinimumBuffers(this.minBuffersForPlayback);
// [BP-DIAG] Phase 21.4 — value of productionPaused actually placed on the WAV/MP3/FLAC
// chunk result handed to C#. TEMPORARY — strip once confirmed.
const formatPaused = this.scheduler.evaluateProductionPause();
this.bpDiagLogChunkResult('format', canStart, formatPaused);
return {
success: true,
canStartStreaming: canStart,
headerParsed: this.streamDecoder.headerParsed,
bufferCount: this.scheduler.getBufferCount(),
duration: this.duration,
productionPaused: formatPaused
productionPaused: this.scheduler.evaluateProductionPause()
};
} catch (error) {
return { success: false, error: (error as Error).message };
@@ -742,21 +731,6 @@ export class AudioPlayer {
// ==================== Private Methods ====================
// ─────────────────────────────────────────────────────────────────────────────────────────
// [BP-DIAG] Phase 21.4 back-pressure diagnostic. TEMPORARY — strip once confirmed in Daniel's
// browser run. Logs the productionPaused flag on the chunk result handed back to C#, throttled
// to ~4 Hz so it does not flood. Grep "[BP-DIAG] chunk-result" in the browser console.
private bpDiagChunkResultLastMs = 0;
private bpDiagLogChunkResult(path: 'opus' | 'format', canStart: boolean, paused: boolean): void {
const now = (typeof performance !== 'undefined' ? performance.now() : Date.now());
if (now - this.bpDiagChunkResultLastMs < 250) return;
this.bpDiagChunkResultLastMs = now;
console.log(
`[BP-DIAG] chunk-result path=${path} productionPaused=${paused} canStart=${canStart} ` +
`bufCount=${this.scheduler.getBufferCount()} streamingStarted=${this.streamingStarted} isPlaying=${this.isPlaying}`);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
private resetState(): void {
this.isPlaying = false;
this.isPaused = false;
@@ -107,14 +107,6 @@ export class PlaybackScheduler {
// Mutated by evaluateProductionPause() — named to signal the state-advance on each call.
private productionPaused_: boolean = false;
// ─────────────────────────────────────────────────────────────────────────────────────────
// [BP-DIAG] Phase 21.4 back-pressure diagnostic. TEMPORARY — strip once the cause is confirmed
// in Daniel's browser run. Throttles evaluateProductionPause() logging to one line per ~250 ms
// so the console shows the live lookahead / byte-estimate / latch without flooding (the signal
// is evaluated on every chunk + every poll). Grep "[BP-DIAG]" in the browser console.
private bpDiagLastLogMs: number = 0;
// ─────────────────────────────────────────────────────────────────────────────────────────
// Callbacks
public onPlaybackEnded: (() => void) | null = null;
@@ -245,22 +237,6 @@ export class PlaybackScheduler {
this.productionPaused_ = true;
}
// [BP-DIAG] Phase 21.4 — the single source of truth for the latch decision. If `paused`
// never goes true while bytes keep arriving, the lookahead is not growing as expected:
// inspect `lookahead` vs `high` (should cross 30 during a fast fill) and `bufCount`/`bytes`
// (decode must actually be populating the scheduler). Throttled to ~4 Hz. TEMPORARY — strip
// once the cause is confirmed. (Uses performance.now when present; Date.now fallback.)
const bpNow = (typeof performance !== 'undefined' ? performance.now() : Date.now());
if (bpNow - this.bpDiagLastLogMs >= 250) {
this.bpDiagLastLogMs = bpNow;
console.log(
`[BP-DIAG] evaluateProductionPause paused=${this.productionPaused_} ` +
`lookahead=${lookahead.toFixed(2)}s high=${this.forwardHighWaterSeconds} low=${this.forwardLowWaterSeconds} ` +
`bytes=${(this.getDecodedByteEstimate() / (1024 * 1024)).toFixed(1)}MB cap=${(this.maxDecodedBytes / (1024 * 1024)).toFixed(0)}MB ` +
`overByteCeiling=${overByteCeiling} bufCount=${this.buffers.length} pos=${this.getCurrentPosition().toFixed(2)}s ` +
`decodedEnd=${(this.getTotalDuration() + this.playbackOffset).toFixed(2)}s active=${this.isActive_}`);
}
return this.productionPaused_;
}