SessionInfo.cs 8.6 KB

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