123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using AForge.Video;
- using AForge.Video.DirectShow;
- using AIDiagnosisDemo.Infrastucture;
- using System;
- using System.Drawing;
- using System.Linq;
- using System.Threading;
- namespace AIDiagnosisDemo.Service
- {
- internal class VideoCapturePlayer : IPlayer
- {
- private readonly string _deviceId;
- private readonly object _deviceLocker = new object();
- private int _startTickCount;
- private int _displayCount;
- private int _fps;
- /// <summary>
- /// 接收到图像输入的事件
- /// </summary>
- public event EventHandler<Bitmap> InputFrameReceived;
- /// <summary>
- /// FPS变更的事件
- /// </summary>
- public event EventHandler<int> FPSChanged;
- private VideoCaptureDevice _videoCaptureDevice;
- /// <summary>
- /// VideoPlayer构造函数
- /// </summary>
- /// <param name="deviceId">Device Id</param>
- public VideoCapturePlayer(string deviceId)
- {
- _deviceId = deviceId;
- }
- /// <summary>
- /// 播放
- /// </summary>
- /// <param name="isVideo">是视频为True,是图片为False</param>
- /// <returns>播放成功为True,播放失败为False</returns>
- public bool Play()
- {
- lock (_deviceLocker)
- {
- try
- {
- var videoFilterInfos = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType<FilterInfo>();
- if (videoFilterInfos.FirstOrDefault(i => i.MonikerString == _deviceId) == null)
- {
- Logger.Info("Video Device is Empty!!!");
- return false;
- }
- _videoCaptureDevice = new VideoCaptureDevice(_deviceId);
- _videoCaptureDevice.NewFrame += OnNewFrame;
- _startTickCount = -1;
- _videoCaptureDevice.Start();
- return true;
- }
- catch
- {
- _videoCaptureDevice = null;
- Logger.Info("Video Device Play Fail!!!");
- }
- return false;
- }
- }
- private void OnNewFrame(object sender, NewFrameEventArgs eventArgs)
- {
- 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;
- }
- }
- InputFrameReceived?.Invoke(this, eventArgs.Frame);
- }
- /// <summary>
- /// 停止播放
- /// </summary>
- public void Stop()
- {
- lock (_deviceLocker)
- {
- if (_videoCaptureDevice == null)
- {
- return;
- }
- try
- {
- _videoCaptureDevice.NewFrame -= OnNewFrame;
- if (_videoCaptureDevice.IsRunning)
- {
- DoStop();
- }
- }
- catch
- {
- }
- finally
- {
- _videoCaptureDevice = null;
- }
- }
- }
- private void DoStop()
- {
- var stopThread = new Thread(() =>
- {
- try
- {
- _videoCaptureDevice.SignalToStop();
- _videoCaptureDevice.WaitForStop();
- }
- catch
- {
- }
- });
- stopThread.Start();
- try
- {
- if (!stopThread.Join(2000))
- {
- stopThread.Abort();
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// 继续播放
- /// </summary>
- public void Continue()
- {
- Play();
- }
- /// <summary>
- /// 暂停播放
- /// </summary>
- public void Pause()
- {
- Stop();
- }
- }
- }
|