GroupInfo.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /// Gets the default ping value used for sessions.
  17. /// </summary>
  18. public long DefaultPing { get; } = 500;
  19. /// <summary>
  20. /// Gets or sets 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. /// <value><c>true</c> if the session is in this group; <c>false</c> otherwise.</value>
  54. public bool ContainsSession(string sessionId)
  55. {
  56. return Participants.ContainsKey(sessionId);
  57. }
  58. /// <summary>
  59. /// Adds the session to the group.
  60. /// </summary>
  61. /// <param name="session">The session.</param>
  62. public void AddSession(SessionInfo session)
  63. {
  64. if (ContainsSession(session.Id))
  65. {
  66. return;
  67. }
  68. var member = new GroupMember();
  69. member.Session = session;
  70. member.Ping = DefaultPing;
  71. member.IsBuffering = false;
  72. Participants[session.Id] = member;
  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. if (!ContainsSession(session.Id))
  81. {
  82. return;
  83. }
  84. Participants.Remove(session.Id, out _);
  85. }
  86. /// <summary>
  87. /// Updates the ping of a session.
  88. /// </summary>
  89. /// <param name="session">The session.</param>
  90. /// <param name="ping">The ping.</param>
  91. public void UpdatePing(SessionInfo session, long ping)
  92. {
  93. if (!ContainsSession(session.Id))
  94. {
  95. return;
  96. }
  97. Participants[session.Id].Ping = ping;
  98. }
  99. /// <summary>
  100. /// Gets the highest ping in the group.
  101. /// </summary>
  102. /// <value name="session">The highest ping in the group.</value>
  103. public long GetHighestPing()
  104. {
  105. long max = long.MinValue;
  106. foreach (var session in Participants.Values)
  107. {
  108. max = Math.Max(max, session.Ping);
  109. }
  110. return max;
  111. }
  112. /// <summary>
  113. /// Sets the session's buffering state.
  114. /// </summary>
  115. /// <param name="session">The session.</param>
  116. /// <param name="isBuffering">The state.</param>
  117. public void SetBuffering(SessionInfo session, bool isBuffering)
  118. {
  119. if (!ContainsSession(session.Id))
  120. {
  121. return;
  122. }
  123. Participants[session.Id].IsBuffering = isBuffering;
  124. }
  125. /// <summary>
  126. /// Gets the group buffering state.
  127. /// </summary>
  128. /// <value><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</value>
  129. public bool IsBuffering()
  130. {
  131. foreach (var session in Participants.Values)
  132. {
  133. if (session.IsBuffering)
  134. {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /// <summary>
  141. /// Checks if the group is empty.
  142. /// </summary>
  143. /// <value><c>true</c> if the group is empty; <c>false</c> otherwise.</value>
  144. public bool IsEmpty()
  145. {
  146. return Participants.Count == 0;
  147. }
  148. }
  149. }