PlayQueueManager.cs 19 KB

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