jellyfin/MediaBrowser.Server.Implementations/Session/WebSocketController.cs

249 lines
7.8 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Net;
2013-10-02 18:22:50 -07:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Session;
2013-10-02 18:22:50 -07:00
using MediaBrowser.Model.Entities;
2014-05-16 21:24:10 -07:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Session;
2013-10-02 18:22:50 -07:00
using MediaBrowser.Model.System;
using System;
2013-10-02 18:22:50 -07:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Session
{
2013-10-02 12:08:58 -07:00
public class WebSocketController : ISessionController
{
2013-10-02 18:22:50 -07:00
public SessionInfo Session { get; private set; }
2014-05-17 11:37:40 -07:00
public IReadOnlyList<IWebSocketConnection> Sockets { get; private set; }
2013-10-02 18:22:50 -07:00
private readonly IServerApplicationHost _appHost;
2014-05-16 21:24:10 -07:00
private readonly ILogger _logger;
2013-10-02 18:22:50 -07:00
2014-05-17 11:37:40 -07:00
private readonly ISessionManager _sessionManager;
public WebSocketController(SessionInfo session, IServerApplicationHost appHost, ILogger logger, ISessionManager sessionManager)
{
2013-10-02 18:22:50 -07:00
Session = session;
_appHost = appHost;
2014-05-16 21:24:10 -07:00
_logger = logger;
2014-05-17 11:37:40 -07:00
_sessionManager = sessionManager;
2013-10-02 18:22:50 -07:00
Sockets = new List<IWebSocketConnection>();
}
2013-10-02 18:22:50 -07:00
public bool IsSessionActive
{
get
{
return Sockets.Any(i => i.State == WebSocketState.Open);
}
}
2014-05-17 11:37:40 -07:00
public bool SupportsMediaControl
{
get { return GetActiveSockets().Any(); }
}
2014-05-16 21:24:10 -07:00
private IEnumerable<IWebSocketConnection> GetActiveSockets()
2013-10-02 18:22:50 -07:00
{
2014-05-16 21:24:10 -07:00
return Sockets
2013-10-02 18:22:50 -07:00
.OrderByDescending(i => i.LastActivityDate)
2014-05-16 21:24:10 -07:00
.Where(i => i.State == WebSocketState.Open);
}
2014-05-17 11:37:40 -07:00
public void AddWebSocket(IWebSocketConnection connection)
{
var sockets = Sockets.ToList();
sockets.Add(connection);
Sockets = sockets;
connection.Closed += connection_Closed;
}
void connection_Closed(object sender, EventArgs e)
{
var capabilities = new SessionCapabilities
{
PlayableMediaTypes = Session.PlayableMediaTypes,
SupportedCommands = Session.SupportedCommands,
SupportsMediaControl = SupportsMediaControl
};
_sessionManager.ReportCapabilities(Session.Id, capabilities);
}
2014-05-16 21:24:10 -07:00
private IWebSocketConnection GetActiveSocket()
{
var socket = GetActiveSockets()
.FirstOrDefault();
if (socket == null)
{
throw new InvalidOperationException("The requested session does not have an open web socket.");
}
return socket;
}
2013-10-02 18:22:50 -07:00
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessage(new WebSocketMessage<PlayRequest>
{
MessageType = "Play",
Data = command
}, cancellationToken);
}
2013-10-02 18:22:50 -07:00
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessage(new WebSocketMessage<PlaystateRequest>
{
MessageType = "Playstate",
Data = command
}, cancellationToken);
}
2013-10-02 18:22:50 -07:00
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<LibraryUpdateInfo>
2013-10-02 18:22:50 -07:00
{
MessageType = "LibraryChanged",
2013-10-02 18:22:50 -07:00
Data = info
}, cancellationToken);
}
/// <summary>
/// Sends the restart required message.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
2013-10-02 18:22:50 -07:00
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<SystemInfo>
2013-10-02 18:22:50 -07:00
{
MessageType = "RestartRequired",
Data = _appHost.GetSystemInfo()
}, cancellationToken);
}
/// <summary>
/// Sends the user data change info.
/// </summary>
/// <param name="info">The info.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<UserDataChangeInfo>
{
MessageType = "UserDataChanged",
Data = info
}, cancellationToken);
}
/// <summary>
/// Sends the server shutdown notification.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<string>
{
MessageType = "ServerShuttingDown",
Data = string.Empty
}, cancellationToken);
}
/// <summary>
/// Sends the server restart notification.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public Task SendServerRestartNotification(CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<string>
{
MessageType = "ServerRestarting",
Data = string.Empty
}, cancellationToken);
}
2014-03-28 12:58:18 -07:00
2014-03-31 14:04:22 -07:00
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
2014-03-28 12:58:18 -07:00
{
2014-05-16 21:24:10 -07:00
return SendMessage(new WebSocketMessage<GeneralCommand>
2014-03-28 12:58:18 -07:00
{
2014-03-31 14:04:22 -07:00
MessageType = "GeneralCommand",
2014-03-28 12:58:18 -07:00
Data = command
}, cancellationToken);
}
2014-04-06 10:53:23 -07:00
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<SessionInfoDto>
2014-04-06 10:53:23 -07:00
{
MessageType = "SessionEnded",
Data = sessionInfo
}, cancellationToken);
}
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<SessionInfoDto>
{
MessageType = "PlaybackStart",
Data = sessionInfo
}, cancellationToken);
}
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
2014-05-16 21:24:10 -07:00
return SendMessages(new WebSocketMessage<SessionInfoDto>
{
MessageType = "PlaybackStopped",
Data = sessionInfo
}, cancellationToken);
}
2014-05-16 21:24:10 -07:00
private Task SendMessage<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
{
var socket = GetActiveSocket();
return socket.SendAsync(message, cancellationToken);
}
private Task SendMessages<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
{
var tasks = GetActiveSockets().Select(i => Task.Run(async () =>
{
try
{
await i.SendAsync(message, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending web socket message", ex);
}
}, cancellationToken));
return Task.WhenAll(tasks);
}
}
}