using EC.Entity.Video; using EC.Utils; using EC.Utils.Config; using ECMonitor.Code; using ECMonitor.Manager; using ECMonitor.UC.Videos; using Prism.Events; using System; using System.Collections.Generic; using System.Linq; 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.Shapes; namespace ECMonitor.Views { /// /// FrmVideoWanging.xaml 的交互逻辑 /// public partial class FrmVideoWanging : Window { VideoPlayManager _videoPlayManager { get; set; } = new VideoPlayManager(); double _videoWidth = 400; double _videoHeight = 300; /// /// 正在播放的视频数量 /// private int _playerNumber { get; set; } = 0; /// ///当前窗体类型1,2(4),3(9),4(16) /// private int _playerRowNumber = 0; /// /// 播放器窗体数量 /// private int _playerWindowsNumber = 0; /// /// 自动运行 /// bool _running = true; /// /// 自动关闭窗体 /// bool? _autoClose { get; set; } = true; bool _formIsShow = false; IEventAggregator _ea; public FrmVideoWanging(IEventAggregator ea) { InitializeComponent(); _ea = ea; _autoClose = ConfigHelper.AutoWarningClose(); chkAutoClose.IsChecked = _autoClose; } #region 系统设置 private void Window_Loaded(object sender, RoutedEventArgs e) { ChangePlayerGridByRowNum(1); RunAutoClosePlayerServer(); } private void btnClose_Click(object sender, RoutedEventArgs e) { FormHide(); } public void ShowAndActive() { if (_formIsShow == false) { this.Dispatcher.Invoke(new Action(() => { this.Show(); this.Activate(); if (this.WindowState == WindowState.Minimized) { this.WindowState = WindowState.Normal; } })); _formIsShow = true; } } public void FormHide() { this.Dispatcher.Invoke(new Action(() => { this.Hide(); })); _formIsShow = false; } private void Window_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); } } #endregion /// /// 设置窗体每行播放器数量 /// /// private void ChangePlayerGridByRowNum(int rowNumber = 1) { this.Dispatcher.Invoke(new Action(() => { ChangePlayerGridByRowNumInvoke(rowNumber); })); } private void ChangePlayerGridByRowNumInvoke(int rowNumber = 1) { if (_playerRowNumber == rowNumber) { return; } _playerRowNumber = rowNumber; double width = 900; double height = 645; #region 计算窗体大小 double screenWidth = SystemParameters.PrimaryScreenWidth;//得到屏幕整体宽度 double screenHeight = SystemParameters.PrimaryScreenHeight;//得到屏幕整体高度 if (rowNumber < 4) { width = 900; height = 645; } else { width = 1200; height = 845; } this.Width = width; this.Height = height; this.Left = (screenWidth - width) / 2 - 10; double top = (screenHeight - height) / 2 - 20; if (top < 10) top = 10; this.Top = top; #endregion //Padding =2,2,2,2 width = width - 4; height = height - 4-45; _videoWidth = width / rowNumber - 5; _videoHeight = height / rowNumber - 5; int showVideoNumber = rowNumber * rowNumber; _playerWindowsNumber = showVideoNumber; int addedVideoNumber = _videoPlayManager.GetUCVideoList().Count(); int dif = showVideoNumber - addedVideoNumber; //已经添加的 多余界面 if (dif < 0) { //设置界面大小 for (int i = 0; i < showVideoNumber; i++) { var videoPlay = _videoPlayManager.GetUCVideoList()[i]; videoPlay.ReSetSize(_videoWidth, _videoHeight); string ucname = videoPlay.GetName(); var rtspVideo = (UserControl)this.panMain.FindName(ucname); if (rtspVideo == null)//如果没有添加的添加 { this.panMain.Children.Add(videoPlay.GetControl()); this.panMain.RegisterName(videoPlay.GetName(), videoPlay.GetControl()); } } //从界面删除 for (int i = showVideoNumber; i < addedVideoNumber; i++) { var videoPlay = _videoPlayManager.GetUCVideoList()[i]; RemoveFromView(videoPlay.GetName()); } } else if (dif >= 0) ///已经添加的少于界面 { //设置界面大小 for (int i = 0; i < addedVideoNumber; i++) { var videoPlay = _videoPlayManager.GetUCVideoList()[i]; videoPlay.ReSetSize(_videoWidth, _videoHeight); string ucname = videoPlay.GetName(); var rtspVideo = (UserControl)this.panMain.FindName(ucname); if (rtspVideo == null) { this.panMain.Children.Add(videoPlay.GetControl()); this.panMain.RegisterName(videoPlay.GetName(), videoPlay.GetControl()); } } //添加新的视频窗体 for (int i = 0; i < dif; i++) { var iUCVideo = _videoPlayManager.CreateWaringVideo(_ea,_videoWidth, _videoHeight,0,false); var ucvideo = iUCVideo.GetControl(); ucvideo.Margin = new Thickness(2, 2, 2, 2); this.panMain.Children.Add(ucvideo); this.panMain.RegisterName(ucvideo.Name, ucvideo); iUCVideo.PlayerMouseDown += PlayerMouseDown; } } } /// /// 选中设置 /// /// /// private void PlayerMouseDown(object sender, int videoIndex) { _videoPlayManager.SetALLUnSelected(); (sender as IUCPlayer).SetSelected(true); } private void Play(string ip, int playNumber) { try { int playerIndex = playNumber - 1; var ucVideoList = _videoPlayManager.GetUCVideoList(); int icount = ucVideoList.Count(); if(icount< playNumber){ LogFactory.GetLogger().Debug("videoPlayManager.GetUCVideoList count ="+ icount); return; } var videoPlay = ucVideoList[playerIndex]; if (videoPlay == null) { MessageManager.ShowMessage("当前选中的播放器 SelectIndex=" + playerIndex.ToString(), true); return; } MonitorCamera camera = CameraManager.GetCamera(ip); if (camera == null) { MessageManager.ShowMessage(ip + "相机没有在系统登记 临时创建 ", true); camera = new MonitorCamera(ip, 8000, "admin", "hk123456"); } bool play = videoPlay.Play(camera); }catch(Exception ex) { LogFactory.GetLogger().Debug(ex.Message); } } public void NewWarningPlay(string cameaIp) { lock (this) { int playNumber = 0; List playerList = _videoPlayManager.GetUCVideoList(); foreach (IUCPlayer player in playerList) { if (player.IsPlaying()) { playNumber++; if (player.GetCameraIp() == cameaIp)//发现已经在播放列表 { player.UPDateTime(); ShowAndActive(); LogFactory.GetLogger().Debug("return ameaIp==" + cameaIp); return; } } } playNumber++; ChangePlayerGridByAllPlayerNumber(playNumber); LogFactory.GetLogger().Debug(" Play--------cameaIp=" + cameaIp + ", playNumber="+ playNumber + " "); Play(cameaIp, playNumber); _playerNumber = playNumber; if (playNumber > 0) { ShowAndActive(); } } } /// ///通过播放器数量 修改播放排列表格 /// /// public void ChangePlayerGridByAllPlayerNumber(int playNumber) { if (playNumber <= _playerWindowsNumber) { return; } if (playNumber <= 1) { ChangePlayerGridByRowNum(1); } else if (playNumber <= 4) { ChangePlayerGridByRowNum(2); } else if (playNumber <= 9) { ChangePlayerGridByRowNum(3); } else if (playNumber <= 16) { ChangePlayerGridByRowNum(4); } else//太多预警没法显示 { return; } } private void Button_Click(object sender, RoutedEventArgs e) { NewWarningPlay("192.168.1.65"); NewWarningPlay("192.168.1.108"); //PlalersNumberChange(tmpplayerRowNumber); //tmpplayerRowNumber++; //if (tmpplayerRowNumber > 4) //{ // tmpplayerRowNumber = 1; //} } private void chkAutoClose_Click(object sender, RoutedEventArgs e) { _autoClose = chkAutoClose.IsChecked; //if (_autoClose == true) //{ // RunAutoClosePlayerServer(); //} } private void RunAutoClosePlayerServer() { Task.Factory.StartNew(() => { while (_running) { try { lock (this) { if (_playerNumber > 0) { AutoClose(); } } }catch(Exception ex) { LogFactory.GetLogger().Error(ex.Message); } Thread.Sleep(1000); } }); } private void AutoClose() { int playNumber = 0; List playerList = _videoPlayManager.GetUCVideoList(); int icount = playerList.Count(); for (int i = icount - 1; i >= 0; i--) { IUCPlayer player = playerList[i]; if (player.IsPlaying()) { if (player.IsTimeOut()) { player.Stop(); //if (i > 0) //{ RemoveFromView(player.GetName()); playerList.RemoveAt(i); //} playerList.Add(player); AddToView(player); continue; } playNumber++; } } if(playNumber<4 ) { if( _playerRowNumber >= 9) { ChangePlayerGridByAllPlayerNumber(playNumber); } } else if (playNumber < 8) { if ( _playerRowNumber >= 16) { ChangePlayerGridByAllPlayerNumber(playNumber); } } if (playNumber == 0)//关闭窗体时才修改窗体大小 { if (_autoClose==true) { FormHide(); ChangePlayerGridByAllPlayerNumber(playNumber); } } _playerNumber = playNumber; } /// /// 从窗体删除 /// /// private void RemoveFromView(string ucname) { this.panMain.Dispatcher.Invoke(new Action(() => { var uiplayer = (UserControl)this.panMain.FindName(ucname); if (uiplayer != null) { this.panMain.Children.Remove(uiplayer); this.panMain.UnregisterName(ucname); } })); } /// /// 从窗体删除 /// /// private void AddToView(IUCPlayer videoPlay) { this.panMain.Dispatcher.Invoke(new Action(() => { if (videoPlay != null) { this.panMain.Children.Add(videoPlay.GetControl()); this.panMain.RegisterName(videoPlay.GetName(), videoPlay.GetControl()); } })); } private void Window_Closed(object sender, EventArgs e) { _running = false; } } }