SessionInfo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. public BaseItem FullNowPlayingItem { get; set; }
  113. public BaseItemDto NowViewingItem { get; set; }
  114. /// <summary>
  115. /// Gets or sets the device id.
  116. /// </summary>
  117. /// <value>The device id.</value>
  118. public string DeviceId { get; set; }
  119. /// <summary>
  120. /// Gets or sets the application version.
  121. /// </summary>
  122. /// <value>The application version.</value>
  123. public string ApplicationVersion { get; set; }
  124. /// <summary>
  125. /// Gets or sets the session controller.
  126. /// </summary>
  127. /// <value>The session controller.</value>
  128. [JsonIgnore]
  129. public ISessionController[] SessionControllers { get; set; }
  130. public TranscodingInfo TranscodingInfo { get; set; }
  131. /// <summary>
  132. /// Gets a value indicating whether this instance is active.
  133. /// </summary>
  134. /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
  135. public bool IsActive
  136. {
  137. get
  138. {
  139. var controllers = SessionControllers;
  140. foreach (var controller in controllers)
  141. {
  142. if (controller.IsSessionActive)
  143. {
  144. return true;
  145. }
  146. }
  147. if (controllers.Length > 0)
  148. {
  149. return false;
  150. }
  151. return true;
  152. }
  153. }
  154. public bool SupportsMediaControl
  155. {
  156. get
  157. {
  158. if (Capabilities is null || !Capabilities.SupportsMediaControl)
  159. {
  160. return false;
  161. }
  162. var controllers = SessionControllers;
  163. foreach (var controller in controllers)
  164. {
  165. if (controller.SupportsMediaControl)
  166. {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. }
  173. public bool SupportsRemoteControl
  174. {
  175. get
  176. {
  177. if (Capabilities is null || !Capabilities.SupportsMediaControl)
  178. {
  179. return false;
  180. }
  181. var controllers = SessionControllers;
  182. foreach (var controller in controllers)
  183. {
  184. if (controller.SupportsMediaControl)
  185. {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. }
  192. public IReadOnlyList<QueueItem> NowPlayingQueue { get; set; }
  193. public IReadOnlyList<BaseItemDto> NowPlayingQueueFullItems { get; set; }
  194. public bool HasCustomDeviceName { get; set; }
  195. public string PlaylistItemId { get; set; }
  196. public string ServerId { get; set; }
  197. public string UserPrimaryImageTag { get; set; }
  198. /// <summary>
  199. /// Gets the supported commands.
  200. /// </summary>
  201. /// <value>The supported commands.</value>
  202. public IReadOnlyList<GeneralCommandType> SupportedCommands
  203. => Capabilities is null ? Array.Empty<GeneralCommandType>() : Capabilities.SupportedCommands;
  204. public Tuple<ISessionController, bool> EnsureController<T>(Func<SessionInfo, ISessionController> factory)
  205. {
  206. var controllers = SessionControllers.ToList();
  207. foreach (var controller in controllers)
  208. {
  209. if (controller is T)
  210. {
  211. return new Tuple<ISessionController, bool>(controller, false);
  212. }
  213. }
  214. var newController = factory(this);
  215. _logger.LogDebug("Creating new {0}", newController.GetType().Name);
  216. controllers.Add(newController);
  217. SessionControllers = controllers.ToArray();
  218. return new Tuple<ISessionController, bool>(newController, true);
  219. }
  220. public void AddController(ISessionController controller)
  221. {
  222. var controllers = SessionControllers.ToList();
  223. controllers.Add(controller);
  224. SessionControllers = controllers.ToArray();
  225. }
  226. public bool ContainsUser(Guid userId)
  227. {
  228. if (UserId.Equals(userId))
  229. {
  230. return true;
  231. }
  232. foreach (var additionalUser in AdditionalUsers)
  233. {
  234. if (additionalUser.UserId.Equals(userId))
  235. {
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. public void StartAutomaticProgress(PlaybackProgressInfo progressInfo)
  242. {
  243. if (_disposed)
  244. {
  245. return;
  246. }
  247. lock (_progressLock)
  248. {
  249. _lastProgressInfo = progressInfo;
  250. if (_progressTimer is null)
  251. {
  252. _progressTimer = new Timer(OnProgressTimerCallback, null, 1000, 1000);
  253. }
  254. else
  255. {
  256. _progressTimer.Change(1000, 1000);
  257. }
  258. }
  259. }
  260. private async void OnProgressTimerCallback(object state)
  261. {
  262. if (_disposed)
  263. {
  264. return;
  265. }
  266. var progressInfo = _lastProgressInfo;
  267. if (progressInfo is null)
  268. {
  269. return;
  270. }
  271. if (progressInfo.IsPaused)
  272. {
  273. return;
  274. }
  275. var positionTicks = progressInfo.PositionTicks ?? 0;
  276. if (positionTicks < 0)
  277. {
  278. positionTicks = 0;
  279. }
  280. var newPositionTicks = positionTicks + ProgressIncrement;
  281. var item = progressInfo.Item;
  282. long? runtimeTicks = item?.RunTimeTicks;
  283. // Don't report beyond the runtime
  284. if (runtimeTicks.HasValue && newPositionTicks >= runtimeTicks.Value)
  285. {
  286. return;
  287. }
  288. progressInfo.PositionTicks = newPositionTicks;
  289. try
  290. {
  291. await _sessionManager.OnPlaybackProgress(progressInfo, true).ConfigureAwait(false);
  292. }
  293. catch (Exception ex)
  294. {
  295. _logger.LogError(ex, "Error reporting playback progress");
  296. }
  297. }
  298. public void StopAutomaticProgress()
  299. {
  300. lock (_progressLock)
  301. {
  302. if (_progressTimer is not null)
  303. {
  304. _progressTimer.Dispose();
  305. _progressTimer = null;
  306. }
  307. _lastProgressInfo = null;
  308. }
  309. }
  310. public async ValueTask DisposeAsync()
  311. {
  312. _disposed = true;
  313. StopAutomaticProgress();
  314. var controllers = SessionControllers.ToList();
  315. SessionControllers = Array.Empty<ISessionController>();
  316. foreach (var controller in controllers)
  317. {
  318. if (controller is IAsyncDisposable disposableAsync)
  319. {
  320. _logger.LogDebug("Disposing session controller asynchronously {TypeName}", disposableAsync.GetType().Name);
  321. await disposableAsync.DisposeAsync().ConfigureAwait(false);
  322. }
  323. else if (controller is IDisposable disposable)
  324. {
  325. _logger.LogDebug("Disposing session controller synchronously {TypeName}", disposable.GetType().Name);
  326. disposable.Dispose();
  327. }
  328. }
  329. }
  330. }
  331. }