-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParty.cs
488 lines (416 loc) · 17.5 KB
/
Party.cs
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Services;
using MediaBrowser.Model.Session;
using MediaBrowser.Model.System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Timers;
namespace EmbyParty
{
public class Party
{
private PartyManager _partyManager;
private ISessionManager _sessionManager;
private IUserManager _userManager;
private ILogger _logger;
private long _id;
private Dictionary<string, Attendee> _attendees = new Dictionary<string, Attendee>();
private Dictionary<string, Attendee> _attendeeTargetMap = new Dictionary<string, Attendee>(); //Map target to remote controller
private Random _tagCharacterRng = new Random();
public long Id { get => _id; }
public string Name { get; set; }
public long[] CurrentQueue { get; set; }
public int CurrentIndex { get; set; }
public string MediaSourceId { get; set; }
public int? AudioStreamIndex { get; set; }
public int? SubtitleStreamIndex { get; set; }
public long? PreviousItem { get; set; }
public bool NoUnpauseAfterSync { get; set; } //Store whether host sync is taking place from a starting paused or unpaused state
public bool InSyncConclusion { get; set; } //True between the final guest reporting playback and the end of the sync process (host pending unpause), prevents out of order pausing
public bool GuestPauseAction { get; set; }
public Timer LateClearHost { get; set; }
public Attendee Host { get => _attendees.Values.FirstOrDefault<Attendee>(attendee => attendee.IsHost); }
public Dictionary<string, Attendee>.ValueCollection Attendees { get => _attendees.Values; }
public IEnumerable<Attendee> AttendeePlayers { get => _attendees.Values.Where(attendee => !attendee.IsBeingRemoteControlled); }
public Dictionary<string, Attendee>.KeyCollection AttendeeTargets { get => _attendeeTargetMap.Keys; }
public IEnumerable<Attendee> AttendeesPaused { get => _attendees.Values.Where(attendee => attendee.IsPaused); }
public Party(long id, PartyManager partyManager, ISessionManager sessionManager, IUserManager userManager, ILogger logger)
{
_partyManager = partyManager;
_sessionManager = sessionManager;
_userManager = userManager;
_logger = logger;
_id = id;
}
public void Log(string message)
{
_logger.Info(message);
}
protected void LogWarning(string message)
{
_logger.Warn(message);
}
private string randomTag(int length)
{
string pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string result = "";
for (int i = 0; i < length; i++)
{
result += pool.Substring(_tagCharacterRng.Next(0, pool.Length), 1);
}
return result;
}
private string[] GenerateAlternativeNames(Attendee attendee)
{
return new string[] {
attendee.UserName + " - " + attendee.DeviceName,
attendee.UserName + " " + randomTag(2)
};
}
public Attendee AttendeeJoin(string sessionId, SessionInfo session)
{
if (_attendees.ContainsKey(sessionId)) { return null; }
Attendee newAttendee = new Attendee()
{
Id = sessionId,
Party = this,
UserId = session.UserId,
UserName = session.UserName,
DeviceName = session.DeviceName,
DeviceType = session.DeviceType,
DisplayName = session.UserName
};
//Resolve name collisions
string[] altNames = GenerateAlternativeNames(newAttendee);
int altNameIndex = 0;
Attendee currentDuplicate = null;
Attendee duplicate = GetAttendeeByDisplayName(newAttendee.DisplayName);
string oldDuplicateName = null;
while (duplicate != null)
{
newAttendee.DisplayName = altNames[altNameIndex];
string[] duplicateAlts = GenerateAlternativeNames(duplicate);
currentDuplicate = duplicate;
if (oldDuplicateName == null) { oldDuplicateName = currentDuplicate.DisplayName; }
currentDuplicate.DisplayName = duplicateAlts[altNameIndex];
duplicate = GetAttendeeByDisplayName(newAttendee.DisplayName);
if (altNameIndex < altNames.Length - 1) { altNameIndex++; }
}
if (oldDuplicateName != null)
{
GeneralCommand renameCommand = new GeneralCommand() { Name = "PartyUpdateName" };
renameCommand.Arguments["OldName"] = oldDuplicateName;
renameCommand.Arguments["NewName"] = currentDuplicate.DisplayName;
SendEventToAttendees(null, renameCommand);
}
_attendees.Add(sessionId, newAttendee);
//Remote control map
if (_attendeeTargetMap.ContainsKey(newAttendee.Id))
{
newAttendee.IsBeingRemoteControlled = true;
}
//Other stuff
User user = _userManager.GetUserById(session.UserId);
bool hasPicture = user.GetImages(ImageType.Primary).Count() > 0;
GeneralCommand command = new GeneralCommand() { Name = "PartyJoin" };
command.Arguments["UserId"] = newAttendee.UserId;
command.Arguments["HasPicture"] = hasPicture ? "true" : "false";
command.Arguments["Name"] = newAttendee.DisplayName;
command.Arguments["IsRemoteControlled"] = newAttendee.IsBeingRemoteControlled ? "true" : "false";
SendEventToAttendees(sessionId, command);
return newAttendee;
}
public bool SetRemoteControl(string sessionId, string remoteControl)
{
if (sessionId == null) { return false; }
if (sessionId == remoteControl) { remoteControl = null; }
Attendee attendee = GetAttendee(sessionId);
if (attendee == null) { return false; }
Log("Request set remote control in " + Name + ": " + sessionId + " to control " + remoteControl);
if (attendee.RemoteControl == remoteControl)
{
//sessionId is already controlling remoteControl
return true;
}
if (attendee.RemoteControl != null)
{
//Attendee loses control over its former RemoteControl session, since we're replacing it
Attendee formerTarget = GetAttendee(attendee.RemoteControl);
if (formerTarget != null)
{
formerTarget.IsBeingRemoteControlled = false;
Log("Former target " + formerTarget.Id + " is now marked as not remote controlled.");
GeneralCommand updateRemoteControlled = new GeneralCommand() { Name = "PartyUpdateRemoteControlled" };
updateRemoteControlled.Arguments["Name"] = formerTarget.DisplayName;
updateRemoteControlled.Arguments["Status"] = "false";
SendEventToAttendees(null, updateRemoteControlled);
}
if (_attendeeTargetMap.ContainsKey(attendee.RemoteControl))
{
_attendeeTargetMap.Remove(attendee.RemoteControl);
}
}
Attendee newTarget = null;
if (remoteControl != null)
{
//Attendee will control remoteControl
newTarget = GetAttendee(remoteControl);
if (newTarget != null)
{
newTarget.IsBeingRemoteControlled = true;
Log("New target " + newTarget.Id + " is now marked as remote controlled.");
GeneralCommand updateRemoteControlled = new GeneralCommand() { Name = "PartyUpdateRemoteControlled" };
updateRemoteControlled.Arguments["Name"] = newTarget.DisplayName;
updateRemoteControlled.Arguments["Status"] = "true";
SendEventToAttendees(null, updateRemoteControlled);
}
_attendeeTargetMap.Add(remoteControl, attendee);
}
attendee.RemoteControl = remoteControl;
//Log("Target map after operation: " + ControlMapForDisplay());
return true;
}
public Attendee AttendeePart(string sessionId)
{
Attendee attendee = _attendees[sessionId];
bool result = _attendees.Remove(sessionId);
foreach (string eachId in _attendeeTargetMap.Keys)
{
if (_attendeeTargetMap[eachId].Id == sessionId)
{
_attendeeTargetMap.Remove(eachId);
}
}
GeneralCommand command = new GeneralCommand() { Name = "PartyLeave" };
command.Arguments["Name"] = attendee.DisplayName;
command.Arguments["IsHosting"] = attendee.IsHost ? "true" : "false";
SendEventToAttendees(sessionId, command);
if (attendee.IsHost && _attendees.Count > 0)
{
_attendees.First().Value.IsHost = true;
GeneralCommand hostnameCommand = new GeneralCommand() { Name = "PartyUpdateHost" };
hostnameCommand.Arguments["Host"] = _attendees.First().Value.DisplayName;
SendEventToAttendees(null, hostnameCommand);
}
return result ? attendee : null;
}
public Attendee GetAttendee(string sessionId)
{
return _attendees.ContainsKey(sessionId) ? _attendees[sessionId] : null;
}
public string ControlMapForDisplay()
{
List<string> items = new List<string>();
foreach (string key in _attendeeTargetMap.Keys)
{
items.Add(key + " => " + _attendeeTargetMap[key].Id);
}
return String.Join(", ", items.ToArray());
}
public Attendee GetAttendeeByTarget(string sessionId)
{
while (_attendeeTargetMap.ContainsKey(sessionId))
{
sessionId = _attendeeTargetMap[sessionId].Id;
}
return GetAttendee(sessionId);
}
public Attendee GetAttendeeByDisplayName(string displayName)
{
foreach (Attendee attendee in _attendees.Values)
{
if (attendee.DisplayName.ToLower() == displayName.ToLower())
{
return attendee;
}
}
return null;
}
public List<User> GetAttendingUsers()
{
HashSet<string> userIds = new HashSet<string>();
foreach (Attendee attendee in _attendees.Values)
{
userIds.Add(attendee.UserId);
}
return userIds.ToList<string>().Select(userId => _userManager.GetUserById(userId)).ToList();
}
public void SetStates(AttendeeState state)
{
foreach (Attendee attendee in _attendees.Values)
{
attendee.State = state;
}
}
public void SetPlayerStates(AttendeeState? stateForPlayers, AttendeeState? stateForRemoteControlled)
{
foreach (Attendee attendee in _attendees.Values)
{
if (attendee.IsHost)
{
continue;
}
else if (!attendee.IsBeingRemoteControlled)
{
if (stateForPlayers != null)
{
attendee.State = (AttendeeState)stateForPlayers;
}
}
else if (stateForRemoteControlled != null)
{
attendee.State = (AttendeeState)stateForRemoteControlled;
}
}
}
public bool AreAllStates(AttendeeState state)
{
foreach (Attendee attendee in _attendees.Values)
{
if (attendee.State != state)
{
return false;
}
}
return true;
}
public bool AreAllInSync(Attendee reference)
{
foreach (Attendee attendee in _attendees.Values)
{
if (attendee.Id == reference.Id) { continue; }
if (attendee.IsOutOfSync(reference.EstimatedPositionTicks))
{
return false;
}
}
return true;
}
public void ResetPositionTicks(long? positionTicks)
{
foreach (Attendee attendee in _attendees.Values)
{
attendee.PositionTicks = (long)positionTicks;
}
}
public void IgnoreNext(ProgressEvent evt)
{
foreach (Attendee attendee in AttendeePlayers)
{
attendee.IgnoreNext(evt);
}
}
public void ClearIgnores()
{
foreach (Attendee attendee in _attendees.Values)
{
attendee.ClearIgnores();
attendee.IsPaused = false;
}
}
public void ClearOngoingInformation()
{
CurrentQueue = null;
CurrentIndex = 0;
MediaSourceId = null;
AudioStreamIndex = null;
SubtitleStreamIndex = null;
}
public void SetHost(Attendee attendee, bool state)
{
if (LateClearHost != null)
{
LateClearHost.Dispose();
LateClearHost = null;
}
if (attendee.IsHost != state)
{
attendee.IsHost = state;
GeneralCommand hostnameCommand = new GeneralCommand() { Name = "PartyUpdateHost" };
hostnameCommand.Arguments["Host"] = state ? attendee.DisplayName : "";
SendEventToAttendees(null, hostnameCommand);
}
}
public void ClearHost()
{
Attendee host = Host;
if (host == null) { return; }
SetHost(host, false);
PreviousItem = null;
ClearOngoingInformation();
ClearIgnores();
//Just to be sure
PlaystateRequest stop = new PlaystateRequest() { Command = PlaystateCommand.Stop };
foreach (Attendee eachAttendee in Attendees)
{
eachAttendee.State = AttendeeState.Idle;
eachAttendee.ResetAccuracy();
eachAttendee.CurrentMediaItemId = null;
if (eachAttendee.Id == host.Id) { continue; }
_partyManager.SendPlaystateCommandToAttendeeTarget(eachAttendee, stop);
}
}
public void SendEventToAttendees(string except, GeneralCommand request)
{
foreach (Attendee attendee in Attendees)
{
if (except != null && attendee.Id == except) { continue; }
try
{
_sessionManager.SendGeneralCommand(null, attendee.Id, request, new System.Threading.CancellationToken());
}
catch
{
LogWarning("Failed to send General command (event) to " + attendee.TargetId + ": " + request.Name);
}
}
}
public void SendEventToSingleAttendee(Attendee attendee, GeneralCommand request)
{
if (attendee == null || attendee.Party != this) { return; }
try
{
_sessionManager.SendGeneralCommand(null, attendee.Id, request, new System.Threading.CancellationToken());
}
catch
{
LogWarning("Failed to send General command (event) to " + attendee.TargetId + ": " + request.Name);
}
}
public void SendSyncStart()
{
SendEventToAttendees(null, new GeneralCommand() { Name = "PartySyncStart" });
}
public void SendSyncWaiting(Attendee attendee)
{
GeneralCommand waiting = new GeneralCommand() { Name = "PartySyncWaiting" };
waiting.Arguments["Name"] = attendee.DisplayName;
SendEventToAttendees(null, waiting);
}
public void SendSyncReset(Attendee attendee)
{
GeneralCommand reset = new GeneralCommand() { Name = "PartySyncReset" };
reset.Arguments["Name"] = attendee.DisplayName;
SendEventToAttendees(null, reset);
}
public void SendSyncEnd()
{
SendEventToAttendees(null, new GeneralCommand() { Name = "PartySyncEnd" });
}
public void SendLogMessageToAttendees(string type, string subject)
{
GeneralCommand logMessage = new GeneralCommand() { Name = "PartyLogMessage" };
logMessage.Arguments["Type"] = type;
logMessage.Arguments["Subject"] = subject;
SendEventToAttendees(null, logMessage);
_partyManager.BroadcastToBridges("GeneralCommand", Name, logMessage);
}
}
}