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

413 lines
13 KiB
C#
Raw Normal View History

2014-04-16 02:17:48 +00:00
using MediaBrowser.Common.Net;
2013-10-03 01:22:50 +00:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Session;
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;
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;
namespace MediaBrowser.Server.Implementations.Session
{
/// <summary>
/// Class SessionWebSocketListener
/// </summary>
public class SessionWebSocketListener : IWebSocketListener
{
/// <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-10-03 01:22:50 +00:00
private readonly IServerApplicationHost _appHost;
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-04-16 02:17:48 +00:00
/// <param name="appHost">The application host.</param>
public SessionWebSocketListener(ISessionManager sessionManager, ILogManager logManager, IServerApplicationHost appHost, IJsonSerializer json)
{
_sessionManager = sessionManager;
_logger = logManager.GetLogger(GetType().Name);
2013-10-03 01:22:50 +00:00
_appHost = appHost;
2014-04-16 02:17:48 +00:00
_json = json;
}
/// <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;
2013-10-03 01:22:50 +00:00
var session = _sessionManager.Sessions
.FirstOrDefault(i => string.Equals(i.DeviceId, deviceId) &&
string.Equals(i.Client, client) &&
string.Equals(i.ApplicationVersion, 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");
await _sessionManager.LogSessionActivity(client, version, deviceId, deviceName, message.Connection.RemoteEndPoint, null).ConfigureAwait(false);
2013-10-04 19:48:31 +00:00
session = _sessionManager.Sessions
.FirstOrDefault(i => string.Equals(i.DeviceId, deviceId) &&
string.Equals(i.Client, client) &&
string.Equals(i.ApplicationVersion, version));
}
2013-10-03 01:22:50 +00:00
if (session != null)
{
var controller = session.SessionController as WebSocketController;
if (controller == null)
{
controller = new WebSocketController(session, _appHost);
}
2013-10-03 01:22:50 +00:00
controller.Sockets.Add(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
2014-05-01 03:24:55 +00:00
_sessionManager.ReportNowViewingItem(session.Id, vals[1]);
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");
/// <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 queueableMediaTypes = string.Empty;
var canSeek = true;
if (vals.Length > 1)
{
canSeek = string.Equals(vals[1], "true", StringComparison.OrdinalIgnoreCase);
}
if (vals.Length > 2)
{
queueableMediaTypes = vals[2];
}
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,
SessionId = session.Id,
QueueableMediaTypes = queueableMediaTypes.Split(',').ToList()
};
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);
}
}
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);
}
}
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);
}
}
}
}