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
  158. .Values
  159. .Select(session => session.Session)
  160. .ToArray();
  161. case SyncPlayBroadcastType.AllExceptCurrentSession:
  162. return Participants.Values.Select(
  163. session => session.Session).Where(
  164. session => !session.Id.Equals(from.Id)).ToArray();
  165. case SyncPlayBroadcastType.AllReady:
  166. return Participants.Values.Where(
  167. session => !session.IsBuffering).Select(
  168. session => session.Session).ToArray();
  169. default:
  170. return Array.Empty<SessionInfo>();
  171. }
  172. }
  173. private bool HasAccessToItem(User user, BaseItem item)
  174. {
  175. var collections = _libraryManager.GetCollectionFolders(item)
  176. .Select(folder => folder.Id.ToString("N", CultureInfo.InvariantCulture));
  177. return collections.Intersect(user.GetPreference(PreferenceKind.EnabledFolders)).Any();
  178. }
  179. private bool HasAccessToQueue(User user, Guid[] queue)
  180. {
  181. if (queue == null || queue.Length == 0)
  182. {
  183. return true;
  184. }
  185. var items = queue.ToList()
  186. .Select(item => _libraryManager.GetItemById(item));
  187. // Find the highest rating value, which becomes the required minimum for the user.
  188. var MinParentalRatingAccessRequired = items
  189. .Select(item => item.InheritedParentalRatingValue)
  190. .Min();
  191. // Check ParentalRating access, user must have the minimum required access level.
  192. var hasParentalRatingAccess = !user.MaxParentalAgeRating.HasValue
  193. || MinParentalRatingAccessRequired <= user.MaxParentalAgeRating;
  194. // Check that user has access to all required folders.
  195. if (!user.HasPermission(PermissionKind.EnableAllFolders) && hasParentalRatingAccess)
  196. {
  197. // Get list of items that are not accessible.
  198. var blockedItems = items.Where(item => !HasAccessToItem(user, item));
  199. // We need the user to be able to access all items.
  200. return !blockedItems.Any();
  201. }
  202. return hasParentalRatingAccess;
  203. }
  204. private bool AllUsersHaveAccessToQueue(Guid[] queue)
  205. {
  206. if (queue == null || queue.Length == 0)
  207. {
  208. return true;
  209. }
  210. // Get list of users.
  211. var users = Participants.Values
  212. .Select(participant => _userManager.GetUserById(participant.Session.UserId));
  213. // Find problematic users.
  214. var usersWithNoAccess = users.Where(user => !HasAccessToQueue(user, queue));
  215. // All users must be able to access the queue.
  216. return !usersWithNoAccess.Any();
  217. }
  218. /// <inheritdoc />
  219. public bool IsGroupEmpty() => Participants.Count == 0;
  220. /// <inheritdoc />
  221. public void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
  222. {
  223. GroupName = request.GroupName;
  224. AddSession(session);
  225. _syncPlayManager.AddSessionToGroup(session, this);
  226. var sessionIsPlayingAnItem = session.FullNowPlayingItem != null;
  227. RestartCurrentItem();
  228. if (sessionIsPlayingAnItem)
  229. {
  230. var playlist = session.NowPlayingQueue.Select(item => item.Id).ToArray();
  231. PlayQueue.SetPlaylist(playlist);
  232. PlayQueue.SetPlayingItemById(session.FullNowPlayingItem.Id);
  233. RunTimeTicks = session.FullNowPlayingItem.RunTimeTicks ?? 0;
  234. PositionTicks = session.PlayState.PositionTicks ?? 0;
  235. // Mantain playstate.
  236. var waitingState = new WaitingGroupState(_logger);
  237. waitingState.ResumePlaying = !session.PlayState.IsPaused;
  238. SetState(waitingState);
  239. }
  240. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
  241. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  242. State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
  243. _logger.LogInformation("InitGroup: {0} created group {1}.", session.Id.ToString(), GroupId.ToString());
  244. }
  245. /// <inheritdoc />
  246. public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
  247. {
  248. AddSession(session);
  249. _syncPlayManager.AddSessionToGroup(session, this);
  250. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
  251. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  252. var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
  253. SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
  254. State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
  255. _logger.LogInformation("SessionJoin: {0} joined group {1}.", session.Id.ToString(), GroupId.ToString());
  256. }
  257. /// <inheritdoc />
  258. public void SessionRestore(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
  259. {
  260. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
  261. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  262. var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
  263. SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
  264. State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
  265. _logger.LogInformation("SessionRestore: {0} re-joined group {1}.", session.Id.ToString(), GroupId.ToString());
  266. }
  267. /// <inheritdoc />
  268. public void SessionLeave(SessionInfo session, CancellationToken cancellationToken)
  269. {
  270. State.SessionLeaving(this, State.GetGroupState(), session, cancellationToken);
  271. RemoveSession(session);
  272. _syncPlayManager.RemoveSessionFromGroup(session, this);
  273. var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupLeft, GroupId.ToString());
  274. SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
  275. var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserLeft, session.UserName);
  276. SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
  277. _logger.LogInformation("SessionLeave: {0} left group {1}.", session.Id.ToString(), GroupId.ToString());
  278. }
  279. /// <inheritdoc />
  280. public void HandleRequest(SessionInfo session, IPlaybackGroupRequest request, CancellationToken cancellationToken)
  281. {
  282. // The server's job is to maintain a consistent state for clients to reference
  283. // and notify clients of state changes. The actual syncing of media playback
  284. // happens client side. Clients are aware of the server's time and use it to sync.
  285. _logger.LogInformation("HandleRequest: {0} requested {1}, group {2} in {3} state.",
  286. session.Id.ToString(), request.GetRequestType(), GroupId.ToString(), State.GetGroupState());
  287. request.Apply(this, State, session, cancellationToken);
  288. }
  289. /// <inheritdoc />
  290. public GroupInfoDto GetInfo()
  291. {
  292. return new GroupInfoDto()
  293. {
  294. GroupId = GroupId.ToString(),
  295. GroupName = GroupName,
  296. State = State.GetGroupState(),
  297. Participants = Participants.Values.Select(session => session.Session.UserName).Distinct().ToList(),
  298. LastUpdatedAt = DateToUTCString(DateTime.UtcNow)
  299. };
  300. }
  301. /// <inheritdoc />
  302. public bool HasAccessToPlayQueue(User user)
  303. {
  304. var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToArray();
  305. return HasAccessToQueue(user, items);
  306. }
  307. /// <inheritdoc />
  308. public void SetIgnoreGroupWait(SessionInfo session, bool ignoreGroupWait)
  309. {
  310. if (!ContainsSession(session.Id))
  311. {
  312. return;
  313. }
  314. Participants[session.Id].IgnoreGroupWait = ignoreGroupWait;
  315. }
  316. /// <inheritdoc />
  317. public void SetState(ISyncPlayState state)
  318. {
  319. _logger.LogInformation("SetState: {0} switching from {1} to {2}.", GroupId.ToString(), State.GetGroupState(), state.GetGroupState());
  320. this.State = state;
  321. }
  322. /// <inheritdoc />
  323. public Task SendGroupUpdate<T>(SessionInfo from, SyncPlayBroadcastType type, GroupUpdate<T> message, CancellationToken cancellationToken)
  324. {
  325. IEnumerable<Task> GetTasks()
  326. {
  327. foreach (var session in FilterSessions(from, type))
  328. {
  329. yield return _sessionManager.SendSyncPlayGroupUpdate(session, message, cancellationToken);
  330. }
  331. }
  332. return Task.WhenAll(GetTasks());
  333. }
  334. /// <inheritdoc />
  335. public Task SendCommand(SessionInfo from, SyncPlayBroadcastType type, SendCommand message, CancellationToken cancellationToken)
  336. {
  337. IEnumerable<Task> GetTasks()
  338. {
  339. foreach (var session in FilterSessions(from, type))
  340. {
  341. yield return _sessionManager.SendSyncPlayCommand(session, message, cancellationToken);
  342. }
  343. }
  344. return Task.WhenAll(GetTasks());
  345. }
  346. /// <inheritdoc />
  347. public SendCommand NewSyncPlayCommand(SendCommandType type)
  348. {
  349. return new SendCommand()
  350. {
  351. GroupId = GroupId.ToString(),
  352. PlaylistItemId = PlayQueue.GetPlayingItemPlaylistId(),
  353. PositionTicks = PositionTicks,
  354. Command = type,
  355. When = DateToUTCString(LastActivity),
  356. EmittedAt = DateToUTCString(DateTime.UtcNow)
  357. };
  358. }
  359. /// <inheritdoc />
  360. public GroupUpdate<T> NewSyncPlayGroupUpdate<T>(GroupUpdateType type, T data)
  361. {
  362. return new GroupUpdate<T>()
  363. {
  364. GroupId = GroupId.ToString(),
  365. Type = type,
  366. Data = data
  367. };
  368. }
  369. /// <inheritdoc />
  370. public string DateToUTCString(DateTime dateTime)
  371. {
  372. return dateTime.ToUniversalTime().ToString("o");
  373. }
  374. /// <inheritdoc />
  375. public long SanitizePositionTicks(long? positionTicks)
  376. {
  377. var ticks = positionTicks ?? 0;
  378. ticks = ticks >= 0 ? ticks : 0;
  379. ticks = ticks > RunTimeTicks ? RunTimeTicks : ticks;
  380. return ticks;
  381. }
  382. /// <inheritdoc />
  383. public void UpdatePing(SessionInfo session, long ping)
  384. {
  385. if (Participants.TryGetValue(session.Id, out GroupMember value))
  386. {
  387. value.Ping = ping;
  388. }
  389. }
  390. /// <inheritdoc />
  391. public long GetHighestPing()
  392. {
  393. long max = long.MinValue;
  394. foreach (var session in Participants.Values)
  395. {
  396. max = Math.Max(max, session.Ping);
  397. }
  398. return max;
  399. }
  400. /// <inheritdoc />
  401. public void SetBuffering(SessionInfo session, bool isBuffering)
  402. {
  403. if (Participants.TryGetValue(session.Id, out GroupMember value))
  404. {
  405. value.IsBuffering = isBuffering;
  406. }
  407. }
  408. /// <inheritdoc />
  409. public void SetAllBuffering(bool isBuffering)
  410. {
  411. foreach (var session in Participants.Values)
  412. {
  413. session.IsBuffering = isBuffering;
  414. }
  415. }
  416. /// <inheritdoc />
  417. public bool IsBuffering()
  418. {
  419. foreach (var session in Participants.Values)
  420. {
  421. if (session.IsBuffering && !session.IgnoreGroupWait)
  422. {
  423. return true;
  424. }
  425. }
  426. return false;
  427. }
  428. /// <inheritdoc />
  429. public bool SetPlayQueue(Guid[] playQueue, int playingItemPosition, long startPositionTicks)
  430. {
  431. // Ignore on empty queue or invalid item position.
  432. if (playQueue.Length < 1 || playingItemPosition >= playQueue.Length || playingItemPosition < 0)
  433. {
  434. return false;
  435. }
  436. // Check if participants can access the new playing queue.
  437. if (!AllUsersHaveAccessToQueue(playQueue))
  438. {
  439. return false;
  440. }
  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. }