Group.cs 23 KB

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