You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

431 lines
11 KiB

3 years ago
using EC.Entity.Video;
using EC.Entity.Warning;
using EC.FFmpegAutoGen;
using EC.Onvif;
using EC.UsingEventAggregator;
using EC.Utils;
using EC.Utils.Config;
using EC.Utils.ZMQ;
using NetMQ;
using NetMQ.Sockets;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Brushes = System.Windows.Media.Brushes;
using Rectangle = System.Windows.Shapes.Rectangle;
namespace ECMonitor.UC.Videos
{
/// <summary>
/// UCFFmpegPlayer.xaml 的交互逻辑
/// </summary>
public partial class UCAIFilePlayer : UserControl, IUCPlayer
{
/// <summary>
/// 相机IP
/// </summary>
private string _cameraIp { get; set; }
/// <summary>
/// 视频播放器序号
/// </summary>
private int _videoIndex { get; set; } = 0;
private string _message { get; set; }
private string _ucName { get; set; }
public MonitorCamera _camera { get; set; }
private OnvifClient _onvifClient { get; set; }
public int _videoType { get; set; }
/// <summary>
/// 更新时间
/// </summary>
private DateTime _upDateTime { get; set; }
/// <summary>
/// 最大化最小化用
/// </summary>
double _normaWidth = 0;
double _normaHeight = 0;
/// <summary>
/// 窗体大小
/// </summary>
double _playWidth = 0;
double _playHeight = 0;
/// <summary>
/// 最大化
/// </summary>
bool _windowStateMax = false;
public event EventHandler OnFromClose;
IEventAggregator _ea;
//SubscriberSocket Sub_Scriber { get; set; } = null;
string subServerAddress = ConfigHelper.SUBImageServer();
public UCAIFilePlayer(IEventAggregator ea, double width, double height, int videoIndex, bool showCloseButton = false)
{
_ea = ea;
InitializeComponent();
this.Name = "UCPlayer" + videoIndex;
_ucName = this.Name;
this.topPlane.Visibility = showCloseButton ? Visibility.Visible : Visibility.Hidden;
ReSetSize(width, height);
ShowMsg("没有视频");
_videoIndex = videoIndex;
}
event EventHandler<int> IUCPlayer.OnFromClose
{
add
{
throw new NotImplementedException();
}
remove
{
throw new NotImplementedException();
}
}
/// <summary>
/// 设置窗体大小
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void ReSetSize(double width, double height)
{
this.Width = width;
this.Height = height;
this._playWidth = width;
this._playHeight = height;
this.imgPlayer.Height = height;
SetSelected(false);
}
public bool Play(MonitorCamera camera,bool replay = false)
{
if (IsPlaying())
{
LogFactory.GetLogger().Debug(" 并行冲突old " +_camera.Ip+"正在播放" +"new Ip="+ camera.Ip );
return false;
}
_camera = camera;
if (_camera == null)
{
return false;
}
_playing = true;
Task.Factory.StartNew(() => Play());
return true;
}
object lockplayer { get; set; } = new object();
public void Stop()
{
lock (lockplayer)
{
_playing = false;
_camera = null;
SetImageSource(null);
Task.Factory.StartNew(() => {
Thread.Sleep(2000);
// SetImageSource(null);
ShowMsg("目标消失");
});
OnFromClose?.Invoke(this, null);
}
}
public void UPDateTime()
{
_upDateTime = DateTime.Now;
}
/// <summary>
/// 判断预警是否接触
/// </summary>
/// <returns></returns>
public bool IsTimeOut()
{
DateTime now = DateTime.Now;
double difSeconds = DateUnit.DateDiffSeconds(_upDateTime, now);
return difSeconds > 3;
}
public string GetCameraIp()
{
if (_camera != null)
{
return _camera.Ip;
}
return "";
}
/// <summary>
/// 播放器类型
/// </summary>
/// <returns></returns>
public int GetPlayerType()
{
return _videoType;
}
/// <summary>
/// 相机
/// </summary>
/// <returns></returns>
public MonitorCamera GetCamera()
{
return _camera;
}
public OnvifClient GetOnvifClient()
{
return _onvifClient;
}
public UserControl GetControl()
{
return this;
}
public string GetMessage()
{
return _message;
}
public string GetName()
{
return _ucName;
}
/// <summary>
/// 是否是空播放器
/// </summary>
/// <returns></returns>
public bool IsNullPlay()
{
return _camera == null;
}
/// <summary>
/// 是否播放
/// </summary>
bool _playing { get; set; } = false;
///// <summary>
///// 播放成功
///// </summary>
//bool _playingSuccess = false;
/// <summary>
/// 是否正在播放
/// </summary>
/// <returns></returns>
public bool IsPlaying()
{
if (_camera == null)
{
return false;
}
return _playing;//&& _playingSuccess;
}
private int listIndex = 0;
public int GetIndex()
{
return listIndex;
}
public void SetIndex(int index)
{
listIndex = index;
}
/// <summary>
/// 是否显示
/// </summary>
private bool IsShow = false;
public bool GetIsShow()
{
return IsShow;
}
public void SetIsShow(bool isShow)
{
IsShow = isShow;
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
Stop();
OnFromClose?.Invoke(sender, e);
}
public delegate void dlgtSetImage(byte[] ms);
void SetImageSource(byte[] ms)
{
this.imgPlayer.Dispatcher.Invoke(new dlgtSetImage(SetImage), ms);
}
void SetImage(byte[] ms)
{
ShowMsg("");
if (!_playing|| ms == null)
{
imgPlayer.Source = null; // done!
return;
}
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(ms); // 不要直接使用 ms
bi.EndInit();
imgPlayer.Source = bi; // done!
}
private void Play()
{
using SubscriberSocket Sub_Scriber = new SubscriberSocket();
lock (lockplayer)
{
if (_camera == null)
{
return;
}
UPDateTime();
Sub_Scriber.Connect(subServerAddress);
string topic = ZMQTopic.AI_Detect_Warning_Image + _camera.Ip.Replace('.', '_');
Sub_Scriber.Subscribe(topic);
}
while (_playing)
{
try
{
bool more = false;
var timeout = new TimeSpan(0, 0, 2);
var rectopic = "";
Sub_Scriber.TryReceiveFrameString(timeout, out rectopic, out more);
if (_playing&&more)
{
byte[] data = Sub_Scriber.ReceiveFrameBytes();
UPDateTime();
SetImageSource(data);
}
}
catch (Exception ex)
{
LogFactory.GetLogger().Error(ex);
}
}
LogFactory.GetLogger().Info("跟踪结束了--------------");
}
public void ShowMsg(string message)
{
this.txtShow.Dispatcher.Invoke(new Action(() => { this.txtShow.Text = message; }));
}
private void Log(string message, bool isErr = false)
{
ShowMsg(message);
}
public void SetSelected(bool selected)
{
Thickness thickness = new Thickness(0);
if (selected)
{
thickness = new Thickness(1);
}
this.playerBorder.Dispatcher.Invoke(new Action(() => { playerBorder.BorderThickness = thickness; }));
}
#region 事件
public event EventHandler<int> PlayerMouseDown;
public event EventHandler<int> PlayerMouseDoubleClick;
private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
{
PlayerMouseDown?.Invoke(this, _videoIndex);
}
private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
PlayerMouseDoubleClick?.Invoke(this, _videoIndex);
}
/// <summary>
/// 设置最大化
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public bool SetMaximized(double width, double height)
{
if (_windowStateMax == false)
{
_normaWidth = this.Width;
_normaHeight = this.Height;
_windowStateMax = true;
ReSetSize(width, height);
}
else
{
_windowStateMax = false;
ReSetSize(_normaWidth, _normaHeight);
}
return _windowStateMax;
}
#endregion
}
}