VideoCapturePlayer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using AForge.Video;
  2. using AForge.Video.DirectShow;
  3. using AIDiagnosisDemo.Infrastucture;
  4. using System;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Threading;
  8. namespace AIDiagnosisDemo.Service
  9. {
  10. internal class VideoCapturePlayer : IPlayer
  11. {
  12. private readonly string _deviceId;
  13. private readonly object _deviceLocker = new object();
  14. private int _startTickCount;
  15. private int _displayCount;
  16. private int _fps;
  17. /// <summary>
  18. /// 接收到图像输入的事件
  19. /// </summary>
  20. public event EventHandler<Bitmap> InputFrameReceived;
  21. /// <summary>
  22. /// FPS变更的事件
  23. /// </summary>
  24. public event EventHandler<int> FPSChanged;
  25. private VideoCaptureDevice _videoCaptureDevice;
  26. /// <summary>
  27. /// VideoPlayer构造函数
  28. /// </summary>
  29. /// <param name="deviceId">Device Id</param>
  30. public VideoCapturePlayer(string deviceId)
  31. {
  32. _deviceId = deviceId;
  33. }
  34. /// <summary>
  35. /// 播放
  36. /// </summary>
  37. /// <param name="isVideo">是视频为True,是图片为False</param>
  38. /// <returns>播放成功为True,播放失败为False</returns>
  39. public bool Play()
  40. {
  41. lock (_deviceLocker)
  42. {
  43. try
  44. {
  45. var videoFilterInfos = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType<FilterInfo>();
  46. if (videoFilterInfos.FirstOrDefault(i => i.MonikerString == _deviceId) == null)
  47. {
  48. Logger.Info("Video Device is Empty!!!");
  49. return false;
  50. }
  51. _videoCaptureDevice = new VideoCaptureDevice(_deviceId);
  52. _videoCaptureDevice.NewFrame += OnNewFrame;
  53. _startTickCount = -1;
  54. _videoCaptureDevice.Start();
  55. return true;
  56. }
  57. catch
  58. {
  59. _videoCaptureDevice = null;
  60. Logger.Info("Video Device Play Fail!!!");
  61. }
  62. return false;
  63. }
  64. }
  65. private void OnNewFrame(object sender, NewFrameEventArgs eventArgs)
  66. {
  67. if (_startTickCount == -1)
  68. {
  69. _displayCount = 0;
  70. _startTickCount = Environment.TickCount;
  71. }
  72. else
  73. {
  74. var currentTickCount = Environment.TickCount;
  75. var fpsTimeUsed = currentTickCount - _startTickCount;
  76. _displayCount++;
  77. if (fpsTimeUsed > 1000)
  78. {
  79. var fps = (int)((double)_displayCount / fpsTimeUsed * 1000);
  80. if (_fps != fps)
  81. {
  82. _fps = fps;
  83. FPSChanged?.Invoke(this, _fps);
  84. }
  85. _startTickCount = currentTickCount;
  86. _displayCount = 0;
  87. }
  88. }
  89. InputFrameReceived?.Invoke(this, eventArgs.Frame);
  90. }
  91. /// <summary>
  92. /// 停止播放
  93. /// </summary>
  94. public void Stop()
  95. {
  96. lock (_deviceLocker)
  97. {
  98. if (_videoCaptureDevice == null)
  99. {
  100. return;
  101. }
  102. try
  103. {
  104. _videoCaptureDevice.NewFrame -= OnNewFrame;
  105. if (_videoCaptureDevice.IsRunning)
  106. {
  107. DoStop();
  108. }
  109. }
  110. catch
  111. {
  112. }
  113. finally
  114. {
  115. _videoCaptureDevice = null;
  116. }
  117. }
  118. }
  119. private void DoStop()
  120. {
  121. var stopThread = new Thread(() =>
  122. {
  123. try
  124. {
  125. _videoCaptureDevice.SignalToStop();
  126. _videoCaptureDevice.WaitForStop();
  127. }
  128. catch
  129. {
  130. }
  131. });
  132. stopThread.Start();
  133. try
  134. {
  135. if (!stopThread.Join(2000))
  136. {
  137. stopThread.Abort();
  138. }
  139. }
  140. catch
  141. {
  142. }
  143. }
  144. /// <summary>
  145. /// 继续播放
  146. /// </summary>
  147. public void Continue()
  148. {
  149. Play();
  150. }
  151. /// <summary>
  152. /// 暂停播放
  153. /// </summary>
  154. public void Pause()
  155. {
  156. Stop();
  157. }
  158. }
  159. }