GroupController.cs 22 KB

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