123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680 |
- #nullable disable
- using System;
- using System.Threading;
- using MediaBrowser.Controller.Session;
- using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
- using MediaBrowser.Model.SyncPlay;
- using Microsoft.Extensions.Logging;
- namespace MediaBrowser.Controller.SyncPlay.GroupStates
- {
- /// <summary>
- /// Class WaitingGroupState.
- /// </summary>
- /// <remarks>
- /// Class is not thread-safe, external locking is required when accessing methods.
- /// </remarks>
- public class WaitingGroupState : AbstractGroupState
- {
- /// <summary>
- /// The logger.
- /// </summary>
- private readonly ILogger<WaitingGroupState> _logger;
- /// <summary>
- /// Initializes a new instance of the <see cref="WaitingGroupState"/> class.
- /// </summary>
- /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
- public WaitingGroupState(ILoggerFactory loggerFactory)
- : base(loggerFactory)
- {
- _logger = LoggerFactory.CreateLogger<WaitingGroupState>();
- }
- /// <inheritdoc />
- public override GroupStateType Type { get; } = GroupStateType.Waiting;
- /// <summary>
- /// Gets or sets a value indicating whether playback should resume when group is ready.
- /// </summary>
- public bool ResumePlaying { get; set; } = false;
- /// <summary>
- /// Gets or sets a value indicating whether the initial state has been set.
- /// </summary>
- private bool InitialStateSet { get; set; } = false;
- /// <summary>
- /// Gets or sets the group state before the first ever event.
- /// </summary>
- private GroupStateType InitialState { get; set; }
- /// <inheritdoc />
- public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- if (prevState.Equals(GroupStateType.Playing))
- {
- ResumePlaying = true;
- // Pause group and compute the media playback position.
- var currentTime = DateTime.UtcNow;
- var elapsedTime = currentTime - context.LastActivity;
- context.LastActivity = currentTime;
- // Elapsed time is negative if event happens
- // during the delay added to account for latency.
- // In this phase clients haven't started the playback yet.
- // In other words, LastActivity is in the future,
- // when playback unpause is supposed to happen.
- // Seek only if playback actually started.
- context.PositionTicks += Math.Max(elapsedTime.Ticks, 0);
- }
- // Prepare new session.
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist);
- var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, update, cancellationToken);
- context.SetBuffering(session, true);
- // Send pause command to all non-buffering sessions.
- var command = context.NewSyncPlayCommand(SendCommandType.Pause);
- context.SendCommand(session, SyncPlayBroadcastType.AllReady, command, cancellationToken);
- }
- /// <inheritdoc />
- public override void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- context.SetBuffering(session, false);
- if (!context.IsBuffering())
- {
- if (ResumePlaying)
- {
- _logger.LogDebug("Session {SessionId} left group {GroupId}, notifying others to resume.", session.Id, context.GroupId.ToString());
- // Client, that was buffering, left the group.
- var playingState = new PlayingGroupState(LoggerFactory);
- context.SetState(playingState);
- var unpauseRequest = new UnpauseGroupRequest();
- playingState.HandleRequest(unpauseRequest, context, Type, session, cancellationToken);
- }
- else
- {
- _logger.LogDebug("Session {SessionId} left group {GroupId}, returning to previous state.", session.Id, context.GroupId.ToString());
- // Group is ready, returning to previous state.
- var pausedState = new PausedGroupState(LoggerFactory);
- context.SetState(pausedState);
- }
- }
- }
- /// <inheritdoc />
- public override void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- ResumePlaying = true;
- var setQueueStatus = context.SetPlayQueue(request.PlayingQueue, request.PlayingItemPosition, request.StartPositionTicks);
- if (!setQueueStatus)
- {
- _logger.LogError("Unable to set playing queue in group {GroupId}.", context.GroupId.ToString());
- // Ignore request and return to previous state.
- IGroupState newState = prevState switch {
- GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
- GroupStateType.Paused => new PausedGroupState(LoggerFactory),
- _ => new IdleGroupState(LoggerFactory)
- };
- context.SetState(newState);
- return;
- }
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist);
- var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
- // Reset status of sessions and await for all Ready events.
- context.SetAllBuffering(true);
- _logger.LogDebug("Session {SessionId} set a new play queue in group {GroupId}.", session.Id, context.GroupId.ToString());
- }
- /// <inheritdoc />
- public override void HandleRequest(SetPlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- ResumePlaying = true;
- var result = context.SetPlayingItem(request.PlaylistItemId);
- if (result)
- {
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem);
- var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
- // Reset status of sessions and await for all Ready events.
- context.SetAllBuffering(true);
- }
- else
- {
- // Return to old state.
- IGroupState newState = prevState switch
- {
- GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
- GroupStateType.Paused => new PausedGroupState(LoggerFactory),
- _ => new IdleGroupState(LoggerFactory)
- };
- context.SetState(newState);
- _logger.LogDebug("Unable to change current playing item in group {GroupId}.", context.GroupId.ToString());
- }
- }
- /// <inheritdoc />
- public override void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- if (prevState.Equals(GroupStateType.Idle))
- {
- ResumePlaying = true;
- context.RestartCurrentItem();
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist);
- var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
- // Reset status of sessions and await for all Ready events.
- context.SetAllBuffering(true);
- _logger.LogDebug("Group {GroupId} is waiting for all ready events.", context.GroupId.ToString());
- }
- else
- {
- if (ResumePlaying)
- {
- _logger.LogDebug("Forcing the playback to start in group {GroupId}. Group-wait is disabled until next state change.", context.GroupId.ToString());
- // An Unpause request is forcing the playback to start, ignoring sessions that are not ready.
- context.SetAllBuffering(false);
- // Change state.
- var playingState = new PlayingGroupState(LoggerFactory)
- {
- IgnoreBuffering = true
- };
- context.SetState(playingState);
- playingState.HandleRequest(request, context, Type, session, cancellationToken);
- }
- else
- {
- // Group would have gone to paused state, now will go to playing state when ready.
- ResumePlaying = true;
- // Notify relevant state change event.
- SendGroupStateUpdate(context, request, session, cancellationToken);
- }
- }
- }
- /// <inheritdoc />
- public override void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- // Wait for sessions to be ready, then switch to paused state.
- ResumePlaying = false;
- // Notify relevant state change event.
- SendGroupStateUpdate(context, request, session, cancellationToken);
- }
- /// <inheritdoc />
- public override void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- // Change state.
- var idleState = new IdleGroupState(LoggerFactory);
- context.SetState(idleState);
- idleState.HandleRequest(request, context, Type, session, cancellationToken);
- }
- /// <inheritdoc />
- public override void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- if (prevState.Equals(GroupStateType.Playing))
- {
- ResumePlaying = true;
- }
- else if (prevState.Equals(GroupStateType.Paused))
- {
- ResumePlaying = false;
- }
- // Sanitize PositionTicks.
- var ticks = context.SanitizePositionTicks(request.PositionTicks);
- // Seek.
- context.PositionTicks = ticks;
- context.LastActivity = DateTime.UtcNow;
- var command = context.NewSyncPlayCommand(SendCommandType.Seek);
- context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
- // Reset status of sessions and await for all Ready events.
- context.SetAllBuffering(true);
- // Notify relevant state change event.
- SendGroupStateUpdate(context, request, session, cancellationToken);
- }
- /// <inheritdoc />
- public override void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- // Make sure the client is playing the correct item.
- if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
- {
- _logger.LogDebug("Session {SessionId} reported wrong playlist item in group {GroupId}.", session.Id, context.GroupId.ToString());
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem);
- var updateSession = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
- context.SetBuffering(session, true);
- return;
- }
- if (prevState.Equals(GroupStateType.Playing))
- {
- // Resume playback when all ready.
- ResumePlaying = true;
- context.SetBuffering(session, true);
- // Pause group and compute the media playback position.
- var currentTime = DateTime.UtcNow;
- var elapsedTime = currentTime - context.LastActivity;
- context.LastActivity = currentTime;
- // Elapsed time is negative if event happens
- // during the delay added to account for latency.
- // In this phase clients haven't started the playback yet.
- // In other words, LastActivity is in the future,
- // when playback unpause is supposed to happen.
- // Seek only if playback actually started.
- context.PositionTicks += Math.Max(elapsedTime.Ticks, 0);
- // Send pause command to all non-buffering sessions.
- var command = context.NewSyncPlayCommand(SendCommandType.Pause);
- context.SendCommand(session, SyncPlayBroadcastType.AllReady, command, cancellationToken);
- }
- else if (prevState.Equals(GroupStateType.Paused))
- {
- // Don't resume playback when all ready.
- ResumePlaying = false;
- context.SetBuffering(session, true);
- // Send pause command to buffering session.
- var command = context.NewSyncPlayCommand(SendCommandType.Pause);
- context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
- }
- else if (prevState.Equals(GroupStateType.Waiting))
- {
- // Another session is now buffering.
- context.SetBuffering(session, true);
- if (!ResumePlaying)
- {
- // Force update for this session that should be paused.
- var command = context.NewSyncPlayCommand(SendCommandType.Pause);
- context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
- }
- }
- // Notify relevant state change event.
- SendGroupStateUpdate(context, request, session, cancellationToken);
- }
- /// <inheritdoc />
- public override void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- // Make sure the client is playing the correct item.
- if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
- {
- _logger.LogDebug("Session {SessionId} reported wrong playlist item in group {GroupId}.", session.Id, context.GroupId.ToString());
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem);
- var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, update, cancellationToken);
- context.SetBuffering(session, true);
- return;
- }
- // Compute elapsed time between the client reported time and now.
- // Elapsed time is used to estimate the client position when playback is unpaused.
- // Ideally, the request is received and handled without major delays.
- // However, to avoid waiting indefinitely when a client is not reporting a correct time,
- // the elapsed time is ignored after a certain threshold.
- var currentTime = DateTime.UtcNow;
- var elapsedTime = currentTime.Subtract(request.When);
- var timeSyncThresholdTicks = TimeSpan.FromMilliseconds(context.TimeSyncOffset).Ticks;
- if (Math.Abs(elapsedTime.Ticks) > timeSyncThresholdTicks)
- {
- _logger.LogWarning("Session {SessionId} is not time syncing properly. Ignoring elapsed time.", session.Id);
- elapsedTime = TimeSpan.Zero;
- }
- // Ignore elapsed time if client is paused.
- if (!request.IsPlaying)
- {
- elapsedTime = TimeSpan.Zero;
- }
- var requestTicks = context.SanitizePositionTicks(request.PositionTicks);
- var clientPosition = TimeSpan.FromTicks(requestTicks) + elapsedTime;
- var delayTicks = context.PositionTicks - clientPosition.Ticks;
- var maxPlaybackOffsetTicks = TimeSpan.FromMilliseconds(context.MaxPlaybackOffset).Ticks;
- _logger.LogDebug("Session {SessionId} is at {PositionTicks} (delay of {Delay} seconds) in group {GroupId}.", session.Id, clientPosition, TimeSpan.FromTicks(delayTicks).TotalSeconds, context.GroupId.ToString());
- if (ResumePlaying)
- {
- // Handle case where session reported as ready but in reality
- // it has no clue of the real position nor the playback state.
- if (!request.IsPlaying && Math.Abs(delayTicks) > maxPlaybackOffsetTicks)
- {
- // Session not ready at all.
- context.SetBuffering(session, true);
- // Correcting session's position.
- var command = context.NewSyncPlayCommand(SendCommandType.Seek);
- context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
- // Notify relevant state change event.
- SendGroupStateUpdate(context, request, session, cancellationToken);
- _logger.LogWarning("Session {SessionId} got lost in time, correcting.", session.Id);
- return;
- }
- // Session is ready.
- context.SetBuffering(session, false);
- if (context.IsBuffering())
- {
- // Others are still buffering, tell this client to pause when ready.
- var command = context.NewSyncPlayCommand(SendCommandType.Pause);
- command.When = currentTime.AddTicks(delayTicks);
- context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
- _logger.LogInformation("Session {SessionId} will pause when ready in {Delay} seconds. Group {GroupId} is waiting for all ready events.", session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds, context.GroupId.ToString());
- }
- else
- {
- // If all ready, then start playback.
- // Let other clients resume as soon as the buffering client catches up.
- if (delayTicks > context.GetHighestPing() * 2 * TimeSpan.TicksPerMillisecond)
- {
- // Client that was buffering is recovering, notifying others to resume.
- context.LastActivity = currentTime.AddTicks(delayTicks);
- var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
- var filter = SyncPlayBroadcastType.AllExceptCurrentSession;
- if (!request.IsPlaying)
- {
- filter = SyncPlayBroadcastType.AllGroup;
- }
- context.SendCommand(session, filter, command, cancellationToken);
- _logger.LogInformation("Session {SessionId} is recovering, group {GroupId} will resume in {Delay} seconds.", session.Id, context.GroupId.ToString(), TimeSpan.FromTicks(delayTicks).TotalSeconds);
- }
- else
- {
- // Client, that was buffering, resumed playback but did not update others in time.
- delayTicks = context.GetHighestPing() * 2 * TimeSpan.TicksPerMillisecond;
- delayTicks = Math.Max(delayTicks, context.DefaultPing);
- context.LastActivity = currentTime.AddTicks(delayTicks);
- var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
- context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
- _logger.LogWarning("Session {SessionId} resumed playback, group {GroupId} has {Delay} seconds to recover.", session.Id, context.GroupId.ToString(), TimeSpan.FromTicks(delayTicks).TotalSeconds);
- }
- // Change state.
- var playingState = new PlayingGroupState(LoggerFactory);
- context.SetState(playingState);
- playingState.HandleRequest(request, context, Type, session, cancellationToken);
- }
- }
- else
- {
- // Check that session is really ready, tolerate player imperfections under a certain threshold.
- if (Math.Abs(context.PositionTicks - requestTicks) > maxPlaybackOffsetTicks)
- {
- // Session still not ready.
- context.SetBuffering(session, true);
- // Session is seeking to wrong position, correcting.
- var command = context.NewSyncPlayCommand(SendCommandType.Seek);
- context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
- // Notify relevant state change event.
- SendGroupStateUpdate(context, request, session, cancellationToken);
- _logger.LogWarning("Session {SessionId} is seeking to wrong position, correcting.", session.Id);
- return;
- }
- // Session is ready.
- context.SetBuffering(session, false);
- if (!context.IsBuffering())
- {
- _logger.LogDebug("Session {SessionId} is ready, group {GroupId} is ready.", session.Id, context.GroupId.ToString());
- // Group is ready, returning to previous state.
- var pausedState = new PausedGroupState(LoggerFactory);
- context.SetState(pausedState);
- if (InitialState.Equals(GroupStateType.Playing))
- {
- // Group went from playing to waiting state and a pause request occurred while waiting.
- var pauseRequest = new PauseGroupRequest();
- pausedState.HandleRequest(pauseRequest, context, Type, session, cancellationToken);
- }
- else if (InitialState.Equals(GroupStateType.Paused))
- {
- pausedState.HandleRequest(request, context, Type, session, cancellationToken);
- }
- }
- }
- }
- /// <inheritdoc />
- public override void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- ResumePlaying = true;
- // Make sure the client knows the playing item, to avoid duplicate requests.
- if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
- {
- _logger.LogDebug("Session {SessionId} provided the wrong playlist item for group {GroupId}.", session.Id, context.GroupId.ToString());
- return;
- }
- var newItem = context.NextItemInQueue();
- if (newItem)
- {
- // Send playing-queue update.
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NextItem);
- var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
- // Reset status of sessions and await for all Ready events.
- context.SetAllBuffering(true);
- }
- else
- {
- // Return to old state.
- IGroupState newState = prevState switch
- {
- GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
- GroupStateType.Paused => new PausedGroupState(LoggerFactory),
- _ => new IdleGroupState(LoggerFactory)
- };
- context.SetState(newState);
- _logger.LogDebug("No next item available in group {GroupId}.", context.GroupId.ToString());
- }
- }
- /// <inheritdoc />
- public override void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- // Save state if first event.
- if (!InitialStateSet)
- {
- InitialState = prevState;
- InitialStateSet = true;
- }
- ResumePlaying = true;
- // Make sure the client knows the playing item, to avoid duplicate requests.
- if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
- {
- _logger.LogDebug("Session {SessionId} provided the wrong playlist item for group {GroupId}.", session.Id, context.GroupId.ToString());
- return;
- }
- var newItem = context.PreviousItemInQueue();
- if (newItem)
- {
- // Send playing-queue update.
- var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.PreviousItem);
- var update = new SyncPlayPlayQueueUpdate(context.GroupId, playQueueUpdate);
- context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
- // Reset status of sessions and await for all Ready events.
- context.SetAllBuffering(true);
- }
- else
- {
- // Return to old state.
- IGroupState newState = prevState switch
- {
- GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
- GroupStateType.Paused => new PausedGroupState(LoggerFactory),
- _ => new IdleGroupState(LoggerFactory)
- };
- context.SetState(newState);
- _logger.LogDebug("No previous item available in group {GroupId}.", context.GroupId.ToString());
- }
- }
- /// <inheritdoc />
- public override void HandleRequest(IgnoreWaitGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
- {
- context.SetIgnoreGroupWait(session, request.IgnoreWait);
- if (!context.IsBuffering())
- {
- _logger.LogDebug("Ignoring session {SessionId}, group {GroupId} is ready.", session.Id, context.GroupId.ToString());
- if (ResumePlaying)
- {
- // Client, that was buffering, stopped following playback.
- var playingState = new PlayingGroupState(LoggerFactory);
- context.SetState(playingState);
- var unpauseRequest = new UnpauseGroupRequest();
- playingState.HandleRequest(unpauseRequest, context, Type, session, cancellationToken);
- }
- else
- {
- // Group is ready, returning to previous state.
- var pausedState = new PausedGroupState(LoggerFactory);
- context.SetState(pausedState);
- }
- }
- }
- }
- }
|