SessionInfo.cs 11 KB

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