Merge pull request #4268 from Bond-009/groupinfo

Improve GroupInfo class
This commit is contained in:
Joshua M. Boniface 2020-10-05 13:18:17 -04:00 committed by GitHub
commit 2d689f101b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 36 deletions

View File

@ -301,8 +301,7 @@ namespace Emby.Server.Implementations.SyncPlay
if (_group.IsPaused) if (_group.IsPaused)
{ {
// Pick a suitable time that accounts for latency // Pick a suitable time that accounts for latency
var delay = _group.GetHighestPing() * 2; var delay = Math.Max(_group.GetHighestPing() * 2, GroupInfo.DefaultPing);
delay = delay < _group.DefaultPing ? _group.DefaultPing : delay;
// Unpause group and set starting point in future // Unpause group and set starting point in future
// Clients will start playback at LastActivity (datetime) from PositionTicks (playback position) // Clients will start playback at LastActivity (datetime) from PositionTicks (playback position)
@ -452,8 +451,7 @@ namespace Emby.Server.Implementations.SyncPlay
else else
{ {
// Client, that was buffering, resumed playback but did not update others in time // Client, that was buffering, resumed playback but did not update others in time
delay = _group.GetHighestPing() * 2; delay = Math.Max(_group.GetHighestPing() * 2, GroupInfo.DefaultPing);
delay = delay < _group.DefaultPing ? _group.DefaultPing : delay;
_group.LastActivity = currentTime.AddMilliseconds( _group.LastActivity = currentTime.AddMilliseconds(
delay); delay);
@ -497,7 +495,7 @@ namespace Emby.Server.Implementations.SyncPlay
private void HandlePingUpdateRequest(SessionInfo session, PlaybackRequest request) private void HandlePingUpdateRequest(SessionInfo session, PlaybackRequest request)
{ {
// Collected pings are used to account for network latency when unpausing playback // Collected pings are used to account for network latency when unpausing playback
_group.UpdatePing(session, request.Ping ?? _group.DefaultPing); _group.UpdatePing(session, request.Ping ?? GroupInfo.DefaultPing);
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -14,12 +14,12 @@ namespace MediaBrowser.Controller.SyncPlay
public class GroupInfo public class GroupInfo
{ {
/// <summary> /// <summary>
/// Gets the default ping value used for sessions. /// The default ping value used for sessions.
/// </summary> /// </summary>
public long DefaultPing { get; } = 500; public const long DefaultPing = 500;
/// <summary> /// <summary>
/// Gets or sets the group identifier. /// Gets the group identifier.
/// </summary> /// </summary>
/// <value>The group identifier.</value> /// <value>The group identifier.</value>
public Guid GroupId { get; } = Guid.NewGuid(); public Guid GroupId { get; } = Guid.NewGuid();
@ -58,7 +58,8 @@ namespace MediaBrowser.Controller.SyncPlay
/// <summary> /// <summary>
/// Checks if a session is in this group. /// Checks if a session is in this group.
/// </summary> /// </summary>
/// <value><c>true</c> if the session is in this group; <c>false</c> otherwise.</value> /// <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>
public bool ContainsSession(string sessionId) public bool ContainsSession(string sessionId)
{ {
return Participants.ContainsKey(sessionId); return Participants.ContainsKey(sessionId);
@ -70,16 +71,14 @@ namespace MediaBrowser.Controller.SyncPlay
/// <param name="session">The session.</param> /// <param name="session">The session.</param>
public void AddSession(SessionInfo session) public void AddSession(SessionInfo session)
{ {
if (ContainsSession(session.Id)) Participants.TryAdd(
{ session.Id,
return; new GroupMember
} {
Session = session,
var member = new GroupMember(); Ping = DefaultPing,
member.Session = session; IsBuffering = false
member.Ping = DefaultPing; });
member.IsBuffering = false;
Participants[session.Id] = member;
} }
/// <summary> /// <summary>
@ -88,12 +87,7 @@ namespace MediaBrowser.Controller.SyncPlay
/// <param name="session">The session.</param> /// <param name="session">The session.</param>
public void RemoveSession(SessionInfo session) public void RemoveSession(SessionInfo session)
{ {
if (!ContainsSession(session.Id)) Participants.Remove(session.Id);
{
return;
}
Participants.Remove(session.Id, out _);
} }
/// <summary> /// <summary>
@ -103,18 +97,16 @@ namespace MediaBrowser.Controller.SyncPlay
/// <param name="ping">The ping.</param> /// <param name="ping">The ping.</param>
public void UpdatePing(SessionInfo session, long ping) public void UpdatePing(SessionInfo session, long ping)
{ {
if (!ContainsSession(session.Id)) if (Participants.TryGetValue(session.Id, out GroupMember value))
{ {
return; value.Ping = ping;
} }
Participants[session.Id].Ping = ping;
} }
/// <summary> /// <summary>
/// Gets the highest ping in the group. /// Gets the highest ping in the group.
/// </summary> /// </summary>
/// <value name="session">The highest ping in the group.</value> /// <returns>The highest ping in the group.</returns>
public long GetHighestPing() public long GetHighestPing()
{ {
long max = long.MinValue; long max = long.MinValue;
@ -133,18 +125,16 @@ namespace MediaBrowser.Controller.SyncPlay
/// <param name="isBuffering">The state.</param> /// <param name="isBuffering">The state.</param>
public void SetBuffering(SessionInfo session, bool isBuffering) public void SetBuffering(SessionInfo session, bool isBuffering)
{ {
if (!ContainsSession(session.Id)) if (Participants.TryGetValue(session.Id, out GroupMember value))
{ {
return; value.IsBuffering = isBuffering;
} }
Participants[session.Id].IsBuffering = isBuffering;
} }
/// <summary> /// <summary>
/// Gets the group buffering state. /// Gets the group buffering state.
/// </summary> /// </summary>
/// <value><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</value> /// <returns><c>true</c> if there is a session buffering in the group; <c>false</c> otherwise.</returns>
public bool IsBuffering() public bool IsBuffering()
{ {
foreach (var session in Participants.Values) foreach (var session in Participants.Values)
@ -161,7 +151,7 @@ namespace MediaBrowser.Controller.SyncPlay
/// <summary> /// <summary>
/// Checks if the group is empty. /// Checks if the group is empty.
/// </summary> /// </summary>
/// <value><c>true</c> if the group is empty; <c>false</c> otherwise.</value> /// <returns><c>true</c> if the group is empty; <c>false</c> otherwise.</returns>
public bool IsEmpty() public bool IsEmpty()
{ {
return Participants.Count == 0; return Participants.Count == 0;