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