cmake_minimum_required(VERSION 3.2)
project(aidstream)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_STANDARD 17)
message("Input V4L2: ${V4L2}")

if(V4L2 STREQUAL "2404-9075")
    set(JSONCPP_SO "/usr/lib/aarch64-linux-gnu/libjsoncpp.so")
    set(JSONCPP_SO_1 "/usr/lib/aarch64-linux-gnu/libjsoncpp.so.1")

    if(EXISTS "${JSONCPP_SO}")
        if(EXISTS "${JSONCPP_SO_1}")
            message(STATUS "jsoncpp library and symlink already exist, skipping setup")
        else()
            message(FATAL_ERROR
                "jsoncpp symlink missing. Run this command manually with sudo and reconfigure:\n"
                "  sudo ln -s ${JSONCPP_SO} ${JSONCPP_SO_1}"
            )
        endif()
    else()
        message(FATAL_ERROR
            "jsoncpp library missing. Run these commands manually with sudo and reconfigure:\n"
            "  sudo apt-get install -y libjsoncpp-dev\n"
            "  sudo ln -s ${JSONCPP_SO} ${JSONCPP_SO_1}"
        )
    endif()
endif()

file(READ "/etc/os-release" OS_RELEASE_CONTENT)

set(_RPATH_LIST
  "/opt/ubun20/usr/lib/aarch64-linux-gnu"
  "/opt/ubun20/usr/lib"
)
list(JOIN _RPATH_LIST ":" _RPATH)
function(set_aidstream_rpath tgt)
  set_target_properties(${tgt} PROPERTIES
    BUILD_RPATH   "${_RPATH}"
    INSTALL_RPATH "${_RPATH}"
  )
endfunction()

# Check for Ubuntu 20.04
if(OS_RELEASE_CONTENT MATCHES "VERSION_ID=\"20.04\"")
    message(STATUS "Configuring for ubuntu 2004 platform")
    set(OPENCV_VERSION "4.2.0")
elseif(OS_RELEASE_CONTENT MATCHES "VERSION_ID=\"22.04\"")
    message(STATUS "Configuring for ubuntu 2204 platform")
    set(OPENCV_VERSION "4.5.4")
elseif(OS_RELEASE_CONTENT MATCHES "VERSION_ID=\"24.04\"")
    message(STATUS "Configuring for ubuntu 2404 platform")
    set(OPENCV_VERSION "4.6.0")
endif()

# 设置高通gstreamer library路径
if(V4L2 STREQUAL "6490A")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,/opt/ubun20/usr/lib/aarch64-linux-gnu/ -Wl,--disable-new-dtags") 
    set(CMAKE_SKIP_RPATH FALSE)
    set(OPENCV_VERSION "4.5.4")
    set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
else()
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,/usr/lib:/usr/lib/aarch64-linux-gnu/ -Wl,--disable-new-dtags") 
endif()

find_package(OpenCV ${OPENCV_VERSION} REQUIRED)

include_directories(
    /usr/local/include/aidlux/aidstream-gst/
    /usr/include/opencv4/
    /usr/local/include/aidlux/aidconnect
    /usr/local/include/aidlux/
)

set(link_libs   
                pthread
                /usr/local/lib/libaidstream-gst.so
                /usr/local/lib/libaidclog.so
                /usr/local/lib/libaidlite.so
                #/usr/local/lib/aidlux/aidconnect/libaidconnect.so
                jsoncpp
                opencv_core
                opencv_videoio
                opencv_imgproc
                opencv_imgcodecs
                opencv_highgui
)

if(V4L2 STREQUAL "2404")
    list(APPEND link_libs
                gstreamer-1.0
                gstapp-1.0
                gstvideo-1.0
    )
endif()

message("${CMAKE_CURRENT_SOURCE_DIR}")

add_executable(demo  example.cpp)
target_link_libraries(demo ${link_libs})

#正常推拉流
add_executable(start  start_stream.cpp)
target_link_libraries(start ${link_libs})

#正常推拉流并同步到共享内存区（方便android端使用）
#add_executable(start_sync  start_stream_sync.cpp)
#target_link_libraries(start_sync ${link_libs})

#多路合一场景（包括推理）
add_executable(start_multi  start_multi.cpp)           
target_link_libraries(start_multi ${link_libs})

# RTSP保存为视频文件
add_executable(rtsp_to_mp4  rtsp_to_mp4.cpp)
target_link_libraries(rtsp_to_mp4 ${link_libs})

# 图片保存为视频
add_executable(save  save_mp4.cpp)
target_link_libraries(save ${link_libs})

# 进1出2流
add_executable(start_split  start_stream_split.cpp)
target_link_libraries(start_split ${link_libs})

# 多线程推拉流
add_executable(rtsp  rtsp_dev.cpp)
target_link_libraries(rtsp ${link_libs})

# rolling save
add_executable(start_rolling  start_rolling.cpp)
target_link_libraries(start_rolling ${link_libs})

# 外接模型推理 - 区分8550还是6490
message("Input V4L2: ${V4L2}")
if(V4L2 STREQUAL "ON")
    add_executable(qnn_rtsp  qnn_rtsp_v4l2.cpp)
elseif(V4L2 STREQUAL "2404")
    add_executable(qnn_rtsp qnn_rtsp_ub2404.cpp)
elseif(V4L2 STREQUAL "2404-9075")
    add_executable(qnn_rtsp qnn_rtsp_ub2404.cpp)
elseif(V4L2 STREQUAL "6490A")
    add_executable(qnn_rtsp  qnn_rtsp_6490.cpp)
else()
    add_executable(qnn_rtsp  qnn_rtsp.cpp)
endif()

target_link_libraries(qnn_rtsp ${link_libs})

if(V4L2 STREQUAL "6490A")
    set_aidstream_rpath(start)
    set_aidstream_rpath(qnn_rtsp)
    set_aidstream_rpath(demo)
    set_aidstream_rpath(save)
    set_aidstream_rpath(rtsp_to_mp4)
endif()
