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.

177 lines
5.3 KiB

2 years ago
using System.Runtime.InteropServices;
namespace EC.Util.CameraSDK;
public class HiKSDK : ICameraSDK
{
#region Attr
private int LoginId { get; set; } = -1;
private int RealplayHandle { get; set; } = -1;
2 years ago
#endregion Attr
public HiKSDK(CameraInfo cameraInfo) : base(cameraInfo)
{
}
#region Base Method
public override bool Init()
{
bool ret = ConnectSuccess();
if (ret) return true;
HiKOriSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new();
LoginId = HiKOriSDK.NET_DVR_Login_V30(CameraInfo.Ip, CameraInfo.Port, CameraInfo.UserName, CameraInfo.Password, ref deviceInfo);
ret = ConnectSuccess();
return ret;
}
public override bool Destory()
{
bool ret = ConnectSuccess();
if (!ret) return true;
ret = HiKOriSDK.NET_DVR_Logout(LoginId);
if (ret) LoginId = -1;
return ret;
}
public override bool ConnectSuccess()
{
return LoginId >= 0;
}
internal override void BuildException()
2 years ago
{
uint errCode = HiKOriSDK.NET_DVR_GetLastError();
if (errCode == 0) return;
throw CameraException.New(CameraInfo, (int)errCode);
2 years ago
}
#endregion Base Method
#region Ptz Method
2 years ago
private static class GPIParams
{
public static int Size { get; private set; }
public static Type Type { get; private set; }
static GPIParams()
{
HiKOriSDK.NET_DVR_PTZPOS ptzPos = new();
Size = Marshal.SizeOf(ptzPos);
Type = ptzPos.GetType();
}
}
public override PtzInfo GetPtzInfo()
{
bool ret = ConnectSuccess();
if (!ret) return PtzInfo.Default;
HiKOriSDK.NET_DVR_PTZPOS entity = new();
int dwSize = GPIParams.Size;
uint dwReturned = 0;
IntPtr ptrBuf = Marshal.AllocHGlobal(dwSize);
Marshal.StructureToPtr(entity, ptrBuf, true);
try
{
ret = HiKOriSDK.NET_DVR_GetDVRConfig(LoginId, HiKOriSDK.NET_DVR_GET_PTZPOS, 0, ptrBuf, (uint)dwSize, ref dwReturned);
if (!ret) { BuildException(); return PtzInfo.Default; }
object? objBuf = Marshal.PtrToStructure(ptrBuf, GPIParams.Type);
if (objBuf == null) return PtzInfo.Default;
entity = (HiKOriSDK.NET_DVR_PTZPOS)objBuf;
return PtzInfo.New(entity.wPanPos, entity.wTiltPos, entity.wZoomPos);
}
finally
{
Marshal.FreeHGlobal(ptrBuf);
}
}
public override bool TryGetPtzInfo(out PtzInfo ptzInfo)
{
bool ret = ConnectSuccess();
if (!ret) { ptzInfo = PtzInfo.Default; return false; }
HiKOriSDK.NET_DVR_PTZPOS entity = new();
int dwSize = GPIParams.Size;
uint dwReturned = 0;
IntPtr ptrBuf = Marshal.AllocHGlobal(dwSize);
Marshal.StructureToPtr(entity, ptrBuf, true);
try
{
ret = HiKOriSDK.NET_DVR_GetDVRConfig(LoginId, HiKOriSDK.NET_DVR_GET_PTZPOS, 0, ptrBuf, (uint)dwSize, ref dwReturned);
if (!ret) { BuildException(); ptzInfo = PtzInfo.Default; return false; }
object? objBuf = Marshal.PtrToStructure(ptrBuf, GPIParams.Type);
if (objBuf == null) { ptzInfo = PtzInfo.Default; return false; }
entity = (HiKOriSDK.NET_DVR_PTZPOS)objBuf;
ptzInfo = PtzInfo.New(entity.wPanPos, entity.wTiltPos, entity.wZoomPos);
return true;
}
finally
{
Marshal.FreeHGlobal(ptrBuf);
}
}
public override bool PtzMove(int cmd, int stop, int speed)
2 years ago
{
if (!ConnectSuccess()) return false;
2 years ago
int lChannel = 1;
bool flag = HiKOriSDK.NET_DVR_PTZControlWithSpeed_Other(LoginId, lChannel, (uint)cmd, (uint)stop, (uint)speed);
//if (flag) BuildException();
return flag;
2 years ago
}
public override bool PtzPreset(int cmd, int presetId)
2 years ago
{
if (!ConnectSuccess()) return false;
2 years ago
int lChannel = 1;
return HiKOriSDK.NET_DVR_PTZPreset_Other(LoginId, lChannel, (uint)cmd, (uint)presetId);
2 years ago
}
#endregion Ptz Method
#region Video Method
public override void StartPlay(IntPtr hwnd)
{
if (!ConnectSuccess() || IsPlaying()) return;
HiKOriSDK.NET_DVR_PREVIEWINFO previewInfo = new()
{
hPlayWnd = hwnd, //预览窗口
lChannel = 1, //预览的设备通道
dwStreamType = 0, //码流类型:0-主码流,1-子码流,2-码流3,3-码流4,以此类推
dwLinkMode = 0, //连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP
bBlocked = true, //0- 非阻塞取流,1- 阻塞取流
dwDisplayBufNum = 1, //播放库播放缓冲区最大缓冲帧数
byProtoType = 0,
byPreviewMode = 0
};
IntPtr pUser = new(); //用户数据
RealplayHandle = HiKOriSDK.NET_DVR_RealPlay_V40(LoginId, ref previewInfo, null, pUser);
if (RealplayHandle < 0) BuildException();
}
public override void StopPlay()
{
if (!IsPlaying()) return;
bool flag = HiKOriSDK.NET_DVR_StopRealPlay(RealplayHandle);
RealplayHandle = -1;
if (!flag) BuildException();
}
public override bool IsPlaying()
{
return RealplayHandle >= 0;
}
#endregion Video Method
2 years ago
}