2020-05-12 17:05:05 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Controller.Session;
|
2020-11-14 22:40:01 +00:00
|
|
|
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
2020-05-12 17:05:05 +00:00
|
|
|
using MediaBrowser.Model.SyncPlay;
|
2020-09-24 21:04:21 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-11-14 22:40:01 +00:00
|
|
|
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class PausedGroupState.
|
|
|
|
/// </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 PausedGroupState : AbstractGroupState
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-11-16 16:40:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The logger.
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogger<PausedGroupState> _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="PausedGroupState"/> 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>
|
|
|
|
public PausedGroupState(ILoggerFactory loggerFactory)
|
|
|
|
: base(loggerFactory)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-11-16 16:40:19 +00:00
|
|
|
_logger = LoggerFactory.CreateLogger<PausedGroupState>();
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:05:05 +00:00
|
|
|
/// <inheritdoc />
|
2020-11-14 17:09:25 +00:00
|
|
|
public override GroupStateType Type { get; } = GroupStateType.Paused;
|
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-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Wait for session to be ready.
|
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-11-13 14:13:32 +00:00
|
|
|
waitingState.SessionJoined(context, Type, session, cancellationToken);
|
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-16 16:40:19 +00:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
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-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-11-16 16:40:19 +00:00
|
|
|
var playingState = new PlayingGroupState(LoggerFactory);
|
2020-05-12 17:05:05 +00:00
|
|
|
context.SetState(playingState);
|
2020-11-13 14:13:32 +00:00
|
|
|
playingState.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, PauseGroupRequest 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-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Pause group and compute the media playback position.
|
2020-05-12 17:05:05 +00:00
|
|
|
var currentTime = DateTime.UtcNow;
|
2020-09-24 21:04:21 +00:00
|
|
|
var elapsedTime = currentTime - context.LastActivity;
|
|
|
|
context.LastActivity = currentTime;
|
2020-10-22 13:51:58 +00:00
|
|
|
// Elapsed time is negative if event happens
|
|
|
|
// during the delay added to account for latency.
|
|
|
|
// In this phase clients haven't started the playback yet.
|
|
|
|
// In other words, LastActivity is in the future,
|
|
|
|
// when playback unpause is supposed to happen.
|
2020-10-21 13:46:50 +00:00
|
|
|
// Seek only if playback actually started.
|
2020-10-21 14:42:57 +00:00
|
|
|
context.PositionTicks += Math.Max(elapsedTime.Ticks, 0);
|
2020-05-12 17:05:05 +00:00
|
|
|
|
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
|
|
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-05-12 17:05:05 +00:00
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
|
|
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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-16 16:40:19 +00:00
|
|
|
var idleState = new IdleGroupState(LoggerFactory);
|
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-16 16:40:19 +00:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
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, BufferGroupRequest 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-16 16:40:19 +00:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
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, ReadyGroupRequest 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
|
|
|
// Client got lost, sending current state.
|
2020-05-12 17:05:05 +00:00
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
|
|
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
|
|
|
// Sending current state to all clients.
|
2020-09-24 21:04:21 +00:00
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
|
|
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
2020-05-12 17:05:05 +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
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-11-13 14:13:32 +00:00
|
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, NextTrackGroupRequest 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-16 16:40:19 +00:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
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-16 16:40:19 +00:00
|
|
|
var waitingState = new WaitingGroupState(LoggerFactory);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|