| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Database.Implementations.Entities; |
| | | 9 | | using Jellyfin.Extensions; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using MediaBrowser.Controller.Session; |
| | | 12 | | using MediaBrowser.Controller.SyncPlay; |
| | | 13 | | using MediaBrowser.Controller.SyncPlay.GroupStates; |
| | | 14 | | using MediaBrowser.Controller.SyncPlay.Queue; |
| | | 15 | | using MediaBrowser.Controller.SyncPlay.Requests; |
| | | 16 | | using MediaBrowser.Model.SyncPlay; |
| | | 17 | | using Microsoft.Extensions.Logging; |
| | | 18 | | |
| | | 19 | | namespace Emby.Server.Implementations.SyncPlay |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Class Group. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// Class is not thread-safe, external locking is required when accessing methods. |
| | | 26 | | /// </remarks> |
| | | 27 | | public class Group : IGroupStateContext |
| | | 28 | | { |
| | | 29 | | /// <summary> |
| | | 30 | | /// The logger. |
| | | 31 | | /// </summary> |
| | | 32 | | private readonly ILogger<Group> _logger; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The logger factory. |
| | | 36 | | /// </summary> |
| | | 37 | | private readonly ILoggerFactory _loggerFactory; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The user manager. |
| | | 41 | | /// </summary> |
| | | 42 | | private readonly IUserManager _userManager; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The session manager. |
| | | 46 | | /// </summary> |
| | | 47 | | private readonly ISessionManager _sessionManager; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The library manager. |
| | | 51 | | /// </summary> |
| | | 52 | | private readonly ILibraryManager _libraryManager; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// The participants, or members of the group. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | private readonly Dictionary<string, GroupMember> _participants = |
| | 0 | 58 | | new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// The internal group state. |
| | | 62 | | /// </summary> |
| | | 63 | | private IGroupState _state; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Initializes a new instance of the <see cref="Group" /> class. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="loggerFactory">The logger factory.</param> |
| | | 69 | | /// <param name="userManager">The user manager.</param> |
| | | 70 | | /// <param name="sessionManager">The session manager.</param> |
| | | 71 | | /// <param name="libraryManager">The library manager.</param> |
| | | 72 | | public Group( |
| | | 73 | | ILoggerFactory loggerFactory, |
| | | 74 | | IUserManager userManager, |
| | | 75 | | ISessionManager sessionManager, |
| | | 76 | | ILibraryManager libraryManager) |
| | | 77 | | { |
| | 0 | 78 | | _loggerFactory = loggerFactory; |
| | 0 | 79 | | _userManager = userManager; |
| | 0 | 80 | | _sessionManager = sessionManager; |
| | 0 | 81 | | _libraryManager = libraryManager; |
| | 0 | 82 | | _logger = loggerFactory.CreateLogger<Group>(); |
| | | 83 | | |
| | 0 | 84 | | _state = new IdleGroupState(loggerFactory); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the default ping value used for sessions. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <value>The default ping.</value> |
| | 0 | 91 | | public long DefaultPing { get; } = 500; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the maximum time offset error accepted for dates reported by clients, in milliseconds. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <value>The maximum time offset error.</value> |
| | 0 | 97 | | public long TimeSyncOffset { get; } = 2000; |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Gets the maximum offset error accepted for position reported by clients, in milliseconds. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <value>The maximum offset error.</value> |
| | 0 | 103 | | public long MaxPlaybackOffset { get; } = 500; |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the group identifier. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <value>The group identifier.</value> |
| | | 109 | | public Guid GroupId { get; } = Guid.NewGuid(); |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets the group name. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <value>The group name.</value> |
| | | 115 | | public string GroupName { get; private set; } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Gets the group identifier. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <value>The group identifier.</value> |
| | | 121 | | public PlayQueueManager PlayQueue { get; } = new PlayQueueManager(); |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets the runtime ticks of current playing item. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <value>The runtime ticks of current playing item.</value> |
| | | 127 | | public long RunTimeTicks { get; private set; } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets or sets the position ticks. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <value>The position ticks.</value> |
| | | 133 | | public long PositionTicks { get; set; } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Gets or sets the last activity. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <value>The last activity.</value> |
| | | 139 | | public DateTime LastActivity { get; set; } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Adds the session to the group. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="session">The session.</param> |
| | | 145 | | private void AddSession(SessionInfo session) |
| | | 146 | | { |
| | 0 | 147 | | _participants.TryAdd( |
| | 0 | 148 | | session.Id, |
| | 0 | 149 | | new GroupMember(session) |
| | 0 | 150 | | { |
| | 0 | 151 | | Ping = DefaultPing, |
| | 0 | 152 | | IsBuffering = false |
| | 0 | 153 | | }); |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Removes the session from the group. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="session">The session.</param> |
| | | 160 | | private void RemoveSession(SessionInfo session) |
| | | 161 | | { |
| | 0 | 162 | | _participants.Remove(session.Id); |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Filters sessions of this group. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <param name="fromId">The current session identifier.</param> |
| | | 169 | | /// <param name="type">The filtering type.</param> |
| | | 170 | | /// <returns>The list of sessions matching the filter.</returns> |
| | | 171 | | private IEnumerable<string> FilterSessions(string fromId, SyncPlayBroadcastType type) |
| | | 172 | | { |
| | 0 | 173 | | return type switch |
| | 0 | 174 | | { |
| | 0 | 175 | | SyncPlayBroadcastType.CurrentSession => new string[] { fromId }, |
| | 0 | 176 | | SyncPlayBroadcastType.AllGroup => _participants |
| | 0 | 177 | | .Values |
| | 0 | 178 | | .Select(member => member.SessionId), |
| | 0 | 179 | | SyncPlayBroadcastType.AllExceptCurrentSession => _participants |
| | 0 | 180 | | .Values |
| | 0 | 181 | | .Select(member => member.SessionId) |
| | 0 | 182 | | .Where(sessionId => !sessionId.Equals(fromId, StringComparison.OrdinalIgnoreCase)), |
| | 0 | 183 | | SyncPlayBroadcastType.AllReady => _participants |
| | 0 | 184 | | .Values |
| | 0 | 185 | | .Where(member => !member.IsBuffering) |
| | 0 | 186 | | .Select(member => member.SessionId), |
| | 0 | 187 | | _ => Enumerable.Empty<string>() |
| | 0 | 188 | | }; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Checks if a given user can access all items of a given queue, that is, |
| | | 193 | | /// the user has the required minimum parental access and has access to all required folders. |
| | | 194 | | /// </summary> |
| | | 195 | | /// <param name="user">The user.</param> |
| | | 196 | | /// <param name="queue">The queue.</param> |
| | | 197 | | /// <returns><c>true</c> if the user can access all the items in the queue, <c>false</c> otherwise.</returns> |
| | | 198 | | private bool HasAccessToQueue(User user, IReadOnlyList<Guid> queue) |
| | | 199 | | { |
| | | 200 | | // Check if queue is empty. |
| | 0 | 201 | | if (queue is null || queue.Count == 0) |
| | | 202 | | { |
| | 0 | 203 | | return true; |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | foreach (var itemId in queue) |
| | | 207 | | { |
| | 0 | 208 | | var item = _libraryManager.GetItemById(itemId); |
| | 0 | 209 | | if (!item.IsVisibleStandalone(user)) |
| | | 210 | | { |
| | 0 | 211 | | return false; |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | return true; |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | private bool AllUsersHaveAccessToQueue(IReadOnlyList<Guid> queue) |
| | | 219 | | { |
| | | 220 | | // Check if queue is empty. |
| | 0 | 221 | | if (queue is null || queue.Count == 0) |
| | | 222 | | { |
| | 0 | 223 | | return true; |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | // Get list of users. |
| | 0 | 227 | | var users = _participants |
| | 0 | 228 | | .Values |
| | 0 | 229 | | .Select(participant => _userManager.GetUserById(participant.UserId)); |
| | | 230 | | |
| | | 231 | | // Find problematic users. |
| | 0 | 232 | | var usersWithNoAccess = users.Where(user => !HasAccessToQueue(user, queue)); |
| | | 233 | | |
| | | 234 | | // All users must be able to access the queue. |
| | 0 | 235 | | return !usersWithNoAccess.Any(); |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Checks if the group is empty. |
| | | 240 | | /// </summary> |
| | | 241 | | /// <returns><c>true</c> if the group is empty, <c>false</c> otherwise.</returns> |
| | 0 | 242 | | public bool IsGroupEmpty() => _participants.Count == 0; |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Initializes the group with the session's info. |
| | | 246 | | /// </summary> |
| | | 247 | | /// <param name="session">The session.</param> |
| | | 248 | | /// <param name="request">The request.</param> |
| | | 249 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 250 | | public void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken) |
| | | 251 | | { |
| | 0 | 252 | | GroupName = request.GroupName; |
| | 0 | 253 | | AddSession(session); |
| | | 254 | | |
| | 0 | 255 | | var sessionIsPlayingAnItem = session.FullNowPlayingItem is not null; |
| | | 256 | | |
| | 0 | 257 | | RestartCurrentItem(); |
| | | 258 | | |
| | 0 | 259 | | if (sessionIsPlayingAnItem) |
| | | 260 | | { |
| | 0 | 261 | | var playlist = session.NowPlayingQueue.Select(item => item.Id).ToList(); |
| | 0 | 262 | | PlayQueue.Reset(); |
| | 0 | 263 | | PlayQueue.SetPlaylist(playlist); |
| | 0 | 264 | | PlayQueue.SetPlayingItemById(session.FullNowPlayingItem.Id); |
| | 0 | 265 | | RunTimeTicks = session.FullNowPlayingItem.RunTimeTicks ?? 0; |
| | 0 | 266 | | PositionTicks = session.PlayState.PositionTicks ?? 0; |
| | | 267 | | |
| | | 268 | | // Maintain playstate. |
| | 0 | 269 | | var waitingState = new WaitingGroupState(_loggerFactory) |
| | 0 | 270 | | { |
| | 0 | 271 | | ResumePlaying = !session.PlayState.IsPaused |
| | 0 | 272 | | }; |
| | 0 | 273 | | SetState(waitingState); |
| | | 274 | | } |
| | | 275 | | |
| | 0 | 276 | | var updateSession = new SyncPlayGroupJoinedUpdate(GroupId, GetInfo()); |
| | 0 | 277 | | SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken); |
| | | 278 | | |
| | 0 | 279 | | _state.SessionJoined(this, _state.Type, session, cancellationToken); |
| | | 280 | | |
| | 0 | 281 | | _logger.LogInformation("Session {SessionId} created group {GroupId}.", session.Id, GroupId.ToString()); |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | /// <summary> |
| | | 285 | | /// Adds the session to the group. |
| | | 286 | | /// </summary> |
| | | 287 | | /// <param name="session">The session.</param> |
| | | 288 | | /// <param name="request">The request.</param> |
| | | 289 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 290 | | public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken) |
| | | 291 | | { |
| | 0 | 292 | | AddSession(session); |
| | | 293 | | |
| | 0 | 294 | | var updateSession = new SyncPlayGroupJoinedUpdate(GroupId, GetInfo()); |
| | 0 | 295 | | SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken); |
| | | 296 | | |
| | 0 | 297 | | var updateOthers = new SyncPlayUserJoinedUpdate(GroupId, session.UserName); |
| | 0 | 298 | | SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken); |
| | | 299 | | |
| | 0 | 300 | | _state.SessionJoined(this, _state.Type, session, cancellationToken); |
| | | 301 | | |
| | 0 | 302 | | _logger.LogInformation("Session {SessionId} joined group {GroupId}.", session.Id, GroupId.ToString()); |
| | 0 | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Removes the session from the group. |
| | | 307 | | /// </summary> |
| | | 308 | | /// <param name="session">The session.</param> |
| | | 309 | | /// <param name="request">The request.</param> |
| | | 310 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 311 | | public void SessionLeave(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken) |
| | | 312 | | { |
| | 0 | 313 | | _state.SessionLeaving(this, _state.Type, session, cancellationToken); |
| | | 314 | | |
| | 0 | 315 | | RemoveSession(session); |
| | | 316 | | |
| | 0 | 317 | | var updateSession = new SyncPlayGroupLeftUpdate(GroupId, GroupId.ToString()); |
| | 0 | 318 | | SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken); |
| | | 319 | | |
| | 0 | 320 | | var updateOthers = new SyncPlayUserLeftUpdate(GroupId, session.UserName); |
| | 0 | 321 | | SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken); |
| | | 322 | | |
| | 0 | 323 | | _logger.LogInformation("Session {SessionId} left group {GroupId}.", session.Id, GroupId.ToString()); |
| | 0 | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Handles the requested action by the session. |
| | | 328 | | /// </summary> |
| | | 329 | | /// <param name="session">The session.</param> |
| | | 330 | | /// <param name="request">The requested action.</param> |
| | | 331 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 332 | | public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToke |
| | | 333 | | { |
| | | 334 | | // The server's job is to maintain a consistent state for clients to reference |
| | | 335 | | // and notify clients of state changes. The actual syncing of media playback |
| | | 336 | | // happens client side. Clients are aware of the server's time and use it to sync. |
| | 0 | 337 | | _logger.LogInformation("Session {SessionId} requested {RequestType} in group {GroupId} that is {StateType}." |
| | | 338 | | |
| | | 339 | | // Apply requested changes to this group given its current state. |
| | | 340 | | // Every request has a slightly different outcome depending on the group's state. |
| | | 341 | | // There are currently four different group states that accomplish different goals: |
| | | 342 | | // - Idle: in this state no media is playing and clients should be idle (playback is stopped). |
| | | 343 | | // - Waiting: in this state the group is waiting for all the clients to be ready to start the playback, |
| | | 344 | | // that is, they've either finished loading the media for the first time or they've finished buffering. |
| | | 345 | | // Once all clients report to be ready the group's state can change to Playing or Paused. |
| | | 346 | | // - Playing: clients have some media loaded and playback is unpaused. |
| | | 347 | | // - Paused: clients have some media loaded but playback is currently paused. |
| | 0 | 348 | | request.Apply(this, _state, session, cancellationToken); |
| | 0 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Gets the info about the group for the clients. |
| | | 353 | | /// </summary> |
| | | 354 | | /// <returns>The group info for the clients.</returns> |
| | | 355 | | public GroupInfoDto GetInfo() |
| | | 356 | | { |
| | 0 | 357 | | var participants = _participants.Values.Select(session => session.UserName).Distinct().ToList(); |
| | 0 | 358 | | return new GroupInfoDto(GroupId, GroupName, _state.Type, participants, DateTime.UtcNow); |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Checks if a user has access to all content in the play queue. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <param name="user">The user.</param> |
| | | 365 | | /// <returns><c>true</c> if the user can access the play queue; <c>false</c> otherwise.</returns> |
| | | 366 | | public bool HasAccessToPlayQueue(User user) |
| | | 367 | | { |
| | 0 | 368 | | var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToList(); |
| | 0 | 369 | | return HasAccessToQueue(user, items); |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <inheritdoc /> |
| | | 373 | | public void SetIgnoreGroupWait(SessionInfo session, bool ignoreGroupWait) |
| | | 374 | | { |
| | 0 | 375 | | if (_participants.TryGetValue(session.Id, out GroupMember value)) |
| | | 376 | | { |
| | 0 | 377 | | value.IgnoreGroupWait = ignoreGroupWait; |
| | | 378 | | } |
| | 0 | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <inheritdoc /> |
| | | 382 | | public void SetState(IGroupState state) |
| | | 383 | | { |
| | 0 | 384 | | _logger.LogInformation("Group {GroupId} switching from {FromStateType} to {ToStateType}.", GroupId.ToString( |
| | 0 | 385 | | this._state = state; |
| | 0 | 386 | | } |
| | | 387 | | |
| | | 388 | | /// <inheritdoc /> |
| | | 389 | | public Task SendGroupUpdate<T>(SessionInfo from, SyncPlayBroadcastType type, GroupUpdate<T> message, Cancellatio |
| | | 390 | | { |
| | | 391 | | IEnumerable<Task> GetTasks() |
| | | 392 | | { |
| | | 393 | | foreach (var sessionId in FilterSessions(from.Id, type)) |
| | | 394 | | { |
| | | 395 | | yield return _sessionManager.SendSyncPlayGroupUpdate(sessionId, message, cancellationToken); |
| | | 396 | | } |
| | | 397 | | } |
| | | 398 | | |
| | 0 | 399 | | return Task.WhenAll(GetTasks()); |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | /// <inheritdoc /> |
| | | 403 | | public Task SendCommand(SessionInfo from, SyncPlayBroadcastType type, SendCommand message, CancellationToken can |
| | | 404 | | { |
| | | 405 | | IEnumerable<Task> GetTasks() |
| | | 406 | | { |
| | | 407 | | foreach (var sessionId in FilterSessions(from.Id, type)) |
| | | 408 | | { |
| | | 409 | | yield return _sessionManager.SendSyncPlayCommand(sessionId, message, cancellationToken); |
| | | 410 | | } |
| | | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | return Task.WhenAll(GetTasks()); |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <inheritdoc /> |
| | | 417 | | public SendCommand NewSyncPlayCommand(SendCommandType type) |
| | | 418 | | { |
| | 0 | 419 | | return new SendCommand( |
| | 0 | 420 | | GroupId, |
| | 0 | 421 | | PlayQueue.GetPlayingItemPlaylistId(), |
| | 0 | 422 | | LastActivity, |
| | 0 | 423 | | type, |
| | 0 | 424 | | PositionTicks, |
| | 0 | 425 | | DateTime.UtcNow); |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | /// <inheritdoc /> |
| | | 429 | | public long SanitizePositionTicks(long? positionTicks) |
| | | 430 | | { |
| | 0 | 431 | | var ticks = positionTicks ?? 0; |
| | 0 | 432 | | return Math.Clamp(ticks, 0, RunTimeTicks); |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | /// <inheritdoc /> |
| | | 436 | | public void UpdatePing(SessionInfo session, long ping) |
| | | 437 | | { |
| | 0 | 438 | | if (_participants.TryGetValue(session.Id, out GroupMember value)) |
| | | 439 | | { |
| | 0 | 440 | | value.Ping = ping; |
| | | 441 | | } |
| | 0 | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <inheritdoc /> |
| | | 445 | | public long GetHighestPing() |
| | | 446 | | { |
| | 0 | 447 | | long max = long.MinValue; |
| | 0 | 448 | | foreach (var session in _participants.Values) |
| | | 449 | | { |
| | 0 | 450 | | max = Math.Max(max, session.Ping); |
| | | 451 | | } |
| | | 452 | | |
| | 0 | 453 | | return max; |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | /// <inheritdoc /> |
| | | 457 | | public void SetBuffering(SessionInfo session, bool isBuffering) |
| | | 458 | | { |
| | 0 | 459 | | if (_participants.TryGetValue(session.Id, out GroupMember value)) |
| | | 460 | | { |
| | 0 | 461 | | value.IsBuffering = isBuffering; |
| | | 462 | | } |
| | 0 | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <inheritdoc /> |
| | | 466 | | public void SetAllBuffering(bool isBuffering) |
| | | 467 | | { |
| | 0 | 468 | | foreach (var session in _participants.Values) |
| | | 469 | | { |
| | 0 | 470 | | session.IsBuffering = isBuffering; |
| | | 471 | | } |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | /// <inheritdoc /> |
| | | 475 | | public bool IsBuffering() |
| | | 476 | | { |
| | 0 | 477 | | foreach (var session in _participants.Values) |
| | | 478 | | { |
| | 0 | 479 | | if (session.IsBuffering && !session.IgnoreGroupWait) |
| | | 480 | | { |
| | 0 | 481 | | return true; |
| | | 482 | | } |
| | | 483 | | } |
| | | 484 | | |
| | 0 | 485 | | return false; |
| | 0 | 486 | | } |
| | | 487 | | |
| | | 488 | | /// <inheritdoc /> |
| | | 489 | | public bool SetPlayQueue(IReadOnlyList<Guid> playQueue, int playingItemPosition, long startPositionTicks) |
| | | 490 | | { |
| | | 491 | | // Ignore on empty queue or invalid item position. |
| | 0 | 492 | | if (playQueue.Count == 0 || playingItemPosition >= playQueue.Count || playingItemPosition < 0) |
| | | 493 | | { |
| | 0 | 494 | | return false; |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | // Check if participants can access the new playing queue. |
| | 0 | 498 | | if (!AllUsersHaveAccessToQueue(playQueue)) |
| | | 499 | | { |
| | 0 | 500 | | return false; |
| | | 501 | | } |
| | | 502 | | |
| | 0 | 503 | | PlayQueue.Reset(); |
| | 0 | 504 | | PlayQueue.SetPlaylist(playQueue); |
| | 0 | 505 | | PlayQueue.SetPlayingItemByIndex(playingItemPosition); |
| | 0 | 506 | | var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId()); |
| | 0 | 507 | | RunTimeTicks = item.RunTimeTicks ?? 0; |
| | 0 | 508 | | PositionTicks = startPositionTicks; |
| | 0 | 509 | | LastActivity = DateTime.UtcNow; |
| | | 510 | | |
| | 0 | 511 | | return true; |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | /// <inheritdoc /> |
| | | 515 | | public bool SetPlayingItem(Guid playlistItemId) |
| | | 516 | | { |
| | 0 | 517 | | var itemFound = PlayQueue.SetPlayingItemByPlaylistId(playlistItemId); |
| | | 518 | | |
| | 0 | 519 | | if (itemFound) |
| | | 520 | | { |
| | 0 | 521 | | var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId()); |
| | 0 | 522 | | RunTimeTicks = item.RunTimeTicks ?? 0; |
| | | 523 | | } |
| | | 524 | | else |
| | | 525 | | { |
| | 0 | 526 | | RunTimeTicks = 0; |
| | | 527 | | } |
| | | 528 | | |
| | 0 | 529 | | RestartCurrentItem(); |
| | | 530 | | |
| | 0 | 531 | | return itemFound; |
| | | 532 | | } |
| | | 533 | | |
| | | 534 | | /// <inheritdoc /> |
| | | 535 | | public void ClearPlayQueue(bool clearPlayingItem) |
| | | 536 | | { |
| | 0 | 537 | | PlayQueue.ClearPlaylist(clearPlayingItem); |
| | 0 | 538 | | if (clearPlayingItem) |
| | | 539 | | { |
| | 0 | 540 | | RestartCurrentItem(); |
| | | 541 | | } |
| | 0 | 542 | | } |
| | | 543 | | |
| | | 544 | | /// <inheritdoc /> |
| | | 545 | | public bool RemoveFromPlayQueue(IReadOnlyList<Guid> playlistItemIds) |
| | | 546 | | { |
| | 0 | 547 | | var playingItemRemoved = PlayQueue.RemoveFromPlaylist(playlistItemIds); |
| | 0 | 548 | | if (playingItemRemoved) |
| | | 549 | | { |
| | 0 | 550 | | var itemId = PlayQueue.GetPlayingItemId(); |
| | 0 | 551 | | if (!itemId.IsEmpty()) |
| | | 552 | | { |
| | 0 | 553 | | var item = _libraryManager.GetItemById(itemId); |
| | 0 | 554 | | RunTimeTicks = item.RunTimeTicks ?? 0; |
| | | 555 | | } |
| | | 556 | | else |
| | | 557 | | { |
| | 0 | 558 | | RunTimeTicks = 0; |
| | | 559 | | } |
| | | 560 | | |
| | 0 | 561 | | RestartCurrentItem(); |
| | | 562 | | } |
| | | 563 | | |
| | 0 | 564 | | return playingItemRemoved; |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | /// <inheritdoc /> |
| | | 568 | | public bool MoveItemInPlayQueue(Guid playlistItemId, int newIndex) |
| | | 569 | | { |
| | 0 | 570 | | return PlayQueue.MovePlaylistItem(playlistItemId, newIndex); |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | /// <inheritdoc /> |
| | | 574 | | public bool AddToPlayQueue(IReadOnlyList<Guid> newItems, GroupQueueMode mode) |
| | | 575 | | { |
| | | 576 | | // Ignore on empty list. |
| | 0 | 577 | | if (newItems.Count == 0) |
| | | 578 | | { |
| | 0 | 579 | | return false; |
| | | 580 | | } |
| | | 581 | | |
| | | 582 | | // Check if participants can access the new playing queue. |
| | 0 | 583 | | if (!AllUsersHaveAccessToQueue(newItems)) |
| | | 584 | | { |
| | 0 | 585 | | return false; |
| | | 586 | | } |
| | | 587 | | |
| | 0 | 588 | | if (mode.Equals(GroupQueueMode.QueueNext)) |
| | | 589 | | { |
| | 0 | 590 | | PlayQueue.QueueNext(newItems); |
| | | 591 | | } |
| | | 592 | | else |
| | | 593 | | { |
| | 0 | 594 | | PlayQueue.Queue(newItems); |
| | | 595 | | } |
| | | 596 | | |
| | 0 | 597 | | return true; |
| | | 598 | | } |
| | | 599 | | |
| | | 600 | | /// <inheritdoc /> |
| | | 601 | | public void RestartCurrentItem() |
| | | 602 | | { |
| | 0 | 603 | | PositionTicks = 0; |
| | 0 | 604 | | LastActivity = DateTime.UtcNow; |
| | 0 | 605 | | } |
| | | 606 | | |
| | | 607 | | /// <inheritdoc /> |
| | | 608 | | public bool NextItemInQueue() |
| | | 609 | | { |
| | 0 | 610 | | var update = PlayQueue.Next(); |
| | 0 | 611 | | if (update) |
| | | 612 | | { |
| | 0 | 613 | | var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId()); |
| | 0 | 614 | | RunTimeTicks = item.RunTimeTicks ?? 0; |
| | 0 | 615 | | RestartCurrentItem(); |
| | 0 | 616 | | return true; |
| | | 617 | | } |
| | | 618 | | |
| | 0 | 619 | | return false; |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | /// <inheritdoc /> |
| | | 623 | | public bool PreviousItemInQueue() |
| | | 624 | | { |
| | 0 | 625 | | var update = PlayQueue.Previous(); |
| | 0 | 626 | | if (update) |
| | | 627 | | { |
| | 0 | 628 | | var item = _libraryManager.GetItemById(PlayQueue.GetPlayingItemId()); |
| | 0 | 629 | | RunTimeTicks = item.RunTimeTicks ?? 0; |
| | 0 | 630 | | RestartCurrentItem(); |
| | 0 | 631 | | return true; |
| | | 632 | | } |
| | | 633 | | |
| | 0 | 634 | | return false; |
| | | 635 | | } |
| | | 636 | | |
| | | 637 | | /// <inheritdoc /> |
| | | 638 | | public void SetRepeatMode(GroupRepeatMode mode) |
| | | 639 | | { |
| | 0 | 640 | | PlayQueue.SetRepeatMode(mode); |
| | 0 | 641 | | } |
| | | 642 | | |
| | | 643 | | /// <inheritdoc /> |
| | | 644 | | public void SetShuffleMode(GroupShuffleMode mode) |
| | | 645 | | { |
| | 0 | 646 | | PlayQueue.SetShuffleMode(mode); |
| | 0 | 647 | | } |
| | | 648 | | |
| | | 649 | | /// <inheritdoc /> |
| | | 650 | | public PlayQueueUpdate GetPlayQueueUpdate(PlayQueueUpdateReason reason) |
| | | 651 | | { |
| | 0 | 652 | | var startPositionTicks = PositionTicks; |
| | 0 | 653 | | var isPlaying = _state.Type.Equals(GroupStateType.Playing); |
| | | 654 | | |
| | 0 | 655 | | if (isPlaying) |
| | | 656 | | { |
| | 0 | 657 | | var currentTime = DateTime.UtcNow; |
| | 0 | 658 | | var elapsedTime = currentTime - LastActivity; |
| | | 659 | | // Elapsed time is negative if event happens |
| | | 660 | | // during the delay added to account for latency. |
| | | 661 | | // In this phase clients haven't started the playback yet. |
| | | 662 | | // In other words, LastActivity is in the future, |
| | | 663 | | // when playback unpause is supposed to happen. |
| | | 664 | | // Adjust ticks only if playback actually started. |
| | 0 | 665 | | startPositionTicks += Math.Max(elapsedTime.Ticks, 0); |
| | | 666 | | } |
| | | 667 | | |
| | 0 | 668 | | return new PlayQueueUpdate( |
| | 0 | 669 | | reason, |
| | 0 | 670 | | PlayQueue.LastChange, |
| | 0 | 671 | | PlayQueue.GetPlaylist(), |
| | 0 | 672 | | PlayQueue.PlayingItemIndex, |
| | 0 | 673 | | startPositionTicks, |
| | 0 | 674 | | isPlaying, |
| | 0 | 675 | | PlayQueue.ShuffleMode, |
| | 0 | 676 | | PlayQueue.RepeatMode); |
| | | 677 | | } |
| | | 678 | | } |
| | | 679 | | } |