From 4eb8132b11aaf62d60b64b5c8af9d324a457e26a Mon Sep 17 00:00:00 2001 From: Khanh H Date: Sat, 14 Dec 2024 00:24:33 -0800 Subject: [PATCH] Make avdevice an optional component --- CMakeLists.txt | 2 +- cmake/FindFFmpeg.cmake | 17 ++++++++--------- src/CMakeLists.txt | 1 + src/avutils.cpp | 3 +++ src/ffmpeg.h | 8 +++++++- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 732a4b29..ccf1ecbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads) find_package(FFmpeg - COMPONENTS AVCODEC AVFORMAT AVUTIL AVDEVICE AVFILTER SWSCALE SWRESAMPLE REQUIRED) + COMPONENTS AVCODEC AVFORMAT AVUTIL AVFILTER SWSCALE SWRESAMPLE REQUIRED OPTIONAL_COMPONENTS AVDEVICE) add_subdirectory(src) diff --git a/cmake/FindFFmpeg.cmake b/cmake/FindFFmpeg.cmake index 2746b393..75b49db4 100644 --- a/cmake/FindFFmpeg.cmake +++ b/cmake/FindFFmpeg.cmake @@ -228,16 +228,15 @@ if (NOT TARGET FFmpeg::FFmpeg) add_library(FFmpeg::FFmpeg ALIAS FFmpeg) endif() -# Now set the noncached _FOUND vars for the components. -foreach (_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWSCALE) - set_component_found(${_component}) -endforeach () - -# Compile the list of required vars -set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS) +set(_FFmpeg_REQUIRED_VARS) foreach (_component ${FFmpeg_FIND_COMPONENTS}) - list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS) + set_component_found(${_component}) + # Compile the list of required vars + if (FFmpeg_FIND_REQUIRED_${_component}) + list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS) + endif() endforeach () # Give a nice error message if some of the required vars are missing. -find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS}) +find_package_handle_standard_args(FFmpeg + REQUIRED_VARS FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARIES ${_FFmpeg_REQUIRED_VARS}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3c00150f..3f6bfb17 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,6 +29,7 @@ foreach(TARGET ${AV_TARGETS}) target_compile_options(${TARGET} PRIVATE ${AVCPP_WARNING_OPTIONS}) target_compile_definitions(${TARGET} PUBLIC __STDC_CONSTANT_MACROS) + target_compile_definitions(${TARGET} PUBLIC AVCPP_HAS_LIBAVDEVICE=${AVDEVICE_FOUND}) target_link_libraries(${TARGET} PRIVATE Threads::Threads PUBLIC FFmpeg::FFmpeg) target_include_directories(${TARGET} PUBLIC diff --git a/src/avutils.cpp b/src/avutils.cpp index 6b7aa969..33be309d 100644 --- a/src/avutils.cpp +++ b/src/avutils.cpp @@ -177,7 +177,10 @@ void init() #if LIBAVFILTER_VERSION_MAJOR < 7 // FFmpeg 4.0 avfilter_register_all(); #endif + +#ifdef AVCPP_HAS_LIBAVDEVICE avdevice_register_all(); +#endif #if LIBAVCODEC_VERSION_MAJOR < 58 // FFmpeg 4.0 av_lockmgr_register(avcpp_lockmgr_cb); diff --git a/src/ffmpeg.h b/src/ffmpeg.h index 397204fd..568dda4f 100644 --- a/src/ffmpeg.h +++ b/src/ffmpeg.h @@ -9,7 +9,7 @@ extern "C" #include #include #include -#include +#include #include #include #include @@ -30,6 +30,12 @@ extern "C" { #endif } +extern "C" { +#ifdef AVCPP_HAS_LIBAVDEVICE +#include +#endif +} + // Compat level // avcodec