forked from ROMaster2/LiveSplit.VideoAutoSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.cs
More file actions
699 lines (634 loc) · 24.9 KB
/
Scanner.cs
File metadata and controls
699 lines (634 loc) · 24.9 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
using Accord.Video;
using Accord.Video.DirectShow;
using ImageMagick;
using LiveSplit.Model;
using LiveSplit.VAS.Models;
using LiveSplit.VAS.Models.Delta;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace LiveSplit.VAS
{
public class Scanner : IDisposable
{
private VASComponent _Component;
private Thread _FrameHandlerThread;
private GameProfile _GameProfile => _Component.GameProfile;
private string _VideoDevice => _Component.VideoDevice;
private VideoCaptureDevice _VideoSource;
private NewFrameEventHandler _NewFrameEventHandler;
private VideoSourceErrorEventHandler _VideoSourceErrorEventHandler;
public CompiledFeatures CompiledFeatures { get; private set; }
public DeltaManager DeltaManager { get; private set; }
public Frame CurrentFrame = Frame.Blank;
public int CurrentIndex = 0;
public bool IsScannerLocked = false;
public int ScanningCount = 0;
public bool IsScanning { get { return ScanningCount > 0; } }
public bool Restarting { get; set; } = false;
public int OverloadCount = 0;
internal int InitCount = 0; // To stop wasting CPU when first starting.
public event EventHandler<Scan> ScanFinished;
public event EventHandler<DeltaOutput> NewResult;
// Todo: Add something for downscaling before comparing for large images.
public Scanner(VASComponent component)
{
_Component = component;
_VideoSource = new VideoCaptureDevice();
_NewFrameEventHandler = HandleNewFrame;
_VideoSourceErrorEventHandler = HandleVideoError;
_CropGeometry = new Geometry(640, 480);
}
private Geometry _VideoGeometry = Geometry.Blank;
public Geometry VideoGeometry
{
get
{
if (!_VideoGeometry.HasSize)
{
try
{
if (!IsVideoSourceRunning() && IsVideoSourceValid())
{
_VideoSource.Source = DeviceMoniker;
_VideoSource.Start();
}
if (IsVideoSourceRunning())
{
_VideoSource.NewFrame += SetFrameSize;
}
}
catch (Exception e)
{
Log.Error(e, "Couldn't obtain video Geometry.");
}
}
return _VideoGeometry;
}
}
// Hacky but it saves on CPU for the scanner.
private void SetFrameSize(object sender, NewFrameEventArgs e)
{
if (!_VideoGeometry.HasSize)
{
_VideoGeometry = new Geometry(e.Frame.Size.ToWindows());
_VideoSource.NewFrame -= SetFrameSize;
}
}
private Geometry _CropGeometry = Geometry.Blank;
public Geometry CropGeometry
{
get
{
if (!_CropGeometry.HasSize)
{
_CropGeometry = VideoGeometry;
}
return _CropGeometry;
}
set
{
if (_CropGeometry != value)
{
_CropGeometry = value;
UpdateCropGeometry();
}
}
}
// Bad name.
// Not fully implemented yet.
private Geometry _TrueCropGeometry = Geometry.Blank;
public Geometry TrueCropGeometry
{
get
{
if (!_TrueCropGeometry.HasSize)
{
if (_GameProfile != null)
{
double x = 32768d;
double y = 32768d;
double width = -32768d;
double height = -32768d;
foreach (var wz in _GameProfile.Screens[0].WatchZones)
{
var geo = wz.Geometry;
geo.RemoveAnchor(wz.Screen.Geometry);
x = Math.Min(x, geo.X);
y = Math.Min(y, geo.Y);
width = Math.Max(width, geo.X + geo.Width);
height = Math.Max(height, geo.Y + geo.Height);
}
width -= x;
height -= y;
var sGeo = new Geometry(x, y, width, height);
sGeo.ResizeTo(CropGeometry, _GameProfile.Screens[0].Geometry);
sGeo.Adjust(CropGeometry.X, CropGeometry.Y);
_TrueCropGeometry = sGeo;
}
else
{
_TrueCropGeometry = CropGeometry;
}
}
return _TrueCropGeometry;
}
}
public double ManuallySetFPS { get; set; } = -1;
public double AverageFPS { get; private set; } = 60; // Assume 60 so that the start of the VASL script doesn't go haywire.
public double RecentMinFPS { get; private set; } = double.MaxValue;
public double RecentMaxFPS { get; private set; } = double.Epsilon;
public double MinFPS { get; private set; } = double.MaxValue;
public double MaxFPS { get; private set; } = double.Epsilon;
public double AverageScanTime { get; private set; } = 0;
public double MinScanTime { get; private set; } = 0;
public double MaxScanTime { get; private set; } = 0;
public double AverageWaitTime { get; private set; } = 0;
public double MinWaitTime { get; private set; } = 0;
public double MaxWaitTime { get; private set; } = 0;
public double CurrentFPS
{
get
{
const double MULTIPLIER_RANGE = 0.05;
const double LOWER_MULTIPLIER = 1 - MULTIPLIER_RANGE;
const double UPPER_MULTIPLIER = 1 / LOWER_MULTIPLIER;
if (ManuallySetFPS > 1
&& ManuallySetFPS > AverageFPS * LOWER_MULTIPLIER
&& ManuallySetFPS < AverageFPS * UPPER_MULTIPLIER)
return ManuallySetFPS;
else
return AverageFPS;
}
}
public bool IsVideoSourceValid()
{
var v = Regex.Match(_VideoDevice, "@device.*}");
return v.Success && !string.IsNullOrEmpty(new FilterInfo(v.Value).Name);
}
public string DeviceMoniker
{
get
{
if (IsVideoSourceValid())
{
return Regex.Match(_VideoDevice, "@device.*}").Value;
}
else
{
return null;
}
}
}
public bool IsVideoSourceRunning()
{
return _VideoSource.IsRunning;
}
public void SubscribeToFrameHandler(EventHandler<Scan> method)
{
ScanFinished += method;
}
public void UnsubscribeFromFrameHandler(EventHandler<Scan> method)
{
ScanFinished -= method;
}
public Geometry ResetCropGeometry()
{
_CropGeometry = Geometry.Blank;
UpdateCropGeometry();
return CropGeometry;
}
public void Stop()
{
Log.Info("Stopping scanner...");
try
{
if (_FrameHandlerThread != null)
{
Log.Verbose("Stopping Frame Handler thread...");
_FrameHandlerThread.Abort();
Log.Verbose("Frame Handler thread stopped.");
}
else
{
Log.Verbose("Frame Handler thread never existed, ignoring.");
}
if (_VideoSource != null)
{
_VideoSource.SignalToStop();
_VideoSource.NewFrame -= _NewFrameEventHandler;
_VideoSource.VideoSourceError -= _VideoSourceErrorEventHandler;
}
else
{
Log.Verbose("Video source was never set, ignoring.");
}
Log.Verbose("Resetting scanner variables...");
CurrentIndex = 0;
OverloadCount = 0;
MinFPS = double.MaxValue;
MaxFPS = double.Epsilon;
DeltaManager = null;
_VideoGeometry = Geometry.Blank;
_TrueCropGeometry = Geometry.Blank;
Log.Verbose("Scanner variables reset.");
Log.Info("Scanner stopped.");
}
catch (Exception e)
{
Log.Error(e, "Scanner failed to stop. This isn't good...");
}
}
public void AsyncStart()
{
if (_FrameHandlerThread == null || _FrameHandlerThread.ThreadState != ThreadState.Running)
{
Log.Info("Creating scanner thread.");
ThreadStart t = new ThreadStart(Start);
_FrameHandlerThread = new Thread(t);
_FrameHandlerThread.Start();
Log.Info("Thread created.");
}
else
{
if (!Restarting)
{
Log.Info("Scanner already running.");
Restart();
}
else
{
Log.Warning("'Kay, this does not look good here, um...");
}
}
}
// Sorry for the nersts mess.
public void Start()
{
try
{
Log.Verbose("Initializing start.");
UpdateCropGeometry();
Log.Info("Trying to start scanner.");
if (_GameProfile != null && IsVideoSourceValid() && CompiledFeatures != null)
{
Log.Info("Starting scanner...");
CurrentIndex = 0;
OverloadCount = 0;
DeltaManager = new DeltaManager(CompiledFeatures, 256); // Todo: Unhardcode?
InitCount = 0;
Log.Verbose("Hooking events onto Accord.");
_VideoSource.NewFrame += _NewFrameEventHandler;
_VideoSource.VideoSourceError += _VideoSourceErrorEventHandler;
Log.Info("Scanner hooked onto video source.");
var moniker = DeviceMoniker;
if (!string.IsNullOrWhiteSpace(moniker))
{
try
{
_VideoSource.Source = moniker;
_VideoSource.Start();
Log.Info("Scanner started.");
}
catch (Exception e)
{
Log.Error(e, "Video source failed to start.");
}
}
else
{
Log.Warning("How did you manage to change the selected device in a few milliseconds?");
}
}
else
{
var str = "Scanner start failed because:";
if (_GameProfile == null) str += " The game profile was empty.";
if (!IsVideoSourceValid()) str += " The script was not loaded.";
if (CompiledFeatures == null) str += " CompiledFeatures was blank.";
Log.Verbose(str);
}
}
catch (Exception e)
{
Log.Error(e, "Unknown Scanner.Start() error, please show this to the component developers.");
}
}
public void Restart()
{
if (!Restarting)
{
Restarting = true;
Log.Info("Restarting scanner...");
Stop();
Log.Verbose("Stopped, sleeping for 1000ms.");
Thread.Sleep(1000);
AsyncStart();
Log.Info("Restart finished.");
Restarting = false;
}
else
{
Log.Verbose("THERE CAN BE ONLY ONE THREAD.");
}
}
public void UpdateCropGeometry()
{
_TrueCropGeometry = Geometry.Blank;
if (_Component.IsScriptLoaded() && _GameProfile != null)
{
Log.Info("Adjusting profile to set dimensions...");
IsScannerLocked = true;
CompiledFeatures = new CompiledFeatures(_GameProfile, CropGeometry);
IsScannerLocked = false;
Log.Info("Profile adjusted.");
}
else
{
Log.Verbose("Game Profile is not set or script is not loaded. UpdateCropGeometry() failed.");
}
}
// Does this dispose properly?
private static IMagickImage GetComposedImage(Bitmap input, int channelIndex, ColorSpace colorSpace)
{
if (input == null)
{
return null;
}
IMagickImage mi = new MagickImage(input);
mi.ColorSpace = colorSpace;
if (channelIndex > -1)
{
mi = mi.Separate().ElementAt(channelIndex);
}
return mi;
}
private void HandleVideoError(object sender, VideoSourceErrorEventArgs e)
{
Log.Error(e.Exception, "Video capture fatal error. " + e.Description);
if (IsVideoSourceRunning())
{
Restart();
}
}
public void HandleNewFrame(object sender, NewFrameEventArgs e)
{
var now = TimeStamp.CurrentDateTime.Time;
InitCount++;
if (!IsScannerLocked &&
(InitCount > 255 || InitCount % 10 == 0) &&
ScanningCount < 12)
{
ScanningCount++;
var currentFrame = new Frame(now, (Bitmap)e.Frame.Clone());
var previousFrame = CurrentFrame;
CurrentFrame = currentFrame;
var newScan = new Scan(currentFrame, previousFrame, CompiledFeatures.UsesDupeCheck(now));
var index = CurrentIndex;
CurrentIndex++;
Task.Factory.StartNew(() => NewRun(newScan, index));
}
else if (ScanningCount >= 12)
{
OverloadCount++;
if (OverloadCount > 50)
{
Log.Warning("Frame handler is too overloaded, restarting scanner...");
Restart();
}
else
{
Log.Warning("Frame handler is overloaded!!!");
}
}
}
// Todo: prevFile isn't necessary. Instead store the features of the current scan to be used on the next.
// That could cause sync problems so it needs to be investigated.
private void NewRun(Scan scan, int index)
{
var deltas = new double[CompiledFeatures.FeatureCount];
var benchmarks = new double[CompiledFeatures.FeatureCount];
var now = scan.CurrentFrame.DateTime;
var fileImageBase = scan.CurrentFrame.Bitmap;
var prevFileImageBase = CompiledFeatures.UsesDupeCheck(now) ? scan.PreviousFrame.Bitmap : null;
try
{
foreach (var cWatchZone in CompiledFeatures.CWatchZones)
CropScan(ref deltas, ref benchmarks, now, fileImageBase, prevFileImageBase, cWatchZone);
}
catch (Exception e)
{
scan.Dispose();
Log.Error(e, "Error scanning frame.");
if (IsVideoSourceRunning() && !IsScannerLocked)
{
ScanningCount--;
}
}
var scanEnd = TimeStamp.CurrentDateTime.Time;
try
{
DeltaManager?.AddResult(index, scan, scanEnd, deltas, benchmarks);
NewResult(this, new DeltaOutput(DeltaManager, index, CurrentFPS));
ScanningCount--;
ScanFinished?.Invoke(this, scan);
//scan.Dispose();
// It's on its own thread so running it here should be okay.
if (index % Math.Ceiling(AverageFPS) == 0)
{
RefreshBenchmarks();
}
if (index >= 32 && AverageFPS > 64 && !Restarting)
{
Log.Warning("Framerate is abnormally high, usually an indicator the video feed is not active.");
Restart();
}
}
catch (Exception e)
{
Log.Error(e, "Unknown Scanner Error.");
}
}
private void RefreshBenchmarks()
{
int count = 0;
double sumFPS = double.Epsilon, minFPS = double.MaxValue, maxFPS = double.Epsilon,
sumScanTime = double.Epsilon, minScanTime = double.MaxValue, maxScanTime = double.Epsilon,
sumWaitTime = double.Epsilon, minWaitTime = double.MaxValue, maxWaitTime = double.Epsilon;
foreach (var d in DeltaManager.History)
{
if (!d.IsBlank)
{
count++;
var fd = d.FrameDuration.TotalSeconds;
sumFPS += fd;
minFPS = Math.Min(minFPS, fd);
maxFPS = Math.Max(maxFPS, fd);
var sd = d.ScanDuration.TotalSeconds;
sumScanTime += sd;
minScanTime = Math.Min(minScanTime, sd);
maxScanTime = Math.Max(maxScanTime, sd);
var wd = d.WaitDuration.TotalSeconds;
sumWaitTime += wd;
minWaitTime = Math.Min(minWaitTime, wd);
maxWaitTime = Math.Max(maxWaitTime, wd);
}
}
count = Math.Max(count, 1); // Make sure count > 0. count is 0 when History is blank.
AverageFPS = 3000d / Math.Round(sumFPS / count * 3000d);
RecentMaxFPS = 1 / minFPS;
RecentMinFPS = 1 / maxFPS;
MinFPS = Math.Min(MinFPS, RecentMinFPS);
MaxFPS = Math.Max(MaxFPS, RecentMaxFPS);
AverageScanTime = sumScanTime / count;
MinScanTime = minScanTime;
MaxScanTime = maxScanTime;
AverageWaitTime = sumWaitTime / count;
MinWaitTime = minWaitTime;
MaxWaitTime = maxWaitTime;
// Todo: If AverageWaitTime is too high, shrink some of the large images before comparing.
// Maybe benchmark individual features though? Some ErrorMetrics are more intense than others.
}
private static void CropScan(
ref double[] deltas,
ref double[] benchmarks,
DateTime now,
Bitmap fileImageBase,
Bitmap prevFileImageBase,
CWatchZone cWatchZone)
{
if (!cWatchZone.IsPaused(now))
{
using (var fileImageCropped = fileImageBase.Clone(cWatchZone.Rectangle, PixelFormat.Format24bppRgb))
using (var prevFileImageCropped = cWatchZone.UsesDupeCheck(now) ?
prevFileImageBase?.Clone(cWatchZone.Rectangle, PixelFormat.Format24bppRgb) : null)
{
foreach (var cWatcher in cWatchZone.CWatches)
ComposeScan(ref deltas, ref benchmarks, now, fileImageCropped, prevFileImageCropped, cWatcher);
}
}
else
{
foreach (var cWatcher in cWatchZone.CWatches)
{
foreach (var cWatchImage in cWatcher.CWatchImages)
{
deltas[cWatchImage.Index] = double.NaN;
benchmarks[cWatchImage.Index] = 0;
}
}
}
}
private static void ComposeScan(
ref double[] deltas,
ref double[] benchmarks,
DateTime now,
Bitmap fileImageCropped,
Bitmap prevFileImageCropped,
CWatcher cWatcher)
{
if (!cWatcher.IsPaused(now))
{
using (var fileImageComposed = GetComposedImage(fileImageCropped, cWatcher.Channel, cWatcher.ColorSpace))
using (var prevFileImageComposed = GetComposedImage(prevFileImageCropped, cWatcher.Channel, cWatcher.ColorSpace))
{
if (cWatcher.Equalize)
{
fileImageComposed.Equalize();
prevFileImageComposed?.Equalize();
}
if (cWatcher.IsStandard)
foreach (var cWatchImage in cWatcher.CWatchImages)
CompareAgainstFeature(ref deltas, ref benchmarks, now, fileImageComposed, cWatcher, cWatchImage);
else if (cWatcher.IsDuplicateFrame)
CompareAgainstPreviousFrame(ref deltas, ref benchmarks, now, fileImageComposed, prevFileImageComposed, cWatcher);
else
throw new NotImplementedException("How'd you get here?");
}
}
else
{
foreach (var cWatchImage in cWatcher.CWatchImages)
{
deltas[cWatchImage.Index] = double.NaN;
benchmarks[cWatchImage.Index] = 0;
}
}
}
private static void CompareAgainstFeature(
ref double[] deltas,
ref double[] benchmarks,
DateTime now,
IMagickImage fileImageComposed,
CWatcher cWatcher,
CWatchImage cWatchImage)
{
if (!cWatchImage.IsPaused(now))
{
var benchmark = TimeStamp.Now;
using (var fileImageCompare = fileImageComposed.Clone())
using (var deltaImage = cWatchImage.MagickImage.Clone())
{
if (cWatchImage.HasAlpha)
{
fileImageCompare.Composite(cWatchImage.AlphaChannel, CompositeOperator.Over);
}
SetDelta(ref deltas, fileImageCompare, deltaImage, cWatcher, cWatchImage);
SetBenchmark(ref benchmarks, benchmark, cWatchImage);
}
}
else
{
deltas[cWatchImage.Index] = double.NaN;
benchmarks[cWatchImage.Index] = 0;
}
}
private static void CompareAgainstPreviousFrame(
ref double[] deltas,
ref double[] benchmarks,
DateTime now,
IMagickImage fileImageComposed,
IMagickImage prevFileImageComposed,
CWatcher cWatcher)
{
var cWatchImage = cWatcher.CWatchImages[0];
if (!cWatchImage.IsPaused(now) && prevFileImageComposed != null)
{
var benchmark = TimeStamp.Now;
SetDelta(ref deltas, fileImageComposed, prevFileImageComposed, cWatcher, cWatchImage);
SetBenchmark(ref benchmarks, benchmark, cWatchImage);
}
else
{
deltas[cWatchImage.Index] = double.NaN;
benchmarks[cWatchImage.Index] = 0;
}
}
private static void SetDelta(
ref double[] deltas,
IMagickImage fileImageCompare,
IMagickImage deltaImage,
CWatcher cWatcher,
CWatchImage cWatchImage)
{
var metricResult = fileImageCompare.Compare(deltaImage, cWatcher.ErrorMetric);
deltas[cWatchImage.Index] = cWatcher.ErrorMetric.Standardize(
cWatchImage.MetricUpperBound,
metricResult,
cWatchImage.TransparencyRate);
}
private static void SetBenchmark(ref double[] benchmarks, TimeStamp timeStamp, CWatchImage cWatchImage)
{
benchmarks[cWatchImage.Index] = (TimeStamp.Now - timeStamp).TotalSeconds;
}
public void Dispose()
{
Stop();
IsScannerLocked = true;
}
}
}