-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServerEntryPoint.cs
437 lines (364 loc) · 16.6 KB
/
ServerEntryPoint.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Session;
namespace ComSkipper
{
public class ServerEntryPoint : IServerEntryPoint
{
private readonly List<EdlSequence> commercialList = new List<EdlSequence>();
private readonly List<EdlTimestamp> timestamps = new List<EdlTimestamp>();
private ISessionManager SessionManager { get; set; }
private IUserManager UserManager { get; set; }
private IServerConfigurationManager ConfigManager { get; set; }
private ILogger Log { get; set; }
private IItemRepository ItemRepository { get; set; }
private ILibraryManager LibraryManager { get; }
private string Locale = string.Empty;
private bool usingChapters = false;
public ServerEntryPoint(ISessionManager sessionManager, IUserManager userManager, ILogManager logManager, IServerConfigurationManager configManager, IItemRepository itemRepo, ILibraryManager libraryManager)
{
SessionManager = sessionManager;
UserManager = userManager;
ConfigManager = configManager;
ItemRepository = itemRepo;
LibraryManager = libraryManager;
Log = logManager.GetLogger(Plugin.Instance.Name);
}
public void Dispose()
{
SessionManager.PlaybackStart -= PlaybackStart;
SessionManager.PlaybackStopped -= PlaybackStopped;
SessionManager.PlaybackProgress -= PlaybackProgress;
}
public void Run()
{
// TODO: When Emby adds clients locale to the Session object, use that instead of the servers locale below.
Locale = ConfigManager.Configuration.UICulture;
Log.Debug("Locale = " + Locale);
SessionManager.PlaybackStart += PlaybackStart;
SessionManager.PlaybackStopped += PlaybackStopped;
SessionManager.PlaybackProgress += PlaybackProgress;
Plugin.Instance.UpdateConfiguration(Plugin.Instance.Configuration);
}
/// <summary>
/// Executed on a playback started Emby event. Read the EDL file and add to commercialList.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PlaybackStart(object sender, PlaybackProgressEventArgs e)
{
if (Plugin.Instance.Configuration.EnableComSkipper == false)
{
Log.Debug("PlaybackStart: Plugin is disabled.");
return;
}
if (string.IsNullOrEmpty(e.MediaInfo.Path))
{
Log.Debug("PlaybackStart: No MediaInfo.Path. Nothing to process.");
return;
}
string filePath = e.MediaInfo.Path;
string session = e.Session.Id;
Log.Debug("Playback Session = " + session + " Path = " + filePath);
// Remove any stragglers
RemoveFromList(session);
AddTimestamp(session);
usingChapters = false;
if (ReadEdlFile(e) == false)
{
Log.Debug("No usable EDL file found. Looking for chapter commercial points.");
usingChapters = ReadChapters(e);
}
}
/// <summary>
/// Executed on a playback prorgress Emby event. See if it is in a identified commercial and skip if it is.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PlaybackProgress(object sender, PlaybackProgressEventArgs e)
{
if (Plugin.Instance.Configuration.EnableComSkipper == false)
return;
if (e.Session.PlayState.IsPaused || !e.PlaybackPositionTicks.HasValue)
return;
string session = e.Session.Id;
// This should allow for people who are watching as it is being recorded to skip commercials
if (usingChapters == false && e.Item.IsActiveRecording() == true && Plugin.Instance.Configuration.RealTimeEnabled == true)
{
// Reload EDL info every minute
long ns = DateTimeOffset.Now.ToUnixTimeSeconds();
EdlTimestamp tsfound = timestamps.Find(x => x.sessionId == session);
if (tsfound != null)
{
if ((ns - tsfound.timeLoaded) >= 60)
{
RemoveFromList(session);
Log.Debug("Reloading EDL data for Session " + tsfound.sessionId);
ReadEdlFile(e);
AddTimestamp(session);
}
}
}
long playbackPositionTicks = e.PlaybackPositionTicks.Value;
EdlSequence found = commercialList.Find(x => x.sessionId == session && x.skipped == false && playbackPositionTicks >= x.startTicks && playbackPositionTicks < (x.endTicks - 1000));
if (found != null)
{
string controlSession = (e.Session.SupportsRemoteControl)
? e.Session.Id
: SessionManager.Sessions.Where(i => i.DeviceId == e.Session.DeviceId && i.SupportsRemoteControl).FirstOrDefault().Id;
if (string.IsNullOrEmpty(controlSession))
{
Log.Debug($"No control session for SessionID {e.Session.Id}");
return;
}
found.skipped = true;
SkipCommercial(controlSession, found.endTicks);
if (Plugin.Instance.Configuration.DisableMessage == false && e.Session.Capabilities.SupportedCommands.Contains("DisplayMessage"))
SendMessageToClient(controlSession, (found.endTicks - found.startTicks) / TimeSpan.TicksPerSecond);
Log.Info("Skipping commercial. Session: " + session + " Start = " + found.startTicks.ToString() + " End = " + found.endTicks.ToString());
}
}
/// <summary>
/// Executed on a playback stopped Emby event. Remove the commercialList entries for the session.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PlaybackStopped(object sender, PlaybackStopEventArgs e)
{
if (Plugin.Instance.Configuration.EnableComSkipper == false)
return;
string name = e.MediaInfo.Name;
string sessionID = e.Session.Id;
Log.Debug("Playback Stopped. Session = " + sessionID + " Name = " + name);
RemoveFromList(sessionID);
}
/// <summary>
/// Read and process the comskip EDL file
/// </summary>
/// <param name="e"></param>
private bool ReadEdlFile(PlaybackProgressEventArgs e)
{
string filePath = e.MediaInfo.Path;
string session = e.Session.Id;
string edlFile = Path.ChangeExtension(filePath, ".edl");
Log.Debug("Media File: " + filePath + " EDL file " + edlFile);
// Check for edl file and load skip list if found
// Seconds to ticks = seconds * TimeSpan.TicksPerSecond
if (!File.Exists(edlFile))
{
Log.Debug($"Comskip EDL file [{edlFile}] does not exist.");
return false;
}
// Remove any stragglers
lock (commercialList)
{
commercialList.RemoveAll(x => x.sessionId == session);
}
Log.Info($"EDL file {edlFile} found.");
List<EdlSequence> commTempList = new List<EdlSequence>();
try
{
string line;
using (StreamReader reader = File.OpenText(edlFile))
{
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split('\t');
Log.Debug("parts " + parts[0] + " " + parts[1] + " " + parts[2]);
// 1 indicates it is meant to mute audio, not skip
if (parts[2] != "1")
{
EdlSequence seq = new EdlSequence();
seq.sessionId = session;
seq.startTicks = (long)(double.Parse(parts[0], CultureInfo.GetCultureInfo("en-US")) * (double)TimeSpan.TicksPerSecond);
if (seq.startTicks < TimeSpan.TicksPerSecond)
seq.startTicks = TimeSpan.TicksPerSecond;
seq.endTicks = (long)(double.Parse(parts[1], CultureInfo.GetCultureInfo("en-US")) * (double)TimeSpan.TicksPerSecond);
commTempList.Add(seq);
}
}
}
}
catch (Exception ex)
{
Log.Error("Could not parse EDL file " + edlFile + ". Exception: " + ex.Message);
return false;
}
lock (commercialList)
{
commercialList.AddRange(commTempList);
}
Log.Debug("Commercial List in seconds for " + e.MediaInfo.Name + ":");
foreach (EdlSequence s in commTempList)
{
Log.Debug("Start: " + (s.startTicks / TimeSpan.TicksPerSecond).ToString() + " End: " + (s.endTicks / TimeSpan.TicksPerSecond).ToString());
}
return true;
}
/// <summary>
/// Get commercial times out of the chapters
/// </summary>
/// <param name="e"></param>
private bool ReadChapters(PlaybackProgressEventArgs e)
{
string filePath = e.MediaInfo.Path;
string session = e.Session.Id;
Log.Debug($"Chapters ... Media File: {filePath}");
long id = e.Item.InternalId;
var item = LibraryManager.GetItemById(id);
List<ChapterInfo> chapters = ItemRepository.GetChapters(item);
if (chapters.Count == 0)
{
Log.Debug("No chapters found.");
return false;
}
foreach (ChapterInfo chapter in chapters)
{
Log.Debug($"Name: {chapter.Name} StartPositionTicks = {chapter.StartPositionTicks.ToString()}");
}
List<EdlSequence> commTempList = new List<EdlSequence>();
// Remove any stragglers
lock (commercialList)
{
commercialList.RemoveAll(x => x.sessionId == session);
}
int chapterNumber = 0;
foreach (ChapterInfo chapter in chapters)
{
chapterNumber++;
if (chapter.Name.ToLower() == "advertisement" && chapterNumber < chapters.Count)
{
EdlSequence seq = new EdlSequence();
seq.sessionId = session;
seq.startTicks = chapter.StartPositionTicks;
if (seq.startTicks < TimeSpan.TicksPerSecond)
seq.startTicks = TimeSpan.TicksPerSecond;
seq.endTicks = chapters[chapterNumber].StartPositionTicks;
commTempList.Add(seq);
}
}
if (commTempList.Count == 0)
{
Log.Debug("No chapters that are advertisements found.");
return false;
}
lock (commercialList)
{
commercialList.AddRange(commTempList);
}
Log.Debug("Commercial List (from chapters) in seconds for " + e.MediaInfo.Name + ":");
foreach (EdlSequence s in commTempList)
{
Log.Debug("Start: " + (s.startTicks / TimeSpan.TicksPerSecond).ToString() + " End: " + (s.endTicks / TimeSpan.TicksPerSecond).ToString());
}
return true;
}
/// <summary>
/// Remove a session from various lists
/// </summary>
/// <param name="sessionID"></param>
private void RemoveFromList(string sessionID)
{
if (Plugin.Instance.Configuration.RealTimeEnabled == true)
{
// Remove all items in timestamp list with this session ID
lock (timestamps)
{
timestamps.RemoveAll(x => x.sessionId == sessionID);
}
}
// Remove all items in skip list with this session ID
lock (commercialList)
{
commercialList.RemoveAll(x => x.sessionId == sessionID);
}
}
/// <summary>
/// Add timestamp to the list for a session
/// </summary>
/// <param name="sessionId"></param>
private void AddTimestamp(string sessionId)
{
if (Plugin.Instance.Configuration.RealTimeEnabled == true)
{
lock (timestamps)
{
EdlTimestamp ts = new EdlTimestamp();
ts.sessionId = sessionId;
ts.timeLoaded = DateTimeOffset.Now.ToUnixTimeSeconds();
timestamps.Add(ts);
}
}
}
/// <summary>
/// Skip the commercial for the given session by seeking to the end of the commercial.
/// </summary>
/// <param name="sessionID"></param>
/// <param name="seek"></param>
private void SkipCommercial(string sessionID, long seek)
{
PlaystateRequest playstateRequest = new PlaystateRequest();
playstateRequest.Command = PlaystateCommand.Seek;
UserQuery userListQuery = new UserQuery();
userListQuery.IsAdministrator = true;
playstateRequest.ControllingUserId = this.UserManager.GetUserList(userListQuery).FirstOrDefault().Id.ToString();
playstateRequest.SeekPositionTicks = new long?(seek);
SessionManager.SendPlaystateCommand((string)null, sessionID, playstateRequest, CancellationToken.None);
}
/// <summary>
/// Send Commercial Skipped message to client
/// </summary>
/// <param name="sessionID"></param>
/// <param name="duration"></param>
private async void SendMessageToClient(string sessionID, long duration)
{
try
{
string message = Plugin.Instance.Configuration.MainMessageText;
if (Plugin.Instance.Configuration.ShowTimeInMessage == true)
{
TimeSpan ts = TimeSpan.FromSeconds(duration);
message = message + " (" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString("00") + ")";
}
MessageCommand messageCommand = new MessageCommand();
messageCommand.Header = String.Empty;
messageCommand.Text = Localize.localize(message, Locale);
messageCommand.TimeoutMs = new long?((long)(Plugin.Instance.Configuration.MessageDisplayTimeSeconds * 1000));
await SessionManager.SendMessageCommand(sessionID, sessionID, messageCommand, CancellationToken.None);
}
catch { }
}
}
/// <summary>
/// EDL file representation
/// </summary>
public class EdlSequence
{
public string sessionId { get; set; }
public bool skipped { get; set; } = false;
public long startTicks { get; set; }
public long endTicks { get; set; }
}
/// <summary>
/// EDL timestamp
/// </summary>
public class EdlTimestamp
{
public long timeLoaded { get; set; }
public string sessionId { get; set; }
}
}