Skip to content

Commit 6fec298

Browse files
committed
Client: play queue support
1 parent 5cab9f1 commit 6fec298

File tree

6 files changed

+159
-4
lines changed

6 files changed

+159
-4
lines changed

src/Client/IPlayerClient.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,53 @@ ValueTask<PlayerState> GetPlayerState(
153153
/// <returns>Request task.</returns>
154154
ValueTask SeekRelative(TimeSpan offset, CancellationToken cancellationToken = default);
155155

156+
// Playback queue API
157+
158+
/// <summary>
159+
/// Gets play queue with optional columns.
160+
/// </summary>
161+
/// <param name="columns">Play queue item columns.</param>
162+
/// <param name="cancellationToken">Cancellation token.</param>
163+
/// <returns>Request task.</returns>
164+
ValueTask<IList<PlayQueueItemInfo>> GetPlayQueue(
165+
IReadOnlyList<string>? columns = null, CancellationToken cancellationToken = default);
166+
167+
/// <summary>
168+
/// Add specified item to play queue.
169+
/// By default items are added at the end of the queue.
170+
/// </summary>
171+
/// <param name="playlist">Playlist to use.</param>
172+
/// <param name="itemIndex">Index of item to add.</param>
173+
/// <param name="queueIndex">Queue index to insert item at (DeaDBeeF only).</param>
174+
/// <param name="cancellationToken">Cancellation token.</param>
175+
/// <returns>Request task.</returns>
176+
ValueTask AddToPlayQueue(PlaylistRef playlist, int itemIndex, int? queueIndex = null,
177+
CancellationToken cancellationToken = default);
178+
179+
/// <summary>
180+
/// Removes specified item from play queue.
181+
/// </summary>
182+
/// <param name="queueIndex">Item index in queue.</param>
183+
/// <param name="cancellationToken">Cancellation token.</param>
184+
/// <returns>Request task.</returns>
185+
ValueTask RemoveFromPlayQueue(int queueIndex, CancellationToken cancellationToken = default);
186+
187+
/// <summary>
188+
/// Removes specified item from play queue.
189+
/// </summary>
190+
/// <param name="playlist">Playlist to use.</param>
191+
/// <param name="itemIndex">Item index to remove.</param>
192+
/// <param name="cancellationToken">Cancellation token.</param>
193+
/// <returns>Request task.</returns>
194+
ValueTask RemoveFromPlayQueue(PlaylistRef playlist, int itemIndex, CancellationToken cancellationToken = default);
195+
196+
/// <summary>
197+
/// Removes all items from play queue.
198+
/// </summary>
199+
/// <param name="cancellationToken">Cancellation token.</param>
200+
/// <returns>Request task.</returns>
201+
ValueTask ClearPlayQueue(CancellationToken cancellationToken = default);
202+
156203
// Playlists API
157204

158205
/// <summary>

src/Client/IPlayerQuery.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public interface IPlayerQuery
3232
IPlayerQuery IncludePlaylistItems(
3333
PlaylistRef playlist, PlaylistItemRange itemRange, IReadOnlyList<string> itemColumns);
3434

35+
/// <summary>
36+
/// Configures query to include play queue.
37+
/// </summary>
38+
/// <param name="columns">List of columns of queued items to return.</param>
39+
/// <returns>Configured query.</returns>
40+
IPlayerQuery IncludePlayQueue(IReadOnlyList<string>? columns = null);
41+
3542
/// <summary>
3643
/// Executes this query and returns player information.
3744
/// </summary>

src/Client/PlayQueueItemInfo.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
3+
namespace Beefweb.Client;
4+
5+
/// <summary>
6+
/// Play queue item information.
7+
/// </summary>
8+
public sealed class PlayQueueItemInfo
9+
{
10+
/// <summary>
11+
/// Playlist index.
12+
/// </summary>
13+
public int PlaylistIndex { get; set; }
14+
15+
/// <summary>
16+
/// Playlist id.
17+
/// </summary>
18+
public string PlaylistId { get; set; } = null!;
19+
20+
/// <summary>
21+
/// Item index in playlist.
22+
/// </summary>
23+
public int ItemIndex { get; set; }
24+
25+
/// <summary>
26+
/// Requested item columns.
27+
/// </summary>
28+
public IList<string> Columns { get; set; } = null!;
29+
}

src/Client/PlayerClient.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async ValueTask<PlayerState> GetPlayerState(
5151
IReadOnlyList<string>? activeItemColumns = null,
5252
CancellationToken cancellationToken = default)
5353
{
54-
var queryParams = activeItemColumns?.Count > 0
54+
var queryParams = activeItemColumns is { Count: > 0 }
5555
? new QueryParameterCollection { ["columns"] = activeItemColumns }
5656
: null;
5757

@@ -174,6 +174,55 @@ await SetPlayerState(new SetPlayerStateRequest { RelativePosition = offset }, ca
174174
.ConfigureAwait(false);
175175
}
176176

177+
// Playback queue API
178+
179+
/// <inheritdoc />
180+
public async ValueTask<IList<PlayQueueItemInfo>> GetPlayQueue(
181+
IReadOnlyList<string>? columns = null, CancellationToken cancellationToken = default)
182+
{
183+
var queryParams = columns is { Count: > 0 }
184+
? new QueryParameterCollection { ["columns"] = columns }
185+
: null;
186+
187+
var result = await _handler
188+
.Get<PlayerQueryResult>("api/playqueue", queryParams, cancellationToken)
189+
.ConfigureAwait(false);
190+
191+
return result.PlayQueue ?? throw PropertyIsNull("playlistItems");
192+
}
193+
194+
/// <inheritdoc />
195+
public async ValueTask AddToPlayQueue(PlaylistRef playlist, int itemIndex, int? queueIndex = null,
196+
CancellationToken cancellationToken = default)
197+
{
198+
var body = queueIndex != null
199+
? new { plref = playlist.GetValue(), itemIndex, queueIndex }
200+
: (object) new { plref = playlist.GetValue(), itemIndex };
201+
202+
await _handler.Post("api/playqueue/add", body, cancellationToken).ConfigureAwait(false);
203+
}
204+
205+
/// <inheritdoc />
206+
public async ValueTask RemoveFromPlayQueue(int queueIndex, CancellationToken cancellationToken = default)
207+
{
208+
await _handler.Post("api/playqueue/remove", new { queueIndex }, cancellationToken).ConfigureAwait(false);
209+
}
210+
211+
/// <inheritdoc />
212+
public async ValueTask RemoveFromPlayQueue(
213+
PlaylistRef playlist, int itemIndex, CancellationToken cancellationToken = default)
214+
{
215+
await _handler
216+
.Post("api/playqueue/remove", new { plref = playlist.GetValue(), itemIndex }, cancellationToken)
217+
.ConfigureAwait(false);
218+
}
219+
220+
/// <inheritdoc />
221+
public async ValueTask ClearPlayQueue(CancellationToken cancellationToken = default)
222+
{
223+
await _handler.Post("api/playqueue/clear", null, cancellationToken).ConfigureAwait(false);
224+
}
225+
177226
// Playlists API
178227

179228
/// <inheritdoc />

src/Client/PlayerQuery.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ internal sealed class PlayerQuery : IPlayerQuery
1111
private readonly IRequestHandler _handler;
1212

1313
private bool _includePlayer;
14+
private bool _includePlayQueue;
1415
private bool _includePlaylists;
1516
private bool _includePlaylistItems;
1617
private IReadOnlyList<string>? _activeItemColumns;
18+
private IReadOnlyList<string>? _playQueueColumns;
1719
private IReadOnlyList<string>? _playlistItemColumns;
1820
private PlaylistRef _playlist;
1921
private PlaylistItemRange _playlistItemRange;
@@ -46,6 +48,13 @@ public IPlayerQuery IncludePlaylistItems(
4648
return this;
4749
}
4850

51+
public IPlayerQuery IncludePlayQueue(IReadOnlyList<string>? columns = null)
52+
{
53+
_includePlayQueue = true;
54+
_playQueueColumns = columns;
55+
return this;
56+
}
57+
4958
public ValueTask<PlayerQueryResult> Execute(CancellationToken cancellationToken = default)
5059
{
5160
return _handler.Get<PlayerQueryResult>("api/query", BuildQuery(), cancellationToken);
@@ -69,10 +78,18 @@ private QueryParameterCollection BuildQuery()
6978
{
7079
query["player"] = true;
7180

72-
if (_activeItemColumns?.Count > 0)
81+
if (_activeItemColumns is { Count: > 0 })
7382
query["trcolumns"] = _activeItemColumns;
7483
}
7584

85+
if (_includePlayQueue)
86+
{
87+
query["playQueue"] = true;
88+
89+
if (_playQueueColumns is { Count: > 0 })
90+
query["qcolumns"] = _playQueueColumns;
91+
}
92+
7693
if (_includePlaylists)
7794
{
7895
query["playlists"] = true;
@@ -84,7 +101,7 @@ private QueryParameterCollection BuildQuery()
84101
query["plref"] = _playlist;
85102
query["plrange"] = _playlistItemRange;
86103

87-
if (_playlistItemColumns?.Count > 0)
104+
if (_playlistItemColumns is { Count: > 0 })
88105
query["plcolumns"] = _playlistItemColumns;
89106
}
90107

src/Client/PlayerQueryResult.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections;
12
using System.Collections.Generic;
23

34
namespace Beefweb.Client;
@@ -21,4 +22,9 @@ public sealed class PlayerQueryResult
2122
/// Requested playlist items.
2223
/// </summary>
2324
public PlaylistItemsResult? PlaylistItems { get; set; }
24-
}
25+
26+
/// <summary>
27+
/// Play queue contents.
28+
/// </summary>
29+
public IList<PlayQueueItemInfo>? PlayQueue { get; set; }
30+
}

0 commit comments

Comments
 (0)