2020-11-13 07:13:32 -07:00
|
|
|
using System;
|
2020-09-24 14:04:21 -07:00
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
using MediaBrowser.Model.SyncPlay;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.SyncPlay
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class AbstractGroupState.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Class is not thread-safe, external locking is required when accessing methods.
|
|
|
|
/// </remarks>
|
2020-11-13 07:13:32 -07:00
|
|
|
public abstract class AbstractGroupState : IGroupState
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-11-13 07:13:32 -07:00
|
|
|
/// Initializes a new instance of the <see cref="AbstractGroupState"/> class.
|
2020-09-24 14:04:21 -07:00
|
|
|
/// </summary>
|
2020-11-13 07:13:32 -07:00
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
|
|
|
|
protected AbstractGroupState(ILogger logger)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
2020-11-13 07:13:32 -07:00
|
|
|
Logger = logger;
|
2020-09-24 14:04:21 -07:00
|
|
|
}
|
|
|
|
|
2020-11-13 07:13:32 -07:00
|
|
|
/// <inheritdoc />
|
|
|
|
public abstract GroupStateType Type { get; }
|
|
|
|
|
2020-09-24 14:04:21 -07:00
|
|
|
/// <summary>
|
2020-11-13 07:13:32 -07:00
|
|
|
/// Gets the logger.
|
2020-09-24 14:04:21 -07:00
|
|
|
/// </summary>
|
2020-11-13 07:13:32 -07:00
|
|
|
protected ILogger Logger { get; }
|
2020-09-24 14:04:21 -07:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public abstract void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
2020-09-24 14:04:21 -07:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public abstract void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
2020-09-24 14:04:21 -07:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, IGroupPlaybackRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, PlayGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, SetPlaylistItemGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
2020-11-13 07:13:32 -07:00
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
2020-09-24 14:04:21 -07:00
|
|
|
context.SetState(waitingState);
|
2020-11-13 07:13:32 -07:00
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-09-24 14:04:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, RemoveFromPlaylistGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07: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 07:13:32 -07:00
|
|
|
if (playingItemRemoved && !context.PlayQueue.IsItemPlaying())
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
2020-11-13 07:13:32 -07:00
|
|
|
Logger.LogDebug("HandleRequest: {0} in group {1}, play queue is empty.", request.Type, context.GroupId.ToString());
|
|
|
|
|
|
|
|
IGroupState idleState = new IdleGroupState(Logger);
|
|
|
|
context.SetState(idleState);
|
|
|
|
var stopRequest = new StopGroupRequest();
|
|
|
|
idleState.HandleRequest(context, Type, stopRequest, session, cancellationToken);
|
2020-09-24 14:04:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, MovePlaylistItemGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
var result = context.MoveItemInPlayQueue(request.PlaylistItemId, request.NewIndex);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
2020-11-13 07:13:32 -07:00
|
|
|
Logger.LogError("HandleRequest: {0} in group {1}, unable to move item in play queue.", request.Type, context.GroupId.ToString());
|
2020-09-24 14:04:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.MoveItem);
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, QueueGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
var result = context.AddToPlayQueue(request.ItemIds, request.Mode);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
2020-11-13 07:13:32 -07:00
|
|
|
Logger.LogError("HandleRequest: {0} in group {1}, unable to add items to play queue.", request.Type, context.GroupId.ToString());
|
2020-09-24 14:04:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-13 07:13:32 -07:00
|
|
|
var reason = request.Mode.Equals("next", StringComparison.OrdinalIgnoreCase) ? PlayQueueUpdateReason.QueueNext : PlayQueueUpdateReason.Queue;
|
2020-09-24 14:04:21 -07:00
|
|
|
var playQueueUpdate = context.GetPlayQueueUpdate(reason);
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, UnpauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, PauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, StopGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, SeekGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, BufferGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, ReadyGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, NextTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, PreviousTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
UnhandledRequest(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, SetRepeatModeGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07: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-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, SetShuffleModeGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07: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-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, PingGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
2020-10-21 06:46:50 -07:00
|
|
|
// Collected pings are used to account for network latency when unpausing playback.
|
2020-09-24 14:04:21 -07:00
|
|
|
context.UpdatePing(session, request.Ping);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 07:13:32 -07:00
|
|
|
public virtual void HandleRequest(IGroupStateContext context, GroupStateType prevState, IgnoreWaitGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
|
|
|
context.SetIgnoreGroupWait(session, request.IgnoreWait);
|
|
|
|
}
|
|
|
|
|
2020-11-13 07:13:32 -07: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.
|
|
|
|
var stateUpdate = new GroupStateUpdate()
|
|
|
|
{
|
|
|
|
State = Type,
|
|
|
|
Reason = reason.Type
|
|
|
|
};
|
|
|
|
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.StateUpdate, stateUpdate);
|
|
|
|
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UnhandledRequest(IGroupPlaybackRequest request)
|
2020-09-24 14:04:21 -07:00
|
|
|
{
|
2020-11-13 07:13:32 -07:00
|
|
|
Logger.LogWarning("HandleRequest: unhandled {0} request for {1} state.", request.Type, Type);
|
2020-09-24 14:04:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|