diff --git a/Emby.Server.Implementations/SyncPlay/GroupController.cs b/Emby.Server.Implementations/SyncPlay/GroupController.cs index a0d951b3e6..48596bb421 100644 --- a/Emby.Server.Implementations/SyncPlay/GroupController.cs +++ b/Emby.Server.Implementations/SyncPlay/GroupController.cs @@ -45,11 +45,6 @@ namespace Emby.Server.Implementations.SyncPlay /// private readonly ILibraryManager _libraryManager; - /// - /// The SyncPlay manager. - /// - private readonly ISyncPlayManager _syncPlayManager; - /// /// Internal group state. /// @@ -63,19 +58,16 @@ namespace Emby.Server.Implementations.SyncPlay /// The user manager. /// The session manager. /// The library manager. - /// The SyncPlay manager. public GroupController( ILogger logger, IUserManager userManager, ISessionManager sessionManager, - ILibraryManager libraryManager, - ISyncPlayManager syncPlayManager) + ILibraryManager libraryManager) { _logger = logger; _userManager = userManager; _sessionManager = sessionManager; _libraryManager = libraryManager; - _syncPlayManager = syncPlayManager; _state = new IdleGroupState(_logger); } @@ -168,7 +160,7 @@ namespace Emby.Server.Implementations.SyncPlay /// /// The current session. /// The filtering type. - /// The array of sessions matching the filter. + /// The list of sessions matching the filter. private IEnumerable FilterSessions(SessionInfo from, SyncPlayBroadcastType type) { return type switch @@ -209,7 +201,7 @@ namespace Emby.Server.Implementations.SyncPlay /// The user. /// The queue. /// true if the user can access all the items in the queue, false otherwise. - private bool HasAccessToQueue(User user, IEnumerable queue) + private bool HasAccessToQueue(User user, IReadOnlyList queue) { // Check if queue is empty. if (!queue?.Any() ?? true) @@ -234,7 +226,7 @@ namespace Emby.Server.Implementations.SyncPlay return true; } - private bool AllUsersHaveAccessToQueue(IEnumerable queue) + private bool AllUsersHaveAccessToQueue(IReadOnlyList queue) { // Check if queue is empty. if (!queue?.Any() ?? true) @@ -262,7 +254,6 @@ namespace Emby.Server.Implementations.SyncPlay { GroupName = request.GroupName; AddSession(session); - _syncPlayManager.AddSessionToGroup(session, this); var sessionIsPlayingAnItem = session.FullNowPlayingItem != null; @@ -270,7 +261,7 @@ namespace Emby.Server.Implementations.SyncPlay if (sessionIsPlayingAnItem) { - var playlist = session.NowPlayingQueue.Select(item => item.Id); + var playlist = session.NowPlayingQueue.Select(item => item.Id).ToList(); PlayQueue.Reset(); PlayQueue.SetPlaylist(playlist); PlayQueue.SetPlayingItemById(session.FullNowPlayingItem.Id); @@ -290,14 +281,13 @@ namespace Emby.Server.Implementations.SyncPlay _state.SessionJoined(this, _state.Type, session, cancellationToken); - _logger.LogInformation("InitGroup: {0} created group {1}.", session.Id, GroupId.ToString()); + _logger.LogInformation("InitGroup: {SessionId} created group {GroupId}.", session.Id, GroupId.ToString()); } /// 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); @@ -307,7 +297,7 @@ namespace Emby.Server.Implementations.SyncPlay _state.SessionJoined(this, _state.Type, session, cancellationToken); - _logger.LogInformation("SessionJoin: {0} joined group {1}.", session.Id, GroupId.ToString()); + _logger.LogInformation("SessionJoin: {SessionId} joined group {GroupId}.", session.Id, GroupId.ToString()); } /// @@ -321,7 +311,7 @@ namespace Emby.Server.Implementations.SyncPlay _state.SessionJoined(this, _state.Type, session, cancellationToken); - _logger.LogInformation("SessionRestore: {0} re-joined group {1}.", session.Id, GroupId.ToString()); + _logger.LogInformation("SessionRestore: {SessionId} re-joined group {GroupId}.", session.Id, GroupId.ToString()); } /// @@ -330,7 +320,6 @@ namespace Emby.Server.Implementations.SyncPlay _state.SessionLeaving(this, _state.Type, session, cancellationToken); RemoveSession(session); - _syncPlayManager.RemoveSessionFromGroup(session, this); var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupLeft, GroupId.ToString()); SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken); @@ -338,7 +327,7 @@ namespace Emby.Server.Implementations.SyncPlay var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserLeft, session.UserName); SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken); - _logger.LogInformation("SessionLeave: {0} left group {1}.", session.Id, GroupId.ToString()); + _logger.LogInformation("SessionLeave: {SessionId} left group {GroupId}.", session.Id, GroupId.ToString()); } /// @@ -347,27 +336,21 @@ namespace Emby.Server.Implementations.SyncPlay // 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, request.Type, GroupId.ToString(), _state.Type); + _logger.LogInformation("HandleRequest: {SessionId} requested {RequestType}, group {GroupId} is {StateType}.", session.Id, request.Type, GroupId.ToString(), _state.Type); request.Apply(this, _state, session, cancellationToken); } /// public GroupInfoDto GetInfo() { - return new GroupInfoDto() - { - GroupId = GroupId.ToString(), - GroupName = GroupName, - State = _state.Type, - Participants = Participants.Values.Select(session => session.Session.UserName).Distinct().ToList(), - LastUpdatedAt = DateTime.UtcNow - }; + var participants = Participants.Values.Select(session => session.Session.UserName).Distinct().ToList(); + return new GroupInfoDto(GroupId, GroupName, _state.Type, participants, DateTime.UtcNow); } /// public bool HasAccessToPlayQueue(User user) { - var items = PlayQueue.GetPlaylist().Select(item => item.ItemId); + var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToList(); return HasAccessToQueue(user, items); } @@ -383,7 +366,7 @@ namespace Emby.Server.Implementations.SyncPlay /// public void SetState(IGroupState state) { - _logger.LogInformation("SetState: {0} switching from {1} to {2}.", GroupId.ToString(), _state.Type, state.Type); + _logger.LogInformation("SetState: {GroupId} switching from {FromStateType} to {ToStateType}.", GroupId.ToString(), _state.Type, state.Type); this._state = state; } @@ -418,26 +401,19 @@ namespace Emby.Server.Implementations.SyncPlay /// public SendCommand NewSyncPlayCommand(SendCommandType type) { - return new SendCommand() - { - GroupId = GroupId.ToString(), - PlaylistItemId = PlayQueue.GetPlayingItemPlaylistId(), - PositionTicks = PositionTicks, - Command = type, - When = LastActivity, - EmittedAt = DateTime.UtcNow - }; + return new SendCommand( + GroupId, + PlayQueue.GetPlayingItemPlaylistId(), + LastActivity, + type, + PositionTicks, + DateTime.UtcNow); } /// public GroupUpdate NewSyncPlayGroupUpdate(GroupUpdateType type, T data) { - return new GroupUpdate() - { - GroupId = GroupId.ToString(), - Type = type, - Data = data - }; + return new GroupUpdate(GroupId, type, data); } /// @@ -501,10 +477,10 @@ namespace Emby.Server.Implementations.SyncPlay } /// - public bool SetPlayQueue(IEnumerable playQueue, int playingItemPosition, long startPositionTicks) + public bool SetPlayQueue(IReadOnlyList playQueue, int playingItemPosition, long startPositionTicks) { // Ignore on empty queue or invalid item position. - if (!playQueue.Any() || playingItemPosition >= playQueue.Count() || playingItemPosition < 0) + if (playQueue.Count == 0 || playingItemPosition >= playQueue.Count || playingItemPosition < 0) { return false; } @@ -547,7 +523,7 @@ namespace Emby.Server.Implementations.SyncPlay } /// - public bool RemoveFromPlayQueue(IEnumerable playlistItemIds) + public bool RemoveFromPlayQueue(IReadOnlyList playlistItemIds) { var playingItemRemoved = PlayQueue.RemoveFromPlaylist(playlistItemIds); if (playingItemRemoved) @@ -576,10 +552,10 @@ namespace Emby.Server.Implementations.SyncPlay } /// - public bool AddToPlayQueue(IEnumerable newItems, GroupQueueMode mode) + public bool AddToPlayQueue(IReadOnlyList newItems, GroupQueueMode mode) { // Ignore on empty list. - if (!newItems.Any()) + if (newItems.Count == 0) { return false; } @@ -673,16 +649,14 @@ namespace Emby.Server.Implementations.SyncPlay startPositionTicks += Math.Max(elapsedTime.Ticks, 0); } - return new PlayQueueUpdate() - { - Reason = reason, - LastUpdate = PlayQueue.LastChange, - Playlist = PlayQueue.GetPlaylist(), - PlayingItemIndex = PlayQueue.PlayingItemIndex, - StartPositionTicks = startPositionTicks, - ShuffleMode = PlayQueue.ShuffleMode, - RepeatMode = PlayQueue.RepeatMode - }; + return new PlayQueueUpdate( + reason, + PlayQueue.LastChange, + PlayQueue.GetPlaylist(), + PlayQueue.PlayingItemIndex, + startPositionTicks, + PlayQueue.ShuffleMode, + PlayQueue.RepeatMode); } } } diff --git a/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs b/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs index 178536631e..ee75580cc1 100644 --- a/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs +++ b/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs @@ -72,19 +72,9 @@ namespace Emby.Server.Implementations.SyncPlay _userManager = userManager; _sessionManager = sessionManager; _libraryManager = libraryManager; - _sessionManager.SessionStarted += OnSessionManagerSessionStarted; - _sessionManager.SessionEnded += OnSessionManagerSessionEnded; - _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart; - _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped; } - /// - /// Gets all groups. - /// - /// All groups. - public IEnumerable Groups => _groups.Values; - /// public void Dispose() { @@ -92,127 +82,6 @@ namespace Emby.Server.Implementations.SyncPlay GC.SuppressFinalize(this); } - /// - /// Releases unmanaged and optionally managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool disposing) - { - if (_disposed) - { - return; - } - - _sessionManager.SessionStarted -= OnSessionManagerSessionStarted; - _sessionManager.SessionEnded -= OnSessionManagerSessionEnded; - _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart; - _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped; - - _disposed = true; - } - - private void OnSessionManagerSessionStarted(object sender, SessionEventArgs e) - { - var session = e.SessionInfo; - if (!IsSessionInGroup(session)) - { - return; - } - - var groupId = GetSessionGroup(session) ?? Guid.Empty; - var request = new JoinGroupRequest() - { - GroupId = groupId - }; - JoinGroup(session, groupId, request, CancellationToken.None); - } - - private void OnSessionManagerSessionEnded(object sender, SessionEventArgs e) - { - var session = e.SessionInfo; - if (!IsSessionInGroup(session)) - { - return; - } - - // TODO: probably remove this event, not used at the moment. - } - - private void OnSessionManagerPlaybackStart(object sender, PlaybackProgressEventArgs e) - { - var session = e.Session; - if (!IsSessionInGroup(session)) - { - return; - } - - // TODO: probably remove this event, not used at the moment. - } - - private void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e) - { - var session = e.Session; - if (!IsSessionInGroup(session)) - { - return; - } - - // TODO: probably remove this event, not used at the moment. - } - - private bool IsRequestValid(SessionInfo session, GroupRequestType requestType, T request, bool checkRequest = true) - { - if (session == null || (request == null && checkRequest)) - { - return false; - } - - var user = _userManager.GetUserById(session.UserId); - - if (user.SyncPlayAccess == SyncPlayAccess.None) - { - _logger.LogWarning("IsRequestValid: {0} does not have access to SyncPlay. Requested {1}.", session.Id, requestType); - - var error = new GroupUpdate() - { - // TODO: rename to a more generic error. Next PR will fix this. - Type = GroupUpdateType.JoinGroupDenied - }; - _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); - return false; - } - - if (requestType.Equals(GroupRequestType.NewGroup) && user.SyncPlayAccess != SyncPlayAccess.CreateAndJoinGroups) - { - _logger.LogWarning("IsRequestValid: {0} does not have permission to create groups.", session.Id); - - var error = new GroupUpdate - { - Type = GroupUpdateType.CreateGroupDenied - }; - _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); - return false; - } - - return true; - } - - private bool IsRequestValid(SessionInfo session, GroupRequestType requestType) - { - return IsRequestValid(session, requestType, session, false); - } - - private bool IsSessionInGroup(SessionInfo session) - { - return _sessionToGroupMap.ContainsKey(session.Id); - } - - private Guid? GetSessionGroup(SessionInfo session) - { - _sessionToGroupMap.TryGetValue(session.Id, out var group); - return group?.GroupId; - } - /// public void NewGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken) { @@ -229,9 +98,10 @@ namespace Emby.Server.Implementations.SyncPlay LeaveGroup(session, cancellationToken); } - var group = new GroupController(_logger, _userManager, _sessionManager, _libraryManager, this); + var group = new GroupController(_logger, _userManager, _sessionManager, _libraryManager); _groups[group.GroupId] = group; + AddSessionToGroup(session, group); group.CreateGroup(session, request, cancellationToken); } } @@ -253,25 +123,18 @@ namespace Emby.Server.Implementations.SyncPlay if (group == null) { - _logger.LogWarning("JoinGroup: {0} tried to join group {0} that does not exist.", session.Id, groupId); + _logger.LogWarning("JoinGroup: {SessionId} tried to join group {GroupId} that does not exist.", session.Id, groupId); - var error = new GroupUpdate() - { - Type = GroupUpdateType.GroupDoesNotExist - }; + var error = new GroupUpdate(Guid.Empty, GroupUpdateType.GroupDoesNotExist, string.Empty); _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); return; } if (!group.HasAccessToPlayQueue(user)) { - _logger.LogWarning("JoinGroup: {0} does not have access to some content from the playing queue of group {1}.", session.Id, group.GroupId.ToString()); + _logger.LogWarning("JoinGroup: {SessionId} does not have access to some content from the playing queue of group {GroupId}.", session.Id, group.GroupId.ToString()); - var error = new GroupUpdate() - { - GroupId = group.GroupId.ToString(), - Type = GroupUpdateType.LibraryAccessDenied - }; + var error = new GroupUpdate(group.GroupId, GroupUpdateType.LibraryAccessDenied, string.Empty); _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); return; } @@ -287,6 +150,7 @@ namespace Emby.Server.Implementations.SyncPlay LeaveGroup(session, cancellationToken); } + AddSessionToGroup(session, group); group.SessionJoin(session, request, cancellationToken); } } @@ -307,21 +171,19 @@ namespace Emby.Server.Implementations.SyncPlay if (group == null) { - _logger.LogWarning("LeaveGroup: {0} does not belong to any group.", session.Id); + _logger.LogWarning("LeaveGroup: {SessionId} does not belong to any group.", session.Id); - var error = new GroupUpdate() - { - Type = GroupUpdateType.NotInGroup - }; + var error = new GroupUpdate(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty); _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); return; } + RemoveSessionFromGroup(session, group); group.SessionLeave(session, cancellationToken); if (group.IsGroupEmpty()) { - _logger.LogInformation("LeaveGroup: removing empty group {0}.", group.GroupId); + _logger.LogInformation("LeaveGroup: removing empty group {GroupId}.", group.GroupId); _groups.Remove(group.GroupId, out _); } } @@ -338,11 +200,14 @@ namespace Emby.Server.Implementations.SyncPlay var user = _userManager.GetUserById(session.UserId); - return _groups - .Values - .Where(group => group.HasAccessToPlayQueue(user)) - .Select(group => group.GetInfo()) - .ToList(); + lock (_groupsLock) + { + return _groups + .Values + .Where(group => group.HasAccessToPlayQueue(user)) + .Select(group => group.GetInfo()) + .ToList(); + } } /// @@ -360,12 +225,9 @@ namespace Emby.Server.Implementations.SyncPlay if (group == null) { - _logger.LogWarning("HandleRequest: {0} does not belong to any group.", session.Id); + _logger.LogWarning("HandleRequest: {SessionId} does not belong to any group.", session.Id); - var error = new GroupUpdate() - { - Type = GroupUpdateType.NotInGroup - }; + var error = new GroupUpdate(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty); _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); return; } @@ -374,8 +236,74 @@ namespace Emby.Server.Implementations.SyncPlay } } - /// - public void AddSessionToGroup(SessionInfo session, IGroupController group) + /// + /// Releases unmanaged and optionally managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + _sessionManager.SessionStarted -= OnSessionManagerSessionStarted; + _disposed = true; + } + + private void OnSessionManagerSessionStarted(object sender, SessionEventArgs e) + { + var session = e.SessionInfo; + lock (_groupsLock) + { + if (!IsSessionInGroup(session)) + { + return; + } + + var groupId = GetSessionGroup(session); + var request = new JoinGroupRequest(groupId); + JoinGroup(session, groupId, request, CancellationToken.None); + } + } + + /// + /// Checks if a given session has joined a group. + /// + /// + /// Not thread-safe, call only under groups-lock. + /// + /// The session. + /// true if the session has joined a group, false otherwise. + private bool IsSessionInGroup(SessionInfo session) + { + return _sessionToGroupMap.ContainsKey(session.Id); + } + + /// + /// Gets the group joined by the given session, if any. + /// + /// + /// Not thread-safe, call only under groups-lock. + /// + /// The session. + /// The group identifier if the session has joined a group, an empty identifier otherwise. + private Guid GetSessionGroup(SessionInfo session) + { + _sessionToGroupMap.TryGetValue(session.Id, out var group); + return group?.GroupId ?? Guid.Empty; + } + + /// + /// Maps a session to a group. + /// + /// + /// Not thread-safe, call only under groups-lock. + /// + /// The session. + /// The group. + /// Thrown when the user is in another group already. + private void AddSessionToGroup(SessionInfo session, IGroupController group) { if (session == null) { @@ -390,8 +318,16 @@ namespace Emby.Server.Implementations.SyncPlay _sessionToGroupMap[session.Id] = group ?? throw new InvalidOperationException("Group is null!"); } - /// - public void RemoveSessionFromGroup(SessionInfo session, IGroupController group) + /// + /// Unmaps a session from a group. + /// + /// + /// Not thread-safe, call only under groups-lock. + /// + /// The session. + /// The group. + /// Thrown when the user is not found in the specified group. + private void RemoveSessionFromGroup(SessionInfo session, IGroupController group) { if (session == null) { @@ -414,5 +350,55 @@ namespace Emby.Server.Implementations.SyncPlay throw new InvalidOperationException("Session was in wrong group!"); } } + + /// + /// Checks if a given session is allowed to make a given request. + /// + /// The session. + /// The request type. + /// The request. + /// Whether to check if request is null. + /// true if the request is valid, false otherwise. Will return false also when session is null. + private bool IsRequestValid(SessionInfo session, GroupRequestType requestType, T request, bool checkRequest = true) + { + if (session == null || (request == null && checkRequest)) + { + return false; + } + + var user = _userManager.GetUserById(session.UserId); + + if (user.SyncPlayAccess == SyncPlayAccess.None) + { + _logger.LogWarning("IsRequestValid: {SessionId} does not have access to SyncPlay. Requested {RequestType}.", session.Id, requestType); + + // TODO: rename to a more generic error. Next PR will fix this. + var error = new GroupUpdate(Guid.Empty, GroupUpdateType.JoinGroupDenied, string.Empty); + _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); + return false; + } + + if (requestType.Equals(GroupRequestType.NewGroup) && user.SyncPlayAccess != SyncPlayAccess.CreateAndJoinGroups) + { + _logger.LogWarning("IsRequestValid: {SessionId} does not have permission to create groups.", session.Id); + + var error = new GroupUpdate(Guid.Empty, GroupUpdateType.CreateGroupDenied, string.Empty); + _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None); + return false; + } + + return true; + } + + /// + /// Checks if a given session is allowed to make a given type of request. + /// + /// The session. + /// The request type. + /// true if the request is valid, false otherwise. Will return false also when session is null. + private bool IsRequestValid(SessionInfo session, GroupRequestType requestType) + { + return IsRequestValid(session, requestType, session, false); + } } } diff --git a/Jellyfin.Api/Controllers/SyncPlayController.cs b/Jellyfin.Api/Controllers/SyncPlayController.cs index 9085a71c88..8e9314b4aa 100644 --- a/Jellyfin.Api/Controllers/SyncPlayController.cs +++ b/Jellyfin.Api/Controllers/SyncPlayController.cs @@ -53,10 +53,7 @@ namespace Jellyfin.Api.Controllers [FromQuery, Required] string groupName) { var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request); - var newGroupRequest = new NewGroupRequest() - { - GroupName = groupName - }; + var newGroupRequest = new NewGroupRequest(groupName); _syncPlayManager.NewGroup(currentSession, newGroupRequest, CancellationToken.None); return NoContent(); } @@ -73,10 +70,7 @@ namespace Jellyfin.Api.Controllers [FromQuery, Required] Guid groupId) { var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request); - var joinRequest = new JoinGroupRequest() - { - GroupId = groupId - }; + var joinRequest = new JoinGroupRequest(groupId); _syncPlayManager.JoinGroup(currentSession, groupId, joinRequest, CancellationToken.None); return NoContent(); } @@ -185,18 +179,18 @@ namespace Jellyfin.Api.Controllers /// /// Request to queue items to the playlist of a SyncPlay group. /// - /// The items to add. + /// The items to add. /// The mode in which to enqueue the items. /// Queue update request sent to all group members. /// A indicating success. [HttpPost("Queue")] [ProducesResponseType(StatusCodes.Status204NoContent)] public ActionResult SyncPlayQueue( - [FromQuery, Required] Guid[] items, + [FromQuery, Required] Guid[] itemIds, [FromQuery, Required] GroupQueueMode mode) { var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request); - var syncPlayRequest = new QueueGroupRequest(items, mode); + var syncPlayRequest = new QueueGroupRequest(itemIds, mode); _syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None); return NoContent(); } diff --git a/Jellyfin.Api/Controllers/TimeSyncController.cs b/Jellyfin.Api/Controllers/TimeSyncController.cs index 2dc744e7ca..5de5604173 100644 --- a/Jellyfin.Api/Controllers/TimeSyncController.cs +++ b/Jellyfin.Api/Controllers/TimeSyncController.cs @@ -13,7 +13,7 @@ namespace Jellyfin.Api.Controllers public class TimeSyncController : BaseJellyfinApiController { /// - /// Gets the current utc time. + /// Gets the current UTC time. /// /// Time returned. /// An to sync the client and server time. @@ -22,18 +22,14 @@ namespace Jellyfin.Api.Controllers public ActionResult GetUtcTime() { // Important to keep the following line at the beginning - var requestReceptionTime = DateTime.UtcNow.ToUniversalTime().ToString("o", DateTimeFormatInfo.InvariantInfo); + var requestReceptionTime = DateTime.UtcNow.ToUniversalTime(); - var response = new UtcTimeResponse(); - response.RequestReceptionTime = requestReceptionTime; - - // Important to keep the following two lines at the end - var responseTransmissionTime = DateTime.UtcNow.ToUniversalTime().ToString("o", DateTimeFormatInfo.InvariantInfo); - response.ResponseTransmissionTime = responseTransmissionTime; + // Important to keep the following line at the end + var responseTransmissionTime = DateTime.UtcNow.ToUniversalTime(); // Implementing NTP on such a high level results in this useless // information being sent. On the other hand it enables future additions. - return response; + return new UtcTimeResponse(requestReceptionTime, responseTransmissionTime); } } } diff --git a/MediaBrowser.Controller/SyncPlay/GroupStates/AbstractGroupState.cs b/MediaBrowser.Controller/SyncPlay/GroupStates/AbstractGroupState.cs index bc2e223802..e5da0ef40f 100644 --- a/MediaBrowser.Controller/SyncPlay/GroupStates/AbstractGroupState.cs +++ b/MediaBrowser.Controller/SyncPlay/GroupStates/AbstractGroupState.cs @@ -68,7 +68,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates if (playingItemRemoved && !context.PlayQueue.IsItemPlaying()) { - Logger.LogDebug("HandleRequest: {0} in group {1}, play queue is empty.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, play queue is empty.", request.Type, context.GroupId.ToString()); IGroupState idleState = new IdleGroupState(Logger); context.SetState(idleState); @@ -84,7 +84,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates if (!result) { - Logger.LogError("HandleRequest: {0} in group {1}, unable to move item in play queue.", request.Type, context.GroupId.ToString()); + Logger.LogError("HandleRequest: {RequestType} in group {GroupId}, unable to move item in play queue.", request.Type, context.GroupId.ToString()); return; } @@ -100,7 +100,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates if (!result) { - Logger.LogError("HandleRequest: {0} in group {1}, unable to add items to play queue.", request.Type, context.GroupId.ToString()); + Logger.LogError("HandleRequest: {RequestType} in group {GroupId}, unable to add items to play queue.", request.Type, context.GroupId.ToString()); return; } @@ -203,18 +203,14 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates protected void SendGroupStateUpdate(IGroupStateContext context, IGroupPlaybackRequest reason, SessionInfo session, CancellationToken cancellationToken) { // Notify relevant state change event. - var stateUpdate = new GroupStateUpdate() - { - State = Type, - Reason = reason.Type - }; + var stateUpdate = new GroupStateUpdate(Type, reason.Type); var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.StateUpdate, stateUpdate); context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken); } private void UnhandledRequest(IGroupPlaybackRequest request) { - Logger.LogWarning("HandleRequest: unhandled {0} request for {1} state.", request.Type, Type); + Logger.LogWarning("HandleRequest: unhandled {RequestType} request in {StateType} state.", request.Type, Type); } } } diff --git a/MediaBrowser.Controller/SyncPlay/GroupStates/WaitingGroupState.cs b/MediaBrowser.Controller/SyncPlay/GroupStates/WaitingGroupState.cs index 78318dd94d..e33e711fb7 100644 --- a/MediaBrowser.Controller/SyncPlay/GroupStates/WaitingGroupState.cs +++ b/MediaBrowser.Controller/SyncPlay/GroupStates/WaitingGroupState.cs @@ -103,7 +103,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates var unpauseRequest = new UnpauseGroupRequest(); playingState.HandleRequest(context, Type, unpauseRequest, session, cancellationToken); - Logger.LogDebug("SessionLeaving: {0} left the group {1}, notifying others to resume.", session.Id, context.GroupId.ToString()); + Logger.LogDebug("SessionLeaving: {SessionId} left group {GroupId}, notifying others to resume.", session.Id, context.GroupId.ToString()); } else { @@ -111,7 +111,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates var pausedState = new PausedGroupState(Logger); context.SetState(pausedState); - Logger.LogDebug("SessionLeaving: {0} left the group {1}, returning to previous state.", session.Id, context.GroupId.ToString()); + Logger.LogDebug("SessionLeaving: {SessionId} left group {GroupId}, returning to previous state.", session.Id, context.GroupId.ToString()); } } } @@ -131,7 +131,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates var setQueueStatus = context.SetPlayQueue(request.PlayingQueue, request.PlayingItemPosition, request.StartPositionTicks); if (!setQueueStatus) { - Logger.LogError("HandleRequest: {0} in group {1}, unable to set playing queue.", request.Type, context.GroupId.ToString()); + Logger.LogError("HandleRequest: {RequestType} in group {GroupId}, unable to set playing queue.", request.Type, context.GroupId.ToString()); // Ignore request and return to previous state. IGroupState newState = prevState switch { @@ -151,7 +151,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Reset status of sessions and await for all Ready events. context.SetAllBuffering(true); - Logger.LogDebug("HandleRequest: {0} in group {1}, {2} set a new play queue.", request.Type, context.GroupId.ToString(), session.Id); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, {SessionId} set a new play queue.", request.Type, context.GroupId.ToString(), session.Id); } /// @@ -188,7 +188,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates context.SetState(newState); - Logger.LogDebug("HandleRequest: {0} in group {1}, unable to change current playing item.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, unable to change current playing item.", request.Type, context.GroupId.ToString()); } } @@ -214,13 +214,13 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Reset status of sessions and await for all Ready events. context.SetAllBuffering(true); - Logger.LogDebug("HandleRequest: {0} in group {1}, waiting for all ready events.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, waiting for all ready events.", request.Type, context.GroupId.ToString()); } else { if (ResumePlaying) { - Logger.LogDebug("HandleRequest: {0} in group {1}, ignoring sessions that are not ready and forcing the playback to start.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, ignoring sessions that are not ready and forcing the playback to start.", request.Type, context.GroupId.ToString()); // An Unpause request is forcing the playback to start, ignoring sessions that are not ready. context.SetAllBuffering(false); @@ -326,7 +326,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Make sure the client is playing the correct item. if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId(), StringComparison.OrdinalIgnoreCase)) { - Logger.LogDebug("HandleRequest: {0} in group {1}, {2} has wrong playlist item.", request.Type, context.GroupId.ToString(), session.Id); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, {SessionId} has wrong playlist item.", request.Type, context.GroupId.ToString(), session.Id); var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem); var updateSession = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate); @@ -400,7 +400,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Make sure the client is playing the correct item. if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId(), StringComparison.OrdinalIgnoreCase)) { - Logger.LogDebug("HandleRequest: {0} in group {1}, {2} has wrong playlist item.", request.Type, context.GroupId.ToString(), session.Id); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, {SessionId} has wrong playlist item.", request.Type, context.GroupId.ToString(), session.Id); var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem); var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate); @@ -420,7 +420,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates var timeSyncThresholdTicks = TimeSpan.FromMilliseconds(context.TimeSyncOffset).Ticks; if (Math.Abs(elapsedTime.Ticks) > timeSyncThresholdTicks) { - Logger.LogWarning("HandleRequest: {0} in group {1}, {2} is not time syncing properly. Ignoring elapsed time.", request.Type, context.GroupId.ToString(), session.Id); + Logger.LogWarning("HandleRequest: {RequestType} in group {GroupId}, {SessionId} is not time syncing properly. Ignoring elapsed time.", request.Type, context.GroupId.ToString(), session.Id); elapsedTime = TimeSpan.Zero; } @@ -436,7 +436,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates var delayTicks = context.PositionTicks - clientPosition.Ticks; var maxPlaybackOffsetTicks = TimeSpan.FromMilliseconds(context.MaxPlaybackOffset).Ticks; - Logger.LogDebug("HandleRequest: {0} in group {1}, {2} at {3} (delay of {4} seconds).", request.Type, context.GroupId.ToString(), session.Id, clientPosition, TimeSpan.FromTicks(delayTicks).TotalSeconds); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, {SessionId} at {PositionTicks} (delay of {Delay} seconds).", request.Type, context.GroupId.ToString(), session.Id, clientPosition, TimeSpan.FromTicks(delayTicks).TotalSeconds); if (ResumePlaying) { @@ -454,7 +454,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Notify relevant state change event. SendGroupStateUpdate(context, request, session, cancellationToken); - Logger.LogWarning("HandleRequest: {0} in group {1}, {2} got lost in time, correcting.", request.Type, context.GroupId.ToString(), session.Id); + Logger.LogWarning("HandleRequest: {RequestType} in group {GroupId}, {SessionId} got lost in time, correcting.", request.Type, context.GroupId.ToString(), session.Id); return; } @@ -468,7 +468,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates command.When = currentTime.AddTicks(delayTicks); context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken); - Logger.LogInformation("HandleRequest: {0} in group {1}, others still buffering, {2} will pause when ready in {3} seconds.", request.Type, context.GroupId.ToString(), session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds); + Logger.LogInformation("HandleRequest: {RequestType} in group {GroupId}, others still buffering, {SessionId} will pause when ready in {Delay} seconds.", request.Type, context.GroupId.ToString(), session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds); } else { @@ -487,7 +487,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates context.SendCommand(session, filter, command, cancellationToken); - Logger.LogInformation("HandleRequest: {0} in group {1}, {2} is recovering, notifying others to resume in {3} seconds.", request.Type, context.GroupId.ToString(), session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds); + Logger.LogInformation("HandleRequest: {RequestType} in group {GroupId}, {SessionId} is recovering, notifying others to resume in {Delay} seconds.", request.Type, context.GroupId.ToString(), session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds); } else { @@ -500,7 +500,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates var command = context.NewSyncPlayCommand(SendCommandType.Unpause); context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken); - Logger.LogWarning("HandleRequest: {0} in group {1}, {2} resumed playback but did not update others in time. {3} seconds to recover.", request.Type, context.GroupId.ToString(), session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds); + Logger.LogWarning("HandleRequest: {RequestType} in group {GroupId}, {SessionId} resumed playback but did not update others in time. {Delay} seconds to recover.", request.Type, context.GroupId.ToString(), session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds); } // Change state. @@ -511,7 +511,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates } else { - // Check that session is really ready, tollerate player imperfections under a certain threshold. + // Check that session is really ready, tolerate player imperfections under a certain threshold. if (Math.Abs(context.PositionTicks - requestTicks) > maxPlaybackOffsetTicks) { // Session still not ready. @@ -523,7 +523,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Notify relevant state change event. SendGroupStateUpdate(context, request, session, cancellationToken); - Logger.LogWarning("HandleRequest: {0} in group {1}, {2} was seeking to wrong position, correcting.", request.Type, context.GroupId.ToString(), session.Id); + Logger.LogWarning("HandleRequest: {RequestType} in group {GroupId}, {SessionId} is seeking to wrong position, correcting.", request.Type, context.GroupId.ToString(), session.Id); return; } else @@ -549,7 +549,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates pausedState.HandleRequest(context, Type, request, session, cancellationToken); } - Logger.LogDebug("HandleRequest: {0} in group {1}, {2} is ready, returning to previous state.", request.Type, context.GroupId.ToString(), session.Id); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, {SessionId} is ready, returning to previous state.", request.Type, context.GroupId.ToString(), session.Id); } } } @@ -569,7 +569,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Make sure the client knows the playing item, to avoid duplicate requests. if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId(), StringComparison.OrdinalIgnoreCase)) { - Logger.LogDebug("HandleRequest: {0} in group {1}, client provided the wrong playlist identifier.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, {SessionId} provided the wrong playlist identifier.", request.Type, context.GroupId.ToString(), session.Id); return; } @@ -596,7 +596,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates context.SetState(newState); - Logger.LogDebug("HandleRequest: {0} in group {1}, no next track available.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, no next track available.", request.Type, context.GroupId.ToString()); } } @@ -615,7 +615,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates // Make sure the client knows the playing item, to avoid duplicate requests. if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId(), StringComparison.OrdinalIgnoreCase)) { - Logger.LogDebug("HandleRequest: {0} in group {1}, client provided the wrong playlist identifier.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, {SessionId} provided the wrong playlist identifier.", request.Type, context.GroupId.ToString(), session.Id); return; } @@ -642,7 +642,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates context.SetState(newState); - Logger.LogDebug("HandleRequest: {0} in group {1}, no previous track available.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, no previous track available.", request.Type, context.GroupId.ToString()); } } @@ -653,7 +653,7 @@ namespace MediaBrowser.Controller.SyncPlay.GroupStates if (!context.IsBuffering()) { - Logger.LogDebug("HandleRequest: {0} in group {1}, returning to previous state.", request.Type, context.GroupId.ToString()); + Logger.LogDebug("HandleRequest: {RequestType} in group {GroupId}, returning to previous state.", request.Type, context.GroupId.ToString()); if (ResumePlaying) { diff --git a/MediaBrowser.Controller/SyncPlay/IGroupStateContext.cs b/MediaBrowser.Controller/SyncPlay/IGroupStateContext.cs index 3609be36b7..13f1b23169 100644 --- a/MediaBrowser.Controller/SyncPlay/IGroupStateContext.cs +++ b/MediaBrowser.Controller/SyncPlay/IGroupStateContext.cs @@ -151,7 +151,7 @@ namespace MediaBrowser.Controller.SyncPlay /// The playing item position in the play queue. /// The start position ticks. /// true if the play queue has been changed; false if something went wrong. - bool SetPlayQueue(IEnumerable playQueue, int playingItemPosition, long startPositionTicks); + bool SetPlayQueue(IReadOnlyList playQueue, int playingItemPosition, long startPositionTicks); /// /// Sets the playing item. @@ -165,7 +165,7 @@ namespace MediaBrowser.Controller.SyncPlay /// /// The items to remove. /// true if playing item got removed; false otherwise. - bool RemoveFromPlayQueue(IEnumerable playlistItemIds); + bool RemoveFromPlayQueue(IReadOnlyList playlistItemIds); /// /// Moves an item in the play queue. @@ -181,7 +181,7 @@ namespace MediaBrowser.Controller.SyncPlay /// The new items to add to the play queue. /// The mode with which the items will be added. /// true if the play queue has been changed; false if something went wrong. - bool AddToPlayQueue(IEnumerable newItems, GroupQueueMode mode); + bool AddToPlayQueue(IReadOnlyList newItems, GroupQueueMode mode); /// /// Restarts current item in play queue. diff --git a/MediaBrowser.Controller/SyncPlay/ISyncPlayManager.cs b/MediaBrowser.Controller/SyncPlay/ISyncPlayManager.cs index 65146d4ae8..a980016827 100644 --- a/MediaBrowser.Controller/SyncPlay/ISyncPlayManager.cs +++ b/MediaBrowser.Controller/SyncPlay/ISyncPlayManager.cs @@ -49,21 +49,5 @@ namespace MediaBrowser.Controller.SyncPlay /// The request. /// The cancellation token. void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken); - - /// - /// Maps a session to a group. - /// - /// The session. - /// The group. - /// Thrown when the user is in another group already. - void AddSessionToGroup(SessionInfo session, IGroupController group); - - /// - /// Unmaps a session from a group. - /// - /// The session. - /// The group. - /// Thrown when the user is not found in the specified group. - void RemoveSessionFromGroup(SessionInfo session, IGroupController group); } } diff --git a/MediaBrowser.Controller/SyncPlay/PlaybackRequests/PlayGroupRequest.cs b/MediaBrowser.Controller/SyncPlay/PlaybackRequests/PlayGroupRequest.cs index 306c161ed9..7d27f61518 100644 --- a/MediaBrowser.Controller/SyncPlay/PlaybackRequests/PlayGroupRequest.cs +++ b/MediaBrowser.Controller/SyncPlay/PlaybackRequests/PlayGroupRequest.cs @@ -19,9 +19,7 @@ namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests /// The start position ticks. public PlayGroupRequest(Guid[] playingQueue, int playingItemPosition, long startPositionTicks) { - var list = new List(); - list.AddRange(playingQueue); - PlayingQueue = list; + PlayingQueue = playingQueue ?? Array.Empty(); PlayingItemPosition = playingItemPosition; StartPositionTicks = startPositionTicks; } diff --git a/MediaBrowser.Controller/SyncPlay/PlaybackRequests/QueueGroupRequest.cs b/MediaBrowser.Controller/SyncPlay/PlaybackRequests/QueueGroupRequest.cs index 9580b53154..106daecc8c 100644 --- a/MediaBrowser.Controller/SyncPlay/PlaybackRequests/QueueGroupRequest.cs +++ b/MediaBrowser.Controller/SyncPlay/PlaybackRequests/QueueGroupRequest.cs @@ -18,9 +18,7 @@ namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests /// The enqueue mode. public QueueGroupRequest(Guid[] items, GroupQueueMode mode) { - var list = new List(); - list.AddRange(items); - ItemIds = list; + ItemIds = items ?? Array.Empty(); Mode = mode; } diff --git a/MediaBrowser.Controller/SyncPlay/PlaybackRequests/RemoveFromPlaylistGroupRequest.cs b/MediaBrowser.Controller/SyncPlay/PlaybackRequests/RemoveFromPlaylistGroupRequest.cs index 21c602846e..1e892d8196 100644 --- a/MediaBrowser.Controller/SyncPlay/PlaybackRequests/RemoveFromPlaylistGroupRequest.cs +++ b/MediaBrowser.Controller/SyncPlay/PlaybackRequests/RemoveFromPlaylistGroupRequest.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading; using MediaBrowser.Controller.Session; @@ -16,9 +17,7 @@ namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests /// The playlist ids of the items to remove. public RemoveFromPlaylistGroupRequest(string[] items) { - var list = new List(); - list.AddRange(items); - PlaylistItemIds = list; + PlaylistItemIds = items ?? Array.Empty(); } /// diff --git a/MediaBrowser.Controller/SyncPlay/Queue/PlayQueueManager.cs b/MediaBrowser.Controller/SyncPlay/Queue/PlayQueueManager.cs index 2d1d1533b9..73457f4471 100644 --- a/MediaBrowser.Controller/SyncPlay/Queue/PlayQueueManager.cs +++ b/MediaBrowser.Controller/SyncPlay/Queue/PlayQueueManager.cs @@ -94,7 +94,7 @@ namespace MediaBrowser.Controller.SyncPlay.Queue /// Sets a new playlist. Playing item is reset. /// /// The new items of the playlist. - public void SetPlaylist(IEnumerable items) + public void SetPlaylist(IReadOnlyList items) { SortedPlaylist.Clear(); ShuffledPlaylist.Clear(); @@ -114,7 +114,7 @@ namespace MediaBrowser.Controller.SyncPlay.Queue /// Appends new items to the playlist. The specified order is mantained. /// /// The items to add to the playlist. - public void Queue(IEnumerable items) + public void Queue(IReadOnlyList items) { var newItems = CreateQueueItemsFromArray(items); @@ -209,7 +209,7 @@ namespace MediaBrowser.Controller.SyncPlay.Queue /// Adds new items to the playlist right after the playing item. The specified order is mantained. /// /// The items to add to the playlist. - public void QueueNext(IEnumerable items) + public void QueueNext(IReadOnlyList items) { var newItems = CreateQueueItemsFromArray(items); @@ -312,13 +312,12 @@ namespace MediaBrowser.Controller.SyncPlay.Queue /// /// The items to remove. /// true if playing item got removed; false otherwise. - public bool RemoveFromPlaylist(IEnumerable playlistItemIds) + public bool RemoveFromPlaylist(IReadOnlyList playlistItemIds) { var playingItem = GetPlayingItem(); - var playlistItemIdsList = playlistItemIds.ToList(); - SortedPlaylist.RemoveAll(item => playlistItemIdsList.Contains(item.PlaylistItemId)); - ShuffledPlaylist.RemoveAll(item => playlistItemIdsList.Contains(item.PlaylistItemId)); + SortedPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId)); + ShuffledPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId)); LastChange = DateTime.UtcNow; @@ -369,8 +368,7 @@ namespace MediaBrowser.Controller.SyncPlay.Queue var queueItem = playlist[oldIndex]; playlist.RemoveAt(oldIndex); - newIndex = Math.Min(newIndex, playlist.Count); - newIndex = Math.Max(newIndex, 0); + newIndex = Math.Clamp(newIndex, 0, playlist.Count); playlist.Insert(newIndex, queueItem); LastChange = DateTime.UtcNow; @@ -489,7 +487,7 @@ namespace MediaBrowser.Controller.SyncPlay.Queue } else { - PlayingItemIndex--; + PlayingItemIndex = SortedPlaylist.Count - 1; return false; } } @@ -519,7 +517,7 @@ namespace MediaBrowser.Controller.SyncPlay.Queue } else { - PlayingItemIndex++; + PlayingItemIndex = 0; return false; } } @@ -558,7 +556,7 @@ namespace MediaBrowser.Controller.SyncPlay.Queue /// Creates a list from the array of items. Each item is given an unique playlist identifier. /// /// The list of queue items. - private List CreateQueueItemsFromArray(IEnumerable items) + private List CreateQueueItemsFromArray(IReadOnlyList items) { var list = new List(); foreach (var item in items) diff --git a/MediaBrowser.Model/SyncPlay/GroupInfoDto.cs b/MediaBrowser.Model/SyncPlay/GroupInfoDto.cs index 16a75eb68e..8c0960b830 100644 --- a/MediaBrowser.Model/SyncPlay/GroupInfoDto.cs +++ b/MediaBrowser.Model/SyncPlay/GroupInfoDto.cs @@ -11,41 +11,48 @@ namespace MediaBrowser.Model.SyncPlay /// /// Initializes a new instance of the class. /// - public GroupInfoDto() + /// The group identifier. + /// The group name. + /// The group state. + /// The participants. + /// The date when this DTO has been created. + public GroupInfoDto(Guid groupId, string groupName, GroupStateType state, IReadOnlyList participants, DateTime lastUpdatedAt) { - GroupId = string.Empty; - GroupName = string.Empty; - Participants = new List(); + GroupId = groupId; + GroupName = groupName; + State = state; + Participants = participants; + LastUpdatedAt = lastUpdatedAt; } /// - /// Gets or sets the group identifier. + /// Gets the group identifier. /// /// The group identifier. - public string GroupId { get; set; } + public Guid GroupId { get; } /// - /// Gets or sets the group name. + /// Gets the group name. /// /// The group name. - public string GroupName { get; set; } + public string GroupName { get; } /// - /// Gets or sets the group state. + /// Gets the group state. /// /// The group state. - public GroupStateType State { get; set; } + public GroupStateType State { get; } /// - /// Gets or sets the participants. + /// Gets the participants. /// /// The participants. - public IReadOnlyList Participants { get; set; } + public IReadOnlyList Participants { get; } /// - /// Gets or sets the date when this dto has been updated. + /// Gets the date when this DTO has been created. /// - /// The date when this dto has been updated. - public DateTime LastUpdatedAt { get; set; } + /// The date when this DTO has been created. + public DateTime LastUpdatedAt { get; } } } diff --git a/MediaBrowser.Model/SyncPlay/GroupStateUpdate.cs b/MediaBrowser.Model/SyncPlay/GroupStateUpdate.cs index 532b5a56f4..7f7deb86bb 100644 --- a/MediaBrowser.Model/SyncPlay/GroupStateUpdate.cs +++ b/MediaBrowser.Model/SyncPlay/GroupStateUpdate.cs @@ -6,15 +6,26 @@ namespace MediaBrowser.Model.SyncPlay public class GroupStateUpdate { /// - /// Gets or sets the state of the group. + /// Initializes a new instance of the class. /// - /// The state of the group. - public GroupStateType State { get; set; } + /// The state of the group. + /// The reason of the state change. + public GroupStateUpdate(GroupStateType state, PlaybackRequestType reason) + { + State = state; + Reason = reason; + } /// - /// Gets or sets the reason of the state change. + /// Gets the state of the group. + /// + /// The state of the group. + public GroupStateType State { get; } + + /// + /// Gets the reason of the state change. /// /// The reason of the state change. - public PlaybackRequestType Reason { get; set; } + public PlaybackRequestType Reason { get; } } } diff --git a/MediaBrowser.Model/SyncPlay/GroupUpdate.cs b/MediaBrowser.Model/SyncPlay/GroupUpdate.cs index 12d6058ac3..6f159d653c 100644 --- a/MediaBrowser.Model/SyncPlay/GroupUpdate.cs +++ b/MediaBrowser.Model/SyncPlay/GroupUpdate.cs @@ -1,4 +1,4 @@ -#nullable disable +using System; namespace MediaBrowser.Model.SyncPlay { @@ -9,21 +9,34 @@ namespace MediaBrowser.Model.SyncPlay public class GroupUpdate { /// - /// Gets or sets the group identifier. + /// Initializes a new instance of the class. + /// + /// The group identifier. + /// The update type. + /// The update data. + public GroupUpdate(Guid groupId, GroupUpdateType type, T data) + { + GroupId = groupId; + Type = type; + Data = data; + } + + /// + /// Gets the group identifier. /// /// The group identifier. - public string GroupId { get; set; } + public Guid GroupId { get; } /// - /// Gets or sets the update type. + /// Gets the update type. /// /// The update type. - public GroupUpdateType Type { get; set; } + public GroupUpdateType Type { get; } /// - /// Gets or sets the data. + /// Gets the update data. /// - /// The data. - public T Data { get; set; } + /// The update data. + public T Data { get; } } } diff --git a/MediaBrowser.Model/SyncPlay/JoinGroupRequest.cs b/MediaBrowser.Model/SyncPlay/JoinGroupRequest.cs index 27a29b8998..7402c4ce29 100644 --- a/MediaBrowser.Model/SyncPlay/JoinGroupRequest.cs +++ b/MediaBrowser.Model/SyncPlay/JoinGroupRequest.cs @@ -8,9 +8,18 @@ namespace MediaBrowser.Model.SyncPlay public class JoinGroupRequest { /// - /// Gets or sets the group identifier. + /// Initializes a new instance of the class. + /// + /// The identifier of the group to join. + public JoinGroupRequest(Guid groupId) + { + GroupId = groupId; + } + + /// + /// Gets the group identifier. /// /// The identifier of the group to join. - public Guid GroupId { get; set; } + public Guid GroupId { get; } } } diff --git a/MediaBrowser.Model/SyncPlay/NewGroupRequest.cs b/MediaBrowser.Model/SyncPlay/NewGroupRequest.cs index eb61a68d15..ba4bd3ef1c 100644 --- a/MediaBrowser.Model/SyncPlay/NewGroupRequest.cs +++ b/MediaBrowser.Model/SyncPlay/NewGroupRequest.cs @@ -8,15 +8,16 @@ namespace MediaBrowser.Model.SyncPlay /// /// Initializes a new instance of the class. /// - public NewGroupRequest() + /// The name of the new group. + public NewGroupRequest(string groupName) { - GroupName = string.Empty; + GroupName = groupName; } /// - /// Gets or sets the group name. + /// Gets the group name. /// /// The name of the new group. - public string GroupName { get; set; } + public string GroupName { get; } } } diff --git a/MediaBrowser.Model/SyncPlay/PlayQueueUpdate.cs b/MediaBrowser.Model/SyncPlay/PlayQueueUpdate.cs index d193b4c663..a851229f74 100644 --- a/MediaBrowser.Model/SyncPlay/PlayQueueUpdate.cs +++ b/MediaBrowser.Model/SyncPlay/PlayQueueUpdate.cs @@ -11,51 +11,64 @@ namespace MediaBrowser.Model.SyncPlay /// /// Initializes a new instance of the class. /// - public PlayQueueUpdate() + /// The reason for the update. + /// The UTC time of the last change to the playing queue. + /// The playlist. + /// The playing item index in the playlist. + /// The start position ticks. + /// The shuffle mode. + /// The repeat mode. + public PlayQueueUpdate(PlayQueueUpdateReason reason, DateTime lastUpdate, IReadOnlyList playlist, int playingItemIndex, long startPositionTicks, GroupShuffleMode shuffleMode, GroupRepeatMode repeatMode) { - Playlist = new List(); + Reason = reason; + LastUpdate = lastUpdate; + Playlist = playlist; + PlayingItemIndex = playingItemIndex; + StartPositionTicks = startPositionTicks; + ShuffleMode = shuffleMode; + RepeatMode = repeatMode; } /// - /// Gets or sets the request type that originated this update. + /// Gets the request type that originated this update. /// /// The reason for the update. - public PlayQueueUpdateReason Reason { get; set; } + public PlayQueueUpdateReason Reason { get; } /// - /// Gets or sets the UTC time of the last change to the playing queue. + /// Gets the UTC time of the last change to the playing queue. /// /// The UTC time of the last change to the playing queue. - public DateTime LastUpdate { get; set; } + public DateTime LastUpdate { get; } /// - /// Gets or sets the playlist. + /// Gets the playlist. /// /// The playlist. - public IReadOnlyList Playlist { get; set; } + public IReadOnlyList Playlist { get; } /// - /// Gets or sets the playing item index in the playlist. + /// Gets the playing item index in the playlist. /// /// The playing item index in the playlist. - public int PlayingItemIndex { get; set; } + public int PlayingItemIndex { get; } /// - /// Gets or sets the start position ticks. + /// Gets the start position ticks. /// /// The start position ticks. - public long StartPositionTicks { get; set; } + public long StartPositionTicks { get; } /// - /// Gets or sets the shuffle mode. + /// Gets the shuffle mode. /// /// The shuffle mode. - public GroupShuffleMode ShuffleMode { get; set; } + public GroupShuffleMode ShuffleMode { get; } /// - /// Gets or sets the repeat mode. + /// Gets the repeat mode. /// /// The repeat mode. - public GroupRepeatMode RepeatMode { get; set; } + public GroupRepeatMode RepeatMode { get; } } } diff --git a/MediaBrowser.Model/SyncPlay/SendCommand.cs b/MediaBrowser.Model/SyncPlay/SendCommand.cs index a3aa54b380..ab4c64130c 100644 --- a/MediaBrowser.Model/SyncPlay/SendCommand.cs +++ b/MediaBrowser.Model/SyncPlay/SendCommand.cs @@ -10,23 +10,33 @@ namespace MediaBrowser.Model.SyncPlay /// /// Initializes a new instance of the class. /// - public SendCommand() + /// The group identifier. + /// The playlist identifier of the playing item. + /// The UTC time when to execute the command. + /// The command. + /// The position ticks, for commands that require it. + /// The UTC time when this command has been emitted. + public SendCommand(Guid groupId, string playlistItemId, DateTime when, SendCommandType command, long? positionTicks, DateTime emittedAt) { - GroupId = string.Empty; - PlaylistItemId = string.Empty; + GroupId = groupId; + PlaylistItemId = playlistItemId; + When = when; + Command = command; + PositionTicks = positionTicks; + EmittedAt = emittedAt; } /// - /// Gets or sets the group identifier. + /// Gets the group identifier. /// /// The group identifier. - public string GroupId { get; set; } + public Guid GroupId { get; } /// - /// Gets or sets the playlist identifier of the playing item. + /// Gets the playlist identifier of the playing item. /// /// The playlist identifier of the playing item. - public string PlaylistItemId { get; set; } + public string PlaylistItemId { get; } /// /// Gets or sets the UTC time when to execute the command. @@ -35,21 +45,21 @@ namespace MediaBrowser.Model.SyncPlay public DateTime When { get; set; } /// - /// Gets or sets the position ticks. + /// Gets the position ticks. /// /// The position ticks. - public long? PositionTicks { get; set; } + public long? PositionTicks { get; } /// - /// Gets or sets the command. + /// Gets the command. /// /// The command. - public SendCommandType Command { get; set; } + public SendCommandType Command { get; } /// - /// Gets or sets the UTC time when this command has been emitted. + /// Gets the UTC time when this command has been emitted. /// /// The UTC time when this command has been emitted. - public DateTime EmittedAt { get; set; } + public DateTime EmittedAt { get; } } } diff --git a/MediaBrowser.Model/SyncPlay/UtcTimeResponse.cs b/MediaBrowser.Model/SyncPlay/UtcTimeResponse.cs index 8ec5eaab3b..219e7b1e0c 100644 --- a/MediaBrowser.Model/SyncPlay/UtcTimeResponse.cs +++ b/MediaBrowser.Model/SyncPlay/UtcTimeResponse.cs @@ -1,4 +1,4 @@ -#nullable disable +using System; namespace MediaBrowser.Model.SyncPlay { @@ -8,15 +8,26 @@ namespace MediaBrowser.Model.SyncPlay public class UtcTimeResponse { /// - /// Gets or sets the UTC time when request has been received. + /// Initializes a new instance of the class. /// - /// The UTC time when request has been received. - public string RequestReceptionTime { get; set; } + /// The UTC time when request has been received. + /// The UTC time when response has been sent. + public UtcTimeResponse(DateTime requestReceptionTime, DateTime responseTransmissionTime) + { + RequestReceptionTime = requestReceptionTime; + ResponseTransmissionTime = responseTransmissionTime; + } /// - /// Gets or sets the UTC time when response has been sent. + /// Gets the UTC time when request has been received. + /// + /// The UTC time when request has been received. + public DateTime RequestReceptionTime { get; } + + /// + /// Gets the UTC time when response has been sent. /// /// The UTC time when response has been sent. - public string ResponseTransmissionTime { get; set; } + public DateTime ResponseTransmissionTime { get; } } }