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

161 lines
4.9 KiB
C#
Raw Normal View History

2020-04-01 15:52:42 +00:00
using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Session;
2020-05-06 21:42:53 +00:00
namespace MediaBrowser.Controller.SyncPlay
2020-04-01 15:52:42 +00:00
{
/// <summary>
/// Class GroupInfo.
/// </summary>
2020-05-04 17:46:02 +00:00
/// <remarks>
/// Class is not thread-safe, external locking is required when accessing methods.
/// </remarks>
2020-04-01 15:52:42 +00:00
public class GroupInfo
{
/// <summary>
/// The default ping value used for sessions.
2020-04-01 15:52:42 +00:00
/// </summary>
public const long DefaultPing = 500;
2020-05-26 09:37:52 +00:00
2020-04-01 15:52:42 +00:00
/// <summary>
/// Gets the group identifier.
2020-04-01 15:52:42 +00:00
/// </summary>
/// <value>The group identifier.</value>
2020-05-26 09:37:52 +00:00
public Guid GroupId { get; } = Guid.NewGuid();
2020-04-01 15:52:42 +00:00
/// <summary>
/// Gets or sets the playing item.
/// </summary>
/// <value>The playing item.</value>
public BaseItem PlayingItem { get; set; }
/// <summary>
2020-06-14 10:14:14 +00:00
/// Gets or sets a value indicating whether playback is paused.
2020-04-01 15:52:42 +00:00
/// </summary>
/// <value>Playback is paused.</value>
public bool IsPaused { get; set; }
/// <summary>
2020-06-14 10:14:14 +00:00
/// Gets or sets a value indicating whether there are position ticks.
2020-04-01 15:52:42 +00:00
/// </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 21:37:37 +00:00
/// Gets the participants.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-04-21 21:37:37 +00:00
/// <value>The participants, or members of the group.</value>
2020-05-26 09:37:52 +00:00
public Dictionary<string, GroupMember> Participants { get; } =
2020-05-04 17:46:02 +00:00
new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
2020-04-01 15:52:42 +00:00
/// <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>
/// <param name="sessionId">The session id to check.</param>
/// <returns><c>true</c> if the session is in this group; <c>false</c> otherwise.</returns>
2020-04-04 15:56:21 +00:00
public bool ContainsSession(string sessionId)
2020-04-01 15:52:42 +00:00
{
2020-04-21 21:37:37 +00:00
return Participants.ContainsKey(sessionId);
2020-04-01 15:52:42 +00:00
}
/// <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
{
Participants.TryAdd(
session.Id,
new GroupMember
{
Session = session,
Ping = DefaultPing,
IsBuffering = false
});
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>
public void RemoveSession(SessionInfo session)
2020-04-01 15:52:42 +00:00
{
Participants.Remove(session.Id);
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
{
if (Participants.TryGetValue(session.Id, out GroupMember value))
2020-05-09 12:34:07 +00:00
{
value.Ping = ping;
2020-05-09 12:34:07 +00:00
}
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Gets the highest ping in the group.
/// </summary>
/// <returns>The highest ping in the group.</returns>
2020-04-01 15:52:42 +00:00
public long GetHighestPing()
{
long max = long.MinValue;
2020-04-21 21:37:37 +00:00
foreach (var session in Participants.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
}
2020-06-15 21:43:52 +00:00
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
{
if (Participants.TryGetValue(session.Id, out GroupMember value))
2020-05-09 12:34:07 +00:00
{
value.IsBuffering = isBuffering;
2020-05-09 12:34:07 +00:00
}
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Gets the group buffering state.
/// </summary>
/// <returns><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</returns>
2020-04-01 15:52:42 +00:00
public bool IsBuffering()
{
2020-04-21 21:37:37 +00:00
foreach (var session in Participants.Values)
2020-04-01 15:52:42 +00:00
{
2020-05-09 12:34:07 +00:00
if (session.IsBuffering)
{
return true;
}
2020-04-01 15:52:42 +00:00
}
2020-05-26 09:37:52 +00:00
2020-04-01 15:52:42 +00:00
return false;
}
/// <summary>
/// Checks if the group is empty.
/// </summary>
/// <returns><c>true</c> if the group is empty; <c>false</c> otherwise.</returns>
2020-04-01 15:52:42 +00:00
public bool IsEmpty()
{
2020-04-21 21:37:37 +00:00
return Participants.Count == 0;
2020-04-01 15:52:42 +00:00
}
}
}