aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations')
-rw-r--r--Emby.Server.Implementations/HttpServer/WebSocketConnection.cs4
-rw-r--r--Emby.Server.Implementations/Session/SessionWebSocketListener.cs127
-rw-r--r--Emby.Server.Implementations/Syncplay/SyncplayController.cs158
-rw-r--r--Emby.Server.Implementations/Syncplay/SyncplayManager.cs61
4 files changed, 196 insertions, 154 deletions
diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
index c819c163a..4c33ff71b 100644
--- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
+++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
@@ -238,10 +238,10 @@ namespace Emby.Server.Implementations.HttpServer
return _socket.SendAsync(text, true, cancellationToken);
}
- private Task SendKeepAliveResponse()
+ private void SendKeepAliveResponse()
{
LastKeepAliveDate = DateTime.UtcNow;
- return SendAsync(new WebSocketMessage<string>
+ SendAsync(new WebSocketMessage<string>
{
MessageType = "KeepAlive"
}, CancellationToken.None);
diff --git a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs
index 7a316b070..d1ee22ea8 100644
--- a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs
+++ b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs
@@ -84,10 +84,10 @@ namespace Emby.Server.Implementations.Session
_logger = loggerFactory.CreateLogger(GetType().Name);
_json = json;
_httpServer = httpServer;
- httpServer.WebSocketConnected += _serverManager_WebSocketConnected;
+ httpServer.WebSocketConnected += OnServerManagerWebSocketConnected;
}
- void _serverManager_WebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
+ void OnServerManagerWebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
{
var session = GetSession(e.Argument.QueryString, e.Argument.RemoteEndPoint);
@@ -121,7 +121,7 @@ namespace Emby.Server.Implementations.Session
public void Dispose()
{
- _httpServer.WebSocketConnected -= _serverManager_WebSocketConnected;
+ _httpServer.WebSocketConnected -= OnServerManagerWebSocketConnected;
StopKeepAlive();
}
@@ -149,7 +149,7 @@ namespace Emby.Server.Implementations.Session
private void OnWebSocketClosed(object sender, EventArgs e)
{
var webSocket = (IWebSocketConnection) sender;
- _logger.LogDebug("WebSockets {0} closed.", webSocket);
+ _logger.LogDebug("WebSocket {0} is closed.", webSocket);
RemoveWebSocket(webSocket);
}
@@ -157,7 +157,7 @@ namespace Emby.Server.Implementations.Session
/// Adds a WebSocket to the KeepAlive watchlist.
/// </summary>
/// <param name="webSocket">The WebSocket to monitor.</param>
- private async Task KeepAliveWebSocket(IWebSocketConnection webSocket)
+ private void KeepAliveWebSocket(IWebSocketConnection webSocket)
{
lock (_webSocketsLock)
{
@@ -175,11 +175,11 @@ namespace Emby.Server.Implementations.Session
// Notify WebSocket about timeout
try
{
- await SendForceKeepAlive(webSocket);
+ SendForceKeepAlive(webSocket).Wait();
}
catch (WebSocketException exception)
{
- _logger.LogWarning(exception, "Error sending ForceKeepAlive message to WebSocket {0}.", webSocket);
+ _logger.LogWarning(exception, "Cannot send ForceKeepAlive message to WebSocket {0}.", webSocket);
}
}
@@ -213,7 +213,8 @@ namespace Emby.Server.Implementations.Session
{
_keepAliveCancellationToken = new CancellationTokenSource();
// Start KeepAlive watcher
- KeepAliveSockets(
+ var task = RepeatAsyncCallbackEvery(
+ KeepAliveSockets,
TimeSpan.FromSeconds(WebSocketLostTimeout * IntervalFactor),
_keepAliveCancellationToken.Token);
}
@@ -245,73 +246,58 @@ namespace Emby.Server.Implementations.Session
}
/// <summary>
- /// Checks status of KeepAlive of WebSockets once every the specified interval time.
+ /// Checks status of KeepAlive of WebSockets.
/// </summary>
- /// <param name="interval">The interval.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- private async Task KeepAliveSockets(TimeSpan interval, CancellationToken cancellationToken)
+ private async Task KeepAliveSockets()
{
- while (true)
+ IEnumerable<IWebSocketConnection> inactive;
+ IEnumerable<IWebSocketConnection> lost;
+
+ lock (_webSocketsLock)
{
- _logger.LogDebug("Watching {0} WebSockets.", _webSockets.Count());
+ _logger.LogDebug("Watching {0} WebSockets.", _webSockets.Count);
- IEnumerable<IWebSocketConnection> inactive;
- IEnumerable<IWebSocketConnection> lost;
- lock (_webSocketsLock)
+ inactive = _webSockets.Where(i =>
{
- inactive = _webSockets.Where(i =>
- {
- var elapsed = (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds;
- return (elapsed > WebSocketLostTimeout * ForceKeepAliveFactor) && (elapsed < WebSocketLostTimeout);
- });
- lost = _webSockets.Where(i => (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds >= WebSocketLostTimeout);
- }
+ var elapsed = (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds;
+ return (elapsed > WebSocketLostTimeout * ForceKeepAliveFactor) && (elapsed < WebSocketLostTimeout);
+ });
+ lost = _webSockets.Where(i => (DateTime.UtcNow - i.LastKeepAliveDate).TotalSeconds >= WebSocketLostTimeout);
+ }
- if (inactive.Any())
+ if (inactive.Any())
+ {
+ _logger.LogInformation("Sending ForceKeepAlive message to {0} inactive WebSockets.", inactive.Count());
+ }
+
+ foreach (var webSocket in inactive)
+ {
+ try
{
- _logger.LogInformation("Sending ForceKeepAlive message to {0} inactive WebSockets.", inactive.Count());
+ await SendForceKeepAlive(webSocket);
}
-
- foreach (var webSocket in inactive)
+ catch (WebSocketException exception)
{
- try
- {
- await SendForceKeepAlive(webSocket);
- }
- catch (WebSocketException exception)
- {
- _logger.LogInformation(exception, "Error sending ForceKeepAlive message to WebSocket.");
- lost = lost.Append(webSocket);
- }
+ _logger.LogInformation(exception, "Error sending ForceKeepAlive message to WebSocket.");
+ lost = lost.Append(webSocket);
}
+ }
- lock (_webSocketsLock)
+ lock (_webSocketsLock)
+ {
+ if (lost.Any())
{
- if (lost.Any())
- {
- _logger.LogInformation("Lost {0} WebSockets.", lost.Count());
- foreach (var webSocket in lost.ToList())
- {
- // TODO: handle session relative to the lost webSocket
- RemoveWebSocket(webSocket);
- }
- }
-
- if (!_webSockets.Any())
+ _logger.LogInformation("Lost {0} WebSockets.", lost.Count());
+ foreach (var webSocket in lost.ToList())
{
- StopKeepAlive();
+ // TODO: handle session relative to the lost webSocket
+ RemoveWebSocket(webSocket);
}
}
- // Wait for next interval
- Task task = Task.Delay(interval, cancellationToken);
- try
+ if (!_webSockets.Any())
{
- await task;
- }
- catch (TaskCanceledException)
- {
- return;
+ StopKeepAlive();
}
}
}
@@ -329,5 +315,30 @@ namespace Emby.Server.Implementations.Session
Data = WebSocketLostTimeout
}, CancellationToken.None);
}
+
+ /// <summary>
+ /// Runs a given async callback once every specified interval time, until cancelled.
+ /// </summary>
+ /// <param name="callback">The async callback.</param>
+ /// <param name="interval">The interval time.</param>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task.</returns>
+ private async Task RepeatAsyncCallbackEvery(Func<Task> callback, TimeSpan interval, CancellationToken cancellationToken)
+ {
+ while (!cancellationToken.IsCancellationRequested)
+ {
+ await callback();
+ Task task = Task.Delay(interval, cancellationToken);
+
+ try
+ {
+ await task;
+ }
+ catch (TaskCanceledException)
+ {
+ return;
+ }
+ }
+ }
}
}
diff --git a/Emby.Server.Implementations/Syncplay/SyncplayController.cs b/Emby.Server.Implementations/Syncplay/SyncplayController.cs
index 02cf08cd7..8cc3d1fac 100644
--- a/Emby.Server.Implementations/Syncplay/SyncplayController.cs
+++ b/Emby.Server.Implementations/Syncplay/SyncplayController.cs
@@ -7,13 +7,15 @@ using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.Syncplay;
using MediaBrowser.Model.Session;
using MediaBrowser.Model.Syncplay;
-using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Syncplay
{
/// <summary>
/// Class SyncplayController.
/// </summary>
+ /// <remarks>
+ /// Class is not thread-safe, external locking is required when accessing methods.
+ /// </remarks>
public class SyncplayController : ISyncplayController, IDisposable
{
/// <summary>
@@ -40,11 +42,6 @@ namespace Emby.Server.Implementations.Syncplay
}
/// <summary>
- /// The logger.
- /// </summary>
- private readonly ILogger _logger;
-
- /// <summary>
/// The session manager.
/// </summary>
private readonly ISessionManager _sessionManager;
@@ -71,11 +68,9 @@ namespace Emby.Server.Implementations.Syncplay
private bool _disposed = false;
public SyncplayController(
- ILogger logger,
ISessionManager sessionManager,
ISyncplayManager syncplayManager)
{
- _logger = logger;
_sessionManager = sessionManager;
_syncplayManager = syncplayManager;
}
@@ -111,6 +106,16 @@ namespace Emby.Server.Implementations.Syncplay
}
/// <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)
+ {
+ return date.ToUniversalTime().ToString("o");
+ }
+
+ /// <summary>
/// Filters sessions of this group.
/// </summary>
/// <param name="from">The current session.</param>
@@ -149,15 +154,16 @@ namespace Emby.Server.Implementations.Syncplay
/// <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>
/// <value>The task.</value>
- private Task SendGroupUpdate<T>(SessionInfo from, BroadcastType type, GroupUpdate<T> message)
+ private Task SendGroupUpdate<T>(SessionInfo from, BroadcastType type, GroupUpdate<T> message, CancellationToken cancellationToken)
{
IEnumerable<Task> GetTasks()
{
SessionInfo[] sessions = FilterSessions(from, type);
foreach (var session in sessions)
{
- yield return _sessionManager.SendSyncplayGroupUpdate(session.Id.ToString(), message, CancellationToken.None);
+ yield return _sessionManager.SendSyncplayGroupUpdate(session.Id.ToString(), message, cancellationToken);
}
}
@@ -170,15 +176,16 @@ namespace Emby.Server.Implementations.Syncplay
/// <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>
/// <value>The task.</value>
- private Task SendCommand(SessionInfo from, BroadcastType type, SendCommand message)
+ private Task SendCommand(SessionInfo from, BroadcastType type, SendCommand message, CancellationToken cancellationToken)
{
IEnumerable<Task> GetTasks()
{
SessionInfo[] sessions = FilterSessions(from, type);
foreach (var session in sessions)
{
- yield return _sessionManager.SendSyncplayCommand(session.Id.ToString(), message, CancellationToken.None);
+ yield return _sessionManager.SendSyncplayCommand(session.Id.ToString(), message, cancellationToken);
}
}
@@ -197,8 +204,8 @@ namespace Emby.Server.Implementations.Syncplay
GroupId = _group.GroupId.ToString(),
Command = type,
PositionTicks = _group.PositionTicks,
- When = _group.LastActivity.ToUniversalTime().ToString("o"),
- EmittedAt = DateTime.UtcNow.ToUniversalTime().ToString("o")
+ When = DateToUTCString(_group.LastActivity),
+ EmittedAt = DateToUTCString(DateTime.UtcNow)
};
}
@@ -219,46 +226,46 @@ namespace Emby.Server.Implementations.Syncplay
}
/// <inheritdoc />
- public void InitGroup(SessionInfo session)
+ public void InitGroup(SessionInfo session, CancellationToken cancellationToken)
{
_group.AddSession(session);
_syncplayManager.AddSessionToGroup(session, this);
_group.PlayingItem = session.FullNowPlayingItem;
_group.IsPaused = true;
- _group.PositionTicks = session.PlayState.PositionTicks ??= 0;
+ _group.PositionTicks = session.PlayState.PositionTicks ?? 0;
_group.LastActivity = DateTime.UtcNow;
- var updateSession = NewSyncplayGroupUpdate(GroupUpdateType.GroupJoined, DateTime.UtcNow.ToUniversalTime().ToString("o"));
- SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession);
+ var updateSession = NewSyncplayGroupUpdate(GroupUpdateType.GroupJoined, DateToUTCString(DateTime.UtcNow));
+ SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession, cancellationToken);
var pauseCommand = NewSyncplayCommand(SendCommandType.Pause);
- SendCommand(session, BroadcastType.CurrentSession, pauseCommand);
+ SendCommand(session, BroadcastType.CurrentSession, pauseCommand, cancellationToken);
}
/// <inheritdoc />
- public void SessionJoin(SessionInfo session, JoinGroupRequest request)
+ public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
{
if (session.NowPlayingItem?.Id == _group.PlayingItem.Id && request.PlayingItemId == _group.PlayingItem.Id)
{
_group.AddSession(session);
_syncplayManager.AddSessionToGroup(session, this);
- var updateSession = NewSyncplayGroupUpdate(GroupUpdateType.GroupJoined, DateTime.UtcNow.ToUniversalTime().ToString("o"));
- SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession);
+ var updateSession = NewSyncplayGroupUpdate(GroupUpdateType.GroupJoined, DateToUTCString(DateTime.UtcNow));
+ SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession, cancellationToken);
var updateOthers = NewSyncplayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
- SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers);
+ SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
// Client join and play, syncing will happen client side
if (!_group.IsPaused)
{
var playCommand = NewSyncplayCommand(SendCommandType.Play);
- SendCommand(session, BroadcastType.CurrentSession, playCommand);
+ SendCommand(session, BroadcastType.CurrentSession, playCommand, cancellationToken);
}
else
{
var pauseCommand = NewSyncplayCommand(SendCommandType.Pause);
- SendCommand(session, BroadcastType.CurrentSession, pauseCommand);
+ SendCommand(session, BroadcastType.CurrentSession, pauseCommand, cancellationToken);
}
}
else
@@ -267,25 +274,25 @@ namespace Emby.Server.Implementations.Syncplay
playRequest.ItemIds = new Guid[] { _group.PlayingItem.Id };
playRequest.StartPositionTicks = _group.PositionTicks;
var update = NewSyncplayGroupUpdate(GroupUpdateType.PrepareSession, playRequest);
- SendGroupUpdate(session, BroadcastType.CurrentSession, update);
+ SendGroupUpdate(session, BroadcastType.CurrentSession, update, cancellationToken);
}
}
/// <inheritdoc />
- public void SessionLeave(SessionInfo session)
+ public void SessionLeave(SessionInfo session, CancellationToken cancellationToken)
{
_group.RemoveSession(session);
_syncplayManager.RemoveSessionFromGroup(session, this);
var updateSession = NewSyncplayGroupUpdate(GroupUpdateType.GroupLeft, _group.PositionTicks);
- SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession);
+ SendGroupUpdate(session, BroadcastType.CurrentSession, updateSession, cancellationToken);
var updateOthers = NewSyncplayGroupUpdate(GroupUpdateType.UserLeft, session.UserName);
- SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers);
+ SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
}
/// <inheritdoc />
- public void HandleRequest(SessionInfo session, PlaybackRequest request)
+ public void HandleRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
{
// The server's job is to mantain a consistent state to which clients refer to,
// as also to notify clients of state changes.
@@ -294,19 +301,19 @@ namespace Emby.Server.Implementations.Syncplay
switch (request.Type)
{
case PlaybackRequestType.Play:
- HandlePlayRequest(session, request);
+ HandlePlayRequest(session, request, cancellationToken);
break;
case PlaybackRequestType.Pause:
- HandlePauseRequest(session, request);
+ HandlePauseRequest(session, request, cancellationToken);
break;
case PlaybackRequestType.Seek:
- HandleSeekRequest(session, request);
+ HandleSeekRequest(session, request, cancellationToken);
break;
case PlaybackRequestType.Buffering:
- HandleBufferingRequest(session, request);
+ HandleBufferingRequest(session, request, cancellationToken);
break;
case PlaybackRequestType.BufferingDone:
- HandleBufferingDoneRequest(session, request);
+ HandleBufferingDoneRequest(session, request, cancellationToken);
break;
case PlaybackRequestType.UpdatePing:
HandlePingUpdateRequest(session, request);
@@ -319,7 +326,8 @@ namespace Emby.Server.Implementations.Syncplay
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The play action.</param>
- private void HandlePlayRequest(SessionInfo session, PlaybackRequest request)
+ /// <param name="cancellationToken">The cancellation token.</param>
+ private void HandlePlayRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
{
if (_group.IsPaused)
{
@@ -337,13 +345,13 @@ namespace Emby.Server.Implementations.Syncplay
);
var command = NewSyncplayCommand(SendCommandType.Play);
- SendCommand(session, BroadcastType.AllGroup, command);
+ SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
}
else
{
// Client got lost, sending current state
var command = NewSyncplayCommand(SendCommandType.Play);
- SendCommand(session, BroadcastType.CurrentSession, command);
+ SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
}
}
@@ -352,7 +360,8 @@ namespace Emby.Server.Implementations.Syncplay
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The pause action.</param>
- private void HandlePauseRequest(SessionInfo session, PlaybackRequest request)
+ /// <param name="cancellationToken">The cancellation token.</param>
+ private void HandlePauseRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
{
if (!_group.IsPaused)
{
@@ -366,13 +375,13 @@ namespace Emby.Server.Implementations.Syncplay
_group.PositionTicks += elapsedTime.Ticks > 0 ? elapsedTime.Ticks : 0;
var command = NewSyncplayCommand(SendCommandType.Pause);
- SendCommand(session, BroadcastType.AllGroup, command);
+ SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
}
else
{
// Client got lost, sending current state
var command = NewSyncplayCommand(SendCommandType.Pause);
- SendCommand(session, BroadcastType.CurrentSession, command);
+ SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
}
}
@@ -381,16 +390,11 @@ namespace Emby.Server.Implementations.Syncplay
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The seek action.</param>
- private void HandleSeekRequest(SessionInfo session, PlaybackRequest request)
+ /// <param name="cancellationToken">The cancellation token.</param>
+ private void HandleSeekRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
{
// Sanitize PositionTicks
- var ticks = request.PositionTicks ??= 0;
- ticks = ticks >= 0 ? ticks : 0;
- if (_group.PlayingItem.RunTimeTicks != null)
- {
- var runTimeTicks = _group.PlayingItem.RunTimeTicks ??= 0;
- ticks = ticks > runTimeTicks ? runTimeTicks : ticks;
- }
+ var ticks = SanitizePositionTicks(request.PositionTicks);
// Pause and seek
_group.IsPaused = true;
@@ -398,7 +402,7 @@ namespace Emby.Server.Implementations.Syncplay
_group.LastActivity = DateTime.UtcNow;
var command = NewSyncplayCommand(SendCommandType.Seek);
- SendCommand(session, BroadcastType.AllGroup, command);
+ SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
}
/// <summary>
@@ -406,7 +410,8 @@ namespace Emby.Server.Implementations.Syncplay
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The buffering action.</param>
- private void HandleBufferingRequest(SessionInfo session, PlaybackRequest request)
+ /// <param name="cancellationToken">The cancellation token.</param>
+ private void HandleBufferingRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
{
if (!_group.IsPaused)
{
@@ -421,16 +426,16 @@ namespace Emby.Server.Implementations.Syncplay
// Send pause command to all non-buffering sessions
var command = NewSyncplayCommand(SendCommandType.Pause);
- SendCommand(session, BroadcastType.AllReady, command);
+ SendCommand(session, BroadcastType.AllReady, command, cancellationToken);
var updateOthers = NewSyncplayGroupUpdate(GroupUpdateType.GroupWait, session.UserName);
- SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers);
+ SendGroupUpdate(session, BroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
}
else
{
// Client got lost, sending current state
var command = NewSyncplayCommand(SendCommandType.Pause);
- SendCommand(session, BroadcastType.CurrentSession, command);
+ SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
}
}
@@ -439,26 +444,28 @@ namespace Emby.Server.Implementations.Syncplay
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The buffering-done action.</param>
- private void HandleBufferingDoneRequest(SessionInfo session, PlaybackRequest request)
+ /// <param name="cancellationToken">The cancellation token.</param>
+ private void HandleBufferingDoneRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
{
if (_group.IsPaused)
{
_group.SetBuffering(session, false);
- var when = request.When ??= DateTime.UtcNow;
+ var requestTicks = SanitizePositionTicks(request.PositionTicks);
+
+ var when = request.When ?? DateTime.UtcNow;
var currentTime = DateTime.UtcNow;
var elapsedTime = currentTime - when;
- var clientPosition = TimeSpan.FromTicks(request.PositionTicks ??= 0) + elapsedTime;
+ var clientPosition = TimeSpan.FromTicks(requestTicks) + elapsedTime;
var delay = _group.PositionTicks - clientPosition.Ticks;
if (_group.IsBuffering())
{
- // Others are buffering, tell this client to pause when ready
+ // Others are still buffering, tell this client to pause when ready
var command = NewSyncplayCommand(SendCommandType.Pause);
- command.When = currentTime.AddMilliseconds(
- delay
- ).ToUniversalTime().ToString("o");
- SendCommand(session, BroadcastType.CurrentSession, command);
+ var pauseAtTime = currentTime.AddMilliseconds(delay);
+ command.When = DateToUTCString(pauseAtTime);
+ SendCommand(session, BroadcastType.CurrentSession, command, cancellationToken);
}
else
{
@@ -472,7 +479,7 @@ namespace Emby.Server.Implementations.Syncplay
delay
);
var command = NewSyncplayCommand(SendCommandType.Play);
- SendCommand(session, BroadcastType.AllExceptCurrentSession, command);
+ SendCommand(session, BroadcastType.AllExceptCurrentSession, command, cancellationToken);
}
else
{
@@ -485,7 +492,7 @@ namespace Emby.Server.Implementations.Syncplay
);
var command = NewSyncplayCommand(SendCommandType.Play);
- SendCommand(session, BroadcastType.AllGroup, command);
+ SendCommand(session, BroadcastType.AllGroup, command, cancellationToken);
}
}
}
@@ -493,8 +500,25 @@ namespace Emby.Server.Implementations.Syncplay
{
// Group was not waiting, make sure client has latest state
var command = NewSyncplayCommand(SendCommandType.Play);
- SendCommand(session, BroadcastType.CurrentSession, command);
+ 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;
}
+ return ticks;
}
/// <summary>
@@ -505,7 +529,7 @@ namespace Emby.Server.Implementations.Syncplay
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.DefaulPing);
+ _group.UpdatePing(session, request.Ping ?? _group.DefaulPing);
}
/// <inheritdoc />
@@ -517,7 +541,7 @@ namespace Emby.Server.Implementations.Syncplay
PlayingItemName = _group.PlayingItem.Name,
PlayingItemId = _group.PlayingItem.Id.ToString(),
PositionTicks = _group.PositionTicks,
- Participants = _group.Participants.Values.Select(session => session.Session.UserName).ToArray()
+ Participants = _group.Participants.Values.Select(session => session.Session.UserName).Distinct().ToArray()
};
}
}
diff --git a/Emby.Server.Implementations/Syncplay/SyncplayManager.cs b/Emby.Server.Implementations/Syncplay/SyncplayManager.cs
index eb61da7f3..7074e2225 100644
--- a/Emby.Server.Implementations/Syncplay/SyncplayManager.cs
+++ b/Emby.Server.Implementations/Syncplay/SyncplayManager.cs
@@ -114,14 +114,14 @@ namespace Emby.Server.Implementations.Syncplay
{
var session = e.SessionInfo;
if (!IsSessionInGroup(session)) return;
- LeaveGroup(session);
+ LeaveGroup(session, CancellationToken.None);
}
private void OnSessionManagerPlaybackStopped(object sender, PlaybackStopEventArgs e)
{
var session = e.Session;
if (!IsSessionInGroup(session)) return;
- LeaveGroup(session);
+ LeaveGroup(session, CancellationToken.None);
}
private bool IsSessionInGroup(SessionInfo session)
@@ -132,7 +132,13 @@ namespace Emby.Server.Implementations.Syncplay
private bool HasAccessToItem(User user, Guid itemId)
{
var item = _libraryManager.GetItemById(itemId);
- var hasParentalRatingAccess = user.Policy.MaxParentalRating.HasValue ? item.InheritedParentalRatingValue <= user.Policy.MaxParentalRating : true;
+
+ // Check ParentalRating access
+ var hasParentalRatingAccess = true;
+ if (user.Policy.MaxParentalRating.HasValue)
+ {
+ hasParentalRatingAccess = item.InheritedParentalRatingValue <= user.Policy.MaxParentalRating;
+ }
if (!user.Policy.EnableAllFolders && hasParentalRatingAccess)
{
@@ -140,7 +146,7 @@ namespace Emby.Server.Implementations.Syncplay
folder => folder.Id.ToString("N", CultureInfo.InvariantCulture)
);
var intersect = collections.Intersect(user.Policy.EnabledFolders);
- return intersect.Count() > 0;
+ return intersect.Any();
}
else
{
@@ -163,13 +169,13 @@ namespace Emby.Server.Implementations.Syncplay
}
/// <inheritdoc />
- public void NewGroup(SessionInfo session)
+ public void NewGroup(SessionInfo session, CancellationToken cancellationToken)
{
var user = _userManager.GetUserById(session.UserId);
if (user.Policy.SyncplayAccess != SyncplayAccess.CreateAndJoinGroups)
{
- _logger.LogWarning("Syncplaymanager NewGroup: {0} does not have permission to create groups.", session.Id);
+ _logger.LogWarning("NewGroup: {0} does not have permission to create groups.", session.Id);
var error = new GroupUpdate<string>()
{
@@ -183,24 +189,24 @@ namespace Emby.Server.Implementations.Syncplay
{
if (IsSessionInGroup(session))
{
- LeaveGroup(session);
+ LeaveGroup(session, cancellationToken);
}
- var group = new SyncplayController(_logger, _sessionManager, this);
+ var group = new SyncplayController(_sessionManager, this);
_groups[group.GetGroupId().ToString()] = group;
- group.InitGroup(session);
+ group.InitGroup(session, cancellationToken);
}
}
/// <inheritdoc />
- public void JoinGroup(SessionInfo session, string groupId, JoinGroupRequest request)
+ public void JoinGroup(SessionInfo session, string groupId, JoinGroupRequest request, CancellationToken cancellationToken)
{
var user = _userManager.GetUserById(session.UserId);
if (user.Policy.SyncplayAccess == SyncplayAccess.None)
{
- _logger.LogWarning("Syncplaymanager JoinGroup: {0} does not have access to Syncplay.", session.Id);
+ _logger.LogWarning("JoinGroup: {0} does not have access to Syncplay.", session.Id);
var error = new GroupUpdate<string>()
{
@@ -217,11 +223,11 @@ namespace Emby.Server.Implementations.Syncplay
if (group == null)
{
- _logger.LogWarning("Syncplaymanager JoinGroup: {0} tried to join group {0} that does not exist.", session.Id, groupId);
+ _logger.LogWarning("JoinGroup: {0} tried to join group {0} that does not exist.", session.Id, groupId);
var error = new GroupUpdate<string>()
{
- Type = GroupUpdateType.GroupNotJoined
+ Type = GroupUpdateType.GroupDoesNotExist
};
_sessionManager.SendSyncplayGroupUpdate(session.Id.ToString(), error, CancellationToken.None);
return;
@@ -229,7 +235,7 @@ namespace Emby.Server.Implementations.Syncplay
if (!HasAccessToItem(user, group.GetPlayingItemId()))
{
- _logger.LogWarning("Syncplaymanager JoinGroup: {0} does not have access to {1}.", session.Id, group.GetPlayingItemId());
+ _logger.LogWarning("JoinGroup: {0} does not have access to {1}.", session.Id, group.GetPlayingItemId());
var error = new GroupUpdate<string>()
{
@@ -243,15 +249,15 @@ namespace Emby.Server.Implementations.Syncplay
if (IsSessionInGroup(session))
{
if (GetSessionGroup(session).Equals(groupId)) return;
- LeaveGroup(session);
+ LeaveGroup(session, cancellationToken);
}
- group.SessionJoin(session, request);
+ group.SessionJoin(session, request, cancellationToken);
}
}
/// <inheritdoc />
- public void LeaveGroup(SessionInfo session)
+ public void LeaveGroup(SessionInfo session, CancellationToken cancellationToken)
{
// TODO: determine what happens to users that are in a group and get their permissions revoked
lock (_groupsLock)
@@ -261,7 +267,7 @@ namespace Emby.Server.Implementations.Syncplay
if (group == null)
{
- _logger.LogWarning("Syncplaymanager LeaveGroup: {0} does not belong to any group.", session.Id);
+ _logger.LogWarning("LeaveGroup: {0} does not belong to any group.", session.Id);
var error = new GroupUpdate<string>()
{
@@ -271,17 +277,18 @@ namespace Emby.Server.Implementations.Syncplay
return;
}
- group.SessionLeave(session);
+ group.SessionLeave(session, cancellationToken);
if (group.IsGroupEmpty())
{
+ _logger.LogInformation("LeaveGroup: removing empty group {0}.", group.GetGroupId());
_groups.Remove(group.GetGroupId().ToString(), out _);
}
}
}
/// <inheritdoc />
- public List<GroupInfoView> ListGroups(SessionInfo session)
+ public List<GroupInfoView> ListGroups(SessionInfo session, Guid filterItemId)
{
var user = _userManager.GetUserById(session.UserId);
@@ -290,11 +297,11 @@ namespace Emby.Server.Implementations.Syncplay
return new List<GroupInfoView>();
}
- // Filter by playing item if the user is viewing something already
- if (session.NowPlayingItem != null)
+ // Filter by item if requested
+ if (!filterItemId.Equals(Guid.Empty))
{
return _groups.Values.Where(
- group => group.GetPlayingItemId().Equals(session.FullNowPlayingItem.Id) && HasAccessToItem(user, group.GetPlayingItemId())
+ group => group.GetPlayingItemId().Equals(filterItemId) && HasAccessToItem(user, group.GetPlayingItemId())
).Select(
group => group.GetInfo()
).ToList();
@@ -311,13 +318,13 @@ namespace Emby.Server.Implementations.Syncplay
}
/// <inheritdoc />
- public void HandleRequest(SessionInfo session, PlaybackRequest request)
+ public void HandleRequest(SessionInfo session, PlaybackRequest request, CancellationToken cancellationToken)
{
var user = _userManager.GetUserById(session.UserId);
if (user.Policy.SyncplayAccess == SyncplayAccess.None)
{
- _logger.LogWarning("Syncplaymanager HandleRequest: {0} does not have access to Syncplay.", session.Id);
+ _logger.LogWarning("HandleRequest: {0} does not have access to Syncplay.", session.Id);
var error = new GroupUpdate<string>()
{
@@ -334,7 +341,7 @@ namespace Emby.Server.Implementations.Syncplay
if (group == null)
{
- _logger.LogWarning("Syncplaymanager HandleRequest: {0} does not belong to any group.", session.Id);
+ _logger.LogWarning("HandleRequest: {0} does not belong to any group.", session.Id);
var error = new GroupUpdate<string>()
{
@@ -344,7 +351,7 @@ namespace Emby.Server.Implementations.Syncplay
return;
}
- group.HandleRequest(session, request);
+ group.HandleRequest(session, request, cancellationToken);
}
}