InputProviderIntel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  93. checkExitErrorstate();
  94. }
  95. private bool scheduledForRestart = false;
  96. private void checkExitErrorstate()
  97. {
  98. if (errorstate && !scheduledForRestart)
  99. {
  100. if (startTimeWatch != null && startTimeWatch.ElapsedMilliseconds < 5000) {
  101. Console.WriteLine("...");
  102. return;
  103. }
  104. //startTimeWatch.Stop();
  105. //we have crashed. Kill the app domain and try again.
  106. Console.WriteLine("Killing AppDomain (" + domain.FriendlyName + ")...");
  107. scheduledForRestart = true;
  108. //wrapper.terminated = true;
  109. try
  110. {
  111. System.AppDomain.Unload(domain);
  112. }
  113. catch (Exception)
  114. {
  115. Console.WriteLine("Could not unload app domain. We will just wait a moment and try to start a new one.");
  116. }
  117. domain = null;
  118. wrapper = null;
  119. Thread.Sleep(5000);
  120. start();
  121. }
  122. }
  123. public void stop()
  124. {
  125. IsActive = false;
  126. }
  127. internal bool hasNewFrameEvent()
  128. {
  129. return NewFrameEvent != null;
  130. }
  131. internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
  132. {
  133. errorstate = false;
  134. if (startTimeWatch != null)
  135. {
  136. Console.WriteLine("done");
  137. startTimeWatch.Stop();
  138. startTimeWatch = null;
  139. }
  140. NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
  141. }
  142. }
  143. }