SessionInfo.cs 11 KB

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