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.

180 lines
5.1 KiB

using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Threading;
using EC.Util.CameraSDK;
using JiLinApp.Core.Avalonia;
using JiLinApp.Docking.Ptz;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace JiLinApp.Components;
public partial class CameraRealPlay : Window
{
#region
private ICameraSdk? CameraSdk { get; set; }
#endregion
public CameraRealPlay()
{
InitializeComponent();
//绑定云台响应事件
List<Button> btnList = new();
ControlsUtil.FindChild(PtzCtrlPanel, ref btnList);
foreach (var btn in btnList)
{
if ((btn.Name ?? string.Empty).StartsWith("goto", StringComparison.OrdinalIgnoreCase)) btn.Click += GotoBtn_Click;
else
{
btn.AddHandler(PointerPressedEvent, PtzBtn_MouseDown, RoutingStrategies.Tunnel);
btn.AddHandler(PointerReleasedEvent, PtzBtn_MouseUp, RoutingStrategies.Tunnel);
}
}
// 初始化预置点 id 下拉框
for (int i = 1; i <= 10; i++) PresetIdCbx.Items.Add(new ComboBoxItem { Content = i });
PresetIdCbx.SelectedIndex = 0;
}
public CameraRealPlay(ICameraSdk cameraSdk) : this()
{
SetCameraSdk(cameraSdk);
}
#region Inner
public void SetCameraSdk(ICameraSdk cameraSdk)
{
CameraSdk = cameraSdk;
}
public IntPtr GetHwnd()
{
return player.Handle;
}
public bool IsClosed { get; private set; }
protected override void OnClosing(WindowClosingEventArgs e)
{
base.OnClosing(e);
IsClosed = true;
StopPlay();
}
#endregion Inner
#region Play
public void StartPlay()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
}
if (CameraSdk is HiKSdk hikSdk)
{
if (RealplayPort < 0)
{
int nPort = -1;
bool ret = PlayCtrlSdk.PlayM4_GetPort(ref nPort);
if (!ret) hikSdk.BuildPlayCtrlException(nPort);
RealplayPort = nPort;
}
hikSdk.RealDataCallBack = HikRealDataCallBack;
}
else if (CameraSdk is DaHuaSdk dhSdk)
{
}
else if (CameraSdk is YuShiSdk ysSdk)
{
}
CameraSdk?.StartPlay(player.Handle);
}
public void StopPlay()
{
Dispatcher.UIThread.InvokeAsync(() => CameraSdk?.StopPlay());
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
}
if (CameraSdk is HiKSdk hikSdk)
{
if (RealplayPort >= 0)
{
PlayCtrlSdk.PlayM4_Stop(RealplayPort);
PlayCtrlSdk.PlayM4_CloseStream(RealplayPort);
PlayCtrlSdk.PlayM4_FreePort(RealplayPort);
RealplayPort = -1;
}
hikSdk.RealDataCallBack = null;
}
else if (CameraSdk is DaHuaSdk dhSdk)
{
}
else if (CameraSdk is YuShiSdk ysSdk)
{
}
}
public bool IsPlaying()
{
return CameraSdk?.IsPlaying() ?? false;
}
private int RealplayPort { get; set; } = -1;
private void HikRealDataCallBack(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser)
{
if (RealplayPort < 0 || dwBufSize <= 0) return;
Dispatcher.UIThread.Invoke(() =>
{
switch (dwDataType)
{
case HiKOriSdk.NET_DVR_SYSHEAD:
PlayCtrlSdk.PlayM4_SetStreamOpenMode(RealplayPort, 0);
PlayCtrlSdk.PlayM4_OpenStream(RealplayPort, pBuffer, dwBufSize, 2 * 1024 * 1024);
PlayCtrlSdk.PlayM4_SetDisplayBuf(RealplayPort, 5);
PlayCtrlSdk.PlayM4_Play(RealplayPort, GetHwnd());
break;
case HiKOriSdk.NET_DVR_STREAMDATA:
PlayCtrlSdk.PlayM4_InputData(RealplayPort, pBuffer, dwBufSize);
break;
}
});
}
#endregion Play
#region ElementEvent
private void PtzBtn_MouseDown(object? sender, PointerPressedEventArgs e)
{
if (CameraSdk == null || sender is not Button btn) return;
PtzCmdType cmdType = PtzCmd.GetCmdType((btn.Name ?? string.Empty).Replace("Btn", ""));
int stop = 0;
PtzCameraCmd.PtzMove(CameraSdk, cmdType, new int[] { stop });
}
private void PtzBtn_MouseUp(object? sender, PointerReleasedEventArgs e)
{
if (CameraSdk == null || sender is not Button btn) return;
PtzCmdType cmdType = PtzCmd.GetCmdType((btn.Name ?? string.Empty).Replace("Btn", ""));
int stop = 1;
PtzCameraCmd.PtzMove(CameraSdk, cmdType, new int[] { stop });
}
private void GotoBtn_Click(object? sender, RoutedEventArgs e)
{
if (CameraSdk == null) return;
PtzCmdType cmdType = PtzCmdType.PresetGoto;
int presetId = int.Parse(PresetIdCbx.GetText());
PtzCameraCmd.PtzMove(CameraSdk, cmdType, new int[] { presetId });
}
#endregion ElementEvent
}