Enable WASM response streaming on audio media fetch

Without SetBrowserResponseStreamingEnabled the browser buffers the whole
body before yielding, so the Phase 21.2 read-loop pause backpressured an
already-downloaded payload. Set it on both the initial and seek/refill
requests; safe no-op on the SSR path.
This commit is contained in:
daniel-c-harvey
2026-06-24 08:45:33 -04:00
parent 9c95a5f23e
commit c7629c15a4
2 changed files with 93 additions and 0 deletions
@@ -3,6 +3,7 @@ using System.Net.Http.Headers;
using System.Net.Http.Json;
using DeepDrftModels.DTOs;
using DeepDrftModels.Enums;
using Microsoft.AspNetCore.Components.WebAssembly.Http;
using Microsoft.Extensions.DependencyInjection;
using NetBlocks.Models;
@@ -79,6 +80,18 @@ public class TrackMediaClient
using var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.Range = new RangeHeaderValue(byteOffset, null);
// Stream the response body incrementally instead of buffering it whole (Phase 21.4 fix).
// In Blazor WebAssembly the HttpClient is backed by the browser fetch API; without this the
// browser buffers the ENTIRE body before the response stream yields a byte, so the 21.2
// read-loop pause (StreamingAudioPlayerService) backpressures nothing — the whole payload is
// already in memory. Enabling streaming makes ReadAsync pull from a browser ReadableStream
// whose backpressure reaches the underlying fetch, so pausing reads genuinely throttles the
// network. This is a request-option flag, not a runtime call: on the SSR server-to-server path
// the SocketsHttpHandler simply ignores the unknown option, so it is safe unguarded. Applies to
// BOTH the initial stream (byteOffset 0) and the seek/refill Range requests (21.3) — both share
// this method, so both depend on the same backpressure.
request.SetBrowserResponseStreamingEnabled(true);
// Use HttpCompletionOption.ResponseHeadersRead to get stream immediately
var response = await _http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
response.EnsureSuccessStatusCode();