aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Session/SessionManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Server.Implementations/Session/SessionManager.cs')
-rw-r--r--MediaBrowser.Server.Implementations/Session/SessionManager.cs99
1 files changed, 74 insertions, 25 deletions
diff --git a/MediaBrowser.Server.Implementations/Session/SessionManager.cs b/MediaBrowser.Server.Implementations/Session/SessionManager.cs
index f88e21aea..cc5aef54a 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(BaseItem 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,27 @@ 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;
- }
+ var mediaSource = await GetMediaSource(libraryItem, 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;
@@ -432,9 +439,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 +583,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 +665,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 +679,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 +756,9 @@ namespace MediaBrowser.Server.Implementations.Session
if (current == null || !string.Equals(current.Id, info.ItemId, StringComparison.OrdinalIgnoreCase))
{
- info.Item = GetItemInfo(libraryItem, libraryItem, info.MediaSourceId);
+ var mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId).ConfigureAwait(false);
+
+ info.Item = GetItemInfo(libraryItem, libraryItem, mediaSource);
}
else
{
@@ -751,6 +781,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 +1267,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 +1288,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 +1307,7 @@ namespace MediaBrowser.Server.Implementations.Session
var newToken = new AuthenticationInfo
{
AppName = app,
+ AppVersion = appVersion,
DateCreated = DateTime.UtcNow,
DeviceId = deviceId,
DeviceName = deviceName,
@@ -1435,10 +1478,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 +1632,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 +1733,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);
}