SessionManager.cs 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Jellyfin.Data.Entities;
  11. using Jellyfin.Data.Entities.Security;
  12. using Jellyfin.Data.Enums;
  13. using Jellyfin.Data.Events;
  14. using Jellyfin.Data.Queries;
  15. using Jellyfin.Extensions;
  16. using MediaBrowser.Common.Events;
  17. using MediaBrowser.Common.Extensions;
  18. using MediaBrowser.Controller;
  19. using MediaBrowser.Controller.Authentication;
  20. using MediaBrowser.Controller.Devices;
  21. using MediaBrowser.Controller.Drawing;
  22. using MediaBrowser.Controller.Dto;
  23. using MediaBrowser.Controller.Entities;
  24. using MediaBrowser.Controller.Events;
  25. using MediaBrowser.Controller.Events.Authentication;
  26. using MediaBrowser.Controller.Events.Session;
  27. using MediaBrowser.Controller.Library;
  28. using MediaBrowser.Controller.Net;
  29. using MediaBrowser.Controller.Session;
  30. using MediaBrowser.Model.Dto;
  31. using MediaBrowser.Model.Entities;
  32. using MediaBrowser.Model.Library;
  33. using MediaBrowser.Model.Querying;
  34. using MediaBrowser.Model.Session;
  35. using MediaBrowser.Model.SyncPlay;
  36. using Microsoft.EntityFrameworkCore;
  37. using Microsoft.Extensions.Hosting;
  38. using Microsoft.Extensions.Logging;
  39. using Episode = MediaBrowser.Controller.Entities.TV.Episode;
  40. namespace Emby.Server.Implementations.Session
  41. {
  42. /// <summary>
  43. /// Class SessionManager.
  44. /// </summary>
  45. public sealed class SessionManager : ISessionManager, IAsyncDisposable
  46. {
  47. private readonly IUserDataManager _userDataManager;
  48. private readonly ILogger<SessionManager> _logger;
  49. private readonly IEventManager _eventManager;
  50. private readonly ILibraryManager _libraryManager;
  51. private readonly IUserManager _userManager;
  52. private readonly IMusicManager _musicManager;
  53. private readonly IDtoService _dtoService;
  54. private readonly IImageProcessor _imageProcessor;
  55. private readonly IMediaSourceManager _mediaSourceManager;
  56. private readonly IServerApplicationHost _appHost;
  57. private readonly IDeviceManager _deviceManager;
  58. private readonly CancellationTokenRegistration _shutdownCallback;
  59. private readonly ConcurrentDictionary<string, SessionInfo> _activeConnections
  60. = new(StringComparer.OrdinalIgnoreCase);
  61. private Timer _idleTimer;
  62. private DtoOptions _itemInfoDtoOptions;
  63. private bool _disposed = false;
  64. public SessionManager(
  65. ILogger<SessionManager> logger,
  66. IEventManager eventManager,
  67. IUserDataManager userDataManager,
  68. ILibraryManager libraryManager,
  69. IUserManager userManager,
  70. IMusicManager musicManager,
  71. IDtoService dtoService,
  72. IImageProcessor imageProcessor,
  73. IServerApplicationHost appHost,
  74. IDeviceManager deviceManager,
  75. IMediaSourceManager mediaSourceManager,
  76. IHostApplicationLifetime hostApplicationLifetime)
  77. {
  78. _logger = logger;
  79. _eventManager = eventManager;
  80. _userDataManager = userDataManager;
  81. _libraryManager = libraryManager;
  82. _userManager = userManager;
  83. _musicManager = musicManager;
  84. _dtoService = dtoService;
  85. _imageProcessor = imageProcessor;
  86. _appHost = appHost;
  87. _deviceManager = deviceManager;
  88. _mediaSourceManager = mediaSourceManager;
  89. _shutdownCallback = hostApplicationLifetime.ApplicationStopping.Register(OnApplicationStopping);
  90. _deviceManager.DeviceOptionsUpdated += OnDeviceManagerDeviceOptionsUpdated;
  91. }
  92. /// <summary>
  93. /// Occurs when playback has started.
  94. /// </summary>
  95. public event EventHandler<PlaybackProgressEventArgs> PlaybackStart;
  96. /// <summary>
  97. /// Occurs when playback has progressed.
  98. /// </summary>
  99. public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
  100. /// <summary>
  101. /// Occurs when playback has stopped.
  102. /// </summary>
  103. public event EventHandler<PlaybackStopEventArgs> PlaybackStopped;
  104. /// <inheritdoc />
  105. public event EventHandler<SessionEventArgs> SessionStarted;
  106. /// <inheritdoc />
  107. public event EventHandler<SessionEventArgs> CapabilitiesChanged;
  108. /// <inheritdoc />
  109. public event EventHandler<SessionEventArgs> SessionEnded;
  110. /// <inheritdoc />
  111. public event EventHandler<SessionEventArgs> SessionActivity;
  112. /// <inheritdoc />
  113. public event EventHandler<SessionEventArgs> SessionControllerConnected;
  114. /// <summary>
  115. /// Gets all connections.
  116. /// </summary>
  117. /// <value>All connections.</value>
  118. public IEnumerable<SessionInfo> Sessions => _activeConnections.Values.OrderByDescending(c => c.LastActivityDate);
  119. private void OnDeviceManagerDeviceOptionsUpdated(object sender, GenericEventArgs<Tuple<string, DeviceOptions>> e)
  120. {
  121. foreach (var session in Sessions)
  122. {
  123. if (string.Equals(session.DeviceId, e.Argument.Item1, StringComparison.Ordinal))
  124. {
  125. if (!string.IsNullOrWhiteSpace(e.Argument.Item2.CustomName))
  126. {
  127. session.HasCustomDeviceName = true;
  128. session.DeviceName = e.Argument.Item2.CustomName;
  129. }
  130. else
  131. {
  132. session.HasCustomDeviceName = false;
  133. }
  134. }
  135. }
  136. }
  137. private void CheckDisposed()
  138. {
  139. if (_disposed)
  140. {
  141. throw new ObjectDisposedException(GetType().Name);
  142. }
  143. }
  144. private void OnSessionStarted(SessionInfo info)
  145. {
  146. if (!string.IsNullOrEmpty(info.DeviceId))
  147. {
  148. var capabilities = _deviceManager.GetCapabilities(info.DeviceId);
  149. if (capabilities is not null)
  150. {
  151. ReportCapabilities(info, capabilities, false);
  152. }
  153. }
  154. _eventManager.Publish(new SessionStartedEventArgs(info));
  155. EventHelper.QueueEventIfNotNull(
  156. SessionStarted,
  157. this,
  158. new SessionEventArgs
  159. {
  160. SessionInfo = info
  161. },
  162. _logger);
  163. }
  164. private void OnSessionEnded(SessionInfo info)
  165. {
  166. EventHelper.QueueEventIfNotNull(
  167. SessionEnded,
  168. this,
  169. new SessionEventArgs
  170. {
  171. SessionInfo = info
  172. },
  173. _logger);
  174. _eventManager.Publish(new SessionEndedEventArgs(info));
  175. info.Dispose();
  176. }
  177. /// <inheritdoc />
  178. public void UpdateDeviceName(string sessionId, string reportedDeviceName)
  179. {
  180. var session = GetSession(sessionId);
  181. if (session is not null)
  182. {
  183. session.DeviceName = reportedDeviceName;
  184. }
  185. }
  186. /// <summary>
  187. /// Logs the user activity.
  188. /// </summary>
  189. /// <param name="appName">Type of the client.</param>
  190. /// <param name="appVersion">The app version.</param>
  191. /// <param name="deviceId">The device id.</param>
  192. /// <param name="deviceName">Name of the device.</param>
  193. /// <param name="remoteEndPoint">The remote end point.</param>
  194. /// <param name="user">The user.</param>
  195. /// <returns>SessionInfo.</returns>
  196. public async Task<SessionInfo> LogSessionActivity(
  197. string appName,
  198. string appVersion,
  199. string deviceId,
  200. string deviceName,
  201. string remoteEndPoint,
  202. User user)
  203. {
  204. CheckDisposed();
  205. ArgumentException.ThrowIfNullOrEmpty(appName);
  206. ArgumentException.ThrowIfNullOrEmpty(appVersion);
  207. ArgumentException.ThrowIfNullOrEmpty(deviceId);
  208. var activityDate = DateTime.UtcNow;
  209. var session = await GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
  210. var lastActivityDate = session.LastActivityDate;
  211. session.LastActivityDate = activityDate;
  212. if (user is not null)
  213. {
  214. var userLastActivityDate = user.LastActivityDate ?? DateTime.MinValue;
  215. if ((activityDate - userLastActivityDate).TotalSeconds > 60)
  216. {
  217. try
  218. {
  219. user.LastActivityDate = activityDate;
  220. await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
  221. }
  222. catch (DbUpdateConcurrencyException e)
  223. {
  224. _logger.LogDebug(e, "Error updating user's last activity date.");
  225. }
  226. }
  227. }
  228. if ((activityDate - lastActivityDate).TotalSeconds > 10)
  229. {
  230. SessionActivity?.Invoke(
  231. this,
  232. new SessionEventArgs
  233. {
  234. SessionInfo = session
  235. });
  236. }
  237. return session;
  238. }
  239. /// <inheritdoc />
  240. public void OnSessionControllerConnected(SessionInfo session)
  241. {
  242. EventHelper.QueueEventIfNotNull(
  243. SessionControllerConnected,
  244. this,
  245. new SessionEventArgs
  246. {
  247. SessionInfo = session
  248. },
  249. _logger);
  250. }
  251. /// <inheritdoc />
  252. public async Task CloseIfNeededAsync(SessionInfo session)
  253. {
  254. if (!session.SessionControllers.Any(i => i.IsSessionActive))
  255. {
  256. var key = GetSessionKey(session.Client, session.DeviceId);
  257. _activeConnections.TryRemove(key, out _);
  258. if (!string.IsNullOrEmpty(session.PlayState?.LiveStreamId))
  259. {
  260. await _mediaSourceManager.CloseLiveStream(session.PlayState.LiveStreamId).ConfigureAwait(false);
  261. }
  262. OnSessionEnded(session);
  263. }
  264. }
  265. /// <inheritdoc />
  266. public void ReportSessionEnded(string sessionId)
  267. {
  268. CheckDisposed();
  269. var session = GetSession(sessionId, false);
  270. if (session is not null)
  271. {
  272. var key = GetSessionKey(session.Client, session.DeviceId);
  273. _activeConnections.TryRemove(key, out _);
  274. OnSessionEnded(session);
  275. }
  276. }
  277. private Task<MediaSourceInfo> GetMediaSource(BaseItem item, string mediaSourceId, string liveStreamId)
  278. {
  279. return _mediaSourceManager.GetMediaSource(item, mediaSourceId, liveStreamId, false, CancellationToken.None);
  280. }
  281. /// <summary>
  282. /// Updates the now playing item id.
  283. /// </summary>
  284. /// <returns>Task.</returns>
  285. private async Task UpdateNowPlayingItem(SessionInfo session, PlaybackProgressInfo info, BaseItem libraryItem, bool updateLastCheckInTime)
  286. {
  287. if (string.IsNullOrEmpty(info.MediaSourceId))
  288. {
  289. info.MediaSourceId = info.ItemId.ToString("N", CultureInfo.InvariantCulture);
  290. }
  291. if (!info.ItemId.Equals(default) && info.Item is null && libraryItem is not null)
  292. {
  293. var current = session.NowPlayingItem;
  294. if (current is null || !info.ItemId.Equals(current.Id))
  295. {
  296. var runtimeTicks = libraryItem.RunTimeTicks;
  297. MediaSourceInfo mediaSource = null;
  298. if (libraryItem is IHasMediaSources)
  299. {
  300. mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId, info.LiveStreamId).ConfigureAwait(false);
  301. if (mediaSource is not null)
  302. {
  303. runtimeTicks = mediaSource.RunTimeTicks;
  304. }
  305. }
  306. info.Item = GetItemInfo(libraryItem, mediaSource);
  307. info.Item.RunTimeTicks = runtimeTicks;
  308. }
  309. else
  310. {
  311. info.Item = current;
  312. }
  313. }
  314. session.NowPlayingItem = info.Item;
  315. session.LastActivityDate = DateTime.UtcNow;
  316. if (updateLastCheckInTime)
  317. {
  318. session.LastPlaybackCheckIn = DateTime.UtcNow;
  319. }
  320. session.PlayState.IsPaused = info.IsPaused;
  321. session.PlayState.PositionTicks = info.PositionTicks;
  322. session.PlayState.MediaSourceId = info.MediaSourceId;
  323. session.PlayState.LiveStreamId = info.LiveStreamId;
  324. session.PlayState.CanSeek = info.CanSeek;
  325. session.PlayState.IsMuted = info.IsMuted;
  326. session.PlayState.VolumeLevel = info.VolumeLevel;
  327. session.PlayState.AudioStreamIndex = info.AudioStreamIndex;
  328. session.PlayState.SubtitleStreamIndex = info.SubtitleStreamIndex;
  329. session.PlayState.PlayMethod = info.PlayMethod;
  330. session.PlayState.RepeatMode = info.RepeatMode;
  331. session.PlaylistItemId = info.PlaylistItemId;
  332. var nowPlayingQueue = info.NowPlayingQueue;
  333. if (nowPlayingQueue?.Length > 0)
  334. {
  335. session.NowPlayingQueue = nowPlayingQueue;
  336. var itemIds = nowPlayingQueue.Select(queue => queue.Id).ToArray();
  337. session.NowPlayingQueueFullItems = _dtoService.GetBaseItemDtos(
  338. _libraryManager.GetItemList(new InternalItemsQuery { ItemIds = itemIds }),
  339. new DtoOptions(true));
  340. }
  341. }
  342. /// <summary>
  343. /// Removes the now playing item id.
  344. /// </summary>
  345. /// <param name="session">The session.</param>
  346. private void RemoveNowPlayingItem(SessionInfo session)
  347. {
  348. session.NowPlayingItem = null;
  349. session.PlayState = new PlayerStateInfo();
  350. if (!string.IsNullOrEmpty(session.DeviceId))
  351. {
  352. ClearTranscodingInfo(session.DeviceId);
  353. }
  354. }
  355. private static string GetSessionKey(string appName, string deviceId)
  356. => appName + deviceId;
  357. /// <summary>
  358. /// Gets the connection.
  359. /// </summary>
  360. /// <param name="appName">Type of the client.</param>
  361. /// <param name="appVersion">The app version.</param>
  362. /// <param name="deviceId">The device id.</param>
  363. /// <param name="deviceName">Name of the device.</param>
  364. /// <param name="remoteEndPoint">The remote end point.</param>
  365. /// <param name="user">The user.</param>
  366. /// <returns>SessionInfo.</returns>
  367. private async Task<SessionInfo> GetSessionInfo(
  368. string appName,
  369. string appVersion,
  370. string deviceId,
  371. string deviceName,
  372. string remoteEndPoint,
  373. User user)
  374. {
  375. CheckDisposed();
  376. ArgumentException.ThrowIfNullOrEmpty(deviceId);
  377. var key = GetSessionKey(appName, deviceId);
  378. CheckDisposed();
  379. if (!_activeConnections.TryGetValue(key, out var sessionInfo))
  380. {
  381. _activeConnections[key] = await CreateSession(key, appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false);
  382. sessionInfo = _activeConnections[key];
  383. }
  384. sessionInfo.UserId = user?.Id ?? Guid.Empty;
  385. sessionInfo.UserName = user?.Username;
  386. sessionInfo.UserPrimaryImageTag = user?.ProfileImage is null ? null : GetImageCacheTag(user);
  387. sessionInfo.RemoteEndPoint = remoteEndPoint;
  388. sessionInfo.Client = appName;
  389. if (!sessionInfo.HasCustomDeviceName || string.IsNullOrEmpty(sessionInfo.DeviceName))
  390. {
  391. sessionInfo.DeviceName = deviceName;
  392. }
  393. sessionInfo.ApplicationVersion = appVersion;
  394. if (user is null)
  395. {
  396. sessionInfo.AdditionalUsers = Array.Empty<SessionUserInfo>();
  397. }
  398. return sessionInfo;
  399. }
  400. private async Task<SessionInfo> CreateSession(
  401. string key,
  402. string appName,
  403. string appVersion,
  404. string deviceId,
  405. string deviceName,
  406. string remoteEndPoint,
  407. User user)
  408. {
  409. var sessionInfo = new SessionInfo(this, _logger)
  410. {
  411. Client = appName,
  412. DeviceId = deviceId,
  413. ApplicationVersion = appVersion,
  414. Id = key.GetMD5().ToString("N", CultureInfo.InvariantCulture),
  415. ServerId = _appHost.SystemId
  416. };
  417. var username = user?.Username;
  418. sessionInfo.UserId = user?.Id ?? Guid.Empty;
  419. sessionInfo.UserName = username;
  420. sessionInfo.UserPrimaryImageTag = user?.ProfileImage is null ? null : GetImageCacheTag(user);
  421. sessionInfo.RemoteEndPoint = remoteEndPoint;
  422. if (string.IsNullOrEmpty(deviceName))
  423. {
  424. deviceName = "Network Device";
  425. }
  426. var deviceOptions = await _deviceManager.GetDeviceOptions(deviceId).ConfigureAwait(false);
  427. if (string.IsNullOrEmpty(deviceOptions.CustomName))
  428. {
  429. sessionInfo.DeviceName = deviceName;
  430. }
  431. else
  432. {
  433. sessionInfo.DeviceName = deviceOptions.CustomName;
  434. sessionInfo.HasCustomDeviceName = true;
  435. }
  436. OnSessionStarted(sessionInfo);
  437. return sessionInfo;
  438. }
  439. private List<User> GetUsers(SessionInfo session)
  440. {
  441. var users = new List<User>();
  442. if (session.UserId.Equals(default))
  443. {
  444. return users;
  445. }
  446. var user = _userManager.GetUserById(session.UserId);
  447. if (user is null)
  448. {
  449. throw new InvalidOperationException("User not found");
  450. }
  451. users.Add(user);
  452. users.AddRange(session.AdditionalUsers
  453. .Select(i => _userManager.GetUserById(i.UserId))
  454. .Where(i => i is not null));
  455. return users;
  456. }
  457. private void StartIdleCheckTimer()
  458. {
  459. _idleTimer ??= new Timer(CheckForIdlePlayback, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
  460. }
  461. private void StopIdleCheckTimer()
  462. {
  463. if (_idleTimer is not null)
  464. {
  465. _idleTimer.Dispose();
  466. _idleTimer = null;
  467. }
  468. }
  469. private async void CheckForIdlePlayback(object state)
  470. {
  471. var playingSessions = Sessions.Where(i => i.NowPlayingItem is not null)
  472. .ToList();
  473. if (playingSessions.Count > 0)
  474. {
  475. var idle = playingSessions
  476. .Where(i => (DateTime.UtcNow - i.LastPlaybackCheckIn).TotalMinutes > 5)
  477. .ToList();
  478. foreach (var session in idle)
  479. {
  480. _logger.LogDebug("Session {0} has gone idle while playing", session.Id);
  481. try
  482. {
  483. await OnPlaybackStopped(new PlaybackStopInfo
  484. {
  485. Item = session.NowPlayingItem,
  486. ItemId = session.NowPlayingItem is null ? Guid.Empty : session.NowPlayingItem.Id,
  487. SessionId = session.Id,
  488. MediaSourceId = session.PlayState?.MediaSourceId,
  489. PositionTicks = session.PlayState?.PositionTicks
  490. }).ConfigureAwait(false);
  491. }
  492. catch (Exception ex)
  493. {
  494. _logger.LogDebug(ex, "Error calling OnPlaybackStopped");
  495. }
  496. }
  497. playingSessions = Sessions.Where(i => i.NowPlayingItem is not null)
  498. .ToList();
  499. }
  500. if (playingSessions.Count == 0)
  501. {
  502. StopIdleCheckTimer();
  503. }
  504. }
  505. private BaseItem GetNowPlayingItem(SessionInfo session, Guid itemId)
  506. {
  507. var item = session.FullNowPlayingItem;
  508. if (item is not null && item.Id.Equals(itemId))
  509. {
  510. return item;
  511. }
  512. item = _libraryManager.GetItemById(itemId);
  513. session.FullNowPlayingItem = item;
  514. return item;
  515. }
  516. /// <summary>
  517. /// Used to report that playback has started for an item.
  518. /// </summary>
  519. /// <param name="info">The info.</param>
  520. /// <returns>Task.</returns>
  521. /// <exception cref="ArgumentNullException"><c>info</c> is <c>null</c>.</exception>
  522. public async Task OnPlaybackStart(PlaybackStartInfo info)
  523. {
  524. CheckDisposed();
  525. ArgumentNullException.ThrowIfNull(info);
  526. var session = GetSession(info.SessionId);
  527. var libraryItem = info.ItemId.Equals(default)
  528. ? null
  529. : GetNowPlayingItem(session, info.ItemId);
  530. await UpdateNowPlayingItem(session, info, libraryItem, true).ConfigureAwait(false);
  531. if (!string.IsNullOrEmpty(session.DeviceId) && info.PlayMethod != PlayMethod.Transcode)
  532. {
  533. ClearTranscodingInfo(session.DeviceId);
  534. }
  535. session.StartAutomaticProgress(info);
  536. var users = GetUsers(session);
  537. if (libraryItem is not null)
  538. {
  539. foreach (var user in users)
  540. {
  541. OnPlaybackStart(user, libraryItem);
  542. }
  543. }
  544. var eventArgs = new PlaybackStartEventArgs
  545. {
  546. Item = libraryItem,
  547. Users = users,
  548. MediaSourceId = info.MediaSourceId,
  549. MediaInfo = info.Item,
  550. DeviceName = session.DeviceName,
  551. ClientName = session.Client,
  552. DeviceId = session.DeviceId,
  553. Session = session,
  554. PlaybackPositionTicks = info.PositionTicks,
  555. PlaySessionId = info.PlaySessionId
  556. };
  557. await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
  558. // Nothing to save here
  559. // Fire events to inform plugins
  560. EventHelper.QueueEventIfNotNull(
  561. PlaybackStart,
  562. this,
  563. eventArgs,
  564. _logger);
  565. StartIdleCheckTimer();
  566. }
  567. /// <summary>
  568. /// Called when [playback start].
  569. /// </summary>
  570. /// <param name="user">The user object.</param>
  571. /// <param name="item">The item.</param>
  572. private void OnPlaybackStart(User user, BaseItem item)
  573. {
  574. var data = _userDataManager.GetUserData(user, item);
  575. data.PlayCount++;
  576. data.LastPlayedDate = DateTime.UtcNow;
  577. if (item.SupportsPlayedStatus && !item.SupportsPositionTicksResume)
  578. {
  579. data.Played = true;
  580. }
  581. else
  582. {
  583. data.Played = false;
  584. }
  585. _userDataManager.SaveUserData(user, item, data, UserDataSaveReason.PlaybackStart, CancellationToken.None);
  586. }
  587. /// <inheritdoc />
  588. public Task OnPlaybackProgress(PlaybackProgressInfo info)
  589. {
  590. return OnPlaybackProgress(info, false);
  591. }
  592. /// <summary>
  593. /// Used to report playback progress for an item.
  594. /// </summary>
  595. /// <param name="info">The playback progress info.</param>
  596. /// <param name="isAutomated">Whether this is an automated update.</param>
  597. /// <returns>Task.</returns>
  598. public async Task OnPlaybackProgress(PlaybackProgressInfo info, bool isAutomated)
  599. {
  600. CheckDisposed();
  601. ArgumentNullException.ThrowIfNull(info);
  602. var session = GetSession(info.SessionId);
  603. var libraryItem = info.ItemId.Equals(default)
  604. ? null
  605. : GetNowPlayingItem(session, info.ItemId);
  606. await UpdateNowPlayingItem(session, info, libraryItem, !isAutomated).ConfigureAwait(false);
  607. if (!string.IsNullOrEmpty(session.DeviceId) && info.PlayMethod != PlayMethod.Transcode)
  608. {
  609. ClearTranscodingInfo(session.DeviceId);
  610. }
  611. var users = GetUsers(session);
  612. // only update saved user data on actual check-ins, not automated ones
  613. if (libraryItem is not null && !isAutomated)
  614. {
  615. foreach (var user in users)
  616. {
  617. OnPlaybackProgress(user, libraryItem, info);
  618. }
  619. }
  620. var eventArgs = new PlaybackProgressEventArgs
  621. {
  622. Item = libraryItem,
  623. Users = users,
  624. PlaybackPositionTicks = session.PlayState.PositionTicks,
  625. MediaSourceId = session.PlayState.MediaSourceId,
  626. MediaInfo = info.Item,
  627. DeviceName = session.DeviceName,
  628. ClientName = session.Client,
  629. DeviceId = session.DeviceId,
  630. IsPaused = info.IsPaused,
  631. PlaySessionId = info.PlaySessionId,
  632. IsAutomated = isAutomated,
  633. Session = session
  634. };
  635. await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
  636. PlaybackProgress?.Invoke(this, eventArgs);
  637. if (!isAutomated)
  638. {
  639. session.StartAutomaticProgress(info);
  640. }
  641. StartIdleCheckTimer();
  642. }
  643. private void OnPlaybackProgress(User user, BaseItem item, PlaybackProgressInfo info)
  644. {
  645. var data = _userDataManager.GetUserData(user, item);
  646. var positionTicks = info.PositionTicks;
  647. var changed = false;
  648. if (positionTicks.HasValue)
  649. {
  650. _userDataManager.UpdatePlayState(item, data, positionTicks.Value);
  651. changed = true;
  652. }
  653. var tracksChanged = UpdatePlaybackSettings(user, info, data);
  654. if (!tracksChanged)
  655. {
  656. changed = true;
  657. }
  658. if (changed)
  659. {
  660. _userDataManager.SaveUserData(user, item, data, UserDataSaveReason.PlaybackProgress, CancellationToken.None);
  661. }
  662. }
  663. private static bool UpdatePlaybackSettings(User user, PlaybackProgressInfo info, UserItemData data)
  664. {
  665. var changed = false;
  666. if (user.RememberAudioSelections)
  667. {
  668. if (data.AudioStreamIndex != info.AudioStreamIndex)
  669. {
  670. data.AudioStreamIndex = info.AudioStreamIndex;
  671. changed = true;
  672. }
  673. }
  674. else
  675. {
  676. if (data.AudioStreamIndex.HasValue)
  677. {
  678. data.AudioStreamIndex = null;
  679. changed = true;
  680. }
  681. }
  682. if (user.RememberSubtitleSelections)
  683. {
  684. if (data.SubtitleStreamIndex != info.SubtitleStreamIndex)
  685. {
  686. data.SubtitleStreamIndex = info.SubtitleStreamIndex;
  687. changed = true;
  688. }
  689. }
  690. else
  691. {
  692. if (data.SubtitleStreamIndex.HasValue)
  693. {
  694. data.SubtitleStreamIndex = null;
  695. changed = true;
  696. }
  697. }
  698. return changed;
  699. }
  700. /// <summary>
  701. /// Used to report that playback has ended for an item.
  702. /// </summary>
  703. /// <param name="info">The info.</param>
  704. /// <returns>Task.</returns>
  705. /// <exception cref="ArgumentNullException"><c>info</c> is <c>null</c>.</exception>
  706. /// <exception cref="ArgumentOutOfRangeException"><c>info.PositionTicks</c> is <c>null</c> or negative.</exception>
  707. public async Task OnPlaybackStopped(PlaybackStopInfo info)
  708. {
  709. CheckDisposed();
  710. ArgumentNullException.ThrowIfNull(info);
  711. if (info.PositionTicks.HasValue && info.PositionTicks.Value < 0)
  712. {
  713. throw new ArgumentOutOfRangeException(nameof(info), "The PlaybackStopInfo's PositionTicks was negative.");
  714. }
  715. var session = GetSession(info.SessionId);
  716. session.StopAutomaticProgress();
  717. var libraryItem = info.ItemId.Equals(default)
  718. ? null
  719. : GetNowPlayingItem(session, info.ItemId);
  720. // Normalize
  721. if (string.IsNullOrEmpty(info.MediaSourceId))
  722. {
  723. info.MediaSourceId = info.ItemId.ToString("N", CultureInfo.InvariantCulture);
  724. }
  725. if (!info.ItemId.Equals(default) && info.Item is null && libraryItem is not null)
  726. {
  727. var current = session.NowPlayingItem;
  728. if (current is null || !info.ItemId.Equals(current.Id))
  729. {
  730. MediaSourceInfo mediaSource = null;
  731. if (libraryItem is IHasMediaSources)
  732. {
  733. mediaSource = await GetMediaSource(libraryItem, info.MediaSourceId, info.LiveStreamId).ConfigureAwait(false);
  734. }
  735. info.Item = GetItemInfo(libraryItem, mediaSource);
  736. }
  737. else
  738. {
  739. info.Item = current;
  740. }
  741. }
  742. if (info.Item is not null)
  743. {
  744. var msString = info.PositionTicks.HasValue ? (info.PositionTicks.Value / 10000).ToString(CultureInfo.InvariantCulture) : "unknown";
  745. _logger.LogInformation(
  746. "Playback stopped reported by app {0} {1} playing {2}. Stopped at {3} ms",
  747. session.Client,
  748. session.ApplicationVersion,
  749. info.Item.Name,
  750. msString);
  751. }
  752. if (info.NowPlayingQueue is not null)
  753. {
  754. session.NowPlayingQueue = info.NowPlayingQueue;
  755. }
  756. session.PlaylistItemId = info.PlaylistItemId;
  757. RemoveNowPlayingItem(session);
  758. var users = GetUsers(session);
  759. var playedToCompletion = false;
  760. if (libraryItem is not null)
  761. {
  762. foreach (var user in users)
  763. {
  764. playedToCompletion = OnPlaybackStopped(user, libraryItem, info.PositionTicks, info.Failed);
  765. }
  766. }
  767. if (!string.IsNullOrEmpty(info.LiveStreamId))
  768. {
  769. try
  770. {
  771. await _mediaSourceManager.CloseLiveStream(info.LiveStreamId).ConfigureAwait(false);
  772. }
  773. catch (Exception ex)
  774. {
  775. _logger.LogError(ex, "Error closing live stream");
  776. }
  777. }
  778. var eventArgs = new PlaybackStopEventArgs
  779. {
  780. Item = libraryItem,
  781. Users = users,
  782. PlaybackPositionTicks = info.PositionTicks,
  783. PlayedToCompletion = playedToCompletion,
  784. MediaSourceId = info.MediaSourceId,
  785. MediaInfo = info.Item,
  786. DeviceName = session.DeviceName,
  787. ClientName = session.Client,
  788. DeviceId = session.DeviceId,
  789. Session = session,
  790. PlaySessionId = info.PlaySessionId
  791. };
  792. await _eventManager.PublishAsync(eventArgs).ConfigureAwait(false);
  793. EventHelper.QueueEventIfNotNull(PlaybackStopped, this, eventArgs, _logger);
  794. }
  795. private bool OnPlaybackStopped(User user, BaseItem item, long? positionTicks, bool playbackFailed)
  796. {
  797. if (playbackFailed)
  798. {
  799. return false;
  800. }
  801. var data = _userDataManager.GetUserData(user, item);
  802. bool playedToCompletion;
  803. if (positionTicks.HasValue)
  804. {
  805. playedToCompletion = _userDataManager.UpdatePlayState(item, data, positionTicks.Value);
  806. }
  807. else
  808. {
  809. // If the client isn't able to report this, then we'll just have to make an assumption
  810. data.PlayCount++;
  811. data.Played = item.SupportsPlayedStatus;
  812. data.PlaybackPositionTicks = 0;
  813. playedToCompletion = true;
  814. }
  815. _userDataManager.SaveUserData(user, item, data, UserDataSaveReason.PlaybackFinished, CancellationToken.None);
  816. return playedToCompletion;
  817. }
  818. /// <summary>
  819. /// Gets the session.
  820. /// </summary>
  821. /// <param name="sessionId">The session identifier.</param>
  822. /// <param name="throwOnMissing">if set to <c>true</c> [throw on missing].</param>
  823. /// <returns>SessionInfo.</returns>
  824. /// <exception cref="ResourceNotFoundException">
  825. /// No session with an Id equal to <c>sessionId</c> was found
  826. /// and <c>throwOnMissing</c> is <c>true</c>.
  827. /// </exception>
  828. private SessionInfo GetSession(string sessionId, bool throwOnMissing = true)
  829. {
  830. var session = Sessions.FirstOrDefault(i => string.Equals(i.Id, sessionId, StringComparison.Ordinal));
  831. if (session is null && throwOnMissing)
  832. {
  833. throw new ResourceNotFoundException(
  834. string.Format(CultureInfo.InvariantCulture, "Session {0} not found.", sessionId));
  835. }
  836. return session;
  837. }
  838. private SessionInfo GetSessionToRemoteControl(string sessionId)
  839. {
  840. // Accept either device id or session id
  841. var session = Sessions.FirstOrDefault(i => string.Equals(i.Id, sessionId, StringComparison.Ordinal));
  842. if (session is null)
  843. {
  844. throw new ResourceNotFoundException(
  845. string.Format(CultureInfo.InvariantCulture, "Session {0} not found.", sessionId));
  846. }
  847. return session;
  848. }
  849. /// <inheritdoc />
  850. public Task SendMessageCommand(string controllingSessionId, string sessionId, MessageCommand command, CancellationToken cancellationToken)
  851. {
  852. CheckDisposed();
  853. var generalCommand = new GeneralCommand
  854. {
  855. Name = GeneralCommandType.DisplayMessage
  856. };
  857. generalCommand.Arguments["Header"] = command.Header;
  858. generalCommand.Arguments["Text"] = command.Text;
  859. if (command.TimeoutMs.HasValue)
  860. {
  861. generalCommand.Arguments["TimeoutMs"] = command.TimeoutMs.Value.ToString(CultureInfo.InvariantCulture);
  862. }
  863. return SendGeneralCommand(controllingSessionId, sessionId, generalCommand, cancellationToken);
  864. }
  865. /// <inheritdoc />
  866. public Task SendGeneralCommand(string controllingSessionId, string sessionId, GeneralCommand command, CancellationToken cancellationToken)
  867. {
  868. CheckDisposed();
  869. var session = GetSessionToRemoteControl(sessionId);
  870. if (!string.IsNullOrEmpty(controllingSessionId))
  871. {
  872. var controllingSession = GetSession(controllingSessionId);
  873. AssertCanControl(session, controllingSession);
  874. }
  875. return SendMessageToSession(session, SessionMessageType.GeneralCommand, command, cancellationToken);
  876. }
  877. private static async Task SendMessageToSession<T>(SessionInfo session, SessionMessageType name, T data, CancellationToken cancellationToken)
  878. {
  879. var controllers = session.SessionControllers;
  880. var messageId = Guid.NewGuid();
  881. foreach (var controller in controllers)
  882. {
  883. await controller.SendMessage(name, messageId, data, cancellationToken).ConfigureAwait(false);
  884. }
  885. }
  886. private static Task SendMessageToSessions<T>(IEnumerable<SessionInfo> sessions, SessionMessageType name, T data, CancellationToken cancellationToken)
  887. {
  888. IEnumerable<Task> GetTasks()
  889. {
  890. var messageId = Guid.NewGuid();
  891. foreach (var session in sessions)
  892. {
  893. var controllers = session.SessionControllers;
  894. foreach (var controller in controllers)
  895. {
  896. yield return controller.SendMessage(name, messageId, data, cancellationToken);
  897. }
  898. }
  899. }
  900. return Task.WhenAll(GetTasks());
  901. }
  902. /// <inheritdoc />
  903. public async Task SendPlayCommand(string controllingSessionId, string sessionId, PlayRequest command, CancellationToken cancellationToken)
  904. {
  905. CheckDisposed();
  906. var session = GetSessionToRemoteControl(sessionId);
  907. var user = session.UserId.Equals(default) ? null : _userManager.GetUserById(session.UserId);
  908. List<BaseItem> items;
  909. if (command.PlayCommand == PlayCommand.PlayInstantMix)
  910. {
  911. items = command.ItemIds.SelectMany(i => TranslateItemForInstantMix(i, user))
  912. .ToList();
  913. command.PlayCommand = PlayCommand.PlayNow;
  914. }
  915. else
  916. {
  917. var list = new List<BaseItem>();
  918. foreach (var itemId in command.ItemIds)
  919. {
  920. var subItems = TranslateItemForPlayback(itemId, user);
  921. list.AddRange(subItems);
  922. }
  923. items = list;
  924. }
  925. if (command.PlayCommand == PlayCommand.PlayShuffle)
  926. {
  927. items.Shuffle();
  928. command.PlayCommand = PlayCommand.PlayNow;
  929. }
  930. command.ItemIds = items.Select(i => i.Id).ToArray();
  931. if (user is not null)
  932. {
  933. if (items.Any(i => i.GetPlayAccess(user) != PlayAccess.Full))
  934. {
  935. throw new ArgumentException(
  936. string.Format(CultureInfo.InvariantCulture, "{0} is not allowed to play media.", user.Username));
  937. }
  938. }
  939. if (user is not null
  940. && command.ItemIds.Length == 1
  941. && user.EnableNextEpisodeAutoPlay
  942. && _libraryManager.GetItemById(command.ItemIds[0]) is Episode episode)
  943. {
  944. var series = episode.Series;
  945. if (series is not null)
  946. {
  947. var episodes = series.GetEpisodes(
  948. user,
  949. new DtoOptions(false)
  950. {
  951. EnableImages = false
  952. })
  953. .Where(i => !i.IsVirtualItem)
  954. .SkipWhile(i => !i.Id.Equals(episode.Id))
  955. .ToList();
  956. if (episodes.Count > 0)
  957. {
  958. command.ItemIds = episodes.Select(i => i.Id).ToArray();
  959. }
  960. }
  961. }
  962. if (!string.IsNullOrEmpty(controllingSessionId))
  963. {
  964. var controllingSession = GetSession(controllingSessionId);
  965. AssertCanControl(session, controllingSession);
  966. if (!controllingSession.UserId.Equals(default))
  967. {
  968. command.ControllingUserId = controllingSession.UserId;
  969. }
  970. }
  971. await SendMessageToSession(session, SessionMessageType.Play, command, cancellationToken).ConfigureAwait(false);
  972. }
  973. /// <inheritdoc />
  974. public async Task SendSyncPlayCommand(string sessionId, SendCommand command, CancellationToken cancellationToken)
  975. {
  976. CheckDisposed();
  977. var session = GetSession(sessionId);
  978. await SendMessageToSession(session, SessionMessageType.SyncPlayCommand, command, cancellationToken).ConfigureAwait(false);
  979. }
  980. /// <inheritdoc />
  981. public async Task SendSyncPlayGroupUpdate<T>(string sessionId, GroupUpdate<T> command, CancellationToken cancellationToken)
  982. {
  983. CheckDisposed();
  984. var session = GetSession(sessionId);
  985. await SendMessageToSession(session, SessionMessageType.SyncPlayGroupUpdate, command, cancellationToken).ConfigureAwait(false);
  986. }
  987. private IEnumerable<BaseItem> TranslateItemForPlayback(Guid id, User user)
  988. {
  989. var item = _libraryManager.GetItemById(id);
  990. if (item is null)
  991. {
  992. _logger.LogError("A non-existent item Id {0} was passed into TranslateItemForPlayback", id);
  993. return Array.Empty<BaseItem>();
  994. }
  995. if (item is IItemByName byName)
  996. {
  997. return byName.GetTaggedItems(new InternalItemsQuery(user)
  998. {
  999. IsFolder = false,
  1000. Recursive = true,
  1001. DtoOptions = new DtoOptions(false)
  1002. {
  1003. EnableImages = false,
  1004. Fields = new[]
  1005. {
  1006. ItemFields.SortName
  1007. }
  1008. },
  1009. IsVirtualItem = false,
  1010. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }
  1011. });
  1012. }
  1013. if (item.IsFolder)
  1014. {
  1015. var folder = (Folder)item;
  1016. return folder.GetItemList(new InternalItemsQuery(user)
  1017. {
  1018. Recursive = true,
  1019. IsFolder = false,
  1020. DtoOptions = new DtoOptions(false)
  1021. {
  1022. EnableImages = false,
  1023. Fields = new ItemFields[]
  1024. {
  1025. ItemFields.SortName
  1026. }
  1027. },
  1028. IsVirtualItem = false,
  1029. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) }
  1030. });
  1031. }
  1032. return new[] { item };
  1033. }
  1034. private IEnumerable<BaseItem> TranslateItemForInstantMix(Guid id, User user)
  1035. {
  1036. var item = _libraryManager.GetItemById(id);
  1037. if (item is null)
  1038. {
  1039. _logger.LogError("A non-existent item Id {0} was passed into TranslateItemForInstantMix", id);
  1040. return new List<BaseItem>();
  1041. }
  1042. return _musicManager.GetInstantMixFromItem(item, user, new DtoOptions(false) { EnableImages = false });
  1043. }
  1044. /// <inheritdoc />
  1045. public Task SendBrowseCommand(string controllingSessionId, string sessionId, BrowseRequest command, CancellationToken cancellationToken)
  1046. {
  1047. var generalCommand = new GeneralCommand
  1048. {
  1049. Name = GeneralCommandType.DisplayContent,
  1050. Arguments =
  1051. {
  1052. ["ItemId"] = command.ItemId,
  1053. ["ItemName"] = command.ItemName,
  1054. ["ItemType"] = command.ItemType.ToString()
  1055. }
  1056. };
  1057. return SendGeneralCommand(controllingSessionId, sessionId, generalCommand, cancellationToken);
  1058. }
  1059. /// <inheritdoc />
  1060. public Task SendPlaystateCommand(string controllingSessionId, string sessionId, PlaystateRequest command, CancellationToken cancellationToken)
  1061. {
  1062. CheckDisposed();
  1063. var session = GetSessionToRemoteControl(sessionId);
  1064. if (!string.IsNullOrEmpty(controllingSessionId))
  1065. {
  1066. var controllingSession = GetSession(controllingSessionId);
  1067. AssertCanControl(session, controllingSession);
  1068. if (!controllingSession.UserId.Equals(default))
  1069. {
  1070. command.ControllingUserId = controllingSession.UserId.ToString("N", CultureInfo.InvariantCulture);
  1071. }
  1072. }
  1073. return SendMessageToSession(session, SessionMessageType.Playstate, command, cancellationToken);
  1074. }
  1075. private static void AssertCanControl(SessionInfo session, SessionInfo controllingSession)
  1076. {
  1077. ArgumentNullException.ThrowIfNull(session);
  1078. ArgumentNullException.ThrowIfNull(controllingSession);
  1079. }
  1080. /// <summary>
  1081. /// Sends the restart required message.
  1082. /// </summary>
  1083. /// <param name="cancellationToken">The cancellation token.</param>
  1084. /// <returns>Task.</returns>
  1085. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  1086. {
  1087. CheckDisposed();
  1088. return SendMessageToSessions(Sessions, SessionMessageType.RestartRequired, string.Empty, cancellationToken);
  1089. }
  1090. /// <summary>
  1091. /// Adds the additional user.
  1092. /// </summary>
  1093. /// <param name="sessionId">The session identifier.</param>
  1094. /// <param name="userId">The user identifier.</param>
  1095. /// <exception cref="UnauthorizedAccessException">Cannot modify additional users without authenticating first.</exception>
  1096. /// <exception cref="ArgumentException">The requested user is already the primary user of the session.</exception>
  1097. public void AddAdditionalUser(string sessionId, Guid userId)
  1098. {
  1099. CheckDisposed();
  1100. var session = GetSession(sessionId);
  1101. if (session.UserId.Equals(userId))
  1102. {
  1103. throw new ArgumentException("The requested user is already the primary user of the session.");
  1104. }
  1105. if (session.AdditionalUsers.All(i => !i.UserId.Equals(userId)))
  1106. {
  1107. var user = _userManager.GetUserById(userId);
  1108. var list = session.AdditionalUsers.ToList();
  1109. list.Add(new SessionUserInfo
  1110. {
  1111. UserId = userId,
  1112. UserName = user.Username
  1113. });
  1114. session.AdditionalUsers = list.ToArray();
  1115. }
  1116. }
  1117. /// <summary>
  1118. /// Removes the additional user.
  1119. /// </summary>
  1120. /// <param name="sessionId">The session identifier.</param>
  1121. /// <param name="userId">The user identifier.</param>
  1122. /// <exception cref="UnauthorizedAccessException">Cannot modify additional users without authenticating first.</exception>
  1123. /// <exception cref="ArgumentException">The requested user is already the primary user of the session.</exception>
  1124. public void RemoveAdditionalUser(string sessionId, Guid userId)
  1125. {
  1126. CheckDisposed();
  1127. var session = GetSession(sessionId);
  1128. if (session.UserId.Equals(userId))
  1129. {
  1130. throw new ArgumentException("The requested user is already the primary user of the session.");
  1131. }
  1132. var user = session.AdditionalUsers.FirstOrDefault(i => i.UserId.Equals(userId));
  1133. if (user is not null)
  1134. {
  1135. var list = session.AdditionalUsers.ToList();
  1136. list.Remove(user);
  1137. session.AdditionalUsers = list.ToArray();
  1138. }
  1139. }
  1140. /// <summary>
  1141. /// Authenticates the new session.
  1142. /// </summary>
  1143. /// <param name="request">The authenticationrequest.</param>
  1144. /// <returns>The authentication result.</returns>
  1145. public Task<AuthenticationResult> AuthenticateNewSession(AuthenticationRequest request)
  1146. {
  1147. return AuthenticateNewSessionInternal(request, true);
  1148. }
  1149. /// <summary>
  1150. /// Directly authenticates the session without enforcing password.
  1151. /// </summary>
  1152. /// <param name="request">The authentication request.</param>
  1153. /// <returns>The authentication result.</returns>
  1154. public Task<AuthenticationResult> AuthenticateDirect(AuthenticationRequest request)
  1155. {
  1156. return AuthenticateNewSessionInternal(request, false);
  1157. }
  1158. private async Task<AuthenticationResult> AuthenticateNewSessionInternal(AuthenticationRequest request, bool enforcePassword)
  1159. {
  1160. CheckDisposed();
  1161. User user = null;
  1162. if (!request.UserId.Equals(default))
  1163. {
  1164. user = _userManager.GetUserById(request.UserId);
  1165. }
  1166. user ??= _userManager.GetUserByName(request.Username);
  1167. if (enforcePassword)
  1168. {
  1169. user = await _userManager.AuthenticateUser(
  1170. request.Username,
  1171. request.Password,
  1172. null,
  1173. request.RemoteEndPoint,
  1174. true).ConfigureAwait(false);
  1175. }
  1176. if (user is null)
  1177. {
  1178. await _eventManager.PublishAsync(new AuthenticationRequestEventArgs(request)).ConfigureAwait(false);
  1179. throw new AuthenticationException("Invalid username or password entered.");
  1180. }
  1181. if (!string.IsNullOrEmpty(request.DeviceId)
  1182. && !_deviceManager.CanAccessDevice(user, request.DeviceId))
  1183. {
  1184. throw new SecurityException("User is not allowed access from this device.");
  1185. }
  1186. int sessionsCount = Sessions.Count(i => i.UserId.Equals(user.Id));
  1187. int maxActiveSessions = user.MaxActiveSessions;
  1188. _logger.LogInformation("Current/Max sessions for user {User}: {Sessions}/{Max}", user.Username, sessionsCount, maxActiveSessions);
  1189. if (maxActiveSessions >= 1 && sessionsCount >= maxActiveSessions)
  1190. {
  1191. throw new SecurityException("User is at their maximum number of sessions.");
  1192. }
  1193. var token = await GetAuthorizationToken(user, request.DeviceId, request.App, request.AppVersion, request.DeviceName).ConfigureAwait(false);
  1194. var session = await LogSessionActivity(
  1195. request.App,
  1196. request.AppVersion,
  1197. request.DeviceId,
  1198. request.DeviceName,
  1199. request.RemoteEndPoint,
  1200. user).ConfigureAwait(false);
  1201. var returnResult = new AuthenticationResult
  1202. {
  1203. User = _userManager.GetUserDto(user, request.RemoteEndPoint),
  1204. SessionInfo = session,
  1205. AccessToken = token,
  1206. ServerId = _appHost.SystemId
  1207. };
  1208. await _eventManager.PublishAsync(new AuthenticationResultEventArgs(returnResult)).ConfigureAwait(false);
  1209. return returnResult;
  1210. }
  1211. private async Task<string> GetAuthorizationToken(User user, string deviceId, string app, string appVersion, string deviceName)
  1212. {
  1213. var existing = (await _deviceManager.GetDevices(
  1214. new DeviceQuery
  1215. {
  1216. DeviceId = deviceId,
  1217. UserId = user.Id
  1218. }).ConfigureAwait(false)).Items;
  1219. foreach (var auth in existing)
  1220. {
  1221. try
  1222. {
  1223. // Logout any existing sessions for the user on this device
  1224. await Logout(auth).ConfigureAwait(false);
  1225. }
  1226. catch (Exception ex)
  1227. {
  1228. _logger.LogError(ex, "Error while logging out existing session.");
  1229. }
  1230. }
  1231. _logger.LogInformation("Creating new access token for user {0}", user.Id);
  1232. var device = await _deviceManager.CreateDevice(new Device(user.Id, app, appVersion, deviceName, deviceId)).ConfigureAwait(false);
  1233. return device.AccessToken;
  1234. }
  1235. /// <inheritdoc />
  1236. public async Task Logout(string accessToken)
  1237. {
  1238. CheckDisposed();
  1239. ArgumentException.ThrowIfNullOrEmpty(accessToken);
  1240. var existing = (await _deviceManager.GetDevices(
  1241. new DeviceQuery
  1242. {
  1243. Limit = 1,
  1244. AccessToken = accessToken
  1245. }).ConfigureAwait(false)).Items;
  1246. if (existing.Count > 0)
  1247. {
  1248. await Logout(existing[0]).ConfigureAwait(false);
  1249. }
  1250. }
  1251. /// <inheritdoc />
  1252. public async Task Logout(Device device)
  1253. {
  1254. CheckDisposed();
  1255. _logger.LogInformation("Logging out access token {0}", device.AccessToken);
  1256. await _deviceManager.DeleteDevice(device).ConfigureAwait(false);
  1257. var sessions = Sessions
  1258. .Where(i => string.Equals(i.DeviceId, device.DeviceId, StringComparison.OrdinalIgnoreCase))
  1259. .ToList();
  1260. foreach (var session in sessions)
  1261. {
  1262. try
  1263. {
  1264. ReportSessionEnded(session.Id);
  1265. }
  1266. catch (Exception ex)
  1267. {
  1268. _logger.LogError(ex, "Error reporting session ended");
  1269. }
  1270. }
  1271. }
  1272. /// <inheritdoc />
  1273. public async Task RevokeUserTokens(Guid userId, string currentAccessToken)
  1274. {
  1275. CheckDisposed();
  1276. var existing = await _deviceManager.GetDevices(new DeviceQuery
  1277. {
  1278. UserId = userId
  1279. }).ConfigureAwait(false);
  1280. foreach (var info in existing.Items)
  1281. {
  1282. if (!string.Equals(currentAccessToken, info.AccessToken, StringComparison.OrdinalIgnoreCase))
  1283. {
  1284. await Logout(info).ConfigureAwait(false);
  1285. }
  1286. }
  1287. }
  1288. /// <summary>
  1289. /// Reports the capabilities.
  1290. /// </summary>
  1291. /// <param name="sessionId">The session identifier.</param>
  1292. /// <param name="capabilities">The capabilities.</param>
  1293. public void ReportCapabilities(string sessionId, ClientCapabilities capabilities)
  1294. {
  1295. CheckDisposed();
  1296. var session = GetSession(sessionId);
  1297. ReportCapabilities(session, capabilities, true);
  1298. }
  1299. private void ReportCapabilities(
  1300. SessionInfo session,
  1301. ClientCapabilities capabilities,
  1302. bool saveCapabilities)
  1303. {
  1304. session.Capabilities = capabilities;
  1305. if (saveCapabilities)
  1306. {
  1307. CapabilitiesChanged?.Invoke(
  1308. this,
  1309. new SessionEventArgs
  1310. {
  1311. SessionInfo = session
  1312. });
  1313. _deviceManager.SaveCapabilities(session.DeviceId, capabilities);
  1314. }
  1315. }
  1316. /// <summary>
  1317. /// Converts a BaseItem to a BaseItemInfo.
  1318. /// </summary>
  1319. private BaseItemDto GetItemInfo(BaseItem item, MediaSourceInfo mediaSource)
  1320. {
  1321. ArgumentNullException.ThrowIfNull(item);
  1322. var dtoOptions = _itemInfoDtoOptions;
  1323. if (_itemInfoDtoOptions is null)
  1324. {
  1325. dtoOptions = new DtoOptions
  1326. {
  1327. AddProgramRecordingInfo = false
  1328. };
  1329. var fields = dtoOptions.Fields.ToList();
  1330. fields.Remove(ItemFields.BasicSyncInfo);
  1331. fields.Remove(ItemFields.CanDelete);
  1332. fields.Remove(ItemFields.CanDownload);
  1333. fields.Remove(ItemFields.ChildCount);
  1334. fields.Remove(ItemFields.CustomRating);
  1335. fields.Remove(ItemFields.DateLastMediaAdded);
  1336. fields.Remove(ItemFields.DateLastRefreshed);
  1337. fields.Remove(ItemFields.DateLastSaved);
  1338. fields.Remove(ItemFields.DisplayPreferencesId);
  1339. fields.Remove(ItemFields.Etag);
  1340. fields.Remove(ItemFields.InheritedParentalRatingValue);
  1341. fields.Remove(ItemFields.ItemCounts);
  1342. fields.Remove(ItemFields.MediaSourceCount);
  1343. fields.Remove(ItemFields.MediaStreams);
  1344. fields.Remove(ItemFields.MediaSources);
  1345. fields.Remove(ItemFields.People);
  1346. fields.Remove(ItemFields.PlayAccess);
  1347. fields.Remove(ItemFields.People);
  1348. fields.Remove(ItemFields.ProductionLocations);
  1349. fields.Remove(ItemFields.RecursiveItemCount);
  1350. fields.Remove(ItemFields.RemoteTrailers);
  1351. fields.Remove(ItemFields.SeasonUserData);
  1352. fields.Remove(ItemFields.Settings);
  1353. fields.Remove(ItemFields.SortName);
  1354. fields.Remove(ItemFields.Tags);
  1355. fields.Remove(ItemFields.ExtraIds);
  1356. dtoOptions.Fields = fields.ToArray();
  1357. _itemInfoDtoOptions = dtoOptions;
  1358. }
  1359. var info = _dtoService.GetBaseItemDto(item, dtoOptions);
  1360. if (mediaSource is not null)
  1361. {
  1362. info.MediaStreams = mediaSource.MediaStreams.ToArray();
  1363. }
  1364. return info;
  1365. }
  1366. private string GetImageCacheTag(User user)
  1367. {
  1368. try
  1369. {
  1370. return _imageProcessor.GetImageCacheTag(user);
  1371. }
  1372. catch (Exception e)
  1373. {
  1374. _logger.LogError(e, "Error getting image information for profile image");
  1375. return null;
  1376. }
  1377. }
  1378. /// <inheritdoc />
  1379. public void ReportNowViewingItem(string sessionId, string itemId)
  1380. {
  1381. ArgumentException.ThrowIfNullOrEmpty(itemId);
  1382. var item = _libraryManager.GetItemById(new Guid(itemId));
  1383. var session = GetSession(sessionId);
  1384. session.NowViewingItem = GetItemInfo(item, null);
  1385. }
  1386. /// <inheritdoc />
  1387. public void ReportTranscodingInfo(string deviceId, TranscodingInfo info)
  1388. {
  1389. var session = Sessions.FirstOrDefault(i =>
  1390. string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase));
  1391. if (session is not null)
  1392. {
  1393. session.TranscodingInfo = info;
  1394. }
  1395. }
  1396. /// <inheritdoc />
  1397. public void ClearTranscodingInfo(string deviceId)
  1398. {
  1399. ReportTranscodingInfo(deviceId, null);
  1400. }
  1401. /// <inheritdoc />
  1402. public SessionInfo GetSession(string deviceId, string client, string version)
  1403. {
  1404. return Sessions.FirstOrDefault(i =>
  1405. string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase)
  1406. && string.Equals(i.Client, client, StringComparison.OrdinalIgnoreCase));
  1407. }
  1408. /// <inheritdoc />
  1409. public Task<SessionInfo> GetSessionByAuthenticationToken(Device info, string deviceId, string remoteEndpoint, string appVersion)
  1410. {
  1411. ArgumentNullException.ThrowIfNull(info);
  1412. var user = info.UserId.Equals(default)
  1413. ? null
  1414. : _userManager.GetUserById(info.UserId);
  1415. appVersion = string.IsNullOrEmpty(appVersion)
  1416. ? info.AppVersion
  1417. : appVersion;
  1418. var deviceName = info.DeviceName;
  1419. var appName = info.AppName;
  1420. if (string.IsNullOrEmpty(deviceId))
  1421. {
  1422. deviceId = info.DeviceId;
  1423. }
  1424. // Prevent argument exception
  1425. if (string.IsNullOrEmpty(appVersion))
  1426. {
  1427. appVersion = "1";
  1428. }
  1429. return LogSessionActivity(appName, appVersion, deviceId, deviceName, remoteEndpoint, user);
  1430. }
  1431. /// <inheritdoc />
  1432. public async Task<SessionInfo> GetSessionByAuthenticationToken(string token, string deviceId, string remoteEndpoint)
  1433. {
  1434. var items = (await _deviceManager.GetDevices(new DeviceQuery
  1435. {
  1436. AccessToken = token,
  1437. Limit = 1
  1438. }).ConfigureAwait(false)).Items;
  1439. if (items.Count == 0)
  1440. {
  1441. return null;
  1442. }
  1443. return await GetSessionByAuthenticationToken(items[0], deviceId, remoteEndpoint, null).ConfigureAwait(false);
  1444. }
  1445. /// <inheritdoc />
  1446. public Task SendMessageToAdminSessions<T>(SessionMessageType name, T data, CancellationToken cancellationToken)
  1447. {
  1448. CheckDisposed();
  1449. var adminUserIds = _userManager.Users
  1450. .Where(i => i.HasPermission(PermissionKind.IsAdministrator))
  1451. .Select(i => i.Id)
  1452. .ToList();
  1453. return SendMessageToUserSessions(adminUserIds, name, data, cancellationToken);
  1454. }
  1455. /// <inheritdoc />
  1456. public Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, Func<T> dataFn, CancellationToken cancellationToken)
  1457. {
  1458. CheckDisposed();
  1459. var sessions = Sessions.Where(i => userIds.Any(i.ContainsUser)).ToList();
  1460. if (sessions.Count == 0)
  1461. {
  1462. return Task.CompletedTask;
  1463. }
  1464. return SendMessageToSessions(sessions, name, dataFn(), cancellationToken);
  1465. }
  1466. /// <inheritdoc />
  1467. public Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, T data, CancellationToken cancellationToken)
  1468. {
  1469. CheckDisposed();
  1470. var sessions = Sessions.Where(i => userIds.Any(i.ContainsUser));
  1471. return SendMessageToSessions(sessions, name, data, cancellationToken);
  1472. }
  1473. /// <inheritdoc />
  1474. public Task SendMessageToUserDeviceSessions<T>(string deviceId, SessionMessageType name, T data, CancellationToken cancellationToken)
  1475. {
  1476. CheckDisposed();
  1477. var sessions = Sessions.Where(i => string.Equals(i.DeviceId, deviceId, StringComparison.OrdinalIgnoreCase));
  1478. return SendMessageToSessions(sessions, name, data, cancellationToken);
  1479. }
  1480. /// <inheritdoc />
  1481. public async ValueTask DisposeAsync()
  1482. {
  1483. if (_disposed)
  1484. {
  1485. return;
  1486. }
  1487. foreach (var session in _activeConnections.Values)
  1488. {
  1489. await session.DisposeAsync().ConfigureAwait(false);
  1490. }
  1491. if (_idleTimer is not null)
  1492. {
  1493. await _idleTimer.DisposeAsync().ConfigureAwait(false);
  1494. _idleTimer = null;
  1495. }
  1496. await _shutdownCallback.DisposeAsync().ConfigureAwait(false);
  1497. _deviceManager.DeviceOptionsUpdated -= OnDeviceManagerDeviceOptionsUpdated;
  1498. _disposed = true;
  1499. }
  1500. private async void OnApplicationStopping()
  1501. {
  1502. _logger.LogInformation("Sending shutdown notifications");
  1503. try
  1504. {
  1505. var messageType = _appHost.ShouldRestart ? SessionMessageType.ServerRestarting : SessionMessageType.ServerShuttingDown;
  1506. await SendMessageToSessions(Sessions, messageType, string.Empty, CancellationToken.None).ConfigureAwait(false);
  1507. }
  1508. catch (Exception ex)
  1509. {
  1510. _logger.LogError(ex, "Error sending server shutdown notifications");
  1511. }
  1512. // Close open websockets to allow Kestrel to shut down cleanly
  1513. foreach (var session in _activeConnections.Values)
  1514. {
  1515. await session.DisposeAsync().ConfigureAwait(false);
  1516. }
  1517. _activeConnections.Clear();
  1518. }
  1519. }
  1520. }