SessionInfo.cs 8.1 KB

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