2
0

SessionInfo.cs 11 KB

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