jellyfin/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs

348 lines
12 KiB
C#
Raw Normal View History

2020-04-01 15:52:42 +00:00
using System;
using System.Collections.Concurrent;
2020-04-01 15:52:42 +00:00
using System.Collections.Generic;
using System.Threading;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
2020-05-06 21:42:53 +00:00
using MediaBrowser.Controller.SyncPlay;
using MediaBrowser.Controller.SyncPlay.Requests;
2020-05-06 21:42:53 +00:00
using MediaBrowser.Model.SyncPlay;
2020-05-27 00:52:05 +00:00
using Microsoft.Extensions.Logging;
2020-04-01 15:52:42 +00:00
2020-05-06 21:42:53 +00:00
namespace Emby.Server.Implementations.SyncPlay
2020-04-01 15:52:42 +00:00
{
/// <summary>
2020-05-06 21:42:53 +00:00
/// Class SyncPlayManager.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-05-06 21:42:53 +00:00
public class SyncPlayManager : ISyncPlayManager, IDisposable
2020-04-01 15:52:42 +00:00
{
/// <summary>
/// The logger.
/// </summary>
2020-06-06 00:15:56 +00:00
private readonly ILogger<SyncPlayManager> _logger;
2020-04-01 15:52:42 +00:00
/// <summary>
/// The logger factory.
/// </summary>
private readonly ILoggerFactory _loggerFactory;
/// <summary>
/// The user manager.
/// </summary>
private readonly IUserManager _userManager;
2020-04-01 15:52:42 +00:00
/// <summary>
/// The session manager.
/// </summary>
private readonly ISessionManager _sessionManager;
2020-04-04 22:50:57 +00:00
/// <summary>
/// The library manager.
/// </summary>
private readonly ILibraryManager _libraryManager;
2020-04-01 15:52:42 +00:00
/// <summary>
2020-04-04 15:56:21 +00:00
/// The map between sessions and groups.
2020-04-01 15:52:42 +00:00
/// </summary>
private readonly ConcurrentDictionary<string, Group> _sessionToGroupMap =
new ConcurrentDictionary<string, Group>(StringComparer.OrdinalIgnoreCase);
2020-04-01 15:52:42 +00:00
/// <summary>
/// The groups.
/// </summary>
private readonly ConcurrentDictionary<Guid, Group> _groups =
new ConcurrentDictionary<Guid, Group>();
2020-04-28 12:12:06 +00:00
/// <summary>
/// Lock used for accessing multiple groups at once.
2020-04-28 12:12:06 +00:00
/// </summary>
2020-11-30 09:03:42 +00:00
/// <remarks>
/// This lock has priority on locks made on <see cref="Group"/>.
2020-11-30 09:03:42 +00:00
/// </remarks>
2020-04-28 12:12:06 +00:00
private readonly object _groupsLock = new object();
2020-04-01 15:52:42 +00:00
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>
2020-05-06 21:42:53 +00:00
public SyncPlayManager(
ILoggerFactory loggerFactory,
IUserManager userManager,
2020-04-04 22:50:57 +00:00
ISessionManager sessionManager,
ILibraryManager libraryManager)
2020-04-01 15:52:42 +00:00
{
_loggerFactory = loggerFactory;
_userManager = userManager;
2020-04-01 15:52:42 +00:00
_sessionManager = sessionManager;
2020-04-04 22:50:57 +00:00
_libraryManager = libraryManager;
_logger = loggerFactory.CreateLogger<SyncPlayManager>();
_sessionManager.SessionStarted += OnSessionManagerSessionStarted;
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <inheritdoc />
public void NewGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
if (session == null)
{
throw new InvalidOperationException("Session is null!");
}
if (request == null)
{
throw new InvalidOperationException("Request is null!");
}
// Locking required to access list of groups.
2020-04-28 12:12:06 +00:00
lock (_groupsLock)
{
// Make sure that session has not joined another group.
if (_sessionToGroupMap.ContainsKey(session.Id))
2020-04-28 12:12:06 +00:00
{
var leaveGroupRequest = new LeaveGroupRequest();
LeaveGroup(session, leaveGroupRequest, cancellationToken);
}
2020-04-01 15:52:42 +00:00
var group = new Group(_loggerFactory, _userManager, _sessionManager, _libraryManager);
_groups[group.GroupId] = group;
2020-04-01 15:52:42 +00:00
if (!_sessionToGroupMap.TryAdd(session.Id, group))
{
throw new InvalidOperationException("Could not add session to group!");
}
group.CreateGroup(session, request, cancellationToken);
2020-04-28 12:12:06 +00:00
}
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
public void JoinGroup(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
if (session == null)
{
throw new InvalidOperationException("Session is null!");
}
if (request == null)
{
throw new InvalidOperationException("Request is null!");
}
var user = _userManager.GetUserById(session.UserId);
// Locking required to access list of groups.
2020-04-28 12:12:06 +00:00
lock (_groupsLock)
2020-04-01 15:52:42 +00:00
{
2020-12-03 20:01:18 +00:00
_groups.TryGetValue(request.GroupId, out Group group);
2020-04-01 15:52:42 +00:00
2020-04-28 12:12:06 +00:00
if (group == null)
2020-04-22 20:05:53 +00:00
{
_logger.LogWarning("Session {SessionId} tried to join group {GroupId} that does not exist.", session.Id, request.GroupId);
2020-04-04 22:50:57 +00:00
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.GroupDoesNotExist, string.Empty);
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
2020-04-28 12:12:06 +00:00
return;
}
2020-04-22 20:05:53 +00:00
// Group lock required to let other requests end first.
lock (group)
2020-04-28 12:12:06 +00:00
{
if (!group.HasAccessToPlayQueue(user))
2020-05-09 12:34:07 +00:00
{
_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 (_sessionToGroupMap.TryGetValue(session.Id, out var existingGroup))
{
if (existingGroup.GroupId.Equals(request.GroupId))
{
// Restore session.
group.SessionJoin(session, request, cancellationToken);
return;
}
var leaveGroupRequest = new LeaveGroupRequest();
LeaveGroup(session, leaveGroupRequest, cancellationToken);
}
if (!_sessionToGroupMap.TryAdd(session.Id, group))
{
throw new InvalidOperationException("Could not add session to group!");
2020-05-09 12:34:07 +00:00
}
group.SessionJoin(session, request, cancellationToken);
2020-04-28 12:12:06 +00:00
}
2020-04-21 21:37:37 +00:00
}
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
public void LeaveGroup(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
if (session == null)
{
throw new InvalidOperationException("Session is null!");
}
if (request == null)
{
throw new InvalidOperationException("Request is null!");
}
// Locking required to access list of groups.
2020-04-28 12:12:06 +00:00
lock (_groupsLock)
2020-04-01 15:52:42 +00:00
{
if (_sessionToGroupMap.TryGetValue(session.Id, out var group))
2020-04-22 20:05:53 +00:00
{
// Group lock required to let other requests end first.
lock (group)
{
if (_sessionToGroupMap.TryRemove(session.Id, out var tempGroup))
{
if (!tempGroup.GroupId.Equals(group.GroupId))
{
throw new InvalidOperationException("Session was in wrong group!");
}
}
else
{
throw new InvalidOperationException("Could not remove session from group!");
}
group.SessionLeave(session, request, cancellationToken);
if (group.IsGroupEmpty())
{
_logger.LogInformation("Group {GroupId} is empty, removing it.", group.GroupId);
_groups.Remove(group.GroupId, out _);
}
}
2020-04-28 12:12:06 +00:00
}
else
{
_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;
}
2020-04-01 15:52:42 +00:00
}
}
/// <inheritdoc />
public List<GroupInfoDto> ListGroups(SessionInfo session, ListGroupsRequest request)
2020-04-01 15:52:42 +00:00
{
if (session == null)
{
throw new InvalidOperationException("Session is null!");
}
if (request == null)
{
throw new InvalidOperationException("Request is null!");
}
var user = _userManager.GetUserById(session.UserId);
List<GroupInfoDto> list = new List<GroupInfoDto>();
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;
2020-04-01 15:52:42 +00:00
}
/// <inheritdoc />
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
2020-04-01 15:52:42 +00:00
{
if (session == null)
2020-11-30 09:03:42 +00:00
{
throw new InvalidOperationException("Session is null!");
2020-11-30 09:03:42 +00:00
}
if (request == null)
{
throw new InvalidOperationException("Request is null!");
}
if (_sessionToGroupMap.TryGetValue(session.Id, out var group))
{
// Group lock required as Group is not thread-safe.
lock (group)
{
// Make sure that session still belongs to this group.
if (_sessionToGroupMap.TryGetValue(session.Id, out var checkGroup) && !checkGroup.GroupId.Equals(group.GroupId))
{
// Drop request.
return;
}
// Drop request if group is empty.
if (group.IsGroupEmpty())
{
return;
}
2020-12-04 22:16:15 +00:00
// Apply requested changes to group.
group.HandleRequest(session, request, cancellationToken);
}
}
else
2020-04-01 15:52:42 +00:00
{
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
2020-04-28 12:12:06 +00:00
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
}
/// <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;
if (_sessionToGroupMap.TryGetValue(session.Id, out var group))
2020-11-30 09:03:42 +00:00
{
var request = new JoinGroupRequest(group.GroupId);
JoinGroup(session, request, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
}
}
}