InputProviderIntel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using Emgu.CV;
  3. using Emgu.CV.Structure;
  4. using System.Timers;
  5. using System.Threading;
  6. using System.Diagnostics;
  7. using System.Runtime.ExceptionServices;
  8. namespace BBIWARG.Input.InputProviding
  9. {
  10. class InputProviderIntel : MarshalByRefObject, IInputProvider
  11. {
  12. IntelCameraWrapper wrapper;
  13. public int CurrentFrameID
  14. {
  15. get;
  16. set;
  17. }
  18. public float FieldOfViewHorizontal
  19. {
  20. get;
  21. set;
  22. }
  23. public float FieldOfViewVertical
  24. {
  25. get;
  26. set;
  27. }
  28. public int ImageWidth
  29. {
  30. get { return 640; }
  31. }
  32. public int ImageHeight
  33. {
  34. get { return 480; }
  35. }
  36. public bool IsActive
  37. {
  38. get;
  39. set;
  40. }
  41. public event DeviceStartedEventHandler DeviceStartedEvent;
  42. public event NewFrameEventHandler NewFrameEvent;
  43. public UInt16 lowConfidenceValue;
  44. private AppDomain domain;
  45. private System.Threading.Timer watchdogTimer;
  46. public void initialize()
  47. {
  48. CurrentFrameID = 0;
  49. watchdogTimer = new System.Threading.Timer(Timer_Elapsed, null, 2000, 1000);
  50. }
  51. private bool errorstate = false;
  52. private void Timer_Elapsed(object state)
  53. {
  54. checkExitErrorstate();
  55. errorstate = true;
  56. }
  57. int startId = 0;
  58. Stopwatch startTimeWatch;
  59. [HandleProcessCorruptedStateExceptions]
  60. public void start()
  61. {
  62. Console.WriteLine("starting new AppDomain (" + startId +")...");
  63. domain = System.AppDomain.CreateDomain("AppDomain-" + startId);
  64. startId++;
  65. wrapper = (IntelCameraWrapper) domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
  66. wrapper.init(this);
  67. IsActive = true;
  68. if (DeviceStartedEvent != null) {
  69. DeviceStartedEvent(this, new EventArgs());
  70. DeviceStartedEvent = null; //only notify once...
  71. }
  72. scheduledForRestart = false;
  73. startTimeWatch = Stopwatch.StartNew();
  74. try
  75. {
  76. wrapper.run();
  77. }
  78. catch (System.AccessViolationException)
  79. {
  80. Console.WriteLine("Camera caused corrupted process state.");
  81. errorstate = true;
  82. }
  83. checkExitErrorstate();
  84. }
  85. private bool scheduledForRestart = false;
  86. private void checkExitErrorstate()
  87. {
  88. if (errorstate && !scheduledForRestart)
  89. {
  90. if (startTimeWatch != null && startTimeWatch.ElapsedMilliseconds < 5000) {
  91. Console.WriteLine("...");
  92. return;
  93. }
  94. //startTimeWatch.Stop();
  95. //we have crashed. Kill the app domain and try again.
  96. Console.WriteLine("Killing AppDomain (" + domain.FriendlyName + ")...");
  97. scheduledForRestart = true;
  98. //wrapper.terminated = true;
  99. try
  100. {
  101. System.AppDomain.Unload(domain);
  102. }
  103. catch (Exception)
  104. {
  105. Console.WriteLine("Could not unload app domain. We will just wait a moment and try to start a new one.");
  106. }
  107. domain = null;
  108. wrapper = null;
  109. Thread.Sleep(5000);
  110. start();
  111. }
  112. }
  113. public void stop()
  114. {
  115. IsActive = false;
  116. }
  117. internal bool hasNewFrameEvent()
  118. {
  119. return NewFrameEvent != null;
  120. }
  121. internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
  122. {
  123. errorstate = false;
  124. if (startTimeWatch != null)
  125. {
  126. Console.WriteLine("done");
  127. startTimeWatch.Stop();
  128. startTimeWatch = null;
  129. }
  130. NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
  131. }
  132. }
  133. }