jellyfin-server/Emby.Server.Implementations/SyncPlay/GroupStates/PausedGroupState.cs

162 lines
7.2 KiB
C#
Raw Normal View History

using System;
using System.Threading;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.SyncPlay;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Controller.SyncPlay
{
/// <summary>
/// Class PausedGroupState.
/// </summary>
/// <remarks>
/// Class is not thread-safe, external locking is required when accessing methods.
/// </remarks>
public class PausedGroupState : AbstractGroupState
{
/// <summary>
/// Default constructor.
/// </summary>
2020-10-22 13:51:58 +00:00
public PausedGroupState(ILogger logger)
: base(logger)
{
2020-10-21 13:46:50 +00:00
// Do nothing.
}
/// <inheritdoc />
public override GroupState GetGroupState()
{
return GroupState.Paused;
}
/// <inheritdoc />
public override void SessionJoined(ISyncPlayStateContext context, GroupState prevState, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Wait for session to be ready.
var waitingState = new WaitingGroupState(_logger);
context.SetState(waitingState);
waitingState.SessionJoined(context, GetGroupState(), session, cancellationToken);
}
/// <inheritdoc />
public override void SessionLeaving(ISyncPlayStateContext context, GroupState prevState, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Do nothing.
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, PlayGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Change state.
var waitingState = new WaitingGroupState(_logger);
context.SetState(waitingState);
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, UnpauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Change state.
var playingState = new PlayingGroupState(_logger);
context.SetState(playingState);
playingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, PauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
if (!prevState.Equals(GetGroupState()))
{
2020-10-21 13:46:50 +00:00
// Pause group and compute the media playback position.
var currentTime = DateTime.UtcNow;
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);
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
2020-10-21 13:46:50 +00:00
// Notify relevant state change event.
SendGroupStateUpdate(context, request, session, cancellationToken);
}
else
{
2020-10-21 13:46:50 +00:00
// Client got lost, sending current state.
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
}
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, StopGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Change state.
var idleState = new IdleGroupState(_logger);
context.SetState(idleState);
idleState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, SeekGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Change state.
var waitingState = new WaitingGroupState(_logger);
context.SetState(waitingState);
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, BufferGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Change state.
var waitingState = new WaitingGroupState(_logger);
context.SetState(waitingState);
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, ReadyGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
if (prevState.Equals(GetGroupState()))
{
2020-10-21 13:46:50 +00:00
// Client got lost, sending current state.
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
}
else if (prevState.Equals(GroupState.Waiting))
{
2020-10-21 13:46:50 +00:00
// Sending current state to all clients.
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
2020-10-21 13:46:50 +00:00
// Notify relevant state change event.
SendGroupStateUpdate(context, request, session, cancellationToken);
}
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, NextTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Change state.
var waitingState = new WaitingGroupState(_logger);
context.SetState(waitingState);
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
}
/// <inheritdoc />
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, PreviousTrackGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
{
2020-10-21 13:46:50 +00:00
// Change state.
var waitingState = new WaitingGroupState(_logger);
context.SetState(waitingState);
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
}
}
}