2013-09-04 17:02:19 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2013-05-10 12:18:07 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-05-09 17:38:02 +00:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
2013-05-10 12:18:07 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-05-09 17:38:02 +00:00
|
|
|
|
using System;
|
2013-05-09 17:45:46 +00:00
|
|
|
|
using System.Linq;
|
2013-05-09 17:38:02 +00:00
|
|
|
|
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>
|
|
|
|
|
private readonly IDtoService _dtoService;
|
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.
|
2013-05-09 17:38:02 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sessionManager">The session manager.</param>
|
2013-07-29 14:18:55 +00:00
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
2013-09-04 17:02:19 +00:00
|
|
|
|
/// <param name="dtoService">The dto service.</param>
|
2013-10-03 01:22:50 +00:00
|
|
|
|
public SessionWebSocketListener(ISessionManager sessionManager, ILogManager logManager, IDtoService dtoService, IServerApplicationHost appHost)
|
2013-05-09 17:38:02 +00:00
|
|
|
|
{
|
|
|
|
|
_sessionManager = sessionManager;
|
2013-07-29 14:18:55 +00:00
|
|
|
|
_logger = logManager.GetLogger(GetType().Name);
|
2013-09-04 17:02:19 +00:00
|
|
|
|
_dtoService = dtoService;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
_appHost = appHost;
|
2013-05-09 17:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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-05-09 17:38:02 +00:00
|
|
|
|
{
|
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))
|
|
|
|
|
{
|
2013-09-24 15:08:51 +00:00
|
|
|
|
ReportPlaybackStart(message);
|
2013-05-10 12:18:07 +00:00
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(message.MessageType, "PlaybackProgress", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2013-10-03 01:22:50 +00:00
|
|
|
|
ReportPlaybackProgress(message);
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(message.MessageType, "PlaybackStopped", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
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>
|
2013-10-03 18:02:23 +00:00
|
|
|
|
private async void ProcessIdentityMessage(WebSocketMessageInfo message)
|
2013-10-03 01:22:50 +00:00
|
|
|
|
{
|
2013-10-03 18:02:23 +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
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
var client = vals[0];
|
|
|
|
|
var deviceId = vals[1];
|
|
|
|
|
var version = vals[2];
|
2013-10-03 18:02:23 +00:00
|
|
|
|
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");
|
|
|
|
|
|
2013-12-25 14:39:46 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2013-12-26 18:46:38 +00:00
|
|
|
|
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
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
session.NowViewingItemType = vals[0];
|
|
|
|
|
session.NowViewingItemId = vals[1];
|
|
|
|
|
session.NowViewingItemName = vals[2];
|
|
|
|
|
session.NowViewingContext = vals.Length > 3 ? vals[3] : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-09-30 15:05:07 +00:00
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
2013-10-03 18:02:23 +00:00
|
|
|
|
_logger.Error("Unable to find session based on web socket message");
|
2013-05-09 17:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
return result;
|
2013-05-09 17:38:02 +00:00
|
|
|
|
}
|
2013-09-24 15:08:51 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reports the playback start.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
private void ReportPlaybackStart(WebSocketMessageInfo message)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Received PlaybackStart message");
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
|
|
|
|
var session = GetSessionFromMessage(message);
|
2013-09-24 15:08:51 +00:00
|
|
|
|
|
2014-01-04 02:35:41 +00:00
|
|
|
|
if (session != null && session.UserId.HasValue)
|
2013-09-24 15:08:51 +00:00
|
|
|
|
{
|
|
|
|
|
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];
|
|
|
|
|
}
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
2013-09-24 15:08:51 +00:00
|
|
|
|
var info = new PlaybackInfo
|
|
|
|
|
{
|
|
|
|
|
CanSeek = canSeek,
|
|
|
|
|
Item = item,
|
|
|
|
|
SessionId = session.Id,
|
|
|
|
|
QueueableMediaTypes = queueableMediaTypes.Split(',').ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_sessionManager.OnPlaybackStart(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reports the playback progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
private void ReportPlaybackProgress(WebSocketMessageInfo message)
|
|
|
|
|
{
|
|
|
|
|
var session = GetSessionFromMessage(message);
|
|
|
|
|
|
2014-01-04 02:35:41 +00:00
|
|
|
|
if (session != null && session.UserId.HasValue)
|
2013-10-03 01:22:50 +00:00
|
|
|
|
{
|
|
|
|
|
var vals = message.Data.Split('|');
|
|
|
|
|
|
|
|
|
|
var item = _dtoService.GetItemByDtoId(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
|
|
|
|
|
{
|
|
|
|
|
Item = item,
|
|
|
|
|
PositionTicks = positionTicks,
|
|
|
|
|
IsMuted = isMuted,
|
|
|
|
|
IsPaused = isPaused,
|
|
|
|
|
SessionId = session.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_sessionManager.OnPlaybackProgress(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reports the playback stopped.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
private void ReportPlaybackStopped(WebSocketMessageInfo message)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Received PlaybackStopped message");
|
|
|
|
|
|
|
|
|
|
var session = GetSessionFromMessage(message);
|
|
|
|
|
|
2014-01-04 02:35:41 +00:00
|
|
|
|
if (session != null && session.UserId.HasValue)
|
2013-10-03 01:22:50 +00:00
|
|
|
|
{
|
|
|
|
|
var vals = message.Data.Split('|');
|
|
|
|
|
|
|
|
|
|
var item = _dtoService.GetItemByDtoId(vals[0]);
|
|
|
|
|
|
|
|
|
|
long? positionTicks = null;
|
|
|
|
|
|
|
|
|
|
if (vals.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
long pos;
|
|
|
|
|
|
|
|
|
|
if (long.TryParse(vals[1], out pos))
|
|
|
|
|
{
|
|
|
|
|
positionTicks = pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var info = new PlaybackStopInfo
|
|
|
|
|
{
|
|
|
|
|
Item = item,
|
|
|
|
|
PositionTicks = positionTicks,
|
|
|
|
|
SessionId = session.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_sessionManager.OnPlaybackStopped(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-09 17:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|