jellyfin-server/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs
2020-12-03 21:01:18 +01:00

398 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.SyncPlay;
using MediaBrowser.Controller.SyncPlay.Requests;
using MediaBrowser.Model.SyncPlay;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.SyncPlay
{
/// <summary>
/// Class SyncPlayManager.
/// </summary>
public class SyncPlayManager : ISyncPlayManager, IDisposable
{
/// <summary>
/// The logger.
/// </summary>
private readonly ILogger<SyncPlayManager> _logger;
/// <summary>
/// The logger factory.
/// </summary>
private readonly ILoggerFactory _loggerFactory;
/// <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 map between sessions and groups.
/// </summary>
private readonly Dictionary<string, Group> _sessionToGroupMap =
new Dictionary<string, Group>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The groups.
/// </summary>
private readonly Dictionary<Guid, Group> _groups =
new Dictionary<Guid, Group>();
/// <summary>
/// Lock used for accessing any group.
/// </summary>
/// <remarks>
/// Always lock before <see cref="_mapsLock"/> and before locking on any <see cref="Group"/>.
/// </remarks>
private readonly object _groupsLock = new object();
/// <summary>
/// Lock used for accessing the session-to-group map.
/// </summary>
/// <remarks>
/// Always lock after <see cref="_groupsLock"/> and before locking on any <see cref="Group"/>.
/// </remarks>
private readonly object _mapsLock = new object();
private bool _disposed = false;
/// <summary>
/// Initializes a new instance of the <see cref="SyncPlayManager" /> class.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="userManager">The user manager.</param>
/// <param name="sessionManager">The session manager.</param>
/// <param name="libraryManager">The library manager.</param>
public SyncPlayManager(
ILoggerFactory loggerFactory,
IUserManager userManager,
ISessionManager sessionManager,
ILibraryManager libraryManager)
{
_loggerFactory = loggerFactory;
_userManager = userManager;
_sessionManager = sessionManager;
_libraryManager = libraryManager;
_logger = loggerFactory.CreateLogger<SyncPlayManager>();
_sessionManager.SessionStarted += OnSessionManagerSessionStarted;
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <inheritdoc />
public void NewGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
{
// Locking required to access list of groups.
lock (_groupsLock)
{
// Locking required as session-to-group map will be edited.
// Locking the group is not required as it is not visible yet.
lock (_mapsLock)
{
if (IsSessionInGroup(session))
{
var leaveGroupRequest = new LeaveGroupRequest();
LeaveGroup(session, leaveGroupRequest, cancellationToken);
}
var group = new Group(_loggerFactory, _userManager, _sessionManager, _libraryManager);
_groups[group.GroupId] = group;
AddSessionToGroup(session, group);
group.CreateGroup(session, request, cancellationToken);
}
}
}
/// <inheritdoc />
public void JoinGroup(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
{
var user = _userManager.GetUserById(session.UserId);
// Locking required to access list of groups.
lock (_groupsLock)
{
_groups.TryGetValue(request.GroupId, out Group group);
if (group == null)
{
_logger.LogWarning("Session {SessionId} tried to join group {GroupId} that does not exist.", session.Id, request.GroupId);
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.GroupDoesNotExist, string.Empty);
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
return;
}
// Locking required as session-to-group map will be edited.
lock (_mapsLock)
{
// Group lock required to let other requests end first.
lock (group)
{
if (!group.HasAccessToPlayQueue(user))
{
_logger.LogWarning("Session {SessionId} tried to join group {GroupId} but does not have access to some content of the playing queue.", session.Id, group.GroupId.ToString());
var error = new GroupUpdate<string>(group.GroupId, GroupUpdateType.LibraryAccessDenied, string.Empty);
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
return;
}
if (IsSessionInGroup(session))
{
if (FindJoinedGroupId(session).Equals(request.GroupId))
{
// Restore session.
group.SessionJoin(session, request, cancellationToken);
return;
}
var leaveGroupRequest = new LeaveGroupRequest();
LeaveGroup(session, leaveGroupRequest, cancellationToken);
}
AddSessionToGroup(session, group);
group.SessionJoin(session, request, cancellationToken);
}
}
}
}
/// <inheritdoc />
public void LeaveGroup(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken)
{
// Locking required to access list of groups.
lock (_groupsLock)
{
// Locking required as session-to-group map will be edited.
lock (_mapsLock)
{
var group = FindJoinedGroup(session);
if (group == null)
{
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
return;
}
// Group lock required to let other requests end first.
lock (group)
{
RemoveSessionFromGroup(session, group);
group.SessionLeave(session, request, cancellationToken);
if (group.IsGroupEmpty())
{
_logger.LogInformation("Group {GroupId} is empty, removing it.", group.GroupId);
_groups.Remove(group.GroupId, out _);
}
}
}
}
}
/// <inheritdoc />
public List<GroupInfoDto> ListGroups(SessionInfo session, ListGroupsRequest request)
{
var user = _userManager.GetUserById(session.UserId);
List<GroupInfoDto> list = new List<GroupInfoDto>();
// Locking required to access list of groups.
lock (_groupsLock)
{
foreach (var group in _groups.Values)
{
// Locking required as group is not thread-safe.
lock (group)
{
if (group.HasAccessToPlayQueue(user))
{
list.Add(group.GetInfo());
}
}
}
}
return list;
}
/// <inheritdoc />
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
{
Group group;
lock (_mapsLock)
{
group = FindJoinedGroup(session);
}
if (group == null)
{
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
return;
}
// Group lock required as Group is not thread-safe.
lock (group)
{
group.HandleRequest(session, request, cancellationToken);
}
}
/// <summary>
/// Releases unmanaged and optionally managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
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;
Guid groupId = Guid.Empty;
lock (_mapsLock)
{
groupId = FindJoinedGroupId(session);
}
if (groupId.Equals(Guid.Empty))
{
return;
}
var request = new JoinGroupRequest(groupId);
JoinGroup(session, request, CancellationToken.None);
}
/// <summary>
/// Checks if a given session has joined a group.
/// </summary>
/// <remarks>
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
/// </remarks>
/// <param name="session">The session.</param>
/// <returns><c>true</c> if the session has joined a group, <c>false</c> otherwise.</returns>
private bool IsSessionInGroup(SessionInfo session)
{
return _sessionToGroupMap.ContainsKey(session.Id);
}
/// <summary>
/// Gets the group joined by the given session, if any.
/// </summary>
/// <remarks>
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
/// </remarks>
/// <param name="session">The session.</param>
/// <returns>The group.</returns>
private Group FindJoinedGroup(SessionInfo session)
{
_sessionToGroupMap.TryGetValue(session.Id, out var group);
return group;
}
/// <summary>
/// Gets the group identifier joined by the given session, if any.
/// </summary>
/// <remarks>
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
/// </remarks>
/// <param name="session">The session.</param>
/// <returns>The group identifier if the session has joined a group, an empty identifier otherwise.</returns>
private Guid FindJoinedGroupId(SessionInfo session)
{
return FindJoinedGroup(session)?.GroupId ?? Guid.Empty;
}
/// <summary>
/// Maps a session to a group.
/// </summary>
/// <remarks>
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
/// </remarks>
/// <param name="session">The session.</param>
/// <param name="group">The group.</param>
/// <exception cref="InvalidOperationException">Thrown when the user is in another group already.</exception>
private void AddSessionToGroup(SessionInfo session, Group group)
{
if (session == null)
{
throw new InvalidOperationException("Session is null!");
}
if (IsSessionInGroup(session))
{
throw new InvalidOperationException("Session in other group already!");
}
_sessionToGroupMap[session.Id] = group ?? throw new InvalidOperationException("Group is null!");
}
/// <summary>
/// Unmaps a session from a group.
/// </summary>
/// <remarks>
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
/// </remarks>
/// <param name="session">The session.</param>
/// <param name="group">The group.</param>
/// <exception cref="InvalidOperationException">Thrown when the user is not found in the specified group.</exception>
private void RemoveSessionFromGroup(SessionInfo session, Group group)
{
if (session == null)
{
throw new InvalidOperationException("Session is null!");
}
if (group == null)
{
throw new InvalidOperationException("Group is null!");
}
if (!IsSessionInGroup(session))
{
throw new InvalidOperationException("Session not in any group!");
}
_sessionToGroupMap.Remove(session.Id, out var tempGroup);
if (!tempGroup.GroupId.Equals(group.GroupId))
{
throw new InvalidOperationException("Session was in wrong group!");
}
}
}
}