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 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-09-24 21:04:21 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Default constructor.
|
|
|
|
/// </summary>
|
|
|
|
public PausedGroupState(ILogger logger) : base(logger)
|
|
|
|
{
|
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 />
|
|
|
|
public override GroupState GetGroupState()
|
|
|
|
{
|
|
|
|
return GroupState.Paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-09-24 21:04:21 +00:00
|
|
|
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.
|
2020-09-24 21:04:21 +00:00
|
|
|
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.
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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.
|
2020-09-24 21:04:21 +00:00
|
|
|
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-05-12 17:05:05 +00:00
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Change state.
|
2020-09-24 21:04:21 +00:00
|
|
|
var playingState = new PlayingGroupState(_logger);
|
2020-05-12 17:05:05 +00:00
|
|
|
context.SetState(playingState);
|
2020-09-24 21:04:21 +00:00
|
|
|
playingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-09-24 21:04:21 +00:00
|
|
|
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, PauseGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
2020-05-12 17:05:05 +00:00
|
|
|
{
|
2020-09-24 21:04:21 +00:00
|
|
|
if (!prevState.Equals(GetGroupState()))
|
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-21 13:46:50 +00:00
|
|
|
// Seek only if playback actually started.
|
|
|
|
// Pause request may be issued during the delay added to account for latency.
|
2020-09-24 21:04:21 +00:00
|
|
|
context.PositionTicks += elapsedTime.Ticks > 0 ? 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-09-24 21:04:21 +00:00
|
|
|
public override void HandleRequest(ISyncPlayStateContext context, GroupState 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-09-24 21:04:21 +00:00
|
|
|
var idleState = new IdleGroupState(_logger);
|
|
|
|
context.SetState(idleState);
|
|
|
|
idleState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-09-24 21:04:21 +00:00
|
|
|
public override void HandleRequest(ISyncPlayStateContext context, GroupState 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-09-24 21:04:21 +00:00
|
|
|
var waitingState = new WaitingGroupState(_logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
|
|
|
|
}
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
/// <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.
|
2020-09-24 21:04:21 +00:00
|
|
|
var waitingState = new WaitingGroupState(_logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
|
|
|
|
}
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public override void HandleRequest(ISyncPlayStateContext context, GroupState prevState, ReadyGroupRequest request, SessionInfo session, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (prevState.Equals(GetGroupState()))
|
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-09-24 21:04:21 +00:00
|
|
|
else if (prevState.Equals(GroupState.Waiting))
|
|
|
|
{
|
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-09-24 21:04:21 +00:00
|
|
|
public override void HandleRequest(ISyncPlayStateContext context, GroupState 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-09-24 21:04:21 +00:00
|
|
|
var waitingState = new WaitingGroupState(_logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
|
|
|
|
}
|
2020-05-12 17:05:05 +00:00
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
/// <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.
|
2020-09-24 21:04:21 +00:00
|
|
|
var waitingState = new WaitingGroupState(_logger);
|
|
|
|
context.SetState(waitingState);
|
|
|
|
waitingState.HandleRequest(context, GetGroupState(), request, session, cancellationToken);
|
2020-05-12 17:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|