2
0

SessionInfo.cs 11 KB

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