using EC.Entity.Video;
using EC.Entity.Warning;
using EC.FFmpegAutoGen;
using EC.Onvif;
using EC.UsingEventAggregator;
using EC.Utils;
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
{
///
/// UCFFmpegPlayer.xaml 的交互逻辑
///
public partial class UCAIWarningPlayer : UserControl, IUCPlayer
{
///
/// 相机IP
///
private string _cameraIp { get; set; }
///
/// 视频播放器序号
///
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; }
///
/// 更新时间
///
private DateTime _upDateTime { get; set; }
///
/// 最大化最小化用
///
double _normaWidth = 0;
double _normaHeight = 0;
///
/// 窗体大小
///
double _playWidth = 0;
double _playHeight = 0;
///
/// 最大化
///
bool _windowStateMax = false;
public event EventHandler OnFromClose;
IEventAggregator _ea;
//SubscriberSocket Sub_Scriber { get; set; } = null;
string subServerAddress = ConfigHelper.SUBImageServer();
public UCAIWarningPlayer(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;
}
///
/// 设置窗体大小
///
///
///
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;
Task.Factory.StartNew(() => {
Thread.Sleep(2000);
//SetImageSource(null);
ShowMsg("目标消失");
});
OnFromClose?.Invoke(this, _videoIndex);
}
}
public void UPDateTime()
{
_upDateTime = DateTime.Now;
}
///
/// 判断预警是否接触
///
///
public bool IsTimeOut()
{
DateTime now = DateTime.Now;
double difSeconds = DateUnit.DateDiffSeconds(_upDateTime, now);
return difSeconds > 4;
}
public string GetCameraIp()
{
if (_camera != null)
{
return _camera.Ip;
}
return "";
}
///
/// 播放器类型
///
///
public int GetPlayerType()
{
return _videoType;
}
///
/// 相机
///
///
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;
}
///
/// 是否是空播放器
///
///
public bool IsNullPlay()
{
return _camera == null;
}
///
/// 是否播放
///
bool _playing { get; set; } = false;
/////
///// 播放成功
/////
//bool _playingSuccess = false;
///
/// 是否正在播放
///
///
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;
}
///
/// 是否显示
///
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, _videoIndex);
}
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 PlayerMouseDown;
public event EventHandler 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);
}
///
/// 设置最大化
///
///
///
///
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
}
}