cmake_minimum_required(VERSION 3.22.1)
project(androidcast_codecs C)

set(CMAKE_C_STANDARD 11)

set(THIRD_PARTY_ROOT ${CMAKE_SOURCE_DIR}/../third-party)
set(NATIVE_LIBS_ROOT ${CMAKE_SOURCE_DIR}/../build/native)
set(NDK_BRIDGE ${CMAKE_SOURCE_DIR})

add_library(androidcast_codecs SHARED
    ${NDK_BRIDGE}/jni/androidcast_codecs.c
    ${NDK_BRIDGE}/jni/libvpx_bridge.c
    ${NDK_BRIDGE}/jni/opus_bridge.c
    ${NDK_BRIDGE}/jni/speex_bridge.c
    ${NDK_BRIDGE}/jni/crash_hook.c
)

target_include_directories(androidcast_codecs PRIVATE
    ${NDK_BRIDGE}/jni
)

# Per-ABI libvpx.a from scripts/build-native-codecs.sh (do not link arm64 into armeabi-v7a).
set(LIBVPX_LIB "")
if(ANDROID_ABI)
    set(LIBVPX_PER_ABI "${NATIVE_LIBS_ROOT}/${ANDROID_ABI}/libvpx.a")
    if(EXISTS "${LIBVPX_PER_ABI}")
        set(LIBVPX_LIB "${LIBVPX_PER_ABI}")
    endif()
endif()
if(NOT LIBVPX_LIB AND DEFINED ENV{ANDROIDCAST_LIBVPX_LIB})
    string(FIND "$ENV{ANDROIDCAST_LIBVPX_LIB}" "${ANDROID_ABI}" _abi_match)
    if(_abi_match GREATER -1 AND EXISTS "$ENV{ANDROIDCAST_LIBVPX_LIB}")
        set(LIBVPX_LIB "$ENV{ANDROIDCAST_LIBVPX_LIB}")
    endif()
endif()

if(LIBVPX_LIB)
    message(STATUS "Linking libvpx for ${ANDROID_ABI}: ${LIBVPX_LIB}")
    target_compile_definitions(androidcast_codecs PRIVATE ANDROIDCAST_HAVE_LIBVPX=1)
    target_link_libraries(androidcast_codecs "${LIBVPX_LIB}")
    if(EXISTS "${THIRD_PARTY_ROOT}/libvpx")
        target_include_directories(androidcast_codecs PRIVATE "${THIRD_PARTY_ROOT}/libvpx")
        target_include_directories(androidcast_codecs PRIVATE "${THIRD_PARTY_ROOT}/libvpx/vpx")
    endif()
else()
    message(STATUS "No libvpx.a for ${ANDROID_ABI} — VPx JNI probe off (build with scripts/build-native-codecs.sh ${ANDROID_ABI})")
endif()

# libopus.a — same autodetect pattern (scripts/build-native-codecs.sh).
set(LIBOPUS_LIB "")
if(ANDROID_ABI)
    set(LIBOPUS_PER_ABI "${NATIVE_LIBS_ROOT}/${ANDROID_ABI}/libopus.a")
    if(EXISTS "${LIBOPUS_PER_ABI}")
        set(LIBOPUS_LIB "${LIBOPUS_PER_ABI}")
    endif()
endif()
if(NOT LIBOPUS_LIB AND DEFINED ENV{ANDROIDCAST_LIBOPUS_LIB})
    string(FIND "$ENV{ANDROIDCAST_LIBOPUS_LIB}" "${ANDROID_ABI}" _opus_abi_match)
    if(_opus_abi_match GREATER -1 AND EXISTS "$ENV{ANDROIDCAST_LIBOPUS_LIB}")
        set(LIBOPUS_LIB "$ENV{ANDROIDCAST_LIBOPUS_LIB}")
    endif()
endif()
if(LIBOPUS_LIB)
    message(STATUS "Linking libopus for ${ANDROID_ABI}: ${LIBOPUS_LIB}")
    target_compile_definitions(androidcast_codecs PRIVATE ANDROIDCAST_HAVE_OPUS=1)
    target_link_libraries(androidcast_codecs "${LIBOPUS_LIB}")
    if(EXISTS "${THIRD_PARTY_ROOT}/opus/include")
        target_include_directories(androidcast_codecs PRIVATE "${THIRD_PARTY_ROOT}/opus/include")
    endif()
else()
    message(STATUS "No libopus.a for ${ANDROID_ABI} — Opus JNI probe off (build with scripts/build-native-codecs.sh ${ANDROID_ABI})")
endif()

# libspeex.a
set(LIBSPEEX_LIB "")
if(ANDROID_ABI)
    set(LIBSPEEX_PER_ABI "${NATIVE_LIBS_ROOT}/${ANDROID_ABI}/libspeex.a")
    if(EXISTS "${LIBSPEEX_PER_ABI}")
        set(LIBSPEEX_LIB "${LIBSPEEX_PER_ABI}")
    endif()
endif()
if(NOT LIBSPEEX_LIB AND DEFINED ENV{ANDROIDCAST_LIBSPEEX_LIB})
    string(FIND "$ENV{ANDROIDCAST_LIBSPEEX_LIB}" "${ANDROID_ABI}" _speex_abi_match)
    if(_speex_abi_match GREATER -1 AND EXISTS "$ENV{ANDROIDCAST_LIBSPEEX_LIB}")
        set(LIBSPEEX_LIB "$ENV{ANDROIDCAST_LIBSPEEX_LIB}")
    endif()
endif()
if(LIBSPEEX_LIB)
    message(STATUS "Linking libspeex for ${ANDROID_ABI}: ${LIBSPEEX_LIB}")
    target_compile_definitions(androidcast_codecs PRIVATE ANDROIDCAST_HAVE_SPEEX=1)
    target_link_libraries(androidcast_codecs "${LIBSPEEX_LIB}")
    if(EXISTS "${THIRD_PARTY_ROOT}/speex/include")
        target_include_directories(androidcast_codecs PRIVATE "${THIRD_PARTY_ROOT}/speex/include")
    endif()
else()
    message(STATUS "No libspeex.a for ${ANDROID_ABI} — Speex JNI probe off (build with scripts/build-native-codecs.sh ${ANDROID_ABI})")
endif()

find_library(log-lib log)
find_library(android-lib android)
find_library(dl-lib dl)
target_link_libraries(androidcast_codecs ${log-lib} ${android-lib} ${dl-lib})
