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

207 lines
7.3 KiB
C#
Raw Normal View History

2013-09-04 10:02:19 -07:00
using MediaBrowser.Common.Net;
2013-05-10 05:18:07 -07:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Session;
2013-05-10 05:18:07 -07:00
using MediaBrowser.Model.Logging;
2013-09-04 10:02:19 -07:00
using MediaBrowser.Model.Net;
using System;
2013-05-09 10:45:46 -07: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 05:18:07 -07:00
/// The _logger
/// </summary>
private readonly ILogger _logger;
2013-09-04 10:02:19 -07:00
/// <summary>
/// The _dto service
/// </summary>
private readonly IDtoService _dtoService;
2013-05-10 05:18:07 -07: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>
2013-09-04 10:02:19 -07:00
/// <param name="dtoService">The dto service.</param>
public SessionWebSocketListener(ISessionManager sessionManager, ILogManager logManager, IDtoService dtoService)
{
_sessionManager = sessionManager;
_logger = logManager.GetLogger(GetType().Name);
2013-09-04 10:02:19 -07:00
_dtoService = dtoService;
}
/// <summary>
/// Processes the message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
public Task ProcessMessage(WebSocketMessageInfo message)
{
2013-05-09 14:05:07 -07:00
if (string.Equals(message.MessageType, "Identity", StringComparison.OrdinalIgnoreCase))
{
_logger.Debug("Received Identity message");
var vals = message.Data.Split('|');
2013-05-09 14:05:07 -07:00
var client = vals[0];
var deviceId = vals[1];
2013-07-09 11:37:41 -07:00
var version = vals[2];
2013-07-09 11:37:41 -07: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 05:18:07 -07:00
if (session != null)
{
2013-05-11 09:41:50 -07:00
var sockets = session.WebSockets.Where(i => i.State == WebSocketState.Open).ToList();
sockets.Add(message.Connection);
session.WebSockets = sockets;
2013-05-10 05:18:07 -07:00
}
2013-05-22 13:36:00 -07:00
else
{
_logger.Warn("Unable to determine session based on identity message: {0}", message.Data);
}
2013-05-10 05:18:07 -07:00
}
else if (string.Equals(message.MessageType, "Context", StringComparison.OrdinalIgnoreCase))
{
2013-05-11 09:41:50 -07:00
var session = _sessionManager.Sessions.FirstOrDefault(i => i.WebSockets.Contains(message.Connection));
if (session != null)
{
2013-05-10 05:18:07 -07:00
var vals = message.Data.Split('|');
session.NowViewingItemType = vals[0];
2013-05-21 20:59:55 -07:00
session.NowViewingItemId = vals[1];
session.NowViewingItemName = vals[2];
session.NowViewingContext = vals.Length > 3 ? vals[3] : null;
2013-05-10 05:18:07 -07:00
}
2013-05-22 13:36:00 -07:00
else
{
_logger.Warn("Unable to determine session based on context message: {0}", message.Data);
}
2013-05-10 05:18:07 -07:00
}
else if (string.Equals(message.MessageType, "PlaybackStart", StringComparison.OrdinalIgnoreCase))
{
ReportPlaybackStart(message);
2013-05-10 05:18:07 -07:00
}
else if (string.Equals(message.MessageType, "PlaybackProgress", StringComparison.OrdinalIgnoreCase))
{
2013-05-11 09:41:50 -07:00
var session = _sessionManager.Sessions.FirstOrDefault(i => i.WebSockets.Contains(message.Connection));
2013-05-10 05:18:07 -07:00
if (session != null && session.User != null)
2013-05-10 05:18:07 -07:00
{
var vals = message.Data.Split('|');
2013-09-04 10:02:19 -07:00
var item = _dtoService.GetItemByDtoId(vals[0]);
2013-05-10 05:18:07 -07: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);
2013-08-29 14:00:27 -07:00
var isMuted = vals.Length > 3 && string.Equals(vals[3], "true", StringComparison.OrdinalIgnoreCase);
2013-05-10 05:18:07 -07:00
2013-08-29 14:00:27 -07:00
_sessionManager.OnPlaybackProgress(item, positionTicks, isPaused, isMuted, session.Id);
2013-05-10 05:18:07 -07:00
}
}
else if (string.Equals(message.MessageType, "PlaybackStopped", StringComparison.OrdinalIgnoreCase))
{
_logger.Debug("Received PlaybackStopped message");
2013-05-11 09:41:50 -07:00
var session = _sessionManager.Sessions.FirstOrDefault(i => i.WebSockets.Contains(message.Connection));
2013-05-10 05:18:07 -07:00
if (session != null && session.User != null)
2013-05-10 05:18:07 -07:00
{
var vals = message.Data.Split('|');
2013-09-04 10:02:19 -07:00
var item = _dtoService.GetItemByDtoId(vals[0]);
2013-05-10 05:18:07 -07:00
long? positionTicks = null;
if (vals.Length > 1)
{
long pos;
if (long.TryParse(vals[1], out pos))
{
positionTicks = pos;
}
}
_sessionManager.OnPlaybackStopped(item, positionTicks, session.Id);
}
}
return _trueTaskResult;
}
/// <summary>
/// Reports the playback start.
/// </summary>
/// <param name="message">The message.</param>
private void ReportPlaybackStart(WebSocketMessageInfo message)
{
_logger.Debug("Received PlaybackStart message");
var session = _sessionManager.Sessions
.FirstOrDefault(i => i.WebSockets.Contains(message.Connection));
if (session != null && session.User != null)
{
var vals = message.Data.Split('|');
var item = _dtoService.GetItemByDtoId(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 PlaybackInfo
{
CanSeek = canSeek,
Item = item,
SessionId = session.Id,
QueueableMediaTypes = queueableMediaTypes.Split(',').ToList()
};
_sessionManager.OnPlaybackStart(info);
}
}
}
}