using EC.Entity.Video; using EC.UsingEventAggregator; using ECMonitor.Code; using ECMonitor.Manager; using ECMonitor.UC.Videos; using Prism.Events; using Prism.Regions; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Controls; namespace ECMonitor.Page.CameraMonitor { /// /// MainVideo.xaml 的交互逻辑 /// public partial class CameraVideo : UserControl { public IRegionManager _regionManager { get; set; } public IEventAggregator _ea { get; set; } private VideoPlayManager _videoPlayManager { get; set; } = new VideoPlayManager(); private double _videoWidth = 400; private double _videoHeight = 300; //窗体长宽比,防止窗体变化时 窗体变形 private double _videoWH { get; set; } = -1; /// /// 当前选中播放器好 /// private int _playerSelectIndex { get; set; } = 0; /// /// 当天窗体内播放器数量 /// private int _videoNumber { get; set; } = 1; /// /// 上一次选择 按钮 窗体数 1窗 4窗 /// private int _barButtonSelectedIndex = 0; public CameraVideo(IRegionManager regionManager, IEventAggregator ea) { _regionManager = regionManager; _ea = ea; InitializeComponent(); _ea.GetEvent().Subscribe(PlayVideoReceived); } private void BarButtons_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (barButtons.SelectedIndex == -1) { barButtons.SelectedIndex = _barButtonSelectedIndex; return; } if (barButtons.SelectedIndex != _barButtonSelectedIndex) { BarButtonsSelectedChange(barButtons.SelectedIndex); } } private void BarButtonsSelectedChange(int selectedIndex) { if (_barButtonSelectedIndex == selectedIndex) { return; } _barButtonSelectedIndex = selectedIndex; switch (selectedIndex) { case 0: ChangePlayerGridByRowNum(1); break; case 1: ChangePlayerGridByRowNum(2); break; case 2: ChangePlayerGridByRowNum(3); break; } _playerSelectIndex = 0; _videoPlayManager.SetALLUnSelected(); _videoPlayManager.GetUCVideoList()[_playerSelectIndex]?.SetSelected(true); } /// /// 设置窗体每行播放器数量 /// /// private void ChangePlayerGridByRowNum(int rowNumber = 1) { if (_videoWH < 0) { return; } double width = panMain.ActualWidth; double height = _videoWH * width; //Padding =2,2,2,2 width = width - 4; height = height - 4; _videoWidth = width / rowNumber - 4; _videoHeight = height / rowNumber - 4; int showVideoNumber = rowNumber * rowNumber; _videoNumber = 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]; string ucname = videoPlay.GetName(); var rtspVideo = (UserControl)this.panMain.FindName(ucname); if (rtspVideo != null) { videoPlay.Stop(); // videoPlay.SetIsShow(false); this.panMain.Children.Remove(rtspVideo); this.panMain.UnregisterName(ucname); } } } 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.CreateVideo(_ea, _videoWidth, _videoHeight); 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; iUCVideo.PlayerMouseDoubleClick += PlayerMouseDoubleClick; iUCVideo.OnFromClose += OnFromClose; } } } private void OnFromClose(object sender, int index) { StaticData.MainPlayerIp = ""; } //双击 最大化、最小化 private void PlayerMouseDoubleClick(object sender, int index) { IUCPlayer player = (sender as IUCPlayer); double width = panMain.ActualWidth; double height = _videoWH * width; //Padding =2,2,2,2 width = width - 4; height = height - 4; bool isMax = player.SetMaximized(width, height); if (isMax) { this.panMain.Children.Remove(player.GetControl()); this.panMain.UnregisterName(player.GetName()); this.panMain1.Children.Add(player.GetControl()); this.panMain1.RegisterName(player.GetName(), player.GetControl()); } else { this.panMain1.Children.Remove(player.GetControl()); this.panMain1.UnregisterName(player.GetName()); this.panMain.Children.Insert(index, player.GetControl()); this.panMain.RegisterName(player.GetName(), player.GetControl()); } } /// /// 选中设置 /// /// /// private void PlayerMouseDown(object sender, int videoIndex) { _videoPlayManager.SetALLUnSelected(); IUCPlayer ucPlayer = (sender as IUCPlayer); ucPlayer.SetSelected(true); _playerSelectIndex = videoIndex; //rightRegion. if (ucPlayer.IsPlaying()) { //MonitorCamera curCamera = ucPlayer.GetCamera(); var curOnvifClient = ucPlayer.GetOnvifClient(); _ea.GetEvent().Publish(curOnvifClient); } else { _ea.GetEvent().Publish(null); } /// } //private void CreateVideo(string cameraIp) //{ // IUCVideo ucVideo = _videoPlayManager.GetUCVideo(cameraIp); // if (ucVideo != null) // { // ucVideo.UPDateTime(); // return; // } // MonitorCamera camera = CameraManager.GetCamera(cameraIp); // if (camera == null) // { // ShowLog("相机Ip:" + cameraIp + "不存在", true); // return; // } // UCRtspVlcVideo rtspVideo = new UCRtspVlcVideo(_videoWidth,_videoHeight); // this.panMain.Children.Add(rtspVideo); // rtspVideo.Play(); // this.panMain.RegisterName(rtspVideo.Name, rtspVideo); // _videoPlayManager.Add(ucVideo); //} //private void btnStop_Click(object sender, RoutedEventArgs e) //{ // var rtspVideo = _videoPlayManager.GetUCVideo("192.168.1.65"); // if (rtspVideo != null) // { // rtspVideo.Stop(); // } //} private void UserControl_Loaded(object sender, RoutedEventArgs e) { _videoWidth = panMain.ActualWidth; _videoHeight = panMain.ActualHeight; _videoWH = _videoHeight / _videoWidth; barButtons.SelectedIndex = ConfigHelper.MainBarButtonSelectedIndex(); } private void Button_Click(object sender, RoutedEventArgs e) { // await MessageManager.MessageBox("测试"); // Play("192.168.1.651"); _ea.GetEvent().Publish("192.168.1.65"); } public virtual void PlayVideoReceived(string cameraIp) { Play(cameraIp); } private void Play(string ip) { var videoPlay = _videoPlayManager.GetUCVideoList()[_playerSelectIndex]; if (videoPlay == null) { MessageManager.ShowMessage("当前选中的播放器 SelectIndex=" + _playerSelectIndex.ToString(), true); return; } if (!videoPlay.IsNullPlay()) { if (videoPlay.GetCameraIp() == ip) { if (videoPlay.IsPlaying()) { return; } } videoPlay.Stop(); Thread.Sleep(1000); } MonitorCamera camera = CameraManager.GetCamera(ip); if (camera == null) { MessageManager.ShowMessage(ip + "相机不存在", true); return; } bool play = videoPlay.Play(camera); AddSelectVideoIndex(); } public void AddSelectVideoIndex() { _playerSelectIndex++; if (_playerSelectIndex >= _videoNumber) { _playerSelectIndex = 0; } _videoPlayManager.SetALLUnSelected(); var ucPlayer = _videoPlayManager.GetUCVideoList()[_playerSelectIndex]; if (ucPlayer != null) { ucPlayer.SetSelected(true); if (ucPlayer.IsPlaying()) { //MonitorCamera curCamera = ucPlayer.GetCamera(); var curOnvifClient = ucPlayer.GetOnvifClient(); _ea.GetEvent().Publish(curOnvifClient); } else { _ea.GetEvent().Publish(null); } } } //窗体尺寸该表 private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) { if (_videoWH < 0) return; int toSelectIndex = _barButtonSelectedIndex; _barButtonSelectedIndex = -1; BarButtonsSelectedChange(toSelectIndex); } } }