SessionInfo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.Json.Serialization;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Data.Enums;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Session;
  13. using Microsoft.Extensions.Logging;
  14. namespace MediaBrowser.Controller.Session
  15. {
  16. /// <summary>
  17. /// Class SessionInfo.
  18. /// </summary>
  19. public sealed class SessionInfo : IAsyncDisposable
  20. {
  21. // 1 second
  22. private const long ProgressIncrement = 10000000;
  23. private readonly ISessionManager _sessionManager;
  24. private readonly ILogger _logger;
  25. private readonly object _progressLock = new object();
  26. private Timer _progressTimer;
  27. private PlaybackProgressInfo _lastProgressInfo;
  28. private bool _disposed = false;
  29. public SessionInfo(ISessionManager sessionManager, ILogger logger)
  30. {
  31. _sessionManager = sessionManager;
  32. _logger = logger;
  33. AdditionalUsers = Array.Empty<SessionUserInfo>();
  34. PlayState = new PlayerStateInfo();
  35. SessionControllers = Array.Empty<ISessionController>();
  36. NowPlayingQueue = Array.Empty<QueueItem>();
  37. NowPlayingQueueFullItems = Array.Empty<BaseItemDto>();
  38. }
  39. public PlayerStateInfo PlayState { get; set; }
  40. public SessionUserInfo[] AdditionalUsers { get; set; }
  41. public ClientCapabilities Capabilities { get; set; }
  42. /// <summary>
  43. /// Gets or sets the remote end point.
  44. /// </summary>
  45. /// <value>The remote end point.</value>
  46. public string RemoteEndPoint { get; set; }
  47. /// <summary>
  48. /// Gets the playable media types.
  49. /// </summary>
  50. /// <value>The playable media types.</value>
  51. public IReadOnlyList<MediaType> PlayableMediaTypes
  52. {
  53. get
  54. {
  55. if (Capabilities is null)
  56. {
  57. return Array.Empty<MediaType>();
  58. }
  59. return Capabilities.PlayableMediaTypes;
  60. }
  61. }
  62. /// <summary>
  63. /// Gets or sets the id.
  64. /// </summary>
  65. /// <value>The id.</value>
  66. public string Id { get; set; }
  67. /// <summary>
  68. /// Gets or sets the user id.
  69. /// </summary>
  70. /// <value>The user id.</value>
  71. public Guid UserId { get; set; }
  72. /// <summary>
  73. /// Gets or sets the username.
  74. /// </summary>
  75. /// <value>The username.</value>
  76. public string UserName { get; set; }
  77. /// <summary>
  78. /// Gets or sets the type of the client.
  79. /// </summary>
  80. /// <value>The type of the client.</value>
  81. public string Client { get; set; }
  82. /// <summary>
  83. /// Gets or sets the last activity date.
  84. /// </summary>
  85. /// <value>The last activity date.</value>
  86. public DateTime LastActivityDate { get; set; }
  87. /// <summary>
  88. /// Gets or sets the last playback check in.
  89. /// </summary>
  90. /// <value>The last playback check in.</value>
  91. public DateTime LastPlaybackCheckIn { get; set; }
  92. /// <summary>
  93. /// Gets or sets the last paused date.
  94. /// </summary>
  95. /// <value>The last paused date.</value>
  96. public DateTime? LastPausedDate { get; set; }
  97. /// <summary>
  98. /// Gets or sets the name of the device.
  99. /// </summary>
  100. /// <value>The name of the device.</value>
  101. public string DeviceName { get; set; }
  102. /// <summary>
  103. /// Gets or sets the type of the device.
  104. /// </summary>
  105. /// <value>The type of the device.</value>
  106. public string DeviceType { get; set; }
  107. /// <summary>
  108. /// Gets or sets the now playing item.
  109. /// </summary>
  110. /// <value>The now playing item.</value>
  111. public BaseItemDto NowPlayingItem { get; set; }
  112. [JsonIgnore]
  113. public BaseItem FullNowPlayingItem { get; set; }
  114. public BaseItemDto NowViewingItem { get; set; }
  115. /// <summary>
  116. /// Gets or sets the device id.
  117. /// </summary>
  118. /// <value>The device id.</value>
  119. public string DeviceId { get; set; }
  120. /// <summary>
  121. /// Gets or sets the application version.
  122. /// </summary>
  123. /// <value>The application version.</value>
  124. public string ApplicationVersion { get; set; }
  125. /// <summary>
  126. /// Gets or sets the session controller.
  127. /// </summary>
  128. /// <value>The session controller.</value>
  129. [JsonIgnore]
  130. public ISessionController[] SessionControllers { get; set; }
  131. public TranscodingInfo TranscodingInfo { get; set; }
  132. /// <summary>
  133. /// Gets a value indicating whether this instance is active.
  134. /// </summary>
  135. /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
  136. public bool IsActive
  137. {
  138. get
  139. {
  140. var controllers = SessionControllers;
  141. foreach (var controller in controllers)
  142. {
  143. if (controller.IsSessionActive)
  144. {
  145. return true;
  146. }
  147. }
  148. if (controllers.Length > 0)
  149. {
  150. return false;
  151. }
  152. return true;
  153. }
  154. }
  155. public bool SupportsMediaControl
  156. {
  157. get
  158. {
  159. if (Capabilities is null || !Capabilities.SupportsMediaControl)
  160. {
  161. return false;
  162. }
  163. var controllers = SessionControllers;
  164. foreach (var controller in controllers)
  165. {
  166. if (controller.SupportsMediaControl)
  167. {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. }
  174. public bool SupportsRemoteControl
  175. {
  176. get
  177. {
  178. if (Capabilities is null || !Capabilities.SupportsMediaControl)
  179. {
  180. return false;
  181. }
  182. var controllers = SessionControllers;
  183. foreach (var controller in controllers)
  184. {
  185. if (controller.SupportsMediaControl)
  186. {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. }
  193. public IReadOnlyList<QueueItem> NowPlayingQueue { get; set; }
  194. public IReadOnlyList<BaseItemDto> NowPlayingQueueFullItems { get; set; }
  195. public bool HasCustomDeviceName { get; set; }
  196. public string PlaylistItemId { get; set; }
  197. public string ServerId { get; set; }
  198. public string UserPrimaryImageTag { get; set; }
  199. /// <summary>
  200. /// Gets the supported commands.
  201. /// </summary>
  202. /// <value>The supported commands.</value>
  203. public IReadOnlyList<GeneralCommandType> SupportedCommands
  204. => Capabilities is null ? Array.Empty<GeneralCommandType>() : Capabilities.SupportedCommands;
  205. public Tuple<ISessionController, bool> EnsureController<T>(Func<SessionInfo, ISessionController> factory)
  206. {
  207. var controllers = SessionControllers.ToList();
  208. foreach (var controller in controllers)
  209. {
  210. if (controller is T)
  211. {
  212. return new Tuple<ISessionController, bool>(controller, false);
  213. }
  214. }
  215. var newController = factory(this);
  216. _logger.LogDebug("Creating new {0}", newController.GetType().Name);
  217. controllers.Add(newController);
  218. SessionControllers = controllers.ToArray();
  219. return new Tuple<ISessionController, bool>(newController, true);
  220. }
  221. public void AddController(ISessionController controller)
  222. {
  223. SessionControllers = [..SessionControllers, controller];
  224. }
  225. public bool ContainsUser(Guid userId)
  226. {
  227. if (UserId.Equals(userId))
  228. {
  229. return true;
  230. }
  231. foreach (var additionalUser in AdditionalUsers)
  232. {
  233. if (additionalUser.UserId.Equals(userId))
  234. {
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. public void StartAutomaticProgress(PlaybackProgressInfo progressInfo)
  241. {
  242. if (_disposed)
  243. {
  244. return;
  245. }
  246. lock (_progressLock)
  247. {
  248. _lastProgressInfo = progressInfo;
  249. if (_progressTimer is null)
  250. {
  251. _progressTimer = new Timer(OnProgressTimerCallback, null, 1000, 1000);
  252. }
  253. else
  254. {
  255. _progressTimer.Change(1000, 1000);
  256. }
  257. }
  258. }
  259. private async void OnProgressTimerCallback(object state)
  260. {
  261. if (_disposed)
  262. {
  263. return;
  264. }
  265. var progressInfo = _lastProgressInfo;
  266. if (progressInfo is null)
  267. {
  268. return;
  269. }
  270. if (progressInfo.IsPaused)
  271. {
  272. return;
  273. }
  274. var positionTicks = progressInfo.PositionTicks ?? 0;
  275. if (positionTicks < 0)
  276. {
  277. positionTicks = 0;
  278. }
  279. var newPositionTicks = positionTicks + ProgressIncrement;
  280. var item = progressInfo.Item;
  281. long? runtimeTicks = item?.RunTimeTicks;
  282. // Don't report beyond the runtime
  283. if (runtimeTicks.HasValue && newPositionTicks >= runtimeTicks.Value)
  284. {
  285. return;
  286. }
  287. progressInfo.PositionTicks = newPositionTicks;
  288. try
  289. {
  290. await _sessionManager.OnPlaybackProgress(progressInfo, true).ConfigureAwait(false);
  291. }
  292. catch (Exception ex)
  293. {
  294. _logger.LogError(ex, "Error reporting playback progress");
  295. }
  296. }
  297. public void StopAutomaticProgress()
  298. {
  299. lock (_progressLock)
  300. {
  301. if (_progressTimer is not null)
  302. {
  303. _progressTimer.Dispose();
  304. _progressTimer = null;
  305. }
  306. _lastProgressInfo = null;
  307. }
  308. }
  309. public async ValueTask DisposeAsync()
  310. {
  311. _disposed = true;
  312. StopAutomaticProgress();
  313. var controllers = SessionControllers.ToList();
  314. SessionControllers = Array.Empty<ISessionController>();
  315. foreach (var controller in controllers)
  316. {
  317. if (controller is IAsyncDisposable disposableAsync)
  318. {
  319. _logger.LogDebug("Disposing session controller asynchronously {TypeName}", disposableAsync.GetType().Name);
  320. await disposableAsync.DisposeAsync().ConfigureAwait(false);
  321. }
  322. else if (controller is IDisposable disposable)
  323. {
  324. _logger.LogDebug("Disposing session controller synchronously {TypeName}", disposable.GetType().Name);
  325. disposable.Dispose();
  326. }
  327. }
  328. }
  329. }
  330. }