FileVideoPlayer.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using Accord.Video.FFMPEG;
  2. using AIDiagnosisDemo.Infrastucture;
  3. using AIDiagnosisDemo.Settings;
  4. using System;
  5. using System.Drawing;
  6. using System.Threading;
  7. using System.Windows;
  8. namespace AIDiagnosisDemo.Service
  9. {
  10. internal class FileVideoPlayer : IPlayer
  11. {
  12. private string _filepath;
  13. private VideoFileReader _videoFileReader;
  14. private Thread _playThread;
  15. private bool _pausing;
  16. private int _startTickCount;
  17. private int _displayCount;
  18. private int _fps;
  19. private readonly object _videoReaderLocker = new object();
  20. private volatile int _frameIntervalTime = 0;
  21. private volatile int _frameCount = 0;
  22. private volatile int _frameIndex = 0;
  23. /// <summary>
  24. /// 接收到图像输入的事件
  25. /// </summary>
  26. public event EventHandler<Bitmap> InputFrameReceived;
  27. /// <summary>
  28. /// FPS变更的事件
  29. /// </summary>
  30. public event EventHandler<int> FPSChanged;
  31. /// <summary>
  32. /// FileVideoPlayer构造函数
  33. /// </summary>
  34. /// <param name="filepath">文件路径</param>
  35. public FileVideoPlayer(string filepath)
  36. {
  37. _filepath = filepath;
  38. }
  39. /// <summary>
  40. /// 播放
  41. /// </summary>
  42. /// <returns>播放成功为True,播放失败为False</returns>
  43. public bool Play()
  44. {
  45. if (!SettingConfig.Instance.IsVideo)
  46. {
  47. if (ImagePlay())
  48. {
  49. return true;
  50. }
  51. else
  52. {
  53. return false;
  54. }
  55. }
  56. else
  57. {
  58. if (VideoPlay())
  59. {
  60. return true;
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67. }
  68. private bool ImagePlay()
  69. {
  70. try
  71. {
  72. if (string.IsNullOrEmpty(_filepath))
  73. {
  74. Logger.Info("Image File Path is Empty!!!");
  75. return false;
  76. }
  77. var bitmap = new Bitmap(_filepath);
  78. OnNewFrame(bitmap);
  79. bitmap.Dispose();
  80. return true;
  81. }
  82. catch (Exception ex)
  83. {
  84. Logger.Info($"Image Play Error: {ex} ");
  85. return false;
  86. }
  87. }
  88. private void OnNewFrame(Bitmap e)
  89. {
  90. InputFrameReceived?.Invoke(this, e);
  91. }
  92. private bool VideoPlay()
  93. {
  94. try
  95. {
  96. if (string.IsNullOrEmpty(_filepath))
  97. {
  98. Logger.Info("Video File Path is Empty!!!");
  99. return false;
  100. }
  101. _videoFileReader = new VideoFileReader();
  102. _videoFileReader.Open(_filepath);
  103. var frameRate = _videoFileReader.FrameRate.ToDouble();
  104. _frameIntervalTime = (int)(1000 / frameRate);
  105. _frameCount = (int)_videoFileReader.FrameCount;
  106. StartPlayThread();
  107. return true;
  108. }
  109. catch
  110. {
  111. return false;
  112. }
  113. }
  114. private void DoVideoPlay()
  115. {
  116. _startTickCount = -1;
  117. while (!_pausing)
  118. {
  119. Bitmap bitmap = null;
  120. lock (_videoReaderLocker)
  121. {
  122. try
  123. {
  124. bitmap = _videoFileReader?.ReadVideoFrame(_frameIndex);
  125. }
  126. catch (Exception ex)
  127. {
  128. MessageBox.Show($"ReadVideoFrame Error:{ex}");
  129. }
  130. }
  131. if (bitmap == null)
  132. {
  133. break;
  134. }
  135. OnNewFrame(bitmap);
  136. var timeUsed = Environment.TickCount - _startTickCount;
  137. bitmap.Dispose();
  138. if (_startTickCount == -1)
  139. {
  140. _displayCount = 0;
  141. _startTickCount = Environment.TickCount;
  142. }
  143. else
  144. {
  145. var currentTickCount = Environment.TickCount;
  146. var fpsTimeUsed = currentTickCount - _startTickCount;
  147. _displayCount++;
  148. if (fpsTimeUsed > 1000)
  149. {
  150. var fps = (int)((double)_displayCount / fpsTimeUsed * 1000);
  151. if (_fps != fps)
  152. {
  153. _fps = fps;
  154. FPSChanged?.Invoke(this, _fps);
  155. }
  156. _startTickCount = currentTickCount;
  157. _displayCount = 0;
  158. }
  159. }
  160. var waitTime = _frameIntervalTime - timeUsed;
  161. Thread.Sleep(waitTime < 0 ? 1 : waitTime);
  162. // 如果到尾了,就从头开始
  163. if (_frameIndex == _frameCount - 1)
  164. {
  165. _frameIndex = 0;
  166. }
  167. else
  168. {
  169. _frameIndex += 1;
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 暂停播放
  175. /// </summary>
  176. public void Pause()
  177. {
  178. if (SettingConfig.Instance.IsVideo)
  179. {
  180. _pausing = true;
  181. }
  182. }
  183. /// <summary>
  184. /// 继续播放
  185. /// </summary>
  186. public void Continue()
  187. {
  188. if (SettingConfig.Instance.IsVideo)
  189. {
  190. StartPlayThread();
  191. }
  192. }
  193. private void StartPlayThread()
  194. {
  195. _pausing = false;
  196. if (_playThread == null || !_playThread.IsAlive)
  197. {
  198. _playThread = new Thread(() => DoVideoPlay())
  199. {
  200. IsBackground = true,
  201. Name = "AIDiagnosisDemo_FileVideoPlay",
  202. };
  203. _playThread.Start();
  204. }
  205. }
  206. /// <summary>
  207. /// 停止播放
  208. /// </summary>
  209. public void Stop()
  210. {
  211. _pausing = true;
  212. lock (_videoReaderLocker)
  213. {
  214. if (_videoFileReader != null)
  215. {
  216. _videoFileReader.Close();
  217. _videoFileReader.Dispose();
  218. _videoFileReader = null;
  219. }
  220. }
  221. }
  222. }
  223. }