| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Threading; |
| | | 5 | | using MediaBrowser.Controller.Session; |
| | | 6 | | using MediaBrowser.Controller.SyncPlay.PlaybackRequests; |
| | | 7 | | using MediaBrowser.Model.SyncPlay; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | |
| | | 10 | | namespace MediaBrowser.Controller.SyncPlay.GroupStates |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Class WaitingGroupState. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// Class is not thread-safe, external locking is required when accessing methods. |
| | | 17 | | /// </remarks> |
| | | 18 | | public class WaitingGroupState : AbstractGroupState |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// The logger. |
| | | 22 | | /// </summary> |
| | | 23 | | private readonly ILogger<WaitingGroupState> _logger; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="WaitingGroupState"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param> |
| | | 29 | | public WaitingGroupState(ILoggerFactory loggerFactory) |
| | 0 | 30 | | : base(loggerFactory) |
| | | 31 | | { |
| | 0 | 32 | | _logger = LoggerFactory.CreateLogger<WaitingGroupState>(); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public override GroupStateType Type { get; } = GroupStateType.Waiting; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets a value indicating whether playback should resume when group is ready. |
| | | 40 | | /// </summary> |
| | | 41 | | public bool ResumePlaying { get; set; } = false; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets a value indicating whether the initial state has been set. |
| | | 45 | | /// </summary> |
| | | 46 | | private bool InitialStateSet { get; set; } = false; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets or sets the group state before the first ever event. |
| | | 50 | | /// </summary> |
| | | 51 | | private GroupStateType InitialState { get; set; } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, Ca |
| | | 55 | | { |
| | | 56 | | // Save state if first event. |
| | 0 | 57 | | if (!InitialStateSet) |
| | | 58 | | { |
| | 0 | 59 | | InitialState = prevState; |
| | 0 | 60 | | InitialStateSet = true; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | if (prevState.Equals(GroupStateType.Playing)) |
| | | 64 | | { |
| | 0 | 65 | | ResumePlaying = true; |
| | | 66 | | // Pause group and compute the media playback position. |
| | 0 | 67 | | var currentTime = DateTime.UtcNow; |
| | 0 | 68 | | var elapsedTime = currentTime - context.LastActivity; |
| | 0 | 69 | | context.LastActivity = currentTime; |
| | | 70 | | // Elapsed time is negative if event happens |
| | | 71 | | // during the delay added to account for latency. |
| | | 72 | | // In this phase clients haven't started the playback yet. |
| | | 73 | | // In other words, LastActivity is in the future, |
| | | 74 | | // when playback unpause is supposed to happen. |
| | | 75 | | // Seek only if playback actually started. |
| | 0 | 76 | | context.PositionTicks += Math.Max(elapsedTime.Ticks, 0); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // Prepare new session. |
| | 0 | 80 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist); |
| | 0 | 81 | | var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 82 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, update, cancellationToken); |
| | | 83 | | |
| | 0 | 84 | | context.SetBuffering(session, true); |
| | | 85 | | |
| | | 86 | | // Send pause command to all non-buffering sessions. |
| | 0 | 87 | | var command = context.NewSyncPlayCommand(SendCommandType.Pause); |
| | 0 | 88 | | context.SendCommand(session, SyncPlayBroadcastType.AllReady, command, cancellationToken); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | | 92 | | public override void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, C |
| | | 93 | | { |
| | | 94 | | // Save state if first event. |
| | 0 | 95 | | if (!InitialStateSet) |
| | | 96 | | { |
| | 0 | 97 | | InitialState = prevState; |
| | 0 | 98 | | InitialStateSet = true; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | context.SetBuffering(session, false); |
| | | 102 | | |
| | 0 | 103 | | if (!context.IsBuffering()) |
| | | 104 | | { |
| | 0 | 105 | | if (ResumePlaying) |
| | | 106 | | { |
| | 0 | 107 | | _logger.LogDebug("Session {SessionId} left group {GroupId}, notifying others to resume.", session.Id |
| | | 108 | | |
| | | 109 | | // Client, that was buffering, left the group. |
| | 0 | 110 | | var playingState = new PlayingGroupState(LoggerFactory); |
| | 0 | 111 | | context.SetState(playingState); |
| | 0 | 112 | | var unpauseRequest = new UnpauseGroupRequest(); |
| | 0 | 113 | | playingState.HandleRequest(unpauseRequest, context, Type, session, cancellationToken); |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | 0 | 117 | | _logger.LogDebug("Session {SessionId} left group {GroupId}, returning to previous state.", session.I |
| | | 118 | | |
| | | 119 | | // Group is ready, returning to previous state. |
| | 0 | 120 | | var pausedState = new PausedGroupState(LoggerFactory); |
| | 0 | 121 | | context.SetState(pausedState); |
| | | 122 | | } |
| | | 123 | | } |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <inheritdoc /> |
| | | 127 | | public override void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevStat |
| | | 128 | | { |
| | | 129 | | // Save state if first event. |
| | 0 | 130 | | if (!InitialStateSet) |
| | | 131 | | { |
| | 0 | 132 | | InitialState = prevState; |
| | 0 | 133 | | InitialStateSet = true; |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | ResumePlaying = true; |
| | | 137 | | |
| | 0 | 138 | | var setQueueStatus = context.SetPlayQueue(request.PlayingQueue, request.PlayingItemPosition, request.StartPo |
| | 0 | 139 | | if (!setQueueStatus) |
| | | 140 | | { |
| | 0 | 141 | | _logger.LogError("Unable to set playing queue in group {GroupId}.", context.GroupId.ToString()); |
| | | 142 | | |
| | | 143 | | // Ignore request and return to previous state. |
| | 0 | 144 | | IGroupState newState = prevState switch { |
| | 0 | 145 | | GroupStateType.Playing => new PlayingGroupState(LoggerFactory), |
| | 0 | 146 | | GroupStateType.Paused => new PausedGroupState(LoggerFactory), |
| | 0 | 147 | | _ => new IdleGroupState(LoggerFactory) |
| | 0 | 148 | | }; |
| | | 149 | | |
| | 0 | 150 | | context.SetState(newState); |
| | 0 | 151 | | return; |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist); |
| | 0 | 155 | | var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 156 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken); |
| | | 157 | | |
| | | 158 | | // Reset status of sessions and await for all Ready events. |
| | 0 | 159 | | context.SetAllBuffering(true); |
| | | 160 | | |
| | 0 | 161 | | _logger.LogDebug("Session {SessionId} set a new play queue in group {GroupId}.", session.Id, context.GroupId |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <inheritdoc /> |
| | | 165 | | public override void HandleRequest(SetPlaylistItemGroupRequest request, IGroupStateContext context, GroupStateTy |
| | | 166 | | { |
| | | 167 | | // Save state if first event. |
| | 0 | 168 | | if (!InitialStateSet) |
| | | 169 | | { |
| | 0 | 170 | | InitialState = prevState; |
| | 0 | 171 | | InitialStateSet = true; |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | ResumePlaying = true; |
| | | 175 | | |
| | 0 | 176 | | var result = context.SetPlayingItem(request.PlaylistItemId); |
| | 0 | 177 | | if (result) |
| | | 178 | | { |
| | 0 | 179 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem); |
| | 0 | 180 | | var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 181 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken); |
| | | 182 | | |
| | | 183 | | // Reset status of sessions and await for all Ready events. |
| | 0 | 184 | | context.SetAllBuffering(true); |
| | | 185 | | } |
| | | 186 | | else |
| | | 187 | | { |
| | | 188 | | // Return to old state. |
| | 0 | 189 | | IGroupState newState = prevState switch |
| | 0 | 190 | | { |
| | 0 | 191 | | GroupStateType.Playing => new PlayingGroupState(LoggerFactory), |
| | 0 | 192 | | GroupStateType.Paused => new PausedGroupState(LoggerFactory), |
| | 0 | 193 | | _ => new IdleGroupState(LoggerFactory) |
| | 0 | 194 | | }; |
| | | 195 | | |
| | 0 | 196 | | context.SetState(newState); |
| | | 197 | | |
| | 0 | 198 | | _logger.LogDebug("Unable to change current playing item in group {GroupId}.", context.GroupId.ToString() |
| | | 199 | | } |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <inheritdoc /> |
| | | 203 | | public override void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevS |
| | | 204 | | { |
| | | 205 | | // Save state if first event. |
| | 0 | 206 | | if (!InitialStateSet) |
| | | 207 | | { |
| | 0 | 208 | | InitialState = prevState; |
| | 0 | 209 | | InitialStateSet = true; |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | if (prevState.Equals(GroupStateType.Idle)) |
| | | 213 | | { |
| | 0 | 214 | | ResumePlaying = true; |
| | 0 | 215 | | context.RestartCurrentItem(); |
| | | 216 | | |
| | 0 | 217 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist); |
| | 0 | 218 | | var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 219 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken); |
| | | 220 | | |
| | | 221 | | // Reset status of sessions and await for all Ready events. |
| | 0 | 222 | | context.SetAllBuffering(true); |
| | | 223 | | |
| | 0 | 224 | | _logger.LogDebug("Group {GroupId} is waiting for all ready events.", context.GroupId.ToString()); |
| | | 225 | | } |
| | | 226 | | else |
| | | 227 | | { |
| | 0 | 228 | | if (ResumePlaying) |
| | | 229 | | { |
| | 0 | 230 | | _logger.LogDebug("Forcing the playback to start in group {GroupId}. Group-wait is disabled until nex |
| | | 231 | | |
| | | 232 | | // An Unpause request is forcing the playback to start, ignoring sessions that are not ready. |
| | 0 | 233 | | context.SetAllBuffering(false); |
| | | 234 | | |
| | | 235 | | // Change state. |
| | 0 | 236 | | var playingState = new PlayingGroupState(LoggerFactory) |
| | 0 | 237 | | { |
| | 0 | 238 | | IgnoreBuffering = true |
| | 0 | 239 | | }; |
| | 0 | 240 | | context.SetState(playingState); |
| | 0 | 241 | | playingState.HandleRequest(request, context, Type, session, cancellationToken); |
| | | 242 | | } |
| | | 243 | | else |
| | | 244 | | { |
| | | 245 | | // Group would have gone to paused state, now will go to playing state when ready. |
| | 0 | 246 | | ResumePlaying = true; |
| | | 247 | | |
| | | 248 | | // Notify relevant state change event. |
| | 0 | 249 | | SendGroupStateUpdate(context, request, session, cancellationToken); |
| | | 250 | | } |
| | | 251 | | } |
| | 0 | 252 | | } |
| | | 253 | | |
| | | 254 | | /// <inheritdoc /> |
| | | 255 | | public override void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevSta |
| | | 256 | | { |
| | | 257 | | // Save state if first event. |
| | 0 | 258 | | if (!InitialStateSet) |
| | | 259 | | { |
| | 0 | 260 | | InitialState = prevState; |
| | 0 | 261 | | InitialStateSet = true; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | // Wait for sessions to be ready, then switch to paused state. |
| | 0 | 265 | | ResumePlaying = false; |
| | | 266 | | |
| | | 267 | | // Notify relevant state change event. |
| | 0 | 268 | | SendGroupStateUpdate(context, request, session, cancellationToken); |
| | 0 | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <inheritdoc /> |
| | | 272 | | public override void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevStat |
| | | 273 | | { |
| | | 274 | | // Save state if first event. |
| | 0 | 275 | | if (!InitialStateSet) |
| | | 276 | | { |
| | 0 | 277 | | InitialState = prevState; |
| | 0 | 278 | | InitialStateSet = true; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | // Change state. |
| | 0 | 282 | | var idleState = new IdleGroupState(LoggerFactory); |
| | 0 | 283 | | context.SetState(idleState); |
| | 0 | 284 | | idleState.HandleRequest(request, context, Type, session, cancellationToken); |
| | 0 | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <inheritdoc /> |
| | | 288 | | public override void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevStat |
| | | 289 | | { |
| | | 290 | | // Save state if first event. |
| | 0 | 291 | | if (!InitialStateSet) |
| | | 292 | | { |
| | 0 | 293 | | InitialState = prevState; |
| | 0 | 294 | | InitialStateSet = true; |
| | | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | if (prevState.Equals(GroupStateType.Playing)) |
| | | 298 | | { |
| | 0 | 299 | | ResumePlaying = true; |
| | | 300 | | } |
| | 0 | 301 | | else if (prevState.Equals(GroupStateType.Paused)) |
| | | 302 | | { |
| | 0 | 303 | | ResumePlaying = false; |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | // Sanitize PositionTicks. |
| | 0 | 307 | | var ticks = context.SanitizePositionTicks(request.PositionTicks); |
| | | 308 | | |
| | | 309 | | // Seek. |
| | 0 | 310 | | context.PositionTicks = ticks; |
| | 0 | 311 | | context.LastActivity = DateTime.UtcNow; |
| | | 312 | | |
| | 0 | 313 | | var command = context.NewSyncPlayCommand(SendCommandType.Seek); |
| | 0 | 314 | | context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken); |
| | | 315 | | |
| | | 316 | | // Reset status of sessions and await for all Ready events. |
| | 0 | 317 | | context.SetAllBuffering(true); |
| | | 318 | | |
| | | 319 | | // Notify relevant state change event. |
| | 0 | 320 | | SendGroupStateUpdate(context, request, session, cancellationToken); |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | /// <inheritdoc /> |
| | | 324 | | public override void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevSt |
| | | 325 | | { |
| | | 326 | | // Save state if first event. |
| | 0 | 327 | | if (!InitialStateSet) |
| | | 328 | | { |
| | 0 | 329 | | InitialState = prevState; |
| | 0 | 330 | | InitialStateSet = true; |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | // Make sure the client is playing the correct item. |
| | 0 | 334 | | if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId())) |
| | | 335 | | { |
| | 0 | 336 | | _logger.LogDebug("Session {SessionId} reported wrong playlist item in group {GroupId}.", session.Id, con |
| | | 337 | | |
| | 0 | 338 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem); |
| | 0 | 339 | | var updateSession = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 340 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken) |
| | 0 | 341 | | context.SetBuffering(session, true); |
| | | 342 | | |
| | 0 | 343 | | return; |
| | | 344 | | } |
| | | 345 | | |
| | 0 | 346 | | if (prevState.Equals(GroupStateType.Playing)) |
| | | 347 | | { |
| | | 348 | | // Resume playback when all ready. |
| | 0 | 349 | | ResumePlaying = true; |
| | | 350 | | |
| | 0 | 351 | | context.SetBuffering(session, true); |
| | | 352 | | |
| | | 353 | | // Pause group and compute the media playback position. |
| | 0 | 354 | | var currentTime = DateTime.UtcNow; |
| | 0 | 355 | | var elapsedTime = currentTime - context.LastActivity; |
| | 0 | 356 | | context.LastActivity = currentTime; |
| | | 357 | | // Elapsed time is negative if event happens |
| | | 358 | | // during the delay added to account for latency. |
| | | 359 | | // In this phase clients haven't started the playback yet. |
| | | 360 | | // In other words, LastActivity is in the future, |
| | | 361 | | // when playback unpause is supposed to happen. |
| | | 362 | | // Seek only if playback actually started. |
| | 0 | 363 | | context.PositionTicks += Math.Max(elapsedTime.Ticks, 0); |
| | | 364 | | |
| | | 365 | | // Send pause command to all non-buffering sessions. |
| | 0 | 366 | | var command = context.NewSyncPlayCommand(SendCommandType.Pause); |
| | 0 | 367 | | context.SendCommand(session, SyncPlayBroadcastType.AllReady, command, cancellationToken); |
| | | 368 | | } |
| | 0 | 369 | | else if (prevState.Equals(GroupStateType.Paused)) |
| | | 370 | | { |
| | | 371 | | // Don't resume playback when all ready. |
| | 0 | 372 | | ResumePlaying = false; |
| | | 373 | | |
| | 0 | 374 | | context.SetBuffering(session, true); |
| | | 375 | | |
| | | 376 | | // Send pause command to buffering session. |
| | 0 | 377 | | var command = context.NewSyncPlayCommand(SendCommandType.Pause); |
| | 0 | 378 | | context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken); |
| | | 379 | | } |
| | 0 | 380 | | else if (prevState.Equals(GroupStateType.Waiting)) |
| | | 381 | | { |
| | | 382 | | // Another session is now buffering. |
| | 0 | 383 | | context.SetBuffering(session, true); |
| | | 384 | | |
| | 0 | 385 | | if (!ResumePlaying) |
| | | 386 | | { |
| | | 387 | | // Force update for this session that should be paused. |
| | 0 | 388 | | var command = context.NewSyncPlayCommand(SendCommandType.Pause); |
| | 0 | 389 | | context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken); |
| | | 390 | | } |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | // Notify relevant state change event. |
| | 0 | 394 | | SendGroupStateUpdate(context, request, session, cancellationToken); |
| | 0 | 395 | | } |
| | | 396 | | |
| | | 397 | | /// <inheritdoc /> |
| | | 398 | | public override void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevSta |
| | | 399 | | { |
| | | 400 | | // Save state if first event. |
| | 0 | 401 | | if (!InitialStateSet) |
| | | 402 | | { |
| | 0 | 403 | | InitialState = prevState; |
| | 0 | 404 | | InitialStateSet = true; |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | // Make sure the client is playing the correct item. |
| | 0 | 408 | | if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId())) |
| | | 409 | | { |
| | 0 | 410 | | _logger.LogDebug("Session {SessionId} reported wrong playlist item in group {GroupId}.", session.Id, con |
| | | 411 | | |
| | 0 | 412 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem); |
| | 0 | 413 | | var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 414 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, update, cancellationToken); |
| | 0 | 415 | | context.SetBuffering(session, true); |
| | | 416 | | |
| | 0 | 417 | | return; |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | // Compute elapsed time between the client reported time and now. |
| | | 421 | | // Elapsed time is used to estimate the client position when playback is unpaused. |
| | | 422 | | // Ideally, the request is received and handled without major delays. |
| | | 423 | | // However, to avoid waiting indefinitely when a client is not reporting a correct time, |
| | | 424 | | // the elapsed time is ignored after a certain threshold. |
| | 0 | 425 | | var currentTime = DateTime.UtcNow; |
| | 0 | 426 | | var elapsedTime = currentTime.Subtract(request.When); |
| | 0 | 427 | | var timeSyncThresholdTicks = TimeSpan.FromMilliseconds(context.TimeSyncOffset).Ticks; |
| | 0 | 428 | | if (Math.Abs(elapsedTime.Ticks) > timeSyncThresholdTicks) |
| | | 429 | | { |
| | 0 | 430 | | _logger.LogWarning("Session {SessionId} is not time syncing properly. Ignoring elapsed time.", session.I |
| | | 431 | | |
| | 0 | 432 | | elapsedTime = TimeSpan.Zero; |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | // Ignore elapsed time if client is paused. |
| | 0 | 436 | | if (!request.IsPlaying) |
| | | 437 | | { |
| | 0 | 438 | | elapsedTime = TimeSpan.Zero; |
| | | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | var requestTicks = context.SanitizePositionTicks(request.PositionTicks); |
| | 0 | 442 | | var clientPosition = TimeSpan.FromTicks(requestTicks) + elapsedTime; |
| | 0 | 443 | | var delayTicks = context.PositionTicks - clientPosition.Ticks; |
| | 0 | 444 | | var maxPlaybackOffsetTicks = TimeSpan.FromMilliseconds(context.MaxPlaybackOffset).Ticks; |
| | | 445 | | |
| | 0 | 446 | | _logger.LogDebug("Session {SessionId} is at {PositionTicks} (delay of {Delay} seconds) in group {GroupId}.", |
| | | 447 | | |
| | 0 | 448 | | if (ResumePlaying) |
| | | 449 | | { |
| | | 450 | | // Handle case where session reported as ready but in reality |
| | | 451 | | // it has no clue of the real position nor the playback state. |
| | 0 | 452 | | if (!request.IsPlaying && Math.Abs(delayTicks) > maxPlaybackOffsetTicks) |
| | | 453 | | { |
| | | 454 | | // Session not ready at all. |
| | 0 | 455 | | context.SetBuffering(session, true); |
| | | 456 | | |
| | | 457 | | // Correcting session's position. |
| | 0 | 458 | | var command = context.NewSyncPlayCommand(SendCommandType.Seek); |
| | 0 | 459 | | context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken); |
| | | 460 | | |
| | | 461 | | // Notify relevant state change event. |
| | 0 | 462 | | SendGroupStateUpdate(context, request, session, cancellationToken); |
| | | 463 | | |
| | 0 | 464 | | _logger.LogWarning("Session {SessionId} got lost in time, correcting.", session.Id); |
| | 0 | 465 | | return; |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | // Session is ready. |
| | 0 | 469 | | context.SetBuffering(session, false); |
| | | 470 | | |
| | 0 | 471 | | if (context.IsBuffering()) |
| | | 472 | | { |
| | | 473 | | // Others are still buffering, tell this client to pause when ready. |
| | 0 | 474 | | var command = context.NewSyncPlayCommand(SendCommandType.Pause); |
| | 0 | 475 | | command.When = currentTime.AddTicks(delayTicks); |
| | 0 | 476 | | context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken); |
| | | 477 | | |
| | 0 | 478 | | _logger.LogInformation("Session {SessionId} will pause when ready in {Delay} seconds. Group {GroupId |
| | | 479 | | } |
| | | 480 | | else |
| | | 481 | | { |
| | | 482 | | // If all ready, then start playback. |
| | | 483 | | // Let other clients resume as soon as the buffering client catches up. |
| | 0 | 484 | | if (delayTicks > context.GetHighestPing() * 2 * TimeSpan.TicksPerMillisecond) |
| | | 485 | | { |
| | | 486 | | // Client that was buffering is recovering, notifying others to resume. |
| | 0 | 487 | | context.LastActivity = currentTime.AddTicks(delayTicks); |
| | 0 | 488 | | var command = context.NewSyncPlayCommand(SendCommandType.Unpause); |
| | 0 | 489 | | var filter = SyncPlayBroadcastType.AllExceptCurrentSession; |
| | 0 | 490 | | if (!request.IsPlaying) |
| | | 491 | | { |
| | 0 | 492 | | filter = SyncPlayBroadcastType.AllGroup; |
| | | 493 | | } |
| | | 494 | | |
| | 0 | 495 | | context.SendCommand(session, filter, command, cancellationToken); |
| | | 496 | | |
| | 0 | 497 | | _logger.LogInformation("Session {SessionId} is recovering, group {GroupId} will resume in {Delay |
| | | 498 | | } |
| | | 499 | | else |
| | | 500 | | { |
| | | 501 | | // Client, that was buffering, resumed playback but did not update others in time. |
| | 0 | 502 | | delayTicks = context.GetHighestPing() * 2 * TimeSpan.TicksPerMillisecond; |
| | 0 | 503 | | delayTicks = Math.Max(delayTicks, context.DefaultPing); |
| | | 504 | | |
| | 0 | 505 | | context.LastActivity = currentTime.AddTicks(delayTicks); |
| | | 506 | | |
| | 0 | 507 | | var command = context.NewSyncPlayCommand(SendCommandType.Unpause); |
| | 0 | 508 | | context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken); |
| | | 509 | | |
| | 0 | 510 | | _logger.LogWarning("Session {SessionId} resumed playback, group {GroupId} has {Delay} seconds to |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | // Change state. |
| | 0 | 514 | | var playingState = new PlayingGroupState(LoggerFactory); |
| | 0 | 515 | | context.SetState(playingState); |
| | 0 | 516 | | playingState.HandleRequest(request, context, Type, session, cancellationToken); |
| | | 517 | | } |
| | | 518 | | } |
| | | 519 | | else |
| | | 520 | | { |
| | | 521 | | // Check that session is really ready, tolerate player imperfections under a certain threshold. |
| | 0 | 522 | | if (Math.Abs(context.PositionTicks - requestTicks) > maxPlaybackOffsetTicks) |
| | | 523 | | { |
| | | 524 | | // Session still not ready. |
| | 0 | 525 | | context.SetBuffering(session, true); |
| | | 526 | | // Session is seeking to wrong position, correcting. |
| | 0 | 527 | | var command = context.NewSyncPlayCommand(SendCommandType.Seek); |
| | 0 | 528 | | context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken); |
| | | 529 | | |
| | | 530 | | // Notify relevant state change event. |
| | 0 | 531 | | SendGroupStateUpdate(context, request, session, cancellationToken); |
| | | 532 | | |
| | 0 | 533 | | _logger.LogWarning("Session {SessionId} is seeking to wrong position, correcting.", session.Id); |
| | 0 | 534 | | return; |
| | | 535 | | } |
| | | 536 | | |
| | | 537 | | // Session is ready. |
| | 0 | 538 | | context.SetBuffering(session, false); |
| | | 539 | | |
| | 0 | 540 | | if (!context.IsBuffering()) |
| | | 541 | | { |
| | 0 | 542 | | _logger.LogDebug("Session {SessionId} is ready, group {GroupId} is ready.", session.Id, context.Grou |
| | | 543 | | |
| | | 544 | | // Group is ready, returning to previous state. |
| | 0 | 545 | | var pausedState = new PausedGroupState(LoggerFactory); |
| | 0 | 546 | | context.SetState(pausedState); |
| | | 547 | | |
| | 0 | 548 | | if (InitialState.Equals(GroupStateType.Playing)) |
| | | 549 | | { |
| | | 550 | | // Group went from playing to waiting state and a pause request occurred while waiting. |
| | 0 | 551 | | var pauseRequest = new PauseGroupRequest(); |
| | 0 | 552 | | pausedState.HandleRequest(pauseRequest, context, Type, session, cancellationToken); |
| | | 553 | | } |
| | 0 | 554 | | else if (InitialState.Equals(GroupStateType.Paused)) |
| | | 555 | | { |
| | 0 | 556 | | pausedState.HandleRequest(request, context, Type, session, cancellationToken); |
| | | 557 | | } |
| | | 558 | | } |
| | | 559 | | } |
| | 0 | 560 | | } |
| | | 561 | | |
| | | 562 | | /// <inheritdoc /> |
| | | 563 | | public override void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prev |
| | | 564 | | { |
| | | 565 | | // Save state if first event. |
| | 0 | 566 | | if (!InitialStateSet) |
| | | 567 | | { |
| | 0 | 568 | | InitialState = prevState; |
| | 0 | 569 | | InitialStateSet = true; |
| | | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | ResumePlaying = true; |
| | | 573 | | |
| | | 574 | | // Make sure the client knows the playing item, to avoid duplicate requests. |
| | 0 | 575 | | if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId())) |
| | | 576 | | { |
| | 0 | 577 | | _logger.LogDebug("Session {SessionId} provided the wrong playlist item for group {GroupId}.", session.Id |
| | 0 | 578 | | return; |
| | | 579 | | } |
| | | 580 | | |
| | 0 | 581 | | var newItem = context.NextItemInQueue(); |
| | 0 | 582 | | if (newItem) |
| | | 583 | | { |
| | | 584 | | // Send playing-queue update. |
| | 0 | 585 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NextItem); |
| | 0 | 586 | | var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 587 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken); |
| | | 588 | | |
| | | 589 | | // Reset status of sessions and await for all Ready events. |
| | 0 | 590 | | context.SetAllBuffering(true); |
| | | 591 | | } |
| | | 592 | | else |
| | | 593 | | { |
| | | 594 | | // Return to old state. |
| | 0 | 595 | | IGroupState newState = prevState switch |
| | 0 | 596 | | { |
| | 0 | 597 | | GroupStateType.Playing => new PlayingGroupState(LoggerFactory), |
| | 0 | 598 | | GroupStateType.Paused => new PausedGroupState(LoggerFactory), |
| | 0 | 599 | | _ => new IdleGroupState(LoggerFactory) |
| | 0 | 600 | | }; |
| | | 601 | | |
| | 0 | 602 | | context.SetState(newState); |
| | | 603 | | |
| | 0 | 604 | | _logger.LogDebug("No next item available in group {GroupId}.", context.GroupId.ToString()); |
| | | 605 | | } |
| | 0 | 606 | | } |
| | | 607 | | |
| | | 608 | | /// <inheritdoc /> |
| | | 609 | | public override void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType |
| | | 610 | | { |
| | | 611 | | // Save state if first event. |
| | 0 | 612 | | if (!InitialStateSet) |
| | | 613 | | { |
| | 0 | 614 | | InitialState = prevState; |
| | 0 | 615 | | InitialStateSet = true; |
| | | 616 | | } |
| | | 617 | | |
| | 0 | 618 | | ResumePlaying = true; |
| | | 619 | | |
| | | 620 | | // Make sure the client knows the playing item, to avoid duplicate requests. |
| | 0 | 621 | | if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId())) |
| | | 622 | | { |
| | 0 | 623 | | _logger.LogDebug("Session {SessionId} provided the wrong playlist item for group {GroupId}.", session.Id |
| | 0 | 624 | | return; |
| | | 625 | | } |
| | | 626 | | |
| | 0 | 627 | | var newItem = context.PreviousItemInQueue(); |
| | 0 | 628 | | if (newItem) |
| | | 629 | | { |
| | | 630 | | // Send playing-queue update. |
| | 0 | 631 | | var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.PreviousItem); |
| | 0 | 632 | | var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate); |
| | 0 | 633 | | context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken); |
| | | 634 | | |
| | | 635 | | // Reset status of sessions and await for all Ready events. |
| | 0 | 636 | | context.SetAllBuffering(true); |
| | | 637 | | } |
| | | 638 | | else |
| | | 639 | | { |
| | | 640 | | // Return to old state. |
| | 0 | 641 | | IGroupState newState = prevState switch |
| | 0 | 642 | | { |
| | 0 | 643 | | GroupStateType.Playing => new PlayingGroupState(LoggerFactory), |
| | 0 | 644 | | GroupStateType.Paused => new PausedGroupState(LoggerFactory), |
| | 0 | 645 | | _ => new IdleGroupState(LoggerFactory) |
| | 0 | 646 | | }; |
| | | 647 | | |
| | 0 | 648 | | context.SetState(newState); |
| | | 649 | | |
| | 0 | 650 | | _logger.LogDebug("No previous item available in group {GroupId}.", context.GroupId.ToString()); |
| | | 651 | | } |
| | 0 | 652 | | } |
| | | 653 | | |
| | | 654 | | /// <inheritdoc /> |
| | | 655 | | public override void HandleRequest(IgnoreWaitGroupRequest request, IGroupStateContext context, GroupStateType pr |
| | | 656 | | { |
| | 0 | 657 | | context.SetIgnoreGroupWait(session, request.IgnoreWait); |
| | | 658 | | |
| | 0 | 659 | | if (!context.IsBuffering()) |
| | | 660 | | { |
| | 0 | 661 | | _logger.LogDebug("Ignoring session {SessionId}, group {GroupId} is ready.", session.Id, context.GroupId. |
| | | 662 | | |
| | 0 | 663 | | if (ResumePlaying) |
| | | 664 | | { |
| | | 665 | | // Client, that was buffering, stopped following playback. |
| | 0 | 666 | | var playingState = new PlayingGroupState(LoggerFactory); |
| | 0 | 667 | | context.SetState(playingState); |
| | 0 | 668 | | var unpauseRequest = new UnpauseGroupRequest(); |
| | 0 | 669 | | playingState.HandleRequest(unpauseRequest, context, Type, session, cancellationToken); |
| | | 670 | | } |
| | | 671 | | else |
| | | 672 | | { |
| | | 673 | | // Group is ready, returning to previous state. |
| | 0 | 674 | | var pausedState = new PausedGroupState(LoggerFactory); |
| | 0 | 675 | | context.SetState(pausedState); |
| | | 676 | | } |
| | | 677 | | } |
| | 0 | 678 | | } |
| | | 679 | | } |
| | | 680 | | } |