using System; using System.Collections.Generic; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Session; namespace MediaBrowser.Controller.SyncPlay { /// /// Class GroupInfo. /// /// /// Class is not thread-safe, external locking is required when accessing methods. /// public class GroupInfo { /// /// Gets the default ping value used for sessions. /// public long DefaultPing { get; } = 500; /// /// Gets or sets the group identifier. /// /// The group identifier. public Guid GroupId { get; } = Guid.NewGuid(); /// /// Gets or sets the playing item. /// /// The playing item. public BaseItem PlayingItem { get; set; } /// /// Gets or sets a value indicating whether playback is paused. /// /// Playback is paused. public bool IsPaused { get; set; } /// /// Gets or sets a value indicating whether there are position ticks. /// /// The position ticks. public long PositionTicks { get; set; } /// /// Gets or sets the last activity. /// /// The last activity. public DateTime LastActivity { get; set; } /// /// Gets the participants. /// /// The participants, or members of the group. public Dictionary Participants { get; } = new Dictionary(StringComparer.OrdinalIgnoreCase); /// /// Checks if a session is in this group. /// /// true if the session is in this group; false otherwise. public bool ContainsSession(string sessionId) { return Participants.ContainsKey(sessionId); } /// /// Adds the session to the group. /// /// The session. public void AddSession(SessionInfo session) { if (ContainsSession(session.Id)) { return; } var member = new GroupMember(); member.Session = session; member.Ping = DefaultPing; member.IsBuffering = false; Participants[session.Id] = member; } /// /// Removes the session from the group. /// /// The session. public void RemoveSession(SessionInfo session) { if (!ContainsSession(session.Id)) { return; } Participants.Remove(session.Id, out _); } /// /// Updates the ping of a session. /// /// The session. /// The ping. public void UpdatePing(SessionInfo session, long ping) { if (!ContainsSession(session.Id)) { return; } Participants[session.Id].Ping = ping; } /// /// Gets the highest ping in the group. /// /// The highest ping in the group. public long GetHighestPing() { long max = long.MinValue; foreach (var session in Participants.Values) { max = Math.Max(max, session.Ping); } return max; } /// /// Sets the session's buffering state. /// /// The session. /// The state. public void SetBuffering(SessionInfo session, bool isBuffering) { if (!ContainsSession(session.Id)) { return; } Participants[session.Id].IsBuffering = isBuffering; } /// /// Gets the group buffering state. /// /// true if there is a session buffering in the group; false otherwise. public bool IsBuffering() { foreach (var session in Participants.Values) { if (session.IsBuffering) { return true; } } return false; } /// /// Checks if the group is empty. /// /// true if the group is empty; false otherwise. public bool IsEmpty() { return Participants.Count == 0; } } }