aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Session/SessionManager.cs
diff options
context:
space:
mode:
authorT. Adams <t.adams88@gmail.com>2015-04-03 11:04:25 -0700
committerT. Adams <t.adams88@gmail.com>2015-04-03 11:04:25 -0700
commitabf12569ba2aa31ea3a00e4faf3adad2f740cbd9 (patch)
tree47c57c6361825491d38e3def6b716926ddd9aa59 /MediaBrowser.Server.Implementations/Session/SessionManager.cs
parent46c92107490263f8e6abefbd2259780013fa195d (diff)
parentef505c8e9e2b8f348aeaa89be6bc446014b72996 (diff)
Merging in latest dev
Diffstat (limited to 'MediaBrowser.Server.Implementations/Session/SessionManager.cs')
-rw-r--r--MediaBrowser.Server.Implementations/Session/SessionManager.cs122
1 files changed, 91 insertions, 31 deletions
diff --git a/MediaBrowser.Server.Implementations/Session/SessionManager.cs b/MediaBrowser.Server.Implementations/Session/SessionManager.cs
index f88e21aea..7f5033b98 100644
--- a/MediaBrowser.Server.Implementations/Session/SessionManager.cs
+++ b/MediaBrowser.Server.Implementations/Session/SessionManager.cs
@@ -14,6 +14,7 @@ using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Devices;
+using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Library;
@@ -304,13 +305,21 @@ namespace MediaBrowser.Server.Implementations.Session
}
}
+ private async Task<MediaSourceInfo> GetMediaSource(IHasMediaSources item, string mediaSourceId)
+ {
+ var sources = await _mediaSourceManager.GetPlayackMediaSources(item.Id.ToString("N"), false, CancellationToken.None)
+ .ConfigureAwait(false);
+
+ return sources.FirstOrDefault(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
+ }
+
/// <summary>
/// Updates the now playing item id.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="info">The information.</param>
/// <param name="libraryItem">The library item.</param>
- private void UpdateNowPlayingItem(SessionInfo session, PlaybackProgressInfo info, BaseItem libraryItem)
+ private async Task UpdateNowPlayingItem(SessionInfo session, PlaybackProgressInfo info, BaseItem libraryItem)
{
if (string.IsNullOrWhiteSpace(info.MediaSourceId))
{
@@ -319,29 +328,32 @@ namespace MediaBrowser.Server.Implementations.Session
if (!string.IsNullOrWhiteSpace(info.ItemId) && info.Item == null && libraryItem != null)
{
- var runtimeTicks = libraryItem.RunTimeTicks;
+ var current = session.NowPlayingItem;
- if (!string.Equals(info.ItemId, info.MediaSourceId) &&
- !string.IsNullOrWhiteSpace(info.MediaSourceId))
+ if (current == null || !string.Equals(current.Id, info.ItemId, StringComparison.OrdinalIgnoreCase))
{
- var runtimeItem = _libraryManager.GetItemById(new Guid(info.MediaSourceId)) ??
- _libraryManager.GetItemById(info.ItemId);
+ var runtimeTicks = libraryItem.RunTimeTicks;
- runtimeTicks = runtimeItem.RunTimeTicks;
- }
+ MediaSourceInfo mediaSource = null;
+ var hasMediaSources = libraryItem as IHasMediaSources;
+ if (hasMediaSources != null)
+ {
+ mediaSource = await GetMediaSource(hasMediaSources, info.MediaSourceId).ConfigureAwait(false);
- var current = session.NowPlayingItem;
+ if (mediaSource != null)
+ {
+ runtimeTicks = mediaSource.RunTimeTicks;
+ }
+ }
- if (current == null || !string.Equals(current.Id, info.ItemId, StringComparison.OrdinalIgnoreCase))
- {
- info.Item = GetItemInfo(libraryItem, libraryItem, info.MediaSourceId);
+ info.Item = GetItemInfo(libraryItem, libraryItem, mediaSource);
+
+ info.Item.RunTimeTicks = runtimeTicks;
}
else
{
info.Item = current;
}
-
- info.Item.RunTimeTicks = runtimeTicks;
}
session.NowPlayingItem = info.Item;
@@ -407,12 +419,12 @@ namespace MediaBrowser.Server.Implementations.Session
if (!_activeConnections.TryGetValue(key, out sessionInfo))
{
sessionInfo = new SessionInfo
- {
- Client = appName,
- DeviceId = deviceId,
- ApplicationVersion = appVersion,
- Id = key.GetMD5().ToString("N")
- };
+ {
+ Client = appName,
+ DeviceId = deviceId,
+ ApplicationVersion = appVersion,
+ Id = key.GetMD5().ToString("N")
+ };
sessionInfo.DeviceName = deviceName;
sessionInfo.UserId = userId;
@@ -432,9 +444,18 @@ namespace MediaBrowser.Server.Implementations.Session
device = device ?? _deviceManager.GetDevice(deviceId);
- if (!string.IsNullOrEmpty(device.CustomName))
+ if (device == null)
+ {
+ var userIdString = userId.HasValue ? userId.Value.ToString("N") : null;
+ device = await _deviceManager.RegisterDevice(deviceId, deviceName, appName, appVersion, userIdString).ConfigureAwait(false);
+ }
+
+ if (device != null)
{
- deviceName = device.CustomName;
+ if (!string.IsNullOrEmpty(device.CustomName))
+ {
+ deviceName = device.CustomName;
+ }
}
sessionInfo.DeviceName = deviceName;
@@ -567,7 +588,7 @@ namespace MediaBrowser.Server.Implementations.Session
? null
: _libraryManager.GetItemById(new Guid(info.ItemId));
- UpdateNowPlayingItem(session, info, libraryItem);
+ await UpdateNowPlayingItem(session, info, libraryItem).ConfigureAwait(false);
if (!string.IsNullOrEmpty(session.DeviceId) && info.PlayMethod != PlayMethod.Transcode)
{
@@ -649,7 +670,7 @@ namespace MediaBrowser.Server.Implementations.Session
? null
: _libraryManager.GetItemById(new Guid(info.ItemId));
- UpdateNowPlayingItem(session, info, libraryItem);
+ await UpdateNowPlayingItem(session, info, libraryItem).ConfigureAwait(false);
var users = GetUsers(session);
@@ -663,6 +684,18 @@ namespace MediaBrowser.Server.Implementations.Session
}
}
+ if (!string.IsNullOrWhiteSpace(info.LiveStreamId))
+ {
+ try
+ {
+ await _mediaSourceManager.PingLiveStream(info.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error closing live stream", ex);
+ }
+ }
+
EventHelper.FireEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs
{
Item = libraryItem,
@@ -728,7 +761,15 @@ namespace MediaBrowser.Server.Implementations.Session
if (current == null || !string.Equals(current.Id, info.ItemId, StringComparison.OrdinalIgnoreCase))
{
- info.Item = GetItemInfo(libraryItem, libraryItem, info.MediaSourceId);
+ MediaSourceInfo mediaSource = null;
+
+ var hasMediaSources = libraryItem as IHasMediaSources;
+ if (hasMediaSources != null)
+ {
+ mediaSource = await GetMediaSource(hasMediaSources, info.MediaSourceId).ConfigureAwait(false);
+ }
+
+ info.Item = GetItemInfo(libraryItem, libraryItem, mediaSource);
}
else
{
@@ -751,6 +792,18 @@ namespace MediaBrowser.Server.Implementations.Session
}
}
+ if (!string.IsNullOrWhiteSpace(info.LiveStreamId))
+ {
+ try
+ {
+ await _mediaSourceManager.CloseLiveStream(info.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error closing live stream", ex);
+ }
+ }
+
EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackStopEventArgs
{
Item = libraryItem,
@@ -1225,7 +1278,7 @@ namespace MediaBrowser.Server.Implementations.Session
throw new UnauthorizedAccessException("Invalid user or password entered.");
}
- var token = await GetAuthorizationToken(user.Id.ToString("N"), request.DeviceId, request.App, request.DeviceName).ConfigureAwait(false);
+ var token = await GetAuthorizationToken(user.Id.ToString("N"), request.DeviceId, request.App, request.AppVersion, request.DeviceName).ConfigureAwait(false);
EventHelper.FireEventIfNotNull(AuthenticationSucceeded, this, new GenericEventArgs<AuthenticationRequest>(request), _logger);
@@ -1246,7 +1299,7 @@ namespace MediaBrowser.Server.Implementations.Session
};
}
- private async Task<string> GetAuthorizationToken(string userId, string deviceId, string app, string deviceName)
+ private async Task<string> GetAuthorizationToken(string userId, string deviceId, string app, string appVersion, string deviceName)
{
var existing = _authRepo.Get(new AuthenticationInfoQuery
{
@@ -1265,6 +1318,7 @@ namespace MediaBrowser.Server.Implementations.Session
var newToken = new AuthenticationInfo
{
AppName = app,
+ AppVersion = appVersion,
DateCreated = DateTime.UtcNow,
DeviceId = deviceId,
DeviceName = deviceName,
@@ -1435,10 +1489,10 @@ namespace MediaBrowser.Server.Implementations.Session
/// </summary>
/// <param name="item">The item.</param>
/// <param name="chapterOwner">The chapter owner.</param>
- /// <param name="mediaSourceId">The media source identifier.</param>
+ /// <param name="mediaSource">The media source.</param>
/// <returns>BaseItemInfo.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
- private BaseItemInfo GetItemInfo(BaseItem item, BaseItem chapterOwner, string mediaSourceId)
+ private BaseItemInfo GetItemInfo(BaseItem item, BaseItem chapterOwner, MediaSourceInfo mediaSource)
{
if (item == null)
{
@@ -1589,9 +1643,9 @@ namespace MediaBrowser.Server.Implementations.Session
info.Chapters = _dtoService.GetChapterInfoDtos(chapterOwner).ToList();
}
- if (!string.IsNullOrWhiteSpace(mediaSourceId))
+ if (mediaSource != null)
{
- info.MediaStreams = _mediaSourceManager.GetMediaStreams(mediaSourceId).ToList();
+ info.MediaStreams = mediaSource.MediaStreams;
}
return info;
@@ -1690,6 +1744,12 @@ namespace MediaBrowser.Server.Implementations.Session
deviceId = info.DeviceId;
}
+ // Prevent argument exception
+ if (string.IsNullOrWhiteSpace(appVersion))
+ {
+ appVersion = "1";
+ }
+
return LogSessionActivity(appName, appVersion, deviceId, deviceName, remoteEndpoint, user);
}