UserDataManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Common.Kernel;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Model.Connectivity;
  5. using System;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Controller.Library
  9. {
  10. /// <summary>
  11. /// Class UserDataManager
  12. /// </summary>
  13. public class UserDataManager : BaseManager<Kernel>
  14. {
  15. #region Events
  16. /// <summary>
  17. /// Occurs when [playback start].
  18. /// </summary>
  19. public event EventHandler<PlaybackProgressEventArgs> PlaybackStart;
  20. /// <summary>
  21. /// Occurs when [playback progress].
  22. /// </summary>
  23. public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
  24. /// <summary>
  25. /// Occurs when [playback stopped].
  26. /// </summary>
  27. public event EventHandler<PlaybackProgressEventArgs> PlaybackStopped;
  28. #endregion
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="UserDataManager" /> class.
  31. /// </summary>
  32. /// <param name="kernel">The kernel.</param>
  33. public UserDataManager(Kernel kernel)
  34. : base(kernel)
  35. {
  36. }
  37. /// <summary>
  38. /// Used to report that playback has started for an item
  39. /// </summary>
  40. /// <param name="user">The user.</param>
  41. /// <param name="item">The item.</param>
  42. /// <param name="clientType">Type of the client.</param>
  43. /// <param name="deviceName">Name of the device.</param>
  44. /// <exception cref="System.ArgumentNullException"></exception>
  45. public void OnPlaybackStart(User user, BaseItem item, ClientType clientType, string deviceName)
  46. {
  47. if (user == null)
  48. {
  49. throw new ArgumentNullException();
  50. }
  51. if (item == null)
  52. {
  53. throw new ArgumentNullException();
  54. }
  55. Kernel.UserManager.UpdateNowPlayingItemId(user, clientType, deviceName, item);
  56. // Nothing to save here
  57. // Fire events to inform plugins
  58. EventHelper.QueueEventIfNotNull(PlaybackStart, this, new PlaybackProgressEventArgs
  59. {
  60. Argument = item,
  61. User = user
  62. });
  63. }
  64. /// <summary>
  65. /// Used to report playback progress for an item
  66. /// </summary>
  67. /// <param name="user">The user.</param>
  68. /// <param name="item">The item.</param>
  69. /// <param name="positionTicks">The position ticks.</param>
  70. /// <param name="clientType">Type of the client.</param>
  71. /// <param name="deviceName">Name of the device.</param>
  72. /// <returns>Task.</returns>
  73. /// <exception cref="System.ArgumentNullException"></exception>
  74. public async Task OnPlaybackProgress(User user, BaseItem item, long? positionTicks, ClientType clientType, string deviceName)
  75. {
  76. if (user == null)
  77. {
  78. throw new ArgumentNullException();
  79. }
  80. if (item == null)
  81. {
  82. throw new ArgumentNullException();
  83. }
  84. Kernel.UserManager.UpdateNowPlayingItemId(user, clientType, deviceName, item, positionTicks);
  85. if (positionTicks.HasValue)
  86. {
  87. var data = item.GetUserData(user, true);
  88. UpdatePlayState(item, data, positionTicks.Value, false);
  89. await SaveUserDataForItem(user, item, data).ConfigureAwait(false);
  90. }
  91. EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs
  92. {
  93. Argument = item,
  94. User = user,
  95. PlaybackPositionTicks = positionTicks
  96. });
  97. }
  98. /// <summary>
  99. /// Used to report that playback has ended for an item
  100. /// </summary>
  101. /// <param name="user">The user.</param>
  102. /// <param name="item">The item.</param>
  103. /// <param name="positionTicks">The position ticks.</param>
  104. /// <param name="clientType">Type of the client.</param>
  105. /// <param name="deviceName">Name of the device.</param>
  106. /// <returns>Task.</returns>
  107. /// <exception cref="System.ArgumentNullException"></exception>
  108. public async Task OnPlaybackStopped(User user, BaseItem item, long? positionTicks, ClientType clientType, string deviceName)
  109. {
  110. if (user == null)
  111. {
  112. throw new ArgumentNullException();
  113. }
  114. if (item == null)
  115. {
  116. throw new ArgumentNullException();
  117. }
  118. Kernel.UserManager.RemoveNowPlayingItemId(user, clientType, deviceName, item);
  119. var data = item.GetUserData(user, true);
  120. if (positionTicks.HasValue)
  121. {
  122. UpdatePlayState(item, data, positionTicks.Value, true);
  123. }
  124. else
  125. {
  126. // If the client isn't able to report this, then we'll just have to make an assumption
  127. data.PlayCount++;
  128. data.Played = true;
  129. }
  130. await SaveUserDataForItem(user, item, data).ConfigureAwait(false);
  131. EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackProgressEventArgs
  132. {
  133. Argument = item,
  134. User = user,
  135. PlaybackPositionTicks = positionTicks
  136. });
  137. }
  138. /// <summary>
  139. /// Updates playstate position for an item but does not save
  140. /// </summary>
  141. /// <param name="item">The item</param>
  142. /// <param name="data">User data for the item</param>
  143. /// <param name="positionTicks">The current playback position</param>
  144. /// <param name="incrementPlayCount">Whether or not to increment playcount</param>
  145. private void UpdatePlayState(BaseItem item, UserItemData data, long positionTicks, bool incrementPlayCount)
  146. {
  147. // If a position has been reported, and if we know the duration
  148. if (positionTicks > 0 && item.RunTimeTicks.HasValue && item.RunTimeTicks > 0)
  149. {
  150. var pctIn = Decimal.Divide(positionTicks, item.RunTimeTicks.Value) * 100;
  151. // Don't track in very beginning
  152. if (pctIn < Kernel.Configuration.MinResumePct)
  153. {
  154. positionTicks = 0;
  155. incrementPlayCount = false;
  156. }
  157. // If we're at the end, assume completed
  158. else if (pctIn > Kernel.Configuration.MaxResumePct || positionTicks >= item.RunTimeTicks.Value)
  159. {
  160. positionTicks = 0;
  161. data.Played = true;
  162. }
  163. else
  164. {
  165. // Enforce MinResumeDuration
  166. var durationSeconds = TimeSpan.FromTicks(item.RunTimeTicks.Value).TotalSeconds;
  167. if (durationSeconds < Kernel.Configuration.MinResumeDurationSeconds)
  168. {
  169. positionTicks = 0;
  170. data.Played = true;
  171. }
  172. }
  173. }
  174. data.PlaybackPositionTicks = positionTicks;
  175. if (incrementPlayCount)
  176. {
  177. data.PlayCount++;
  178. data.LastPlayedDate = DateTime.UtcNow;
  179. }
  180. }
  181. /// <summary>
  182. /// Saves user data for an item
  183. /// </summary>
  184. /// <param name="user">The user.</param>
  185. /// <param name="item">The item.</param>
  186. /// <param name="data">The data.</param>
  187. public Task SaveUserDataForItem(User user, BaseItem item, UserItemData data)
  188. {
  189. item.AddOrUpdateUserData(user, data);
  190. return Kernel.UserDataRepository.SaveUserData(item, CancellationToken.None);
  191. }
  192. }
  193. }