jellyfin/MediaBrowser.Controller/SyncPlay/GroupInfo.cs

170 lines
5 KiB
C#
Raw Normal View History

2020-04-01 17:52:42 +02:00
using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Session;
2020-05-06 23:42:53 +02:00
namespace MediaBrowser.Controller.SyncPlay
2020-04-01 17:52:42 +02:00
{
/// <summary>
/// Class GroupInfo.
/// </summary>
2020-05-04 19:46:02 +02:00
/// <remarks>
/// Class is not thread-safe, external locking is required when accessing methods.
/// </remarks>
2020-04-01 17:52:42 +02:00
public class GroupInfo
{
/// <summary>
2020-04-04 17:56:21 +02:00
/// Default ping value used for sessions.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-05-26 11:37:52 +02:00
public long DefaulPing { get; } = 500;
2020-04-01 17:52:42 +02:00
/// <summary>
/// Gets or sets the group identifier.
/// </summary>
/// <value>The group identifier.</value>
2020-05-26 11:37:52 +02:00
public Guid GroupId { get; } = Guid.NewGuid();
2020-04-01 17:52:42 +02:00
/// <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>
2020-04-21 23:37:37 +02:00
/// Gets the participants.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-04-21 23:37:37 +02:00
/// <value>The participants, or members of the group.</value>
2020-05-26 11:37:52 +02:00
public Dictionary<string, GroupMember> Participants { get; } =
2020-05-04 19:46:02 +02:00
new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
2020-04-01 17:52:42 +02:00
/// <summary>
2020-04-04 17:56:21 +02:00
/// Checks if a session is in this group.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-04-04 17:56:21 +02: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 17:52:42 +02:00
{
2020-04-21 23:37:37 +02:00
return Participants.ContainsKey(sessionId);
2020-04-01 17:52:42 +02:00
}
/// <summary>
2020-04-04 17:56:21 +02:00
/// Adds the session to the group.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-04-04 17:56:21 +02:00
/// <param name="session">The session.</param>
public void AddSession(SessionInfo session)
2020-04-01 17:52:42 +02:00
{
2020-05-09 14:34:07 +02:00
if (ContainsSession(session.Id.ToString()))
{
return;
}
2020-04-01 17:52:42 +02:00
var member = new GroupMember();
2020-04-04 17:56:21 +02:00
member.Session = session;
2020-04-01 17:52:42 +02:00
member.Ping = DefaulPing;
member.IsBuffering = false;
2020-04-21 23:37:37 +02:00
Participants[session.Id.ToString()] = member;
2020-04-01 17:52:42 +02:00
}
/// <summary>
2020-04-04 17:56:21 +02:00
/// Removes the session from the group.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-04-04 17:56:21 +02:00
/// <param name="session">The session.</param>
public void RemoveSession(SessionInfo session)
2020-04-01 17:52:42 +02:00
{
2020-05-09 14:34:07 +02:00
if (!ContainsSession(session.Id.ToString()))
{
return;
}
Participants.Remove(session.Id.ToString(), out _);
2020-04-01 17:52:42 +02:00
}
/// <summary>
2020-04-04 17:56:21 +02:00
/// Updates the ping of a session.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-04-04 17:56:21 +02:00
/// <param name="session">The session.</param>
2020-04-01 17:52:42 +02:00
/// <param name="ping">The ping.</param>
2020-04-04 17:56:21 +02:00
public void UpdatePing(SessionInfo session, long ping)
2020-04-01 17:52:42 +02:00
{
2020-05-09 14:34:07 +02:00
if (!ContainsSession(session.Id.ToString()))
{
return;
}
2020-04-21 23:37:37 +02:00
Participants[session.Id.ToString()].Ping = ping;
2020-04-01 17:52:42 +02:00
}
/// <summary>
/// Gets the highest ping in the group.
/// </summary>
2020-04-04 17:56:21 +02:00
/// <value name="session">The highest ping in the group.</value>
2020-04-01 17:52:42 +02:00
public long GetHighestPing()
{
long max = Int64.MinValue;
2020-04-21 23:37:37 +02:00
foreach (var session in Participants.Values)
2020-04-01 17:52:42 +02:00
{
2020-04-04 17:56:21 +02:00
max = Math.Max(max, session.Ping);
2020-04-01 17:52:42 +02:00
}
return max;
}
/// <summary>
2020-04-04 17:56:21 +02:00
/// Sets the session's buffering state.
2020-04-01 17:52:42 +02:00
/// </summary>
2020-04-04 17:56:21 +02:00
/// <param name="session">The session.</param>
2020-04-01 17:52:42 +02:00
/// <param name="isBuffering">The state.</param>
2020-04-04 17:56:21 +02:00
public void SetBuffering(SessionInfo session, bool isBuffering)
2020-04-01 17:52:42 +02:00
{
2020-05-09 14:34:07 +02:00
if (!ContainsSession(session.Id.ToString()))
{
return;
}
2020-04-21 23:37:37 +02:00
Participants[session.Id.ToString()].IsBuffering = isBuffering;
2020-04-01 17:52:42 +02:00
}
/// <summary>
/// Gets the group buffering state.
/// </summary>
2020-04-04 17:56:21 +02:00
/// <value><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</value>
2020-04-01 17:52:42 +02:00
public bool IsBuffering()
{
2020-04-21 23:37:37 +02:00
foreach (var session in Participants.Values)
2020-04-01 17:52:42 +02:00
{
2020-05-09 14:34:07 +02:00
if (session.IsBuffering)
{
return true;
}
2020-04-01 17:52:42 +02:00
}
2020-05-26 11:37:52 +02:00
2020-04-01 17:52:42 +02: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()
{
2020-04-21 23:37:37 +02:00
return Participants.Count == 0;
2020-04-01 17:52:42 +02:00
}
}
}