PlayQueueManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Jellyfin.Extensions;
  6. using MediaBrowser.Model.SyncPlay;
  7. namespace MediaBrowser.Controller.SyncPlay.Queue
  8. {
  9. /// <summary>
  10. /// Class PlayQueueManager.
  11. /// </summary>
  12. public class PlayQueueManager
  13. {
  14. /// <summary>
  15. /// Placeholder index for when no item is playing.
  16. /// </summary>
  17. /// <value>The no-playing item index.</value>
  18. private const int NoPlayingItemIndex = -1;
  19. /// <summary>
  20. /// The sorted playlist.
  21. /// </summary>
  22. /// <value>The sorted playlist, or play queue of the group.</value>
  23. private List<SyncPlayQueueItem> _sortedPlaylist = new List<SyncPlayQueueItem>();
  24. /// <summary>
  25. /// The shuffled playlist.
  26. /// </summary>
  27. /// <value>The shuffled playlist, or play queue of the group.</value>
  28. private List<SyncPlayQueueItem> _shuffledPlaylist = new List<SyncPlayQueueItem>();
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="PlayQueueManager" /> class.
  31. /// </summary>
  32. public PlayQueueManager()
  33. {
  34. Reset();
  35. }
  36. /// <summary>
  37. /// Gets the playing item index.
  38. /// </summary>
  39. /// <value>The playing item index.</value>
  40. public int PlayingItemIndex { get; private set; }
  41. /// <summary>
  42. /// Gets the last time the queue has been changed.
  43. /// </summary>
  44. /// <value>The last time the queue has been changed.</value>
  45. public DateTime LastChange { get; private set; }
  46. /// <summary>
  47. /// Gets the shuffle mode.
  48. /// </summary>
  49. /// <value>The shuffle mode.</value>
  50. public GroupShuffleMode ShuffleMode { get; private set; } = GroupShuffleMode.Sorted;
  51. /// <summary>
  52. /// Gets the repeat mode.
  53. /// </summary>
  54. /// <value>The repeat mode.</value>
  55. public GroupRepeatMode RepeatMode { get; private set; } = GroupRepeatMode.RepeatNone;
  56. /// <summary>
  57. /// Checks if an item is playing.
  58. /// </summary>
  59. /// <returns><c>true</c> if an item is playing; <c>false</c> otherwise.</returns>
  60. public bool IsItemPlaying()
  61. {
  62. return PlayingItemIndex != NoPlayingItemIndex;
  63. }
  64. /// <summary>
  65. /// Gets the current playlist considering the shuffle mode.
  66. /// </summary>
  67. /// <returns>The playlist.</returns>
  68. public IReadOnlyList<SyncPlayQueueItem> GetPlaylist()
  69. {
  70. return GetPlaylistInternal();
  71. }
  72. /// <summary>
  73. /// Sets a new playlist. Playing item is reset.
  74. /// </summary>
  75. /// <param name="items">The new items of the playlist.</param>
  76. public void SetPlaylist(IReadOnlyList<Guid> items)
  77. {
  78. _sortedPlaylist.Clear();
  79. _shuffledPlaylist.Clear();
  80. _sortedPlaylist = CreateQueueItemsFromArray(items);
  81. if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
  82. {
  83. _shuffledPlaylist = new List<SyncPlayQueueItem>(_sortedPlaylist);
  84. _shuffledPlaylist.Shuffle();
  85. }
  86. PlayingItemIndex = NoPlayingItemIndex;
  87. LastChange = DateTime.UtcNow;
  88. }
  89. /// <summary>
  90. /// Appends new items to the playlist. The specified order is maintained.
  91. /// </summary>
  92. /// <param name="items">The items to add to the playlist.</param>
  93. public void Queue(IReadOnlyList<Guid> items)
  94. {
  95. var newItems = CreateQueueItemsFromArray(items);
  96. _sortedPlaylist.AddRange(newItems);
  97. if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
  98. {
  99. _shuffledPlaylist.AddRange(newItems);
  100. }
  101. LastChange = DateTime.UtcNow;
  102. }
  103. /// <summary>
  104. /// Shuffles the playlist. Shuffle mode is changed. The playlist gets re-shuffled if already shuffled.
  105. /// </summary>
  106. public void ShufflePlaylist()
  107. {
  108. if (PlayingItemIndex == NoPlayingItemIndex)
  109. {
  110. _shuffledPlaylist = new List<SyncPlayQueueItem>(_sortedPlaylist);
  111. _shuffledPlaylist.Shuffle();
  112. }
  113. else if (ShuffleMode.Equals(GroupShuffleMode.Sorted))
  114. {
  115. // First time shuffle.
  116. var playingItem = _sortedPlaylist[PlayingItemIndex];
  117. _shuffledPlaylist = new List<SyncPlayQueueItem>(_sortedPlaylist);
  118. _shuffledPlaylist.RemoveAt(PlayingItemIndex);
  119. _shuffledPlaylist.Shuffle();
  120. _shuffledPlaylist.Insert(0, playingItem);
  121. PlayingItemIndex = 0;
  122. }
  123. else
  124. {
  125. // Re-shuffle playlist.
  126. var playingItem = _shuffledPlaylist[PlayingItemIndex];
  127. _shuffledPlaylist.RemoveAt(PlayingItemIndex);
  128. _shuffledPlaylist.Shuffle();
  129. _shuffledPlaylist.Insert(0, playingItem);
  130. PlayingItemIndex = 0;
  131. }
  132. ShuffleMode = GroupShuffleMode.Shuffle;
  133. LastChange = DateTime.UtcNow;
  134. }
  135. /// <summary>
  136. /// Resets the playlist to sorted mode. Shuffle mode is changed.
  137. /// </summary>
  138. public void RestoreSortedPlaylist()
  139. {
  140. if (PlayingItemIndex != NoPlayingItemIndex)
  141. {
  142. var playingItem = _shuffledPlaylist[PlayingItemIndex];
  143. PlayingItemIndex = _sortedPlaylist.IndexOf(playingItem);
  144. }
  145. _shuffledPlaylist.Clear();
  146. ShuffleMode = GroupShuffleMode.Sorted;
  147. LastChange = DateTime.UtcNow;
  148. }
  149. /// <summary>
  150. /// Clears the playlist. Shuffle mode is preserved.
  151. /// </summary>
  152. /// <param name="clearPlayingItem">Whether to remove the playing item as well.</param>
  153. public void ClearPlaylist(bool clearPlayingItem)
  154. {
  155. var playingItem = GetPlayingItem();
  156. _sortedPlaylist.Clear();
  157. _shuffledPlaylist.Clear();
  158. LastChange = DateTime.UtcNow;
  159. if (!clearPlayingItem && playingItem is not null)
  160. {
  161. _sortedPlaylist.Add(playingItem);
  162. if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
  163. {
  164. _shuffledPlaylist.Add(playingItem);
  165. }
  166. PlayingItemIndex = 0;
  167. }
  168. else
  169. {
  170. PlayingItemIndex = NoPlayingItemIndex;
  171. }
  172. }
  173. /// <summary>
  174. /// Adds new items to the playlist right after the playing item. The specified order is maintained.
  175. /// </summary>
  176. /// <param name="items">The items to add to the playlist.</param>
  177. public void QueueNext(IReadOnlyList<Guid> items)
  178. {
  179. var newItems = CreateQueueItemsFromArray(items);
  180. if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
  181. {
  182. var playingItem = GetPlayingItem();
  183. var sortedPlayingItemIndex = _sortedPlaylist.IndexOf(playingItem);
  184. // Append items to sorted and shuffled playlist as they are.
  185. _sortedPlaylist.InsertRange(sortedPlayingItemIndex + 1, newItems);
  186. _shuffledPlaylist.InsertRange(PlayingItemIndex + 1, newItems);
  187. }
  188. else
  189. {
  190. _sortedPlaylist.InsertRange(PlayingItemIndex + 1, newItems);
  191. }
  192. LastChange = DateTime.UtcNow;
  193. }
  194. /// <summary>
  195. /// Gets playlist identifier of the playing item, if any.
  196. /// </summary>
  197. /// <returns>The playlist identifier of the playing item.</returns>
  198. public Guid GetPlayingItemPlaylistId()
  199. {
  200. var playingItem = GetPlayingItem();
  201. return playingItem?.PlaylistItemId ?? Guid.Empty;
  202. }
  203. /// <summary>
  204. /// Gets the playing item identifier, if any.
  205. /// </summary>
  206. /// <returns>The playing item identifier.</returns>
  207. public Guid GetPlayingItemId()
  208. {
  209. var playingItem = GetPlayingItem();
  210. return playingItem?.ItemId ?? Guid.Empty;
  211. }
  212. /// <summary>
  213. /// Sets the playing item using its identifier. If not in the playlist, the playing item is reset.
  214. /// </summary>
  215. /// <param name="itemId">The new playing item identifier.</param>
  216. public void SetPlayingItemById(Guid itemId)
  217. {
  218. var playlist = GetPlaylistInternal();
  219. PlayingItemIndex = playlist.FindIndex(item => item.ItemId.Equals(itemId));
  220. LastChange = DateTime.UtcNow;
  221. }
  222. /// <summary>
  223. /// Sets the playing item using its playlist identifier. If not in the playlist, the playing item is reset.
  224. /// </summary>
  225. /// <param name="playlistItemId">The new playing item identifier.</param>
  226. /// <returns><c>true</c> if playing item has been set; <c>false</c> if item is not in the playlist.</returns>
  227. public bool SetPlayingItemByPlaylistId(Guid playlistItemId)
  228. {
  229. var playlist = GetPlaylistInternal();
  230. PlayingItemIndex = playlist.FindIndex(item => item.PlaylistItemId.Equals(playlistItemId));
  231. LastChange = DateTime.UtcNow;
  232. return PlayingItemIndex != NoPlayingItemIndex;
  233. }
  234. /// <summary>
  235. /// Sets the playing item using its position. If not in range, the playing item is reset.
  236. /// </summary>
  237. /// <param name="playlistIndex">The new playing item index.</param>
  238. public void SetPlayingItemByIndex(int playlistIndex)
  239. {
  240. var playlist = GetPlaylistInternal();
  241. if (playlistIndex < 0 || playlistIndex > playlist.Count)
  242. {
  243. PlayingItemIndex = NoPlayingItemIndex;
  244. }
  245. else
  246. {
  247. PlayingItemIndex = playlistIndex;
  248. }
  249. LastChange = DateTime.UtcNow;
  250. }
  251. /// <summary>
  252. /// Removes items from the playlist. If not removed, the playing item is preserved.
  253. /// </summary>
  254. /// <param name="playlistItemIds">The items to remove.</param>
  255. /// <returns><c>true</c> if playing item got removed; <c>false</c> otherwise.</returns>
  256. public bool RemoveFromPlaylist(IReadOnlyList<Guid> playlistItemIds)
  257. {
  258. var playingItem = GetPlayingItem();
  259. _sortedPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId));
  260. _shuffledPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId));
  261. LastChange = DateTime.UtcNow;
  262. if (playingItem is not null)
  263. {
  264. if (playlistItemIds.Contains(playingItem.PlaylistItemId))
  265. {
  266. // Playing item has been removed, picking previous item.
  267. PlayingItemIndex--;
  268. if (PlayingItemIndex < 0)
  269. {
  270. // Was first element, picking next if available.
  271. // Default to no playing item otherwise.
  272. PlayingItemIndex = _sortedPlaylist.Count > 0 ? 0 : NoPlayingItemIndex;
  273. }
  274. return true;
  275. }
  276. // Restoring playing item.
  277. SetPlayingItemByPlaylistId(playingItem.PlaylistItemId);
  278. return false;
  279. }
  280. return false;
  281. }
  282. /// <summary>
  283. /// Moves an item in the playlist to another position.
  284. /// </summary>
  285. /// <param name="playlistItemId">The item to move.</param>
  286. /// <param name="newIndex">The new position.</param>
  287. /// <returns><c>true</c> if the item has been moved; <c>false</c> otherwise.</returns>
  288. public bool MovePlaylistItem(Guid playlistItemId, int newIndex)
  289. {
  290. var playlist = GetPlaylistInternal();
  291. var playingItem = GetPlayingItem();
  292. var oldIndex = playlist.FindIndex(item => item.PlaylistItemId.Equals(playlistItemId));
  293. if (oldIndex < 0)
  294. {
  295. return false;
  296. }
  297. var queueItem = playlist[oldIndex];
  298. playlist.RemoveAt(oldIndex);
  299. newIndex = Math.Clamp(newIndex, 0, playlist.Count);
  300. playlist.Insert(newIndex, queueItem);
  301. LastChange = DateTime.UtcNow;
  302. PlayingItemIndex = playlist.IndexOf(playingItem);
  303. return true;
  304. }
  305. /// <summary>
  306. /// Resets the playlist to its initial state.
  307. /// </summary>
  308. public void Reset()
  309. {
  310. _sortedPlaylist.Clear();
  311. _shuffledPlaylist.Clear();
  312. PlayingItemIndex = NoPlayingItemIndex;
  313. ShuffleMode = GroupShuffleMode.Sorted;
  314. RepeatMode = GroupRepeatMode.RepeatNone;
  315. LastChange = DateTime.UtcNow;
  316. }
  317. /// <summary>
  318. /// Sets the repeat mode.
  319. /// </summary>
  320. /// <param name="mode">The new mode.</param>
  321. public void SetRepeatMode(GroupRepeatMode mode)
  322. {
  323. RepeatMode = mode;
  324. LastChange = DateTime.UtcNow;
  325. }
  326. /// <summary>
  327. /// Sets the shuffle mode.
  328. /// </summary>
  329. /// <param name="mode">The new mode.</param>
  330. public void SetShuffleMode(GroupShuffleMode mode)
  331. {
  332. if (mode.Equals(GroupShuffleMode.Shuffle))
  333. {
  334. ShufflePlaylist();
  335. }
  336. else
  337. {
  338. RestoreSortedPlaylist();
  339. }
  340. }
  341. /// <summary>
  342. /// Toggles the shuffle mode between sorted and shuffled.
  343. /// </summary>
  344. public void ToggleShuffleMode()
  345. {
  346. if (ShuffleMode.Equals(GroupShuffleMode.Sorted))
  347. {
  348. ShufflePlaylist();
  349. }
  350. else
  351. {
  352. RestoreSortedPlaylist();
  353. }
  354. }
  355. /// <summary>
  356. /// Gets the next item in the playlist considering repeat mode and shuffle mode.
  357. /// </summary>
  358. /// <returns>The next item in the playlist.</returns>
  359. public SyncPlayQueueItem GetNextItemPlaylistId()
  360. {
  361. int newIndex;
  362. var playlist = GetPlaylistInternal();
  363. switch (RepeatMode)
  364. {
  365. case GroupRepeatMode.RepeatOne:
  366. newIndex = PlayingItemIndex;
  367. break;
  368. case GroupRepeatMode.RepeatAll:
  369. newIndex = PlayingItemIndex + 1;
  370. if (newIndex >= playlist.Count)
  371. {
  372. newIndex = 0;
  373. }
  374. break;
  375. default:
  376. newIndex = PlayingItemIndex + 1;
  377. break;
  378. }
  379. if (newIndex < 0 || newIndex >= playlist.Count)
  380. {
  381. return null;
  382. }
  383. return playlist[newIndex];
  384. }
  385. /// <summary>
  386. /// Sets the next item in the queue as playing item.
  387. /// </summary>
  388. /// <returns><c>true</c> if the playing item changed; <c>false</c> otherwise.</returns>
  389. public bool Next()
  390. {
  391. if (RepeatMode.Equals(GroupRepeatMode.RepeatOne))
  392. {
  393. LastChange = DateTime.UtcNow;
  394. return true;
  395. }
  396. PlayingItemIndex++;
  397. if (PlayingItemIndex >= _sortedPlaylist.Count)
  398. {
  399. if (RepeatMode.Equals(GroupRepeatMode.RepeatAll))
  400. {
  401. PlayingItemIndex = 0;
  402. }
  403. else
  404. {
  405. PlayingItemIndex = _sortedPlaylist.Count - 1;
  406. return false;
  407. }
  408. }
  409. LastChange = DateTime.UtcNow;
  410. return true;
  411. }
  412. /// <summary>
  413. /// Sets the previous item in the queue as playing item.
  414. /// </summary>
  415. /// <returns><c>true</c> if the playing item changed; <c>false</c> otherwise.</returns>
  416. public bool Previous()
  417. {
  418. if (RepeatMode.Equals(GroupRepeatMode.RepeatOne))
  419. {
  420. LastChange = DateTime.UtcNow;
  421. return true;
  422. }
  423. PlayingItemIndex--;
  424. if (PlayingItemIndex < 0)
  425. {
  426. if (RepeatMode.Equals(GroupRepeatMode.RepeatAll))
  427. {
  428. PlayingItemIndex = _sortedPlaylist.Count - 1;
  429. }
  430. else
  431. {
  432. PlayingItemIndex = 0;
  433. return false;
  434. }
  435. }
  436. LastChange = DateTime.UtcNow;
  437. return true;
  438. }
  439. /// <summary>
  440. /// Creates a list from the array of items. Each item is given an unique playlist identifier.
  441. /// </summary>
  442. /// <returns>The list of queue items.</returns>
  443. private List<SyncPlayQueueItem> CreateQueueItemsFromArray(IReadOnlyList<Guid> items)
  444. {
  445. var list = new List<SyncPlayQueueItem>();
  446. foreach (var item in items)
  447. {
  448. var queueItem = new SyncPlayQueueItem(item);
  449. list.Add(queueItem);
  450. }
  451. return list;
  452. }
  453. /// <summary>
  454. /// Gets the current playlist considering the shuffle mode.
  455. /// </summary>
  456. /// <returns>The playlist.</returns>
  457. private List<SyncPlayQueueItem> GetPlaylistInternal()
  458. {
  459. if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
  460. {
  461. return _shuffledPlaylist;
  462. }
  463. return _sortedPlaylist;
  464. }
  465. /// <summary>
  466. /// Gets the current playing item, depending on the shuffle mode.
  467. /// </summary>
  468. /// <returns>The playing item.</returns>
  469. private SyncPlayQueueItem GetPlayingItem()
  470. {
  471. if (PlayingItemIndex == NoPlayingItemIndex)
  472. {
  473. return null;
  474. }
  475. if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
  476. {
  477. return _shuffledPlaylist[PlayingItemIndex];
  478. }
  479. return _sortedPlaylist[PlayingItemIndex];
  480. }
  481. }
  482. }