mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-20 17:14:42 +01:00
Merge pull request #3194 from OancaAndrei/syncplay-enhanced
SyncPlay for TV series (and Music)
This commit is contained in:
@@ -92,6 +92,9 @@ namespace MediaBrowser.Controller.Net
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task ProcessWebSocketConnectedAsync(IWebSocketConnection connection) => Task.CompletedTask;
|
||||
|
||||
/// <summary>
|
||||
/// Starts sending messages over a web socket.
|
||||
/// </summary>
|
||||
|
||||
@@ -3,7 +3,7 @@ using System.Threading.Tasks;
|
||||
namespace MediaBrowser.Controller.Net
|
||||
{
|
||||
/// <summary>
|
||||
///This is an interface for listening to messages coming through a web socket connection.
|
||||
/// Interface for listening to messages coming through a web socket connection.
|
||||
/// </summary>
|
||||
public interface IWebSocketListener
|
||||
{
|
||||
@@ -13,5 +13,12 @@ namespace MediaBrowser.Controller.Net
|
||||
/// <param name="message">The message.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task ProcessMessageAsync(WebSocketMessageInfo message);
|
||||
|
||||
/// <summary>
|
||||
/// Processes a new web socket connection.
|
||||
/// </summary>
|
||||
/// <param name="connection">An instance of the <see cref="IWebSocketConnection"/> interface.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task ProcessWebSocketConnectedAsync(IWebSocketConnection connection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Data.Events;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace MediaBrowser.Controller.Net
|
||||
@@ -11,11 +8,6 @@ namespace MediaBrowser.Controller.Net
|
||||
/// </summary>
|
||||
public interface IWebSocketManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Occurs when [web socket connected].
|
||||
/// </summary>
|
||||
event EventHandler<GenericEventArgs<IWebSocketConnection>> WebSocketConnected;
|
||||
|
||||
/// <summary>
|
||||
/// The HTTP request handler.
|
||||
/// </summary>
|
||||
|
||||
@@ -143,22 +143,22 @@ namespace MediaBrowser.Controller.Session
|
||||
Task SendPlayCommand(string controllingSessionId, string sessionId, PlayRequest command, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Sends the SyncPlayCommand.
|
||||
/// Sends a SyncPlayCommand to a session.
|
||||
/// </summary>
|
||||
/// <param name="sessionId">The session id.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="command">The command.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task SendSyncPlayCommand(string sessionId, SendCommand command, CancellationToken cancellationToken);
|
||||
Task SendSyncPlayCommand(SessionInfo session, SendCommand command, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Sends the SyncPlayGroupUpdate.
|
||||
/// Sends a SyncPlayGroupUpdate to a session.
|
||||
/// </summary>
|
||||
/// <param name="sessionId">The session id.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="command">The group update.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
Task SendSyncPlayGroupUpdate<T>(string sessionId, GroupUpdate<T> command, CancellationToken cancellationToken);
|
||||
Task SendSyncPlayGroupUpdate<T>(SessionInfo session, GroupUpdate<T> command, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Sends the browse command.
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Session;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay
|
||||
{
|
||||
/// <summary>
|
||||
/// Class GroupInfo.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Class is not thread-safe, external locking is required when accessing methods.
|
||||
/// </remarks>
|
||||
public class GroupInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// The default ping value used for sessions.
|
||||
/// </summary>
|
||||
public const long DefaultPing = 500;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the group identifier.
|
||||
/// </summary>
|
||||
/// <value>The group identifier.</value>
|
||||
public Guid GroupId { get; } = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the playing item.
|
||||
/// </summary>
|
||||
/// <value>The playing item.</value>
|
||||
public BaseItem PlayingItem { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether playback is paused.
|
||||
/// </summary>
|
||||
/// <value>Playback is paused.</value>
|
||||
public bool IsPaused { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether there are position ticks.
|
||||
/// </summary>
|
||||
/// <value>The position ticks.</value>
|
||||
public long PositionTicks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the last activity.
|
||||
/// </summary>
|
||||
/// <value>The last activity.</value>
|
||||
public DateTime LastActivity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the participants.
|
||||
/// </summary>
|
||||
/// <value>The participants, or members of the group.</value>
|
||||
public Dictionary<string, GroupMember> Participants { get; } =
|
||||
new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a session is in this group.
|
||||
/// </summary>
|
||||
/// <param name="sessionId">The session id to check.</param>
|
||||
/// <returns><c>true</c> if the session is in this group; <c>false</c> otherwise.</returns>
|
||||
public bool ContainsSession(string sessionId)
|
||||
{
|
||||
return Participants.ContainsKey(sessionId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the session to the group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
public void AddSession(SessionInfo session)
|
||||
{
|
||||
Participants.TryAdd(
|
||||
session.Id,
|
||||
new GroupMember
|
||||
{
|
||||
Session = session,
|
||||
Ping = DefaultPing,
|
||||
IsBuffering = false
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the session from the group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
public void RemoveSession(SessionInfo session)
|
||||
{
|
||||
Participants.Remove(session.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the ping of a session.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="ping">The ping.</param>
|
||||
public void UpdatePing(SessionInfo session, long ping)
|
||||
{
|
||||
if (Participants.TryGetValue(session.Id, out GroupMember value))
|
||||
{
|
||||
value.Ping = ping;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the highest ping in the group.
|
||||
/// </summary>
|
||||
/// <returns>The highest ping in the group.</returns>
|
||||
public long GetHighestPing()
|
||||
{
|
||||
long max = long.MinValue;
|
||||
foreach (var session in Participants.Values)
|
||||
{
|
||||
max = Math.Max(max, session.Ping);
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the session's buffering state.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="isBuffering">The state.</param>
|
||||
public void SetBuffering(SessionInfo session, bool isBuffering)
|
||||
{
|
||||
if (Participants.TryGetValue(session.Id, out GroupMember value))
|
||||
{
|
||||
value.IsBuffering = isBuffering;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the group buffering state.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</returns>
|
||||
public bool IsBuffering()
|
||||
{
|
||||
foreach (var session in Participants.Values)
|
||||
{
|
||||
if (session.IsBuffering)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the group is empty.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the group is empty; <c>false</c> otherwise.</returns>
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return Participants.Count == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,27 @@ namespace MediaBrowser.Controller.SyncPlay
|
||||
/// </summary>
|
||||
public class GroupMember
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GroupMember"/> class.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
public GroupMember(SessionInfo session)
|
||||
{
|
||||
Session = session;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the session.
|
||||
/// </summary>
|
||||
/// <value>The session.</value>
|
||||
public SessionInfo Session { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ping, in milliseconds.
|
||||
/// </summary>
|
||||
/// <value>The ping.</value>
|
||||
public long Ping { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this member is buffering.
|
||||
/// </summary>
|
||||
@@ -14,15 +35,9 @@ namespace MediaBrowser.Controller.SyncPlay
|
||||
public bool IsBuffering { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the session.
|
||||
/// Gets or sets a value indicating whether this member is following group playback.
|
||||
/// </summary>
|
||||
/// <value>The session.</value>
|
||||
public SessionInfo Session { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ping.
|
||||
/// </summary>
|
||||
/// <value>The ping.</value>
|
||||
public long Ping { get; set; }
|
||||
/// <value><c>true</c> to ignore member on group wait; <c>false</c> if they're following group playback.</value>
|
||||
public bool IgnoreGroupWait { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
||||
{
|
||||
/// <summary>
|
||||
/// Class AbstractGroupState.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Class is not thread-safe, external locking is required when accessing methods.
|
||||
/// </remarks>
|
||||
public abstract class AbstractGroupState : IGroupState
|
||||
{
|
||||
/// <summary>
|
||||
/// The logger.
|
||||
/// </summary>
|
||||
private readonly ILogger<AbstractGroupState> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractGroupState"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
||||
protected AbstractGroupState(ILoggerFactory loggerFactory)
|
||||
{
|
||||
LoggerFactory = loggerFactory;
|
||||
_logger = loggerFactory.CreateLogger<AbstractGroupState>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract GroupStateType Type { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the logger factory.
|
||||
/// </summary>
|
||||
protected ILoggerFactory LoggerFactory { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(IGroupPlaybackRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(SetPlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(RemoveFromPlaylistGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
var playingItemRemoved = context.RemoveFromPlayQueue(request.PlaylistItemIds);
|
||||
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.RemoveItems);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
|
||||
if (playingItemRemoved && !context.PlayQueue.IsItemPlaying())
|
||||
{
|
||||
_logger.LogDebug("Play queue in group {GroupId} is now empty.", context.GroupId.ToString());
|
||||
|
||||
IGroupState idleState = new IdleGroupState(LoggerFactory);
|
||||
context.SetState(idleState);
|
||||
var stopRequest = new StopGroupRequest();
|
||||
idleState.HandleRequest(stopRequest, context, Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(MovePlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = context.MoveItemInPlayQueue(request.PlaylistItemId, request.NewIndex);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
_logger.LogError("Unable to move item in group {GroupId}.", context.GroupId.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.MoveItem);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(QueueGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = context.AddToPlayQueue(request.ItemIds, request.Mode);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
_logger.LogError("Unable to add items to play queue in group {GroupId}.", context.GroupId.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
var reason = request.Mode switch
|
||||
{
|
||||
GroupQueueMode.QueueNext => PlayQueueUpdateReason.QueueNext,
|
||||
_ => PlayQueueUpdateReason.Queue
|
||||
};
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(reason);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
UnhandledRequest(request);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(SetRepeatModeGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
context.SetRepeatMode(request.Mode);
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.RepeatMode);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(SetShuffleModeGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
context.SetShuffleMode(request.Mode);
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.ShuffleMode);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(PingGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Collected pings are used to account for network latency when unpausing playback.
|
||||
context.UpdatePing(session, request.Ping);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void HandleRequest(IgnoreWaitGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
context.SetIgnoreGroupWait(session, request.IgnoreWait);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a group state update to all group.
|
||||
/// </summary>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="reason">The reason of the state change.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
protected void SendGroupStateUpdate(IGroupStateContext context, IGroupPlaybackRequest reason, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Notify relevant state change event.
|
||||
var stateUpdate = new GroupStateUpdate(Type, reason.Action);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.StateUpdate, stateUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
}
|
||||
|
||||
private void UnhandledRequest(IGroupPlaybackRequest request)
|
||||
{
|
||||
_logger.LogWarning("Unhandled request of type {RequestType} in {StateType} state.", request.Action, Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
126
MediaBrowser.Controller/SyncPlay/GroupStates/IdleGroupState.cs
Normal file
126
MediaBrowser.Controller/SyncPlay/GroupStates/IdleGroupState.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
||||
{
|
||||
/// <summary>
|
||||
/// Class IdleGroupState.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Class is not thread-safe, external locking is required when accessing methods.
|
||||
/// </remarks>
|
||||
public class IdleGroupState : AbstractGroupState
|
||||
{
|
||||
/// <summary>
|
||||
/// The logger.
|
||||
/// </summary>
|
||||
private readonly ILogger<IdleGroupState> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="IdleGroupState"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
||||
public IdleGroupState(ILoggerFactory loggerFactory)
|
||||
: base(loggerFactory)
|
||||
{
|
||||
_logger = LoggerFactory.CreateLogger<IdleGroupState>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override GroupStateType Type { get; } = GroupStateType.Idle;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
SendStopCommand(context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
SendStopCommand(context, prevState, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
SendStopCommand(context, prevState, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
SendStopCommand(context, prevState, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
SendStopCommand(context, prevState, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
SendStopCommand(context, prevState, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
private void SendStopCommand(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Stop);
|
||||
if (!prevState.Equals(Type))
|
||||
{
|
||||
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
165
MediaBrowser.Controller/SyncPlay/GroupStates/PausedGroupState.cs
Normal file
165
MediaBrowser.Controller/SyncPlay/GroupStates/PausedGroupState.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
||||
{
|
||||
/// <summary>
|
||||
/// Class PausedGroupState.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Class is not thread-safe, external locking is required when accessing methods.
|
||||
/// </remarks>
|
||||
public class PausedGroupState : AbstractGroupState
|
||||
{
|
||||
/// <summary>
|
||||
/// The logger.
|
||||
/// </summary>
|
||||
private readonly ILogger<PausedGroupState> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PausedGroupState"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
||||
public PausedGroupState(ILoggerFactory loggerFactory)
|
||||
: base(loggerFactory)
|
||||
{
|
||||
_logger = LoggerFactory.CreateLogger<PausedGroupState>();
|
||||
}
|
||||
|
||||
/// <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(LoggerFactory);
|
||||
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(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var playingState = new PlayingGroupState(LoggerFactory);
|
||||
context.SetState(playingState);
|
||||
playingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, 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(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var idleState = new IdleGroupState(LoggerFactory);
|
||||
context.SetState(idleState);
|
||||
idleState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, 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(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
||||
{
|
||||
/// <summary>
|
||||
/// Class PlayingGroupState.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Class is not thread-safe, external locking is required when accessing methods.
|
||||
/// </remarks>
|
||||
public class PlayingGroupState : AbstractGroupState
|
||||
{
|
||||
/// <summary>
|
||||
/// The logger.
|
||||
/// </summary>
|
||||
private readonly ILogger<PlayingGroupState> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PlayingGroupState"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
||||
public PlayingGroupState(ILoggerFactory loggerFactory)
|
||||
: base(loggerFactory)
|
||||
{
|
||||
_logger = LoggerFactory.CreateLogger<PlayingGroupState>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override GroupStateType Type { get; } = GroupStateType.Playing;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether requests for buffering should be ignored.
|
||||
/// </summary>
|
||||
public bool IgnoreBuffering { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Wait for session to be ready.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
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(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!prevState.Equals(Type))
|
||||
{
|
||||
// Pick a suitable time that accounts for latency.
|
||||
var delayMillis = Math.Max(context.GetHighestPing() * 2, context.DefaultPing);
|
||||
|
||||
// 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.
|
||||
context.LastActivity = DateTime.UtcNow.AddMilliseconds(delayMillis);
|
||||
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
|
||||
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.Unpause);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var pausedState = new PausedGroupState(LoggerFactory);
|
||||
context.SetState(pausedState);
|
||||
pausedState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var idleState = new IdleGroupState(LoggerFactory);
|
||||
context.SetState(idleState);
|
||||
idleState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
if (IgnoreBuffering)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
if (prevState.Equals(Type))
|
||||
{
|
||||
// Group was not waiting, make sure client has latest state.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
}
|
||||
else if (prevState.Equals(GroupStateType.Waiting))
|
||||
{
|
||||
// Notify relevant state change event.
|
||||
SendGroupStateUpdate(context, request, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Change state.
|
||||
var waitingState = new WaitingGroupState(LoggerFactory);
|
||||
context.SetState(waitingState);
|
||||
waitingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,680 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.GroupStates
|
||||
{
|
||||
/// <summary>
|
||||
/// Class WaitingGroupState.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Class is not thread-safe, external locking is required when accessing methods.
|
||||
/// </remarks>
|
||||
public class WaitingGroupState : AbstractGroupState
|
||||
{
|
||||
/// <summary>
|
||||
/// The logger.
|
||||
/// </summary>
|
||||
private readonly ILogger<WaitingGroupState> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WaitingGroupState"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
||||
public WaitingGroupState(ILoggerFactory loggerFactory)
|
||||
: base(loggerFactory)
|
||||
{
|
||||
_logger = LoggerFactory.CreateLogger<WaitingGroupState>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override GroupStateType Type { get; } = GroupStateType.Waiting;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether playback should resume when group is ready.
|
||||
/// </summary>
|
||||
public bool ResumePlaying { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the initial state has been set.
|
||||
/// </summary>
|
||||
private bool InitialStateSet { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the group state before the first ever event.
|
||||
/// </summary>
|
||||
private GroupStateType InitialState { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
if (prevState.Equals(GroupStateType.Playing))
|
||||
{
|
||||
ResumePlaying = true;
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Prepare new session.
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, update, cancellationToken);
|
||||
|
||||
context.SetBuffering(session, true);
|
||||
|
||||
// Send pause command to all non-buffering sessions.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.AllReady, command, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
context.SetBuffering(session, false);
|
||||
|
||||
if (!context.IsBuffering())
|
||||
{
|
||||
if (ResumePlaying)
|
||||
{
|
||||
_logger.LogDebug("Session {SessionId} left group {GroupId}, notifying others to resume.", session.Id, context.GroupId.ToString());
|
||||
|
||||
// Client, that was buffering, left the group.
|
||||
var playingState = new PlayingGroupState(LoggerFactory);
|
||||
context.SetState(playingState);
|
||||
var unpauseRequest = new UnpauseGroupRequest();
|
||||
playingState.HandleRequest(unpauseRequest, context, Type, session, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Session {SessionId} left group {GroupId}, returning to previous state.", session.Id, context.GroupId.ToString());
|
||||
|
||||
// Group is ready, returning to previous state.
|
||||
var pausedState = new PausedGroupState(LoggerFactory);
|
||||
context.SetState(pausedState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
ResumePlaying = true;
|
||||
|
||||
var setQueueStatus = context.SetPlayQueue(request.PlayingQueue, request.PlayingItemPosition, request.StartPositionTicks);
|
||||
if (!setQueueStatus)
|
||||
{
|
||||
_logger.LogError("Unable to set playing queue in group {GroupId}.", context.GroupId.ToString());
|
||||
|
||||
// Ignore request and return to previous state.
|
||||
IGroupState newState = prevState switch {
|
||||
GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
|
||||
GroupStateType.Paused => new PausedGroupState(LoggerFactory),
|
||||
_ => new IdleGroupState(LoggerFactory)
|
||||
};
|
||||
|
||||
context.SetState(newState);
|
||||
return;
|
||||
}
|
||||
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
|
||||
// Reset status of sessions and await for all Ready events.
|
||||
context.SetAllBuffering(true);
|
||||
|
||||
_logger.LogDebug("Session {SessionId} set a new play queue in group {GroupId}.", session.Id, context.GroupId.ToString());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(SetPlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
ResumePlaying = true;
|
||||
|
||||
var result = context.SetPlayingItem(request.PlaylistItemId);
|
||||
if (result)
|
||||
{
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
|
||||
// Reset status of sessions and await for all Ready events.
|
||||
context.SetAllBuffering(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Return to old state.
|
||||
IGroupState newState = prevState switch
|
||||
{
|
||||
GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
|
||||
GroupStateType.Paused => new PausedGroupState(LoggerFactory),
|
||||
_ => new IdleGroupState(LoggerFactory)
|
||||
};
|
||||
|
||||
context.SetState(newState);
|
||||
|
||||
_logger.LogDebug("Unable to change current playing item in group {GroupId}.", context.GroupId.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
if (prevState.Equals(GroupStateType.Idle))
|
||||
{
|
||||
ResumePlaying = true;
|
||||
context.RestartCurrentItem();
|
||||
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NewPlaylist);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
|
||||
// Reset status of sessions and await for all Ready events.
|
||||
context.SetAllBuffering(true);
|
||||
|
||||
_logger.LogDebug("Group {GroupId} is waiting for all ready events.", context.GroupId.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ResumePlaying)
|
||||
{
|
||||
_logger.LogDebug("Forcing the playback to start in group {GroupId}. Group-wait is disabled until next state change.", context.GroupId.ToString());
|
||||
|
||||
// An Unpause request is forcing the playback to start, ignoring sessions that are not ready.
|
||||
context.SetAllBuffering(false);
|
||||
|
||||
// Change state.
|
||||
var playingState = new PlayingGroupState(LoggerFactory)
|
||||
{
|
||||
IgnoreBuffering = true
|
||||
};
|
||||
context.SetState(playingState);
|
||||
playingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Group would have gone to paused state, now will go to playing state when ready.
|
||||
ResumePlaying = true;
|
||||
|
||||
// Notify relevant state change event.
|
||||
SendGroupStateUpdate(context, request, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
// Wait for sessions to be ready, then switch to paused state.
|
||||
ResumePlaying = false;
|
||||
|
||||
// Notify relevant state change event.
|
||||
SendGroupStateUpdate(context, request, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
// Change state.
|
||||
var idleState = new IdleGroupState(LoggerFactory);
|
||||
context.SetState(idleState);
|
||||
idleState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
if (prevState.Equals(GroupStateType.Playing))
|
||||
{
|
||||
ResumePlaying = true;
|
||||
}
|
||||
else if (prevState.Equals(GroupStateType.Paused))
|
||||
{
|
||||
ResumePlaying = false;
|
||||
}
|
||||
|
||||
// Sanitize PositionTicks.
|
||||
var ticks = context.SanitizePositionTicks(request.PositionTicks);
|
||||
|
||||
// Seek.
|
||||
context.PositionTicks = ticks;
|
||||
context.LastActivity = DateTime.UtcNow;
|
||||
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Seek);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
||||
|
||||
// Reset status of sessions and await for all Ready events.
|
||||
context.SetAllBuffering(true);
|
||||
|
||||
// Notify relevant state change event.
|
||||
SendGroupStateUpdate(context, request, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
// Make sure the client is playing the correct item.
|
||||
if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
|
||||
{
|
||||
_logger.LogDebug("Session {SessionId} reported wrong playlist item in group {GroupId}.", session.Id, context.GroupId.ToString());
|
||||
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem);
|
||||
var updateSession = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
|
||||
context.SetBuffering(session, true);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (prevState.Equals(GroupStateType.Playing))
|
||||
{
|
||||
// Resume playback when all ready.
|
||||
ResumePlaying = true;
|
||||
|
||||
context.SetBuffering(session, true);
|
||||
|
||||
// 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);
|
||||
|
||||
// Send pause command to all non-buffering sessions.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.AllReady, command, cancellationToken);
|
||||
}
|
||||
else if (prevState.Equals(GroupStateType.Paused))
|
||||
{
|
||||
// Don't resume playback when all ready.
|
||||
ResumePlaying = false;
|
||||
|
||||
context.SetBuffering(session, true);
|
||||
|
||||
// Send pause command to buffering session.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
}
|
||||
else if (prevState.Equals(GroupStateType.Waiting))
|
||||
{
|
||||
// Another session is now buffering.
|
||||
context.SetBuffering(session, true);
|
||||
|
||||
if (!ResumePlaying)
|
||||
{
|
||||
// Force update for this session that should be paused.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
// Notify relevant state change event.
|
||||
SendGroupStateUpdate(context, request, session, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
// Make sure the client is playing the correct item.
|
||||
if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
|
||||
{
|
||||
_logger.LogDebug("Session {SessionId} reported wrong playlist item in group {GroupId}.", session.Id, context.GroupId.ToString());
|
||||
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.SetCurrentItem);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, update, cancellationToken);
|
||||
context.SetBuffering(session, true);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute elapsed time between the client reported time and now.
|
||||
// Elapsed time is used to estimate the client position when playback is unpaused.
|
||||
// Ideally, the request is received and handled without major delays.
|
||||
// However, to avoid waiting indefinitely when a client is not reporting a correct time,
|
||||
// the elapsed time is ignored after a certain threshold.
|
||||
var currentTime = DateTime.UtcNow;
|
||||
var elapsedTime = currentTime.Subtract(request.When);
|
||||
var timeSyncThresholdTicks = TimeSpan.FromMilliseconds(context.TimeSyncOffset).Ticks;
|
||||
if (Math.Abs(elapsedTime.Ticks) > timeSyncThresholdTicks)
|
||||
{
|
||||
_logger.LogWarning("Session {SessionId} is not time syncing properly. Ignoring elapsed time.", session.Id);
|
||||
|
||||
elapsedTime = TimeSpan.Zero;
|
||||
}
|
||||
|
||||
// Ignore elapsed time if client is paused.
|
||||
if (!request.IsPlaying)
|
||||
{
|
||||
elapsedTime = TimeSpan.Zero;
|
||||
}
|
||||
|
||||
var requestTicks = context.SanitizePositionTicks(request.PositionTicks);
|
||||
var clientPosition = TimeSpan.FromTicks(requestTicks) + elapsedTime;
|
||||
var delayTicks = context.PositionTicks - clientPosition.Ticks;
|
||||
var maxPlaybackOffsetTicks = TimeSpan.FromMilliseconds(context.MaxPlaybackOffset).Ticks;
|
||||
|
||||
_logger.LogDebug("Session {SessionId} is at {PositionTicks} (delay of {Delay} seconds) in group {GroupId}.", session.Id, clientPosition, TimeSpan.FromTicks(delayTicks).TotalSeconds, context.GroupId.ToString());
|
||||
|
||||
if (ResumePlaying)
|
||||
{
|
||||
// Handle case where session reported as ready but in reality
|
||||
// it has no clue of the real position nor the playback state.
|
||||
if (!request.IsPlaying && Math.Abs(delayTicks) > maxPlaybackOffsetTicks)
|
||||
{
|
||||
// Session not ready at all.
|
||||
context.SetBuffering(session, true);
|
||||
|
||||
// Correcting session's position.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Seek);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
|
||||
// Notify relevant state change event.
|
||||
SendGroupStateUpdate(context, request, session, cancellationToken);
|
||||
|
||||
_logger.LogWarning("Session {SessionId} got lost in time, correcting.", session.Id);
|
||||
return;
|
||||
}
|
||||
|
||||
// Session is ready.
|
||||
context.SetBuffering(session, false);
|
||||
|
||||
if (context.IsBuffering())
|
||||
{
|
||||
// Others are still buffering, tell this client to pause when ready.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Pause);
|
||||
command.When = currentTime.AddTicks(delayTicks);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
|
||||
_logger.LogInformation("Session {SessionId} will pause when ready in {Delay} seconds. Group {GroupId} is waiting for all ready events.", session.Id, TimeSpan.FromTicks(delayTicks).TotalSeconds, context.GroupId.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
// If all ready, then start playback.
|
||||
// Let other clients resume as soon as the buffering client catches up.
|
||||
if (delayTicks > context.GetHighestPing() * 2 * TimeSpan.TicksPerMillisecond)
|
||||
{
|
||||
// Client that was buffering is recovering, notifying others to resume.
|
||||
context.LastActivity = currentTime.AddTicks(delayTicks);
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
|
||||
var filter = SyncPlayBroadcastType.AllExceptCurrentSession;
|
||||
if (!request.IsPlaying)
|
||||
{
|
||||
filter = SyncPlayBroadcastType.AllGroup;
|
||||
}
|
||||
|
||||
context.SendCommand(session, filter, command, cancellationToken);
|
||||
|
||||
_logger.LogInformation("Session {SessionId} is recovering, group {GroupId} will resume in {Delay} seconds.", session.Id, context.GroupId.ToString(), TimeSpan.FromTicks(delayTicks).TotalSeconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Client, that was buffering, resumed playback but did not update others in time.
|
||||
delayTicks = context.GetHighestPing() * 2 * TimeSpan.TicksPerMillisecond;
|
||||
delayTicks = Math.Max(delayTicks, context.DefaultPing);
|
||||
|
||||
context.LastActivity = currentTime.AddTicks(delayTicks);
|
||||
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Unpause);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.AllGroup, command, cancellationToken);
|
||||
|
||||
_logger.LogWarning("Session {SessionId} resumed playback, group {GroupId} has {Delay} seconds to recover.", session.Id, context.GroupId.ToString(), TimeSpan.FromTicks(delayTicks).TotalSeconds);
|
||||
}
|
||||
|
||||
// Change state.
|
||||
var playingState = new PlayingGroupState(LoggerFactory);
|
||||
context.SetState(playingState);
|
||||
playingState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check that session is really ready, tolerate player imperfections under a certain threshold.
|
||||
if (Math.Abs(context.PositionTicks - requestTicks) > maxPlaybackOffsetTicks)
|
||||
{
|
||||
// Session still not ready.
|
||||
context.SetBuffering(session, true);
|
||||
// Session is seeking to wrong position, correcting.
|
||||
var command = context.NewSyncPlayCommand(SendCommandType.Seek);
|
||||
context.SendCommand(session, SyncPlayBroadcastType.CurrentSession, command, cancellationToken);
|
||||
|
||||
// Notify relevant state change event.
|
||||
SendGroupStateUpdate(context, request, session, cancellationToken);
|
||||
|
||||
_logger.LogWarning("Session {SessionId} is seeking to wrong position, correcting.", session.Id);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Session is ready.
|
||||
context.SetBuffering(session, false);
|
||||
}
|
||||
|
||||
if (!context.IsBuffering())
|
||||
{
|
||||
_logger.LogDebug("Session {SessionId} is ready, group {GroupId} is ready.", session.Id, context.GroupId.ToString());
|
||||
|
||||
// Group is ready, returning to previous state.
|
||||
var pausedState = new PausedGroupState(LoggerFactory);
|
||||
context.SetState(pausedState);
|
||||
|
||||
if (InitialState.Equals(GroupStateType.Playing))
|
||||
{
|
||||
// Group went from playing to waiting state and a pause request occured while waiting.
|
||||
var pauseRequest = new PauseGroupRequest();
|
||||
pausedState.HandleRequest(pauseRequest, context, Type, session, cancellationToken);
|
||||
}
|
||||
else if (InitialState.Equals(GroupStateType.Paused))
|
||||
{
|
||||
pausedState.HandleRequest(request, context, Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
ResumePlaying = true;
|
||||
|
||||
// Make sure the client knows the playing item, to avoid duplicate requests.
|
||||
if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
|
||||
{
|
||||
_logger.LogDebug("Session {SessionId} provided the wrong playlist item for group {GroupId}.", session.Id, context.GroupId.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
var newItem = context.NextItemInQueue();
|
||||
if (newItem)
|
||||
{
|
||||
// Send playing-queue update.
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.NextItem);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
|
||||
// Reset status of sessions and await for all Ready events.
|
||||
context.SetAllBuffering(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Return to old state.
|
||||
IGroupState newState = prevState switch
|
||||
{
|
||||
GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
|
||||
GroupStateType.Paused => new PausedGroupState(LoggerFactory),
|
||||
_ => new IdleGroupState(LoggerFactory)
|
||||
};
|
||||
|
||||
context.SetState(newState);
|
||||
|
||||
_logger.LogDebug("No next item available in group {GroupId}.", context.GroupId.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
// Save state if first event.
|
||||
if (!InitialStateSet)
|
||||
{
|
||||
InitialState = prevState;
|
||||
InitialStateSet = true;
|
||||
}
|
||||
|
||||
ResumePlaying = true;
|
||||
|
||||
// Make sure the client knows the playing item, to avoid duplicate requests.
|
||||
if (!request.PlaylistItemId.Equals(context.PlayQueue.GetPlayingItemPlaylistId()))
|
||||
{
|
||||
_logger.LogDebug("Session {SessionId} provided the wrong playlist item for group {GroupId}.", session.Id, context.GroupId.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
var newItem = context.PreviousItemInQueue();
|
||||
if (newItem)
|
||||
{
|
||||
// Send playing-queue update.
|
||||
var playQueueUpdate = context.GetPlayQueueUpdate(PlayQueueUpdateReason.PreviousItem);
|
||||
var update = context.NewSyncPlayGroupUpdate(GroupUpdateType.PlayQueue, playQueueUpdate);
|
||||
context.SendGroupUpdate(session, SyncPlayBroadcastType.AllGroup, update, cancellationToken);
|
||||
|
||||
// Reset status of sessions and await for all Ready events.
|
||||
context.SetAllBuffering(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Return to old state.
|
||||
IGroupState newState = prevState switch
|
||||
{
|
||||
GroupStateType.Playing => new PlayingGroupState(LoggerFactory),
|
||||
GroupStateType.Paused => new PausedGroupState(LoggerFactory),
|
||||
_ => new IdleGroupState(LoggerFactory)
|
||||
};
|
||||
|
||||
context.SetState(newState);
|
||||
|
||||
_logger.LogDebug("No previous item available in group {GroupId}.", context.GroupId.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void HandleRequest(IgnoreWaitGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
context.SetIgnoreGroupWait(session, request.IgnoreWait);
|
||||
|
||||
if (!context.IsBuffering())
|
||||
{
|
||||
_logger.LogDebug("Ignoring session {SessionId}, group {GroupId} is ready.", session.Id, context.GroupId.ToString());
|
||||
|
||||
if (ResumePlaying)
|
||||
{
|
||||
// Client, that was buffering, stopped following playback.
|
||||
var playingState = new PlayingGroupState(LoggerFactory);
|
||||
context.SetState(playingState);
|
||||
var unpauseRequest = new UnpauseGroupRequest();
|
||||
playingState.HandleRequest(unpauseRequest, context, Type, session, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Group is ready, returning to previous state.
|
||||
var pausedState = new PausedGroupState(LoggerFactory);
|
||||
context.SetState(pausedState);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
MediaBrowser.Controller/SyncPlay/IGroupPlaybackRequest.cs
Normal file
27
MediaBrowser.Controller/SyncPlay/IGroupPlaybackRequest.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface IGroupPlaybackRequest.
|
||||
/// </summary>
|
||||
public interface IGroupPlaybackRequest : ISyncPlayRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the playback request type.
|
||||
/// </summary>
|
||||
/// <returns>The playback request type.</returns>
|
||||
PlaybackRequestType Action { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Applies the request to a group.
|
||||
/// </summary>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="state">The current state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
217
MediaBrowser.Controller/SyncPlay/IGroupState.cs
Normal file
217
MediaBrowser.Controller/SyncPlay/IGroupState.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.PlaybackRequests;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface IGroupState.
|
||||
/// </summary>
|
||||
public interface IGroupState
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the group state type.
|
||||
/// </summary>
|
||||
/// <value>The group state type.</value>
|
||||
GroupStateType Type { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Handles a session that joined the group.
|
||||
/// </summary>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void SessionJoined(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a session that is leaving the group.
|
||||
/// </summary>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void SessionLeaving(IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Generic handler. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The generic request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(IGroupPlaybackRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a play request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The play request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(PlayGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a set-playlist-item request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The set-playlist-item request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(SetPlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a remove-items request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The remove-items request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(RemoveFromPlaylistGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a move-playlist-item request from a session. Context's state should not change.
|
||||
/// </summary>
|
||||
/// <param name="request">The move-playlist-item request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(MovePlaylistItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a queue request from a session. Context's state should not change.
|
||||
/// </summary>
|
||||
/// <param name="request">The queue request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(QueueGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles an unpause request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The unpause request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(UnpauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a pause request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The pause request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(PauseGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a stop request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The stop request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(StopGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a seek request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The seek request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(SeekGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a buffer request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The buffer request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(BufferGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a ready request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The ready request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(ReadyGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a next-item request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The next-item request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(NextItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a previous-item request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The previous-item request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(PreviousItemGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a set-repeat-mode request from a session. Context's state should not change.
|
||||
/// </summary>
|
||||
/// <param name="request">The repeat-mode request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(SetRepeatModeGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a set-shuffle-mode request from a session. Context's state should not change.
|
||||
/// </summary>
|
||||
/// <param name="request">The shuffle-mode request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(SetShuffleModeGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the ping of a session. Context's state should not change.
|
||||
/// </summary>
|
||||
/// <param name="request">The ping request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(PingGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles a ignore-wait request from a session. Context's state can change.
|
||||
/// </summary>
|
||||
/// <param name="request">The ignore-wait request.</param>
|
||||
/// <param name="context">The context of the state.</param>
|
||||
/// <param name="prevState">The previous state.</param>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(IgnoreWaitGroupRequest request, IGroupStateContext context, GroupStateType prevState, SessionInfo session, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
222
MediaBrowser.Controller/SyncPlay/IGroupStateContext.cs
Normal file
222
MediaBrowser.Controller/SyncPlay/IGroupStateContext.cs
Normal file
@@ -0,0 +1,222 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.Queue;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface IGroupStateContext.
|
||||
/// </summary>
|
||||
public interface IGroupStateContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the default ping value used for sessions, in milliseconds.
|
||||
/// </summary>
|
||||
/// <value>The default ping value used for sessions, in milliseconds.</value>
|
||||
long DefaultPing { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum time offset error accepted for dates reported by clients, in milliseconds.
|
||||
/// </summary>
|
||||
/// <value>The maximum offset error accepted, in milliseconds.</value>
|
||||
long TimeSyncOffset { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the maximum offset error accepted for position reported by clients, in milliseconds.
|
||||
/// </summary>
|
||||
/// <value>The maximum offset error accepted, in milliseconds.</value>
|
||||
long MaxPlaybackOffset { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the group identifier.
|
||||
/// </summary>
|
||||
/// <value>The group identifier.</value>
|
||||
Guid GroupId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the position ticks.
|
||||
/// </summary>
|
||||
/// <value>The position ticks.</value>
|
||||
long PositionTicks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the last activity.
|
||||
/// </summary>
|
||||
/// <value>The last activity.</value>
|
||||
DateTime LastActivity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the play queue.
|
||||
/// </summary>
|
||||
/// <value>The play queue.</value>
|
||||
PlayQueueManager PlayQueue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new state.
|
||||
/// </summary>
|
||||
/// <param name="state">The new state.</param>
|
||||
void SetState(IGroupState state);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a GroupUpdate message to the interested sessions.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the data of the message.</typeparam>
|
||||
/// <param name="from">The current session.</param>
|
||||
/// <param name="type">The filtering type.</param>
|
||||
/// <param name="message">The message to send.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The task.</returns>
|
||||
Task SendGroupUpdate<T>(SessionInfo from, SyncPlayBroadcastType type, GroupUpdate<T> message, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a playback command to the interested sessions.
|
||||
/// </summary>
|
||||
/// <param name="from">The current session.</param>
|
||||
/// <param name="type">The filtering type.</param>
|
||||
/// <param name="message">The message to send.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The task.</returns>
|
||||
Task SendCommand(SessionInfo from, SyncPlayBroadcastType type, SendCommand message, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Builds a new playback command with some default values.
|
||||
/// </summary>
|
||||
/// <param name="type">The command type.</param>
|
||||
/// <returns>The command.</returns>
|
||||
SendCommand NewSyncPlayCommand(SendCommandType type);
|
||||
|
||||
/// <summary>
|
||||
/// Builds a new group update message.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the data of the message.</typeparam>
|
||||
/// <param name="type">The update type.</param>
|
||||
/// <param name="data">The data to send.</param>
|
||||
/// <returns>The group update.</returns>
|
||||
GroupUpdate<T> NewSyncPlayGroupUpdate<T>(GroupUpdateType type, T data);
|
||||
|
||||
/// <summary>
|
||||
/// Sanitizes the PositionTicks, considers the current playing item when available.
|
||||
/// </summary>
|
||||
/// <param name="positionTicks">The PositionTicks.</param>
|
||||
/// <returns>The sanitized position ticks.</returns>
|
||||
long SanitizePositionTicks(long? positionTicks);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the ping of a session, in milliseconds.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="ping">The ping, in milliseconds.</param>
|
||||
void UpdatePing(SessionInfo session, long ping);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the highest ping in the group, in milliseconds.
|
||||
/// </summary>
|
||||
/// <returns>The highest ping in the group.</returns>
|
||||
long GetHighestPing();
|
||||
|
||||
/// <summary>
|
||||
/// Sets the session's buffering state.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="isBuffering">The state.</param>
|
||||
void SetBuffering(SessionInfo session, bool isBuffering);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the buffering state of all the sessions.
|
||||
/// </summary>
|
||||
/// <param name="isBuffering">The state.</param>
|
||||
void SetAllBuffering(bool isBuffering);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the group buffering state.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</returns>
|
||||
bool IsBuffering();
|
||||
|
||||
/// <summary>
|
||||
/// Sets the session's group wait state.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="ignoreGroupWait">The state.</param>
|
||||
void SetIgnoreGroupWait(SessionInfo session, bool ignoreGroupWait);
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new play queue.
|
||||
/// </summary>
|
||||
/// <param name="playQueue">The new play queue.</param>
|
||||
/// <param name="playingItemPosition">The playing item position in the play queue.</param>
|
||||
/// <param name="startPositionTicks">The start position ticks.</param>
|
||||
/// <returns><c>true</c> if the play queue has been changed; <c>false</c> if something went wrong.</returns>
|
||||
bool SetPlayQueue(IReadOnlyList<Guid> playQueue, int playingItemPosition, long startPositionTicks);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the playing item.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The new playing item identifier.</param>
|
||||
/// <returns><c>true</c> if the play queue has been changed; <c>false</c> if something went wrong.</returns>
|
||||
bool SetPlayingItem(Guid playlistItemId);
|
||||
|
||||
/// <summary>
|
||||
/// Removes items from the play queue.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemIds">The items to remove.</param>
|
||||
/// <returns><c>true</c> if playing item got removed; <c>false</c> otherwise.</returns>
|
||||
bool RemoveFromPlayQueue(IReadOnlyList<Guid> playlistItemIds);
|
||||
|
||||
/// <summary>
|
||||
/// Moves an item in the play queue.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The playlist identifier of the item to move.</param>
|
||||
/// <param name="newIndex">The new position.</param>
|
||||
/// <returns><c>true</c> if item has been moved; <c>false</c> if something went wrong.</returns>
|
||||
bool MoveItemInPlayQueue(Guid playlistItemId, int newIndex);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the play queue.
|
||||
/// </summary>
|
||||
/// <param name="newItems">The new items to add to the play queue.</param>
|
||||
/// <param name="mode">The mode with which the items will be added.</param>
|
||||
/// <returns><c>true</c> if the play queue has been changed; <c>false</c> if something went wrong.</returns>
|
||||
bool AddToPlayQueue(IReadOnlyList<Guid> newItems, GroupQueueMode mode);
|
||||
|
||||
/// <summary>
|
||||
/// Restarts current item in play queue.
|
||||
/// </summary>
|
||||
void RestartCurrentItem();
|
||||
|
||||
/// <summary>
|
||||
/// Picks next item in play queue.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the item changed; <c>false</c> otherwise.</returns>
|
||||
bool NextItemInQueue();
|
||||
|
||||
/// <summary>
|
||||
/// Picks previous item in play queue.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the item changed; <c>false</c> otherwise.</returns>
|
||||
bool PreviousItemInQueue();
|
||||
|
||||
/// <summary>
|
||||
/// Sets the repeat mode.
|
||||
/// </summary>
|
||||
/// <param name="mode">The new mode.</param>
|
||||
void SetRepeatMode(GroupRepeatMode mode);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the shuffle mode.
|
||||
/// </summary>
|
||||
/// <param name="mode">The new mode.</param>
|
||||
void SetShuffleMode(GroupShuffleMode mode);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a play queue update.
|
||||
/// </summary>
|
||||
/// <param name="reason">The reason for the update.</param>
|
||||
/// <returns>The play queue update.</returns>
|
||||
PlayQueueUpdate GetPlayQueueUpdate(PlayQueueUpdateReason reason);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface ISyncPlayController.
|
||||
/// </summary>
|
||||
public interface ISyncPlayController
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the group id.
|
||||
/// </summary>
|
||||
/// <value>The group id.</value>
|
||||
Guid GetGroupId();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playing item id.
|
||||
/// </summary>
|
||||
/// <value>The playing item id.</value>
|
||||
Guid GetPlayingItemId();
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the group is empty.
|
||||
/// </summary>
|
||||
/// <value>If the group is empty.</value>
|
||||
bool IsGroupEmpty();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the group with the session's info.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void CreateGroup(SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Adds the session to the group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="request">The request.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the session from the group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void SessionLeave(SessionInfo session, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Handles the requested action by the session.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="request">The requested action.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the info about the group for the clients.
|
||||
/// </summary>
|
||||
/// <value>The group info for the clients.</value>
|
||||
GroupInfoView GetInfo();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Controller.SyncPlay.Requests;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay
|
||||
@@ -15,32 +16,33 @@ namespace MediaBrowser.Controller.SyncPlay
|
||||
/// Creates a new group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session that's creating the group.</param>
|
||||
/// <param name="request">The request.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void NewGroup(SessionInfo session, CancellationToken cancellationToken);
|
||||
void NewGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Adds the session to a group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="groupId">The group id.</param>
|
||||
/// <param name="request">The request.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void JoinGroup(SessionInfo session, Guid groupId, JoinGroupRequest request, CancellationToken cancellationToken);
|
||||
void JoinGroup(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the session from a group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="request">The request.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void LeaveGroup(SessionInfo session, CancellationToken cancellationToken);
|
||||
void LeaveGroup(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Gets list of available groups for a session.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="filterItemId">The item id to filter by.</param>
|
||||
/// <value>The list of available groups.</value>
|
||||
List<GroupInfoView> ListGroups(SessionInfo session, Guid filterItemId);
|
||||
/// <param name="request">The request.</param>
|
||||
/// <returns>The list of available groups.</returns>
|
||||
List<GroupInfoDto> ListGroups(SessionInfo session, ListGroupsRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// Handle a request by a session in a group.
|
||||
@@ -48,22 +50,6 @@ namespace MediaBrowser.Controller.SyncPlay
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="request">The request.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
void HandleRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Maps a session to a group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
void AddSessionToGroup(SessionInfo session, ISyncPlayController group);
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a session from a group.
|
||||
/// </summary>
|
||||
/// <param name="session">The session.</param>
|
||||
/// <param name="group">The group.</param>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
void RemoveSessionFromGroup(SessionInfo session, ISyncPlayController group);
|
||||
void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
16
MediaBrowser.Controller/SyncPlay/ISyncPlayRequest.cs
Normal file
16
MediaBrowser.Controller/SyncPlay/ISyncPlayRequest.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface ISyncPlayRequest.
|
||||
/// </summary>
|
||||
public interface ISyncPlayRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the request type.
|
||||
/// </summary>
|
||||
/// <returns>The request type.</returns>
|
||||
RequestType Type { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class AbstractPlaybackRequest.
|
||||
/// </summary>
|
||||
public abstract class AbstractPlaybackRequest : IGroupPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractPlaybackRequest"/> class.
|
||||
/// </summary>
|
||||
protected AbstractPlaybackRequest()
|
||||
{
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public RequestType Type { get; } = RequestType.Playback;
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract PlaybackRequestType Action { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class BufferGroupRequest.
|
||||
/// </summary>
|
||||
public class BufferGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BufferGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="when">When the request has been made, as reported by the client.</param>
|
||||
/// <param name="positionTicks">The position ticks.</param>
|
||||
/// <param name="isPlaying">Whether the client playback is unpaused.</param>
|
||||
/// <param name="playlistItemId">The playlist item identifier of the playing item.</param>
|
||||
public BufferGroupRequest(DateTime when, long positionTicks, bool isPlaying, Guid playlistItemId)
|
||||
{
|
||||
When = when;
|
||||
PositionTicks = positionTicks;
|
||||
IsPlaying = isPlaying;
|
||||
PlaylistItemId = playlistItemId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets when the request has been made by the client.
|
||||
/// </summary>
|
||||
/// <value>The date of the request.</value>
|
||||
public DateTime When { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the position ticks.
|
||||
/// </summary>
|
||||
/// <value>The position ticks.</value>
|
||||
public long PositionTicks { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the client playback is unpaused.
|
||||
/// </summary>
|
||||
/// <value>The client playback status.</value>
|
||||
public bool IsPlaying { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playlist item identifier of the playing item.
|
||||
/// </summary>
|
||||
/// <value>The playlist item identifier.</value>
|
||||
public Guid PlaylistItemId { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Buffer;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class IgnoreWaitGroupRequest.
|
||||
/// </summary>
|
||||
public class IgnoreWaitGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="IgnoreWaitGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="ignoreWait">Whether the client should be ignored.</param>
|
||||
public IgnoreWaitGroupRequest(bool ignoreWait)
|
||||
{
|
||||
IgnoreWait = ignoreWait;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the client should be ignored.
|
||||
/// </summary>
|
||||
/// <value>The client group-wait status.</value>
|
||||
public bool IgnoreWait { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.IgnoreWait;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class MovePlaylistItemGroupRequest.
|
||||
/// </summary>
|
||||
public class MovePlaylistItemGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MovePlaylistItemGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The playlist identifier of the item.</param>
|
||||
/// <param name="newIndex">The new position.</param>
|
||||
public MovePlaylistItemGroupRequest(Guid playlistItemId, int newIndex)
|
||||
{
|
||||
PlaylistItemId = playlistItemId;
|
||||
NewIndex = newIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playlist identifier of the item.
|
||||
/// </summary>
|
||||
/// <value>The playlist identifier of the item.</value>
|
||||
public Guid PlaylistItemId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the new position.
|
||||
/// </summary>
|
||||
/// <value>The new position.</value>
|
||||
public int NewIndex { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.MovePlaylistItem;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class NextItemGroupRequest.
|
||||
/// </summary>
|
||||
public class NextItemGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="NextItemGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The playing item identifier.</param>
|
||||
public NextItemGroupRequest(Guid playlistItemId)
|
||||
{
|
||||
PlaylistItemId = playlistItemId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playing item identifier.
|
||||
/// </summary>
|
||||
/// <value>The playing item identifier.</value>
|
||||
public Guid PlaylistItemId { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.NextItem;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class PauseGroupRequest.
|
||||
/// </summary>
|
||||
public class PauseGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Pause;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class PingGroupRequest.
|
||||
/// </summary>
|
||||
public class PingGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PingGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="ping">The ping time.</param>
|
||||
public PingGroupRequest(long ping)
|
||||
{
|
||||
Ping = ping;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ping time.
|
||||
/// </summary>
|
||||
/// <value>The ping time.</value>
|
||||
public long Ping { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Ping;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class PlayGroupRequest.
|
||||
/// </summary>
|
||||
public class PlayGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PlayGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="playingQueue">The playing queue.</param>
|
||||
/// <param name="playingItemPosition">The playing item position.</param>
|
||||
/// <param name="startPositionTicks">The start position ticks.</param>
|
||||
public PlayGroupRequest(IReadOnlyList<Guid> playingQueue, int playingItemPosition, long startPositionTicks)
|
||||
{
|
||||
PlayingQueue = playingQueue ?? Array.Empty<Guid>();
|
||||
PlayingItemPosition = playingItemPosition;
|
||||
StartPositionTicks = startPositionTicks;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playing queue.
|
||||
/// </summary>
|
||||
/// <value>The playing queue.</value>
|
||||
public IReadOnlyList<Guid> PlayingQueue { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the position of the playing item in the queue.
|
||||
/// </summary>
|
||||
/// <value>The playing item position.</value>
|
||||
public int PlayingItemPosition { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the start position ticks.
|
||||
/// </summary>
|
||||
/// <value>The start position ticks.</value>
|
||||
public long StartPositionTicks { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Play;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class PreviousItemGroupRequest.
|
||||
/// </summary>
|
||||
public class PreviousItemGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PreviousItemGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The playing item identifier.</param>
|
||||
public PreviousItemGroupRequest(Guid playlistItemId)
|
||||
{
|
||||
PlaylistItemId = playlistItemId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playing item identifier.
|
||||
/// </summary>
|
||||
/// <value>The playing item identifier.</value>
|
||||
public Guid PlaylistItemId { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.PreviousItem;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class QueueGroupRequest.
|
||||
/// </summary>
|
||||
public class QueueGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="QueueGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="items">The items to add to the queue.</param>
|
||||
/// <param name="mode">The enqueue mode.</param>
|
||||
public QueueGroupRequest(IReadOnlyList<Guid> items, GroupQueueMode mode)
|
||||
{
|
||||
ItemIds = items ?? Array.Empty<Guid>();
|
||||
Mode = mode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the items to enqueue.
|
||||
/// </summary>
|
||||
/// <value>The items to enqueue.</value>
|
||||
public IReadOnlyList<Guid> ItemIds { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mode in which to add the new items.
|
||||
/// </summary>
|
||||
/// <value>The enqueue mode.</value>
|
||||
public GroupQueueMode Mode { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Queue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class ReadyGroupRequest.
|
||||
/// </summary>
|
||||
public class ReadyGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReadyGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="when">When the request has been made, as reported by the client.</param>
|
||||
/// <param name="positionTicks">The position ticks.</param>
|
||||
/// <param name="isPlaying">Whether the client playback is unpaused.</param>
|
||||
/// <param name="playlistItemId">The playlist item identifier of the playing item.</param>
|
||||
public ReadyGroupRequest(DateTime when, long positionTicks, bool isPlaying, Guid playlistItemId)
|
||||
{
|
||||
When = when;
|
||||
PositionTicks = positionTicks;
|
||||
IsPlaying = isPlaying;
|
||||
PlaylistItemId = playlistItemId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets when the request has been made by the client.
|
||||
/// </summary>
|
||||
/// <value>The date of the request.</value>
|
||||
public DateTime When { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the position ticks.
|
||||
/// </summary>
|
||||
/// <value>The position ticks.</value>
|
||||
public long PositionTicks { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the client playback is unpaused.
|
||||
/// </summary>
|
||||
/// <value>The client playback status.</value>
|
||||
public bool IsPlaying { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playlist item identifier of the playing item.
|
||||
/// </summary>
|
||||
/// <value>The playlist item identifier.</value>
|
||||
public Guid PlaylistItemId { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Ready;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class RemoveFromPlaylistGroupRequest.
|
||||
/// </summary>
|
||||
public class RemoveFromPlaylistGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RemoveFromPlaylistGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="items">The playlist ids of the items to remove.</param>
|
||||
public RemoveFromPlaylistGroupRequest(IReadOnlyList<Guid> items)
|
||||
{
|
||||
PlaylistItemIds = items ?? Array.Empty<Guid>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playlist identifiers ot the items.
|
||||
/// </summary>
|
||||
/// <value>The playlist identifiers ot the items.</value>
|
||||
public IReadOnlyList<Guid> PlaylistItemIds { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.RemoveFromPlaylist;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class SeekGroupRequest.
|
||||
/// </summary>
|
||||
public class SeekGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SeekGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="positionTicks">The position ticks.</param>
|
||||
public SeekGroupRequest(long positionTicks)
|
||||
{
|
||||
PositionTicks = positionTicks;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the position ticks.
|
||||
/// </summary>
|
||||
/// <value>The position ticks.</value>
|
||||
public long PositionTicks { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Seek;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class SetPlaylistItemGroupRequest.
|
||||
/// </summary>
|
||||
public class SetPlaylistItemGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SetPlaylistItemGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The playlist identifier of the item.</param>
|
||||
public SetPlaylistItemGroupRequest(Guid playlistItemId)
|
||||
{
|
||||
PlaylistItemId = playlistItemId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playlist identifier of the playing item.
|
||||
/// </summary>
|
||||
/// <value>The playlist identifier of the playing item.</value>
|
||||
public Guid PlaylistItemId { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.SetPlaylistItem;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class SetRepeatModeGroupRequest.
|
||||
/// </summary>
|
||||
public class SetRepeatModeGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SetRepeatModeGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="mode">The repeat mode.</param>
|
||||
public SetRepeatModeGroupRequest(GroupRepeatMode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the repeat mode.
|
||||
/// </summary>
|
||||
/// <value>The repeat mode.</value>
|
||||
public GroupRepeatMode Mode { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.SetRepeatMode;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class SetShuffleModeGroupRequest.
|
||||
/// </summary>
|
||||
public class SetShuffleModeGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SetShuffleModeGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="mode">The shuffle mode.</param>
|
||||
public SetShuffleModeGroupRequest(GroupShuffleMode mode)
|
||||
{
|
||||
Mode = mode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the shuffle mode.
|
||||
/// </summary>
|
||||
/// <value>The shuffle mode.</value>
|
||||
public GroupShuffleMode Mode { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.SetShuffleMode;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class StopGroupRequest.
|
||||
/// </summary>
|
||||
public class StopGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Stop;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Threading;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.PlaybackRequests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class UnpauseGroupRequest.
|
||||
/// </summary>
|
||||
public class UnpauseGroupRequest : AbstractPlaybackRequest
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override PlaybackRequestType Action { get; } = PlaybackRequestType.Unpause;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply(IGroupStateContext context, IGroupState state, SessionInfo session, CancellationToken cancellationToken)
|
||||
{
|
||||
state.HandleRequest(this, context, state.Type, session, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
577
MediaBrowser.Controller/SyncPlay/Queue/PlayQueueManager.cs
Normal file
577
MediaBrowser.Controller/SyncPlay/Queue/PlayQueueManager.cs
Normal file
@@ -0,0 +1,577 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.Queue
|
||||
{
|
||||
/// <summary>
|
||||
/// Class PlayQueueManager.
|
||||
/// </summary>
|
||||
public class PlayQueueManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Placeholder index for when no item is playing.
|
||||
/// </summary>
|
||||
/// <value>The no-playing item index.</value>
|
||||
private const int NoPlayingItemIndex = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Random number generator used to shuffle lists.
|
||||
/// </summary>
|
||||
/// <value>The random number generator.</value>
|
||||
private readonly Random _randomNumberGenerator = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PlayQueueManager" /> class.
|
||||
/// </summary>
|
||||
public PlayQueueManager()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playing item index.
|
||||
/// </summary>
|
||||
/// <value>The playing item index.</value>
|
||||
public int PlayingItemIndex { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the last time the queue has been changed.
|
||||
/// </summary>
|
||||
/// <value>The last time the queue has been changed.</value>
|
||||
public DateTime LastChange { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the shuffle mode.
|
||||
/// </summary>
|
||||
/// <value>The shuffle mode.</value>
|
||||
public GroupShuffleMode ShuffleMode { get; private set; } = GroupShuffleMode.Sorted;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the repeat mode.
|
||||
/// </summary>
|
||||
/// <value>The repeat mode.</value>
|
||||
public GroupRepeatMode RepeatMode { get; private set; } = GroupRepeatMode.RepeatNone;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the sorted playlist.
|
||||
/// </summary>
|
||||
/// <value>The sorted playlist, or play queue of the group.</value>
|
||||
private List<QueueItem> SortedPlaylist { get; set; } = new List<QueueItem>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the shuffled playlist.
|
||||
/// </summary>
|
||||
/// <value>The shuffled playlist, or play queue of the group.</value>
|
||||
private List<QueueItem> ShuffledPlaylist { get; set; } = new List<QueueItem>();
|
||||
|
||||
/// <summary>
|
||||
/// Checks if an item is playing.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if an item is playing; <c>false</c> otherwise.</returns>
|
||||
public bool IsItemPlaying()
|
||||
{
|
||||
return PlayingItemIndex != NoPlayingItemIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current playlist considering the shuffle mode.
|
||||
/// </summary>
|
||||
/// <returns>The playlist.</returns>
|
||||
public IReadOnlyList<QueueItem> GetPlaylist()
|
||||
{
|
||||
return GetPlaylistInternal();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a new playlist. Playing item is reset.
|
||||
/// </summary>
|
||||
/// <param name="items">The new items of the playlist.</param>
|
||||
public void SetPlaylist(IReadOnlyList<Guid> items)
|
||||
{
|
||||
SortedPlaylist.Clear();
|
||||
ShuffledPlaylist.Clear();
|
||||
|
||||
SortedPlaylist = CreateQueueItemsFromArray(items);
|
||||
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
||||
{
|
||||
ShuffledPlaylist = new List<QueueItem>(SortedPlaylist);
|
||||
Shuffle(ShuffledPlaylist);
|
||||
}
|
||||
|
||||
PlayingItemIndex = NoPlayingItemIndex;
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends new items to the playlist. The specified order is mantained.
|
||||
/// </summary>
|
||||
/// <param name="items">The items to add to the playlist.</param>
|
||||
public void Queue(IReadOnlyList<Guid> items)
|
||||
{
|
||||
var newItems = CreateQueueItemsFromArray(items);
|
||||
|
||||
SortedPlaylist.AddRange(newItems);
|
||||
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
||||
{
|
||||
ShuffledPlaylist.AddRange(newItems);
|
||||
}
|
||||
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles the playlist. Shuffle mode is changed. The playlist gets re-shuffled if already shuffled.
|
||||
/// </summary>
|
||||
public void ShufflePlaylist()
|
||||
{
|
||||
if (PlayingItemIndex == NoPlayingItemIndex)
|
||||
{
|
||||
ShuffledPlaylist = new List<QueueItem>(SortedPlaylist);
|
||||
Shuffle(ShuffledPlaylist);
|
||||
}
|
||||
else if (ShuffleMode.Equals(GroupShuffleMode.Sorted))
|
||||
{
|
||||
// First time shuffle.
|
||||
var playingItem = SortedPlaylist[PlayingItemIndex];
|
||||
ShuffledPlaylist = new List<QueueItem>(SortedPlaylist);
|
||||
ShuffledPlaylist.RemoveAt(PlayingItemIndex);
|
||||
Shuffle(ShuffledPlaylist);
|
||||
ShuffledPlaylist.Insert(0, playingItem);
|
||||
PlayingItemIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Re-shuffle playlist.
|
||||
var playingItem = ShuffledPlaylist[PlayingItemIndex];
|
||||
ShuffledPlaylist.RemoveAt(PlayingItemIndex);
|
||||
Shuffle(ShuffledPlaylist);
|
||||
ShuffledPlaylist.Insert(0, playingItem);
|
||||
PlayingItemIndex = 0;
|
||||
}
|
||||
|
||||
ShuffleMode = GroupShuffleMode.Shuffle;
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the playlist to sorted mode. Shuffle mode is changed.
|
||||
/// </summary>
|
||||
public void RestoreSortedPlaylist()
|
||||
{
|
||||
if (PlayingItemIndex != NoPlayingItemIndex)
|
||||
{
|
||||
var playingItem = ShuffledPlaylist[PlayingItemIndex];
|
||||
PlayingItemIndex = SortedPlaylist.IndexOf(playingItem);
|
||||
}
|
||||
|
||||
ShuffledPlaylist.Clear();
|
||||
|
||||
ShuffleMode = GroupShuffleMode.Sorted;
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the playlist. Shuffle mode is preserved.
|
||||
/// </summary>
|
||||
/// <param name="clearPlayingItem">Whether to remove the playing item as well.</param>
|
||||
public void ClearPlaylist(bool clearPlayingItem)
|
||||
{
|
||||
var playingItem = GetPlayingItem();
|
||||
SortedPlaylist.Clear();
|
||||
ShuffledPlaylist.Clear();
|
||||
LastChange = DateTime.UtcNow;
|
||||
|
||||
if (!clearPlayingItem && playingItem != null)
|
||||
{
|
||||
SortedPlaylist.Add(playingItem);
|
||||
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
||||
{
|
||||
ShuffledPlaylist.Add(playingItem);
|
||||
}
|
||||
|
||||
PlayingItemIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
PlayingItemIndex = NoPlayingItemIndex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds new items to the playlist right after the playing item. The specified order is mantained.
|
||||
/// </summary>
|
||||
/// <param name="items">The items to add to the playlist.</param>
|
||||
public void QueueNext(IReadOnlyList<Guid> items)
|
||||
{
|
||||
var newItems = CreateQueueItemsFromArray(items);
|
||||
|
||||
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
||||
{
|
||||
var playingItem = GetPlayingItem();
|
||||
var sortedPlayingItemIndex = SortedPlaylist.IndexOf(playingItem);
|
||||
// Append items to sorted and shuffled playlist as they are.
|
||||
SortedPlaylist.InsertRange(sortedPlayingItemIndex + 1, newItems);
|
||||
ShuffledPlaylist.InsertRange(PlayingItemIndex + 1, newItems);
|
||||
}
|
||||
else
|
||||
{
|
||||
SortedPlaylist.InsertRange(PlayingItemIndex + 1, newItems);
|
||||
}
|
||||
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets playlist identifier of the playing item, if any.
|
||||
/// </summary>
|
||||
/// <returns>The playlist identifier of the playing item.</returns>
|
||||
public Guid GetPlayingItemPlaylistId()
|
||||
{
|
||||
var playingItem = GetPlayingItem();
|
||||
return playingItem?.PlaylistItemId ?? Guid.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the playing item identifier, if any.
|
||||
/// </summary>
|
||||
/// <returns>The playing item identifier.</returns>
|
||||
public Guid GetPlayingItemId()
|
||||
{
|
||||
var playingItem = GetPlayingItem();
|
||||
return playingItem?.ItemId ?? Guid.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the playing item using its identifier. If not in the playlist, the playing item is reset.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The new playing item identifier.</param>
|
||||
public void SetPlayingItemById(Guid itemId)
|
||||
{
|
||||
var playlist = GetPlaylistInternal();
|
||||
PlayingItemIndex = playlist.FindIndex(item => item.ItemId.Equals(itemId));
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the playing item using its playlist identifier. If not in the playlist, the playing item is reset.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The new playing item identifier.</param>
|
||||
/// <returns><c>true</c> if playing item has been set; <c>false</c> if item is not in the playlist.</returns>
|
||||
public bool SetPlayingItemByPlaylistId(Guid playlistItemId)
|
||||
{
|
||||
var playlist = GetPlaylistInternal();
|
||||
PlayingItemIndex = playlist.FindIndex(item => item.PlaylistItemId.Equals(playlistItemId));
|
||||
LastChange = DateTime.UtcNow;
|
||||
|
||||
return PlayingItemIndex != NoPlayingItemIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the playing item using its position. If not in range, the playing item is reset.
|
||||
/// </summary>
|
||||
/// <param name="playlistIndex">The new playing item index.</param>
|
||||
public void SetPlayingItemByIndex(int playlistIndex)
|
||||
{
|
||||
var playlist = GetPlaylistInternal();
|
||||
if (playlistIndex < 0 || playlistIndex > playlist.Count)
|
||||
{
|
||||
PlayingItemIndex = NoPlayingItemIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
PlayingItemIndex = playlistIndex;
|
||||
}
|
||||
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes items from the playlist. If not removed, the playing item is preserved.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemIds">The items to remove.</param>
|
||||
/// <returns><c>true</c> if playing item got removed; <c>false</c> otherwise.</returns>
|
||||
public bool RemoveFromPlaylist(IReadOnlyList<Guid> playlistItemIds)
|
||||
{
|
||||
var playingItem = GetPlayingItem();
|
||||
|
||||
SortedPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId));
|
||||
ShuffledPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId));
|
||||
|
||||
LastChange = DateTime.UtcNow;
|
||||
|
||||
if (playingItem != null)
|
||||
{
|
||||
if (playlistItemIds.Contains(playingItem.PlaylistItemId))
|
||||
{
|
||||
// Playing item has been removed, picking previous item.
|
||||
PlayingItemIndex--;
|
||||
if (PlayingItemIndex < 0)
|
||||
{
|
||||
// Was first element, picking next if available.
|
||||
// Default to no playing item otherwise.
|
||||
PlayingItemIndex = SortedPlaylist.Count > 0 ? 0 : NoPlayingItemIndex;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Restoring playing item.
|
||||
SetPlayingItemByPlaylistId(playingItem.PlaylistItemId);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves an item in the playlist to another position.
|
||||
/// </summary>
|
||||
/// <param name="playlistItemId">The item to move.</param>
|
||||
/// <param name="newIndex">The new position.</param>
|
||||
/// <returns><c>true</c> if the item has been moved; <c>false</c> otherwise.</returns>
|
||||
public bool MovePlaylistItem(Guid playlistItemId, int newIndex)
|
||||
{
|
||||
var playlist = GetPlaylistInternal();
|
||||
var playingItem = GetPlayingItem();
|
||||
|
||||
var oldIndex = playlist.FindIndex(item => item.PlaylistItemId.Equals(playlistItemId));
|
||||
if (oldIndex < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var queueItem = playlist[oldIndex];
|
||||
playlist.RemoveAt(oldIndex);
|
||||
newIndex = Math.Clamp(newIndex, 0, playlist.Count);
|
||||
playlist.Insert(newIndex, queueItem);
|
||||
|
||||
LastChange = DateTime.UtcNow;
|
||||
PlayingItemIndex = playlist.IndexOf(playingItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the playlist to its initial state.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
SortedPlaylist.Clear();
|
||||
ShuffledPlaylist.Clear();
|
||||
PlayingItemIndex = NoPlayingItemIndex;
|
||||
ShuffleMode = GroupShuffleMode.Sorted;
|
||||
RepeatMode = GroupRepeatMode.RepeatNone;
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the repeat mode.
|
||||
/// </summary>
|
||||
/// <param name="mode">The new mode.</param>
|
||||
public void SetRepeatMode(GroupRepeatMode mode)
|
||||
{
|
||||
RepeatMode = mode;
|
||||
LastChange = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the shuffle mode.
|
||||
/// </summary>
|
||||
/// <param name="mode">The new mode.</param>
|
||||
public void SetShuffleMode(GroupShuffleMode mode)
|
||||
{
|
||||
if (mode.Equals(GroupShuffleMode.Shuffle))
|
||||
{
|
||||
ShufflePlaylist();
|
||||
}
|
||||
else
|
||||
{
|
||||
RestoreSortedPlaylist();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggles the shuffle mode between sorted and shuffled.
|
||||
/// </summary>
|
||||
public void ToggleShuffleMode()
|
||||
{
|
||||
if (ShuffleMode.Equals(GroupShuffleMode.Sorted))
|
||||
{
|
||||
ShufflePlaylist();
|
||||
}
|
||||
else
|
||||
{
|
||||
RestoreSortedPlaylist();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the next item in the playlist considering repeat mode and shuffle mode.
|
||||
/// </summary>
|
||||
/// <returns>The next item in the playlist.</returns>
|
||||
public QueueItem GetNextItemPlaylistId()
|
||||
{
|
||||
int newIndex;
|
||||
var playlist = GetPlaylistInternal();
|
||||
|
||||
switch (RepeatMode)
|
||||
{
|
||||
case GroupRepeatMode.RepeatOne:
|
||||
newIndex = PlayingItemIndex;
|
||||
break;
|
||||
case GroupRepeatMode.RepeatAll:
|
||||
newIndex = PlayingItemIndex + 1;
|
||||
if (newIndex >= playlist.Count)
|
||||
{
|
||||
newIndex = 0;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
newIndex = PlayingItemIndex + 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (newIndex < 0 || newIndex >= playlist.Count)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return playlist[newIndex];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the next item in the queue as playing item.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the playing item changed; <c>false</c> otherwise.</returns>
|
||||
public bool Next()
|
||||
{
|
||||
if (RepeatMode.Equals(GroupRepeatMode.RepeatOne))
|
||||
{
|
||||
LastChange = DateTime.UtcNow;
|
||||
return true;
|
||||
}
|
||||
|
||||
PlayingItemIndex++;
|
||||
if (PlayingItemIndex >= SortedPlaylist.Count)
|
||||
{
|
||||
if (RepeatMode.Equals(GroupRepeatMode.RepeatAll))
|
||||
{
|
||||
PlayingItemIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
PlayingItemIndex = SortedPlaylist.Count - 1;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
LastChange = DateTime.UtcNow;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the previous item in the queue as playing item.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if the playing item changed; <c>false</c> otherwise.</returns>
|
||||
public bool Previous()
|
||||
{
|
||||
if (RepeatMode.Equals(GroupRepeatMode.RepeatOne))
|
||||
{
|
||||
LastChange = DateTime.UtcNow;
|
||||
return true;
|
||||
}
|
||||
|
||||
PlayingItemIndex--;
|
||||
if (PlayingItemIndex < 0)
|
||||
{
|
||||
if (RepeatMode.Equals(GroupRepeatMode.RepeatAll))
|
||||
{
|
||||
PlayingItemIndex = SortedPlaylist.Count - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
PlayingItemIndex = 0;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
LastChange = DateTime.UtcNow;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles a given list.
|
||||
/// </summary>
|
||||
/// <param name="list">The list to shuffle.</param>
|
||||
private void Shuffle<T>(IList<T> list)
|
||||
{
|
||||
int n = list.Count;
|
||||
while (n > 1)
|
||||
{
|
||||
n--;
|
||||
int k = _randomNumberGenerator.Next(n + 1);
|
||||
T value = list[k];
|
||||
list[k] = list[n];
|
||||
list[n] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list from the array of items. Each item is given an unique playlist identifier.
|
||||
/// </summary>
|
||||
/// <returns>The list of queue items.</returns>
|
||||
private List<QueueItem> CreateQueueItemsFromArray(IReadOnlyList<Guid> items)
|
||||
{
|
||||
var list = new List<QueueItem>();
|
||||
foreach (var item in items)
|
||||
{
|
||||
var queueItem = new QueueItem(item);
|
||||
list.Add(queueItem);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current playlist considering the shuffle mode.
|
||||
/// </summary>
|
||||
/// <returns>The playlist.</returns>
|
||||
private List<QueueItem> GetPlaylistInternal()
|
||||
{
|
||||
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
||||
{
|
||||
return ShuffledPlaylist;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SortedPlaylist;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current playing item, depending on the shuffle mode.
|
||||
/// </summary>
|
||||
/// <returns>The playing item.</returns>
|
||||
private QueueItem GetPlayingItem()
|
||||
{
|
||||
if (PlayingItemIndex == NoPlayingItemIndex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
||||
{
|
||||
return ShuffledPlaylist[PlayingItemIndex];
|
||||
}
|
||||
else
|
||||
{
|
||||
return SortedPlaylist[PlayingItemIndex];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class JoinGroupRequest.
|
||||
/// </summary>
|
||||
public class JoinGroupRequest : ISyncPlayRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JoinGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="groupId">The identifier of the group to join.</param>
|
||||
public JoinGroupRequest(Guid groupId)
|
||||
{
|
||||
GroupId = groupId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the group identifier.
|
||||
/// </summary>
|
||||
/// <value>The identifier of the group to join.</value>
|
||||
public Guid GroupId { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public RequestType Type { get; } = RequestType.JoinGroup;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class LeaveGroupRequest.
|
||||
/// </summary>
|
||||
public class LeaveGroupRequest : ISyncPlayRequest
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public RequestType Type { get; } = RequestType.LeaveGroup;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class ListGroupsRequest.
|
||||
/// </summary>
|
||||
public class ListGroupsRequest : ISyncPlayRequest
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public RequestType Type { get; } = RequestType.ListGroups;
|
||||
}
|
||||
}
|
||||
28
MediaBrowser.Controller/SyncPlay/Requests/NewGroupRequest.cs
Normal file
28
MediaBrowser.Controller/SyncPlay/Requests/NewGroupRequest.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using MediaBrowser.Model.SyncPlay;
|
||||
|
||||
namespace MediaBrowser.Controller.SyncPlay.Requests
|
||||
{
|
||||
/// <summary>
|
||||
/// Class NewGroupRequest.
|
||||
/// </summary>
|
||||
public class NewGroupRequest : ISyncPlayRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="NewGroupRequest"/> class.
|
||||
/// </summary>
|
||||
/// <param name="groupName">The name of the new group.</param>
|
||||
public NewGroupRequest(string groupName)
|
||||
{
|
||||
GroupName = groupName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the group name.
|
||||
/// </summary>
|
||||
/// <value>The name of the new group.</value>
|
||||
public string GroupName { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public RequestType Type { get; } = RequestType.NewGroup;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user