jellyfin-server/Emby.Server.Implementations/Session/SessionWebSocketListener.cs

462 lines
15 KiB
C#
Raw Normal View History

2015-03-08 19:06:15 +00:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
2015-03-08 19:48:30 +00:00
using MediaBrowser.Model.Events;
2013-05-10 12:18:07 +00:00
using MediaBrowser.Model.Logging;
2014-04-16 02:17:48 +00:00
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Session;
using System;
2015-03-08 19:48:30 +00:00
using System.Collections.Specialized;
2014-04-16 02:17:48 +00:00
using System.Globalization;
2013-05-09 17:45:46 +00:00
using System.Linq;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
2016-11-03 23:35:19 +00:00
namespace Emby.Server.Implementations.Session
{
/// <summary>
/// Class SessionWebSocketListener
/// </summary>
2015-03-08 19:48:30 +00:00
public class SessionWebSocketListener : IWebSocketListener, IDisposable
{
/// <summary>
/// The _true task result
/// </summary>
private readonly Task _trueTaskResult = Task.FromResult(true);
/// <summary>
/// The _session manager
/// </summary>
private readonly ISessionManager _sessionManager;
/// <summary>
2013-05-10 12:18:07 +00:00
/// The _logger
/// </summary>
private readonly ILogger _logger;
2013-09-04 17:02:19 +00:00
/// <summary>
/// The _dto service
/// </summary>
2014-04-16 02:17:48 +00:00
private readonly IJsonSerializer _json;
2013-05-10 12:18:07 +00:00
2015-03-08 19:48:30 +00:00
private readonly IHttpServer _httpServer;
private readonly IServerManager _serverManager;
2013-05-10 12:18:07 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="SessionWebSocketListener" /> class.
/// </summary>
/// <param name="sessionManager">The session manager.</param>
/// <param name="logManager">The log manager.</param>
2014-05-18 19:58:42 +00:00
/// <param name="json">The json.</param>
2015-03-08 19:48:30 +00:00
/// <param name="httpServer">The HTTP server.</param>
/// <param name="serverManager">The server manager.</param>
public SessionWebSocketListener(ISessionManager sessionManager, ILogManager logManager, IJsonSerializer json, IHttpServer httpServer, IServerManager serverManager)
{
_sessionManager = sessionManager;
_logger = logManager.GetLogger(GetType().Name);
2014-04-16 02:17:48 +00:00
_json = json;
2015-03-08 19:48:30 +00:00
_httpServer = httpServer;
_serverManager = serverManager;
serverManager.WebSocketConnected += _serverManager_WebSocketConnected;
}
2015-03-09 19:40:03 +00:00
async void _serverManager_WebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
2015-03-08 19:48:30 +00:00
{
2015-03-09 19:40:03 +00:00
var session = await GetSession(e.Argument.QueryString, e.Argument.RemoteEndPoint).ConfigureAwait(false);
2015-03-08 19:48:30 +00:00
if (session != null)
{
var controller = session.SessionController as WebSocketController;
if (controller == null)
{
controller = new WebSocketController(session, _logger, _sessionManager);
}
controller.AddWebSocket(e.Argument);
session.SessionController = controller;
}
else
{
_logger.Warn("Unable to determine session based on url: {0}", e.Argument.Url);
}
}
2016-10-25 19:02:04 +00:00
private Task<SessionInfo> GetSession(QueryParamCollection queryString, string remoteEndpoint)
2015-03-08 19:48:30 +00:00
{
2015-03-16 16:47:14 +00:00
if (queryString == null)
{
throw new ArgumentNullException("queryString");
}
2015-03-08 19:48:30 +00:00
var token = queryString["api_key"];
if (string.IsNullOrWhiteSpace(token))
{
return Task.FromResult<SessionInfo>(null);
}
2015-03-13 01:55:22 +00:00
var deviceId = queryString["deviceId"];
return _sessionManager.GetSessionByAuthenticationToken(token, deviceId, remoteEndpoint);
2015-03-08 19:48:30 +00:00
}
public void Dispose()
{
_serverManager.WebSocketConnected -= _serverManager_WebSocketConnected;
2017-09-05 19:49:02 +00:00
GC.SuppressFinalize(this);
}
/// <summary>
/// Processes the message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
public Task ProcessMessage(WebSocketMessageInfo message)
{
2013-05-09 21:05:07 +00:00
if (string.Equals(message.MessageType, "Identity", StringComparison.OrdinalIgnoreCase))
{
2013-10-03 01:22:50 +00:00
ProcessIdentityMessage(message);
2013-05-10 12:18:07 +00:00
}
else if (string.Equals(message.MessageType, "Context", StringComparison.OrdinalIgnoreCase))
{
2013-10-03 01:22:50 +00:00
ProcessContextMessage(message);
2013-05-10 12:18:07 +00:00
}
else if (string.Equals(message.MessageType, "PlaybackStart", StringComparison.OrdinalIgnoreCase))
{
2014-04-16 02:17:48 +00:00
OnPlaybackStart(message);
2013-05-10 12:18:07 +00:00
}
else if (string.Equals(message.MessageType, "PlaybackProgress", StringComparison.OrdinalIgnoreCase))
{
2014-04-16 02:17:48 +00:00
OnPlaybackProgress(message);
2013-10-03 01:22:50 +00:00
}
else if (string.Equals(message.MessageType, "PlaybackStopped", StringComparison.OrdinalIgnoreCase))
2014-04-16 02:17:48 +00:00
{
OnPlaybackStopped(message);
}
else if (string.Equals(message.MessageType, "ReportPlaybackStart", StringComparison.OrdinalIgnoreCase))
{
ReportPlaybackStart(message);
}
else if (string.Equals(message.MessageType, "ReportPlaybackProgress", StringComparison.OrdinalIgnoreCase))
{
ReportPlaybackProgress(message);
}
else if (string.Equals(message.MessageType, "ReportPlaybackStopped", StringComparison.OrdinalIgnoreCase))
2013-10-03 01:22:50 +00:00
{
ReportPlaybackStopped(message);
}
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
return _trueTaskResult;
}
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
/// <summary>
/// Processes the identity message.
/// </summary>
/// <param name="message">The message.</param>
private async void ProcessIdentityMessage(WebSocketMessageInfo message)
2013-10-03 01:22:50 +00:00
{
_logger.Debug("Received Identity message: " + message.Data);
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
var vals = message.Data.Split('|');
2013-05-10 12:18:07 +00:00
2014-01-10 13:52:01 +00:00
if (vals.Length < 3)
{
_logger.Error("Client sent invalid identity message.");
return;
}
2013-10-03 01:22:50 +00:00
var client = vals[0];
var deviceId = vals[1];
var version = vals[2];
var deviceName = vals.Length > 3 ? vals[3] : string.Empty;
2014-06-25 15:12:39 +00:00
var session = _sessionManager.GetSession(deviceId, client, version);
2013-05-10 12:18:07 +00:00
2013-10-04 19:48:31 +00:00
if (session == null && !string.IsNullOrEmpty(deviceName))
{
_logger.Debug("Logging session activity");
2014-06-25 15:12:39 +00:00
session = await _sessionManager.LogSessionActivity(client, version, deviceId, deviceName, message.Connection.RemoteEndPoint, null).ConfigureAwait(false);
2013-10-04 19:48:31 +00:00
}
2013-10-03 01:22:50 +00:00
if (session != null)
{
var controller = session.SessionController as WebSocketController;
if (controller == null)
{
2014-05-18 19:58:42 +00:00
controller = new WebSocketController(session, _logger, _sessionManager);
}
2014-05-17 18:37:40 +00:00
controller.AddWebSocket(message.Connection);
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
session.SessionController = controller;
2013-05-10 12:18:07 +00:00
}
2013-10-03 01:22:50 +00:00
else
2013-05-10 12:18:07 +00:00
{
2013-10-03 01:22:50 +00:00
_logger.Warn("Unable to determine session based on identity message: {0}", message.Data);
}
}
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
/// <summary>
/// Processes the context message.
/// </summary>
/// <param name="message">The message.</param>
private void ProcessContextMessage(WebSocketMessageInfo message)
{
var session = GetSessionFromMessage(message);
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
if (session != null)
{
var vals = message.Data.Split('|');
2013-05-10 12:18:07 +00:00
2016-05-30 16:08:46 +00:00
var itemId = vals[1];
if (!string.IsNullOrWhiteSpace(itemId))
{
_sessionManager.ReportNowViewingItem(session.Id, itemId);
}
2013-10-03 01:22:50 +00:00
}
}
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
/// <summary>
/// Gets the session from message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>SessionInfo.</returns>
private SessionInfo GetSessionFromMessage(WebSocketMessageInfo message)
{
var result = _sessionManager.Sessions.FirstOrDefault(i =>
{
var controller = i.SessionController as WebSocketController;
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
if (controller != null)
{
if (controller.Sockets.Any(s => s.Id == message.Connection.Id))
{
return true;
2013-05-10 12:18:07 +00:00
}
2013-10-03 01:22:50 +00:00
}
2013-05-10 12:18:07 +00:00
2013-10-03 01:22:50 +00:00
return false;
2013-10-03 01:22:50 +00:00
});
if (result == null)
{
_logger.Error("Unable to find session based on web socket message");
}
2013-10-03 01:22:50 +00:00
return result;
}
2014-04-06 17:53:23 +00:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
2014-06-25 15:12:39 +00:00
/// <summary>
/// Reports the playback start.
/// </summary>
/// <param name="message">The message.</param>
2014-04-16 02:17:48 +00:00
private void OnPlaybackStart(WebSocketMessageInfo message)
{
_logger.Debug("Received PlaybackStart message");
2013-10-03 01:22:50 +00:00
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
{
var vals = message.Data.Split('|');
2014-04-16 02:17:48 +00:00
var itemId = vals[0];
var canSeek = true;
if (vals.Length > 1)
{
canSeek = string.Equals(vals[1], "true", StringComparison.OrdinalIgnoreCase);
}
if (vals.Length > 2)
{
2017-04-17 20:33:07 +00:00
// vals[2] used to be QueueableMediaTypes
}
2013-10-03 01:22:50 +00:00
2014-04-16 02:17:48 +00:00
var info = new PlaybackStartInfo
{
CanSeek = canSeek,
2014-04-16 02:17:48 +00:00
ItemId = itemId,
2017-04-17 20:33:07 +00:00
SessionId = session.Id
};
if (vals.Length > 3)
{
2014-03-22 16:16:43 +00:00
info.MediaSourceId = vals[3];
}
2014-04-06 17:53:23 +00:00
if (vals.Length > 4 && !string.IsNullOrWhiteSpace(vals[4]))
{
info.AudioStreamIndex = int.Parse(vals[4], _usCulture);
}
if (vals.Length > 5 && !string.IsNullOrWhiteSpace(vals[5]))
{
info.SubtitleStreamIndex = int.Parse(vals[5], _usCulture);
}
_sessionManager.OnPlaybackStart(info);
}
}
2013-10-03 01:22:50 +00:00
2014-04-16 02:17:48 +00:00
private void ReportPlaybackStart(WebSocketMessageInfo message)
{
_logger.Debug("Received ReportPlaybackStart message");
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
{
var info = _json.DeserializeFromString<PlaybackStartInfo>(message.Data);
info.SessionId = session.Id;
_sessionManager.OnPlaybackStart(info);
}
}
private void ReportPlaybackProgress(WebSocketMessageInfo message)
{
//_logger.Debug("Received ReportPlaybackProgress message");
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
{
var info = _json.DeserializeFromString<PlaybackProgressInfo>(message.Data);
info.SessionId = session.Id;
_sessionManager.OnPlaybackProgress(info);
}
}
2014-06-25 15:12:39 +00:00
2013-10-03 01:22:50 +00:00
/// <summary>
/// Reports the playback progress.
/// </summary>
/// <param name="message">The message.</param>
2014-04-16 02:17:48 +00:00
private void OnPlaybackProgress(WebSocketMessageInfo message)
2013-10-03 01:22:50 +00:00
{
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
2013-10-03 01:22:50 +00:00
{
var vals = message.Data.Split('|');
2014-04-16 02:17:48 +00:00
var itemId = vals[0];
2013-10-03 01:22:50 +00:00
long? positionTicks = null;
if (vals.Length > 1)
{
long pos;
if (long.TryParse(vals[1], out pos))
{
positionTicks = pos;
}
}
var isPaused = vals.Length > 2 && string.Equals(vals[2], "true", StringComparison.OrdinalIgnoreCase);
var isMuted = vals.Length > 3 && string.Equals(vals[3], "true", StringComparison.OrdinalIgnoreCase);
var info = new PlaybackProgressInfo
{
2014-04-16 02:17:48 +00:00
ItemId = itemId,
2013-10-03 01:22:50 +00:00
PositionTicks = positionTicks,
IsMuted = isMuted,
IsPaused = isPaused,
SessionId = session.Id
};
if (vals.Length > 4)
{
2014-03-22 16:16:43 +00:00
info.MediaSourceId = vals[4];
}
2014-04-06 17:53:23 +00:00
if (vals.Length > 5 && !string.IsNullOrWhiteSpace(vals[5]))
{
info.VolumeLevel = int.Parse(vals[5], _usCulture);
}
if (vals.Length > 5 && !string.IsNullOrWhiteSpace(vals[6]))
{
info.AudioStreamIndex = int.Parse(vals[6], _usCulture);
}
if (vals.Length > 7 && !string.IsNullOrWhiteSpace(vals[7]))
{
info.SubtitleStreamIndex = int.Parse(vals[7], _usCulture);
}
2013-10-03 01:22:50 +00:00
_sessionManager.OnPlaybackProgress(info);
}
}
2014-04-16 02:17:48 +00:00
private void ReportPlaybackStopped(WebSocketMessageInfo message)
{
_logger.Debug("Received ReportPlaybackStopped message");
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
{
var info = _json.DeserializeFromString<PlaybackStopInfo>(message.Data);
info.SessionId = session.Id;
_sessionManager.OnPlaybackStopped(info);
}
}
2014-06-25 15:12:39 +00:00
2013-10-03 01:22:50 +00:00
/// <summary>
/// Reports the playback stopped.
/// </summary>
/// <param name="message">The message.</param>
2014-04-16 02:17:48 +00:00
private void OnPlaybackStopped(WebSocketMessageInfo message)
2013-10-03 01:22:50 +00:00
{
_logger.Debug("Received PlaybackStopped message");
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
2013-10-03 01:22:50 +00:00
{
var vals = message.Data.Split('|');
2014-04-16 02:17:48 +00:00
var itemId = vals[0];
2013-10-03 01:22:50 +00:00
long? positionTicks = null;
if (vals.Length > 1)
{
long pos;
if (long.TryParse(vals[1], out pos))
{
positionTicks = pos;
}
}
var info = new PlaybackStopInfo
{
2014-04-16 02:17:48 +00:00
ItemId = itemId,
2013-10-03 01:22:50 +00:00
PositionTicks = positionTicks,
SessionId = session.Id
};
if (vals.Length > 2)
{
2014-03-22 16:16:43 +00:00
info.MediaSourceId = vals[2];
}
2013-10-03 01:22:50 +00:00
_sessionManager.OnPlaybackStopped(info);
}
}
}
}