SessionInfo.cs 10 KB

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