2020-05-12 17:05:05 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
using MediaBrowser.Model.SyncPlay;
|
2020-09-24 21:04:21 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-05-12 17:05:05 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.SyncPlay
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class PlayingGroupState.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Class is not thread-safe, external locking is required when accessing methods.
|
|
|
|
/// </remarks>
|
2020-09-24 21:04:21 +00:00
|
|
|
public class PlayingGroupState : AbstractGroupState
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
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="PlayingGroupState"/> class.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
|
2020-10-22 13:51:58 +00:00
|
|
|
public PlayingGroupState(ILogger logger)
|
|
|
|
: base(logger)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Do nothing.
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:05:05 +00:00
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override GroupStateType Type
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return GroupStateType.Playing;
|
|
|
|
}
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether requests for buffering should be ignored.
|
|
|
|
/// </summary>
|
|
|
|
public bool IgnoreBuffering { get; set; }
|
|
|
|
|
2020-05-12 17:05:05 +00:00
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Wait for session to be ready.
|
2020-11-13 14:13:32 +00:00
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
2020-09-24 21:04:21 +00:00
|
|
|
context.SetState(waitingState);
|
2020-11-13 14:13:32 +00:00
|
|
|
waitingState.SessionJoined(context, Type, session, cancellationToken);
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Do nothing.
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, PlayGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-13 14:13:32 +00:00
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
2020-09-24 21:04:21 +00:00
|
|
|
context.SetState(waitingState);
|
2020-11-13 14:13:32 +00:00
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, UnpauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
if (!prevState.Equals(Type))
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Pick a suitable time that accounts for latency.
|
2020-09-24 21:04:21 +00:00
|
|
|
var delayMillis = Math.Max(context.GetHighestPing() * 2, context.DefaultPing);
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-10-21 13:46:50 +00:00
|
|
|
// Unpause group and set starting point in future.
|
|
|
|
// Clients will start playback at LastActivity (datetime) from PositionTicks (playback position).
|
|
|
|
// The added delay does not guarantee, of course, that the command will be received in time.
|
|
|
|
// Playback synchronization will mainly happen client side.
|
2020-11-13 14:13:32 +00:00
|
|
|
context.LastActivity = DateTime.UtcNow.AddMilliseconds(delayMillis);
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
|
2020-05-12 17:05:05 +00:00
|
|
|
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
2020-09-24 21:04:21 +00:00
|
|
|
|
2020-10-21 13:46:50 +00:00
|
|
|
// Notify relevant state change event.
|
2020-09-24 21:04:21 +00:00
|
|
|
SendGroupStateUpdate(context, request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Client got lost, sending current state.
|
2020-09-24 21:04:21 +00:00
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
|
2020-05-12 17:05:05 +00:00
|
|
|
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, PauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-13 14:13:32 +00:00
|
|
|
var pausedState = new PausedGroupState(Logger);
|
2020-05-12 17:05:05 +00:00
|
|
|
context.SetState(pausedState);
|
2020-11-13 14:13:32 +00:00
|
|
|
pausedState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, StopGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-13 14:13:32 +00:00
|
|
|
var idleState = new IdleGroupState(Logger);
|
2020-09-24 21:04:21 +00:00
|
|
|
context.SetState(idleState);
|
2020-11-13 14:13:32 +00:00
|
|
|
idleState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, SeekGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-13 14:13:32 +00:00
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
2020-09-24 21:04:21 +00:00
|
|
|
context.SetState(waitingState);
|
2020-11-13 14:13:32 +00:00
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, BufferGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
if (IgnoreBuffering)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-13 14:13:32 +00:00
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
2020-09-24 21:04:21 +00:00
|
|
|
context.SetState(waitingState);
|
2020-11-13 14:13:32 +00:00
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, ReadyGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
if (prevState.Equals(Type))
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Group was not waiting, make sure client has latest state.
|
2020-09-24 21:04:21 +00:00
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
|
|
|
|
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
|
|
|
}
|
2020-11-13 14:13:32 +00:00
|
|
|
else if (prevState.Equals(GroupStateType.Waiting))
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Notify relevant state change event.
|
2020-09-24 21:04:21 +00:00
|
|
|
SendGroupStateUpdate(context, request, session, cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, NextTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-13 14:13:32 +00:00
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
2020-09-24 21:04:21 +00:00
|
|
|
context.SetState(waitingState);
|
2020-11-13 14:13:32 +00:00
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, PreviousTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-13 14:13:32 +00:00
|
|
|
var waitingState = new WaitingGroupState(Logger);
|
2020-09-24 21:04:21 +00:00
|
|
|
context.SetState(waitingState);
|
2020-11-13 14:13:32 +00:00
|
|
|
waitingState.HandleRequest(context, Type, request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|