True Streaming Support Draft

This commit is contained in:
daniel-c-harvey
2025-09-15 17:03:36 -04:00
parent 0fa8ac7379
commit 605fc94fbb
16 changed files with 1124 additions and 295 deletions
@@ -33,7 +33,8 @@ public class TrackMediaClient
{
try
{
var response = await _http.GetAsync($"api/track/{trackId}");
// Use HttpCompletionOption.ResponseHeadersRead to get stream immediately
var response = await _http.GetAsync($"api/track/{trackId}", HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
var contentLength = response.Content.Headers.ContentLength ?? 0;
@@ -23,7 +23,7 @@ else
IsLoaded="IsLoaded"
TogglePlayPause="@TogglePlayPause"
Stop="@Stop"/>
@if (IsLoading)
@if (IsLoading && !IsStreaming)
{
<MudProgressCircular Color="Color.Tertiary"
Size="Size.Small"
@@ -58,7 +58,7 @@ else
IsLoaded="IsLoaded"
TogglePlayPause="@TogglePlayPause"
Stop="@Stop"/>
@if (IsLoading)
@if (IsLoading && !IsStreaming)
{
<MudProgressCircular Color="Color.Tertiary"
Size="Size.Small"
@@ -6,12 +6,13 @@ namespace DeepDrftWeb.Client.Controls.AudioPlayerBar;
public partial class AudioPlayerBar : ComponentBase
{
[CascadingParameter] public required IPlayerService PlayerService { get; set; }
[CascadingParameter] public required IStreamingPlayerService PlayerService { get; set; }
private bool _isMinimized = true;
private bool IsLoaded => PlayerService.IsLoaded;
private bool IsLoading => PlayerService.IsLoading;
private bool IsStreaming => PlayerService.CanStartStreaming;
private bool IsPlaying => PlayerService.IsPlaying;
private bool IsPaused => PlayerService.IsPaused;
private double CurrentTime => PlayerService.CurrentTime;
@@ -23,8 +24,8 @@ public partial class AudioPlayerBar : ComponentBase
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
PlayerService.OnStateChanged += StateHasChanged;
PlayerService.OnTrackSelected += Expand;
// Set up EventCallback for track selection
PlayerService.OnTrackSelected = new EventCallback(this, Expand);
}
private async Task Expand()
@@ -1,6 +1,7 @@
using DeepDrftWeb.Client.Services;
using DeepDrftWeb.Client.Clients;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
namespace DeepDrftWeb.Client.Controls;
@@ -8,15 +9,20 @@ public partial class AudioPlayerProvider : ComponentBase
{
[Inject] public required AudioInteropService AudioInterop { get; set; }
[Inject] public required TrackMediaClient TrackMediaClient { get; set; }
[Inject] public required ILogger<StreamingAudioPlayerService> Logger { get; set; }
private AudioPlayerService? _audioPlayerService;
private StreamingAudioPlayerService? _audioPlayerService;
[Parameter] public RenderFragment? ChildContent { get; set; }
protected override void OnInitialized()
{
// Create the service immediately (but don't initialize yet)
_audioPlayerService = new AudioPlayerService(AudioInterop, TrackMediaClient);
_audioPlayerService = new StreamingAudioPlayerService(AudioInterop, TrackMediaClient, Logger);
// Set up EventCallback to properly marshal UI updates back to UI thread
_audioPlayerService.OnStateChanged = new EventCallback(this, StateHasChanged);
// OnTrackSelected will be set by individual child components that need it
}
protected override async Task OnAfterRenderAsync(bool firstRender)
@@ -41,9 +41,9 @@ public class AudioInteropService : IAsyncDisposable
}
// Streaming methods
public async Task<AudioOperationResult> InitializeStreaming(string playerId)
public async Task<AudioOperationResult> InitializeStreaming(string playerId, long totalStreamLength)
{
return await InvokeJsAsync<AudioOperationResult>("DeepDrftAudio.initializeStreaming", playerId);
return await InvokeJsAsync<AudioOperationResult>("DeepDrftAudio.initializeStreaming", playerId, totalStreamLength);
}
public async Task<StreamingResult> ProcessStreamingChunk(string playerId, byte[] audioChunk)
@@ -56,6 +56,11 @@ public class AudioInteropService : IAsyncDisposable
return await InvokeJsAsync<AudioOperationResult>("DeepDrftAudio.startStreamingPlayback", playerId);
}
public async Task<AudioOperationResult> EnsureAudioContextReady(string playerId)
{
return await InvokeJsAsync<AudioOperationResult>("DeepDrftAudio.ensureAudioContextReady", playerId);
}
public async Task<AudioOperationResult> PlayAsync(string playerId)
{
return await InvokeJsAsync<AudioOperationResult>("DeepDrftAudio.play", playerId);
@@ -122,11 +127,6 @@ public class AudioInteropService : IAsyncDisposable
wrapper => wrapper.OnEnd = callback);
}
public async Task<AudioOperationResult> SetOnLoadProgressCallbackAsync(string playerId, Func<double, Task> callback)
{
return await SetCallbackAsync(playerId, "_loadprogress", "setOnLoadProgressCallback", "OnLoadProgressCallback",
wrapper => wrapper.OnLoadProgress = callback);
}
public async Task<AudioOperationResult> DisposePlayerAsync(string playerId)
{
@@ -146,6 +146,8 @@ public class AudioInteropService : IAsyncDisposable
return (T)(object)new AudioOperationResult { Success = false, Error = ex.Message };
if (typeof(T) == typeof(AudioLoadResult))
return (T)(object)new AudioLoadResult { Success = false, Error = ex.Message };
if (typeof(T) == typeof(StreamingResult))
return (T)(object)new StreamingResult { Success = false, Error = ex.Message };
throw;
}
}
@@ -193,7 +195,6 @@ public class AudioPlayerCallback
{
public Func<double, Task>? OnProgress { get; set; }
public Func<Task>? OnEnd { get; set; }
public Func<double, Task>? OnLoadProgress { get; set; }
[JSInvokable]
public async Task OnProgressCallback(double currentTime)
@@ -208,13 +209,6 @@ public class AudioPlayerCallback
if (OnEnd != null)
await OnEnd();
}
[JSInvokable]
public async Task OnLoadProgressCallback(double progress)
{
if (OnLoadProgress != null)
await OnLoadProgress(progress);
}
}
public class AudioOperationResult
@@ -1,33 +1,34 @@
using DeepDrftModels.Entities;
using DeepDrftWeb.Client.Clients;
using Microsoft.AspNetCore.Components;
using NetBlocks.Models;
namespace DeepDrftWeb.Client.Services;
public class AudioPlayerService : IPlayerService, IAsyncDisposable
public abstract class AudioPlayerService : IPlayerService, IAsyncDisposable
{
private readonly AudioInteropService _audioInterop;
private readonly TrackMediaClient _trackMediaClient;
protected readonly AudioInteropService _audioInterop;
protected readonly TrackMediaClient _trackMediaClient;
public string PlayerId { get; private set; } = Guid.NewGuid().ToString();
// State properties
public bool IsInitialized { get; private set; } = false;
public bool IsLoaded { get; private set; } = false;
public bool IsLoading { get; private set; } = false;
public bool IsPlaying { get; private set; } = false;
public bool IsPaused { get; private set; } = false;
public double CurrentTime { get; private set; } = 0;
public double? Duration { get; private set; } = null;
public double Volume { get; private set; } = 0.8;
public double LoadProgress { get; private set; } = 0;
public string? ErrorMessage { get; private set; }
public bool IsInitialized { get; protected set; } = false;
public bool IsLoaded { get; protected set; } = false;
public bool IsLoading { get; protected set; } = false;
public bool IsPlaying { get; protected set; } = false;
public bool IsPaused { get; protected set; } = false;
public double CurrentTime { get; protected set; } = 0;
public double? Duration { get; protected set; } = null;
public double Volume { get; protected set; } = 0.8;
public double LoadProgress { get; protected set; } = 0;
public string? ErrorMessage { get; protected set; }
// Events
public event Action? OnStateChanged;
public event Events.EventAsync? OnTrackSelected;
public EventCallback? OnStateChanged { get; set; }
public EventCallback? OnTrackSelected { get; set; }
public AudioPlayerService(AudioInteropService audioInterop, TrackMediaClient trackMediaClient)
protected AudioPlayerService(AudioInteropService audioInterop, TrackMediaClient trackMediaClient)
{
_audioInterop = audioInterop;
_trackMediaClient = trackMediaClient;
@@ -43,38 +44,37 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
if (!result.Success)
{
ErrorMessage = $"Failed to initialize audio player: {result.Error}";
NotifyStateChanged();
await NotifyStateChanged();
return;
}
await _audioInterop.SetOnProgressCallbackAsync(PlayerId, OnProgressCallback);
await _audioInterop.SetOnEndCallbackAsync(PlayerId, OnPlaybackEndCallback);
await _audioInterop.SetOnLoadProgressCallbackAsync(PlayerId, OnLoadProgressCallback);
await _audioInterop.SetVolumeAsync(PlayerId, Volume);
IsInitialized = true;
ErrorMessage = null;
NotifyStateChanged();
await NotifyStateChanged();
}
catch (Exception ex)
{
ErrorMessage = $"Failed to initialize audio player: {ex.Message}";
NotifyStateChanged();
await NotifyStateChanged();
}
}
public async Task SelectTrack(TrackEntity track)
public virtual async Task SelectTrack(TrackEntity track)
{
await EnsureInitializedAsync();
NotifyStateChanged();
await NotifyStateChanged();
if (OnTrackSelected != null)
await OnTrackSelected.Invoke();
if (OnTrackSelected.HasValue)
await OnTrackSelected.Value.InvokeAsync();
await LoadTrack(track);
NotifyStateChanged();
await NotifyStateChanged();
}
private async Task LoadTrack(TrackEntity track)
@@ -95,7 +95,7 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
IsLoading = true;
Duration = null;
CurrentTime = 0;
NotifyStateChanged();
await NotifyStateChanged();
var loadResult = await _audioInterop.InitializeBufferedPlayerAsync(PlayerId);
if (loadResult?.Success != true)
@@ -129,7 +129,7 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
finally
{
IsLoading = false;
NotifyStateChanged();
await NotifyStateChanged();
}
}
@@ -166,7 +166,7 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
if (audio.ContentLength > 0)
{
LoadProgress = Math.Min(1.0, (double)totalBytesRead / audio.ContentLength);
NotifyStateChanged();
await NotifyStateChanged();
}
}
} while (currentBytes > 0);
@@ -181,14 +181,14 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
LoadProgress = 1.0;
IsLoaded = true;
ErrorMessage = null;
NotifyStateChanged();
await NotifyStateChanged();
}
catch (Exception ex)
{
ErrorMessage = $"Error streaming audio: {ex.Message}";
LoadProgress = 0;
IsLoaded = false;
NotifyStateChanged();
await NotifyStateChanged();
throw;
}
}
@@ -229,12 +229,12 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
ErrorMessage = null;
}
NotifyStateChanged();
await NotifyStateChanged();
}
catch (Exception ex)
{
ErrorMessage = $"Error controlling playback: {ex.Message}";
NotifyStateChanged();
await NotifyStateChanged();
}
}
@@ -257,16 +257,16 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
ErrorMessage = $"Stop error: {result.Error}";
}
NotifyStateChanged();
await NotifyStateChanged();
}
catch (Exception ex)
{
ErrorMessage = $"Error stopping playback: {ex.Message}";
NotifyStateChanged();
await NotifyStateChanged();
}
}
public async Task Unload()
public virtual async Task Unload()
{
if (!IsLoaded) return;
@@ -289,12 +289,12 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
ErrorMessage = $"Unload error: {result.Error}";
}
NotifyStateChanged();
await NotifyStateChanged();
}
catch (Exception ex)
{
ErrorMessage = $"Error unloading track: {ex.Message}";
NotifyStateChanged();
await NotifyStateChanged();
}
}
@@ -315,12 +315,12 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
ErrorMessage = $"Seek error: {result.Error}";
}
NotifyStateChanged();
await NotifyStateChanged();
}
catch (Exception ex)
{
ErrorMessage = $"Error seeking: {ex.Message}";
NotifyStateChanged();
await NotifyStateChanged();
}
}
@@ -348,19 +348,19 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
}
}
NotifyStateChanged();
await NotifyStateChanged();
}
public void ClearError()
public async Task ClearError()
{
ErrorMessage = null;
NotifyStateChanged();
await NotifyStateChanged();
}
private async Task OnProgressCallback(double currentTime)
{
CurrentTime = currentTime;
NotifyStateChanged();
await NotifyStateChanged();
}
private async Task OnPlaybackEndCallback()
@@ -368,16 +368,11 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
IsPlaying = false;
IsPaused = false;
CurrentTime = 0;
NotifyStateChanged();
await NotifyStateChanged();
}
private async Task OnLoadProgressCallback(double progress)
{
LoadProgress = progress;
NotifyStateChanged();
}
private async Task EnsureInitializedAsync()
protected async Task EnsureInitializedAsync()
{
if (!IsInitialized)
{
@@ -385,9 +380,16 @@ public class AudioPlayerService : IPlayerService, IAsyncDisposable
}
}
private void NotifyStateChanged()
protected async Task NotifyStateChanged()
{
OnStateChanged?.Invoke();
if (OnStateChanged.HasValue)
await OnStateChanged.Value.InvokeAsync();
}
protected async Task NotifyTrackSelected()
{
if (OnTrackSelected.HasValue)
await OnTrackSelected.Value.InvokeAsync();
}
public async ValueTask DisposeAsync()
@@ -0,0 +1,301 @@
using DeepDrftModels.Entities;
using DeepDrftWeb.Client.Clients;
using System.Buffers;
using Microsoft.Extensions.Logging;
namespace DeepDrftWeb.Client.Services;
public class StreamingAudioPlayerService : AudioPlayerService, IStreamingPlayerService
{
// Configuration constants
private const int DefaultBufferSize = 32 * 1024; // 32KB chunks
private const int NotificationThrottleMs = 100; // Throttle UI updates to max 10 per second
// Adaptive chunk sizing
private const int MinBufferSize = 16 * 1024; // 16KB minimum
private const int MaxBufferSize = 64 * 1024; // 64KB maximum
private int _currentBufferSize = DefaultBufferSize;
private int _consecutiveSlowReads = 0;
// Streaming state properties
public bool IsStreamingMode { get; private set; } = false;
public bool CanStartStreaming { get; private set; } = false;
public bool HeaderParsed { get; private set; } = false;
public int BufferedChunks { get; private set; } = 0;
private bool _streamingPlaybackStarted = false;
private CancellationTokenSource? _streamingCancellation;
private DateTime _lastNotification = DateTime.MinValue;
private readonly ILogger<StreamingAudioPlayerService> _logger;
public StreamingAudioPlayerService(
AudioInteropService audioInterop,
TrackMediaClient trackMediaClient,
ILogger<StreamingAudioPlayerService> logger)
: base(audioInterop, trackMediaClient)
{
_logger = logger;
}
public override async Task SelectTrack(TrackEntity track)
{
await SelectTrackStreaming(track);
}
public async Task SelectTrackStreaming(TrackEntity track)
{
await EnsureInitializedAsync();
// Resume AudioContext immediately on track selection (user interaction) to avoid clicks later
await _audioInterop.EnsureAudioContextReady(PlayerId);
// NotifyStateChanged();
await NotifyTrackSelected();
await LoadTrackStreaming(track);
await NotifyStateChanged();
}
private async Task LoadTrackStreaming(TrackEntity track)
{
// Cancel and replace any previous streaming operation atomically
var oldCancellation = _streamingCancellation;
_streamingCancellation = new CancellationTokenSource();
// Cancel the old operation after we've replaced it
oldCancellation?.Cancel();
oldCancellation?.Dispose();
try
{
// No need to check IsLoading - we cancel previous operations
if (IsPlaying || IsPaused)
{
await Unload();
}
// Reset state to indicate streaming has started
ErrorMessage = null;
LoadProgress = 0;
IsLoaded = false;
IsLoading = true;
IsStreamingMode = true;
CanStartStreaming = false;
HeaderParsed = false;
BufferedChunks = 0;
_streamingPlaybackStarted = false;
Duration = null;
CurrentTime = 0;
// Reset adaptive buffer sizing
_currentBufferSize = DefaultBufferSize;
_consecutiveSlowReads = 0;
await NotifyStateChanged();
var mediaResult = await _trackMediaClient.GetTrackMedia(track.EntryKey);
if (!mediaResult.Success)
{
var technicalError = mediaResult.GetMessage();
_logger.LogError("Failed to get track media for {TrackId}: {Error}",
track.EntryKey, technicalError);
ErrorMessage = StreamingErrorHandler.GetUserFriendlyMessage(technicalError);
return;
}
if (mediaResult.Value == null)
{
const string technicalError = "No audio returned from server";
_logger.LogError("No audio data returned for track {TrackId}", track.EntryKey);
ErrorMessage = StreamingErrorHandler.GetUserFriendlyMessage(technicalError);
return;
}
using var audio = mediaResult.Value;
// Initialize streaming mode with content length
var streamingResult = await _audioInterop.InitializeStreaming(PlayerId, audio.ContentLength);
if (!streamingResult.Success)
{
var technicalError = $"Failed to initialize streaming: {streamingResult.Error}";
_logger.LogError("Streaming initialization failed for track {TrackId}: {Error}",
track.EntryKey, technicalError);
ErrorMessage = StreamingErrorHandler.GetUserFriendlyMessage(technicalError);
return;
}
await StreamAudioWithEarlyPlayback(audio, _streamingCancellation.Token);
}
catch (OperationCanceledException)
{
// Cancellation is expected, reset state
_logger.LogDebug("Audio streaming cancelled for track {TrackId}", track.EntryKey);
IsLoaded = false;
IsStreamingMode = false;
}
catch (Exception ex)
{
StreamingErrorHandler.LogError(_logger, ex, "LoadTrackStreaming", track.EntryKey);
ErrorMessage = StreamingErrorHandler.GetUserFriendlyMessage(ex.Message);
LoadProgress = 0;
IsLoaded = false;
IsStreamingMode = false;
}
finally
{
IsLoading = false;
await NotifyStateChanged();
}
}
private async Task StreamAudioWithEarlyPlayback(TrackMediaResponse audio, CancellationToken cancellationToken)
{
byte[]? buffer = null;
try
{
long totalBytesRead = 0;
buffer = ArrayPool<byte>.Shared.Rent(MaxBufferSize); // Rent larger buffer to accommodate adaptive sizing
int currentBytes;
var readTimer = System.Diagnostics.Stopwatch.StartNew();
do
{
readTimer.Restart();
currentBytes = await audio.Stream.ReadAsync(buffer, 0, _currentBufferSize, cancellationToken);
readTimer.Stop();
// Adapt buffer size based on read performance
AdaptBufferSize(currentBytes, readTimer.ElapsedMilliseconds);
if (currentBytes > 0)
{
totalBytesRead += currentBytes;
// Use only the actual bytes read, no copying needed
var actualBuffer = currentBytes == _currentBufferSize ? buffer : buffer[..currentBytes];
// Process chunk for streaming
var chunkResult = await _audioInterop.ProcessStreamingChunk(PlayerId, actualBuffer);
if (!chunkResult.Success)
{
var error = $"Failed to process streaming chunk: {chunkResult.Error}";
_logger.LogWarning("Chunk processing failed: {Error}", error);
throw new Exception(error);
}
// Update streaming state
CanStartStreaming = chunkResult.CanStartStreaming;
HeaderParsed = chunkResult.HeaderParsed;
BufferedChunks = chunkResult.BufferCount;
// Start playback as soon as we can
if (!_streamingPlaybackStarted && CanStartStreaming)
{
var playbackResult = await _audioInterop.StartStreamingPlayback(PlayerId);
if (playbackResult.Success)
{
_streamingPlaybackStarted = true;
IsPlaying = true;
IsPaused = false;
IsLoaded = true; // Track is loaded and ready to play (even if still downloading)
ErrorMessage = null;
await NotifyStateChanged(); // Immediate notification for critical state change
}
else
{
var technicalError = $"Failed to start streaming playback: {playbackResult.Error}";
_logger.LogError("Failed to start playback: {Error}", technicalError);
ErrorMessage = StreamingErrorHandler.GetUserFriendlyMessage(technicalError);
}
}
// Update progress
if (audio.ContentLength > 0)
{
LoadProgress = Math.Min(1.0, (double)totalBytesRead / audio.ContentLength);
}
await ThrottledNotifyStateChanged();
}
} while (currentBytes > 0);
// Mark as fully loaded
LoadProgress = 1.0;
await NotifyStateChanged();
}
catch (Exception ex)
{
StreamingErrorHandler.LogError(_logger, ex, "StreamAudioWithEarlyPlayback");
ErrorMessage = StreamingErrorHandler.GetUserFriendlyMessage(ex.Message);
LoadProgress = 0;
IsLoaded = false;
IsStreamingMode = false;
await NotifyStateChanged();
throw;
}
finally
{
if (buffer != null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
}
public override async Task Unload()
{
// Cancel any ongoing streaming operation
_streamingCancellation?.Cancel();
_streamingCancellation?.Dispose();
_streamingCancellation = null;
IsStreamingMode = false;
CanStartStreaming = false;
HeaderParsed = false;
BufferedChunks = 0;
_streamingPlaybackStarted = false;
await base.Unload();
}
private async Task ThrottledNotifyStateChanged()
{
var now = DateTime.UtcNow;
if ((now - _lastNotification).TotalMilliseconds >= NotificationThrottleMs)
{
_lastNotification = now;
await NotifyStateChanged();
}
}
private void AdaptBufferSize(int bytesRead, long readTimeMs)
{
// Adaptive buffer sizing based on network performance
if (readTimeMs > 100) // Slow read (>100ms)
{
_consecutiveSlowReads++;
if (_consecutiveSlowReads >= 3 && _currentBufferSize > MinBufferSize)
{
// Reduce buffer size for slow connections
_currentBufferSize = Math.Max(MinBufferSize, _currentBufferSize / 2);
_consecutiveSlowReads = 0;
}
}
else if (readTimeMs < 20 && bytesRead == _currentBufferSize) // Fast read, buffer fully utilized
{
_consecutiveSlowReads = 0;
if (_currentBufferSize < MaxBufferSize)
{
// Increase buffer size for fast connections
_currentBufferSize = Math.Min(MaxBufferSize, _currentBufferSize * 2);
}
}
else
{
_consecutiveSlowReads = 0;
}
}
}
@@ -0,0 +1,30 @@
using Microsoft.Extensions.Logging;
namespace DeepDrftWeb.Client.Services;
public static class StreamingErrorHandler
{
public static string GetUserFriendlyMessage(string technicalError)
{
var lowerError = technicalError.ToLowerInvariant();
return lowerError switch
{
_ when lowerError.Contains("network") || lowerError.Contains("connection") || lowerError.Contains("timeout") =>
"Unable to load audio. Please check your connection and try again.",
_ when lowerError.Contains("audio") || lowerError.Contains("decode") || lowerError.Contains("format") =>
"This audio file may be corrupted or in an unsupported format.",
_ when lowerError.Contains("cancel") || lowerError.Contains("abort") =>
"Audio loading was cancelled.",
_ => "Unable to play audio. Please try again."
};
}
public static void LogError(ILogger logger, Exception ex, string operation, string trackId = "")
{
logger.LogError(ex, "Streaming error in {Operation} for track {TrackId}", operation, trackId);
}
}