cmake_minimum_required(VERSION 3.10)
project(rtsp_multi_view C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED OFF)

find_package(PkgConfig REQUIRED)

pkg_check_modules(GST REQUIRED gstreamer-1.0 gstreamer-app-1.0
                           gstreamer-video-1.0 gstreamer-allocators-1.0)
pkg_check_modules(RGA REQUIRED librga)
pkg_check_modules(DRM REQUIRED libdrm)
pkg_check_modules(PNG REQUIRED libpng)
pkg_check_modules(JPEG REQUIRED libjpeg)
pkg_check_modules(FT REQUIRED freetype2)
pkg_check_modules(CURL REQUIRED libcurl)

include_directories(${CMAKE_SOURCE_DIR}
                    ${GST_INCLUDE_DIRS}
                    ${RGA_INCLUDE_DIRS}
                    ${DRM_INCLUDE_DIRS}
                    ${PNG_INCLUDE_DIRS}
                    ${JPEG_INCLUDE_DIRS}
                    ${FT_INCLUDE_DIRS}
                    ${CURL_INCLUDE_DIRS}
                    /usr/include/rockchip)

link_directories(${GST_LIBRARY_DIRS}
                 ${RGA_LIBRARY_DIRS}
                 ${DRM_LIBRARY_DIRS}
                 ${PNG_LIBRARY_DIRS}
                 ${JPEG_LIBRARY_DIRS}
                 ${FT_LIBRARY_DIRS}
                 ${CURL_LIBRARY_DIRS})

function(common_cflags tgt)
    target_compile_options(${tgt} PRIVATE -Wall -Wextra -O2)
endfunction()

add_executable(rtsp_multi_view
    main.c
    decoder.c
    composer.c
    display.c
    plc.c
    title.c
    flog.c
    hikvision.c
    dma_util.c
    web_server.c
)

target_link_libraries(rtsp_multi_view
    ${GST_LIBRARIES}
    ${RGA_LIBRARIES}
    ${DRM_LIBRARIES}
    ${PNG_LIBRARIES}
    ${JPEG_LIBRARIES}
    ${FT_LIBRARIES}
    ${CURL_LIBRARIES}
    rockchip_mpp
    pthread
    dl
    m
)

common_cflags(rtsp_multi_view)

# --- Unit tests / benchmarks ---
add_executable(test_dma_heap test_dma_heap.c dma_util.c)
target_link_libraries(test_dma_heap ${DRM_LIBRARIES})
common_cflags(test_dma_heap)

add_executable(test_drm_display test_drm_display.c)
target_link_libraries(test_drm_display ${DRM_LIBRARIES})
common_cflags(test_drm_display)

add_executable(test_rga test_rga.c)
target_link_libraries(test_rga ${RGA_LIBRARIES} ${DRM_LIBRARIES})
common_cflags(test_rga)

add_executable(test_rga_color test_rga_color.c)
target_link_libraries(test_rga_color ${RGA_LIBRARIES} ${DRM_LIBRARIES})
common_cflags(test_rga_color)

# RKNN detection benchmark (not part of the main binary; needs rknnrt)
add_executable(test_detector_bench test_detector_bench.c detector.c dma_util.c)
target_link_libraries(test_detector_bench ${RGA_LIBRARIES} ${DRM_LIBRARIES}
                      rknnrt rockchip_mpp pthread dl m)
common_cflags(test_detector_bench)
target_include_directories(test_detector_bench PRIVATE ${CMAKE_SOURCE_DIR})

add_executable(test_plc test_plc.c plc.c flog.c)
target_link_libraries(test_plc pthread)
common_cflags(test_plc)

add_executable(plc_sim plc_sim.c)
target_link_libraries(plc_sim pthread)
common_cflags(plc_sim)
