2020-11-13 07:13:32 -07:00
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Controller.Session;
|
2020-11-14 15:40:01 -07:00
|
|
|
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
2020-11-13 07:13:32 -07:00
|
|
|
using MediaBrowser.Model.SyncPlay;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2020-11-14 15:40:01 -07:00
|
|
|
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class IdleGroupState.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Class is not thread-safe, external locking is required when accessing methods.
|
|
|
|
/// </remarks>
|
|
|
|
public class IdleGroupState : AbstractGroupState
|
|
|
|
{
|
2020-11-16 09:40:19 -07:00
|
|
|
/// <summary>
|
|
|
|
/// The logger.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogger<IdleGroupState> _logger;
|
|
|
|
|
2020-11-13 07:13:32 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="IdleGroupState"/> class.
|
|
|
|
/// </summary>
|
2020-11-16 09:40:19 -07:00
|
|
|
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
|
|
|
public IdleGroupState(ILoggerFactory loggerFactory)
|
|
|
|
: base(loggerFactory)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
2020-11-16 09:40:19 -07:00
|
|
|
_logger = LoggerFactory.CreateLogger<IdleGroupState>();
|
2020-11-13 07:13:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-14 10:09:25 -07:00
|
|
|
public override GroupStateType Type { get; } = GroupStateType.Idle;
|
2020-11-13 07:13:32 -07:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
SendStopCommand(context, Type, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
// Change state.
|
2020-11-16 09:40:19 -07:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
2020-11-13 07:13:32 -07:00
|
|
|
context.SetState(waitingState);
|
2020-12-04 15:16:15 -07:00
|
|
|
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
2020-11-13 07:13:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
// Change state.
|
2020-11-16 09:40:19 -07:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
2020-11-13 07:13:32 -07:00
|
|
|
context.SetState(waitingState);
|
2020-12-04 15:16:15 -07:00
|
|
|
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
2020-11-13 07:13:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
// Change state.
|
2020-11-16 09:40:19 -07:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
2020-11-13 07:13:32 -07:00
|
|
|
context.SetState(waitingState);
|
2020-12-04 15:16:15 -07:00
|
|
|
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
2020-11-13 07:13:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-12-04 15:16:15 -07:00
|
|
|
public override void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-11-13 07:13:32 -07:00
|
|
|
{
|
|
|
|
// Change state.
|
2020-11-16 09:40:19 -07:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
2020-11-13 07:13:32 -07:00
|
|
|
context.SetState(waitingState);
|
2020-12-04 15:16:15 -07:00
|
|
|
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
2020-11-13 07:13:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SendStopCommand(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Stop);
|
|
|
|
if (!prevState.Equals(Type))
|
|
|
|
{
|
|
|
|
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|