HackyInputProviderIntel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.ExceptionServices;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace BBIWARG.Input.InputProviding
  9. {
  10. class HackyInputProviderIntel : MarshalByRefObject, IInputProvider
  11. {
  12. private bool shouldRun;
  13. public int startId;
  14. public int CurrentFrameID
  15. {
  16. get;
  17. set;
  18. }
  19. public float FieldOfViewHorizontal
  20. {
  21. get;
  22. set;
  23. }
  24. public float FieldOfViewVertical
  25. {
  26. get;
  27. set;
  28. }
  29. public int ImageWidth
  30. {
  31. get { return 640; }
  32. }
  33. public int ImageHeight
  34. {
  35. get { return 480; }
  36. }
  37. public bool IsActive
  38. {
  39. get;
  40. set;
  41. }
  42. public UInt16 lowConfidenceValue;
  43. public event DeviceStartedEventHandler DeviceStartedEvent;
  44. public event NewFrameEventHandler NewFrameEvent;
  45. public void initialize()
  46. {
  47. CurrentFrameID = 0;
  48. }
  49. public void start()
  50. {
  51. shouldRun = true;
  52. if (DeviceStartedEvent != null)
  53. {
  54. DeviceStartedEvent(this, new EventArgs());
  55. DeviceStartedEvent = null; //only notify once...
  56. }
  57. IntelDomainWrapper wrapper = null;
  58. while (shouldRun) {
  59. if (wrapper == null || wrapper.errorstate) {//plus timestamp
  60. //time to start a new one!
  61. wrapper = new IntelDomainWrapper(this);
  62. Task runCamTask = new Task(wrapper.run);
  63. var timeoutCancellationTokenSource = new CancellationTokenSource();
  64. }
  65. }
  66. }
  67. public void stop()
  68. {
  69. shouldRun = false;
  70. }
  71. }
  72. class IntelDomainWrapper {
  73. private AppDomain domain;
  74. private IntelCameraWrapper wrapper;
  75. public bool errorstate = false;
  76. private HackyInputProviderIntel inputProvider;
  77. public IntelDomainWrapper(HackyInputProviderIntel inputProvider) {
  78. this.inputProvider = inputProvider;
  79. }
  80. [HandleProcessCorruptedStateExceptions]
  81. public void run() {
  82. domain = System.AppDomain.CreateDomain("AppDomain-" + inputProvider.startId);
  83. inputProvider.startId++;
  84. wrapper = (IntelCameraWrapper)domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
  85. //wrapper.init(inputProvider);
  86. inputProvider.IsActive = true;
  87. try
  88. {
  89. wrapper.run();
  90. }
  91. catch (System.AccessViolationException)
  92. {
  93. Console.WriteLine("Camera caused corrupted process state.");
  94. errorstate = true;
  95. }
  96. }
  97. }
  98. }