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