jellyfin/MediaBrowser.Controller/SyncPlay/GroupInfo.cs

170 lines
5.1 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>
2020-06-14 10:14:14 +00:00
/// Gets the default ping value used for sessions.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-05-26 09:37:52 +00:00
public long DefaulPing { get; } = 500;
2020-04-01 15:52:42 +00:00
/// <summary>
/// Gets or sets the group identifier.
/// </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>
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
{
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
{
2020-05-09 12:34:07 +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-21 21:37:37 +00:00
Participants[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>
public void RemoveSession(SessionInfo session)
2020-04-01 15:52:42 +00:00
{
2020-05-09 12:34:07 +00:00
if (!ContainsSession(session.Id.ToString()))
{
return;
}
Participants.Remove(session.Id.ToString(), out _);
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-05-09 12:34:07 +00:00
if (!ContainsSession(session.Id.ToString()))
{
return;
}
2020-04-21 21:37:37 +00:00
Participants[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-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
}
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-05-09 12:34:07 +00:00
if (!ContainsSession(session.Id.ToString()))
{
return;
}
2020-04-21 21:37:37 +00:00
Participants[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-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>
/// <value><c>true</c> if the group is empty; <c>false</c> otherwise.</value>
public bool IsEmpty()
{
2020-04-21 21:37:37 +00:00
return Participants.Count == 0;
2020-04-01 15:52:42 +00:00
}
}
}