jellyfin/Emby.Server.Implementations/SyncPlay/SyncPlayController.cs

517 lines
22 KiB
C#
Raw Normal View History

2020-04-01 17:52:42 +02:00
using System;
using System.Collections.Generic;
2020-07-24 16:37:54 +02:00
using System.Globalization;
2020-04-01 17:52:42 +02:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Session;
2020-05-06 23:42:53 +02:00
using MediaBrowser.Controller.SyncPlay;
2020-04-01 17:52:42 +02:00
using MediaBrowser.Model.Session;
2020-05-06 23:42:53 +02:00
using MediaBrowser.Model.SyncPlay;
2020-04-01 17:52:42 +02:00
2020-05-06 23:42:53 +02:00
namespace Emby.Server.Implementations.SyncPlay
2020-04-01 17:52:42 +02:00
{
/// <summary>
2020-05-06 23:42:53 +02:00
/// Class SyncPlayController.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-05-04 19:46:02 +02:00
/// <remarks>
/// Class is not thread-safe, external locking is required when accessing methods.
/// </remarks>
2020-05-26 11:37:52 +02:00
public class SyncPlayController : ISyncPlayController
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
/// <summary>
/// Used to filter the sessions of a group.
/// </summary>
2020-04-01 17:52:42 +02:00
private enum BroadcastType
{
2020-04-21 23:37:37 +02:00
/// <summary>
/// All sessions will receive the message.
/// </summary>
2020-04-01 17:52:42 +02:00
AllGroup = 0,
2020-07-24 16:37:54 +02:00
2020-04-21 23:37:37 +02:00
/// <summary>
/// Only the specified session will receive the message.
/// </summary>
CurrentSession = 1,
2020-07-24 16:37:54 +02:00
2020-04-21 23:37:37 +02:00
/// <summary>
/// All sessions, except the current one, will receive the message.
/// </summary>
AllExceptCurrentSession = 2,
2020-07-24 16:37:54 +02:00
2020-04-21 23:37:37 +02:00
/// <summary>
/// Only sessions that are not buffering will receive the message.
/// </summary>
2020-04-01 17:52:42 +02:00
AllReady = 3
}
/// <summary>
/// The session manager.
/// </summary>
private readonly ISessionManager _sessionManager;
/// <summary>
2020-05-06 23:42:53 +02:00
/// The SyncPlay manager.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-05-06 23:42:53 +02:00
private readonly ISyncPlayManager _syncPlayManager;
2020-04-01 17:52:42 +02:00
/// <summary>
/// The group to manage.
/// </summary>
private readonly GroupInfo _group = new GroupInfo();
/// <summary>
/// Initializes a new instance of the <see cref="SyncPlayController" /> class.
/// </summary>
/// <param name="sessionManager">The session manager.</param>
/// <param name="syncPlayManager">The SyncPlay manager.</param>
2020-05-06 23:42:53 +02:00
public SyncPlayController(
2020-04-01 17:52:42 +02:00
ISessionManager sessionManager,
2020-05-06 23:42:53 +02:00
ISyncPlayManager syncPlayManager)
2020-04-01 17:52:42 +02:00
{
_sessionManager = sessionManager;
2020-05-06 23:42:53 +02:00
_syncPlayManager = syncPlayManager;
2020-04-01 17:52:42 +02:00
}
2020-07-24 16:37:54 +02:00
/// <inheritdoc />
public Guid GetGroupId() => _group.GroupId;
/// <inheritdoc />
public Guid GetPlayingItemId() => _group.PlayingItem.Id;
/// <inheritdoc />
public bool IsGroupEmpty() => _group.IsEmpty();
2020-05-04 19:46:02 +02:00
/// <summary>
/// Converts DateTime to UTC string.
/// </summary>
/// <param name="date">The date to convert.</param>
/// <value>The UTC string.</value>
private string DateToUTCString(DateTime date)
{
2020-07-24 16:37:54 +02:00
return date.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
2020-05-04 19:46:02 +02:00
}
2020-04-21 23:37:37 +02:00
/// <summary>
/// Filters sessions of this group.
/// </summary>
/// <param name="from">The current session.</param>
/// <param name="type">The filtering type.</param>
/// <value>The array of sessions matching the filter.</value>
2020-07-24 16:37:54 +02:00
private IEnumerable<SessionInfo> FilterSessions(SessionInfo from, BroadcastType type)
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
switch (type)
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
case BroadcastType.CurrentSession:
return new SessionInfo[] { from };
case BroadcastType.AllGroup:
2020-07-24 16:37:54 +02:00
return _group.Participants.Values
.Select(session => session.Session);
2020-04-21 23:37:37 +02:00
case BroadcastType.AllExceptCurrentSession:
2020-07-24 16:37:54 +02:00
return _group.Participants.Values
.Select(session => session.Session)
.Where(session => !session.Id.Equals(from.Id, StringComparison.Ordinal));
2020-04-21 23:37:37 +02:00
case BroadcastType.AllReady:
2020-07-24 16:37:54 +02:00
return _group.Participants.Values
.Where(session => !session.IsBuffering)
.Select(session => session.Session);
2020-04-21 23:37:37 +02:00
default:
2020-05-09 14:34:07 +02:00
return Array.Empty<SessionInfo>();
2020-04-01 17:52:42 +02:00
}
}
2020-04-21 23:37:37 +02:00
/// <summary>
/// Sends a GroupUpdate message 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>
2020-05-04 19:46:02 +02:00
/// <param name="cancellationToken">The cancellation token.</param>
2020-04-21 23:37:37 +02:00
/// <value>The task.</value>
2020-05-04 19:46:02 +02:00
private Task SendGroupUpdate<T>(SessionInfo from, BroadcastType type, GroupUpdate<T> message, CancellationToken cancellationToken)
2020-04-01 17:52:42 +02:00
{
IEnumerable<Task> GetTasks()
{
2020-07-24 16:37:54 +02:00
foreach (var session in FilterSessions(from, type))
2020-04-01 17:52:42 +02:00
{
2020-07-24 16:37:54 +02:00
yield return _sessionManager.SendSyncPlayGroupUpdate(session.Id, message, cancellationToken);
2020-04-01 17:52:42 +02:00
}
}
return Task.WhenAll(GetTasks());
}
2020-04-21 23:37:37 +02:00
/// <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>
2020-05-04 19:46:02 +02:00
/// <param name="cancellationToken">The cancellation token.</param>
2020-04-21 23:37:37 +02:00
/// <value>The task.</value>
2020-05-04 19:46:02 +02:00
private Task SendCommand(SessionInfo from, BroadcastType type, SendCommand message, CancellationToken cancellationToken)
2020-04-01 17:52:42 +02:00
{
IEnumerable<Task> GetTasks()
{
2020-07-24 16:37:54 +02:00
foreach (var session in FilterSessions(from, type))
2020-04-01 17:52:42 +02:00
{
2020-07-24 16:37:54 +02:00
yield return _sessionManager.SendSyncPlayCommand(session.Id, message, cancellationToken);
2020-04-01 17:52:42 +02:00
}
}
return Task.WhenAll(GetTasks());
}
2020-04-21 23:37:37 +02:00
/// <summary>
/// Builds a new playback command with some default values.
/// </summary>
/// <param name="type">The command type.</param>
/// <value>The SendCommand.</value>
2020-05-06 23:42:53 +02:00
private SendCommand NewSyncPlayCommand(SendCommandType type)
{
2020-04-21 23:37:37 +02:00
return new SendCommand()
{
GroupId = _group.GroupId.ToString(),
Command = type,
PositionTicks = _group.PositionTicks,
2020-05-04 19:46:02 +02:00
When = DateToUTCString(_group.LastActivity),
EmittedAt = DateToUTCString(DateTime.UtcNow)
2020-04-21 23:37:37 +02:00
};
2020-04-01 17:52:42 +02:00
}
2020-04-21 23:37:37 +02:00
/// <summary>
/// Builds a new group update message.
/// </summary>
/// <param name="type">The update type.</param>
/// <param name="data">The data to send.</param>
/// <value>The GroupUpdate.</value>
2020-05-06 23:42:53 +02:00
private GroupUpdate<T> NewSyncPlayGroupUpdate<T>(GroupUpdateType type, T data)
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
return new GroupUpdate<T>()
{
GroupId = _group.GroupId.ToString(),
Type = type,
Data = data
};
2020-04-01 17:52:42 +02:00
}
/// <inheritdoc />
public void CreateGroup(SessionInfo session, CancellationToken cancellationToken)
2020-04-01 17:52:42 +02:00
{
2020-04-04 17:56:21 +02:00
_group.AddSession(session);
2020-05-06 23:42:53 +02:00
_syncPlayManager.AddSessionToGroup(session, this);
2020-04-01 17:52:42 +02:00
2020-04-04 17:56:21 +02:00
_group.PlayingItem = session.FullNowPlayingItem;
_group.IsPaused = session.PlayState.IsPaused;
2020-05-04 19:46:02 +02:00
_group.PositionTicks = session.PlayState.PositionTicks ?? 0;
2020-04-01 17:52:42 +02:00
_group.LastActivity = DateTime.UtcNow;
2020-05-06 23:42:53 +02:00
var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, DateToUTCString(DateTime.UtcNow));
2020-05-04 19:46:02 +02:00
SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession, cancellationToken);
2020-04-01 17:52:42 +02:00
}
/// <inheritdoc />
2020-05-04 19:46:02 +02:00
public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
2020-04-01 17:52:42 +02:00
{
if (session.NowPlayingItem?.Id == _group.PlayingItem.Id)
2020-04-01 17:52:42 +02:00
{
2020-04-04 17:56:21 +02:00
_group.AddSession(session);
2020-05-06 23:42:53 +02:00
_syncPlayManager.AddSessionToGroup(session, this);
2020-04-01 17:52:42 +02:00
2020-05-06 23:42:53 +02:00
var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, DateToUTCString(DateTime.UtcNow));
2020-05-04 19:46:02 +02:00
SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession, cancellationToken);
2020-04-01 17:52:42 +02:00
2020-05-06 23:42:53 +02:00
var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
2020-05-04 19:46:02 +02:00
SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
2020-04-01 17:52:42 +02:00
// Syncing will happen client-side
2020-04-01 17:52:42 +02:00
if (!_group.IsPaused)
{
2020-05-06 23:42:53 +02:00
var playCommand = NewSyncPlayCommand(SendCommandType.Play);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.CurrentSession, playCommand, cancellationToken);
2020-04-01 17:52:42 +02:00
}
else
{
2020-05-06 23:42:53 +02:00
var pauseCommand = NewSyncPlayCommand(SendCommandType.Pause);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.CurrentSession, pauseCommand, cancellationToken);
2020-04-01 17:52:42 +02:00
}
}
else
{
2020-07-24 16:37:54 +02:00
var playRequest = new PlayRequest
{
ItemIds = new Guid[] { _group.PlayingItem.Id },
StartPositionTicks = _group.PositionTicks
};
2020-05-06 23:42:53 +02:00
var update = NewSyncPlayGroupUpdate(GroupUpdateType.PrepareSession, playRequest);
2020-05-04 19:46:02 +02:00
SendGroupUpdate(session, BroadcastType.CurrentSession, update, cancellationToken);
2020-04-01 17:52:42 +02:00
}
}
/// <inheritdoc />
2020-05-04 19:46:02 +02:00
public void SessionLeave(SessionInfo session, CancellationToken cancellationToken)
2020-04-01 17:52:42 +02:00
{
2020-04-04 17:56:21 +02:00
_group.RemoveSession(session);
2020-05-06 23:42:53 +02:00
_syncPlayManager.RemoveSessionFromGroup(session, this);
2020-04-01 17:52:42 +02:00
2020-05-06 23:42:53 +02:00
var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupLeft, _group.PositionTicks);
2020-05-04 19:46:02 +02:00
SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession, cancellationToken);
2020-04-01 17:52:42 +02:00
2020-05-06 23:42:53 +02:00
var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserLeft, session.UserName);
2020-05-04 19:46:02 +02:00
SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
2020-04-01 17:52:42 +02:00
}
/// <inheritdoc />
2020-05-04 19:46:02 +02:00
public void HandleRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
2020-04-01 17:52:42 +02:00
{
// The server's job is to maintain a consistent state for clients to reference
// and notify clients of state changes. The actual syncing of media playback
// happens client side. Clients are aware of the server's time and use it to sync.
2020-04-21 23:37:37 +02:00
switch (request.Type)
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
case PlaybackRequestType.Play:
2020-05-04 19:46:02 +02:00
HandlePlayRequest(session, request, cancellationToken);
2020-04-21 23:37:37 +02:00
break;
case PlaybackRequestType.Pause:
2020-05-04 19:46:02 +02:00
HandlePauseRequest(session, request, cancellationToken);
2020-04-21 23:37:37 +02:00
break;
case PlaybackRequestType.Seek:
2020-05-04 19:46:02 +02:00
HandleSeekRequest(session, request, cancellationToken);
2020-04-21 23:37:37 +02:00
break;
case PlaybackRequestType.Buffer:
2020-05-04 19:46:02 +02:00
HandleBufferingRequest(session, request, cancellationToken);
2020-04-21 23:37:37 +02:00
break;
case PlaybackRequestType.Ready:
2020-05-04 19:46:02 +02:00
HandleBufferingDoneRequest(session, request, cancellationToken);
2020-04-21 23:37:37 +02:00
break;
case PlaybackRequestType.Ping:
2020-04-21 23:37:37 +02:00
HandlePingUpdateRequest(session, request);
break;
}
}
2020-04-01 17:52:42 +02:00
2020-04-21 23:37:37 +02:00
/// <summary>
/// Handles a play action requested by a session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The play action.</param>
2020-05-04 19:46:02 +02:00
/// <param name="cancellationToken">The cancellation token.</param>
private void HandlePlayRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
2020-04-21 23:37:37 +02:00
{
if (_group.IsPaused)
{
// Pick a suitable time that accounts for latency
var delay = _group.GetHighestPing() * 2;
delay = delay < _group.DefaultPing ? _group.DefaultPing : delay;
2020-04-21 23:37:37 +02:00
// 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
_group.IsPaused = false;
_group.LastActivity = DateTime.UtcNow.AddMilliseconds(
2020-06-14 12:20:19 +02:00
delay);
2020-04-21 23:37:37 +02:00
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Play);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
2020-04-01 17:52:42 +02:00
}
2020-04-21 23:37:37 +02:00
else
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
// Client got lost, sending current state
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Play);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
2020-04-21 23:37:37 +02:00
}
}
2020-04-01 17:52:42 +02:00
2020-04-21 23:37:37 +02:00
/// <summary>
/// Handles a pause action requested by a session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The pause action.</param>
2020-05-04 19:46:02 +02:00
/// <param name="cancellationToken">The cancellation token.</param>
private void HandlePauseRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
2020-04-21 23:37:37 +02:00
{
if (!_group.IsPaused)
{
// Pause group and compute the media playback position
_group.IsPaused = true;
var currentTime = DateTime.UtcNow;
var elapsedTime = currentTime - _group.LastActivity;
_group.LastActivity = currentTime;
2020-04-21 23:37:37 +02:00
// Seek only if playback actually started
// Pause request may be issued during the delay added to account for latency
2020-04-21 23:37:37 +02:00
_group.PositionTicks += elapsedTime.Ticks > 0 ? elapsedTime.Ticks : 0;
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Pause);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
2020-04-01 17:52:42 +02:00
}
2020-04-21 23:37:37 +02:00
else
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
// Client got lost, sending current state
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Pause);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
2020-04-21 23:37:37 +02:00
}
}
/// <summary>
/// Handles a seek action requested by a session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The seek action.</param>
2020-05-04 19:46:02 +02:00
/// <param name="cancellationToken">The cancellation token.</param>
private void HandleSeekRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
2020-04-21 23:37:37 +02:00
{
// Sanitize PositionTicks
2020-05-04 19:46:02 +02:00
var ticks = SanitizePositionTicks(request.PositionTicks);
2020-04-21 23:37:37 +02:00
// Pause and seek
_group.IsPaused = true;
_group.PositionTicks = ticks;
_group.LastActivity = DateTime.UtcNow;
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Seek);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
2020-04-21 23:37:37 +02:00
}
2020-04-01 17:52:42 +02:00
2020-04-21 23:37:37 +02:00
/// <summary>
/// Handles a buffering action requested by a session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The buffering action.</param>
2020-05-04 19:46:02 +02:00
/// <param name="cancellationToken">The cancellation token.</param>
private void HandleBufferingRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
2020-04-21 23:37:37 +02:00
{
if (!_group.IsPaused)
{
// Pause group and compute the media playback position
2020-04-01 17:52:42 +02:00
_group.IsPaused = true;
2020-04-21 23:37:37 +02:00
var currentTime = DateTime.UtcNow;
var elapsedTime = currentTime - _group.LastActivity;
_group.LastActivity = currentTime;
_group.PositionTicks += elapsedTime.Ticks > 0 ? elapsedTime.Ticks : 0;
2020-04-01 17:52:42 +02:00
2020-04-21 23:37:37 +02:00
_group.SetBuffering(session, true);
// Send pause command to all non-buffering sessions
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Pause);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.AllReady, command, cancellationToken);
2020-04-21 23:37:37 +02:00
2020-05-06 23:42:53 +02:00
var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.GroupWait, session.UserName);
2020-05-04 19:46:02 +02:00
SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
2020-04-01 17:52:42 +02:00
}
2020-04-21 23:37:37 +02:00
else
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
// Client got lost, sending current state
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Pause);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
2020-04-21 23:37:37 +02:00
}
}
2020-04-01 17:52:42 +02:00
2020-04-21 23:37:37 +02:00
/// <summary>
/// Handles a buffering-done action requested by a session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The buffering-done action.</param>
2020-05-04 19:46:02 +02:00
/// <param name="cancellationToken">The cancellation token.</param>
private void HandleBufferingDoneRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
2020-04-21 23:37:37 +02:00
{
if (_group.IsPaused)
{
_group.SetBuffering(session, false);
2020-04-01 17:52:42 +02:00
2020-05-04 19:46:02 +02:00
var requestTicks = SanitizePositionTicks(request.PositionTicks);
var when = request.When ?? DateTime.UtcNow;
2020-04-21 23:37:37 +02:00
var currentTime = DateTime.UtcNow;
var elapsedTime = currentTime - when;
2020-05-04 19:46:02 +02:00
var clientPosition = TimeSpan.FromTicks(requestTicks) + elapsedTime;
2020-04-21 23:37:37 +02:00
var delay = _group.PositionTicks - clientPosition.Ticks;
2020-04-01 17:52:42 +02:00
2020-04-21 23:37:37 +02:00
if (_group.IsBuffering())
2020-04-01 17:52:42 +02:00
{
2020-05-04 19:46:02 +02:00
// Others are still buffering, tell this client to pause when ready
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Pause);
2020-05-04 19:46:02 +02:00
var pauseAtTime = currentTime.AddMilliseconds(delay);
command.When = DateToUTCString(pauseAtTime);
SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
2020-04-01 17:52:42 +02:00
}
2020-04-21 23:37:37 +02:00
else
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
// Let other clients resume as soon as the buffering client catches up
_group.IsPaused = false;
if (delay > _group.GetHighestPing() * 2)
{
// Client that was buffering is recovering, notifying others to resume
_group.LastActivity = currentTime.AddMilliseconds(
2020-06-14 12:20:19 +02:00
delay);
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Play);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.AllExceptCurrentSession, command, cancellationToken);
2020-04-01 17:52:42 +02:00
}
else
{
2020-04-21 23:37:37 +02:00
// Client, that was buffering, resumed playback but did not update others in time
delay = _group.GetHighestPing() * 2;
delay = delay < _group.DefaultPing ? _group.DefaultPing : delay;
2020-04-21 23:37:37 +02:00
_group.LastActivity = currentTime.AddMilliseconds(
2020-06-14 12:20:19 +02:00
delay);
2020-04-21 23:37:37 +02:00
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Play);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
2020-04-21 23:37:37 +02:00
}
2020-04-01 17:52:42 +02:00
}
}
2020-04-21 23:37:37 +02:00
else
2020-04-01 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
// Group was not waiting, make sure client has latest state
2020-05-06 23:42:53 +02:00
var command = NewSyncPlayCommand(SendCommandType.Play);
2020-05-04 19:46:02 +02:00
SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
}
}
/// <summary>
/// Sanitizes the PositionTicks, considers the current playing item when available.
/// </summary>
/// <param name="positionTicks">The PositionTicks.</param>
/// <value>The sanitized PositionTicks.</value>
private long SanitizePositionTicks(long? positionTicks)
{
var ticks = positionTicks ?? 0;
ticks = ticks >= 0 ? ticks : 0;
if (_group.PlayingItem != null)
{
var runTimeTicks = _group.PlayingItem.RunTimeTicks ?? 0;
ticks = ticks > runTimeTicks ? runTimeTicks : ticks;
2020-04-01 17:52:42 +02:00
}
2020-05-26 11:37:52 +02:00
2020-05-04 19:46:02 +02:00
return ticks;
2020-04-01 17:52:42 +02:00
}
2020-04-21 23:37:37 +02:00
/// <summary>
/// Updates ping of a session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The update.</param>
private void HandlePingUpdateRequest(SessionInfo session, PlaybackRequest request)
{
// Collected pings are used to account for network latency when unpausing playback
_group.UpdatePing(session, request.Ping ?? _group.DefaultPing);
2020-04-21 23:37:37 +02:00
}
2020-04-01 17:52:42 +02:00
/// <inheritdoc />
public GroupInfoView GetInfo()
{
2020-04-21 23:37:37 +02:00
return new GroupInfoView()
{
GroupId = GetGroupId().ToString(),
PlayingItemName = _group.PlayingItem.Name,
PlayingItemId = _group.PlayingItem.Id.ToString(),
PositionTicks = _group.PositionTicks,
2020-05-26 11:37:52 +02:00
Participants = _group.Participants.Values.Select(session => session.Session.UserName).Distinct().ToList()
2020-04-21 23:37:37 +02:00
};
2020-04-01 17:52:42 +02:00
}
}
}