progress on remote control

This commit is contained in:
Luke Pulverenti
2013-05-10 08:18:07 -04:00
parent 35a7986b3f
commit 44f33fdb55
16 changed files with 335 additions and 73 deletions

View File

@@ -207,7 +207,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="swagger-ui\css\hightlight.default.css" />
<Content Include="swagger-ui\css\hightlight.default.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="swagger-ui\css\screen.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View File

@@ -32,7 +32,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// <summary>
/// The _send semaphore
/// </summary>
private readonly SemaphoreSlim _sendSemaphore = new SemaphoreSlim(1,1);
private readonly SemaphoreSlim _sendSemaphore = new SemaphoreSlim(1, 1);
/// <summary>
/// The logger
@@ -100,7 +100,13 @@ namespace MediaBrowser.Server.Implementations.ServerManager
using (var memoryStream = new MemoryStream(bytes))
{
info = (WebSocketMessageInfo)_jsonSerializer.DeserializeFromStream(memoryStream, typeof(WebSocketMessageInfo));
var stub = (WebSocketMessage<object>)_jsonSerializer.DeserializeFromStream(memoryStream, typeof(WebSocketMessage<object>));
info = new WebSocketMessageInfo
{
MessageType = stub.MessageType,
Data = stub.Data == null ? null : stub.Data.ToString()
};
}
info.Connection = this;
@@ -163,7 +169,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager
{
throw new ArgumentNullException("cancellationToken");
}
cancellationToken.ThrowIfCancellationRequested();
// Per msdn docs, attempting to send simultaneous messages will result in one failing.

View File

@@ -1,14 +1,12 @@
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Session;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -18,10 +16,19 @@ using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Session
{
/// <summary>
/// Class SessionManager
/// </summary>
public class SessionManager : ISessionManager
{
/// <summary>
/// The _user data repository
/// </summary>
private readonly IUserDataRepository _userDataRepository;
/// <summary>
/// The _user repository
/// </summary>
private readonly IUserRepository _userRepository;
/// <summary>
@@ -54,6 +61,13 @@ namespace MediaBrowser.Server.Implementations.Session
/// </summary>
public event EventHandler<PlaybackProgressEventArgs> PlaybackStopped;
/// <summary>
/// Initializes a new instance of the <see cref="SessionManager"/> class.
/// </summary>
/// <param name="userDataRepository">The user data repository.</param>
/// <param name="configurationManager">The configuration manager.</param>
/// <param name="logger">The logger.</param>
/// <param name="userRepository">The user repository.</param>
public SessionManager(IUserDataRepository userDataRepository, IServerConfigurationManager configurationManager, ILogger logger, IUserRepository userRepository)
{
_userDataRepository = userDataRepository;
@@ -66,20 +80,14 @@ namespace MediaBrowser.Server.Implementations.Session
/// Gets all connections.
/// </summary>
/// <value>All connections.</value>
public IEnumerable<SessionInfo> AllConnections
public IEnumerable<SessionInfo> Sessions
{
get { return _activeConnections.Values.OrderByDescending(c => c.LastActivityDate); }
get { return _activeConnections.Values.OrderByDescending(c => c.LastActivityDate).ToList(); }
}
/// <summary>
/// Gets the active connections.
/// The _true task result
/// </summary>
/// <value>The active connections.</value>
public IEnumerable<SessionInfo> RecentConnections
{
get { return AllConnections.Where(c => (DateTime.UtcNow - c.LastActivityDate).TotalMinutes <= 5); }
}
private readonly Task _trueTaskResult = Task.FromResult(true);
/// <summary>
@@ -124,11 +132,13 @@ namespace MediaBrowser.Server.Implementations.Session
/// <param name="deviceId">The device id.</param>
/// <param name="deviceName">Name of the device.</param>
/// <param name="item">The item.</param>
/// <param name="isPaused">if set to <c>true</c> [is paused].</param>
/// <param name="currentPositionTicks">The current position ticks.</param>
private void UpdateNowPlayingItemId(User user, string clientType, string deviceId, string deviceName, BaseItem item, long? currentPositionTicks = null)
private void UpdateNowPlayingItemId(User user, string clientType, string deviceId, string deviceName, BaseItem item, bool isPaused, long? currentPositionTicks = null)
{
var conn = GetConnection(clientType, deviceId, deviceName, user);
conn.IsPaused = isPaused;
conn.NowPlayingPositionTicks = currentPositionTicks;
conn.NowPlayingItem = item;
conn.LastActivityDate = DateTime.UtcNow;
@@ -150,6 +160,7 @@ namespace MediaBrowser.Server.Implementations.Session
{
conn.NowPlayingItem = null;
conn.NowPlayingPositionTicks = null;
conn.IsPaused = null;
}
}
@@ -187,7 +198,8 @@ namespace MediaBrowser.Server.Implementations.Session
/// <param name="clientType">Type of the client.</param>
/// <param name="deviceId">The device id.</param>
/// <param name="deviceName">Name of the device.</param>
/// <exception cref="System.ArgumentNullException"></exception>
/// <exception cref="System.ArgumentNullException">
/// </exception>
public void OnPlaybackStart(User user, BaseItem item, string clientType, string deviceId, string deviceName)
{
if (user == null)
@@ -199,7 +211,7 @@ namespace MediaBrowser.Server.Implementations.Session
throw new ArgumentNullException();
}
UpdateNowPlayingItemId(user, clientType, deviceId, deviceName, item);
UpdateNowPlayingItemId(user, clientType, deviceId, deviceName, item, false);
// Nothing to save here
// Fire events to inform plugins
@@ -216,12 +228,14 @@ namespace MediaBrowser.Server.Implementations.Session
/// <param name="user">The user.</param>
/// <param name="item">The item.</param>
/// <param name="positionTicks">The position ticks.</param>
/// <param name="isPaused">if set to <c>true</c> [is paused].</param>
/// <param name="clientType">Type of the client.</param>
/// <param name="deviceId">The device id.</param>
/// <param name="deviceName">Name of the device.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public async Task OnPlaybackProgress(User user, BaseItem item, long? positionTicks, string clientType, string deviceId, string deviceName)
/// <exception cref="System.ArgumentNullException">
/// </exception>
public async Task OnPlaybackProgress(User user, BaseItem item, long? positionTicks, bool isPaused, string clientType, string deviceId, string deviceName)
{
if (user == null)
{
@@ -232,7 +246,7 @@ namespace MediaBrowser.Server.Implementations.Session
throw new ArgumentNullException();
}
UpdateNowPlayingItemId(user, clientType, deviceId, deviceName, item, positionTicks);
UpdateNowPlayingItemId(user, clientType, deviceId, deviceName, item, isPaused, positionTicks);
var key = item.GetUserDataKey();
@@ -262,7 +276,8 @@ namespace MediaBrowser.Server.Implementations.Session
/// <param name="deviceId">The device id.</param>
/// <param name="deviceName">Name of the device.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
/// <exception cref="System.ArgumentNullException">
/// </exception>
public async Task OnPlaybackStopped(User user, BaseItem item, long? positionTicks, string clientType, string deviceId, string deviceName)
{
if (user == null)
@@ -355,20 +370,5 @@ namespace MediaBrowser.Server.Implementations.Session
data.LastPlayedDate = DateTime.UtcNow;
}
}
/// <summary>
/// Identifies the web socket.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="webSocket">The web socket.</param>
public void IdentifyWebSocket(Guid sessionId, IWebSocketConnection webSocket)
{
var session = AllConnections.FirstOrDefault(i => i.Id == sessionId);
if (session != null)
{
session.WebSocket = webSocket;
}
}
}
}

View File

@@ -1,5 +1,9 @@
using MediaBrowser.Common.Net;
using System.Globalization;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;
@@ -22,12 +26,26 @@ namespace MediaBrowser.Server.Implementations.Session
private readonly ISessionManager _sessionManager;
/// <summary>
/// Initializes a new instance of the <see cref="SessionWebSocketListener"/> class.
/// The _logger
/// </summary>
private readonly ILogger _logger;
private readonly IUserManager _userManager;
private readonly ILibraryManager _libraryManager;
/// <summary>
/// Initializes a new instance of the <see cref="SessionWebSocketListener" /> class.
/// </summary>
/// <param name="sessionManager">The session manager.</param>
public SessionWebSocketListener(ISessionManager sessionManager)
/// <param name="logger">The logger.</param>
/// <param name="libraryManager">The library manager.</param>
/// <param name="userManager">The user manager.</param>
public SessionWebSocketListener(ISessionManager sessionManager, ILogger logger, ILibraryManager libraryManager, IUserManager userManager)
{
_sessionManager = sessionManager;
_logger = logger;
_libraryManager = libraryManager;
_userManager = userManager;
}
/// <summary>
@@ -44,11 +62,87 @@ namespace MediaBrowser.Server.Implementations.Session
var client = vals[0];
var deviceId = vals[1];
var session = _sessionManager.AllConnections.FirstOrDefault(i => string.Equals(i.DeviceId, deviceId) && string.Equals(i.Client, client));
var session = _sessionManager.Sessions.FirstOrDefault(i => string.Equals(i.DeviceId, deviceId) && string.Equals(i.Client, client));
if (session != null)
{
((SessionManager)_sessionManager).IdentifyWebSocket(session.Id, message.Connection);
session.WebSocket = message.Connection;
}
}
else if (string.Equals(message.MessageType, "Context", StringComparison.OrdinalIgnoreCase))
{
var session = _sessionManager.Sessions.FirstOrDefault(i => i.WebSocket == message.Connection);
if (session != null)
{
var vals = message.Data.Split('|');
session.NowViewingItemType = vals[0];
session.NowViewingItemIdentifier = vals[1];
session.NowViewingContext = vals.Length > 2 ? vals[2] : null;
}
}
else if (string.Equals(message.MessageType, "PlaybackStart", StringComparison.OrdinalIgnoreCase))
{
var session = _sessionManager.Sessions.FirstOrDefault(i => i.WebSocket == message.Connection);
if (session != null && session.UserId.HasValue)
{
var item = DtoBuilder.GetItemByClientId(message.Data, _userManager, _libraryManager);
_sessionManager.OnPlaybackStart(_userManager.GetUserById(session.UserId.Value), item, session.Client, session.DeviceId, session.DeviceName);
}
}
else if (string.Equals(message.MessageType, "PlaybackProgress", StringComparison.OrdinalIgnoreCase))
{
var session = _sessionManager.Sessions.FirstOrDefault(i => i.WebSocket == message.Connection);
if (session != null && session.UserId.HasValue)
{
var vals = message.Data.Split('|');
var item = DtoBuilder.GetItemByClientId(vals[0], _userManager, _libraryManager);
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);
_sessionManager.OnPlaybackProgress(_userManager.GetUserById(session.UserId.Value), item, positionTicks, isPaused, session.Client, session.DeviceId, session.DeviceName);
}
}
else if (string.Equals(message.MessageType, "PlaybackStopped", StringComparison.OrdinalIgnoreCase))
{
var session = _sessionManager.Sessions.FirstOrDefault(i => i.WebSocket == message.Connection);
if (session != null && session.UserId.HasValue)
{
var vals = message.Data.Split('|');
var item = DtoBuilder.GetItemByClientId(vals[0], _userManager, _libraryManager);
long? positionTicks = null;
if (vals.Length > 1)
{
long pos;
if (long.TryParse(vals[1], out pos))
{
positionTicks = pos;
}
}
_sessionManager.OnPlaybackStopped(_userManager.GetUserById(session.UserId.Value), item, positionTicks, session.Client, session.DeviceId, session.DeviceName);
}
}