cmake_minimum_required(VERSION 3.16)
file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/../linux-host/VERSION" SUN_STREAM_VERSION LIMIT_COUNT 1)
project(sun_stream_player VERSION "${SUN_STREAM_VERSION}" LANGUAGES C CXX)
if(NOT TARGET sun-update-helper)
    add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../shared/updates" "${CMAKE_CURRENT_BINARY_DIR}/sun-update-helper")
endif()

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(PkgConfig REQUIRED)
pkg_check_modules(JSON_GLIB REQUIRED IMPORTED_TARGET json-glib-1.0>=1.6 gio-2.0>=2.66)

# C0 is also usable by the future GTK shell; the diagnostic CLI owns no UI/TLS.
add_library(sun-player-catalog STATIC src/catalog.cpp src/discovery.cpp)
target_include_directories(sun-player-catalog PUBLIC src)
target_link_libraries(sun-player-catalog PUBLIC PkgConfig::JSON_GLIB)
target_compile_options(sun-player-catalog PRIVATE -Wall -Wextra -Wpedantic)
add_executable(sun-stream-player-probe src/main.cpp)
target_compile_definitions(sun-stream-player-probe PRIVATE SUN_STREAM_VERSION="${PROJECT_VERSION}")
target_link_libraries(sun-stream-player-probe PRIVATE sun-player-catalog)

find_package(OpenSSL REQUIRED)
find_package(Threads REQUIRED)
find_package(CURL REQUIRED)
pkg_check_modules(PLAYER_NATIVE REQUIRED IMPORTED_TARGET gtk+-3.0 fontconfig pangoft2
    gstreamer-1.0>=1.20 gstreamer-app-1.0>=1.20 gstreamer-video-1.0>=1.20
    gstreamer-gl-1.0>=1.20 gstreamer-gl-egl-1.0 gstreamer-gl-x11-1.0 epoxy)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
if(NOT TARGET sun_pairing)
    add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../shared/pairing" "${CMAKE_CURRENT_BINARY_DIR}/sun-pairing")
endif()
add_library(sun-player-core STATIC ../linux-host/src/desktop_host.cpp src/wire.cpp src/transport.cpp src/media.cpp src/video_presenter.cpp src/client.cpp
    src/extensions.cpp src/extension_session.cpp )
include("${CMAKE_CURRENT_LIST_DIR}/../shared/video/json.cmake")
target_link_libraries(sun-player-core PUBLIC sun-codec-json)
target_include_directories(sun-player-core PUBLIC src "${CMAKE_CURRENT_BINARY_DIR}/generated" "${CMAKE_CURRENT_LIST_DIR}/..")
target_link_libraries(sun-player-core PUBLIC sun-player-catalog PkgConfig::PLAYER_NATIVE
    OpenSSL::SSL OpenSSL::Crypto Threads::Threads sun_pairing ${CMAKE_DL_LIBS})
target_compile_options(sun-player-core PRIVATE -Wall -Wextra -Wpedantic)
add_library(sun-player-workspace STATIC src/workspace.cpp ../third_party/qrcodegen/qrcodegen.c)
add_dependencies(sun-player-workspace sun-update-helper)
target_link_libraries(sun-player-workspace PUBLIC sun-player-core)
target_link_libraries(sun-player-workspace PRIVATE CURL::libcurl)
target_compile_definitions(sun-player-workspace PRIVATE
    SUN_STREAM_VERSION="${PROJECT_VERSION}")

include(CTest)
if(BUILD_TESTING)
    add_executable(sun-linux-rtc-tls-tests tests/rtc_tls_tests.cpp src/transport.cpp)
    target_compile_features(sun-linux-rtc-tls-tests PRIVATE cxx_std_20)
    target_link_libraries(sun-linux-rtc-tls-tests PRIVATE PkgConfig::JSON_GLIB OpenSSL::SSL OpenSSL::Crypto Threads::Threads)
    add_test(NAME sun-linux-rtc-tls COMMAND sun-linux-rtc-tls-tests)
    set_tests_properties(sun-linux-rtc-tls PROPERTIES TIMEOUT 30)
    find_package(Python3 COMPONENTS Interpreter REQUIRED)
    add_library(sun-linux-rtc-mock SHARED tests/rtc_mock.cpp)
    target_compile_features(sun-linux-rtc-mock PRIVATE cxx_std_20)
    target_link_libraries(sun-linux-rtc-mock PRIVATE sun-codec-json)
    set_target_properties(sun-linux-rtc-mock PROPERTIES OUTPUT_NAME sun-rtc-test-only)
    add_executable(sun-linux-rtc-endpoint-tests tests/rtc_endpoint_tests.cpp src/transport.cpp)
    target_compile_features(sun-linux-rtc-endpoint-tests PRIVATE cxx_std_20)
    target_link_libraries(sun-linux-rtc-endpoint-tests PRIVATE sun-codec-json PkgConfig::JSON_GLIB Threads::Threads OpenSSL::SSL OpenSSL::Crypto ${CMAKE_DL_LIBS})
    add_dependencies(sun-linux-rtc-endpoint-tests sun-linux-rtc-mock)
    add_test(NAME sun-linux-rtc-endpoint COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_LIST_DIR}/tests/rtc_fixture.py" $<TARGET_FILE:sun-linux-rtc-endpoint-tests> $<TARGET_FILE:sun-linux-rtc-mock>)
    set_tests_properties(sun-linux-rtc-endpoint PROPERTIES TIMEOUT 40)
    add_library(sun-linux-signaling-mock SHARED tests/rtc_signaling_mock.cpp)
    target_compile_features(sun-linux-signaling-mock PRIVATE cxx_std_20)
    set_target_properties(sun-linux-signaling-mock PROPERTIES OUTPUT_NAME sun-signaling-test-only)
    add_executable(sun-linux-rtc-internet-tests tests/rtc_internet_tests.cpp)
    target_compile_features(sun-linux-rtc-internet-tests PRIVATE cxx_std_20)
    target_link_libraries(sun-linux-rtc-internet-tests PRIVATE sun-codec-json Threads::Threads OpenSSL::SSL OpenSSL::Crypto ${CMAKE_DL_LIBS})
    add_dependencies(sun-linux-rtc-internet-tests sun-linux-rtc-mock sun-linux-signaling-mock)
    add_test(NAME sun-linux-rtc-internet COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_LIST_DIR}/tests/rtc_fixture.py"
        $<TARGET_FILE:sun-linux-rtc-internet-tests> $<TARGET_FILE:sun-linux-rtc-mock> $<TARGET_FILE:sun-linux-signaling-mock>)
    set_tests_properties(sun-linux-rtc-internet PROPERTIES TIMEOUT 40)
    add_executable(sun-player-icon-tests tests/icon_tests.cpp)
    target_link_libraries(sun-player-icon-tests PRIVATE PkgConfig::PLAYER_NATIVE)
    target_link_options(sun-player-icon-tests PRIVATE -Wl,--wrap=gdk_pixbuf_new_from_file_at_scale)
    target_compile_options(sun-player-icon-tests PRIVATE -UNDEBUG)
    add_test(NAME sun-player-icons COMMAND sun-player-icon-tests "${CMAKE_CURRENT_LIST_DIR}/../shared/desktop/ui-assets")
    set_tests_properties(sun-player-icons PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77)
    add_executable(sun-linux-local-views-tests tests/local_views_tests.cpp)
    target_link_libraries(sun-linux-local-views-tests PRIVATE sun-player-workspace)
    target_compile_options(sun-linux-local-views-tests PRIVATE -UNDEBUG)
    add_test(NAME sun-linux-current-local-views COMMAND sun-linux-local-views-tests)
    set_tests_properties(sun-linux-current-local-views PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77)
    add_executable(sun-linux-media-settings-tests tests/media_settings_tests.cpp)
    target_link_libraries(sun-linux-media-settings-tests PRIVATE sun-player-workspace)
    target_compile_options(sun-linux-media-settings-tests PRIVATE -UNDEBUG)
    add_test(NAME sun-linux-media-settings COMMAND sun-linux-media-settings-tests)
    set_tests_properties(sun-linux-media-settings PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77)
    add_executable(sun-linux-media-policy-tests tests/media_policy_tests.cpp)
    target_link_libraries(sun-linux-media-policy-tests PRIVATE sun-codec-json Threads::Threads OpenSSL::Crypto)
    target_compile_options(sun-linux-media-policy-tests PRIVATE -UNDEBUG)
    add_test(NAME sun-linux-media-policy COMMAND sun-linux-media-policy-tests)
    add_executable(sun-player-components-tests tests/components_tests.cpp)
    target_link_libraries(sun-player-components-tests PRIVATE sun-player-workspace)
    target_link_options(sun-player-components-tests PRIVATE -Wl,--wrap=gdk_pixbuf_new_from_file_at_scale)
    target_compile_options(sun-player-components-tests PRIVATE -UNDEBUG)
    add_test(NAME sun-player-components COMMAND sun-player-components-tests "${CMAKE_CURRENT_LIST_DIR}/../linux-host/src/main.cpp")
    set_tests_properties(sun-player-components PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77)
    add_executable(sun-player-video-tests EXCLUDE_FROM_ALL tests/video_presenter_tests.cpp)
    target_link_libraries(sun-player-video-tests PRIVATE sun-player-core)
    find_package(Threads REQUIRED)
    add_executable(sun-player-catalog-tests tests/catalog_tests.cpp)
    target_link_libraries(sun-player-catalog-tests PRIVATE sun-player-catalog Threads::Threads)
    add_test(NAME sun-player-catalog COMMAND sun-player-catalog-tests)
    set_tests_properties(sun-player-catalog PROPERTIES TIMEOUT 15)
    add_executable(sun-player-wire-tests tests/wire_tests.cpp)
    target_link_libraries(sun-player-wire-tests PRIVATE sun-player-core)
    target_compile_definitions(sun-player-wire-tests PRIVATE SUN_PLAYER_REPO_DIR="${CMAKE_CURRENT_LIST_DIR}/..")
    add_test(NAME sun-player-desktop-wire COMMAND sun-player-wire-tests)
    add_executable(sun-player-client-tests tests/client_tests.cpp)
    target_link_libraries(sun-player-client-tests PRIVATE sun-player-core)
    target_compile_definitions(sun-player-client-tests PRIVATE SUN_PLAYER_REPO_DIR="${CMAKE_CURRENT_LIST_DIR}/..")
    add_test(NAME sun-player-desktop-session COMMAND sun-player-client-tests)
    set_tests_properties(sun-player-desktop-session PROPERTIES TIMEOUT 25)
    foreach(profile auto manual source unverified)
        add_test(NAME sun-player-media-settings-${profile} COMMAND sun-player-client-tests --settings-${profile})
        set_tests_properties(sun-player-media-settings-${profile} PROPERTIES TIMEOUT 25 RUN_SERIAL TRUE)
    endforeach()
    foreach(codec h264 hevc av1)
        add_test(NAME sun-player-media-codec-${codec} COMMAND sun-player-client-tests --codec-${codec})
        set_tests_properties(sun-player-media-codec-${codec} PROPERTIES TIMEOUT 25 RUN_SERIAL TRUE)
    endforeach()
    add_test(NAME sun-player-native-unencrypted COMMAND sun-player-client-tests --unencrypted)
    set_tests_properties(sun-player-native-unencrypted PROPERTIES TIMEOUT 25 RUN_SERIAL TRUE)
    add_test(NAME sun-player-stop-unknown COMMAND sun-player-client-tests --stop-unknown)
    add_test(NAME sun-player-stop-unapplied COMMAND sun-player-client-tests --stop-unapplied)
    set_tests_properties(sun-player-stop-unknown PROPERTIES TIMEOUT 25 RUN_SERIAL TRUE)
    add_test(NAME sun-player-two-sessions-revocation COMMAND sun-player-client-tests --two-sessions)
    set_tests_properties(sun-player-two-sessions-revocation PROPERTIES TIMEOUT 25 RUN_SERIAL TRUE)
    add_executable(sun-player-extension-tests tests/extensions_tests.cpp)
    target_link_libraries(sun-player-extension-tests PRIVATE sun-player-core)
    target_compile_definitions(sun-player-extension-tests PRIVATE SUN_PLAYER_REPO_DIR="${CMAKE_CURRENT_LIST_DIR}/..")
    add_test(NAME sun-player-desktop-extensions COMMAND sun-player-extension-tests)
    set_tests_properties(sun-player-desktop-extensions PROPERTIES TIMEOUT 25)
    add_executable(sun-player-media-tests tests/media_tests.cpp)
    target_link_libraries(sun-player-media-tests PRIVATE sun-player-core)
    add_test(NAME sun-player-authenticated-media COMMAND sun-player-media-tests)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/../shared/video/gstreamer_worker.cmake")
add_dependencies(sun-player-workspace sun-stream-codec-inspector)
add_dependencies(sun-player-core sun-stream-codec-inspector)
