jellyfin/Emby.Server.Implementations/Session/SessionManager.cs

1831 lines
64 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2020-05-15 14:24:01 -07:00
using Jellyfin.Data.Entities;
2021-04-10 13:17:36 -07:00
using Jellyfin.Data.Entities.Security;
2020-05-12 19:10:35 -07:00
using Jellyfin.Data.Enums;
using Jellyfin.Data.Events;
2021-05-20 20:56:59 -07:00
using Jellyfin.Data.Queries;
using Jellyfin.Extensions;
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Extensions;
2014-05-18 12:58:42 -07:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Authentication;
2014-10-11 13:38:13 -07:00
using MediaBrowser.Controller.Devices;
2014-04-06 10:53:23 -07:00
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2020-08-15 12:55:31 -07:00
using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.Events.Authentication;
2020-08-15 12:55:31 -07:00
using MediaBrowser.Controller.Events.Session;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
2015-03-29 09:45:16 -07:00
using MediaBrowser.Model.Dto;
2013-10-02 10:23:10 -07:00
using MediaBrowser.Model.Entities;
2014-02-20 22:35:56 -07:00
using MediaBrowser.Model.Library;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Session;
2020-05-06 14:42:53 -07:00
using MediaBrowser.Model.SyncPlay;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
2020-05-15 14:24:01 -07:00
using Episode = MediaBrowser.Controller.Entities.TV.Episode;
2016-11-03 16:35:19 -07:00
namespace Emby.Server.Implementations.Session
{
2013-05-10 05:18:07 -07:00
/// <summary>
2020-01-16 16:19:58 -07:00
/// Class SessionManager.
2013-05-10 05:18:07 -07:00
/// </summary>
public sealed class SessionManager : ISessionManager, IAsyncDisposable
{
private readonly IUserDataManager _userDataManager;
2020-06-05 17:15:56 -07:00
private readonly ILogger<SessionManager> _logger;
2020-08-15 12:55:31 -07:00
private readonly IEventManager _eventManager;
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IMusicManager _musicManager;
2014-04-06 10:53:23 -07:00
private readonly IDtoService _dtoService;
private readonly IImageProcessor _imageProcessor;
private readonly IMediaSourceManager _mediaSourceManager;
2014-05-18 12:58:42 -07:00
private readonly IServerApplicationHost _appHost;
2014-10-11 13:38:13 -07:00
private readonly IDeviceManager _deviceManager;
private readonly CancellationTokenRegistration _shutdownCallback;
private readonly ConcurrentDictionary<string, SessionInfo> _activeConnections
= new(StringComparer.OrdinalIgnoreCase);
2020-01-16 16:19:58 -07:00
private Timer _idleTimer;
2020-01-16 16:19:58 -07:00
private DtoOptions _itemInfoDtoOptions;
private bool _disposed = false;
2014-04-06 10:53:23 -07:00
public SessionManager(
2020-01-16 16:19:58 -07:00
ILogger<SessionManager> logger,
2020-08-15 12:55:31 -07:00
IEventManager eventManager,
IUserDataManager userDataManager,
ILibraryManager libraryManager,
IUserManager userManager,
IMusicManager musicManager,
IDtoService dtoService,
IImageProcessor imageProcessor,
IServerApplicationHost appHost,
IDeviceManager deviceManager,
IMediaSourceManager mediaSourceManager,
IHostApplicationLifetime hostApplicationLifetime)
{
2020-01-16 16:19:58 -07:00
_logger = logger;
2020-08-15 12:55:31 -07:00
_eventManager = eventManager;
_userDataManager = userDataManager;
_libraryManager = libraryManager;
_userManager = userManager;
2014-04-02 14:55:19 -07:00
_musicManager = musicManager;
2014-04-06 10:53:23 -07:00
_dtoService = dtoService;
_imageProcessor = imageProcessor;
2014-05-18 12:58:42 -07:00
_appHost = appHost;
2014-10-11 13:38:13 -07:00
_deviceManager = deviceManager;
_mediaSourceManager = mediaSourceManager;
_shutdownCallback = hostApplicationLifetime.ApplicationStopping.Register(OnApplicationStopping);
2020-01-16 16:19:58 -07:00
2019-03-13 14:32:52 -07:00
_deviceManager.DeviceOptionsUpdated += OnDeviceManagerDeviceOptionsUpdated;
2014-10-13 13:14:53 -07:00
}
2020-01-16 16:19:58 -07:00
/// <summary>
/// Occurs when playback has started.
2020-01-16 16:19:58 -07:00
/// </summary>
public event EventHandler<PlaybackProgressEventArgs> PlaybackStart;
/// <summary>
/// Occurs when playback has progressed.
2020-01-16 16:19:58 -07:00
/// </summary>
public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
/// <summary>
/// Occurs when playback has stopped.
2020-01-16 16:19:58 -07:00
/// </summary>
public event EventHandler<PlaybackStopEventArgs> PlaybackStopped;
/// <inheritdoc />
public event EventHandler<SessionEventArgs> SessionStarted;
/// <inheritdoc />
public event EventHandler<SessionEventArgs> CapabilitiesChanged;
/// <inheritdoc />
public event EventHandler<SessionEventArgs> SessionEnded;
/// <inheritdoc />
public event EventHandler<SessionEventArgs> SessionActivity;
2020-12-06 17:04:48 -07:00
/// <inheritdoc />
public event EventHandler<SessionEventArgs> SessionControllerConnected;
2020-01-16 16:19:58 -07:00
/// <summary>
/// Gets all connections.
/// </summary>
/// <value>All connections.</value>
public IEnumerable<SessionInfo> Sessions => _activeConnections.Values.OrderByDescending(c => c.LastActivityDate);
2019-03-13 14:32:52 -07:00
private void OnDeviceManagerDeviceOptionsUpdated(object sender, GenericEventArgs<Tuple<string, DeviceOptions>> e)
2014-10-13 13:14:53 -07:00
{
foreach (var session in Sessions)
{
2019-03-13 14:32:52 -07:00
if (string.Equals(session.DeviceId, e.Argument.Item1, StringComparison.Ordinal))
2014-10-13 13:14:53 -07:00
{
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrWhiteSpace(e.Argument.Item2.CustomName))
{
session.HasCustomDeviceName = true;
session.DeviceName = e.Argument.Item2.CustomName;
}
else
{
session.HasCustomDeviceName = false;
}
2014-10-13 13:14:53 -07:00
}
}
}
2020-01-16 16:19:58 -07:00
private void CheckDisposed()
{
2018-09-12 10:26:21 -07:00
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
2014-04-06 10:53:23 -07:00
private void OnSessionStarted(SessionInfo info)
{
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(info.DeviceId))
2014-06-03 20:34:36 -07:00
{
var capabilities = _deviceManager.GetCapabilities(info.DeviceId);
2014-06-03 20:34:36 -07:00
2022-12-05 07:01:13 -07:00
if (capabilities is not null)
2014-06-03 20:34:36 -07:00
{
ReportCapabilities(info, capabilities, false);
}
}
2014-04-06 10:53:23 -07:00
2020-08-15 12:55:31 -07:00
_eventManager.Publish(new SessionStartedEventArgs(info));
2019-03-13 14:32:52 -07:00
EventHelper.QueueEventIfNotNull(
SessionStarted,
this,
new SessionEventArgs
{
SessionInfo = info
},
_logger);
2018-09-12 10:26:21 -07:00
}
private void OnSessionEnded(SessionInfo info)
{
2019-03-13 14:32:52 -07:00
EventHelper.QueueEventIfNotNull(
SessionEnded,
this,
new SessionEventArgs
{
SessionInfo = info
},
_logger);
2014-04-06 10:53:23 -07:00
2020-08-15 12:55:31 -07:00
_eventManager.Publish(new SessionEndedEventArgs(info));
2018-09-12 10:26:21 -07:00
info.Dispose();
}
2014-04-06 10:53:23 -07:00
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2021-09-03 09:46:34 -07:00
public void UpdateDeviceName(string sessionId, string reportedDeviceName)
2018-09-12 10:26:21 -07:00
{
var session = GetSession(sessionId);
2022-12-05 07:01:13 -07:00
if (session is not null)
2018-09-12 10:26:21 -07:00
{
2021-09-03 09:46:34 -07:00
session.DeviceName = reportedDeviceName;
2018-09-12 10:26:21 -07:00
}
2014-04-06 10:53:23 -07:00
}
/// <summary>
/// Logs the user activity.
/// </summary>
2015-03-14 18:42:09 -07:00
/// <param name="appName">Type of the client.</param>
/// <param name="appVersion">The app version.</param>
/// <param name="deviceId">The device id.</param>
/// <param name="deviceName">Name of the device.</param>
/// <param name="remoteEndPoint">The remote end point.</param>
/// <param name="user">The user.</param>
2019-03-13 14:32:52 -07:00
/// <returns>SessionInfo.</returns>
2021-04-10 13:57:25 -07:00
public async Task<SessionInfo> LogSessionActivity(
2019-03-13 14:32:52 -07:00
string appName,
2014-07-07 18:41:03 -07:00
string appVersion,
string deviceId,
string deviceName,
string remoteEndPoint,
User user)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
ArgumentException.ThrowIfNullOrEmpty(appName);
ArgumentException.ThrowIfNullOrEmpty(appVersion);
ArgumentException.ThrowIfNullOrEmpty(deviceId);
var activityDate = DateTime.UtcNow;
2021-04-10 13:57:25 -07:00
var session = await GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
2015-03-21 09:10:02 -07:00
var lastActivityDate = session.LastActivityDate;
session.LastActivityDate = activityDate;
2022-12-05 07:01:13 -07:00
if (user is not null)
{
2015-03-21 09:10:02 -07:00
var userLastActivityDate = user.LastActivityDate ?? DateTime.MinValue;
if ((activityDate - userLastActivityDate).TotalSeconds > 60)
2015-03-21 09:10:02 -07:00
{
try
{
user.LastActivityDate = activityDate;
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
}
catch (DbUpdateConcurrencyException e)
{
2020-07-07 15:20:17 -07:00
_logger.LogDebug(e, "Error updating user's last activity date.");
}
2015-03-21 09:10:02 -07:00
}
}
2015-03-21 09:10:02 -07:00
if ((activityDate - lastActivityDate).TotalSeconds > 10)
{
2019-03-13 14:32:52 -07:00
SessionActivity?.Invoke(
this,
new SessionEventArgs
{
SessionInfo = session
});
2015-03-21 09:10:02 -07:00
}
return session;
}
2020-12-06 17:04:48 -07:00
/// <inheritdoc />
2021-09-03 09:46:34 -07:00
public void OnSessionControllerConnected(SessionInfo session)
2020-12-06 17:04:48 -07:00
{
EventHelper.QueueEventIfNotNull(
SessionControllerConnected,
this,
new SessionEventArgs
{
2021-09-03 09:46:34 -07:00
SessionInfo = session
2020-12-06 17:04:48 -07:00
},
_logger);
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
public async Task CloseIfNeededAsync(SessionInfo session)
2014-03-09 19:33:32 -07:00
{
2018-09-12 10:26:21 -07:00
if (!session.SessionControllers.Any(i => i.IsSessionActive))
2014-03-09 19:33:32 -07:00
{
2018-09-12 10:26:21 -07:00
var key = GetSessionKey(session.Client, session.DeviceId);
2014-03-09 19:33:32 -07:00
2020-01-16 16:19:58 -07:00
_activeConnections.TryRemove(key, out _);
if (!string.IsNullOrEmpty(session.PlayState?.LiveStreamId))
{
await _mediaSourceManager.CloseLiveStream(session.PlayState.LiveStreamId).ConfigureAwait(false);
}
2014-03-09 19:33:32 -07:00
2018-09-12 10:26:21 -07:00
OnSessionEnded(session);
2014-03-09 19:33:32 -07:00
}
2018-09-12 10:26:21 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2018-09-12 10:26:21 -07:00
public void ReportSessionEnded(string sessionId)
{
CheckDisposed();
var session = GetSession(sessionId, false);
2022-12-05 07:01:13 -07:00
if (session is not null)
2014-03-09 19:33:32 -07:00
{
2018-09-12 10:26:21 -07:00
var key = GetSessionKey(session.Client, session.DeviceId);
2020-01-16 16:19:58 -07:00
_activeConnections.TryRemove(key, out _);
2018-09-12 10:26:21 -07:00
OnSessionEnded(session);
2014-03-09 19:33:32 -07:00
}
}
2018-09-12 10:26:21 -07:00
private Task<MediaSourceInfo> GetMediaSource(BaseItem item, string mediaSourceId, string liveStreamId)
2015-03-29 09:45:16 -07:00
{
2016-09-18 13:38:38 -07:00
return _mediaSourceManager.GetMediaSource(item, mediaSourceId, liveStreamId, false, CancellationToken.None);
2015-03-29 09:45:16 -07:00
}
/// <summary>
/// Updates the now playing item id.
/// </summary>
2019-03-13 14:32:52 -07:00
/// <returns>Task.</returns>
2017-04-17 12:01:16 -07:00
private async Task UpdateNowPlayingItem(SessionInfo session, PlaybackProgressInfo info, BaseItem libraryItem, bool updateLastCheckInTime)
{
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(info.MediaSourceId))
{
info.MediaSourceId = info.ItemId.ToString("N", CultureInfo.InvariantCulture);
}
2014-04-15 19:17:48 -07:00
2022-12-05 07:01:13 -07:00
if (!info.ItemId.Equals(default) && info.Item is null && libraryItem is not null)
{
2015-03-29 09:45:16 -07:00
var current = session.NowPlayingItem;
2015-01-16 21:29:53 -07:00
2022-12-05 07:00:20 -07:00
if (current is null || !info.ItemId.Equals(current.Id))
2015-01-16 21:29:53 -07:00
{
2015-03-29 09:45:16 -07:00
var runtimeTicks = libraryItem.RunTimeTicks;
2015-03-16 21:08:09 -07:00
2015-04-02 09:24:59 -07:00
MediaSourceInfo mediaSource = null;
2020-01-16 16:19:58 -07:00
if (libraryItem is IHasMediaSources)
2015-03-29 09:45:16 -07:00
{
2018-09-12 10:26:21 -07:00
mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId, info.LiveStreamId).ConfigureAwait(false);
2015-04-02 09:24:59 -07:00
2022-12-05 07:01:13 -07:00
if (mediaSource is not null)
2015-04-02 09:24:59 -07:00
{
runtimeTicks = mediaSource.RunTimeTicks;
}
2015-03-29 09:45:16 -07:00
}
2014-05-10 10:28:03 -07:00
2017-06-09 12:26:54 -07:00
info.Item = GetItemInfo(libraryItem, mediaSource);
2015-03-29 09:45:16 -07:00
info.Item.RunTimeTicks = runtimeTicks;
2014-05-10 10:28:03 -07:00
}
else
{
info.Item = current;
}
}
2014-04-15 19:17:48 -07:00
session.NowPlayingItem = info.Item;
session.LastActivityDate = DateTime.UtcNow;
2017-04-17 12:01:16 -07:00
if (updateLastCheckInTime)
{
session.LastPlaybackCheckIn = DateTime.UtcNow;
}
2014-04-15 19:17:48 -07:00
2014-04-22 10:25:54 -07:00
session.PlayState.IsPaused = info.IsPaused;
session.PlayState.PositionTicks = info.PositionTicks;
session.PlayState.MediaSourceId = info.MediaSourceId;
session.PlayState.LiveStreamId = info.LiveStreamId;
2014-04-15 19:17:48 -07:00
session.PlayState.CanSeek = info.CanSeek;
session.PlayState.IsMuted = info.IsMuted;
session.PlayState.VolumeLevel = info.VolumeLevel;
session.PlayState.AudioStreamIndex = info.AudioStreamIndex;
session.PlayState.SubtitleStreamIndex = info.SubtitleStreamIndex;
2014-04-17 22:03:01 -07:00
session.PlayState.PlayMethod = info.PlayMethod;
2015-07-30 07:34:46 -07:00
session.PlayState.RepeatMode = info.RepeatMode;
2018-09-12 10:26:21 -07:00
session.PlaylistItemId = info.PlaylistItemId;
var nowPlayingQueue = info.NowPlayingQueue;
2022-01-14 08:12:45 -07:00
if (nowPlayingQueue?.Length > 0)
2018-09-12 10:26:21 -07:00
{
session.NowPlayingQueue = nowPlayingQueue;
2021-05-17 04:33:37 -07:00
2022-01-14 08:12:45 -07:00
var itemIds = nowPlayingQueue.Select(queue => queue.Id).ToArray();
session.NowPlayingQueueFullItems = _dtoService.GetBaseItemDtos(
_libraryManager.GetItemList(new InternalItemsQuery { ItemIds = itemIds }),
new DtoOptions(true));
}
}
/// <summary>
/// Removes the now playing item id.
/// </summary>
/// <param name="session">The session.</param>
2014-04-15 19:17:48 -07:00
private void RemoveNowPlayingItem(SessionInfo session)
{
2014-04-15 19:17:48 -07:00
session.NowPlayingItem = null;
session.PlayState = new PlayerStateInfo();
if (!string.IsNullOrEmpty(session.DeviceId))
{
ClearTranscodingInfo(session.DeviceId);
}
}
private static string GetSessionKey(string appName, string deviceId)
2020-01-16 16:19:58 -07:00
=> appName + deviceId;
2014-03-09 19:33:32 -07:00
/// <summary>
/// Gets the connection.
/// </summary>
2015-03-14 18:42:09 -07:00
/// <param name="appName">Type of the client.</param>
/// <param name="appVersion">The app version.</param>
/// <param name="deviceId">The device id.</param>
/// <param name="deviceName">Name of the device.</param>
/// <param name="remoteEndPoint">The remote end point.</param>
2015-03-09 12:40:03 -07:00
/// <param name="user">The user.</param>
/// <returns>SessionInfo.</returns>
2021-04-10 13:57:25 -07:00
private async Task<SessionInfo> GetSessionInfo(
2020-05-12 19:10:35 -07:00
string appName,
string appVersion,
string deviceId,
string deviceName,
string remoteEndPoint,
2020-05-15 14:24:01 -07:00
User user)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
ArgumentException.ThrowIfNullOrEmpty(deviceId);
2020-01-16 16:19:58 -07:00
2015-03-14 18:42:09 -07:00
var key = GetSessionKey(appName, deviceId);
2018-09-12 10:26:21 -07:00
CheckDisposed();
2014-03-09 19:33:32 -07:00
2021-04-10 13:57:25 -07:00
if (!_activeConnections.TryGetValue(key, out var sessionInfo))
{
_activeConnections[key] = await CreateSession(key, appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
sessionInfo = _activeConnections[key];
}
2014-08-10 15:13:17 -07:00
2020-05-12 19:10:35 -07:00
sessionInfo.UserId = user?.Id ?? Guid.Empty;
sessionInfo.UserName = user?.Username;
2022-12-05 07:00:20 -07:00
sessionInfo.UserPrimaryImageTag = user?.ProfileImage is null ? null : GetImageCacheTag(user);
2018-09-12 10:26:21 -07:00
sessionInfo.RemoteEndPoint = remoteEndPoint;
sessionInfo.Client = appName;
2014-04-06 10:53:23 -07:00
2018-09-12 10:26:21 -07:00
if (!sessionInfo.HasCustomDeviceName || string.IsNullOrEmpty(sessionInfo.DeviceName))
{
sessionInfo.DeviceName = deviceName;
}
2014-10-11 13:38:13 -07:00
2018-09-12 10:26:21 -07:00
sessionInfo.ApplicationVersion = appVersion;
2014-03-09 19:33:32 -07:00
2022-12-05 07:00:20 -07:00
if (user is null)
2018-09-12 10:26:21 -07:00
{
2019-03-13 14:32:52 -07:00
sessionInfo.AdditionalUsers = Array.Empty<SessionUserInfo>();
2018-09-12 10:26:21 -07:00
}
2014-10-13 13:14:53 -07:00
2018-09-12 10:26:21 -07:00
return sessionInfo;
}
2015-03-29 09:45:16 -07:00
2021-04-10 13:57:25 -07:00
private async Task<SessionInfo> CreateSession(
2020-05-12 19:10:35 -07:00
string key,
string appName,
string appVersion,
string deviceId,
string deviceName,
string remoteEndPoint,
2020-05-15 14:24:01 -07:00
User user)
2018-09-12 10:26:21 -07:00
{
var sessionInfo = new SessionInfo(this, _logger)
{
Client = appName,
DeviceId = deviceId,
ApplicationVersion = appVersion,
2020-06-24 14:09:15 -07:00
Id = key.GetMD5().ToString("N", CultureInfo.InvariantCulture),
ServerId = _appHost.SystemId
2018-09-12 10:26:21 -07:00
};
2014-10-13 13:14:53 -07:00
2020-05-12 19:10:35 -07:00
var username = user?.Username;
2014-03-09 19:33:32 -07:00
2019-03-13 14:32:52 -07:00
sessionInfo.UserId = user?.Id ?? Guid.Empty;
2018-09-12 10:26:21 -07:00
sessionInfo.UserName = username;
2022-12-05 07:00:20 -07:00
sessionInfo.UserPrimaryImageTag = user?.ProfileImage is null ? null : GetImageCacheTag(user);
2018-09-12 10:26:21 -07:00
sessionInfo.RemoteEndPoint = remoteEndPoint;
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(deviceName))
{
deviceName = "Network Device";
}
2014-03-09 19:33:32 -07:00
2021-04-10 13:57:25 -07:00
var deviceOptions = await _deviceManager.GetDeviceOptions(deviceId).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(deviceOptions.CustomName))
{
sessionInfo.DeviceName = deviceName;
2014-03-09 19:33:32 -07:00
}
2018-09-12 10:26:21 -07:00
else
{
2018-09-12 10:26:21 -07:00
sessionInfo.DeviceName = deviceOptions.CustomName;
sessionInfo.HasCustomDeviceName = true;
}
2018-09-12 10:26:21 -07:00
OnSessionStarted(sessionInfo);
return sessionInfo;
}
private List<User> GetUsers(SessionInfo session)
{
var users = new List<User>();
if (session.UserId.Equals(default))
{
return users;
}
var user = _userManager.GetUserById(session.UserId);
2022-12-05 07:00:20 -07:00
if (user is null)
{
throw new InvalidOperationException("User not found");
}
users.Add(user);
users.AddRange(session.AdditionalUsers
.Select(i => _userManager.GetUserById(i.UserId))
2022-12-05 07:01:13 -07:00
.Where(i => i is not null));
return users;
}
2014-10-22 21:26:01 -07:00
private void StartIdleCheckTimer()
{
2020-05-12 19:10:35 -07:00
_idleTimer ??= new Timer(CheckForIdlePlayback, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
2014-10-22 21:26:01 -07:00
}
2019-03-13 14:32:52 -07:00
2014-10-22 21:26:01 -07:00
private void StopIdleCheckTimer()
{
2022-12-05 07:01:13 -07:00
if (_idleTimer is not null)
2014-10-22 21:26:01 -07:00
{
_idleTimer.Dispose();
_idleTimer = null;
}
}
private async void CheckForIdlePlayback(object state)
{
2022-12-05 07:01:13 -07:00
var playingSessions = Sessions.Where(i => i.NowPlayingItem is not null)
2014-10-22 21:26:01 -07:00
.ToList();
if (playingSessions.Count > 0)
{
var idle = playingSessions
.Where(i => (DateTime.UtcNow - i.LastPlaybackCheckIn).TotalMinutes > 5)
.ToList();
foreach (var session in idle)
{
_logger.LogDebug("Session {0} has gone idle while playing", session.Id);
2014-10-22 21:26:01 -07:00
try
{
await OnPlaybackStopped(new PlaybackStopInfo
{
Item = session.NowPlayingItem,
2022-12-05 07:00:20 -07:00
ItemId = session.NowPlayingItem is null ? Guid.Empty : session.NowPlayingItem.Id,
2014-10-22 21:26:01 -07:00
SessionId = session.Id,
2019-03-13 14:32:52 -07:00
MediaSourceId = session.PlayState?.MediaSourceId,
PositionTicks = session.PlayState?.PositionTicks
}).ConfigureAwait(false);
2014-10-22 21:26:01 -07:00
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Error calling OnPlaybackStopped");
2014-10-22 21:26:01 -07:00
}
}
2022-12-05 07:01:13 -07:00
playingSessions = Sessions.Where(i => i.NowPlayingItem is not null)
2014-10-22 21:26:01 -07:00
.ToList();
}
if (playingSessions.Count == 0)
{
StopIdleCheckTimer();
}
}
2018-09-12 10:26:21 -07:00
private BaseItem GetNowPlayingItem(SessionInfo session, Guid itemId)
2016-11-11 21:02:22 -07:00
{
var item = session.FullNowPlayingItem;
2022-12-05 07:01:13 -07:00
if (item is not null && item.Id.Equals(itemId))
2016-11-11 21:02:22 -07:00
{
return item;
}
item = _libraryManager.GetItemById(itemId);
session.FullNowPlayingItem = item;
return item;
}
/// <summary>
2020-01-16 16:19:58 -07:00
/// Used to report that playback has started for an item.
/// </summary>
/// <param name="info">The info.</param>
/// <returns>Task.</returns>
2020-01-16 16:19:58 -07:00
/// <exception cref="ArgumentNullException"><c>info</c> is <c>null</c>.</exception>
2014-04-15 19:17:48 -07:00
public async Task OnPlaybackStart(PlaybackStartInfo info)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
ArgumentNullException.ThrowIfNull(info);
2014-04-15 19:17:48 -07:00
var session = GetSession(info.SessionId);
var libraryItem = info.ItemId.Equals(default)
2014-04-15 19:17:48 -07:00
? null
2016-11-11 21:02:22 -07:00
: GetNowPlayingItem(session, info.ItemId);
2017-04-17 12:01:16 -07:00
await UpdateNowPlayingItem(session, info, libraryItem, true).ConfigureAwait(false);
2014-06-05 21:56:47 -07:00
if (!string.IsNullOrEmpty(session.DeviceId) && info.PlayMethod != PlayMethod.Transcode)
{
ClearTranscodingInfo(session.DeviceId);
}
2019-02-05 01:49:46 -07:00
session.StartAutomaticProgress(info);
var users = GetUsers(session);
2013-05-17 11:05:49 -07:00
2022-12-05 07:01:13 -07:00
if (libraryItem is not null)
2013-05-22 10:58:49 -07:00
{
2014-04-15 19:17:48 -07:00
foreach (var user in users)
{
2018-09-12 10:26:21 -07:00
OnPlaybackStart(user, libraryItem);
2014-04-15 19:17:48 -07:00
}
2013-05-22 10:58:49 -07:00
}
var eventArgs = new PlaybackStartEventArgs
2020-08-15 12:55:31 -07:00
{
Item = libraryItem,
Users = users,
MediaSourceId = info.MediaSourceId,
MediaInfo = info.Item,
DeviceName = session.DeviceName,
ClientName = session.Client,
DeviceId = session.DeviceId,
Session = session,
PlaybackPositionTicks = info.PositionTicks,
PlaySessionId = info.PlaySessionId
2020-08-15 12:55:31 -07:00
};
await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
// Nothing to save here
// Fire events to inform plugins
2019-03-13 14:32:52 -07:00
EventHelper.QueueEventIfNotNull(
PlaybackStart,
this,
2020-08-15 12:55:31 -07:00
eventArgs,
2019-03-13 14:32:52 -07:00
_logger);
2014-10-22 21:26:01 -07:00
StartIdleCheckTimer();
}
/// <summary>
/// Called when [playback start].
/// </summary>
/// <param name="user">The user object.</param>
/// <param name="item">The item.</param>
2018-09-12 10:26:21 -07:00
private void OnPlaybackStart(User user, BaseItem item)
{
2018-09-12 10:26:21 -07:00
var data = _userDataManager.GetUserData(user, item);
data.PlayCount++;
data.LastPlayedDate = DateTime.UtcNow;
if (item.SupportsPlayedStatus && !item.SupportsPositionTicksResume)
{
data.Played = true;
}
2016-10-10 23:46:59 -07:00
else
{
data.Played = false;
}
2018-09-12 10:26:21 -07:00
_userDataManager.SaveUserData(user, item, data, UserDataSaveReason.PlaybackStart, CancellationToken.None);
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2017-04-17 12:01:16 -07:00
public Task OnPlaybackProgress(PlaybackProgressInfo info)
{
return OnPlaybackProgress(info, false);
}
/// <summary>
2020-01-16 16:19:58 -07:00
/// Used to report playback progress for an item.
/// </summary>
2021-10-02 11:01:57 -07:00
/// <param name="info">The playback progress info.</param>
/// <param name="isAutomated">Whether this is an automated update.</param>
2019-03-13 14:32:52 -07:00
/// <returns>Task.</returns>
2017-04-17 12:01:16 -07:00
public async Task OnPlaybackProgress(PlaybackProgressInfo info, bool isAutomated)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
ArgumentNullException.ThrowIfNull(info);
2014-04-15 19:17:48 -07:00
var session = GetSession(info.SessionId);
var libraryItem = info.ItemId.Equals(default)
2014-04-15 19:17:48 -07:00
? null
2016-11-11 21:02:22 -07:00
: GetNowPlayingItem(session, info.ItemId);
2014-04-06 10:53:23 -07:00
2017-04-17 12:01:16 -07:00
await UpdateNowPlayingItem(session, info, libraryItem, !isAutomated).ConfigureAwait(false);
if (!string.IsNullOrEmpty(session.DeviceId) && info.PlayMethod != PlayMethod.Transcode)
{
ClearTranscodingInfo(session.DeviceId);
}
var users = GetUsers(session);
2018-09-12 10:26:21 -07:00
// only update saved user data on actual check-ins, not automated ones
2022-12-05 07:01:13 -07:00
if (libraryItem is not null && !isAutomated)
{
2014-04-15 19:17:48 -07:00
foreach (var user in users)
{
OnPlaybackProgress(user, libraryItem, info);
2014-04-15 19:17:48 -07:00
}
}
2020-08-15 12:55:31 -07:00
var eventArgs = new PlaybackProgressEventArgs
{
Item = libraryItem,
Users = users,
PlaybackPositionTicks = session.PlayState.PositionTicks,
MediaSourceId = session.PlayState.MediaSourceId,
MediaInfo = info.Item,
DeviceName = session.DeviceName,
ClientName = session.Client,
DeviceId = session.DeviceId,
IsPaused = info.IsPaused,
PlaySessionId = info.PlaySessionId,
IsAutomated = isAutomated,
Session = session
};
await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
PlaybackProgress?.Invoke(this, eventArgs);
2014-10-22 21:26:01 -07:00
2017-04-17 13:33:07 -07:00
if (!isAutomated)
{
2019-02-05 01:49:46 -07:00
session.StartAutomaticProgress(info);
2017-04-17 13:33:07 -07:00
}
2014-10-22 21:26:01 -07:00
StartIdleCheckTimer();
}
private void OnPlaybackProgress(User user, BaseItem item, PlaybackProgressInfo info)
{
2018-09-12 10:26:21 -07:00
var data = _userDataManager.GetUserData(user, item);
var positionTicks = info.PositionTicks;
2018-09-12 10:26:21 -07:00
var changed = false;
if (positionTicks.HasValue)
{
_userDataManager.UpdatePlayState(item, data, positionTicks.Value);
2018-09-12 10:26:21 -07:00
changed = true;
}
2018-09-12 10:26:21 -07:00
var tracksChanged = UpdatePlaybackSettings(user, info, data);
if (!tracksChanged)
{
changed = true;
}
2018-09-12 10:26:21 -07:00
if (changed)
{
_userDataManager.SaveUserData(user, item, data, UserDataSaveReason.PlaybackProgress, CancellationToken.None);
}
}
private static bool UpdatePlaybackSettings(User user, PlaybackProgressInfo info, UserItemData data)
{
2018-09-12 10:26:21 -07:00
var changed = false;
2020-05-12 19:10:35 -07:00
if (user.RememberAudioSelections)
{
2018-09-12 10:26:21 -07:00
if (data.AudioStreamIndex != info.AudioStreamIndex)
{
data.AudioStreamIndex = info.AudioStreamIndex;
changed = true;
}
}
else
{
2018-09-12 10:26:21 -07:00
if (data.AudioStreamIndex.HasValue)
{
data.AudioStreamIndex = null;
changed = true;
}
}
2020-05-12 19:10:35 -07:00
if (user.RememberSubtitleSelections)
{
2018-09-12 10:26:21 -07:00
if (data.SubtitleStreamIndex != info.SubtitleStreamIndex)
{
data.SubtitleStreamIndex = info.SubtitleStreamIndex;
changed = true;
}
}
else
{
2018-09-12 10:26:21 -07:00
if (data.SubtitleStreamIndex.HasValue)
{
data.SubtitleStreamIndex = null;
changed = true;
}
}
2018-09-12 10:26:21 -07:00
return changed;
}
/// <summary>
/// Used to report that playback has ended for an item.
/// </summary>
/// <param name="info">The info.</param>
/// <returns>Task.</returns>
2020-07-24 07:37:54 -07:00
/// <exception cref="ArgumentNullException"><c>info</c> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><c>info.PositionTicks</c> is <c>null</c> or negative.</exception>
2014-04-15 19:17:48 -07:00
public async Task OnPlaybackStopped(PlaybackStopInfo info)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
ArgumentNullException.ThrowIfNull(info);
if (info.PositionTicks.HasValue && info.PositionTicks.Value < 0)
{
throw new ArgumentOutOfRangeException(nameof(info), "The PlaybackStopInfo's PositionTicks was negative.");
}
2014-04-15 19:17:48 -07:00
var session = GetSession(info.SessionId);
2017-04-17 11:40:42 -07:00
session.StopAutomaticProgress();
var libraryItem = info.ItemId.Equals(default)
2014-04-15 19:17:48 -07:00
? null
2016-11-11 21:02:22 -07:00
: GetNowPlayingItem(session, info.ItemId);
2014-04-15 19:17:48 -07:00
// Normalize
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(info.MediaSourceId))
2014-04-15 19:17:48 -07:00
{
info.MediaSourceId = info.ItemId.ToString("N", CultureInfo.InvariantCulture);
2014-04-15 19:17:48 -07:00
}
2022-12-05 07:01:13 -07:00
if (!info.ItemId.Equals(default) && info.Item is null && libraryItem is not null)
{
var current = session.NowPlayingItem;
2022-12-05 07:00:20 -07:00
if (current is null || !info.ItemId.Equals(current.Id))
{
2015-04-02 09:24:59 -07:00
MediaSourceInfo mediaSource = null;
2020-01-16 16:19:58 -07:00
if (libraryItem is IHasMediaSources)
2015-04-02 09:24:59 -07:00
{
2018-09-12 10:26:21 -07:00
mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId, info.LiveStreamId).ConfigureAwait(false);
2015-04-02 09:24:59 -07:00
}
2015-03-29 09:45:16 -07:00
2017-06-09 12:26:54 -07:00
info.Item = GetItemInfo(libraryItem, mediaSource);
}
else
{
info.Item = current;
}
}
2022-12-05 07:01:13 -07:00
if (info.Item is not null)
2016-11-30 22:46:32 -07:00
{
var msString = info.PositionTicks.HasValue ? (info.PositionTicks.Value / 10000).ToString(CultureInfo.InvariantCulture) : "unknown";
2019-03-13 14:32:52 -07:00
_logger.LogInformation(
"Playback stopped reported by app {0} {1} playing {2}. Stopped at {3} ms",
2016-11-30 22:46:32 -07:00
session.Client,
session.ApplicationVersion,
info.Item.Name,
msString);
}
2022-12-05 07:01:13 -07:00
if (info.NowPlayingQueue is not null)
2018-09-12 10:26:21 -07:00
{
session.NowPlayingQueue = info.NowPlayingQueue;
}
session.PlaylistItemId = info.PlaylistItemId;
2014-04-15 19:17:48 -07:00
RemoveNowPlayingItem(session);
var users = GetUsers(session);
var playedToCompletion = false;
2014-04-15 19:17:48 -07:00
2022-12-05 07:01:13 -07:00
if (libraryItem is not null)
{
2014-04-15 19:17:48 -07:00
foreach (var user in users)
{
2018-09-12 10:26:21 -07:00
playedToCompletion = OnPlaybackStopped(user, libraryItem, info.PositionTicks, info.Failed);
2014-04-15 19:17:48 -07:00
}
}
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(info.LiveStreamId))
2015-03-29 11:16:40 -07:00
{
try
{
2016-09-29 05:55:49 -07:00
await _mediaSourceManager.CloseLiveStream(info.LiveStreamId).ConfigureAwait(false);
2015-03-29 11:16:40 -07:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Error closing live stream");
2015-03-29 11:16:40 -07:00
}
}
2020-08-15 12:55:31 -07:00
var eventArgs = new PlaybackStopEventArgs
{
Item = libraryItem,
Users = users,
PlaybackPositionTicks = info.PositionTicks,
PlayedToCompletion = playedToCompletion,
MediaSourceId = info.MediaSourceId,
MediaInfo = info.Item,
DeviceName = session.DeviceName,
ClientName = session.Client,
DeviceId = session.DeviceId,
Session = session,
PlaySessionId = info.PlaySessionId
2020-08-15 12:55:31 -07:00
};
await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
EventHelper.QueueEventIfNotNull(PlaybackStopped, this, eventArgs, _logger);
}
2018-09-12 10:26:21 -07:00
private bool OnPlaybackStopped(User user, BaseItem item, long? positionTicks, bool playbackFailed)
{
if (playbackFailed)
{
return false;
}
var data = _userDataManager.GetUserData(user, item);
bool playedToCompletion;
if (positionTicks.HasValue)
{
playedToCompletion = _userDataManager.UpdatePlayState(item, data, positionTicks.Value);
2016-03-06 21:56:45 -07:00
}
else
{
// If the client isn't able to report this, then we'll just have to make an assumption
data.PlayCount++;
data.Played = item.SupportsPlayedStatus;
data.PlaybackPositionTicks = 0;
playedToCompletion = true;
}
_userDataManager.SaveUserData(user, item, data, UserDataSaveReason.PlaybackFinished, CancellationToken.None);
2013-12-30 10:18:18 -07:00
return playedToCompletion;
}
2014-03-29 11:20:42 -07:00
/// <summary>
/// Gets the session.
/// </summary>
/// <param name="sessionId">The session identifier.</param>
/// <param name="throwOnMissing">if set to <c>true</c> [throw on missing].</param>
/// <returns>SessionInfo.</returns>
2020-01-16 16:19:58 -07:00
/// <exception cref="ResourceNotFoundException">
/// No session with an Id equal to <c>sessionId</c> was found
/// and <c>throwOnMissing</c> is <c>true</c>.
/// </exception>
private SessionInfo GetSession(string sessionId, bool throwOnMissing = true)
{
2019-03-13 14:32:52 -07:00
var session = Sessions.FirstOrDefault(i => string.Equals(i.Id, sessionId, StringComparison.Ordinal));
2022-12-05 07:00:20 -07:00
if (session is null && throwOnMissing)
{
2020-01-16 16:19:58 -07:00
throw new ResourceNotFoundException(
string.Format(CultureInfo.InvariantCulture, "Session {0} not found.", sessionId));
}
return session;
}
2015-03-01 22:16:29 -07:00
private SessionInfo GetSessionToRemoteControl(string sessionId)
{
// Accept either device id or session id
2019-03-13 14:32:52 -07:00
var session = Sessions.FirstOrDefault(i => string.Equals(i.Id, sessionId, StringComparison.Ordinal));
2015-03-01 22:16:29 -07:00
2022-12-05 07:00:20 -07:00
if (session is null)
2015-03-01 22:16:29 -07:00
{
2020-01-16 16:19:58 -07:00
throw new ResourceNotFoundException(
string.Format(CultureInfo.InvariantCulture, "Session {0} not found.", sessionId));
2015-03-01 22:16:29 -07:00
}
return session;
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2014-04-15 19:17:48 -07:00
public Task SendMessageCommand(string controllingSessionId, string sessionId, MessageCommand command, CancellationToken cancellationToken)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2014-05-08 13:09:53 -07:00
var generalCommand = new GeneralCommand
{
Name = GeneralCommandType.DisplayMessage
2014-05-08 13:09:53 -07:00
};
2014-05-08 13:09:53 -07:00
generalCommand.Arguments["Header"] = command.Header;
generalCommand.Arguments["Text"] = command.Text;
2014-03-29 11:20:42 -07:00
2014-05-08 13:09:53 -07:00
if (command.TimeoutMs.HasValue)
{
generalCommand.Arguments["TimeoutMs"] = command.TimeoutMs.Value.ToString(CultureInfo.InvariantCulture);
}
return SendGeneralCommand(controllingSessionId, sessionId, generalCommand, cancellationToken);
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2014-04-15 19:17:48 -07:00
public Task SendGeneralCommand(string controllingSessionId, string sessionId, GeneralCommand command, CancellationToken cancellationToken)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2015-03-01 22:16:29 -07:00
var session = GetSessionToRemoteControl(sessionId);
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(controllingSessionId))
2017-02-20 00:04:03 -07:00
{
var controllingSession = GetSession(controllingSessionId);
AssertCanControl(session, controllingSession);
}
2014-03-29 11:20:42 -07:00
return SendMessageToSession(session, SessionMessageType.GeneralCommand, command, cancellationToken);
2018-09-12 10:26:21 -07:00
}
private static async Task SendMessageToSession<T>(SessionInfo session, SessionMessageType name, T data, CancellationToken cancellationToken)
2018-09-12 10:26:21 -07:00
{
2019-12-17 15:15:02 -07:00
var controllers = session.SessionControllers;
var messageId = Guid.NewGuid();
2018-09-12 10:26:21 -07:00
foreach (var controller in controllers)
{
2019-12-17 15:15:02 -07:00
await controller.SendMessage(name, messageId, data, cancellationToken).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
}
}
private static Task SendMessageToSessions<T>(IEnumerable<SessionInfo> sessions, SessionMessageType name, T data, CancellationToken cancellationToken)
2019-03-27 08:07:08 -07:00
{
IEnumerable<Task> GetTasks()
{
2019-12-17 15:15:02 -07:00
var messageId = Guid.NewGuid();
2019-03-27 08:07:08 -07:00
foreach (var session in sessions)
{
var controllers = session.SessionControllers;
foreach (var controller in controllers)
{
2019-12-17 15:15:02 -07:00
yield return controller.SendMessage(name, messageId, data, cancellationToken);
2019-03-27 08:07:08 -07:00
}
}
}
return Task.WhenAll(GetTasks());
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2016-06-18 23:18:29 -07:00
public async Task SendPlayCommand(string controllingSessionId, string sessionId, PlayRequest command, CancellationToken cancellationToken)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2015-03-01 22:16:29 -07:00
var session = GetSessionToRemoteControl(sessionId);
var user = session.UserId.Equals(default) ? null : _userManager.GetUserById(session.UserId);
2014-03-29 11:20:42 -07:00
List<BaseItem> items;
if (command.PlayCommand == PlayCommand.PlayInstantMix)
{
items = command.ItemIds.SelectMany(i => TranslateItemForInstantMix(i, user))
.ToList();
command.PlayCommand = PlayCommand.PlayNow;
}
else
{
2016-06-18 23:18:29 -07:00
var list = new List<BaseItem>();
foreach (var itemId in command.ItemIds)
{
2017-05-25 23:48:54 -07:00
var subItems = TranslateItemForPlayback(itemId, user);
2016-06-18 23:18:29 -07:00
list.AddRange(subItems);
}
2016-11-30 22:46:32 -07:00
2017-11-09 13:58:09 -07:00
items = list;
}
2014-02-20 22:35:56 -07:00
2014-03-29 11:20:42 -07:00
if (command.PlayCommand == PlayCommand.PlayShuffle)
{
2019-10-20 07:08:40 -07:00
items.Shuffle();
2014-03-29 11:20:42 -07:00
command.PlayCommand = PlayCommand.PlayNow;
}
2018-12-28 08:48:26 -07:00
command.ItemIds = items.Select(i => i.Id).ToArray();
2022-12-05 07:01:13 -07:00
if (user is not null)
2014-03-29 11:20:42 -07:00
{
2014-02-20 22:35:56 -07:00
if (items.Any(i => i.GetPlayAccess(user) != PlayAccess.Full))
{
2020-01-16 16:19:58 -07:00
throw new ArgumentException(
2020-05-12 19:10:35 -07:00
string.Format(CultureInfo.InvariantCulture, "{0} is not allowed to play media.", user.Username));
}
}
2022-12-05 07:01:13 -07:00
if (user is not null
2019-10-20 07:08:40 -07:00
&& command.ItemIds.Length == 1
2020-05-12 19:10:35 -07:00
&& user.EnableNextEpisodeAutoPlay
2019-10-20 07:08:40 -07:00
&& _libraryManager.GetItemById(command.ItemIds[0]) is Episode episode)
2016-02-26 07:50:58 -07:00
{
2019-10-20 07:08:40 -07:00
var series = episode.Series;
2022-12-05 07:01:13 -07:00
if (series is not null)
2016-02-26 07:50:58 -07:00
{
2019-10-20 07:08:40 -07:00
var episodes = series.GetEpisodes(
user,
new DtoOptions(false)
{
EnableImages = false
})
.Where(i => !i.IsVirtualItem)
.SkipWhile(i => !i.Id.Equals(episode.Id))
2019-10-20 07:08:40 -07:00
.ToList();
if (episodes.Count > 0)
2016-02-26 07:50:58 -07:00
{
2019-10-20 07:08:40 -07:00
command.ItemIds = episodes.Select(i => i.Id).ToArray();
2016-02-26 07:50:58 -07:00
}
}
}
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(controllingSessionId))
{
2017-02-20 00:04:03 -07:00
var controllingSession = GetSession(controllingSessionId);
AssertCanControl(session, controllingSession);
if (!controllingSession.UserId.Equals(default))
2017-02-20 00:04:03 -07:00
{
2018-09-12 10:26:21 -07:00
command.ControllingUserId = controllingSession.UserId;
2017-02-20 00:04:03 -07:00
}
}
await SendMessageToSession(session, SessionMessageType.Play, command, cancellationToken).ConfigureAwait(false);
}
2020-04-01 08:52:42 -07:00
/// <inheritdoc />
public async Task SendSyncPlayCommand(string sessionId, SendCommand command, CancellationToken cancellationToken)
2020-04-01 08:52:42 -07:00
{
CheckDisposed();
var session = GetSession(sessionId);
await SendMessageToSession(session, SessionMessageType.SyncPlayCommand, command, cancellationToken).ConfigureAwait(false);
2020-04-01 08:52:42 -07:00
}
/// <inheritdoc />
public async Task SendSyncPlayGroupUpdate<T>(string sessionId, GroupUpdate<T> command, CancellationToken cancellationToken)
2020-04-01 08:52:42 -07:00
{
CheckDisposed();
var session = GetSession(sessionId);
await SendMessageToSession(session, SessionMessageType.SyncPlayGroupUpdate, command, cancellationToken).ConfigureAwait(false);
2020-04-01 08:52:42 -07:00
}
2019-02-26 12:47:23 -07:00
private IEnumerable<BaseItem> TranslateItemForPlayback(Guid id, User user)
2014-03-29 11:20:42 -07:00
{
2015-07-05 11:34:52 -07:00
var item = _libraryManager.GetItemById(id);
2022-12-05 07:00:20 -07:00
if (item is null)
2015-07-05 11:34:52 -07:00
{
_logger.LogError("A non-existent item Id {0} was passed into TranslateItemForPlayback", id);
2019-10-20 07:08:40 -07:00
return Array.Empty<BaseItem>();
2015-07-05 11:34:52 -07:00
}
2014-03-29 11:20:42 -07:00
2019-03-13 14:32:52 -07:00
if (item is IItemByName byName)
{
2018-09-12 10:26:21 -07:00
return byName.GetTaggedItems(new InternalItemsQuery(user)
2016-05-07 10:47:41 -07:00
{
IsFolder = false,
2017-05-21 00:25:49 -07:00
Recursive = true,
DtoOptions = new DtoOptions(false)
{
2017-05-24 12:12:55 -07:00
EnableImages = false,
2020-05-12 19:10:35 -07:00
Fields = new[]
2017-05-24 12:12:55 -07:00
{
ItemFields.SortName
}
2017-11-09 13:58:09 -07:00
},
2018-09-12 10:26:21 -07:00
IsVirtualItem = false,
2019-10-20 07:08:40 -07:00
OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }
2016-05-07 10:47:41 -07:00
});
}
2015-03-09 12:40:03 -07:00
2014-03-29 11:20:42 -07:00
if (item.IsFolder)
{
var folder = (Folder)item;
2018-09-12 10:26:21 -07:00
return folder.GetItemList(new InternalItemsQuery(user)
2016-05-07 10:47:41 -07:00
{
Recursive = true,
2017-05-21 00:25:49 -07:00
IsFolder = false,
DtoOptions = new DtoOptions(false)
{
2017-05-24 12:12:55 -07:00
EnableImages = false,
2017-08-19 12:43:35 -07:00
Fields = new ItemFields[]
2017-05-24 12:12:55 -07:00
{
ItemFields.SortName
}
2017-11-09 13:58:09 -07:00
},
2018-09-12 10:26:21 -07:00
IsVirtualItem = false,
2019-10-20 07:08:40 -07:00
OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }
2017-05-25 23:48:54 -07:00
});
2014-03-29 11:20:42 -07:00
}
2019-10-20 07:08:40 -07:00
return new[] { item };
2014-03-29 11:20:42 -07:00
}
2018-09-12 10:26:21 -07:00
private IEnumerable<BaseItem> TranslateItemForInstantMix(Guid id, User user)
{
var item = _libraryManager.GetItemById(id);
2022-12-05 07:00:20 -07:00
if (item is null)
2015-07-05 11:34:52 -07:00
{
2019-03-13 14:32:52 -07:00
_logger.LogError("A non-existent item Id {0} was passed into TranslateItemForInstantMix", id);
2015-07-05 11:34:52 -07:00
return new List<BaseItem>();
}
2017-05-21 00:25:49 -07:00
return _musicManager.GetInstantMixFromItem(item, user, new DtoOptions(false) { EnableImages = false });
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2014-04-15 19:17:48 -07:00
public Task SendBrowseCommand(string controllingSessionId, string sessionId, BrowseRequest command, CancellationToken cancellationToken)
{
2014-04-13 10:27:13 -07:00
var generalCommand = new GeneralCommand
{
Name = GeneralCommandType.DisplayContent,
2019-03-13 14:32:52 -07:00
Arguments =
{
["ItemId"] = command.ItemId,
["ItemName"] = command.ItemName,
2021-12-11 19:31:30 -07:00
["ItemType"] = command.ItemType.ToString()
2019-03-13 14:32:52 -07:00
}
2014-04-13 10:27:13 -07:00
};
2014-04-13 10:27:13 -07:00
return SendGeneralCommand(controllingSessionId, sessionId, generalCommand, cancellationToken);
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2014-04-15 19:17:48 -07:00
public Task SendPlaystateCommand(string controllingSessionId, string sessionId, PlaystateRequest command, CancellationToken cancellationToken)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2015-03-01 22:16:29 -07:00
var session = GetSessionToRemoteControl(sessionId);
2018-09-12 10:26:21 -07:00
if (!string.IsNullOrEmpty(controllingSessionId))
{
2017-02-20 00:04:03 -07:00
var controllingSession = GetSession(controllingSessionId);
AssertCanControl(session, controllingSession);
if (!controllingSession.UserId.Equals(default))
2017-02-20 00:04:03 -07:00
{
command.ControllingUserId = controllingSession.UserId.ToString("N", CultureInfo.InvariantCulture);
2017-02-20 00:04:03 -07:00
}
}
return SendMessageToSession(session, SessionMessageType.Playstate, command, cancellationToken);
2013-10-02 18:22:50 -07:00
}
2019-03-27 08:13:36 -07:00
private static void AssertCanControl(SessionInfo session, SessionInfo controllingSession)
2014-03-16 09:15:10 -07:00
{
ArgumentNullException.ThrowIfNull(session);
2019-03-27 08:13:36 -07:00
ArgumentNullException.ThrowIfNull(controllingSession);
2014-03-16 09:15:10 -07:00
}
/// <summary>
/// Sends the restart required message.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
2019-03-27 08:13:36 -07:00
public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
return SendMessageToSessions(Sessions, SessionMessageType.RestartRequired, string.Empty, cancellationToken);
}
/// <summary>
/// Adds the additional user.
/// </summary>
/// <param name="sessionId">The session identifier.</param>
/// <param name="userId">The user identifier.</param>
2019-01-13 13:37:13 -07:00
/// <exception cref="UnauthorizedAccessException">Cannot modify additional users without authenticating first.</exception>
/// <exception cref="ArgumentException">The requested user is already the primary user of the session.</exception>
2018-09-12 10:26:21 -07:00
public void AddAdditionalUser(string sessionId, Guid userId)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
var session = GetSession(sessionId);
if (session.UserId.Equals(userId))
{
throw new ArgumentException("The requested user is already the primary user of the session.");
}
if (session.AdditionalUsers.All(i => !i.UserId.Equals(userId)))
{
var user = _userManager.GetUserById(userId);
2017-08-19 12:43:35 -07:00
var list = session.AdditionalUsers.ToList();
list.Add(new SessionUserInfo
{
2015-05-29 16:51:33 -07:00
UserId = userId,
2020-05-12 19:10:35 -07:00
UserName = user.Username
});
2017-08-19 12:43:35 -07:00
2018-12-28 08:48:26 -07:00
session.AdditionalUsers = list.ToArray();
}
}
/// <summary>
/// Removes the additional user.
/// </summary>
/// <param name="sessionId">The session identifier.</param>
/// <param name="userId">The user identifier.</param>
2019-01-13 13:37:13 -07:00
/// <exception cref="UnauthorizedAccessException">Cannot modify additional users without authenticating first.</exception>
/// <exception cref="ArgumentException">The requested user is already the primary user of the session.</exception>
2018-09-12 10:26:21 -07:00
public void RemoveAdditionalUser(string sessionId, Guid userId)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
var session = GetSession(sessionId);
2018-09-12 10:26:21 -07:00
if (session.UserId.Equals(userId))
{
throw new ArgumentException("The requested user is already the primary user of the session.");
}
2018-09-12 10:26:21 -07:00
var user = session.AdditionalUsers.FirstOrDefault(i => i.UserId.Equals(userId));
2022-12-05 07:01:13 -07:00
if (user is not null)
{
2017-08-19 12:43:35 -07:00
var list = session.AdditionalUsers.ToList();
list.Remove(user);
2018-12-28 08:48:26 -07:00
session.AdditionalUsers = list.ToArray();
}
}
2014-01-11 16:07:56 -07:00
/// <summary>
/// Authenticates the new session.
/// </summary>
/// <param name="request">The authenticationrequest.</param>
/// <returns>The authentication result.</returns>
2016-02-20 23:25:25 -07:00
public Task<AuthenticationResult> AuthenticateNewSession(AuthenticationRequest request)
{
return AuthenticateNewSessionInternal(request, true);
}
/// <summary>
/// Directly authenticates the session without enforcing password.
/// </summary>
/// <param name="request">The authentication request.</param>
/// <returns>The authentication result.</returns>
public Task<AuthenticationResult> AuthenticateDirect(AuthenticationRequest request)
2020-04-15 12:28:42 -07:00
{
return AuthenticateNewSessionInternal(request, false);
2016-02-20 23:25:25 -07:00
}
private async Task<AuthenticationResult> AuthenticateNewSessionInternal(AuthenticationRequest request, bool enforcePassword)
2014-01-11 16:07:56 -07:00
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2016-09-14 14:34:19 -07:00
User user = null;
if (!request.UserId.Equals(default))
2016-09-14 14:34:19 -07:00
{
2019-06-09 13:08:01 -07:00
user = _userManager.GetUserById(request.UserId);
2016-09-14 14:34:19 -07:00
}
user ??= _userManager.GetUserByName(request.Username);
2014-10-15 20:26:39 -07:00
2020-03-29 13:57:13 -07:00
if (enforcePassword)
{
user = await _userManager.AuthenticateUser(
request.Username,
request.Password,
2021-04-22 05:49:54 -07:00
null,
2020-03-29 13:57:13 -07:00
request.RemoteEndPoint,
true).ConfigureAwait(false);
}
2022-12-05 07:00:20 -07:00
if (user is null)
2014-12-29 13:18:48 -07:00
{
2023-07-29 04:50:55 -07:00
await _eventManager.PublishAsync(new AuthenticationRequestEventArgs(request)).ConfigureAwait(false);
throw new AuthenticationException("Invalid username or password entered.");
2014-12-29 13:18:48 -07:00
}
2020-02-01 08:09:18 -07:00
if (!string.IsNullOrEmpty(request.DeviceId)
&& !_deviceManager.CanAccessDevice(user, request.DeviceId))
2019-12-13 07:27:12 -07:00
{
2020-02-01 08:09:18 -07:00
throw new SecurityException("User is not allowed access from this device.");
2019-12-13 07:27:12 -07:00
}
2020-10-04 13:57:48 -07:00
int sessionsCount = Sessions.Count(i => i.UserId.Equals(user.Id));
int maxActiveSessions = user.MaxActiveSessions;
2020-10-04 10:30:21 -07:00
_logger.LogInformation("Current/Max sessions for user {User}: {Sessions}/{Max}", user.Username, sessionsCount, maxActiveSessions);
if (maxActiveSessions >= 1 && sessionsCount >= maxActiveSessions)
{
throw new SecurityException("User is at their maximum number of sessions.");
}
2021-05-20 20:56:59 -07:00
var token = await GetAuthorizationToken(user, request.DeviceId, request.App, request.AppVersion, request.DeviceName).ConfigureAwait(false);
2014-07-07 18:41:03 -07:00
2021-04-10 13:57:25 -07:00
var session = await LogSessionActivity(
2019-03-13 14:32:52 -07:00
request.App,
2014-08-10 15:13:17 -07:00
request.AppVersion,
request.DeviceId,
request.DeviceName,
request.RemoteEndPoint,
2021-04-10 13:57:25 -07:00
user).ConfigureAwait(false);
2014-10-11 13:38:13 -07:00
2018-09-12 10:26:21 -07:00
var returnResult = new AuthenticationResult
2014-07-02 11:34:08 -07:00
{
2014-08-15 09:35:41 -07:00
User = _userManager.GetUserDto(user, request.RemoteEndPoint),
2018-09-12 10:26:21 -07:00
SessionInfo = session,
2014-07-27 15:01:29 -07:00
AccessToken = token,
2014-09-06 10:46:09 -07:00
ServerId = _appHost.SystemId
2014-07-02 11:34:08 -07:00
};
2016-05-07 10:47:41 -07:00
2023-07-29 04:50:55 -07:00
await _eventManager.PublishAsync(new AuthenticationResultEventArgs(returnResult)).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
return returnResult;
}
2016-05-07 10:47:41 -07:00
2021-05-20 20:56:59 -07:00
private async Task<string> GetAuthorizationToken(User user, string deviceId, string app, string appVersion, string deviceName)
2014-07-07 18:41:03 -07:00
{
2021-05-20 20:56:59 -07:00
var existing = (await _deviceManager.GetDevices(
new DeviceQuery
2020-01-16 16:19:58 -07:00
{
DeviceId = deviceId,
UserId = user.Id
}).ConfigureAwait(false)).Items;
2018-09-12 10:26:21 -07:00
foreach (var auth in existing)
2014-07-07 18:41:03 -07:00
{
try
{
// Logout any existing sessions for the user on this device
await Logout(auth).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while logging out existing session.");
}
2014-07-07 18:41:03 -07:00
}
_logger.LogInformation("Creating new access token for user {0}", user.Id);
2021-05-20 20:56:59 -07:00
var device = await _deviceManager.CreateDevice(new Device(user.Id, app, appVersion, deviceName, deviceId)).ConfigureAwait(false);
2014-07-07 18:41:03 -07:00
2021-05-20 20:56:59 -07:00
return device.AccessToken;
2014-07-07 18:41:03 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2021-05-20 20:56:59 -07:00
public async Task Logout(string accessToken)
2014-07-07 18:41:03 -07:00
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
ArgumentException.ThrowIfNullOrEmpty(accessToken);
2014-07-07 18:41:03 -07:00
2021-05-20 20:56:59 -07:00
var existing = (await _deviceManager.GetDevices(
new DeviceQuery
2020-01-16 16:19:58 -07:00
{
Limit = 1,
AccessToken = accessToken
2021-05-20 20:56:59 -07:00
}).ConfigureAwait(false)).Items;
2014-07-07 18:41:03 -07:00
2020-01-16 16:19:58 -07:00
if (existing.Count > 0)
2014-07-07 18:41:03 -07:00
{
2021-05-20 20:56:59 -07:00
await Logout(existing[0]).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
}
}
2014-07-07 18:41:03 -07:00
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2021-09-03 09:46:34 -07:00
public async Task Logout(Device device)
2018-09-12 10:26:21 -07:00
{
CheckDisposed();
2014-07-07 18:41:03 -07:00
2021-09-03 09:46:34 -07:00
_logger.LogInformation("Logging out access token {0}", device.AccessToken);
2018-09-12 10:26:21 -07:00
2021-09-03 09:46:34 -07:00
await _deviceManager.DeleteDevice(device).ConfigureAwait(false);
2014-07-07 18:41:03 -07:00
2018-09-12 10:26:21 -07:00
var sessions = Sessions
2021-09-03 09:46:34 -07:00
.Where(i => string.Equals(i.DeviceId, device.DeviceId, StringComparison.OrdinalIgnoreCase))
2018-09-12 10:26:21 -07:00
.ToList();
foreach (var session in sessions)
{
try
2014-07-07 18:41:03 -07:00
{
2018-09-12 10:26:21 -07:00
ReportSessionEnded(session.Id);
}
catch (Exception ex)
{
2021-09-03 09:46:34 -07:00
_logger.LogError(ex, "Error reporting session ended");
2014-07-07 18:41:03 -07:00
}
}
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2021-05-20 20:56:59 -07:00
public async Task RevokeUserTokens(Guid userId, string currentAccessToken)
2014-07-07 18:41:03 -07:00
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2021-05-20 20:56:59 -07:00
var existing = await _deviceManager.GetDevices(new DeviceQuery
2014-07-07 18:41:03 -07:00
{
UserId = userId
2021-05-20 20:56:59 -07:00
}).ConfigureAwait(false);
2014-07-07 18:41:03 -07:00
foreach (var info in existing.Items)
{
if (!string.Equals(currentAccessToken, info.AccessToken, StringComparison.OrdinalIgnoreCase))
{
2021-05-20 20:56:59 -07:00
await Logout(info).ConfigureAwait(false);
}
2014-07-07 18:41:03 -07:00
}
}
2014-07-11 19:31:08 -07:00
2014-03-20 20:31:40 -07:00
/// <summary>
/// Reports the capabilities.
/// </summary>
/// <param name="sessionId">The session identifier.</param>
/// <param name="capabilities">The capabilities.</param>
2014-10-11 13:38:13 -07:00
public void ReportCapabilities(string sessionId, ClientCapabilities capabilities)
2014-03-20 20:31:40 -07:00
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2014-03-20 20:31:40 -07:00
var session = GetSession(sessionId);
2014-06-03 20:34:36 -07:00
ReportCapabilities(session, capabilities, true);
}
2019-03-13 14:32:52 -07:00
private void ReportCapabilities(
SessionInfo session,
2014-10-11 13:38:13 -07:00
ClientCapabilities capabilities,
2014-06-03 20:34:36 -07:00
bool saveCapabilities)
{
2015-02-19 12:21:03 -07:00
session.Capabilities = capabilities;
2014-05-13 17:46:45 -07:00
2018-09-12 10:26:21 -07:00
if (saveCapabilities)
2014-05-13 17:46:45 -07:00
{
2019-03-13 14:32:52 -07:00
CapabilitiesChanged?.Invoke(
this,
new SessionEventArgs
{
SessionInfo = session
});
2014-06-03 20:34:36 -07:00
_deviceManager.SaveCapabilities(session.DeviceId, capabilities);
2014-06-03 20:34:36 -07:00
}
}
2014-04-06 10:53:23 -07:00
/// <summary>
2020-01-16 16:19:58 -07:00
/// Converts a BaseItem to a BaseItemInfo.
2014-04-06 10:53:23 -07:00
/// </summary>
2017-06-09 12:26:54 -07:00
private BaseItemDto GetItemInfo(BaseItem item, MediaSourceInfo mediaSource)
2014-04-06 10:53:23 -07:00
{
ArgumentNullException.ThrowIfNull(item);
2014-04-06 10:53:23 -07:00
2017-06-09 12:26:54 -07:00
var dtoOptions = _itemInfoDtoOptions;
2014-04-06 10:53:23 -07:00
2022-12-05 07:00:20 -07:00
if (_itemInfoDtoOptions is null)
2014-04-06 10:53:23 -07:00
{
2017-06-09 12:26:54 -07:00
dtoOptions = new DtoOptions
2014-04-06 10:53:23 -07:00
{
2017-06-09 12:26:54 -07:00
AddProgramRecordingInfo = false
};
2017-08-19 12:43:35 -07:00
var fields = dtoOptions.Fields.ToList();
fields.Remove(ItemFields.BasicSyncInfo);
fields.Remove(ItemFields.CanDelete);
fields.Remove(ItemFields.CanDownload);
fields.Remove(ItemFields.ChildCount);
fields.Remove(ItemFields.CustomRating);
fields.Remove(ItemFields.DateLastMediaAdded);
fields.Remove(ItemFields.DateLastRefreshed);
fields.Remove(ItemFields.DateLastSaved);
fields.Remove(ItemFields.DisplayPreferencesId);
fields.Remove(ItemFields.Etag);
fields.Remove(ItemFields.InheritedParentalRatingValue);
fields.Remove(ItemFields.ItemCounts);
fields.Remove(ItemFields.MediaSourceCount);
fields.Remove(ItemFields.MediaStreams);
fields.Remove(ItemFields.MediaSources);
fields.Remove(ItemFields.People);
fields.Remove(ItemFields.PlayAccess);
fields.Remove(ItemFields.People);
fields.Remove(ItemFields.ProductionLocations);
fields.Remove(ItemFields.RecursiveItemCount);
fields.Remove(ItemFields.RemoteTrailers);
fields.Remove(ItemFields.SeasonUserData);
fields.Remove(ItemFields.Settings);
fields.Remove(ItemFields.SortName);
fields.Remove(ItemFields.Tags);
2018-09-12 10:26:21 -07:00
fields.Remove(ItemFields.ExtraIds);
2017-08-19 12:43:35 -07:00
2018-12-28 08:48:26 -07:00
dtoOptions.Fields = fields.ToArray();
2017-06-09 12:26:54 -07:00
_itemInfoDtoOptions = dtoOptions;
}
var info = _dtoService.GetBaseItemDto(item, dtoOptions);
2014-05-07 22:04:39 -07:00
2022-12-05 07:01:13 -07:00
if (mediaSource is not null)
2014-05-07 22:04:39 -07:00
{
2017-08-19 12:43:35 -07:00
info.MediaStreams = mediaSource.MediaStreams.ToArray();
2014-05-07 22:04:39 -07:00
}
2014-04-06 10:53:23 -07:00
return info;
}
2020-05-15 14:24:01 -07:00
private string GetImageCacheTag(User user)
2014-04-06 10:53:23 -07:00
{
try
{
2020-05-12 19:10:35 -07:00
return _imageProcessor.GetImageCacheTag(user);
2014-04-06 10:53:23 -07:00
}
2020-05-12 19:10:35 -07:00
catch (Exception e)
2014-04-06 10:53:23 -07:00
{
2020-05-12 19:10:35 -07:00
_logger.LogError(e, "Error getting image information for profile image");
2014-04-06 10:53:23 -07:00
return null;
}
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2014-04-30 20:24:55 -07:00
public void ReportNowViewingItem(string sessionId, string itemId)
2014-04-15 19:17:48 -07:00
{
ArgumentException.ThrowIfNullOrEmpty(itemId);
2016-05-30 09:08:46 -07:00
2020-01-16 16:19:58 -07:00
var item = _libraryManager.GetItemById(new Guid(itemId));
2020-02-01 08:16:11 -07:00
var session = GetSession(sessionId);
2014-04-15 19:17:48 -07:00
2021-04-01 07:42:00 -07:00
session.NowViewingItem = GetItemInfo(item, null);
2014-04-15 19:17:48 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
public void ReportTranscodingInfo(string deviceId, TranscodingInfo info)
{
2020-02-01 08:07:46 -07:00
var session = Sessions.FirstOrDefault(i =>
string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase));
2022-12-05 07:01:13 -07:00
if (session is not null)
{
session.TranscodingInfo = info;
}
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
public void ClearTranscodingInfo(string deviceId)
{
ReportTranscodingInfo(deviceId, null);
}
2014-06-25 08:12:39 -07:00
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2014-06-25 08:12:39 -07:00
public SessionInfo GetSession(string deviceId, string client, string version)
{
2020-02-01 08:07:46 -07:00
return Sessions.FirstOrDefault(i =>
string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase)
&& string.Equals(i.Client, client, StringComparison.OrdinalIgnoreCase));
2014-06-25 08:12:39 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2021-05-20 20:56:59 -07:00
public Task<SessionInfo> GetSessionByAuthenticationToken(Device info, string deviceId, string remoteEndpoint, string appVersion)
2015-03-09 12:40:03 -07:00
{
ArgumentNullException.ThrowIfNull(info);
2015-03-09 12:40:03 -07:00
var user = info.UserId.Equals(default)
2015-03-09 12:40:03 -07:00
? null
: _userManager.GetUserById(info.UserId);
2018-09-12 10:26:21 -07:00
appVersion = string.IsNullOrEmpty(appVersion)
2015-03-14 18:42:09 -07:00
? info.AppVersion
2015-03-09 12:40:03 -07:00
: appVersion;
2015-03-12 18:55:22 -07:00
var deviceName = info.DeviceName;
2015-03-14 18:42:09 -07:00
var appName = info.AppName;
2015-03-12 18:55:22 -07:00
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(deviceId))
2015-03-12 18:55:22 -07:00
{
deviceId = info.DeviceId;
}
2015-03-28 11:55:00 -07:00
// Prevent argument exception
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(appVersion))
2015-03-28 11:55:00 -07:00
{
appVersion = "1";
}
2015-03-21 09:10:02 -07:00
return LogSessionActivity(appName, appVersion, deviceId, deviceName, remoteEndpoint, user);
2015-03-09 12:40:03 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
2021-05-20 20:56:59 -07:00
public async Task<SessionInfo> GetSessionByAuthenticationToken(string token, string deviceId, string remoteEndpoint)
2015-03-08 12:48:30 -07:00
{
2021-05-20 20:56:59 -07:00
var items = (await _deviceManager.GetDevices(new DeviceQuery
2015-03-08 12:48:30 -07:00
{
2020-01-16 16:19:58 -07:00
AccessToken = token,
Limit = 1
2021-05-20 20:56:59 -07:00
}).ConfigureAwait(false)).Items;
2015-03-08 12:48:30 -07:00
2020-01-16 16:19:58 -07:00
if (items.Count == 0)
2015-03-09 12:40:03 -07:00
{
2018-09-12 10:26:21 -07:00
return null;
2015-03-09 12:40:03 -07:00
}
2021-05-20 20:56:59 -07:00
return await GetSessionByAuthenticationToken(items[0], deviceId, remoteEndpoint, null).ConfigureAwait(false);
2015-03-08 12:48:30 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
public Task SendMessageToAdminSessions<T>(SessionMessageType name, T data, CancellationToken cancellationToken)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
2020-05-12 19:10:35 -07:00
var adminUserIds = _userManager.Users
.Where(i => i.HasPermission(PermissionKind.IsAdministrator))
.Select(i => i.Id)
.ToList();
2016-08-20 11:43:13 -07:00
return SendMessageToUserSessions(adminUserIds, name, data, cancellationToken);
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
public Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, Func<T> dataFn, CancellationToken cancellationToken)
2018-09-12 10:26:21 -07:00
{
CheckDisposed();
var sessions = Sessions.Where(i => userIds.Any(i.ContainsUser)).ToList();
if (sessions.Count == 0)
{
return Task.CompletedTask;
}
2020-01-16 16:19:58 -07:00
return SendMessageToSessions(sessions, name, dataFn(), cancellationToken);
2018-09-12 10:26:21 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
public Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, T data, CancellationToken cancellationToken)
2018-09-12 10:26:21 -07:00
{
CheckDisposed();
var sessions = Sessions.Where(i => userIds.Any(i.ContainsUser));
2019-03-27 08:07:08 -07:00
return SendMessageToSessions(sessions, name, data, cancellationToken);
2018-09-12 10:26:21 -07:00
}
2020-01-16 16:19:58 -07:00
/// <inheritdoc />
public Task SendMessageToUserDeviceSessions<T>(string deviceId, SessionMessageType name, T data, CancellationToken cancellationToken)
{
2018-09-12 10:26:21 -07:00
CheckDisposed();
var sessions = Sessions.Where(i => string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase));
2019-03-27 08:07:08 -07:00
return SendMessageToSessions(sessions, name, data, cancellationToken);
2015-01-27 21:04:26 -07:00
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (_disposed)
{
return;
}
foreach (var session in _activeConnections.Values)
{
await session.DisposeAsync().ConfigureAwait(false);
}
if (_idleTimer is not null)
{
await _idleTimer.DisposeAsync().ConfigureAwait(false);
_idleTimer = null;
}
await _shutdownCallback.DisposeAsync().ConfigureAwait(false);
_deviceManager.DeviceOptionsUpdated -= OnDeviceManagerDeviceOptionsUpdated;
_disposed = true;
}
private async void OnApplicationStopping()
{
_logger.LogInformation("Sending shutdown notifications");
try
{
var messageType = _appHost.ShouldRestart ? SessionMessageType.ServerRestarting : SessionMessageType.ServerShuttingDown;
await SendMessageToSessions(Sessions, messageType, string.Empty, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending server shutdown notifications");
}
// Close open websockets to allow Kestrel to shut down cleanly
foreach (var session in _activeConnections.Values)
{
await session.DisposeAsync().ConfigureAwait(false);
}
_activeConnections.Clear();
}
}
2018-12-15 08:30:38 -07:00
}