jellyfin-server/MediaBrowser.Controller/Syncplay/GroupInfo.cs

149 lines
4.7 KiB
C#
Raw Normal View History

2020-04-01 15:52:42 +00:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Session;
namespace MediaBrowser.Controller.Syncplay
{
/// <summary>
/// Class GroupInfo.
/// </summary>
public class GroupInfo
{
/// <summary>
2020-04-04 15:56:21 +00:00
/// Default ping value used for sessions.
2020-04-01 15:52:42 +00:00
/// </summary>
public readonly long DefaulPing = 500;
/// <summary>
/// Gets or sets the group identifier.
/// </summary>
/// <value>The group identifier.</value>
public readonly Guid GroupId = Guid.NewGuid();
/// <summary>
/// Gets or sets the playing item.
/// </summary>
/// <value>The playing item.</value>
public BaseItem PlayingItem { get; set; }
/// <summary>
/// Gets or sets whether playback is paused.
/// </summary>
/// <value>Playback is paused.</value>
public bool IsPaused { get; set; }
/// <summary>
/// Gets or sets the position ticks.
/// </summary>
/// <value>The position ticks.</value>
public long PositionTicks { get; set; }
/// <summary>
/// Gets or sets the last activity.
/// </summary>
/// <value>The last activity.</value>
public DateTime LastActivity { get; set; }
/// <summary>
/// Gets the partecipants.
/// </summary>
/// <value>The partecipants.</value>
public readonly ConcurrentDictionary<string, GroupMember> Partecipants =
new ConcurrentDictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
/// <summary>
2020-04-04 15:56:21 +00:00
/// Checks if a session is in this group.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-04-04 15:56:21 +00:00
/// <value><c>true</c> if the session is in this group; <c>false</c> otherwise.</value>
public bool ContainsSession(string sessionId)
2020-04-01 15:52:42 +00:00
{
return Partecipants.ContainsKey(sessionId);
}
/// <summary>
2020-04-04 15:56:21 +00:00
/// Adds the session to the group.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-04-04 15:56:21 +00:00
/// <param name="session">The session.</param>
public void AddSession(SessionInfo session)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
if (ContainsSession(session.Id.ToString())) return;
2020-04-01 15:52:42 +00:00
var member = new GroupMember();
2020-04-04 15:56:21 +00:00
member.Session = session;
2020-04-01 15:52:42 +00:00
member.Ping = DefaulPing;
member.IsBuffering = false;
2020-04-04 15:56:21 +00:00
Partecipants[session.Id.ToString()] = member;
2020-04-01 15:52:42 +00:00
}
/// <summary>
2020-04-04 15:56:21 +00:00
/// Removes the session from the group.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-04-04 15:56:21 +00:00
/// <param name="session">The session.</param>
2020-04-01 15:52:42 +00:00
2020-04-04 15:56:21 +00:00
public void RemoveSession(SessionInfo session)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
if (!ContainsSession(session.Id.ToString())) return;
2020-04-01 15:52:42 +00:00
GroupMember member;
2020-04-04 15:56:21 +00:00
Partecipants.Remove(session.Id.ToString(), out member);
2020-04-01 15:52:42 +00:00
}
/// <summary>
2020-04-04 15:56:21 +00:00
/// Updates the ping of a session.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-04-04 15:56:21 +00:00
/// <param name="session">The session.</param>
2020-04-01 15:52:42 +00:00
/// <param name="ping">The ping.</param>
2020-04-04 15:56:21 +00:00
public void UpdatePing(SessionInfo session, long ping)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
if (!ContainsSession(session.Id.ToString())) return;
Partecipants[session.Id.ToString()].Ping = ping;
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Gets the highest ping in the group.
/// </summary>
2020-04-04 15:56:21 +00:00
/// <value name="session">The highest ping in the group.</value>
2020-04-01 15:52:42 +00:00
public long GetHighestPing()
{
long max = Int64.MinValue;
2020-04-04 15:56:21 +00:00
foreach (var session in Partecipants.Values)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
max = Math.Max(max, session.Ping);
2020-04-01 15:52:42 +00:00
}
return max;
}
/// <summary>
2020-04-04 15:56:21 +00:00
/// Sets the session's buffering state.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-04-04 15:56:21 +00:00
/// <param name="session">The session.</param>
2020-04-01 15:52:42 +00:00
/// <param name="isBuffering">The state.</param>
2020-04-04 15:56:21 +00:00
public void SetBuffering(SessionInfo session, bool isBuffering)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
if (!ContainsSession(session.Id.ToString())) return;
Partecipants[session.Id.ToString()].IsBuffering = isBuffering;
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Gets the group buffering state.
/// </summary>
2020-04-04 15:56:21 +00:00
/// <value><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</value>
2020-04-01 15:52:42 +00:00
public bool IsBuffering()
{
2020-04-04 15:56:21 +00:00
foreach (var session in Partecipants.Values)
2020-04-01 15:52:42 +00:00
{
2020-04-04 15:56:21 +00:00
if (session.IsBuffering) return true;
2020-04-01 15:52:42 +00:00
}
return false;
}
/// <summary>
/// Checks if the group is empty.
/// </summary>
/// <value><c>true</c> if the group is empty; <c>false</c> otherwise.</value>
public bool IsEmpty()
{
return Partecipants.Count == 0;
}
}
}