# ANSCV — Computer Vision / Media DLL (video capture, RTSP, FFmpeg)
file(GLOB ANSCV_HEADERS "*.h")
file(GLOB ANSCV_SOURCES "*.cpp")
# Exclude old VideoPlayer.cpp — superseded by MediaClient/media/video_player.cpp
list(REMOVE_ITEM ANSCV_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer.cpp")

# MediaClient sources compiled into ANSCV (from original vcxproj)
set(MEDIACLIENT_SOURCES
    ${CMAKE_SOURCE_DIR}/MediaClient/media/audio_capture.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/audio_capture_win.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/audio_decoder.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/audio_encoder.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/audio_play.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/audio_play_win.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/avcodec_mutex.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/avi_write.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/file_player.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/http_flv_player.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/http_mjpeg_player.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/media_codec.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/media_parse.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/media_util.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/rtmp_player.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/rtsp_player.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/srt_player.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/video_decoder.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/media/video_player.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp/h264_rtp_rx.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp/h264_util.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp/h265_rtp_rx.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp/h265_util.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp/mjpeg_rtp_rx.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp/mjpeg_tables.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp/ts_parser.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtsp/rtsp_backchannel.cpp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtsp/rtsp_cln.cpp
)

add_library(ANSCV SHARED ${ANSCV_HEADERS} ${ANSCV_SOURCES} ${MEDIACLIENT_SOURCES})

# MediaClient sources skip precompiled headers (they don't include ANSCV's pch.h)
set_source_files_properties(${MEDIACLIENT_SOURCES} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)

target_include_directories(ANSCV PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# NI Vision (IMAQ) support
target_include_directories(ANSCV PRIVATE
    "C:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/include"
)
target_link_directories(ANSCV PRIVATE
    "C:/Program Files (x86)/National Instruments/Shared/ExternalCompilerSupport/C/lib64/msvc"
)
target_link_libraries(ANSCV PRIVATE nivision)

# MediaClient includes (referenced from original ANLS)
target_include_directories(ANSCV PRIVATE
    ${CMAKE_SOURCE_DIR}/MediaClient
    ${CMAKE_SOURCE_DIR}/MediaClient/media
    ${CMAKE_SOURCE_DIR}/MediaClient/rtsp
    ${CMAKE_SOURCE_DIR}/MediaClient/rtp
    ${CMAKE_SOURCE_DIR}/MediaClient/http
    ${CMAKE_SOURCE_DIR}/MediaClient/directx
    ${CMAKE_SOURCE_DIR}/MediaClient/directx/include
    ${CMAKE_SOURCE_DIR}/MediaClient/ffmpeg/include
    ${CMAKE_SOURCE_DIR}/MediaClient/openssl/include
    ${CMAKE_SOURCE_DIR}/MediaClient/libsrt/include
    ${CMAKE_SOURCE_DIR}/MediaClient/bm
    ${CMAKE_SOURCE_DIR}/MediaClient/rtmp
    ${CMAKE_SOURCE_DIR}/MediaClient/librtmp
    ${CMAKE_SOURCE_DIR}/MediaClient/srt
    ${ANSLIBS_DIR}/nlohmann
    ${SHARED_INCLUDE_DIR}
)

target_link_libraries(ANSCV
    PUBLIC  ANSLicensingSystem   # PUBLIC: SPDLogger/FileExist symbols must be re-exported
    PRIVATE labview
    PRIVATE spdlog_dep
    PUBLIC  ANSLibsLoader        # PUBLIC: loader symbols used in public headers
    PRIVATE opencv
    PRIVATE ffmpeg
    PRIVATE turbojpeg
    PRIVATE zxing
    PRIVATE zlib_dep
    PRIVATE CUDA::cudart_static
    PRIVATE CUDA::cublasLt
    PRIVATE CUDA::nvjpeg
)

# libyuv — vendored at 3rdparty/libyuv (added by top-level CMakeLists when
# the submodule is present). Provides SIMD-accelerated I420→RGB24 used by
# CVideoPlayer::avframeYUV420PToCvMat for the SW-decode fast path.
if(ANSCORE_HAS_LIBYUV)
    target_link_libraries(ANSCV PRIVATE yuv)
    target_include_directories(ANSCV PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/libyuv/include)
    target_compile_definitions(ANSCV PRIVATE ANSCORE_HAS_LIBYUV=1)
endif()

# Platform-specific libs
if(WIN32)
    target_link_directories(ANSCV PRIVATE
        ${CMAKE_SOURCE_DIR}/MediaClient/directx/lib/x64
        ${CMAKE_SOURCE_DIR}/MediaClient/openssl/lib/x64
        ${CMAKE_SOURCE_DIR}/MediaClient/libsrt/lib/x64
    )
    # Prebuilt MediaClient protocol libraries (built from MediaClient VS project)
    target_link_directories(ANSCV PRIVATE "${ANSLIBS_DIR}/MediaClient/x64/Release")
    target_link_libraries(ANSCV PRIVATE
        RtspClientLibrary HttpFlvClientLibrary HttpMjpegClientLibrary
        RtmpClientLibrary SrtClientLibrary
        d3d9 d3dx9 dxva2
        libcrypto libssl srt
        ${WIN_COMMON_LIBS}
    )
else()
    target_link_libraries(ANSCV PRIVATE ${UNIX_COMMON_LIBS})
endif()

target_compile_definitions(ANSCV PRIVATE
    ANSCV_EXPORTS
    _USRDLL
    HT_STATIC
    HTTPS
    BACKCHANNEL
    METADATA
    REPLAY
    OVER_HTTP
    OVER_WEBSOCKET
    NOMINMAX
)

if(WIN32)
    target_compile_definitions(ANSCV PRIVATE UNICODE _UNICODE __WINDOWS_OS__)
else()
    target_compile_definitions(ANSCV PRIVATE __LINUX_OS__)
endif()

target_precompile_headers(ANSCV PRIVATE pch.h)
