feat: add playback engine that runs a schedule
PlaybackEngine plays a generated schedule from start to end without user input. Songs play through the MusicPlayer singleton and the next item starts on TrackFinished. News, ads and moderator placeholders are held for their duration. A song that cannot be started, or that does not finish within its length plus 10 seconds, raises ItemFailed and the engine moves on, so one broken file or a device error does not stop the broadcast. Start replaces a running schedule and Stop ends playback. MusicPlayer now implements a small IAudioPlayer interface so the engine can be tested with a fake player and a fake delay. Refs #9 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4866545f3a
commit
b1e361f728
4 files changed
+322
-1
No files matched your search
@@ -0,0 +1,11 @@
|
||||
namespace Radiosender.Services;
|
||||
|
||||
/// <summary>The part of <see cref="MusicPlayer"/> the playback engine needs; lets tests run without audio.</summary>
|
||||
public interface IAudioPlayer
|
||||
{
|
||||
event EventHandler? TrackFinished;
|
||||
|
||||
void Play(string filePath);
|
||||
|
||||
void Stop();
|
||||
}
|
||||
@@ -11,7 +11,7 @@ namespace Radiosender.Services;
|
||||
/// never on the WPF UI thread. WPF callers must marshal with <c>Dispatcher.Invoke</c> or
|
||||
/// <c>Dispatcher.BeginInvoke</c> before touching UI elements.
|
||||
/// </remarks>
|
||||
public sealed class MusicPlayer
|
||||
public sealed class MusicPlayer : IAudioPlayer
|
||||
{
|
||||
private static readonly Lazy<MusicPlayer> LazyInstance = new(() => new MusicPlayer());
|
||||
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
using Radiosender.Models;
|
||||
|
||||
namespace Radiosender.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Runs a generated schedule from start to end: songs play through the audio player, news, ads and
|
||||
/// moderator placeholders are held for their duration.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Events are raised on the engine's thread-pool thread. WPF callers must marshal with
|
||||
/// <c>Dispatcher.BeginInvoke</c>, not <c>Dispatcher.Invoke</c>: <see cref="Stop"/> waits for that thread,
|
||||
/// so a handler that blocks on the UI thread while the UI calls Stop would deadlock.
|
||||
/// </remarks>
|
||||
public sealed class PlaybackEngine
|
||||
{
|
||||
// Extra time before a song counts as stuck, e.g. after an audio device error that never raises TrackFinished.
|
||||
private static readonly TimeSpan SongGrace = TimeSpan.FromSeconds(10);
|
||||
|
||||
private readonly IAudioPlayer _player;
|
||||
private readonly Func<TimeSpan, CancellationToken, Task> _delay;
|
||||
private readonly Lock _lock = new();
|
||||
private CancellationTokenSource? _cts;
|
||||
private Task _run = Task.CompletedTask;
|
||||
|
||||
public PlaybackEngine() : this(MusicPlayer.Instance, Task.Delay)
|
||||
{
|
||||
}
|
||||
|
||||
public PlaybackEngine(IAudioPlayer player, Func<TimeSpan, CancellationToken, Task> delay)
|
||||
{
|
||||
_player = player;
|
||||
_delay = delay;
|
||||
}
|
||||
|
||||
/// <summary>Raised when an item starts, with its index in <see cref="Items"/>.</summary>
|
||||
public event EventHandler<(int Index, ScheduleItem Item)>? ItemStarted;
|
||||
|
||||
/// <summary>Raised when a song cannot be played; the engine then moves on to the next item.</summary>
|
||||
public event EventHandler<(ScheduleItem Item, Exception Error)>? ItemFailed;
|
||||
|
||||
/// <summary>Raised after the last item has ended. Not raised by <see cref="Stop"/>.</summary>
|
||||
public event EventHandler? Finished;
|
||||
|
||||
public IReadOnlyList<ScheduleItem> Items { get; private set; } = [];
|
||||
|
||||
/// <summary>Index of the item that is playing, or -1 when the engine is not running.</summary>
|
||||
public int CurrentIndex { get; private set; } = -1;
|
||||
|
||||
public bool IsRunning => !_run.IsCompleted;
|
||||
|
||||
/// <summary>Stops any running schedule and starts <paramref name="items"/> from the first item.</summary>
|
||||
public void Start(IReadOnlyList<ScheduleItem> items)
|
||||
{
|
||||
Stop();
|
||||
lock (_lock)
|
||||
{
|
||||
Items = items;
|
||||
_cts = new CancellationTokenSource();
|
||||
var token = _cts.Token;
|
||||
_run = Task.Run(() => RunAsync(token));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Stops playback. Waits until the running schedule has ended.</summary>
|
||||
public void Stop()
|
||||
{
|
||||
Task run;
|
||||
lock (_lock)
|
||||
{
|
||||
_cts?.Cancel();
|
||||
run = _run;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
run.Wait();
|
||||
}
|
||||
catch (AggregateException ex) when (ex.InnerExceptions.All(e => e is OperationCanceledException))
|
||||
{
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_cts?.Dispose();
|
||||
_cts = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunAsync(CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (var i = 0; i < Items.Count; i++)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var item = Items[i];
|
||||
CurrentIndex = i;
|
||||
ItemStarted?.Invoke(this, (i, item));
|
||||
|
||||
if (item.Kind == ScheduleItemKind.Song && item.Song is not null)
|
||||
await PlaySongAsync(item, token);
|
||||
else
|
||||
await _delay(item.Duration, token);
|
||||
}
|
||||
|
||||
CurrentIndex = -1;
|
||||
Finished?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
CurrentIndex = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PlaySongAsync(ScheduleItem item, CancellationToken token)
|
||||
{
|
||||
var finished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
void OnFinished(object? sender, EventArgs e) => finished.TrySetResult();
|
||||
|
||||
_player.TrackFinished += OnFinished;
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_player.Play(item.Song!.FilePath);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
ItemFailed?.Invoke(this, (item, ex));
|
||||
return;
|
||||
}
|
||||
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||
var stuck = _delay(item.Duration + SongGrace, timeout.Token);
|
||||
var done = await Task.WhenAny(finished.Task, stuck);
|
||||
timeout.Cancel();
|
||||
token.ThrowIfCancellationRequested();
|
||||
if (done == stuck)
|
||||
ItemFailed?.Invoke(this, (item, new TimeoutException($"Song did not finish in time: {item.Title}")));
|
||||
}
|
||||
finally
|
||||
{
|
||||
_player.TrackFinished -= OnFinished;
|
||||
// On cancel, timeout or a failed start the song has not ended by itself.
|
||||
if (!finished.Task.IsCompleted) _player.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user