PlayQueueManager.cs 19 KB

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