1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Session;
namespace MediaBrowser.Controller.SyncPlay
{
/// <summary>
/// Class GroupInfo.
/// </summary>
/// <remarks>
/// Class is not thread-safe, external locking is required when accessing methods.
/// </remarks>
public class GroupInfo
{
/// <summary>
/// The default ping value used for sessions.
/// </summary>
public const long DefaultPing = 500;
/// <summary>
/// Gets the group identifier.
/// </summary>
/// <value>The group identifier.</value>
public Guid GroupId { get; } = Guid.NewGuid();
/// <summary>
/// Gets or sets the playing item.
/// </summary>
/// <value>The playing item.</value>
public BaseItem PlayingItem { get; set; }
/// <summary>
/// Gets or sets a value indicating whether playback is paused.
/// </summary>
/// <value>Playback is paused.</value>
public bool IsPaused { get; set; }
/// <summary>
/// Gets or sets a value indicating whether there are 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 participants.
/// </summary>
/// <value>The participants, or members of the group.</value>
public Dictionary<string, GroupMember> Participants { get; } =
new Dictionary<string, GroupMember>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Checks if a session is in this group.
/// </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>
public bool ContainsSession(string sessionId)
{
return Participants.ContainsKey(sessionId);
}
/// <summary>
/// Adds the session to the group.
/// </summary>
/// <param name="session">The session.</param>
public void AddSession(SessionInfo session)
{
Participants.TryAdd(
session.Id,
new GroupMember
{
Session = session,
Ping = DefaultPing,
IsBuffering = false
});
}
/// <summary>
/// Removes the session from the group.
/// </summary>
/// <param name="session">The session.</param>
public void RemoveSession(SessionInfo session)
{
Participants.Remove(session.Id);
}
/// <summary>
/// Updates the ping of a session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="ping">The ping.</param>
public void UpdatePing(SessionInfo session, long ping)
{
if (Participants.TryGetValue(session.Id, out GroupMember value))
{
value.Ping = ping;
}
}
/// <summary>
/// Gets the highest ping in the group.
/// </summary>
/// <returns>The highest ping in the group.</returns>
public long GetHighestPing()
{
long max = long.MinValue;
foreach (var session in Participants.Values)
{
max = Math.Max(max, session.Ping);
}
return max;
}
/// <summary>
/// Sets the session's buffering state.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="isBuffering">The state.</param>
public void SetBuffering(SessionInfo session, bool isBuffering)
{
if (Participants.TryGetValue(session.Id, out GroupMember value))
{
value.IsBuffering = isBuffering;
}
}
/// <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>
public bool IsBuffering()
{
foreach (var session in Participants.Values)
{
if (session.IsBuffering)
{
return true;
}
}
return false;
}
/// <summary>
/// Checks if the group is empty.
/// </summary>
/// <returns><c>true</c> if the group is empty; <c>false</c> otherwise.</returns>
public bool IsEmpty()
{
return Participants.Count == 0;
}
}
}
|