jellyfin-server/MediaBrowser.Server.Implementations/Session/SessionWebSocketListener.cs
Luke Pulverenti 715119b525 updated nuget
2014-05-17 14:37:40 -04:00

413 lines
13 KiB
C#

using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Session;
using System;
using System.Globalization;
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>
/// The _logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// The _dto service
/// </summary>
private readonly IJsonSerializer _json;
private readonly IServerApplicationHost _appHost;
/// <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>
/// <param name="appHost">The application host.</param>
public SessionWebSocketListener(ISessionManager sessionManager, ILogManager logManager, IServerApplicationHost appHost, IJsonSerializer json)
{
_sessionManager = sessionManager;
_logger = logManager.GetLogger(GetType().Name);
_appHost = appHost;
_json = json;
}
/// <summary>
/// Processes the message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
public Task ProcessMessage(WebSocketMessageInfo message)
{
if (string.Equals(message.MessageType, "Identity", StringComparison.OrdinalIgnoreCase))
{
ProcessIdentityMessage(message);
}
else if (string.Equals(message.MessageType, "Context", StringComparison.OrdinalIgnoreCase))
{
ProcessContextMessage(message);
}
else if (string.Equals(message.MessageType, "PlaybackStart", StringComparison.OrdinalIgnoreCase))
{
OnPlaybackStart(message);
}
else if (string.Equals(message.MessageType, "PlaybackProgress", StringComparison.OrdinalIgnoreCase))
{
OnPlaybackProgress(message);
}
else if (string.Equals(message.MessageType, "PlaybackStopped", StringComparison.OrdinalIgnoreCase))
{
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))
{
ReportPlaybackStopped(message);
}
return _trueTaskResult;
}
/// <summary>
/// Processes the identity message.
/// </summary>
/// <param name="message">The message.</param>
private async void ProcessIdentityMessage(WebSocketMessageInfo message)
{
_logger.Debug("Received Identity message: " + message.Data);
var vals = message.Data.Split('|');
if (vals.Length < 3)
{
_logger.Error("Client sent invalid identity message.");
return;
}
var client = vals[0];
var deviceId = vals[1];
var version = vals[2];
var deviceName = vals.Length > 3 ? vals[3] : string.Empty;
var session = _sessionManager.Sessions
.FirstOrDefault(i => string.Equals(i.DeviceId, deviceId) &&
string.Equals(i.Client, client) &&
string.Equals(i.ApplicationVersion, version));
if (session == null && !string.IsNullOrEmpty(deviceName))
{
_logger.Debug("Logging session activity");
await _sessionManager.LogSessionActivity(client, version, deviceId, deviceName, message.Connection.RemoteEndPoint, null).ConfigureAwait(false);
session = _sessionManager.Sessions
.FirstOrDefault(i => string.Equals(i.DeviceId, deviceId) &&
string.Equals(i.Client, client) &&
string.Equals(i.ApplicationVersion, version));
}
if (session != null)
{
var controller = session.SessionController as WebSocketController;
if (controller == null)
{
controller = new WebSocketController(session, _appHost, _logger, _sessionManager);
}
controller.AddWebSocket(message.Connection);
session.SessionController = controller;
}
else
{
_logger.Warn("Unable to determine session based on identity message: {0}", message.Data);
}
}
/// <summary>
/// Processes the context message.
/// </summary>
/// <param name="message">The message.</param>
private void ProcessContextMessage(WebSocketMessageInfo message)
{
var session = GetSessionFromMessage(message);
if (session != null)
{
var vals = message.Data.Split('|');
_sessionManager.ReportNowViewingItem(session.Id, vals[1]);
}
}
/// <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;
if (controller != null)
{
if (controller.Sockets.Any(s => s.Id == message.Connection.Id))
{
return true;
}
}
return false;
});
if (result == null)
{
_logger.Error("Unable to find session based on web socket message");
}
return result;
}
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
/// <summary>
/// Reports the playback start.
/// </summary>
/// <param name="message">The message.</param>
private void OnPlaybackStart(WebSocketMessageInfo message)
{
_logger.Debug("Received PlaybackStart message");
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
{
var vals = message.Data.Split('|');
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];
}
var info = new PlaybackStartInfo
{
CanSeek = canSeek,
ItemId = itemId,
SessionId = session.Id,
QueueableMediaTypes = queueableMediaTypes.Split(',').ToList()
};
if (vals.Length > 3)
{
info.MediaSourceId = vals[3];
}
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);
}
}
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);
}
}
/// <summary>
/// Reports the playback progress.
/// </summary>
/// <param name="message">The message.</param>
private void OnPlaybackProgress(WebSocketMessageInfo message)
{
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
{
var vals = message.Data.Split('|');
var itemId = vals[0];
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
{
ItemId = itemId,
PositionTicks = positionTicks,
IsMuted = isMuted,
IsPaused = isPaused,
SessionId = session.Id
};
if (vals.Length > 4)
{
info.MediaSourceId = vals[4];
}
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);
}
_sessionManager.OnPlaybackProgress(info);
}
}
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);
}
}
/// <summary>
/// Reports the playback stopped.
/// </summary>
/// <param name="message">The message.</param>
private void OnPlaybackStopped(WebSocketMessageInfo message)
{
_logger.Debug("Received PlaybackStopped message");
var session = GetSessionFromMessage(message);
if (session != null && session.UserId.HasValue)
{
var vals = message.Data.Split('|');
var itemId = vals[0];
long? positionTicks = null;
if (vals.Length > 1)
{
long pos;
if (long.TryParse(vals[1], out pos))
{
positionTicks = pos;
}
}
var info = new PlaybackStopInfo
{
ItemId = itemId,
PositionTicks = positionTicks,
SessionId = session.Id
};
if (vals.Length > 2)
{
info.MediaSourceId = vals[2];
}
_sessionManager.OnPlaybackStopped(info);
}
}
}
}