GroupController.cs 23 KB

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