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