InputProviderIntel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. wrapper = (IntelCameraWrapper) domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
  68. var startTask = Task.Run(() => wrapper.init(this));
  69. if (!startTask.Wait(TimeSpan.FromSeconds(4)))
  70. {
  71. Console.WriteLine("Timeout during init");
  72. errorstate = true;
  73. scheduledForRestart = false;
  74. }
  75. else {
  76. IsActive = true;
  77. if (DeviceStartedEvent != null)
  78. {
  79. DeviceStartedEvent(this, new EventArgs());
  80. DeviceStartedEvent = null; //only notify once...
  81. }
  82. scheduledForRestart = false;
  83. try
  84. {
  85. wrapper.run();
  86. }
  87. catch (System.AccessViolationException)
  88. {
  89. Console.WriteLine("Camera caused corrupted process state.");
  90. errorstate = true;
  91. }
  92. catch (Exception)
  93. {
  94. Console.WriteLine("Camera Thread crashed.");
  95. errorstate = true;
  96. }
  97. }
  98. checkExitErrorstate();
  99. }
  100. private bool scheduledForRestart = false;
  101. private void checkExitErrorstate()
  102. {
  103. if (errorstate && !scheduledForRestart)
  104. {
  105. if (startTimeWatch != null && startTimeWatch.ElapsedMilliseconds < 5000) {
  106. Console.WriteLine("...");
  107. return;
  108. }
  109. //startTimeWatch.Stop();
  110. //we have crashed. Kill the app domain and try again.
  111. Console.WriteLine("Killing AppDomain (" + domain.FriendlyName + ")...");
  112. scheduledForRestart = true;
  113. //wrapper.terminated = true;
  114. try
  115. {
  116. System.AppDomain.Unload(domain);
  117. }
  118. catch (Exception)
  119. {
  120. Console.WriteLine("Could not unload app domain. We will just wait a moment and try to start a new one.");
  121. }
  122. domain = null;
  123. wrapper = null;
  124. Thread.Sleep(5000);
  125. start();
  126. }
  127. }
  128. public void stop()
  129. {
  130. IsActive = false;
  131. }
  132. internal bool hasNewFrameEvent()
  133. {
  134. return NewFrameEvent != null;
  135. }
  136. internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
  137. {
  138. errorstate = false;
  139. if (startTimeWatch != null)
  140. {
  141. Console.WriteLine("done");
  142. startTimeWatch.Stop();
  143. startTimeWatch = null;
  144. }
  145. NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
  146. }
  147. }
  148. }