-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCore.cs
1536 lines (1379 loc) · 45.8 KB
/
Core.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
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using AtmoLight.Targets;
using ProtoBuffer;
namespace AtmoLight
{
public enum ContentEffect
{
LEDsDisabled = 0,
MediaPortalLiveMode,
StaticColor,
GIFReader,
VUMeter,
VUMeterRainbow,
ExternalLiveMode,
AtmoWinColorchanger,
AtmoWinColorchangerLR,
Undefined = -1
}
public enum Target
{
AmbiBox,
AtmoOrb,
AtmoWin,
Boblight,
Hue,
Hyperion
}
public enum TargetType
{
Local,
Network
}
public enum BlackbarDetectionAR
{
_1_33x1,
_1_78x1,
_1_85x1,
_2_35x1
}
public class ChangeImageData
{
private byte[] pixelDataInfo;
private byte[] bmiInfoHeaderInfo;
private long tickInfo;
private bool forceInfo;
public ChangeImageData() { }
public ChangeImageData(byte[] pixelData, byte[] bmiInfoHeader, long tick, bool force)
{
pixelDataInfo = pixelData;
bmiInfoHeaderInfo = bmiInfoHeader;
tickInfo = tick;
forceInfo = force;
}
public byte[] pixelData
{
get { return pixelDataInfo; }
set { pixelDataInfo = value; }
}
public byte[] bmiInfoHeader
{
get { return bmiInfoHeaderInfo; }
set { bmiInfoHeaderInfo = value; }
}
public long tick
{
get { return tickInfo; }
set { tickInfo = value; }
}
public bool force
{
get { return forceInfo; }
set { forceInfo = value; }
}
}
public class Core
{
#region Fields
// Core Instance
private static Core instance = null;
// Threads
private Thread setPixelDataThreadHelper;
private Thread gifReaderThreadHelper;
private Thread vuMeterThreadHelper;
private Thread apiThreadHelper;
// States
private ContentEffect currentEffect = ContentEffect.Undefined; // Current active effect
public ContentEffect apiStoredPlaybackEffect; // Stored playbackEffect to restore when apiOverrideActive is disabled
public bool targetChangeImageEnabled;
public bool apiServerEnabled;
public bool apiOverrideActive; // If true disables all other AtmoLight internal commands
// Lists
private List<ITargets> targets = new List<ITargets>();
private List<byte[]> pixelDataList = new List<byte[]>(); // List for pixelData (Delay)
private List<byte[]> bmiInfoHeaderList = new List<byte[]>(); // List for bmiInfoHeader (Delay)
private List<long> delayTimingList = new List<long>(); // List for timings (Delay)
// Locks
private readonly object listLock = new object(); // Lock object for the above lists
private readonly object targetsLock = new object(); // Lock object for the target list
private volatile bool setPixelDataLock = true; // Lock for SetPixelData thread
private volatile bool gifReaderLock = true;
private volatile bool vuMeterLock = true;
private volatile bool apiServerLock = true;
// Event Handler
public delegate void NewConnectionLostHandler(Target target);
public static event NewConnectionLostHandler OnNewConnectionLost;
public delegate double[] NewVUMeterHander();
public static event NewVUMeterHander OnNewVUMeter;
// Stopwatches
private Stopwatch blackbarStopwatch = new Stopwatch();
// Generic Fields
private int captureWidth = 64; // Default fallback capture width
private int captureHeight = 48; // Default fallback capture height
private static int targetChangeImageQueueSize = 240;
private Queue targetChangeImageQueue = new Queue();
private bool delayEnabled = false;
private int delayTime = 0;
private string gifPath = "";
private Rectangle blackbarDetectionRect;
// General settings for targets
public int[] staticColor = { 0, 0, 0 }; // RGB code for static color
public bool reInitOnError;
public bool blackbarDetection;
public int blackbarDetectionTime;
public int blackbarDetectionThreshold;
public bool blackbarDetectionHorizontal;
public bool blackbarDetectionVertical;
public bool blackbarDetectionLinkAreas;
public bool blackbarDetectionManual = false;
public BlackbarDetectionAR blackbarDetectionAR;
public bool targetResendCommand = true;
public int powerModeChangedDelay;
public int vuMeterMindB;
public double vuMeterMaxHue;
public double vuMeterMinHue;
// AmbiBox Settings Fields
public string ambiBoxIP;
public int ambiBoxPort;
public int ambiBoxMaxReconnectAttempts;
public int ambiBoxReconnectDelay;
public int ambiBoxChangeImageDelay;
public string ambiBoxMediaPortalProfile;
public string ambiBoxExternalProfile;
public string ambiBoxPath;
public bool ambiBoxAutoStart;
public bool ambiBoxAutoStop;
// AtmoOrb
public int atmoOrbBroadcastPort;
public int atmoOrbThreshold;
public int atmoOrbMinDiversion;
public double atmoOrbSaturation;
public double atmoOrbGamma;
public int atmoOrbBlackThreshold;
public bool atmoOrbUseOverallLightness;
public bool atmoOrbUseSmoothing;
public int atmoOrbSmoothThreshold;
public List<string> atmoOrbLamps = new List<string>();
// AtmoWin Settings Fields
public bool atmoWinAutoStart;
public bool atmoWinAutoStop;
public string atmoWinPath;
public bool atmoWakeHelperEnabled;
public string atmoWakeHelperComPort;
public int atmoWakeHelperResumeDelay;
public int atmoWakeHelperDisconnectDelay;
public int atmoWakeHelperConnectDelay;
public int atmoWakeHelperReinitializationDelay;
// Boblight Settings Fields
public string boblightIP;
public int boblightPort;
public int boblightMaxFPS;
public int boblightMaxReconnectAttempts;
public int boblightReconnectDelay;
public int boblightSpeed;
public int boblightAutospeed;
public bool boblightInterpolation;
public int boblightSaturation;
public int boblightValue;
public int boblightThreshold;
public double boblightGamma;
// Hyperion Settings Fields
public string hyperionIP;
public int hyperionPort;
public int hyperionPriority;
public int hyperionReconnectDelay;
public int hyperionReconnectAttempts;
public int hyperionPriorityStaticColor;
public bool hyperionLiveReconnect;
// Hue Settings Fields
public string huePath;
public bool hueStart;
public bool hueIsRemoteMachine;
public string hueIP;
public int huePort;
public int hueReconnectDelay;
public int hueReconnectAttempts;
public bool hueBridgeEnableOnResume;
public bool hueBridgeDisableOnSuspend;
public int hueBlackThreshold;
public int hueThreshold;
public int hueMinDiversion;
public bool hueUseOverallLightness;
public double hueSaturation;
public bool hueTheaterEnabled;
public bool hueTheaterRestoreLights;
public bool hueTheaterEnabledVU;
#endregion
#region Constructor/Deconstructor
/// <summary>
/// Core Constructor
/// </summary>
private Core()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
DateTime buildDate = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).LastWriteTime;
Log.Debug("Core Version {0}.{1}.{2}.{3}, build on {4} at {5}.", version.Major, version.Minor, version.Build, version.Revision, buildDate.ToShortDateString(), buildDate.ToLongTimeString());
return;
}
/// <summary>
/// Disposes of all targets
/// </summary>
public void Dispose()
{
foreach (var target in targets)
{
target.Dispose();
}
// Stop Target change image worker thread
targetChangeImageEnabled = false;
TargetChangeImageWorker.CancelAsync();
// Stop API server
StopAPIserverThread();
}
#endregion
#region Initialisation
/// <summary>
/// Generate all targets and initialise them.
/// </summary>
/// <returns></returns>
BackgroundWorker TargetChangeImageWorker = new BackgroundWorker();
public void Initialise()
{
foreach (var target in targets)
{
if (!target.IsConnected())
{
target.Initialise(false);
}
}
targetChangeImageEnabled = true;
TargetChangeImageWorker.WorkerReportsProgress = false;
TargetChangeImageWorker.WorkerSupportsCancellation = true;
TargetChangeImageWorker.DoWork += TargetChangeDoWork;
TargetChangeImageWorker.RunWorkerAsync();
// Start API server
apiServerLock = false;
StartAPIserverThread();
}
/// <summary>
/// Reinitialise all targets that are not connected.
/// </summary>
public void ReInitialise()
{
foreach (var target in targets)
{
if (!target.IsConnected())
{
target.ReInitialise(true);
}
}
}
#endregion
#region Configuration Methods (set)
/// <summary>
/// Set capture dimensions that should be used by everbody.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public bool SetCaptureDimensions(int width, int height)
{
if (width >= 0 && height >= 0)
{
captureWidth = width;
captureHeight = height;
blackbarDetectionRect = new Rectangle(0, 0, width, height);
return true;
}
return false;
}
/// <summary>
/// Add a target to be used.
/// </summary>
/// <param name="target"></param>
public void AddTarget(Target target)
{
// Dont allow the same target to be added more than once.
lock (targetsLock)
{
foreach (var t in targets)
{
if (t.Name == target)
{
return;
}
}
if (target == Target.AtmoWin)
{
targets.Add(new AtmoWinHandler());
}
else if (target == Target.Hue)
{
targets.Add(new HueHandler());
}
else if (target == Target.Hyperion)
{
targets.Add(new HyperionHandler());
}
else if (target == Target.AmbiBox)
{
targets.Add(new AmbiBoxHandler());
}
else if (target == Target.Boblight)
{
targets.Add(new BoblightHandler());
}
else if (target == Target.AtmoOrb)
{
targets.Add(new AtmoOrbHandler());
}
}
}
/// <summary>
/// Removes a target.
/// </summary>
/// <param name="target"></param>
public bool RemoveTarget(Target target)
{
lock (targetsLock)
{
foreach (var t in targets)
{
if (t.Name == target)
{
Log.Debug("Removing {0} as target.", target.ToString());
t.Dispose();
targets.Remove(t);
return true;
}
}
}
return false;
}
/// <summary>
/// Define if the handlers should try to reinitialise when the connection is lost
/// or and error occurs.
/// </summary>
/// <param name="reInit"></param>
public void SetReInitOnError(bool reInit)
{
reInitOnError = reInit;
}
/// <summary>
/// Set the path to the gif file that should be used by the GIFReader
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool SetGIFPath(string path)
{
if (path.Length > 4)
{
if (path.Substring(path.Length - 3, 3).ToLower() == "gif")
{
gifPath = path;
return true;
}
}
return false;
}
/// <summary>
/// Changes the delay time.
/// </summary>
/// <param name="delay">Delay in ms.</param>
/// <returns>true or false</returns>
public bool SetDelay(int delay)
{
if (delay > 0)
{
Log.Debug("Changing delay to {0}ms.", delay);
delayTime = delay;
return true;
}
return false;
}
/// <summary>
/// Changes the static color.
/// </summary>
/// <param name="red">Red in RGB format.</param>
/// <param name="green">Green in RGB format.</param>
/// <param name="blue">Blue in RGB format.</param>
/// <returns>true or false</returns>
public bool SetStaticColor(int red, int green, int blue)
{
if ((red >= 0 && red <= 255) && (green >= 0 && green <= 255) && (blue >= 0 && blue <= 255))
{
staticColor[0] = red;
staticColor[1] = green;
staticColor[2] = blue;
return true;
}
return false;
}
#endregion
#region Information Methods (get)
/// <summary>
/// Returns the instance of the core.
/// </summary>
/// <returns></returns>
public static Core GetInstance()
{
if (instance == null)
{
instance = new Core();
}
return instance;
}
/// <summary>
/// Returns if there are targets that are connected.
/// </summary>
/// <returns></returns>
public bool IsConnected()
{
lock (targetsLock)
{
foreach (var target in targets)
{
if (target.IsConnected())
{
return true;
}
}
}
return false;
}
/// <summary>
/// Returns if all targets are connected.
/// </summary>
/// <returns></returns>
public bool AreAllConnected()
{
lock (targetsLock)
{
foreach (var target in targets)
{
if (!target.IsConnected())
{
return false;
}
}
}
return true;
}
/// <summary>
/// Returns if AtmoLight/LEDs are on.
/// </summary>
/// <returns>true or false</returns>
public bool IsAtmoLightOn()
{
if (!IsConnected())
{
return false;
}
return !(GetCurrentEffect() == ContentEffect.LEDsDisabled || GetCurrentEffect() == ContentEffect.Undefined);
}
/// <summary>
/// Returns if the delay in enabled.
/// </summary>
/// <returns>true or false</returns>
public bool IsDelayEnabled()
{
return delayEnabled;
}
/// <summary>
/// Returns the delay tick.
/// </summary>
/// <returns>delay tick in ms.</returns>
public long GetDelayTick()
{
return Win32API.GetTickCount();
}
/// <summary>
/// Returns the static color.
/// </summary>
/// <returns>Static Color as int array</returns>
public int[] GetStaticColor()
{
return staticColor;
}
/// <summary>
/// Returns the capture width
/// </summary>
/// <returns></returns>
public int GetCaptureWidth()
{
return captureWidth;
}
/// <summary>
/// Returns the capture height
/// </summary>
/// <returns></returns>
public int GetCaptureHeight()
{
return captureHeight;
}
/// <summary>
/// Returns the current effect.
/// </summary>
/// <returns>Current effect</returns>
public ContentEffect GetCurrentEffect()
{
return currentEffect;
}
/// <summary>
/// Returns the number of active targets.
/// </summary>
/// <returns></returns>
public int GetTargetCount()
{
return targets.Count();
}
public List<ContentEffect> GetSupportedEffects()
{
List<ContentEffect> tempList = new List<ContentEffect>();
lock (targetsLock)
{
foreach (var target in targets)
{
for (int i = 0; i < target.SupportedEffects.Count; i++)
{
if (!tempList.Contains(target.SupportedEffects[i]))
{
tempList.Add(target.SupportedEffects[i]);
}
}
}
}
return tempList;
}
/// <summary>
/// Returns if at least one target allows the use of a delay
/// </summary>
/// <returns></returns>
public bool IsAllowDelayTargetPresent()
{
lock (targetsLock)
{
foreach (var target in targets)
{
if (target.AllowDelay)
{
return true;
}
}
}
return false;
}
/// <summary>
/// Returns if at least one target disallows the use of a delay
/// </summary>
/// <returns></returns>
public bool IsDisAllowDelayTargetPresent()
{
lock (targetsLock)
{
foreach (var target in targets)
{
if (!target.AllowDelay)
{
return true;
}
}
}
return false;
}
public ITargets GetTarget(Target target)
{
lock (targetsLock)
{
foreach (var t in targets)
{
if (t.Name == target)
{
return t;
}
}
}
return null;
}
#endregion
#region Events
/// <summary>
/// Method to allow the handlers to raise the NewConnectionLost event.
/// </summary>
/// <param name="target"></param>
public void NewConnectionLost(Target target)
{
if (OnNewConnectionLost != null)
{
OnNewConnectionLost(target);
}
}
#endregion
#region Utilities
/// <summary>
/// Calculates the needed information from a bitmap stream and sends them to SendPixelData().
/// </summary>
/// <param name="stream"></param>
public void CalculateBitmap(Stream stream)
{
// Debug file output
// new Bitmap(stream).Save("C:\\ProgramData\\Team MediaPortal\\MediaPortal\\" + Win32API.GetTickCount() + ".bmp");
if (blackbarDetection && currentEffect == ContentEffect.MediaPortalLiveMode)
{
stream = BlackbarDetection(stream);
}
// Debug file output after blackbar detection
// new Bitmap(stream).Save("C:\\ProgramData\\Team MediaPortal\\MediaPortal\\" + Win32API.GetTickCount() + "_.bmp");
BinaryReader reader = new BinaryReader(stream);
stream.Position = 0; // ensure that what start at the beginning of the stream.
reader.ReadBytes(14); // skip bitmap file info header
byte[] bmiInfoHeader = reader.ReadBytes(4 + 4 + 4 + 2 + 2 + 4 + 4 + 4 + 4 + 4 + 4);
int rgbL = (int)(stream.Length - stream.Position);
int rgb = (int)(rgbL / (GetCaptureWidth() * GetCaptureHeight()));
byte[] pixelData = reader.ReadBytes((int)(stream.Length - stream.Position));
byte[] h1pixelData = new byte[GetCaptureWidth() * rgb];
byte[] h2pixelData = new byte[GetCaptureWidth() * rgb];
// We need to flip the image horizontally.
// Because after reading the bytes into the bytearray with BinaryReader the image is upside down (bmp characteristic).
int i;
for (i = 0; i < ((GetCaptureHeight() / 2) - 1); i++)
{
Array.Copy(pixelData, i * GetCaptureWidth() * rgb, h1pixelData, 0, GetCaptureWidth() * rgb);
Array.Copy(pixelData, (GetCaptureHeight() - i - 1) * GetCaptureWidth() * rgb, h2pixelData, 0, GetCaptureWidth() * rgb);
Array.Copy(h1pixelData, 0, pixelData, (GetCaptureHeight() - i - 1) * GetCaptureWidth() * rgb, GetCaptureWidth() * rgb);
Array.Copy(h2pixelData, 0, pixelData, i * GetCaptureWidth() * rgb, GetCaptureWidth() * rgb);
}
SendPixelData(pixelData, bmiInfoHeader);
}
/// <summary>
/// Sends picture information either to the delay thread or directly to the targets.
/// </summary>
/// <param name="pixelData"></param>
/// <param name="bmiInfoHeader"></param>
/// <param name="force"></param>
private void SendPixelData(byte[] pixelData, byte[] bmiInfoHeader, bool force = false)
{
if (GetCurrentEffect() != ContentEffect.MediaPortalLiveMode && GetCurrentEffect() != ContentEffect.GIFReader && GetCurrentEffect()
!= ContentEffect.VUMeter && GetCurrentEffect() != ContentEffect.VUMeterRainbow)
{
return;
}
if (targetChangeImageQueue.Count >= targetChangeImageQueueSize)
{
targetChangeImageQueue.Dequeue();
}
ChangeImageData data = new ChangeImageData(pixelData, bmiInfoHeader, GetDelayTick(), force);
targetChangeImageQueue.Enqueue(data);
}
private void TargetChangeDoWork(object sender, DoWorkEventArgs e)
{
ChangeImageData data;
while (targetChangeImageEnabled)
{
try
{
if (targetChangeImageQueue.Count == 0 || targets == null)
{
Thread.Sleep(1);
continue;
}
else if (GetCurrentEffect() == ContentEffect.LEDsDisabled && targetChangeImageQueue.Count > 0)
{
targetChangeImageQueue.Clear();
continue;
}
// Use optimized method if all targets support delay
if (!IsDisAllowDelayTargetPresent() && delayEnabled)
{
data = (ChangeImageData)targetChangeImageQueue.Dequeue();
// Delay if target allows for delay and delay was enabled
if (IsAllowDelayTargetPresent() && !data.force && delayEnabled)
{
while (GetDelayTick() < (data.tick + delayTime))
{
Thread.Sleep(1);
}
//Log.Debug("Frame tick matched diff -> {0} | Queue size: {1}", Math.Abs(GetDelayTick() - data.tick - delayTime), targetChangeImageQueue.Count);
}
lock (targetsLock)
{
foreach (var target in targets)
{
if (target.AllowDelay && target.IsConnected())
{
target.ChangeImage(data.pixelData, data.bmiInfoHeader);
}
}
}
}
else
{
data = (ChangeImageData)targetChangeImageQueue.Peek();
if (IsDelayEnabled() && !data.force && IsAllowDelayTargetPresent())
{
bool frameTickMatched = false;
long tickCount = Win32API.GetTickCount();
if (tickCount >= (data.tick + delayTime))
{
targetChangeImageQueue.Dequeue();
frameTickMatched = true;
}
lock (targetsLock)
{
foreach (var target in targets)
{
if (!target.AllowDelay && target.IsConnected())
{
target.ChangeImage(data.pixelData, data.bmiInfoHeader);
}
else if (target.IsConnected() && frameTickMatched)
{
//Log.Debug("Frame tick matched -> {0} / {1}", Win32API.GetTickCount(), data.tick + delayTime);
target.ChangeImage(data.pixelData, data.bmiInfoHeader);
}
else
{
Thread.Sleep(5);
continue;
}
}
}
}
else
{
data = (ChangeImageData)targetChangeImageQueue.Dequeue();
lock (targetsLock)
{
foreach (var target in targets)
{
if (target.IsConnected() && (target.AllowDelay || !data.force || !IsDelayEnabled()))
{
target.ChangeImage(data.pixelData, data.bmiInfoHeader);
}
}
}
}
}
}
catch (Exception) { }
}
}
private Stream BlackbarDetection(Stream stream)
{
if (!blackbarStopwatch.IsRunning)
{
blackbarStopwatch.Start();
}
if (!blackbarDetectionManual && (blackbarStopwatch.ElapsedMilliseconds >= blackbarDetectionTime))
{
Bitmap blackBarBitmap = new Bitmap(stream);
Color colorTemp;
int yTopBound = -1;
int yBottomBound = -1;
int xLeftBound = -1;
int xRightBound = -1;
// Horizontal Scan
if (blackbarDetectionHorizontal)
{
for (int y = 0; y < (int)(blackBarBitmap.Height / 3); y++)
{
if (yTopBound != -1 && yBottomBound != -1)
{
break;
}
for (int x = (int)(blackBarBitmap.Width * 0.33); x < (int)(blackBarBitmap.Width * 0.66); x++)
{
if (yTopBound != -1 && yBottomBound != -1)
{
break;
}
if (yTopBound == -1)
{
colorTemp = blackBarBitmap.GetPixel(x, y);
if (colorTemp.R > blackbarDetectionThreshold || colorTemp.G > blackbarDetectionThreshold ||
colorTemp.B > blackbarDetectionThreshold)
{
yTopBound = y;
if (blackbarDetectionLinkAreas)
{
yBottomBound = blackBarBitmap.Height - y;
break;
}
}
}
if (yBottomBound == -1)
{
colorTemp = blackBarBitmap.GetPixel(x, blackBarBitmap.Height - 1 - y);
if (colorTemp.R > blackbarDetectionThreshold || colorTemp.G > blackbarDetectionThreshold ||
colorTemp.B > blackbarDetectionThreshold)
{
yBottomBound = blackBarBitmap.Height - y;
if (blackbarDetectionLinkAreas)
{
yTopBound = y;
break;
}
}
}
}
}
}
// Vertical Scan
if (blackbarDetectionVertical)
{
for (int x = 0; x < (int)(blackBarBitmap.Width / 3); x++)
{
if (xLeftBound != -1 && xRightBound != -1)
{
break;
}
for (int y = (int)(blackBarBitmap.Height * 0.33); y < (int)(blackBarBitmap.Height * 0.66); y++)
{
if (xLeftBound != -1 && xRightBound != -1)
{
break;
}
if (xLeftBound == -1)
{
colorTemp = blackBarBitmap.GetPixel(x, y);
if (colorTemp.R > blackbarDetectionThreshold || colorTemp.G > blackbarDetectionThreshold ||
colorTemp.B > blackbarDetectionThreshold)
{
xLeftBound = x;
if (blackbarDetectionLinkAreas)
{
xRightBound = blackBarBitmap.Width - x;
break;
}
}
}
if (xRightBound == -1)
{
colorTemp = blackBarBitmap.GetPixel(blackBarBitmap.Width - 1 - x, y);
if (colorTemp.R > blackbarDetectionThreshold || colorTemp.G > blackbarDetectionThreshold ||
colorTemp.B > blackbarDetectionThreshold)
{
xRightBound = blackBarBitmap.Width - x;
if (blackbarDetectionLinkAreas)
{
xLeftBound = x;
break;
}
}
}
}
}
}
if (yTopBound != -1 && yBottomBound != -1 && xLeftBound != -1 && xRightBound != -1)
{
blackbarDetectionRect = new Rectangle(xLeftBound, yTopBound, xRightBound - xLeftBound, yBottomBound - yTopBound);
}
blackBarBitmap.Dispose();
blackbarStopwatch.Restart();
}
else if (blackbarDetectionManual)
{
if (blackbarDetectionRect == null)
{
blackbarDetectionRect = new Rectangle(0, 0, GetCaptureWidth(), GetCaptureHeight());
}
if (blackbarDetectionAR == BlackbarDetectionAR._1_33x1)
{
blackbarDetectionRect.X = (int)(0.125 * GetCaptureWidth());
blackbarDetectionRect.Y = 0;
blackbarDetectionRect.Width = (int)(0.75 * GetCaptureWidth());
blackbarDetectionRect.Height = GetCaptureHeight();
}
else if (blackbarDetectionAR == BlackbarDetectionAR._1_78x1)
{
blackbarDetectionRect.X = 0;