GroupInfo.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Session;
  5. namespace MediaBrowser.Controller.SyncPlay
  6. {
  7. /// <summary>
  8. /// Class GroupInfo.
  9. /// </summary>
  10. /// <remarks>
  11. /// Class is not thread-safe, external locking is required when accessing methods.
  12. /// </remarks>
  13. public class GroupInfo
  14. {
  15. /// <summary>
  16. /// The default ping value used for sessions.
  17. /// </summary>
  18. public const long DefaultPing = 500;
  19. /// <summary>
  20. /// Gets the group identifier.
  21. /// </summary>
  22. /// <value>The group identifier.</value>
  23. public Guid GroupId { get; } = Guid.NewGuid();
  24. /// <summary>
  25. /// Gets or sets the playing item.
  26. /// </summary>
  27. /// <value>The playing item.</value>
  28. public BaseItem PlayingItem { get; set; }
  29. /// <summary>
  30. /// Gets or sets a value indicating whether playback is paused.
  31. /// </summary>
  32. /// <value>Playback is paused.</value>
  33. public bool IsPaused { get; set; }
  34. /// <summary>
  35. /// Gets or sets a value indicating whether there are position ticks.
  36. /// </summary>
  37. /// <value>The position ticks.</value>
  38. public long PositionTicks { get; set; }
  39. /// <summary>
  40. /// Gets or sets the last activity.
  41. /// </summary>
  42. /// <value>The last activity.</value>
  43. public DateTime LastActivity { get; set; }
  44. /// <summary>
  45. /// Gets the participants.
  46. /// </summary>
  47. /// <value>The participants, or members of the group.</value>
  48. public Dictionary<string, GroupMember> Participants { get; } =
  49. new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
  50. /// <summary>
  51. /// Checks if a session is in this group.
  52. /// </summary>
  53. /// <param name="sessionId">The session id to check.</param>
  54. /// <returns><c>true</c> if the session is in this group; <c>false</c> otherwise.</returns>
  55. public bool ContainsSession(string sessionId)
  56. {
  57. return Participants.ContainsKey(sessionId);
  58. }
  59. /// <summary>
  60. /// Adds the session to the group.
  61. /// </summary>
  62. /// <param name="session">The session.</param>
  63. public void AddSession(SessionInfo session)
  64. {
  65. Participants.TryAdd(
  66. session.Id,
  67. new GroupMember
  68. {
  69. Session = session,
  70. Ping = DefaultPing,
  71. IsBuffering = false
  72. });
  73. }
  74. /// <summary>
  75. /// Removes the session from the group.
  76. /// </summary>
  77. /// <param name="session">The session.</param>
  78. public void RemoveSession(SessionInfo session)
  79. {
  80. Participants.Remove(session.Id);
  81. }
  82. /// <summary>
  83. /// Updates the ping of a session.
  84. /// </summary>
  85. /// <param name="session">The session.</param>
  86. /// <param name="ping">The ping.</param>
  87. public void UpdatePing(SessionInfo session, long ping)
  88. {
  89. if (Participants.TryGetValue(session.Id, out GroupMember value))
  90. {
  91. value.Ping = ping;
  92. }
  93. }
  94. /// <summary>
  95. /// Gets the highest ping in the group.
  96. /// </summary>
  97. /// <returns>The highest ping in the group.</returns>
  98. public long GetHighestPing()
  99. {
  100. long max = long.MinValue;
  101. foreach (var session in Participants.Values)
  102. {
  103. max = Math.Max(max, session.Ping);
  104. }
  105. return max;
  106. }
  107. /// <summary>
  108. /// Sets the session's buffering state.
  109. /// </summary>
  110. /// <param name="session">The session.</param>
  111. /// <param name="isBuffering">The state.</param>
  112. public void SetBuffering(SessionInfo session, bool isBuffering)
  113. {
  114. if (Participants.TryGetValue(session.Id, out GroupMember value))
  115. {
  116. value.IsBuffering = isBuffering;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets the group buffering state.
  121. /// </summary>
  122. /// <returns><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</returns>
  123. public bool IsBuffering()
  124. {
  125. foreach (var session in Participants.Values)
  126. {
  127. if (session.IsBuffering)
  128. {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. /// <summary>
  135. /// Checks if the group is empty.
  136. /// </summary>
  137. /// <returns><c>true</c> if the group is empty; <c>false</c> otherwise.</returns>
  138. public bool IsEmpty()
  139. {
  140. return Participants.Count == 0;
  141. }
  142. }
  143. }