using System.Runtime.InteropServices;

namespace SHH.CameraSdk;

/// <summary>
/// 大华 PlaySDK 核心接口封装 (play.dll)
/// </summary>
public static class DahuaPlaySDK
{
    private const string DLL_PATH = "Drivers\\Dahua\\play.dll";

    // 解码回调委托
    public delegate void DECCBFUN(int nPort, IntPtr pBuf, int nSize, ref FRAME_INFO pFrameInfo, IntPtr nUser, int nReserved2);

    [StructLayout(LayoutKind.Sequential)]
    public struct FRAME_INFO
    {
        public int nWidth;
        public int nHeight;
        public int nStamp;
        public int nType;
        public int nFrameRate;
        public uint dwFrameNum;
    }

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_GetFreePort(ref int plPort);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_ReleasePort(int nPort);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_OpenStream(int nPort, IntPtr pFileHead, uint nSize, uint nBufPoolSize);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_CloseStream(int nPort);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_Play(int nPort, IntPtr hWnd);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_Stop(int nPort);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_InputData(int nPort, IntPtr pBuf, uint nSize);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_SetDecCallBack(int nPort, DECCBFUN DecCBFun);

    [DllImport(DLL_PATH)]
    public static extern bool PLAY_SetStreamOpenMode(int nPort, uint nMode);

    // 解码模式枚举
    public enum DecodeType
    {
        DECODE_SW = 1,          // 软解 (CPU)
        DECODE_HW = 2,          // 硬解拷贝模式 (GPU解码后拷贝回内存)
        DECODE_HW_FAST = 3,     // 硬解直接显示模式 (GPU解码直接渲染，最高性能)
        DECODE_HW_NV_CUDA = 7,  // 英伟达显卡 CUDA 硬解 (Ayay 推荐，多路并发最强)
        DECODE_HW_D3D11 = 8     // D3D11 硬解
    }

    // 渲染模式枚举
    public enum RenderType
    {
        RENDER_GDI = 1,
        RENDER_D3D9 = 4,
        RENDER_D3D11 = 7
    }

    [DllImport(DLL_PATH, EntryPoint = "PLAY_SetEngine")]
    public static extern bool PLAY_SetEngine(int nPort, DecodeType decodeType, RenderType renderType);
}