#ifndef HWSCAPTURE_H
#define HWSCAPTURE_H

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#ifndef NOMINMAX
#define NOMINMAX
#endif

#include <windows.h>

#ifdef __cplusplus
#define HWS_EXTERN_C extern "C"
#else
#define HWS_EXTERN_C extern
#endif

#ifdef HWSCAPTURE_EXPORTS
#define HWS_API HWS_EXTERN_C __declspec(dllexport)
#else
#define HWS_API HWS_EXTERN_C __declspec(dllimport)
#endif

typedef void* HWS_PORT;

#define HWS_SDK_VERSION_MAJOR 6
#define HWS_SDK_VERSION_MINOR 2
#define HWS_SDK_VERSION_PATCH 8
#define HWS_SDK_VERSION_STRING "6.2.8"
#define HWS_SDK_API_VERSION 60208

typedef struct HWS_VERSION
{
    int major;
    int minor;
    int patch;
} HWS_VERSION;

typedef enum HWS_RESULT
{
    HWS_OK = 0,
    HWS_E_FAIL = -1,
    HWS_E_INVALID_ARG = -2,
    HWS_E_NOT_INIT = -3,
    HWS_E_NOT_SUPPORT = -4,
    HWS_E_NOT_IMPLEMENTED = -5
} HWS_RESULT;

typedef enum HWS_PORT_TYPE
{
    HWS_PORT_UNKNOWN = 0,
    HWS_PORT_VIDEO_INPUT = 1,
    HWS_PORT_AUDIO_INPUT = 2,
    // Generic external video source. Applications can push processed frames
    // into SDK pipeline, then connect this port to render/OSD/recorder.
    HWS_PORT_VIDEO_FRAME_SOURCE = 3,
    // FileSource is a control port. Obtain its borrowed video/audio outputs
    // with HWS_FileSourceGetVideoOutput/HWS_FileSourceGetAudioOutput.
    HWS_PORT_FILE_SOURCE = 4,
    HWS_PORT_FILE_VIDEO_OUTPUT = 5,
    HWS_PORT_FILE_AUDIO_OUTPUT = 6,
    HWS_PORT_VIDEO_RENDER = 10,
    HWS_PORT_AUDIO_RENDER = 11,
    HWS_PORT_VIDEO_MUX = 20,
    HWS_PORT_AUDIO_MUX = 21,
    HWS_PORT_VIDEO_OSD = 22,
    HWS_PORT_VIDEO_CROP = 23,
    // HWS Color Engine: CPU 3D-LUT/color conversion module.
    // Typical graph: VideoInput -> ColorEngine -> Crop/OSD/Render/Recorder.
    HWS_PORT_VIDEO_COLOR_ENGINE = 24,
    HWS_PORT_RECORDER = 30,
    HWS_PORT_VIDEO_ENCODER = 31,
    HWS_PORT_AUDIO_ENCODER = 32
} HWS_PORT_TYPE;

typedef enum HWS_PIXEL_FORMAT
{
    HWS_PIXFMT_UNKNOWN = 0,
    HWS_PIXFMT_YUY2 = 1,
    HWS_PIXFMT_NV12 = 2,
    HWS_PIXFMT_RGB24 = 3,
    HWS_PIXFMT_RGB32 = 4,
    HWS_PIXFMT_MJPG = 5,
    // Compressed H.264 elementary stream from the capture device.
    // data[0] points to one compressed access unit, stride[0] is the byte size.
    HWS_PIXFMT_H264 = 6
} HWS_PIXEL_FORMAT;

typedef struct HWS_DEVICE_INFO
{
    int  index;
    char name[256];
    char id[512];
} HWS_DEVICE_INFO;

typedef enum HWS_COLOR_SPACE
{
    HWS_COLOR_SPACE_AUTO = 0,
    HWS_COLOR_SPACE_BT601 = 1,
    HWS_COLOR_SPACE_BT709 = 2,
    HWS_COLOR_SPACE_BT2020 = 3
} HWS_COLOR_SPACE;

typedef enum HWS_COLOR_RANGE
{
    HWS_COLOR_RANGE_AUTO = 0,
    HWS_COLOR_RANGE_FULL = 1,
    HWS_COLOR_RANGE_LIMITED = 2
} HWS_COLOR_RANGE;

typedef struct HWS_VIDEO_FORMAT
{
    int width;
    int height;
    int fpsNum;
    int fpsDen;
    HWS_PIXEL_FORMAT pixelFormat;
} HWS_VIDEO_FORMAT;

typedef HWS_VIDEO_FORMAT HWS_VIDEO_CAP;

typedef struct HWS_VIDEO_INPUT_CONFIG
{
    int deviceIndex;
    int width;
    int height;
    int fps;
    HWS_PIXEL_FORMAT pixelFormat;
    HWS_COLOR_SPACE colorSpace;
    HWS_COLOR_RANGE colorRange;
    int bufferCount;
} HWS_VIDEO_INPUT_CONFIG;

typedef enum HWS_RENDER_INTERPOLATION_MODE
{
    // Smooth bilinear sampling. This was the legacy renderer default.
    HWS_RENDER_INTERPOLATION_LINEAR = 0,
    // Nearest-neighbor sampling. Useful for desktop/text preview when you prefer
    // crisp edges over smooth scaling.
    HWS_RENDER_INTERPOLATION_POINT = 1,
    // Point sampling when the preview is effectively 1:1; linear otherwise.
    HWS_RENDER_INTERPOLATION_AUTO = 2
} HWS_RENDER_INTERPOLATION_MODE;

typedef enum HWS_SDK_PIPELINE_MODE
{
    // Default v6.0 route. Applications use CPU ColorEngine / CPU OSD / normal Recorder.
    // GPU is still used internally by the preview renderer when a HWND is supplied,
    // but LUT/OSD/FinalFrame effects are not enabled on the renderer.
    HWS_SDK_PIPELINE_MODE_CPU = 0,
    // High-performance route. Applications may use renderer GPU LUT, GPU OSD and
    // GPU FinalFrame recorder. This should be selected explicitly by applications
    // that require GPU compositing / 3D-LUT preview / FinalFrame recording.
    HWS_SDK_PIPELINE_MODE_GPU = 1
} HWS_SDK_PIPELINE_MODE;

typedef struct HWS_VIDEO_RENDER_CONFIG
{
    int width;
    int height;
    HWS_PIXEL_FORMAT pixelFormat;
} HWS_VIDEO_RENDER_CONFIG;

typedef struct HWS_VIDEO_CROP_RECT
{
    int x;
    int y;
    int width;
    int height;
} HWS_VIDEO_CROP_RECT;

typedef enum HWS_COLOR_ENGINE_PROCESSOR
{
    // CPU processor: outputs a normal CPU HWS_VIDEO_FRAME.
    // This path is kept for FPGA reference, recorder/snapshot, and systems without GPU.
    HWS_COLOR_ENGINE_PROCESSOR_CPU = 0,
    // GPU preview processor: ColorEngine itself bypasses CPU conversion and the
    // renderer applies the LUT in a D3D11 pixel shader. This is preview-only;
    // downstream recorder/callbacks receive the original frame unless another
    // CPU ColorEngine is inserted.
    HWS_COLOR_ENGINE_PROCESSOR_GPU_PREVIEW = 1,
    // Reserved for future policy selection. Current SDK treats AUTO as CPU.
    HWS_COLOR_ENGINE_PROCESSOR_AUTO = 2,
    // Design target for realtime product path: GPU LUT output becomes FinalFrame.
    // In v6.0 the renderer already keeps a source-size GPU final texture for
    // LUT preview/snapshot. Recorder GPU texture input is the next integration step.
    HWS_COLOR_ENGINE_PROCESSOR_GPU_FINAL = 3
} HWS_COLOR_ENGINE_PROCESSOR;


// Future/target routing flags for GPU FinalFrame pipeline.
// v6.0 uses these as documented policy values; existing APIs remain compatible.
typedef enum HWS_COLOR_OUTPUT_TARGET
{
    HWS_COLOR_OUTPUT_TARGET_PREVIEW  = 1 << 0,
    HWS_COLOR_OUTPUT_TARGET_SNAPSHOT = 1 << 1,
    HWS_COLOR_OUTPUT_TARGET_RECORD   = 1 << 2,
    HWS_COLOR_OUTPUT_TARGET_CALLBACK = 1 << 3
} HWS_COLOR_OUTPUT_TARGET;

typedef enum HWS_COLOR_OSD_ORDER
{
    // Recommended: video is color-graded first, then OSD/AI boxes are blended.
    HWS_COLOR_OSD_AFTER_LUT = 0,
    // Compatibility path: CPU OSD is upstream of GPU render LUT, so OSD is also color-graded.
    HWS_COLOR_OSD_BEFORE_LUT = 1
} HWS_COLOR_OSD_ORDER;

typedef struct HWS_COLOR_ENGINE_CONFIG
{
    // Processor selection. 0/CPU is the backward-compatible default.
    // Use HWS_COLOR_ENGINE_PROCESSOR_GPU_FINAL for realtime D3D11 preview/snapshot LUT.
    HWS_COLOR_ENGINE_PROCESSOR processor;
    // 0 = bypass/format-convert only; non-zero = apply loaded 3D-LUT.
    int enable;
    // HWS_PIXFMT_UNKNOWN keeps the input format. Supported explicit outputs:
    // HWS_PIXFMT_YUY2, HWS_PIXFMT_RGB24, HWS_PIXFMT_RGB32, HWS_PIXFMT_NV12.
    HWS_PIXEL_FORMAT outputPixelFormat;
    // Used for YUV <-> RGB conversion. AUTO: BT.709 for HD, BT.601 for SD.
    HWS_COLOR_SPACE colorSpace;
    // Used for YUV <-> RGB conversion. AUTO defaults to LIMITED for YUV video.
    HWS_COLOR_RANGE colorRange;
    // 0.0 = original color, 1.0 = full LUT result.
    float strength;
} HWS_COLOR_ENGINE_CONFIG;

typedef enum HWS_OSD_OUTPUT
{
    HWS_OSD_OUTPUT_BYPASS = 0,
    HWS_OSD_OUTPUT_OSD = 1
} HWS_OSD_OUTPUT;

typedef enum HWS_OSD_ITEM_TYPE
{
    HWS_OSD_ITEM_TEXT = 1,
    HWS_OSD_ITEM_IMAGE = 2,
    // Rectangle border overlay. Uses x/y/width/height/color/opacity.
    // fontSize is reused as border thickness; 0 means SDK default.
    HWS_OSD_ITEM_RECT = 3
} HWS_OSD_ITEM_TYPE;

typedef struct HWS_OSD_ITEM
{
    // Unique item id. If zero, the SDK assigns one internally.
    int id;
    int enable;
    HWS_OSD_ITEM_TYPE type;
    int zOrder;

    int x;
    int y;
    int width;
    int height;
    float opacity;

    // ARGB format: 0xAARRGGBB.
    unsigned int color;
    unsigned int backgroundColor;
    int transparentBackground;

    int fontSize;
    wchar_t text[512];
    wchar_t fontName[128];
    wchar_t filePath[MAX_PATH];
} HWS_OSD_ITEM;

typedef struct HWS_VIDEO_FRAME
{
    const unsigned char* data[4];
    int stride[4];
    int width;
    int height;
    HWS_PIXEL_FORMAT pixelFormat;
    long long timestamp100ns;
    long long frameIndex;
} HWS_VIDEO_FRAME;



typedef enum HWS_CONTAINER_FORMAT
{
    HWS_CONTAINER_MP4 = 1,
    HWS_CONTAINER_MOV = 2
} HWS_CONTAINER_FORMAT;

typedef enum HWS_VIDEO_CODEC
{
    HWS_VIDEO_CODEC_H264 = 1
} HWS_VIDEO_CODEC;

typedef enum HWS_RECORDER_SPLIT_MODE
{
    // Default legacy behavior: one file is finalized only when HWS_Stop/HWS_DestroyPort is called.
    HWS_RECORDER_SPLIT_NONE = 0,
    // SDK automatically finalizes current segment and opens a new file when file size reaches maxSegmentBytes.
    HWS_RECORDER_SPLIT_BY_SIZE = 1,
    // SDK automatically finalizes current segment and opens a new file when duration reaches segmentSeconds.
    HWS_RECORDER_SPLIT_BY_TIME = 2
} HWS_RECORDER_SPLIT_MODE;

typedef enum HWS_RECORDER_INPUT_MODE
{
    // Auto: raw H264 input is muxed directly; YUY2/NV12/RGB input is encoded to H264.
    HWS_RECORDER_INPUT_AUTO = 0,
    // Force existing path: convert uncompressed video to NV12 and encode to H264.
    HWS_RECORDER_INPUT_REENCODE = 1,
    // Force direct path: only accepts HWS_PIXFMT_H264 and muxes it without video re-encode.
    HWS_RECORDER_INPUT_DIRECT_H264 = 2,
    // Since SDK 6.2.8. Accept AudioInput without VideoInput and write AAC-only MP4/M4A.
    HWS_RECORDER_INPUT_AUDIO_ONLY = 3
} HWS_RECORDER_INPUT_MODE;

typedef struct HWS_RECORDER_CONFIG
{
    wchar_t filePath[MAX_PATH];
    HWS_CONTAINER_FORMAT container;
    HWS_VIDEO_CODEC videoCodec;
    int width;
    int height;
    int fps;       // Input/source FPS hint. Used when recordFps <= 0.
    int recordFps; // <=0: follow input/source FPS. >0: custom CFR recording FPS; recorder drops or duplicates frames to match it.
    int videoBitrate;
    int enableAudio;
    HWS_RECORDER_INPUT_MODE inputMode;

    // Since SDK 5.0. Keeps legacy behavior when splitMode is HWS_RECORDER_SPLIT_NONE.
    HWS_RECORDER_SPLIT_MODE splitMode;
    unsigned long long maxSegmentBytes; // e.g. 1024ULL * 1024ULL * 1024ULL for 1GB.
    int segmentSeconds;                // e.g. 30 * 60 for 30 minutes.
} HWS_RECORDER_CONFIG;

typedef enum HWS_ENCODED_VIDEO_FORMAT
{
    HWS_ENCODED_VIDEO_H264_ANNEXB = 1,
    // MP4/AVCC length-prefixed H.264 access unit. Used by direct H264 recording path.
    HWS_ENCODED_VIDEO_H264_AVCC = 2
} HWS_ENCODED_VIDEO_FORMAT;

typedef enum HWS_ENCODED_AUDIO_FORMAT
{
    HWS_ENCODED_AUDIO_AAC = 1
} HWS_ENCODED_AUDIO_FORMAT;

typedef struct HWS_ENCODED_VIDEO_PACKET
{
    const unsigned char* data;
    int size;
    HWS_ENCODED_VIDEO_FORMAT format;
    long long pts100ns;
    long long dts100ns;
    long long frameIndex;
    int keyFrame;
    int width;
    int height;
} HWS_ENCODED_VIDEO_PACKET;

typedef struct HWS_ENCODED_AUDIO_PACKET
{
    const unsigned char* data;
    int size;
    HWS_ENCODED_AUDIO_FORMAT format;
    long long pts100ns;
    int sampleRate;
    int channels;
    int bitsPerSample;
} HWS_ENCODED_AUDIO_PACKET;

typedef void (__stdcall *HWS_ENCODED_VIDEO_CALLBACK)(const HWS_ENCODED_VIDEO_PACKET* packet, void* userData);
typedef void (__stdcall *HWS_ENCODED_AUDIO_CALLBACK)(const HWS_ENCODED_AUDIO_PACKET* packet, void* userData);

typedef struct HWS_RECORDER_STATS
{
    long long inputFrames;
    long long encodedFrames;
    long long droppedFrames;
    long long duplicatedFrames;
    long long videoCallbackFrames;
    long long audioCallbackFrames;
    int targetFps;
    int width;
    int height;
}
HWS_RECORDER_STATS;

typedef struct HWS_AUDIO_FRAME
{
    const unsigned char* data;
    int dataSize;
    int sampleRate;
    int channels;
    int bitsPerSample;
    long long timestamp100ns;
} HWS_AUDIO_FRAME;

// Decoded file input. FileSource owns both output ports. Output ports are
// borrowed handles: connect/callback them normally, but destroy only the
// parent HWS_PORT_FILE_SOURCE.
typedef struct HWS_FILE_SOURCE_CONFIG
{
    wchar_t filePath[MAX_PATH];
    HWS_PIXEL_FORMAT outputVideoFormat; // UNKNOWN/NV12: NV12 in v6.1.x.
    int outputAudioSampleRate;          // 0: preserve source sample rate.
    int outputAudioChannels;            // 0: preserve source channel count.
    int videoQueueDepth;                // 0: SDK default (12).
    int audioQueueDepth;                // 0: SDK default (64).
} HWS_FILE_SOURCE_CONFIG;

typedef struct HWS_FILE_SOURCE_INFO
{
    long long duration100ns;
    int hasVideo;
    int hasAudio;
    int videoWidth;
    int videoHeight;
    int videoFpsNum;
    int videoFpsDen;
    HWS_PIXEL_FORMAT videoPixelFormat;
    int audioSampleRate;
    int audioChannels;
    int audioBitsPerSample;
} HWS_FILE_SOURCE_INFO;

typedef enum HWS_FILE_SOURCE_EVENT
{
    HWS_FILE_SOURCE_EVENT_OPENED = 1,
    HWS_FILE_SOURCE_EVENT_POSITION = 2,
    HWS_FILE_SOURCE_EVENT_END_OF_STREAM = 3,
    HWS_FILE_SOURCE_EVENT_ERROR = 4
} HWS_FILE_SOURCE_EVENT;

typedef struct HWS_FILE_SOURCE_EVENT_INFO
{
    HWS_FILE_SOURCE_EVENT eventType;
    HWS_RESULT result;
    long long position100ns;
    long long duration100ns;
    long long nativeError; // HRESULT when result == HWS_E_FAIL.
} HWS_FILE_SOURCE_EVENT_INFO;

typedef void (__stdcall *HWS_FILE_SOURCE_EVENT_CALLBACK)(
    HWS_PORT fileSource,
    const HWS_FILE_SOURCE_EVENT_INFO* eventInfo,
    void* userData);

typedef void (__stdcall *HWS_VIDEO_CALLBACK)(const HWS_VIDEO_FRAME* frame, void* userData);
typedef void (__stdcall *HWS_AUDIO_CALLBACK)(const HWS_AUDIO_FRAME* frame, void* userData);


typedef enum HWS_IMAGE_FORMAT
{
    HWS_IMAGE_FORMAT_BMP = 1,
    HWS_IMAGE_FORMAT_JPG = 2
} HWS_IMAGE_FORMAT;

typedef struct HWS_SNAPSHOT_CONFIG
{
    wchar_t directory[MAX_PATH];
    HWS_IMAGE_FORMAT imageFormat;
} HWS_SNAPSHOT_CONFIG;


// GPU FinalFrame recorder config. This path records the renderer FinalTexture
// after GPU 3DLUT and GPU OSD composition. v6.0 keeps optional PCM->AAC
// audio muxing through the same Media Foundation SinkWriter.
typedef struct HWS_RENDER_GPU_RECORD_CONFIG
{
    wchar_t filePath[MAX_PATH];
    int width;           // 0 = use FinalTexture width.
    int height;          // 0 = use FinalTexture height.
    int fps;             // 0 = 30.
    int videoBitrate;    // 0 = 8000000.

    int enableAudio;     // 0 = video only, 1 = add AAC audio track.
    int audioSampleRate; // 0 = 48000. The connected HWS_AUDIO_FRAME must match.
    int audioChannels;   // 0 = 2. The connected HWS_AUDIO_FRAME must match.
    int audioBitrate;    // 0 = 128000.
} HWS_RENDER_GPU_RECORD_CONFIG;

HWS_API const char* HWS_GetVersion(void);
HWS_API void HWS_GetVersionEx(HWS_VERSION* version);
HWS_API int HWS_GetApiVersion(void);

// Enables or disables SDK runtime logging. Logging is disabled by default.
// Call HWS_EnableLog(1) once when the application starts, before HWS_Init().
// Enabling recreates hwscapture.log in the application executable directory,
// so the log from the previous application run is removed automatically.
HWS_API HWS_RESULT HWS_EnableLog(int enable);

HWS_API HWS_RESULT HWS_Init();
HWS_API HWS_RESULT HWS_Shutdown();

// v6.0 pipeline policy. Default is HWS_SDK_PIPELINE_MODE_CPU.
// Set the global default before creating ports, or set a specific render port
// with HWS_SetVideoRenderPipelineMode(). Existing CPU ports continue to work.
HWS_API HWS_RESULT HWS_SetDefaultPipelineMode(HWS_SDK_PIPELINE_MODE mode);
HWS_API HWS_SDK_PIPELINE_MODE HWS_GetDefaultPipelineMode(void);
// Re-enumerates video/audio devices and clears cached video capability data.
// Call this before opening a device selection dialog when devices may be hot-plugged.
HWS_API HWS_RESULT HWS_RefreshDeviceList();
// Clears cached video/audio device enumeration data. Normally called by HWS_Shutdown.

HWS_API int HWS_GetVideoDeviceCount();
HWS_API HWS_RESULT HWS_GetVideoDeviceInfo(int index, HWS_DEVICE_INFO* info);
HWS_API int HWS_GetAudioInputDeviceCount();
HWS_API HWS_RESULT HWS_GetAudioInputDeviceInfo(int index, HWS_DEVICE_INFO* info);
// Active WASAPI playback endpoints used by AudioRender. Device index -1 means
// the current Windows default output endpoint.
HWS_API int HWS_GetAudioRenderDeviceCount();
HWS_API HWS_RESULT HWS_GetAudioRenderDeviceInfo(int index, HWS_DEVICE_INFO* info);

HWS_API int HWS_GetVideoFormatCount(int deviceIndex);
HWS_API HWS_RESULT HWS_GetVideoFormatInfo(int deviceIndex, int formatIndex, HWS_VIDEO_FORMAT* fmt);
HWS_API int HWS_GetVideoCapabilityCount(int deviceIndex);
HWS_API HWS_RESULT HWS_GetVideoCapability(int deviceIndex, int capIndex, HWS_VIDEO_CAP* cap);

HWS_API HWS_PORT HWS_CreateVideoInput();
HWS_API HWS_PORT HWS_CreateAudioInput();
// Creates a generic external frame source port. Use HWS_PushVideoFrame() to
// inject frames produced by an algorithm/DLL module into the normal SDK graph.
HWS_API HWS_PORT HWS_CreateVideoFrameSource();
HWS_API HWS_PORT HWS_CreateFileSource(const HWS_FILE_SOURCE_CONFIG* config);
HWS_API HWS_PORT HWS_FileSourceGetVideoOutput(HWS_PORT fileSource);
HWS_API HWS_PORT HWS_FileSourceGetAudioOutput(HWS_PORT fileSource);
HWS_API HWS_RESULT HWS_FileSourceGetInfo(HWS_PORT fileSource, HWS_FILE_SOURCE_INFO* info);
HWS_API HWS_RESULT HWS_FileSourceSetEventCallback(HWS_PORT fileSource, HWS_FILE_SOURCE_EVENT_CALLBACK callback, void* userData);
HWS_API HWS_RESULT HWS_FileSourcePause(HWS_PORT fileSource, int pause);
HWS_API HWS_RESULT HWS_FileSourceSeek(HWS_PORT fileSource, long long position100ns);
HWS_API HWS_RESULT HWS_FileSourceGetPosition(HWS_PORT fileSource, long long* position100ns);
// If hwnd is NULL, the render port works in callback-only mode and does not create D3D resources.
HWS_API HWS_PORT HWS_CreateVideoRender(HWND hwnd);
HWS_API HWS_PORT HWS_CreateAudioRender();
HWS_API HWS_PORT HWS_CreateVideoMux();
HWS_API HWS_PORT HWS_CreateVideoOsd();
HWS_API HWS_PORT HWS_CreateVideoCrop();
HWS_API HWS_PORT HWS_CreateVideoColorEngine();
HWS_API HWS_PORT HWS_CreateAudioMux();
HWS_API HWS_PORT HWS_CreateRecorder(const char* filePath);
HWS_API HWS_RESULT HWS_SetRecorderConfig(HWS_PORT port, const HWS_RECORDER_CONFIG* config);
HWS_API HWS_RESULT HWS_GetRecorderStats(HWS_PORT port, HWS_RECORDER_STATS* stats);
HWS_API HWS_RESULT HWS_SetRecorderVideoCallback(HWS_PORT port, HWS_ENCODED_VIDEO_CALLBACK cb, void* userData);
HWS_API HWS_RESULT HWS_SetRecorderAudioCallback(HWS_PORT port, HWS_ENCODED_AUDIO_CALLBACK cb, void* userData);

HWS_API HWS_RESULT HWS_SetVideoInputDevice(HWS_PORT port, int deviceIndex);
HWS_API HWS_RESULT HWS_SetVideoInputFormat(HWS_PORT port, int width, int height, int fps, HWS_PIXEL_FORMAT pixelFormat);
HWS_API HWS_RESULT HWS_SetVideoInputConfig(HWS_PORT port, const HWS_VIDEO_INPUT_CONFIG* config);
HWS_API HWS_RESULT HWS_SetVideoRenderConfig(HWS_PORT port, const HWS_VIDEO_RENDER_CONFIG* config);
HWS_API HWS_RESULT HWS_SetVideoRenderInterpolationMode(HWS_PORT port, HWS_RENDER_INTERPOLATION_MODE mode);
HWS_API HWS_RESULT HWS_SetVideoRenderPipelineMode(HWS_PORT port, HWS_SDK_PIPELINE_MODE mode);
HWS_API HWS_SDK_PIPELINE_MODE HWS_GetVideoRenderPipelineMode(HWS_PORT port);
HWS_API HWS_RESULT HWS_SetVideoCropRect(HWS_PORT port, const HWS_VIDEO_CROP_RECT* rect);
HWS_API HWS_RESULT HWS_EnableVideoCrop(HWS_PORT port, int enable);
HWS_API HWS_RESULT HWS_SetVideoColorEngineConfig(HWS_PORT port, const HWS_COLOR_ENGINE_CONFIG* config);
HWS_API HWS_RESULT HWS_GetVideoColorEngineConfig(HWS_PORT port, HWS_COLOR_ENGINE_CONFIG* config);
HWS_API HWS_RESULT HWS_LoadVideoColorEngineCubeFile(HWS_PORT port, const wchar_t* cubePath);
HWS_API HWS_RESULT HWS_ClearVideoColorEngineLut(HWS_PORT port);

// D3D11 preview LUT processor. These APIs affect the renderer GPU path.
// In v6.0 they produce preview and a source-size GPU FinalTexture for snapshot.
HWS_API HWS_RESULT HWS_SetVideoRenderLutConfig(HWS_PORT port, const HWS_COLOR_ENGINE_CONFIG* config);
HWS_API HWS_RESULT HWS_LoadVideoRenderCubeFile(HWS_PORT port, const wchar_t* cubePath);
HWS_API HWS_RESULT HWS_ClearVideoRenderLut(HWS_PORT port);

HWS_API HWS_RESULT HWS_SaveVideoRenderSnapshot(HWS_PORT port, const wchar_t* filePath, HWS_IMAGE_FORMAT imageFormat);

// GPU OSD APIs. These are rendered after GPU LUT in the renderer FinalFrame path.
HWS_API HWS_RESULT HWS_AddVideoRenderGpuOsdText(HWS_PORT port, const HWS_OSD_ITEM* item);
HWS_API HWS_RESULT HWS_AddVideoRenderGpuOsdImage(HWS_PORT port, const HWS_OSD_ITEM* item);
HWS_API HWS_RESULT HWS_AddVideoRenderGpuOsdRect(HWS_PORT port, const HWS_OSD_ITEM* item);
HWS_API HWS_RESULT HWS_UpdateVideoRenderGpuOsdText(HWS_PORT port, int itemId, const wchar_t* textW);
HWS_API HWS_RESULT HWS_RemoveVideoRenderGpuOsdItem(HWS_PORT port, int itemId);
HWS_API HWS_RESULT HWS_ClearVideoRenderGpuOsd(HWS_PORT port);

// GPU FinalFrame recorder APIs. Records FinalTexture after LUT + GPU OSD.
HWS_API HWS_RESULT HWS_StartVideoRenderGpuRecord(HWS_PORT port, const HWS_RENDER_GPU_RECORD_CONFIG* config);
HWS_API HWS_RESULT HWS_StopVideoRenderGpuRecord(HWS_PORT port);
HWS_API int HWS_IsVideoRenderGpuRecording(HWS_PORT port);
HWS_API HWS_RESULT HWS_SetVideoOsdText(HWS_PORT port, const HWS_OSD_ITEM* item);
HWS_API HWS_RESULT HWS_SetVideoOsdImage(HWS_PORT port, const HWS_OSD_ITEM* item);
// Adds one OSD item. Existing OSD items are kept.
HWS_API HWS_RESULT HWS_AddVideoOsdText(HWS_PORT port, const HWS_OSD_ITEM* item);
HWS_API HWS_RESULT HWS_AddVideoOsdImage(HWS_PORT port, const HWS_OSD_ITEM* item);
HWS_API HWS_RESULT HWS_AddVideoOsdRect(HWS_PORT port, const HWS_OSD_ITEM* item);
// Removes all OSD items from the OSD port.
// Updates the text string of an existing text OSD item without changing position, font, alpha, or z-order.
HWS_API HWS_RESULT HWS_UpdateVideoOsdText(HWS_PORT port, int itemId, const wchar_t* textW);
HWS_API HWS_RESULT HWS_RemoveVideoOsdItem(HWS_PORT port, int itemId);
HWS_API HWS_RESULT HWS_ClearVideoOsd(HWS_PORT port);
HWS_API HWS_RESULT HWS_ShowVideoDevicePropertyPage(HWND parent, int deviceIndex);
HWS_API HWS_RESULT HWS_ShowVideoCrossbarPropertyPage(HWND parent, int deviceIndex);
HWS_API HWS_RESULT HWS_SetAudioInputDevice(HWS_PORT port, int deviceIndex);
HWS_API HWS_RESULT HWS_SetAudioInputPCM(HWS_PORT port, int sampleRate, int channels, int bitsPerSample);
HWS_API HWS_RESULT HWS_SetAudioRenderDefaultDevice(HWS_PORT port);
HWS_API HWS_RESULT HWS_SetAudioRenderDevice(HWS_PORT port, int deviceIndex);
HWS_API HWS_RESULT HWS_SetAudioRenderVolume(HWS_PORT port, float volume);
HWS_API HWS_RESULT HWS_SetAudioRenderMute(HWS_PORT port, int mute);

HWS_API HWS_RESULT HWS_Connect(HWS_PORT src, HWS_PORT dst);
HWS_API HWS_RESULT HWS_ConnectVideoOsdOutput(HWS_PORT osd, HWS_OSD_OUTPUT output, HWS_PORT dst);
HWS_API HWS_RESULT HWS_Disconnect(HWS_PORT src, HWS_PORT dst);
HWS_API HWS_RESULT HWS_Start(HWS_PORT port);
HWS_API HWS_RESULT HWS_Stop(HWS_PORT port);
HWS_API HWS_RESULT HWS_DestroyPort(HWS_PORT port);

HWS_API HWS_RESULT HWS_SetVideoFrameCallback(HWS_PORT port, HWS_VIDEO_CALLBACK cb, void* userData);
// Pushes one video frame into a HWS_PORT_VIDEO_FRAME_SOURCE. The SDK dispatches
// it synchronously to connected downstream ports. The caller may release/reuse
// the source buffer after this call returns.
HWS_API HWS_RESULT HWS_PushVideoFrame(HWS_PORT sourcePort, const HWS_VIDEO_FRAME* frame);

HWS_API HWS_RESULT HWS_SetAudioFrameCallback(HWS_PORT port, HWS_AUDIO_CALLBACK cb, void* userData);

#endif
