123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- using Accord.Video.FFMPEG;
- using AIDiagnosisDemo.Infrastucture;
- using AIDiagnosisDemo.Settings;
- using System;
- using System.Drawing;
- using System.Threading;
- using System.Windows;
- namespace AIDiagnosisDemo.Service
- {
- internal class FileVideoPlayer : IPlayer
- {
- private string _filepath;
- private VideoFileReader _videoFileReader;
- private Thread _playThread;
- private bool _pausing;
- private int _startTickCount;
- private int _displayCount;
- private int _fps;
- private readonly object _videoReaderLocker = new object();
- private volatile int _frameIntervalTime = 0;
- private volatile int _frameCount = 0;
- private volatile int _frameIndex = 0;
- /// <summary>
- /// 接收到图像输入的事件
- /// </summary>
- public event EventHandler<Bitmap> InputFrameReceived;
- /// <summary>
- /// FPS变更的事件
- /// </summary>
- public event EventHandler<int> FPSChanged;
- /// <summary>
- /// FileVideoPlayer构造函数
- /// </summary>
- /// <param name="filepath">文件路径</param>
- public FileVideoPlayer(string filepath)
- {
- _filepath = filepath;
- }
- /// <summary>
- /// 播放
- /// </summary>
- /// <returns>播放成功为True,播放失败为False</returns>
- public bool Play()
- {
- if (!SettingConfig.Instance.IsVideo)
- {
- if (ImagePlay())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- if (VideoPlay())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- private bool ImagePlay()
- {
- try
- {
- if (string.IsNullOrEmpty(_filepath))
- {
- Logger.Info("Image File Path is Empty!!!");
- return false;
- }
- var bitmap = new Bitmap(_filepath);
- OnNewFrame(bitmap);
- bitmap.Dispose();
- return true;
- }
- catch (Exception ex)
- {
- Logger.Info($"Image Play Error: {ex} ");
- return false;
- }
- }
- private void OnNewFrame(Bitmap e)
- {
- InputFrameReceived?.Invoke(this, e);
- }
- private bool VideoPlay()
- {
- try
- {
- if (string.IsNullOrEmpty(_filepath))
- {
- Logger.Info("Video File Path is Empty!!!");
- return false;
- }
- _videoFileReader = new VideoFileReader();
- _videoFileReader.Open(_filepath);
- var frameRate = _videoFileReader.FrameRate.ToDouble();
- _frameIntervalTime = (int)(1000 / frameRate);
- _frameCount = (int)_videoFileReader.FrameCount;
- StartPlayThread();
- return true;
- }
- catch
- {
- return false;
- }
- }
- private void DoVideoPlay()
- {
- _startTickCount = -1;
- while (!_pausing)
- {
- Bitmap bitmap = null;
- lock (_videoReaderLocker)
- {
- try
- {
- bitmap = _videoFileReader?.ReadVideoFrame(_frameIndex);
- }
- catch (Exception ex)
- {
- MessageBox.Show($"ReadVideoFrame Error:{ex}");
- }
- }
- if (bitmap == null)
- {
- break;
- }
- OnNewFrame(bitmap);
- var timeUsed = Environment.TickCount - _startTickCount;
- bitmap.Dispose();
- if (_startTickCount == -1)
- {
- _displayCount = 0;
- _startTickCount = Environment.TickCount;
- }
- else
- {
- var currentTickCount = Environment.TickCount;
- var fpsTimeUsed = currentTickCount - _startTickCount;
- _displayCount++;
- if (fpsTimeUsed > 1000)
- {
- var fps = (int)((double)_displayCount / fpsTimeUsed * 1000);
- if (_fps != fps)
- {
- _fps = fps;
- FPSChanged?.Invoke(this, _fps);
- }
- _startTickCount = currentTickCount;
- _displayCount = 0;
- }
- }
- var waitTime = _frameIntervalTime - timeUsed;
- Thread.Sleep(waitTime < 0 ? 1 : waitTime);
- // 如果到尾了,就从头开始
- if (_frameIndex == _frameCount - 1)
- {
- _frameIndex = 0;
- }
- else
- {
- _frameIndex += 1;
- }
- }
- }
- /// <summary>
- /// 暂停播放
- /// </summary>
- public void Pause()
- {
- if (SettingConfig.Instance.IsVideo)
- {
- _pausing = true;
- }
- }
- /// <summary>
- /// 继续播放
- /// </summary>
- public void Continue()
- {
- if (SettingConfig.Instance.IsVideo)
- {
- StartPlayThread();
- }
- }
- private void StartPlayThread()
- {
- _pausing = false;
- if (_playThread == null || !_playThread.IsAlive)
- {
- _playThread = new Thread(() => DoVideoPlay())
- {
- IsBackground = true,
- Name = "AIDiagnosisDemo_FileVideoPlay",
- };
- _playThread.Start();
- }
- }
- /// <summary>
- /// 停止播放
- /// </summary>
- public void Stop()
- {
- _pausing = true;
- lock (_videoReaderLocker)
- {
- if (_videoFileReader != null)
- {
- _videoFileReader.Close();
- _videoFileReader.Dispose();
- _videoFileReader = null;
- }
- }
- }
- }
- }
|