160 lines
7.3 KiB
C#
160 lines
7.3 KiB
C#
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>
|
|
/// Initializes a new instance of the <see cref="PausedGroupState"/> class.
|
|
/// </summary>
|
|
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
|
|
public PausedGroupState(ILogger logger)
|
|
: base(logger)
|
|
{
|
|
// Do nothing.
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override GroupStateType Type { get; } = GroupStateType.Paused;
|
|
|
|
/// <inheritdoc />
|
|
public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
|
{
|
|
// Wait for session to be ready.
|
|
var waitingState = new WaitingGroupState(Logger);
|
|
context.SetState(waitingState);
|
|
waitingState.SessionJoined(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 playingState = new PlayingGroupState(Logger);
|
|
context.SetState(playingState);
|
|
playingState.HandleRequest(context, Type, request, session, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, PauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
{
|
|
if (!prevState.Equals(Type))
|
|
{
|
|
// Pause group and compute the media playback position.
|
|
var currentTime = DateTime.UtcNow;
|
|
var elapsedTime = currentTime - context.LastActivity;
|
|
context.LastActivity = currentTime;
|
|
// 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.
|
|
// Seek only if playback actually started.
|
|
context.PositionTicks += Math.Max(elapsedTime.Ticks, 0);
|
|
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
|
|
|
// Notify relevant state change event.
|
|
SendGroupStateUpdate(context, request, session, cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
// Client got lost, sending current state.
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, StopGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
{
|
|
// Change state.
|
|
var idleState = new IdleGroupState(Logger);
|
|
context.SetState(idleState);
|
|
idleState.HandleRequest(context, Type, request, session, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void HandleRequest(IGroupStateContext context, GroupStateType prevState, SeekGroupRequest 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, BufferGroupRequest 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, ReadyGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
{
|
|
if (prevState.Equals(Type))
|
|
{
|
|
// Client got lost, sending current state.
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
|
}
|
|
else if (prevState.Equals(GroupStateType.Waiting))
|
|
{
|
|
// Sending current state to all clients.
|
|
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
|
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
|
|
|
// Notify relevant state change event.
|
|
SendGroupStateUpdate(context, request, 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);
|
|
}
|
|
}
|
|
}
|