GroupController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Data.Entities;
  8. using Jellyfin.Data.Enums;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Session;
  12. using MediaBrowser.Controller.SyncPlay;
  13. using MediaBrowser.Model.SyncPlay;
  14. using Microsoft.Extensions.Logging;
  15. namespace Emby.Server.Implementations.SyncPlay
  16. {
  17. /// <summary>
  18. /// Class SyncPlayGroupController.
  19. /// </summary>
  20. /// <remarks>
  21. /// Class is not thread-safe, external locking is required when accessing methods.
  22. /// </remarks>
  23. public class SyncPlayGroupController : ISyncPlayGroupController, ISyncPlayStateContext
  24. {
  25. /// <summary>
  26. /// Gets the default ping value used for sessions.
  27. /// </summary>
  28. public long DefaultPing { get; } = 500;
  29. /// <summary>
  30. /// The logger.
  31. /// </summary>
  32. private readonly ILogger _logger;
  33. /// <summary>
  34. /// The user manager.
  35. /// </summary>
  36. private readonly IUserManager _userManager;
  37. /// <summary>
  38. /// The session manager.
  39. /// </summary>
  40. private readonly ISessionManager _sessionManager;
  41. /// <summary>
  42. /// The library manager.
  43. /// </summary>
  44. private readonly ILibraryManager _libraryManager;
  45. /// <summary>
  46. /// The SyncPlay manager.
  47. /// </summary>
  48. private readonly ISyncPlayManager _syncPlayManager;
  49. /// <summary>
  50. /// Internal group state.
  51. /// </summary>
  52. /// <value>The group's state.</value>
  53. private ISyncPlayState State;
  54. /// <summary>
  55. /// Gets the group identifier.
  56. /// </summary>
  57. /// <value>The group identifier.</value>
  58. public Guid GroupId { get; } = Guid.NewGuid();
  59. /// <summary>
  60. /// Gets the group name.
  61. /// </summary>
  62. /// <value>The group name.</value>
  63. public string GroupName { get; private set; }
  64. /// <summary>
  65. /// Gets the group identifier.
  66. /// </summary>
  67. /// <value>The group identifier.</value>
  68. public PlayQueueManager PlayQueue { get; } = new PlayQueueManager();
  69. /// <summary>
  70. /// Gets or sets the runtime ticks of current playing item.
  71. /// </summary>
  72. /// <value>The runtime ticks of current playing item.</value>
  73. public long RunTimeTicks { get; private set; }
  74. /// <summary>
  75. /// Gets or sets the position ticks.
  76. /// </summary>
  77. /// <value>The position ticks.</value>
  78. public long PositionTicks { get; set; }
  79. /// <summary>
  80. /// Gets or sets the last activity.
  81. /// </summary>
  82. /// <value>The last activity.</value>
  83. public DateTime LastActivity { get; set; }
  84. /// <summary>
  85. /// Gets the participants.
  86. /// </summary>
  87. /// <value>The participants, or members of the group.</value>
  88. public Dictionary<string, GroupMember> Participants { get; } =
  89. new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
  90. /// <summary>
  91. /// Initializes a new instance of the <see cref="SyncPlayGroupController" /> class.
  92. /// </summary>
  93. /// <param name="logger">The logger.</param>
  94. /// <param name="userManager">The user manager.</param>
  95. /// <param name="sessionManager">The session manager.</param>
  96. /// <param name="libraryManager">The library manager.</param>
  97. /// <param name="syncPlayManager">The SyncPlay manager.</param>
  98. public SyncPlayGroupController(
  99. ILogger logger,
  100. IUserManager userManager,
  101. ISessionManager sessionManager,
  102. ILibraryManager libraryManager,
  103. ISyncPlayManager syncPlayManager)
  104. {
  105. _logger = logger;
  106. _userManager = userManager;
  107. _sessionManager = sessionManager;
  108. _libraryManager = libraryManager;
  109. _syncPlayManager = syncPlayManager;
  110. State = new IdleGroupState(_logger);
  111. }
  112. /// <summary>
  113. /// Checks if a session is in this group.
  114. /// </summary>
  115. /// <param name="sessionId">The session identifier to check.</param>
  116. /// <returns><c>true</c> if the session is in this group; <c>false</c> otherwise.</returns>
  117. private bool ContainsSession(string sessionId)
  118. {
  119. return Participants.ContainsKey(sessionId);
  120. }
  121. /// <summary>
  122. /// Adds the session to the group.
  123. /// </summary>
  124. /// <param name="session">The session.</param>
  125. private void AddSession(SessionInfo session)
  126. {
  127. Participants.TryAdd(
  128. session.Id,
  129. new GroupMember
  130. {
  131. Session = session,
  132. Ping = DefaultPing,
  133. IsBuffering = false
  134. });
  135. }
  136. /// <summary>
  137. /// Removes the session from the group.
  138. /// </summary>
  139. /// <param name="session">The session.</param>
  140. private void RemoveSession(SessionInfo session)
  141. {
  142. Participants.Remove(session.Id);
  143. }
  144. /// <summary>
  145. /// Filters sessions of this group.
  146. /// </summary>
  147. /// <param name="from">The current session.</param>
  148. /// <param name="type">The filtering type.</param>
  149. /// <returns>The array of sessions matching the filter.</returns>
  150. private SessionInfo[] FilterSessions(SessionInfo from, SyncPlayBroadcastType type)
  151. {
  152. switch (type)
  153. {
  154. case SyncPlayBroadcastType.CurrentSession:
  155. return new SessionInfo[] { from };
  156. case SyncPlayBroadcastType.AllGroup:
  157. return Participants.Values.Select(
  158. session => session.Session).ToArray();
  159. case SyncPlayBroadcastType.AllExceptCurrentSession:
  160. return Participants.Values.Select(
  161. session => session.Session).Where(
  162. session => !session.Id.Equals(from.Id)).ToArray();
  163. case SyncPlayBroadcastType.AllReady:
  164. return Participants.Values.Where(
  165. session => !session.IsBuffering).Select(
  166. session => session.Session).ToArray();
  167. default:
  168. return Array.Empty<SessionInfo>();
  169. }
  170. }
  171. private bool HasAccessToItem(User user, BaseItem item)
  172. {
  173. var collections = _libraryManager.GetCollectionFolders(item)
  174. .Select(folder => folder.Id.ToString("N", CultureInfo.InvariantCulture));
  175. return collections.Intersect(user.GetPreference(PreferenceKind.EnabledFolders)).Any();
  176. }
  177. private bool HasAccessToQueue(User user, Guid[] queue)
  178. {
  179. if (queue == null || queue.Length == 0)
  180. {
  181. return true;
  182. }
  183. var items = queue.ToList()
  184. .Select(item => _libraryManager.GetItemById(item));
  185. // Find the highest rating value, which becomes the required minimum for the user.
  186. var MinParentalRatingAccessRequired = items
  187. .Select(item => item.InheritedParentalRatingValue)
  188. .Min();
  189. // Check ParentalRating access, user must have the minimum required access level.
  190. var hasParentalRatingAccess = !user.MaxParentalAgeRating.HasValue
  191. || MinParentalRatingAccessRequired <= user.MaxParentalAgeRating;
  192. // Check that user has access to all required folders.
  193. if (!user.HasPermission(PermissionKind.EnableAllFolders) && hasParentalRatingAccess)
  194. {
  195. // Get list of items that are not accessible.
  196. var blockedItems = items.Where(item => !HasAccessToItem(user, item));
  197. // We need the user to be able to access all items.
  198. return !blockedItems.Any();
  199. }
  200. return hasParentalRatingAccess;
  201. }
  202. private bool AllUsersHaveAccessToQueue(Guid[] queue)
  203. {
  204. if (queue == null || queue.Length == 0)
  205. {
  206. return true;
  207. }
  208. // Get list of users.
  209. var users = Participants.Values
  210. .Select(participant => _userManager.GetUserById(participant.Session.UserId));
  211. // Find problematic users.
  212. var usersWithNoAccess = users.Where(user => !HasAccessToQueue(user, queue));
  213. // All users must be able to access the queue.
  214. return !usersWithNoAccess.Any();
  215. }
  216. /// <inheritdoc />
  217. public bool IsGroupEmpty() => Participants.Count == 0;
  218. /// <inheritdoc />
  219. public void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
  220. {
  221. GroupName = request.GroupName;
  222. AddSession(session);
  223. _syncPlayManager.AddSessionToGroup(session, this);
  224. var sessionIsPlayingAnItem = session.FullNowPlayingItem != null;
  225. RestartCurrentItem();
  226. if (sessionIsPlayingAnItem)
  227. {
  228. var playlist = session.NowPlayingQueue.Select(item => item.Id).ToArray();
  229. PlayQueue.Reset();
  230. PlayQueue.SetPlaylist(playlist);
  231. PlayQueue.SetPlayingItemById(session.FullNowPlayingItem.Id);
  232. RunTimeTicks = session.FullNowPlayingItem.RunTimeTicks ?? 0;
  233. PositionTicks = session.PlayState.PositionTicks ?? 0;
  234. // Mantain playstate.
  235. var waitingState = new WaitingGroupState(_logger);
  236. waitingState.ResumePlaying = !session.PlayState.IsPaused;
  237. SetState(waitingState);
  238. }
  239. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
  240. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  241. State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
  242. _logger.LogInformation("InitGroup: {0} created group {1}.", session.Id.ToString(), GroupId.ToString());
  243. }
  244. /// <inheritdoc />
  245. public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
  246. {
  247. AddSession(session);
  248. _syncPlayManager.AddSessionToGroup(session, this);
  249. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
  250. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  251. var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
  252. SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
  253. State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
  254. _logger.LogInformation("SessionJoin: {0} joined group {1}.", session.Id.ToString(), GroupId.ToString());
  255. }
  256. /// <inheritdoc />
  257. public void SessionRestore(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
  258. {
  259. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
  260. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  261. var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
  262. SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
  263. State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
  264. _logger.LogInformation("SessionRestore: {0} re-joined group {1}.", session.Id.ToString(), GroupId.ToString());
  265. }
  266. /// <inheritdoc />
  267. public void SessionLeave(SessionInfo session, CancellationToken cancellationToken)
  268. {
  269. State.SessionLeaving(this, State.GetGroupState(), session, cancellationToken);
  270. RemoveSession(session);
  271. _syncPlayManager.RemoveSessionFromGroup(session, this);
  272. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupLeft, GroupId.ToString());
  273. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  274. var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserLeft, session.UserName);
  275. SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
  276. _logger.LogInformation("SessionLeave: {0} left group {1}.", session.Id.ToString(), GroupId.ToString());
  277. }
  278. /// <inheritdoc />
  279. public void HandleRequest(SessionInfo session, IPlaybackGroupRequest request, CancellationToken cancellationToken)
  280. {
  281. // The server's job is to maintain a consistent state for clients to reference
  282. // and notify clients of state changes. The actual syncing of media playback
  283. // happens client side. Clients are aware of the server's time and use it to sync.
  284. _logger.LogInformation("HandleRequest: {0} requested {1}, group {2} in {3} state.",
  285. session.Id.ToString(), request.GetRequestType(), GroupId.ToString(), State.GetGroupState());
  286. request.Apply(this, State, session, cancellationToken);
  287. }
  288. /// <inheritdoc />
  289. public GroupInfoDto GetInfo()
  290. {
  291. return new GroupInfoDto()
  292. {
  293. GroupId = GroupId.ToString(),
  294. GroupName = GroupName,
  295. State = State.GetGroupState(),
  296. Participants = Participants.Values.Select(session => session.Session.UserName).Distinct().ToList(),
  297. LastUpdatedAt = DateToUTCString(DateTime.UtcNow)
  298. };
  299. }
  300. /// <inheritdoc />
  301. public bool HasAccessToPlayQueue(User user)
  302. {
  303. var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToArray();
  304. return HasAccessToQueue(user, items);
  305. }
  306. /// <inheritdoc />
  307. public void SetIgnoreGroupWait(SessionInfo session, bool ignoreGroupWait)
  308. {
  309. if (!ContainsSession(session.Id))
  310. {
  311. return;
  312. }
  313. Participants[session.Id].IgnoreGroupWait = ignoreGroupWait;
  314. }
  315. /// <inheritdoc />
  316. public void SetState(ISyncPlayState state)
  317. {
  318. _logger.LogInformation("SetState: {0} switching from {1} to {2}.", GroupId.ToString(), State.GetGroupState(), state.GetGroupState());
  319. this.State = state;
  320. }
  321. /// <inheritdoc />
  322. public Task SendGroupUpdate<T>(SessionInfo from, SyncPlayBroadcastType type, GroupUpdate<T> message, CancellationToken cancellationToken)
  323. {
  324. IEnumerable<Task> GetTasks()
  325. {
  326. foreach (var session in FilterSessions(from, type))
  327. {
  328. yield return _sessionManager.SendSyncPlayGroupUpdate(session, message, cancellationToken);
  329. }
  330. }
  331. return Task.WhenAll(GetTasks());
  332. }
  333. /// <inheritdoc />
  334. public Task SendCommand(SessionInfo from, SyncPlayBroadcastType type, SendCommand message, CancellationToken cancellationToken)
  335. {
  336. IEnumerable<Task> GetTasks()
  337. {
  338. foreach (var session in FilterSessions(from, type))
  339. {
  340. yield return _sessionManager.SendSyncPlayCommand(session, message, cancellationToken);
  341. }
  342. }
  343. return Task.WhenAll(GetTasks());
  344. }
  345. /// <inheritdoc />
  346. public SendCommand NewSyncPlayCommand(SendCommandType type)
  347. {
  348. return new SendCommand()
  349. {
  350. GroupId = GroupId.ToString(),
  351. PlaylistItemId = PlayQueue.GetPlayingItemPlaylistId(),
  352. PositionTicks = PositionTicks,
  353. Command = type,
  354. When = DateToUTCString(LastActivity),
  355. EmittedAt = DateToUTCString(DateTime.UtcNow)
  356. };
  357. }
  358. /// <inheritdoc />
  359. public GroupUpdate<T> NewSyncPlayGroupUpdate<T>(GroupUpdateType type, T data)
  360. {
  361. return new GroupUpdate<T>()
  362. {
  363. GroupId = GroupId.ToString(),
  364. Type = type,
  365. Data = data
  366. };
  367. }
  368. /// <inheritdoc />
  369. public string DateToUTCString(DateTime dateTime)
  370. {
  371. return dateTime.ToUniversalTime().ToString("o");
  372. }
  373. /// <inheritdoc />
  374. public long SanitizePositionTicks(long? positionTicks)
  375. {
  376. var ticks = positionTicks ?? 0;
  377. ticks = ticks >= 0 ? ticks : 0;
  378. ticks = ticks > RunTimeTicks ? RunTimeTicks : ticks;
  379. return ticks;
  380. }
  381. /// <inheritdoc />
  382. public void UpdatePing(SessionInfo session, long ping)
  383. {
  384. if (Participants.TryGetValue(session.Id, out GroupMember value))
  385. {
  386. value.Ping = ping;
  387. }
  388. }
  389. /// <inheritdoc />
  390. public long GetHighestPing()
  391. {
  392. long max = long.MinValue;
  393. foreach (var session in Participants.Values)
  394. {
  395. max = Math.Max(max, session.Ping);
  396. }
  397. return max;
  398. }
  399. /// <inheritdoc />
  400. public void SetBuffering(SessionInfo session, bool isBuffering)
  401. {
  402. if (Participants.TryGetValue(session.Id, out GroupMember value))
  403. {
  404. value.IsBuffering = isBuffering;
  405. }
  406. }
  407. /// <inheritdoc />
  408. public void SetAllBuffering(bool isBuffering)
  409. {
  410. foreach (var session in Participants.Values)
  411. {
  412. session.IsBuffering = isBuffering;
  413. }
  414. }
  415. /// <inheritdoc />
  416. public bool IsBuffering()
  417. {
  418. foreach (var session in Participants.Values)
  419. {
  420. if (session.IsBuffering && !session.IgnoreGroupWait)
  421. {
  422. return true;
  423. }
  424. }
  425. return false;
  426. }
  427. /// <inheritdoc />
  428. public bool SetPlayQueue(Guid[] playQueue, int playingItemPosition, long startPositionTicks)
  429. {
  430. // Ignore on empty queue or invalid item position.
  431. if (playQueue.Length < 1 || playingItemPosition >= playQueue.Length || playingItemPosition < 0)
  432. {
  433. return false;
  434. }
  435. // Check if participants can access the new playing queue.
  436. if (!AllUsersHaveAccessToQueue(playQueue))
  437. {
  438. return false;
  439. }
  440. PlayQueue.Reset();
  441. PlayQueue.SetPlaylist(playQueue);
  442. PlayQueue.SetPlayingItemByIndex(playingItemPosition);
  443. var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
  444. RunTimeTicks = item.RunTimeTicks ?? 0;
  445. PositionTicks = startPositionTicks;
  446. LastActivity = DateTime.UtcNow;
  447. return true;
  448. }
  449. /// <inheritdoc />
  450. public bool SetPlayingItem(string playlistItemId)
  451. {
  452. var itemFound = PlayQueue.SetPlayingItemByPlaylistId(playlistItemId);
  453. if (itemFound)
  454. {
  455. var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
  456. RunTimeTicks = item.RunTimeTicks ?? 0;
  457. }
  458. else
  459. {
  460. RunTimeTicks = 0;
  461. }
  462. RestartCurrentItem();
  463. return itemFound;
  464. }
  465. /// <inheritdoc />
  466. public bool RemoveFromPlayQueue(string[] playlistItemIds)
  467. {
  468. var playingItemRemoved = PlayQueue.RemoveFromPlaylist(playlistItemIds);
  469. if (playingItemRemoved)
  470. {
  471. var itemId = PlayQueue.GetPlayingItemId();
  472. if (!itemId.Equals(Guid.Empty))
  473. {
  474. var item = _libraryManager.GetItemById(itemId);
  475. RunTimeTicks = item.RunTimeTicks ?? 0;
  476. }
  477. else
  478. {
  479. RunTimeTicks = 0;
  480. }
  481. RestartCurrentItem();
  482. }
  483. return playingItemRemoved;
  484. }
  485. /// <inheritdoc />
  486. public bool MoveItemInPlayQueue(string playlistItemId, int newIndex)
  487. {
  488. return PlayQueue.MovePlaylistItem(playlistItemId, newIndex);
  489. }
  490. /// <inheritdoc />
  491. public bool AddToPlayQueue(Guid[] newItems, string mode)
  492. {
  493. // Ignore on empty list.
  494. if (newItems.Length < 1)
  495. {
  496. return false;
  497. }
  498. // Check if participants can access the new playing queue.
  499. if (!AllUsersHaveAccessToQueue(newItems))
  500. {
  501. return false;
  502. }
  503. if (mode.Equals("next"))
  504. {
  505. PlayQueue.QueueNext(newItems);
  506. }
  507. else
  508. {
  509. PlayQueue.Queue(newItems);
  510. }
  511. return true;
  512. }
  513. /// <inheritdoc />
  514. public void RestartCurrentItem()
  515. {
  516. PositionTicks = 0;
  517. LastActivity = DateTime.UtcNow;
  518. }
  519. /// <inheritdoc />
  520. public bool NextItemInQueue()
  521. {
  522. var update = PlayQueue.Next();
  523. if (update)
  524. {
  525. var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
  526. RunTimeTicks = item.RunTimeTicks ?? 0;
  527. RestartCurrentItem();
  528. return true;
  529. }
  530. else
  531. {
  532. return false;
  533. }
  534. }
  535. /// <inheritdoc />
  536. public bool PreviousItemInQueue()
  537. {
  538. var update = PlayQueue.Previous();
  539. if (update)
  540. {
  541. var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
  542. RunTimeTicks = item.RunTimeTicks ?? 0;
  543. RestartCurrentItem();
  544. return true;
  545. }
  546. else
  547. {
  548. return false;
  549. }
  550. }
  551. /// <inheritdoc />
  552. public void SetRepeatMode(string mode) {
  553. PlayQueue.SetRepeatMode(mode);
  554. }
  555. /// <inheritdoc />
  556. public void SetShuffleMode(string mode) {
  557. PlayQueue.SetShuffleMode(mode);
  558. }
  559. /// <inheritdoc />
  560. public PlayQueueUpdate GetPlayQueueUpdate(PlayQueueUpdateReason reason)
  561. {
  562. var startPositionTicks = PositionTicks;
  563. if (State.GetGroupState().Equals(GroupState.Playing))
  564. {
  565. var currentTime = DateTime.UtcNow;
  566. var elapsedTime = currentTime - LastActivity;
  567. // Event may happen during the delay added to account for latency.
  568. startPositionTicks += elapsedTime.Ticks > 0 ? elapsedTime.Ticks : 0;
  569. }
  570. return new PlayQueueUpdate()
  571. {
  572. Reason = reason,
  573. LastUpdate = DateToUTCString(PlayQueue.LastChange),
  574. Playlist = PlayQueue.GetPlaylist(),
  575. PlayingItemIndex = PlayQueue.PlayingItemIndex,
  576. StartPositionTicks = startPositionTicks,
  577. ShuffleMode = PlayQueue.ShuffleMode,
  578. RepeatMode = PlayQueue.RepeatMode
  579. };
  580. }
  581. }
  582. }