2
0

SessionInfo.cs 12 KB

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