feat(playback): re-time or end the item on air
ReplaceCurrent hands the engine a plan in which the running item changed. A news, ad or moderator item then runs for its new length, counted from its start, and a song ends at once. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
5193427629
commit
1482402852
1 file changed
+55
-3
@@ -27,6 +27,9 @@ public sealed class PlaybackEngine
|
|||||||
private CancellationTokenSource? _cts;
|
private CancellationTokenSource? _cts;
|
||||||
private Task _run = Task.CompletedTask;
|
private Task _run = Task.CompletedTask;
|
||||||
|
|
||||||
|
// Completed by ReplaceCurrent to wake the wait of the item on air; a new one per wait.
|
||||||
|
private TaskCompletionSource? _currentChanged;
|
||||||
|
|
||||||
public PlaybackEngine() : this(MusicPlayer.Instance, Task.Delay, () => DateTime.Now)
|
public PlaybackEngine() : this(MusicPlayer.Instance, Task.Delay, () => DateTime.Now)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -88,6 +91,24 @@ public sealed class PlaybackEngine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Switches to <paramref name="items"/> while the item at <paramref name="index"/> is on air and that item
|
||||||
|
/// itself changed: a news, ad or moderator item then runs for its new length, counted from when it started,
|
||||||
|
/// and a song ends at once. Items before <paramref name="index"/> must be unchanged. Returns <c>false</c>
|
||||||
|
/// if that item is not the one on air.
|
||||||
|
/// </summary>
|
||||||
|
public bool ReplaceCurrent(IReadOnlyList<ScheduleItem> items, int index)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (!IsRunning || CurrentIndex != index || index >= items.Count)
|
||||||
|
return false;
|
||||||
|
Items = items;
|
||||||
|
_currentChanged?.TrySetResult();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Stops playback. Waits until the running schedule has ended.</summary>
|
/// <summary>Stops playback. Waits until the running schedule has ended.</summary>
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
@@ -151,7 +172,7 @@ public sealed class PlaybackEngine
|
|||||||
if (item.Kind == ScheduleItemKind.Song && item.Song is not null)
|
if (item.Kind == ScheduleItemKind.Song && item.Song is not null)
|
||||||
await PlaySongAsync(item, token);
|
await PlaySongAsync(item, token);
|
||||||
else
|
else
|
||||||
await _delay(item.Duration, token);
|
await HoldAsync(i, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
CurrentIndex = -1;
|
CurrentIndex = -1;
|
||||||
@@ -163,6 +184,33 @@ public sealed class PlaybackEngine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keeps a news, ad or moderator item on air for its length. ReplaceCurrent can change that length
|
||||||
|
// meanwhile; the wait then starts over with what is left.
|
||||||
|
private async Task HoldAsync(int index, CancellationToken token)
|
||||||
|
{
|
||||||
|
var startedAt = _now();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
TaskCompletionSource changed;
|
||||||
|
TimeSpan remaining;
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
changed = _currentChanged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
|
remaining = Items[index].Duration - (_now() - startedAt);
|
||||||
|
}
|
||||||
|
if (remaining <= TimeSpan.Zero)
|
||||||
|
return;
|
||||||
|
|
||||||
|
using var wait = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||||
|
var delay = _delay(remaining, wait.Token);
|
||||||
|
var done = await Task.WhenAny(delay, changed.Task);
|
||||||
|
wait.Cancel();
|
||||||
|
token.ThrowIfCancellationRequested();
|
||||||
|
if (done == delay)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task PlaySongAsync(ScheduleItem item, CancellationToken token)
|
private async Task PlaySongAsync(ScheduleItem item, CancellationToken token)
|
||||||
{
|
{
|
||||||
var finished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
var finished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
@@ -181,13 +229,17 @@ public sealed class PlaybackEngine
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TaskCompletionSource changed;
|
||||||
|
lock (_lock)
|
||||||
|
changed = _currentChanged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
|
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||||
var stuck = _delay(item.Duration + SongGrace, timeout.Token);
|
var stuck = _delay(item.Duration + SongGrace, timeout.Token);
|
||||||
var done = await Task.WhenAny(finished.Task, stuck);
|
// changed: the user ended the song early; the finally block stops the player.
|
||||||
|
var done = await Task.WhenAny(finished.Task, stuck, changed.Task);
|
||||||
timeout.Cancel();
|
timeout.Cancel();
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
if (done == stuck)
|
if (done == stuck)
|
||||||
ItemFailed?.Invoke(this, (item, new TimeoutException($"Song did not finish in time: {item.Title}")));
|
ItemFailed?.Invoke(this, (item, new TimeoutException($"Der Song wurde nicht rechtzeitig fertig: {item.Title}")));
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user