123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using Jellyfin.Data.Entities;
- using Jellyfin.Data.Enums;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Library;
- using MediaBrowser.Controller.Session;
- using MediaBrowser.Controller.SyncPlay;
- using MediaBrowser.Model.SyncPlay;
- using Microsoft.Extensions.Logging;
- namespace Emby.Server.Implementations.SyncPlay
- {
- /// <summary>
- /// Class SyncPlayGroupController.
- /// </summary>
- /// <remarks>
- /// Class is not thread-safe, external locking is required when accessing methods.
- /// </remarks>
- public class SyncPlayGroupController : ISyncPlayGroupController, ISyncPlayStateContext
- {
- /// <summary>
- /// Gets the default ping value used for sessions.
- /// </summary>
- public long DefaultPing { get; } = 500;
- /// <summary>
- /// The logger.
- /// </summary>
- private readonly ILogger _logger;
- /// <summary>
- /// The user manager.
- /// </summary>
- private readonly IUserManager _userManager;
- /// <summary>
- /// The session manager.
- /// </summary>
- private readonly ISessionManager _sessionManager;
- /// <summary>
- /// The library manager.
- /// </summary>
- private readonly ILibraryManager _libraryManager;
- /// <summary>
- /// The SyncPlay manager.
- /// </summary>
- private readonly ISyncPlayManager _syncPlayManager;
- /// <summary>
- /// Internal group state.
- /// </summary>
- /// <value>The group's state.</value>
- private ISyncPlayState State;
- /// <summary>
- /// Gets the group identifier.
- /// </summary>
- /// <value>The group identifier.</value>
- public Guid GroupId { get; } = Guid.NewGuid();
- /// <summary>
- /// Gets the group name.
- /// </summary>
- /// <value>The group name.</value>
- public string GroupName { get; private set; }
- /// <summary>
- /// Gets the group identifier.
- /// </summary>
- /// <value>The group identifier.</value>
- public PlayQueueManager PlayQueue { get; } = new PlayQueueManager();
- /// <summary>
- /// Gets or sets the runtime ticks of current playing item.
- /// </summary>
- /// <value>The runtime ticks of current playing item.</value>
- public long RunTimeTicks { get; private set; }
- /// <summary>
- /// Gets or sets the position ticks.
- /// </summary>
- /// <value>The position ticks.</value>
- public long PositionTicks { get; set; }
- /// <summary>
- /// Gets or sets the last activity.
- /// </summary>
- /// <value>The last activity.</value>
- public DateTime LastActivity { get; set; }
- /// <summary>
- /// Gets the participants.
- /// </summary>
- /// <value>The participants, or members of the group.</value>
- public Dictionary<string, GroupMember> Participants { get; } =
- new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
- /// <summary>
- /// Initializes a new instance of the <see cref="SyncPlayGroupController" /> class.
- /// </summary>
- /// <param name="logger">The logger.</param>
- /// <param name="userManager">The user manager.</param>
- /// <param name="sessionManager">The session manager.</param>
- /// <param name="libraryManager">The library manager.</param>
- /// <param name="syncPlayManager">The SyncPlay manager.</param>
- public SyncPlayGroupController(
- ILogger logger,
- IUserManager userManager,
- ISessionManager sessionManager,
- ILibraryManager libraryManager,
- ISyncPlayManager syncPlayManager)
- {
- _logger = logger;
- _userManager = userManager;
- _sessionManager = sessionManager;
- _libraryManager = libraryManager;
- _syncPlayManager = syncPlayManager;
- State = new IdleGroupState(_logger);
- }
- /// <summary>
- /// Adds the session to the group.
- /// </summary>
- /// <param name="session">The session.</param>
- private void AddSession(SessionInfo session)
- {
- Participants.TryAdd(
- session.Id,
- new GroupMember
- {
- Session = session,
- Ping = DefaultPing,
- IsBuffering = false
- });
- }
- /// <summary>
- /// Removes the session from the group.
- /// </summary>
- /// <param name="session">The session.</param>
- private void RemoveSession(SessionInfo session)
- {
- Participants.Remove(session.Id);
- }
- /// <summary>
- /// Filters sessions of this group.
- /// </summary>
- /// <param name="from">The current session.</param>
- /// <param name="type">The filtering type.</param>
- /// <returns>The array of sessions matching the filter.</returns>
- private SessionInfo[] FilterSessions(SessionInfo from, SyncPlayBroadcastType type)
- {
- switch (type)
- {
- case SyncPlayBroadcastType.CurrentSession:
- return new SessionInfo[] { from };
- case SyncPlayBroadcastType.AllGroup:
- return Participants
- .Values
- .Select(session => session.Session)
- .ToArray();
- case SyncPlayBroadcastType.AllExceptCurrentSession:
- return Participants
- .Values
- .Select(session => session.Session)
- .Where(session => !session.Id.Equals(from.Id))
- .ToArray();
- case SyncPlayBroadcastType.AllReady:
- return Participants
- .Values
- .Where(session => !session.IsBuffering)
- .Select(session => session.Session)
- .ToArray();
- default:
- return Array.Empty<SessionInfo>();
- }
- }
- private bool HasAccessToItem(User user, BaseItem item)
- {
- var collections = _libraryManager.GetCollectionFolders(item)
- .Select(folder => folder.Id.ToString("N", CultureInfo.InvariantCulture));
- return collections.Intersect(user.GetPreference(PreferenceKind.EnabledFolders)).Any();
- }
- private bool HasAccessToQueue(User user, Guid[] queue)
- {
- if (queue == null || queue.Length == 0)
- {
- return true;
- }
- var items = queue.ToList()
- .Select(item => _libraryManager.GetItemById(item));
- // Find the highest rating value, which becomes the required minimum for the user.
- var MinParentalRatingAccessRequired = items
- .Select(item => item.InheritedParentalRatingValue)
- .Min();
- // Check ParentalRating access, user must have the minimum required access level.
- var hasParentalRatingAccess = !user.MaxParentalAgeRating.HasValue
- || MinParentalRatingAccessRequired <= user.MaxParentalAgeRating;
- // Check that user has access to all required folders.
- if (!user.HasPermission(PermissionKind.EnableAllFolders) && hasParentalRatingAccess)
- {
- // Get list of items that are not accessible.
- var blockedItems = items.Where(item => !HasAccessToItem(user, item));
- // We need the user to be able to access all items.
- return !blockedItems.Any();
- }
- return hasParentalRatingAccess;
- }
- private bool AllUsersHaveAccessToQueue(Guid[] queue)
- {
- if (queue == null || queue.Length == 0)
- {
- return true;
- }
- // Get list of users.
- var users = Participants
- .Values
- .Select(participant => _userManager.GetUserById(participant.Session.UserId));
- // Find problematic users.
- var usersWithNoAccess = users.Where(user => !HasAccessToQueue(user, queue));
- // All users must be able to access the queue.
- return !usersWithNoAccess.Any();
- }
- /// <inheritdoc />
- public bool IsGroupEmpty() => Participants.Count == 0;
- /// <inheritdoc />
- public void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
- {
- GroupName = request.GroupName;
- AddSession(session);
- _syncPlayManager.AddSessionToGroup(session, this);
- var sessionIsPlayingAnItem = session.FullNowPlayingItem != null;
- RestartCurrentItem();
- if (sessionIsPlayingAnItem)
- {
- var playlist = session.NowPlayingQueue.Select(item => item.Id).ToArray();
- PlayQueue.Reset();
- PlayQueue.SetPlaylist(playlist);
- PlayQueue.SetPlayingItemById(session.FullNowPlayingItem.Id);
- RunTimeTicks = session.FullNowPlayingItem.RunTimeTicks ?? 0;
- PositionTicks = session.PlayState.PositionTicks ?? 0;
- // Mantain playstate.
- var waitingState = new WaitingGroupState(_logger);
- waitingState.ResumePlaying = !session.PlayState.IsPaused;
- SetState(waitingState);
- }
- var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
- SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
- State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
- _logger.LogInformation("InitGroup: {0} created group {1}.", session.Id.ToString(), GroupId.ToString());
- }
- /// <inheritdoc />
- public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
- {
- AddSession(session);
- _syncPlayManager.AddSessionToGroup(session, this);
- var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
- SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
- var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
- SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
- State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
- _logger.LogInformation("SessionJoin: {0} joined group {1}.", session.Id.ToString(), GroupId.ToString());
- }
- /// <inheritdoc />
- public void SessionRestore(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
- {
- var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
- SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
- var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
- SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
- State.SessionJoined(this, State.GetGroupState(), session, cancellationToken);
- _logger.LogInformation("SessionRestore: {0} re-joined group {1}.", session.Id.ToString(), GroupId.ToString());
- }
- /// <inheritdoc />
- public void SessionLeave(SessionInfo session, CancellationToken cancellationToken)
- {
- State.SessionLeaving(this, State.GetGroupState(), session, cancellationToken);
- RemoveSession(session);
- _syncPlayManager.RemoveSessionFromGroup(session, this);
- var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupLeft, GroupId.ToString());
- SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
- var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserLeft, session.UserName);
- SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
- _logger.LogInformation("SessionLeave: {0} left group {1}.", session.Id.ToString(), GroupId.ToString());
- }
- /// <inheritdoc />
- public void HandleRequest(SessionInfo session, IPlaybackGroupRequest request, CancellationToken cancellationToken)
- {
- // The server's job is to maintain a consistent state for clients to reference
- // and notify clients of state changes. The actual syncing of media playback
- // happens client side. Clients are aware of the server's time and use it to sync.
- _logger.LogInformation("HandleRequest: {0} requested {1}, group {2} in {3} state.",
- session.Id.ToString(), request.GetRequestType(), GroupId.ToString(), State.GetGroupState());
- request.Apply(this, State, session, cancellationToken);
- }
- /// <inheritdoc />
- public GroupInfoDto GetInfo()
- {
- return new GroupInfoDto()
- {
- GroupId = GroupId.ToString(),
- GroupName = GroupName,
- State = State.GetGroupState(),
- Participants = Participants.Values.Select(session => session.Session.UserName).Distinct().ToList(),
- LastUpdatedAt = DateToUTCString(DateTime.UtcNow)
- };
- }
- /// <inheritdoc />
- public bool HasAccessToPlayQueue(User user)
- {
- var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToArray();
- return HasAccessToQueue(user, items);
- }
- /// <inheritdoc />
- public void SetIgnoreGroupWait(SessionInfo session, bool ignoreGroupWait)
- {
- if (!Participants.ContainsKey(session.Id))
- {
- return;
- }
- Participants[session.Id].IgnoreGroupWait = ignoreGroupWait;
- }
- /// <inheritdoc />
- public void SetState(ISyncPlayState state)
- {
- _logger.LogInformation("SetState: {0} switching from {1} to {2}.", GroupId.ToString(), State.GetGroupState(), state.GetGroupState());
- this.State = state;
- }
- /// <inheritdoc />
- public Task SendGroupUpdate<T>(SessionInfo from, SyncPlayBroadcastType type, GroupUpdate<T> message, CancellationToken cancellationToken)
- {
- IEnumerable<Task> GetTasks()
- {
- foreach (var session in FilterSessions(from, type))
- {
- yield return _sessionManager.SendSyncPlayGroupUpdate(session, message, cancellationToken);
- }
- }
- return Task.WhenAll(GetTasks());
- }
- /// <inheritdoc />
- public Task SendCommand(SessionInfo from, SyncPlayBroadcastType type, SendCommand message, CancellationToken cancellationToken)
- {
- IEnumerable<Task> GetTasks()
- {
- foreach (var session in FilterSessions(from, type))
- {
- yield return _sessionManager.SendSyncPlayCommand(session, message, cancellationToken);
- }
- }
- return Task.WhenAll(GetTasks());
- }
- /// <inheritdoc />
- public SendCommand NewSyncPlayCommand(SendCommandType type)
- {
- return new SendCommand()
- {
- GroupId = GroupId.ToString(),
- PlaylistItemId = PlayQueue.GetPlayingItemPlaylistId(),
- PositionTicks = PositionTicks,
- Command = type,
- When = DateToUTCString(LastActivity),
- EmittedAt = DateToUTCString(DateTime.UtcNow)
- };
- }
- /// <inheritdoc />
- public GroupUpdate<T> NewSyncPlayGroupUpdate<T>(GroupUpdateType type, T data)
- {
- return new GroupUpdate<T>()
- {
- GroupId = GroupId.ToString(),
- Type = type,
- Data = data
- };
- }
- /// <inheritdoc />
- public string DateToUTCString(DateTime dateTime)
- {
- return dateTime.ToUniversalTime().ToString("o");
- }
- /// <inheritdoc />
- public long SanitizePositionTicks(long? positionTicks)
- {
- var ticks = positionTicks ?? 0;
- ticks = Math.Max(ticks, 0);
- ticks = Math.Min(ticks, RunTimeTicks);
- return ticks;
- }
- /// <inheritdoc />
- public void UpdatePing(SessionInfo session, long ping)
- {
- if (Participants.TryGetValue(session.Id, out GroupMember value))
- {
- value.Ping = ping;
- }
- }
- /// <inheritdoc />
- public long GetHighestPing()
- {
- long max = long.MinValue;
- foreach (var session in Participants.Values)
- {
- max = Math.Max(max, session.Ping);
- }
- return max;
- }
- /// <inheritdoc />
- public void SetBuffering(SessionInfo session, bool isBuffering)
- {
- if (Participants.TryGetValue(session.Id, out GroupMember value))
- {
- value.IsBuffering = isBuffering;
- }
- }
- /// <inheritdoc />
- public void SetAllBuffering(bool isBuffering)
- {
- foreach (var session in Participants.Values)
- {
- session.IsBuffering = isBuffering;
- }
- }
- /// <inheritdoc />
- public bool IsBuffering()
- {
- foreach (var session in Participants.Values)
- {
- if (session.IsBuffering && !session.IgnoreGroupWait)
- {
- return true;
- }
- }
- return false;
- }
- /// <inheritdoc />
- public bool SetPlayQueue(Guid[] playQueue, int playingItemPosition, long startPositionTicks)
- {
- // Ignore on empty queue or invalid item position.
- if (playQueue.Length < 1 || playingItemPosition >= playQueue.Length || playingItemPosition < 0)
- {
- return false;
- }
- // Check if participants can access the new playing queue.
- if (!AllUsersHaveAccessToQueue(playQueue))
- {
- return false;
- }
- PlayQueue.Reset();
- PlayQueue.SetPlaylist(playQueue);
- PlayQueue.SetPlayingItemByIndex(playingItemPosition);
- var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
- RunTimeTicks = item.RunTimeTicks ?? 0;
- PositionTicks = startPositionTicks;
- LastActivity = DateTime.UtcNow;
- return true;
- }
- /// <inheritdoc />
- public bool SetPlayingItem(string playlistItemId)
- {
- var itemFound = PlayQueue.SetPlayingItemByPlaylistId(playlistItemId);
- if (itemFound)
- {
- var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
- RunTimeTicks = item.RunTimeTicks ?? 0;
- }
- else
- {
- RunTimeTicks = 0;
- }
- RestartCurrentItem();
- return itemFound;
- }
- /// <inheritdoc />
- public bool RemoveFromPlayQueue(string[] playlistItemIds)
- {
- var playingItemRemoved = PlayQueue.RemoveFromPlaylist(playlistItemIds);
- if (playingItemRemoved)
- {
- var itemId = PlayQueue.GetPlayingItemId();
- if (!itemId.Equals(Guid.Empty))
- {
- var item = _libraryManager.GetItemById(itemId);
- RunTimeTicks = item.RunTimeTicks ?? 0;
- }
- else
- {
- RunTimeTicks = 0;
- }
- RestartCurrentItem();
- }
- return playingItemRemoved;
- }
- /// <inheritdoc />
- public bool MoveItemInPlayQueue(string playlistItemId, int newIndex)
- {
- return PlayQueue.MovePlaylistItem(playlistItemId, newIndex);
- }
- /// <inheritdoc />
- public bool AddToPlayQueue(Guid[] newItems, string mode)
- {
- // Ignore on empty list.
- if (newItems.Length < 1)
- {
- return false;
- }
- // Check if participants can access the new playing queue.
- if (!AllUsersHaveAccessToQueue(newItems))
- {
- return false;
- }
- if (mode.Equals("next"))
- {
- PlayQueue.QueueNext(newItems);
- }
- else
- {
- PlayQueue.Queue(newItems);
- }
- return true;
- }
- /// <inheritdoc />
- public void RestartCurrentItem()
- {
- PositionTicks = 0;
- LastActivity = DateTime.UtcNow;
- }
- /// <inheritdoc />
- public bool NextItemInQueue()
- {
- var update = PlayQueue.Next();
- if (update)
- {
- var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
- RunTimeTicks = item.RunTimeTicks ?? 0;
- RestartCurrentItem();
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <inheritdoc />
- public bool PreviousItemInQueue()
- {
- var update = PlayQueue.Previous();
- if (update)
- {
- var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId());
- RunTimeTicks = item.RunTimeTicks ?? 0;
- RestartCurrentItem();
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <inheritdoc />
- public void SetRepeatMode(string mode) {
- switch (mode)
- {
- case "RepeatOne":
- PlayQueue.SetRepeatMode(GroupRepeatMode.RepeatOne);
- break;
- case "RepeatAll":
- PlayQueue.SetRepeatMode(GroupRepeatMode.RepeatAll);
- break;
- default:
- // On unknown values, default to repeat none.
- PlayQueue.SetRepeatMode(GroupRepeatMode.RepeatNone);
- break;
- }
- }
- /// <inheritdoc />
- public void SetShuffleMode(string mode) {
- switch (mode)
- {
- case "Shuffle":
- PlayQueue.SetShuffleMode(GroupShuffleMode.Shuffle);
- break;
- default:
- // On unknown values, default to sorted playlist.
- PlayQueue.SetShuffleMode(GroupShuffleMode.Sorted);
- break;
- }
- }
- /// <inheritdoc />
- public PlayQueueUpdate GetPlayQueueUpdate(PlayQueueUpdateReason reason)
- {
- var startPositionTicks = PositionTicks;
- if (State.GetGroupState().Equals(GroupState.Playing))
- {
- var currentTime = DateTime.UtcNow;
- var elapsedTime = currentTime - LastActivity;
- // Elapsed time is negative if event happens
- // during the delay added to account for latency.
- // In this phase clients haven't started the playback yet.
- // In other words, LastActivity is in the future,
- // when playback unpause is supposed to happen.
- // Adjust ticks only if playback actually started.
- startPositionTicks += Math.Max(elapsedTime.Ticks, 0);
- }
- return new PlayQueueUpdate()
- {
- Reason = reason,
- LastUpdate = DateToUTCString(PlayQueue.LastChange),
- Playlist = PlayQueue.GetPlaylist(),
- PlayingItemIndex = PlayQueue.PlayingItemIndex,
- StartPositionTicks = startPositionTicks,
- ShuffleMode = PlayQueue.ShuffleMode,
- RepeatMode = PlayQueue.RepeatMode
- };
- }
- }
- }
|