InputProviderIntel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. using System.Threading.Tasks;
  9. namespace BBIWARG.Input.InputProviding
  10. {
  11. class InputProviderIntel : MarshalByRefObject, IInputProvider
  12. {
  13. IntelCameraWrapper wrapper;
  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 event DeviceStartedEventHandler DeviceStartedEvent;
  43. public event NewFrameEventHandler NewFrameEvent;
  44. public UInt16 lowConfidenceValue;
  45. private AppDomain domain;
  46. private System.Threading.Timer watchdogTimer;
  47. public void initialize()
  48. {
  49. CurrentFrameID = 0;
  50. watchdogTimer = new System.Threading.Timer(Timer_Elapsed, null, 2000, 1000);
  51. }
  52. private bool errorstate = false;
  53. private void Timer_Elapsed(object state)
  54. {
  55. checkExitErrorstate();
  56. errorstate = true;
  57. }
  58. int startId = 0;
  59. Stopwatch startTimeWatch;
  60. [HandleProcessCorruptedStateExceptions]
  61. public void start()
  62. {
  63. startTimeWatch = Stopwatch.StartNew();
  64. Console.WriteLine("starting new AppDomain (" + startId +")...");
  65. domain = System.AppDomain.CreateDomain("AppDomain-" + startId);
  66. startId++;
  67. Task startTask = null;
  68. bool errorDuringInit = false;
  69. try
  70. {
  71. wrapper = (IntelCameraWrapper)domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
  72. startTask = Task.Run(() => wrapper.init(this));
  73. errorDuringInit = startTask.Wait(TimeSpan.FromSeconds(4));
  74. }
  75. catch (Exception)
  76. {
  77. Console.WriteLine("Camera crashed while init");
  78. errorstate = true;
  79. scheduledForRestart = false;
  80. errorDuringInit = true;
  81. }
  82. if (errorDuringInit)
  83. {
  84. Console.WriteLine("Timeout during init");
  85. errorstate = true;
  86. scheduledForRestart = false;
  87. }
  88. else {
  89. IsActive = true;
  90. if (DeviceStartedEvent != null)
  91. {
  92. DeviceStartedEvent(this, new EventArgs());
  93. DeviceStartedEvent = null; //only notify once...
  94. }
  95. scheduledForRestart = false;
  96. try
  97. {
  98. if(!errorDuringInit)
  99. wrapper.run();
  100. }
  101. catch (System.AccessViolationException)
  102. {
  103. Console.WriteLine("Camera caused corrupted process state.");
  104. errorstate = true;
  105. }
  106. catch (Exception)
  107. {
  108. Console.WriteLine("Camera Thread crashed.");
  109. errorstate = true;
  110. }
  111. }
  112. checkExitErrorstate();
  113. }
  114. private bool scheduledForRestart = false;
  115. private void checkExitErrorstate()
  116. {
  117. if (errorstate && !scheduledForRestart)
  118. {
  119. if (startTimeWatch != null && startTimeWatch.ElapsedMilliseconds < 5000) {
  120. Console.WriteLine("...");
  121. return;
  122. }
  123. //startTimeWatch.Stop();
  124. //we have crashed. Kill the app domain and try again.
  125. Console.WriteLine("Killing AppDomain (" + domain.FriendlyName + ")...");
  126. scheduledForRestart = true;
  127. //wrapper.terminated = true;
  128. try
  129. {
  130. System.AppDomain.Unload(domain);
  131. }
  132. catch (Exception)
  133. {
  134. Console.WriteLine("Could not unload app domain. We will just wait a moment and try to start a new one.");
  135. }
  136. domain = null;
  137. wrapper = null;
  138. Thread.Sleep(5000);
  139. start();
  140. }
  141. }
  142. public void stop()
  143. {
  144. IsActive = false;
  145. }
  146. internal bool hasNewFrameEvent()
  147. {
  148. return NewFrameEvent != null;
  149. }
  150. internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
  151. {
  152. errorstate = false;
  153. if (startTimeWatch != null)
  154. {
  155. Console.WriteLine("done");
  156. startTimeWatch.Stop();
  157. startTimeWatch = null;
  158. }
  159. NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
  160. }
  161. }
  162. }