2020-11-13 07:13:32 -07:00
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
using MediaBrowser.Model.SyncPlay;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.SyncPlay
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class IdleGroupState.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Class is not thread-safe, external locking is required when accessing methods.
|
|
|
|
/// </remarks>
|
|
|
|
public class IdleGroupState : AbstractGroupState
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="IdleGroupState"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
|
|
|
|
public IdleGroupState(ILogger logger)
|
|
|
|
: base(logger)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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 />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, PlayGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// Change state.
|
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, UnpauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// Change state.
|
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, PauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, StopGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, SeekGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, BufferGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, ReadyGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
SendStopCommand(context, prevState, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, NextTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// Change state.
|
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, PreviousTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// Change state.
|
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|