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
{
///
/// The default ping value used for sessions.
///
public const long DefaultPing = 500;
///
/// Gets 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.
///
/// The session id to check.
/// 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)
{
Participants.TryAdd(
session.Id,
new GroupMember
{
Session = session,
Ping = DefaultPing,
IsBuffering = false
});
}
///
/// Removes the session from the group.
///
/// The session.
public void RemoveSession(SessionInfo session)
{
Participants.Remove(session.Id);
}
///
/// Updates the ping of a session.
///
/// The session.
/// The ping.
public void UpdatePing(SessionInfo session, long ping)
{
if (Participants.TryGetValue(session.Id, out GroupMember value))
{
value.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 (Participants.TryGetValue(session.Id, out GroupMember value))
{
value.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;
}
}
}