2021-05-06 18:39:20 -04:00
|
|
|
#nullable disable
|
|
|
|
|
2020-09-24 17:04:21 -04:00
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Controller.Session;
|
2020-11-14 17:40:01 -05:00
|
|
|
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
2020-09-24 17:04:21 -04:00
|
|
|
using MediaBrowser.Model.SyncPlay;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2020-11-14 17:40:01 -05:00
|
|
|
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class AbstractGroupState.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Class is not thread-safe, external locking is required when accessing methods.
|
|
|
|
/// </remarks>
|
2020-11-13 09:13:32 -05:00
|
|
|
public abstract class AbstractGroupState : IGroupState
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
2020-11-16 11:40:19 -05:00
|
|
|
/// <summary>
|
|
|
|
/// The logger.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogger<AbstractGroupState> _logger;
|
|
|
|
|
2020-09-24 17:04:21 -04:00
|
|
|
/// <summary>
|
2020-11-13 09:13:32 -05:00
|
|
|
/// Initializes a new instance of the <see cref="AbstractGroupState"/> class.
|
2020-09-24 17:04:21 -04:00
|
|
|
/// </summary>
|
2020-11-16 11:40:19 -05:00
|
|
|
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
|
|
|
protected AbstractGroupState(ILoggerFactory loggerFactory)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
2020-11-16 11:40:19 -05:00
|
|
|
LoggerFactory = loggerFactory;
|
|
|
|
_logger = loggerFactory.CreateLogger<AbstractGroupState>();
|
2020-09-24 17:04:21 -04:00
|
|
|
}
|
|
|
|
|
2020-11-13 09:13:32 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
public abstract GroupStateType Type { get; }
|
|
|
|
|
2020-09-24 17:04:21 -04:00
|
|
|
/// <summary>
|
2020-11-16 11:40:19 -05:00
|
|
|
/// Gets the logger factory.
|
2020-09-24 17:04:21 -04:00
|
|
|
/// </summary>
|
2020-11-16 11:40:19 -05:00
|
|
|
protected ILoggerFactory LoggerFactory { get; }
|
2020-09-24 17:04:21 -04:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 09:13:32 -05:00
|
|
|
public abstract void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
2020-09-24 17:04:21 -04:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 09:13:32 -05:00
|
|
|
public abstract void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
2020-09-24 17:04:21 -04:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(IGroupPlaybackRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(SetPlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
2020-11-16 11:40:19 -05:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
2020-09-24 17:04:21 -04:00
|
|
|
context.SetState(waitingState);
|
2020-12-04 17:16:15 -05:00
|
|
|
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
2020-09-24 17:04:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(RemoveFromPlaylistGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
2021-01-23 19:05:17 -05:00
|
|
|
bool playingItemRemoved;
|
|
|
|
if (request.ClearPlaylist)
|
|
|
|
{
|
|
|
|
context.ClearPlayQueue(request.ClearPlayingItem);
|
|
|
|
playingItemRemoved = request.ClearPlayingItem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
playingItemRemoved = context.RemoveFromPlayQueue(request.PlaylistItemIds);
|
|
|
|
}
|
2020-09-24 17:04:21 -04:00
|
|
|
|
|
|
|
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.RemoveItems);
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
|
2020-11-13 09:13:32 -05:00
|
|
|
if (playingItemRemoved && !context.PlayQueue.IsItemPlaying())
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
2020-11-16 11:40:19 -05:00
|
|
|
_logger.LogDebug("Play queue in group {GroupId} is now empty.", context.GroupId.ToString());
|
2020-11-13 09:13:32 -05:00
|
|
|
|
2020-11-16 11:40:19 -05:00
|
|
|
IGroupState idleState = new IdleGroupState(LoggerFactory);
|
2020-11-13 09:13:32 -05:00
|
|
|
context.SetState(idleState);
|
|
|
|
var stopRequest = new StopGroupRequest();
|
2020-12-04 17:16:15 -05:00
|
|
|
idleState.HandleRequest(stopRequest, context, Type, session, cancellationToken);
|
2020-09-24 17:04:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(MovePlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
var result = context.MoveItemInPlayQueue(request.PlaylistItemId, request.NewIndex);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
2020-11-16 11:40:19 -05:00
|
|
|
_logger.LogError("Unable to move item in group {GroupId}.", context.GroupId.ToString());
|
2020-09-24 17:04:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.MoveItem);
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(QueueGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
var result = context.AddToPlayQueue(request.ItemIds, request.Mode);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
2020-11-16 11:40:19 -05:00
|
|
|
_logger.LogError("Unable to add items to play queue in group {GroupId}.", context.GroupId.ToString());
|
2020-09-24 17:04:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:40:01 -05:00
|
|
|
var reason = request.Mode switch
|
|
|
|
{
|
|
|
|
GroupQueueMode.QueueNext => PlayQueueUpdateReason.QueueNext,
|
|
|
|
_ => PlayQueueUpdateReason.Queue
|
|
|
|
};
|
2020-09-24 17:04:21 -04:00
|
|
|
var playQueueUpdate = context.GetPlayQueueUpdate(reason);
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(SetRepeatModeGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
context.SetRepeatMode(request.Mode);
|
|
|
|
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.RepeatMode);
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(SetShuffleModeGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
context.SetShuffleMode(request.Mode);
|
|
|
|
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.ShuffleMode);
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(PingGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
2020-10-21 09:46:50 -04:00
|
|
|
// Collected pings are used to account for network latency when unpausing playback.
|
2020-09-24 17:04:21 -04:00
|
|
|
context.UpdatePing(session, request.Ping);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 17:16:15 -05:00
|
|
|
public virtual void HandleRequest(IgnoreWaitGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
|
|
|
context.SetIgnoreGroupWait(session, request.IgnoreWait);
|
|
|
|
}
|
|
|
|
|
2020-11-13 09:13:32 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Sends a group state update to all group.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">The context of the state.</param>
|
|
|
|
/// <param name="reason">The reason of the state change.</param>
|
|
|
|
/// <param name="session">The session.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
protected void SendGroupStateUpdate(IGroupStateContext context, IGroupPlaybackRequest reason, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// Notify relevant state change event.
|
2020-11-28 10:03:02 -05:00
|
|
|
var stateUpdate = new GroupStateUpdate(Type, reason.Action);
|
2020-11-13 09:13:32 -05:00
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.StateUpdate, stateUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UnhandledRequest(IGroupPlaybackRequest request)
|
2020-09-24 17:04:21 -04:00
|
|
|
{
|
2020-11-28 10:03:02 -05:00
|
|
|
_logger.LogWarning("Unhandled request of type {RequestType} in {StateType} state.", request.Action, Type);
|
2020-09-24 17:04:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|