2
0

SessionInfo.cs 11 KB

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