| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Concurrent; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using MediaBrowser.Controller.Library; |
| | 8 | | using MediaBrowser.Controller.Session; |
| | 9 | | using MediaBrowser.Controller.SyncPlay; |
| | 10 | | using MediaBrowser.Controller.SyncPlay.Requests; |
| | 11 | | using MediaBrowser.Model.SyncPlay; |
| | 12 | | using Microsoft.Extensions.Logging; |
| | 13 | |
|
| | 14 | | namespace Emby.Server.Implementations.SyncPlay |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Class SyncPlayManager. |
| | 18 | | /// </summary> |
| | 19 | | public class SyncPlayManager : ISyncPlayManager, IDisposable |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// The logger. |
| | 23 | | /// </summary> |
| | 24 | | private readonly ILogger<SyncPlayManager> _logger; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// The logger factory. |
| | 28 | | /// </summary> |
| | 29 | | private readonly ILoggerFactory _loggerFactory; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// The user manager. |
| | 33 | | /// </summary> |
| | 34 | | private readonly IUserManager _userManager; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// The session manager. |
| | 38 | | /// </summary> |
| | 39 | | private readonly ISessionManager _sessionManager; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// The library manager. |
| | 43 | | /// </summary> |
| | 44 | | private readonly ILibraryManager _libraryManager; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// The map between users and counter of active sessions. |
| | 48 | | /// </summary> |
| 16 | 49 | | private readonly ConcurrentDictionary<Guid, int> _activeUsers = |
| 16 | 50 | | new ConcurrentDictionary<Guid, int>(); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// The map between sessions and groups. |
| | 54 | | /// </summary> |
| 16 | 55 | | private readonly ConcurrentDictionary<string, Group> _sessionToGroupMap = |
| 16 | 56 | | new ConcurrentDictionary<string, Group>(StringComparer.OrdinalIgnoreCase); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// The groups. |
| | 60 | | /// </summary> |
| 16 | 61 | | private readonly ConcurrentDictionary<Guid, Group> _groups = |
| 16 | 62 | | new ConcurrentDictionary<Guid, Group>(); |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Lock used for accessing multiple groups at once. |
| | 66 | | /// </summary> |
| | 67 | | /// <remarks> |
| | 68 | | /// This lock has priority on locks made on <see cref="Group"/>. |
| | 69 | | /// </remarks> |
| 16 | 70 | | private readonly Lock _groupsLock = new(); |
| | 71 | |
|
| | 72 | | private bool _disposed = false; |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Initializes a new instance of the <see cref="SyncPlayManager" /> class. |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="loggerFactory">The logger factory.</param> |
| | 78 | | /// <param name="userManager">The user manager.</param> |
| | 79 | | /// <param name="sessionManager">The session manager.</param> |
| | 80 | | /// <param name="libraryManager">The library manager.</param> |
| | 81 | | public SyncPlayManager( |
| | 82 | | ILoggerFactory loggerFactory, |
| | 83 | | IUserManager userManager, |
| | 84 | | ISessionManager sessionManager, |
| | 85 | | ILibraryManager libraryManager) |
| | 86 | | { |
| 16 | 87 | | _loggerFactory = loggerFactory; |
| 16 | 88 | | _userManager = userManager; |
| 16 | 89 | | _sessionManager = sessionManager; |
| 16 | 90 | | _libraryManager = libraryManager; |
| 16 | 91 | | _logger = loggerFactory.CreateLogger<SyncPlayManager>(); |
| 16 | 92 | | _sessionManager.SessionEnded += OnSessionEnded; |
| 16 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <inheritdoc /> |
| | 96 | | public void Dispose() |
| | 97 | | { |
| 16 | 98 | | Dispose(true); |
| 16 | 99 | | GC.SuppressFinalize(this); |
| 16 | 100 | | } |
| | 101 | |
|
| | 102 | | /// <inheritdoc /> |
| | 103 | | public void NewGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken) |
| | 104 | | { |
| 0 | 105 | | if (session is null) |
| | 106 | | { |
| 0 | 107 | | throw new InvalidOperationException("Session is null!"); |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | if (request is null) |
| | 111 | | { |
| 0 | 112 | | throw new InvalidOperationException("Request is null!"); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | // Locking required to access list of groups. |
| | 116 | | lock (_groupsLock) |
| | 117 | | { |
| | 118 | | // Make sure that session has not joined another group. |
| 0 | 119 | | if (_sessionToGroupMap.ContainsKey(session.Id)) |
| | 120 | | { |
| 0 | 121 | | var leaveGroupRequest = new LeaveGroupRequest(); |
| 0 | 122 | | LeaveGroup(session, leaveGroupRequest, cancellationToken); |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | var group = new Group(_loggerFactory, _userManager, _sessionManager, _libraryManager); |
| 0 | 126 | | _groups[group.GroupId] = group; |
| | 127 | |
|
| 0 | 128 | | if (!_sessionToGroupMap.TryAdd(session.Id, group)) |
| | 129 | | { |
| 0 | 130 | | throw new InvalidOperationException("Could not add session to group!"); |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | UpdateSessionsCounter(session.UserId, 1); |
| 0 | 134 | | group.CreateGroup(session, request, cancellationToken); |
| 0 | 135 | | } |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | /// <inheritdoc /> |
| | 139 | | public void JoinGroup(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken) |
| | 140 | | { |
| 0 | 141 | | if (session is null) |
| | 142 | | { |
| 0 | 143 | | throw new InvalidOperationException("Session is null!"); |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | if (request is null) |
| | 147 | | { |
| 0 | 148 | | throw new InvalidOperationException("Request is null!"); |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | var user = _userManager.GetUserById(session.UserId); |
| | 152 | |
|
| | 153 | | // Locking required to access list of groups. |
| | 154 | | lock (_groupsLock) |
| | 155 | | { |
| 0 | 156 | | _groups.TryGetValue(request.GroupId, out Group group); |
| | 157 | |
|
| 0 | 158 | | if (group is null) |
| | 159 | | { |
| 0 | 160 | | _logger.LogWarning("Session {SessionId} tried to join group {GroupId} that does not exist.", session |
| | 161 | |
|
| 0 | 162 | | var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.GroupDoesNotExist, string.Empty); |
| 0 | 163 | | _sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None); |
| 0 | 164 | | return; |
| | 165 | | } |
| | 166 | |
|
| | 167 | | // Group lock required to let other requests end first. |
| 0 | 168 | | lock (group) |
| | 169 | | { |
| 0 | 170 | | if (!group.HasAccessToPlayQueue(user)) |
| | 171 | | { |
| 0 | 172 | | _logger.LogWarning("Session {SessionId} tried to join group {GroupId} but does not have access t |
| | 173 | |
|
| 0 | 174 | | var error = new GroupUpdate<string>(group.GroupId, GroupUpdateType.LibraryAccessDenied, string.E |
| 0 | 175 | | _sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None); |
| 0 | 176 | | return; |
| | 177 | | } |
| | 178 | |
|
| 0 | 179 | | if (_sessionToGroupMap.TryGetValue(session.Id, out var existingGroup)) |
| | 180 | | { |
| 0 | 181 | | if (existingGroup.GroupId.Equals(request.GroupId)) |
| | 182 | | { |
| | 183 | | // Restore session. |
| 0 | 184 | | UpdateSessionsCounter(session.UserId, 1); |
| 0 | 185 | | group.SessionJoin(session, request, cancellationToken); |
| 0 | 186 | | return; |
| | 187 | | } |
| | 188 | |
|
| 0 | 189 | | var leaveGroupRequest = new LeaveGroupRequest(); |
| 0 | 190 | | LeaveGroup(session, leaveGroupRequest, cancellationToken); |
| | 191 | | } |
| | 192 | |
|
| 0 | 193 | | if (!_sessionToGroupMap.TryAdd(session.Id, group)) |
| | 194 | | { |
| 0 | 195 | | throw new InvalidOperationException("Could not add session to group!"); |
| | 196 | | } |
| | 197 | |
|
| 0 | 198 | | UpdateSessionsCounter(session.UserId, 1); |
| 0 | 199 | | group.SessionJoin(session, request, cancellationToken); |
| 0 | 200 | | } |
| | 201 | | } |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | /// <inheritdoc /> |
| | 205 | | public void LeaveGroup(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken) |
| | 206 | | { |
| 0 | 207 | | if (session is null) |
| | 208 | | { |
| 0 | 209 | | throw new InvalidOperationException("Session is null!"); |
| | 210 | | } |
| | 211 | |
|
| 0 | 212 | | if (request is null) |
| | 213 | | { |
| 0 | 214 | | throw new InvalidOperationException("Request is null!"); |
| | 215 | | } |
| | 216 | |
|
| | 217 | | // Locking required to access list of groups. |
| | 218 | | lock (_groupsLock) |
| | 219 | | { |
| 0 | 220 | | if (_sessionToGroupMap.TryGetValue(session.Id, out var group)) |
| | 221 | | { |
| | 222 | | // Group lock required to let other requests end first. |
| 0 | 223 | | lock (group) |
| | 224 | | { |
| 0 | 225 | | if (_sessionToGroupMap.TryRemove(session.Id, out var tempGroup)) |
| | 226 | | { |
| 0 | 227 | | if (!tempGroup.GroupId.Equals(group.GroupId)) |
| | 228 | | { |
| 0 | 229 | | throw new InvalidOperationException("Session was in wrong group!"); |
| | 230 | | } |
| | 231 | | } |
| | 232 | | else |
| | 233 | | { |
| 0 | 234 | | throw new InvalidOperationException("Could not remove session from group!"); |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | UpdateSessionsCounter(session.UserId, -1); |
| 0 | 238 | | group.SessionLeave(session, request, cancellationToken); |
| | 239 | |
|
| 0 | 240 | | if (group.IsGroupEmpty()) |
| | 241 | | { |
| 0 | 242 | | _logger.LogInformation("Group {GroupId} is empty, removing it.", group.GroupId); |
| 0 | 243 | | _groups.Remove(group.GroupId, out _); |
| | 244 | | } |
| 0 | 245 | | } |
| | 246 | | } |
| | 247 | | else |
| | 248 | | { |
| 0 | 249 | | _logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id); |
| | 250 | |
|
| 0 | 251 | | var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty); |
| 0 | 252 | | _sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None); |
| | 253 | | } |
| 0 | 254 | | } |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | /// <inheritdoc /> |
| | 258 | | public List<GroupInfoDto> ListGroups(SessionInfo session, ListGroupsRequest request) |
| | 259 | | { |
| 0 | 260 | | if (session is null) |
| | 261 | | { |
| 0 | 262 | | throw new InvalidOperationException("Session is null!"); |
| | 263 | | } |
| | 264 | |
|
| 0 | 265 | | if (request is null) |
| | 266 | | { |
| 0 | 267 | | throw new InvalidOperationException("Request is null!"); |
| | 268 | | } |
| | 269 | |
|
| 0 | 270 | | var user = _userManager.GetUserById(session.UserId); |
| 0 | 271 | | List<GroupInfoDto> list = new List<GroupInfoDto>(); |
| | 272 | |
|
| | 273 | | lock (_groupsLock) |
| | 274 | | { |
| 0 | 275 | | foreach (var (_, group) in _groups) |
| | 276 | | { |
| | 277 | | // Locking required as group is not thread-safe. |
| 0 | 278 | | lock (group) |
| | 279 | | { |
| 0 | 280 | | if (group.HasAccessToPlayQueue(user)) |
| | 281 | | { |
| 0 | 282 | | list.Add(group.GetInfo()); |
| | 283 | | } |
| 0 | 284 | | } |
| | 285 | | } |
| | 286 | | } |
| | 287 | |
|
| 0 | 288 | | return list; |
| | 289 | | } |
| | 290 | |
|
| | 291 | | /// <inheritdoc /> |
| | 292 | | public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToke |
| | 293 | | { |
| 0 | 294 | | if (session is null) |
| | 295 | | { |
| 0 | 296 | | throw new InvalidOperationException("Session is null!"); |
| | 297 | | } |
| | 298 | |
|
| 0 | 299 | | if (request is null) |
| | 300 | | { |
| 0 | 301 | | throw new InvalidOperationException("Request is null!"); |
| | 302 | | } |
| | 303 | |
|
| 0 | 304 | | if (_sessionToGroupMap.TryGetValue(session.Id, out var group)) |
| | 305 | | { |
| | 306 | | // Group lock required as Group is not thread-safe. |
| 0 | 307 | | lock (group) |
| | 308 | | { |
| | 309 | | // Make sure that session still belongs to this group. |
| 0 | 310 | | if (_sessionToGroupMap.TryGetValue(session.Id, out var checkGroup) && !checkGroup.GroupId.Equals(gro |
| | 311 | | { |
| | 312 | | // Drop request. |
| 0 | 313 | | return; |
| | 314 | | } |
| | 315 | |
|
| | 316 | | // Drop request if group is empty. |
| 0 | 317 | | if (group.IsGroupEmpty()) |
| | 318 | | { |
| 0 | 319 | | return; |
| | 320 | | } |
| | 321 | |
|
| | 322 | | // Apply requested changes to group. |
| 0 | 323 | | group.HandleRequest(session, request, cancellationToken); |
| 0 | 324 | | } |
| | 325 | | } |
| | 326 | | else |
| | 327 | | { |
| 0 | 328 | | _logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id); |
| | 329 | |
|
| 0 | 330 | | var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty); |
| 0 | 331 | | _sessionManager.SendSyncPlayGroupUpdate(session.Id, error, CancellationToken.None); |
| | 332 | | } |
| 0 | 333 | | } |
| | 334 | |
|
| | 335 | | /// <inheritdoc /> |
| | 336 | | public bool IsUserActive(Guid userId) |
| | 337 | | { |
| 0 | 338 | | if (_activeUsers.TryGetValue(userId, out var sessionsCounter)) |
| | 339 | | { |
| 0 | 340 | | return sessionsCounter > 0; |
| | 341 | | } |
| | 342 | |
|
| 0 | 343 | | return false; |
| | 344 | | } |
| | 345 | |
|
| | 346 | | /// <summary> |
| | 347 | | /// Releases unmanaged and optionally managed resources. |
| | 348 | | /// </summary> |
| | 349 | | /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release |
| | 350 | | protected virtual void Dispose(bool disposing) |
| | 351 | | { |
| 16 | 352 | | if (_disposed) |
| | 353 | | { |
| 0 | 354 | | return; |
| | 355 | | } |
| | 356 | |
|
| 16 | 357 | | _sessionManager.SessionEnded -= OnSessionEnded; |
| 16 | 358 | | _disposed = true; |
| 16 | 359 | | } |
| | 360 | |
|
| | 361 | | private void OnSessionEnded(object sender, SessionEventArgs e) |
| | 362 | | { |
| 0 | 363 | | var session = e.SessionInfo; |
| | 364 | |
|
| 0 | 365 | | if (_sessionToGroupMap.TryGetValue(session.Id, out _)) |
| | 366 | | { |
| 0 | 367 | | var leaveGroupRequest = new LeaveGroupRequest(); |
| 0 | 368 | | LeaveGroup(session, leaveGroupRequest, CancellationToken.None); |
| | 369 | | } |
| 0 | 370 | | } |
| | 371 | |
|
| | 372 | | private void UpdateSessionsCounter(Guid userId, int toAdd) |
| | 373 | | { |
| | 374 | | // Update sessions counter. |
| 0 | 375 | | var newSessionsCounter = _activeUsers.AddOrUpdate( |
| 0 | 376 | | userId, |
| 0 | 377 | | 1, |
| 0 | 378 | | (_, sessionsCounter) => sessionsCounter + toAdd); |
| | 379 | |
|
| | 380 | | // Should never happen. |
| 0 | 381 | | if (newSessionsCounter < 0) |
| | 382 | | { |
| 0 | 383 | | throw new InvalidOperationException("Sessions counter is negative!"); |
| | 384 | | } |
| | 385 | |
|
| | 386 | | // Clean record if user has no more active sessions. |
| 0 | 387 | | if (newSessionsCounter == 0) |
| | 388 | | { |
| 0 | 389 | | _activeUsers.TryRemove(new KeyValuePair<Guid, int>(userId, newSessionsCounter)); |
| | 390 | | } |
| 0 | 391 | | } |
| | 392 | | } |
| | 393 | | } |