############################################################################
# Copyright (c) 2010-2023 Belledonne Communications SARL.
#
# This file is part of mediastreamer2 
# (see https://gitlab.linphone.org/BC/public/mediastreamer2).
#
############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
############################################################################

cmake_minimum_required(VERSION 3.22)

project(Mediastreamer2 VERSION 5.3.0 LANGUAGES C CXX)

if(WIN32)
    if( NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
        add_compile_definitions(_WIN32_WINNT=0x0601 _ALLOW_KEYWORD_MACROS)	#_WIN32_WINNT_WIN7
    endif()
endif()

# See: https://cmake.org/cmake/help/v3.12/policy/CMP0072.html
if(POLICY CMP0072)
	cmake_policy(SET CMP0072 NEW)
endif()

set(MEDIASTREAMER_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
set(MEDIASTREAMER_MINOR_VERSION ${PROJECT_VERSION_MINOR})
set(MEDIASTREAMER_MICRO_VERSION ${PROJECT_VERSION_PATCH})
set(MEDIASTREAMER_VERSION ${PROJECT_VERSION})
set(MEDIASTREAMER_SO_VERSION "11") # incremented for mediastreamer2-4.4.0

string(REGEX MATCH "^(arm*|aarch64)" FIXED_POINT_PROCESSOR "${CMAKE_SYSTEM_PROCESSOR}")
if(FIXED_POINT_PROCESSOR)
	set(ENABLE_FIXED_POINT_DEFAULT_VALUE YES)
else()
	set(ENABLE_FIXED_POINT_DEFAULT_VALUE NO)
endif()
if(IOS)
	set(ENABLE_MACSND_DEFAULT_VALUE NO)
	set(ENABLE_MACAQSND_DEFAULT_VALUE YES)
else()
	set(ENABLE_MACSND_DEFAULT_VALUE YES)
	set(ENABLE_MACAQSND_DEFAULT_VALUE NO)
endif()
set(ENABLE_GLX_DEFAULT_VALUE NO)
set(ENABLE_X11_DEFAULT_VALUE NO)
if(UNIX)
	if(NOT APPLE)
		set(ENABLE_GLX_DEFAULT_VALUE YES)
		set(ENABLE_X11_DEFAULT_VALUE YES)
	endif()
endif()
if(UNIX AND NOT APPLE AND NOT ANDROID)
	set(LINUX_OR_BSD 1)
endif()

include(CMakeDependentOption)

option(ENABLE_DEBUG_LOGS "Enable the display of traces showing the execution of the library." NO)
option(ENABLE_DOC "Enable documentation generation with Doxygen." YES)
option(ENABLE_FIXED_POINT "Turn on fixed point computations." ${ENABLE_FIXED_POINT_DEFAULT_VALUE})
option(ENABLE_NON_FREE_FEATURES "Allow inclusion of non-free features in the build." NO)
option(ENABLE_PCAP "Enable PCAP support." NO)
option(ENABLE_STRICT "Build with strict compile options." YES)
option(ENABLE_RELATIVE_PREFIX "Find resources relatively to the installation directory." NO)
option(ENABLE_TOOLS "Turn on or off compilation of tools." YES)
option(ENABLE_UNIT_TESTS "Enable compilation of unit tests." YES)
option(ENABLE_PACKAGE_SOURCE "Create 'package_source' target for source archive making" OFF)
option(ENABLE_OPENGL_PROFILING "Measure OpenGL render time (for performance profiling)" OFF)

option(ENABLE_SRTP "Build with the SRTP transport support." YES)
cmake_dependent_option(ENABLE_ZRTP "Build with ZRTP support." YES "ENABLE_SRTP" NO)
cmake_dependent_option(ENABLE_GOCLEAR "Build with ZRTP GoClear message support (RFC 6189 - section 5.11)." YES "ENABLE_ZRTP" NO)

option(ENABLE_SOUND "Can be used to turn off all possible sound backends." YES)
cmake_dependent_option(ENABLE_ALSA "Enable ALSA support." YES "ENABLE_SOUND;LINUX_OR_BSD" NO)
cmake_dependent_option(ENABLE_ANDROIDSND "Enable Android sound support." NO "ENABLE_SOUND;ANDROID" NO)
cmake_dependent_option(ENABLE_ARTSC "Enable artsc (KDE<4) sound input/output." NO "ENABLE_SOUND;LINUX_OR_BSD" NO)
cmake_dependent_option(ENABLE_MACSND "Enable MAC OS X Audio Units sound support." ${ENABLE_MACSND_DEFAULT_VALUE} "ENABLE_SOUND;APPLE" NO)
cmake_dependent_option(ENABLE_MACAQSND "Enable native MAC OS X Audio Queue sound support." ${ENABLE_MACAQSND_DEFAULT_VALUE} "ENABLE_SOUND;APPLE" NO)
cmake_dependent_option(ENABLE_QSA "Enable QSA (QNX Sound Architecture) support." NO "ENABLE_SOUND" NO)
# OSS is mostly deprecated in 2016, so it should not be built by default (it won't work anyway). Let user defines it
# itself only if he REALLY needs it.
cmake_dependent_option(ENABLE_OSS "Enable OSS support." NO "ENABLE_SOUND;LINUX_OR_BSD" NO)
cmake_dependent_option(ENABLE_PORTAUDIO "Enable portaudio native support." NO "ENABLE_SOUND" NO)
cmake_dependent_option(ENABLE_PULSEAUDIO "Enable pulseaudio support." YES "ENABLE_SOUND" NO)
option(ENABLE_G726 "Build mediastreamer2 with the G726 codec." NO)
option(ENABLE_GSM "Build mediastreamer2 with the GSM codec." YES)
option(ENABLE_BV16 "Build mediastreamer2 with the BV16 codec." YES)
option(ENABLE_OPUS "Build mediastreamer2 with the OPUS codec." YES)
option(ENABLE_SPEEX_CODEC "Build mediastreamer2 with the SPEEX codec." YES)
option(ENABLE_SPEEX_DSP "Build mediastreamer2 with the SPEEX DSP support." YES)
option(ENABLE_G729 "Build mediastreamer2 with the G729 codec." YES)
option(ENABLE_G729B_CNG "Build mediastreamer2 with G729 annex B cng." NO)
cmake_dependent_option(ENABLE_RESAMPLE "Build mediastreamer2 with the resampling capability." YES "ENABLE_SPEEX_DSP" NO)

option(ENABLE_VIDEO "Build mediastreamer2 with video support." YES)
option(ENABLE_QT_GL "Enable Qt OpenGL rendering support." OFF)
cmake_dependent_option(ENABLE_FFMPEG "Build mediastreamer2 with ffmpeg video support." YES "ENABLE_VIDEO" NO)
mark_as_advanced(ENABLE_FFMPEG)
cmake_dependent_option(ENABLE_GL "Enable generic OpenGL rendering support." YES "ENABLE_VIDEO" NO)
cmake_dependent_option(ENABLE_GLX "Enable X11+OpenGL rendering support (requires glx and glew)." ${ENABLE_GLX_DEFAULT_VALUE} "ENABLE_VIDEO;UNIX" NO)
cmake_dependent_option(ENABLE_SDL "Enable SDL support." NO "ENABLE_FFMPEG" NO)
cmake_dependent_option(ENABLE_V4L "Enable Video4Linux support." YES "ENABLE_VIDEO;LINUX_OR_BSD" NO)
cmake_dependent_option(ENABLE_X11 "Enable X11 support." ${ENABLE_X11_DEFAULT_VALUE} "ENABLE_VIDEO;UNIX" NO)
cmake_dependent_option(ENABLE_XV "Enable XV support." ${ENABLE_X11_DEFAULT_VALUE} "ENABLE_VIDEO;UNIX" NO)
cmake_dependent_option(ENABLE_THEORA "Enable theora video codec support." YES "ENABLE_VIDEO" NO)
cmake_dependent_option(ENABLE_VPX "Build mediastreamer2 with VPX codec." YES "ENABLE_VIDEO" NO)
cmake_dependent_option(ENABLE_LIBYUV "Build mediastreamer2 with LibYUV support." YES "ENABLE_VIDEO" NO)
option(ENABLE_MKV "Enable support of MKV files reading and writing." YES)
cmake_dependent_option(ENABLE_QNX "Build medistreamer2 with BB10 filters." NO "ENABLE_VIDEO" NO)
option(ENABLE_JPEG "Enable JPEG support" YES)
cmake_dependent_option(ENABLE_QRCODE "Enable QRCode support" YES "ENABLE_VIDEO" NO)
cmake_dependent_option(ENABLE_AV1 "Build mediastreamer2 with AV1 support." YES "ENABLE_VIDEO" NO)


set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS NO)

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

include(CheckIncludeFile)
include(CheckLibraryExists)
include(CMakePushCheckState)
include(GNUInstallDirs)

if(NOT CMAKE_INSTALL_RPATH AND CMAKE_INSTALL_PREFIX)
	set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR})
	message(STATUS "Setting install rpath to ${CMAKE_INSTALL_RPATH}")
endif()

check_include_file(sys/shm.h HAVE_SYS_SHM_H)
check_include_file(alloca.h HAVE_ALLOCA_H)
if(ENABLE_OSS)
	check_include_file(soundcard.h HAVE_SOUNDCARD_H)
	check_include_file(sys/soundcard.h HAVE_SYS_SOUNDCARD_H)
	check_include_file(machine/soundcard.h HAVE_MACHINE_SOUNDCARD_H)
	check_include_file(sys/audio.h HAVE_SYS_AUDIO_H)
	if(NOT (HAVE_SOUNDCARD_H OR HAVE_SYS_SOUNDCARD_H OR HAVE_MACHINE_SOUNDCARD_H OR HAVE_SYS_AUDIO_H))
		set(ENABLE_OSS OFF CACHE BOOL "Enable OSS support." FORCE)
	endif()
endif()

check_library_exists("dl" "dlopen" "" HAVE_DLOPEN)

include(TestBigEndian)
test_big_endian(WORDS_BIGENDIAN)

find_package(Ortp 5.3.0 REQUIRED)

if(ENABLE_PCAP)
	find_package(PCAP)
endif()
if(ENABLE_SRTP)
	find_package(SRTP 2 REQUIRED)
endif()
if(ENABLE_ZRTP)
	find_package(BZRTP 5.3.0 REQUIRED)
endif()
if(ENABLE_G726)
	find_package(SpanDSP REQUIRED)
endif()
if(ENABLE_GSM)
	find_package(GSM)
	if(NOT GSM_FOUND)
		message(WARNING "Could not find GSM library, mediastreamer2 will be compiled without the GSM codec.")
		set(ENABLE_GSM OFF CACHE BOOL "Build mediastreamer2 with the GSM codec." FORCE)
	endif()
endif()
if(ENABLE_BV16)
	find_package(BV16)
	if(NOT BV16_FOUND)
		message(WARNING "Could not find BV16 library, mediastreamer2 will be compiled without the BV16 codec.")
		set(ENABLE_BV16 OFF CACHE BOOL "Build mediastreamer2 with the BV16 codec." FORCE)
	endif()
endif()
if(ENABLE_OPUS)
	find_package(Opus)
	if(NOT Opus_FOUND)
		message(WARNING "Could not find opus library, mediastreamer2 will be compiled without the OPUS codec.")
		set(ENABLE_OPUS OFF CACHE BOOL "Build mediastreamer2 with the OPUS codec." FORCE)
	endif()
endif()
if(ENABLE_SPEEX_CODEC)
	find_package(Speex)
	if(NOT Speex_FOUND)
		message(WARNING "Could not find speex library, mediastreamer2 will be compiled without the SPEEX codec.")
		set(ENABLE_SPEEX_CODEC OFF CACHE BOOL "Build mediastreamer2 with the SPEEX codec." FORCE)
	endif()
endif()
if(ENABLE_SPEEX_DSP)
	find_package(SpeexDSP REQUIRED)
endif()
if(ENABLE_ALSA)
	find_package(ALSA)
	if(NOT ALSA_FOUND)
		set(ENABLE_ALSA OFF CACHE BOOL "Enable ALSA support." FORCE)
	endif()
endif()
if(ENABLE_ARTSC)
	find_package(Arts REQUIRED)
endif()
if(ENABLE_PORTAUDIO)
	find_package(PortAudio REQUIRED)
endif()
if(ENABLE_PULSEAUDIO)
	find_package(PulseAudio)
	if(NOT PulseAudio_FOUND)
		set(ENABLE_PULSEAUDIO OFF CACHE BOOL "Enable pulseaudio support." FORCE)
	endif()
endif()
if(ENABLE_QSA)
	find_package(QSA REQUIRED)
	find_package(QnxAudioManager REQUIRED)
endif()
if(ENABLE_VIDEO)
	add_definitions(-DVIDEO_ENABLED)
endif()
if(ENABLE_FFMPEG)
	find_package(FFMpeg REQUIRED)
else()
	add_definitions(-DNO_FFMPEG)
endif()
if(ENABLE_V4L)
	find_package(V4L)
	if(NOT V4L_FOUND)
		message(FATAL_ERROR "Missing V4L. It is highly recommended to build with libv4l2 headers and library. Many camera will won't work or will crash your application if libv4l2 is not installed. If you know what you are doing, you can use -DENABLE_V4L=0 to disable this check.")
	endif()
endif()
if(ENABLE_X11)
	find_package(X11)
	if(NOT X11_FOUND)
		set(ENABLE_X11 OFF CACHE BOOL "Enable X11 support." FORCE)
	elseif(X11_Xlib_INCLUDE_PATH)
		set(HAVE_X11_XLIB_H 1)
	endif()
endif()
if(ENABLE_XV AND ENABLE_X11)
	find_package(X11)
	if(X11_Xv_FOUND)
		set(HAVE_XV 1)
	else()
		set(HAVE_XV 0)
		set(ENABLE_XV OFF CACHE BOOL "Enable XV support." FORCE)
	endif()
else()
	set(HAVE_XV 0)
endif()
if(ENABLE_GL)
	if(WIN32)
		find_package(QtAngle)
		set(HAVE_GL 1)
		if(NOT QtAngle_FOUND) # QtAngle is not found so we use internal files
			include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include/OpenGL")
		endif()
	else()
		find_package(OpenGL)
		if(OPENGL_FOUND)
			set(HAVE_GL 1)
			include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include/OpenGL")
		else()
			set(ENABLE_GL OFF CACHE BOOL "Enable generic OpenGL rendering support." FORCE)
		endif()
	endif()
endif()

if(ENABLE_VIDEO AND APPLE)
	include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include/OpenGL")
endif()
if(ENABLE_GLX)
	find_package(OpenGL COMPONENTS GLX)
	if(OpenGL_GLX_FOUND)
		set(HAVE_GLX 1)
	else()
		set(ENABLE_GLX OFF CACHE BOOL "Enable X11+OpenGL rendering support (requires glx and glew)." FORCE)
	endif()
endif()
if(NOT APPLE AND NOT ANDROID AND OPENGL_FOUND OR OpenGL_GLX_FOUND)	
	set(GLEW_VERBOSE 1)
	include(FindGLEW) # Use cmake find in place of find_package(Glew REQUIRED)
	if(NOT GLEW_FOUND)
		message(FATAL_ERROR "Glew is missing. Mediastreamer2 need it")
	endif()
	if(GLEW_VERSION_MAJOR LESS_EQUAL 1 AND GLEW_VERSION_MINOR LESS_EQUAL 11)
		set(MS2_USE_OLD_OPENGL_PROTOTYPE TRUE) # Before 1.11, Glew have few different headers from OpenGL Specs
	endif()
endif()
# TODO: Handle SDL option
if(ENABLE_THEORA)
	find_package(Theora)
	if(NOT Theora_FOUND)
		message(WARNING "Could not find theora library, mediastreamer2 will be compiled without theora support.")
		set(ENABLE_THEORA OFF CACHE BOOL "Enable theora video codec support." FORCE)
	endif()
endif()
if(ENABLE_VPX)
	find_package(VPX)
	if(VPX_FOUND)
		set(HAVE_VPX 1)
	else()
		message(WARNING "Could not find vpx library, mediastreamer2 will be compiled without vpx support.")
		set(ENABLE_VPX OFF CACHE BOOL "Build mediastreamer2 with VPX codec." FORCE)
	endif()
endif()
if(ENABLE_MKV)
	find_package(BCMatroska2 0.23)
	if(NOT BCMatroska2_FOUND)
		message(WARNING "Could not find bcmatroska2 library, mediastreamer2 will be compiled without MKV support.")
		set(ENABLE_MKV OFF CACHE BOOL "Enable support of MKV files reading and writing." FORCE)
	endif()
endif()

if(ENABLE_QNX)
	find_package(Screen REQUIRED)
	find_package(CamApi REQUIRED)
endif()

if(ENABLE_SOUND AND NOT(WIN32 OR ENABLE_ALSA OR ENABLE_ARTSC OR ENABLE_MACSND OR ENABLE_MACAQSND OR ENABLE_OSS OR ENABLE_PORTAUDIO OR ENABLE_PULSEAUDIO OR ENABLE_QSA OR ENABLE_ANDROIDSND))
	message(FATAL_ERROR "Could not find a support sound driver API. Use -DENABLE_SOUND=NO if you don't care about having sound.")
endif()

if(ENABLE_G729 OR ENABLE_G729B_CNG)
	find_package(BCG729 1.1.1)
	if(NOT BCG729_FOUND)
		message(WARNING "Could not find bcg729, mediastreamer2 will be compiled without G729 codec and G729 AnnexB in RFC3389.")
		set(ENABLE_G729 OFF CACHE BOOL "Build mediastreamer2 with the G729 codec." FORCE)
		set(ENABLE_G729_CNG OFF CACHE BOOL "Build mediastreamer2 with G729 Annex B cng." FORCE)
	endif()
endif()

if(ENABLE_JPEG)
	find_package(TurboJpeg)
	if(NOT TurboJpeg_FOUND)
		message(WARNING "Could not find libturbo-jpeg, mediastreamer2 will be compiled without LibJpeg-Turbo.")
		set(ENABLE_JPEG OFF CACHE BOOL "Enable JPEG support" FORCE)
	endif()
endif()
if(ENABLE_LIBYUV)
	find_package(LibYUV)
	if(NOT LibYUV_FOUND)
		message(WARNING "Could not find libyuv, mediastreamer2 will be compiled without LibYUV support.")
		set(ENABLE_LIBYUV OFF CACHE BOOL "Enable LibYUV support" FORCE)
	endif()
endif()
if(ENABLE_QRCODE)
	find_package(ZXing)
	if(NOT ZXing_FOUND)
		message(WARNING "Could not find ZXing, mediastreamer2 will be compiled without ZXing(QRCode)")
		set(ENABLE_QRCODE OFF CACHE BOOL "Enable QRCode support" FORCE)
	else()
		add_definitions(-DQRCODE_ENABLED)
	endif()
endif()
if(ENABLE_AV1)
	find_package(Dav1d)
	if(NOT Dav1d_FOUND)
		message(WARNING "Could not find dav1d, mediastreamer2 will be compiled without AV1 support.")
		set(ENABLE_AV1 OFF CACHE BOOL "Enable AV1 support" FORCE)
	endif()
	find_package(Aom)
	if(NOT Aom_FOUND)
		message(WARNING "Could not find aom, mediastreamer2 will be compiled without AV1 support.")
		set(ENABLE_AV1 OFF CACHE BOOL "Enable AV1 support" FORCE)
	endif()
endif()
if(ANDROID)
	find_package(CpuFeatures REQUIRED)
	if(CMAKE_ANDROID_NDK_VERSION VERSION_LESS 19)
		find_package(Support REQUIRED)
	endif()
endif()

find_package(BCToolbox 5.3.0 REQUIRED OPTIONAL_COMPONENTS tester)

include_directories(
	include
	src/audiofilters
	src/utils
	src/voip
	${CMAKE_CURRENT_BINARY_DIR}
	${CMAKE_CURRENT_BINARY_DIR}/src
)

set(MEDIASTREAMER2_INCLUDE_DIRS )
set(LINK_LIBS )
if(WIN32)
	if(NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsPhone" AND NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
		list(APPEND LINK_LIBS ole32 oleaut32 uuid Winmm Ws2_32)
		if(ENABLE_VIDEO)
			list(APPEND LINK_LIBS gdi32 strmiids user32 vfw32)
		endif()
	endif()
	list(APPEND LINK_LIBS ws2_32)# symbols for in6addr_any
endif()
if(LIBM)
	list(APPEND LINK_LIBS m)
endif()
if(CpuFeatures_FOUND)
	list(APPEND LINK_LIBS ${CpuFeatures_TARGET})
endif()
if(Support_FOUND)
	list(APPEND LINK_LIBS ${Support_TARGET})
endif()
if(ANDROID AND ENABLE_VIDEO)
	list(APPEND LINK_LIBS EGL GLESv3 android)
endif()
if(HAVE_DLOPEN)
	list(APPEND LINK_LIBS dl)
endif()
if(ALSA_FOUND)
	list(APPEND LINK_LIBS ALSA::ALSA)
	set(__ALSA_ENABLED__ 1)
endif()
if(Arts_FOUND)
	list(APPEND LINK_LIBS ${Arts_TARGET})
	set(__ARTS_ENABLED__ 1)
endif()
if(PortAudio_FOUND)
	list(APPEND LINK_LIBS ${PortAudio_TARGET})
	set(__PORTAUDIO_ENABLED__ 1)
endif()
if(PulseAudio_FOUND)
	list(APPEND LINK_LIBS ${PulseAudio_TARGET})
  	set(__PULSEAUDIO_ENABLED__ 1)
endif()
if(QSA_FOUND)
	list(APPEND LINK_LIBS ${QSA_TARGET})
	list(APPEND LINK_LIBS ${QnxAudioManager_TARGET})
	set(__QSA_ENABLED__ 1)
	set(__QNX__ 1)
endif()
if(FFMpeg_FOUND)
	list(APPEND LINK_LIBS ${FFMpeg_TARGETS})
	if(MSVC)
		add_definitions(-Dinline=__inline)
	endif()
endif()
if(V4L_FOUND)
	list(APPEND LINK_LIBS ${V4L_TARGETS})
endif()
if(XV_FOUND)
	list(APPEND MEDIASTREAMER2_INCLUDE_DIRS ${XV_INCLUDE_DIRS})
endif()
if(OPENGL_FOUND)
	list(APPEND LINK_LIBS OpenGL::GL)
endif()
if(OpenGL_GLX_FOUND)
	list(APPEND LINK_LIBS OpenGL::GLX)
endif()
if(GLEW_FOUND)
	list(APPEND LINK_LIBS GLEW::GLEW)
endif()
if(Theora_FOUND)
	list(APPEND LINK_LIBS ${Theora_TARGET})
endif()
if(VPX_FOUND)
	list(APPEND LINK_LIBS ${VPX_TARGET})
endif()
if(Screen_FOUND)
	list(APPEND LINK_LIBS ${Screen_TARGET})
endif()
if(CamApi_FOUND)
	list(APPEND LINK_LIBS ${CamApi_TARGET})
endif()
if(TurboJpeg_FOUND)
	list(APPEND LINK_LIBS ${TurboJpeg_TARGET})
	set(HAVE_TURBO_JPEG 1)
endif()
if(LibYUV_FOUND)
	list(APPEND LINK_LIBS ${LibYUV_TARGET})
endif()
if(ZXing_FOUND)
	list(APPEND LINK_LIBS ${ZXing_TARGET})
endif()
if(BCG729_FOUND)
	if(ENABLE_G729_CNG)
		add_definitions("-DHAVE_G729B=1")
	endif()
	list(APPEND LINK_LIBS ${BCG729_TARGET})
endif()
if(GSM_FOUND)
	list(APPEND LINK_LIBS ${GSM_TARGET})
endif()
if(BZRTP_FOUND)
	list(APPEND LINK_LIBS ${BZRTP_TARGET})
	set(HAVE_ZRTP 1)
	set(HAVE_GOCLEAR 1)
endif()

if(SRTP_FOUND)
	list(APPEND LINK_LIBS ${SRTP_TARGET})
	set(HAVE_SRTP 1)
endif()
if(SpanDSP_FOUND)
	list(APPEND LINK_LIBS ${SpanDSP_TARGET})
endif()
if(Opus_FOUND)
	list(APPEND LINK_LIBS ${Opus_TARGET})
endif()
if(Speex_FOUND)
	list(APPEND LINK_LIBS ${Speex_TARGET})
endif()
if(SpeexDSP_FOUND)
	list(APPEND LINK_LIBS  ${SpeexDSP_TARGET})
	set(HAVE_SPEEXDSP 1)
endif()
if(BV16_FOUND)
	list(APPEND LINK_LIBS ${BV16_TARGET})
endif()
if(BCMatroska2_FOUND)
	set(HAVE_MATROSKA 1)
endif()
if(PCAP_FOUND)
	list(APPEND LINK_LIBS ${PCAP_TARGET})
	set(HAVE_PCAP 1)
endif()
if(QtAngle_FOUND)
	list(APPEND MEDIASTREAMER2_INCLUDE_DIRS ${QtAngle_INCLUDE_DIRS})
endif()
if(Dav1d_FOUND)
	list(APPEND LINK_LIBS ${Dav1d_TARGET})
endif()
if(Aom_FOUND)
	list(APPEND LINK_LIBS ${Aom_TARGET})
endif()
if(ENABLE_ANDROIDSND)
	include_directories(
		src/android
		src/android/media
	)
endif()
if(ENABLE_DEBUG_LOGS)
	add_definitions(-DDEBUG)
endif()
if(ENABLE_FIXED_POINT)
	set(MS_FIXED_POINT 1)
endif()
if(ENABLE_NON_FREE_FEATURES)
	set(HAVE_NON_FREE_CODECS 1)
else()
	set(HAVE_NON_FREE_CODECS 0)
endif()
if(ENABLE_MACSND)
	set(__MACSND_ENABLED__ 1)
endif()
if(ENABLE_MACAQSND)
	set(__MAC_AQ_ENABLED__ 1)
endif()

if(ENABLE_QT_GL)#Only add headers and lib to Qt plugins
	set(QT_PACKAGES Core Gui Quick)
	if(NOT QT_DEFAULT_MAJOR_VERSION)
		set(QT_DEFAULT_MAJOR_VERSION 6)
	endif()
	find_package(Qt${QT_DEFAULT_MAJOR_VERSION} QUIET COMPONENTS Core)# Check for Qt6
	if( NOT Qt${QT_DEFAULT_MAJOR_VERSION}_FOUND)
		set(QT_DEFAULT_MAJOR_VERSION 5)
	endif()
	find_package(Qt${QT_DEFAULT_MAJOR_VERSION} REQUIRED COMPONENTS ${QT_PACKAGES})#Find package with the default version.
	message(STATUS "Build MSQOgl with Qt${QT_DEFAULT_MAJOR_VERSION}")
	foreach (package ${QT_PACKAGES})
		if(TARGET Qt${QT_DEFAULT_MAJOR_VERSION}::${package})
			list(APPEND QT_LINK_LIBS Qt${QT_DEFAULT_MAJOR_VERSION}::${package})
		endif()
	endforeach ()
endif()

set(APPLE_FRAMEWORKS)
if(APPLE)
	list(APPEND APPLE_FRAMEWORKS "-framework CoreFoundation" "-framework AudioToolbox" "-framework CoreAudio")
	if(IOS) # Necessary for Mediastreamer's tools and testers
		list(APPEND APPLE_FRAMEWORKS "-framework Foundation" "-framework QuartzCore" "-framework OpenGLES" "-framework UIKit" "-framework AVFoundation")
		if(ENABLE_VIDEO)
			list(APPEND APPLE_FRAMEWORKS "-framework CoreGraphics" "-framework CoreMedia" "-framework CoreVideo" "-framework VideoToolbox")
		endif()
	else()
		list(APPEND APPLE_FRAMEWORKS "-framework AppKit")
		if(ENABLE_MACSND)
			list(APPEND APPLE_FRAMEWORKS "-framework Carbon" "-framework AudioUnit")
		endif()
		if(ENABLE_VIDEO)
			list(APPEND APPLE_FRAMEWORKS "-framework Cocoa" "-framework OpenGL" "-framework QuartzCore" "-framework AVFoundation" "-framework VideoToolbox" "-framework CoreMedia")
		endif()
		add_definitions("-DTARGET_OS_MAC=1")
	endif()
elseif(WIN32 AND CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")#Fix error on 'vccorlib_lib_should_be_specified_before_msvcrt_lib_to_linker''
	list(APPEND LINK_OPTIONS_DEBUG "/nodefaultlib:vccorlibd" "/nodefaultlib:msvcrtd" "vccorlibd.lib" "msvcrtd.lib")
	list(APPEND LINK_OPTIONS_RELEASE "/nodefaultlib:vccorlib" "/nodefaultlib:msvcrt" "vccorlib.lib" "msvcrt.lib")
endif()

set(MEDIASTREAMER2_CPPFLAGS )
if(NOT BUILD_SHARED_LIBS)
	list(APPEND MEDIASTREAMER2_CPPFLAGS "-DMS2_STATIC")
endif()
if(MEDIASTREAMER2_CPPFLAGS)
	list(REMOVE_DUPLICATES MEDIASTREAMER2_CPPFLAGS)
	add_definitions(${MEDIASTREAMER2_CPPFLAGS})
endif()

set(STRICT_OPTIONS_CPP )
set(STRICT_OPTIONS_C )
set(STRICT_OPTIONS_OBJC "-Wno-error")
if(MSVC)
	list(APPEND STRICT_OPTIONS_CPP "/wd4142") # Disable "benign redefinition of type" warnings.
	list(APPEND STRICT_OPTIONS_CPP "/wd4996") # Disable POSIX functions warnings from bctbx.
	if(ENABLE_STRICT)
		list(APPEND STRICT_OPTIONS_CPP "/WX")
	endif()
else()
	list(APPEND STRICT_OPTIONS_CPP "-Wall" "-Wuninitialized")
	list(APPEND STRICT_OPTIONS_C "-Wstrict-prototypes" "-Wno-error=strict-prototypes")

	if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
		list(APPEND STRICT_OPTIONS_CXX "-x c++")
		if(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "10")
			list(APPEND STRICT_OPTIONS_CXX "-Wno-deprecated-copy") # introduced by clang 10
		endif()
	elseif(ENABLE_QT_GL AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER "9")
		list(APPEND STRICT_OPTIONS_CPP "-Wno-error=deprecated-copy") # introduced by gcc 9
	endif()

	if(CMAKE_C_COMPILER_ID MATCHES "Clang")
		list(APPEND STRICT_OPTIONS_CPP "-Qunused-arguments" "-Wno-unused-function" "-Wno-array-bounds")
	elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
		if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER "7")
		    list(APPEND STRICT_OPTIONS_CPP "-Wno-format-truncation")
		endif()
		if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER "8")
			list(APPEND STRICT_OPTIONS_CPP "-Wno-error=stringop-truncation" "-Wno-cast-function-type")
		endif()
	endif()
	if(APPLE)
		list(APPEND STRICT_OPTIONS_CPP "-Wno-error=unknown-warning-option" "-Qunused-arguments" "-Wno-tautological-compare" "-Wno-unused-function" "-Wno-array-bounds")
	endif()
	if(APPLE OR WIN32)
		list(APPEND STRICT_OPTIONS_CPP "-Wno-shorten-64-to-32")
	endif()
	if(ENABLE_STRICT)
		list(APPEND STRICT_OPTIONS_CPP "-Werror" "-Wextra" "-Wunused-parameter" "-fno-strict-aliasing" "-Wno-missing-field-initializers" "-Wno-error=deprecated-declarations")
	endif()
endif()
if(STRICT_OPTIONS_CPP)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_CPP)
endif()
if(STRICT_OPTIONS_C)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_C)
endif()
if(STRICT_OPTIONS_OBJC)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_OBJC)
endif()

# Set log domain for the entire project.
add_definitions(
	-DBCTBX_LOG_DOMAIN="mediastreamer"
)

#Setup framework structure and variables for ms2 plugins
set(MS2_FRAMEWORK_VERSION "A")

if(APPLE AND NOT IOS)
	set(MS2_PLUGINS_DIR "Frameworks/mediastreamer2.framework/Versions/${MS2_FRAMEWORK_VERSION}/Libraries")

	if (NOT EXISTS "${CMAKE_INSTALL_PREFIX}/Frameworks/mediastreamer2.framework/Libraries")
		install(CODE "execute_process(COMMAND sh -c \"ln -sf Versions/Current/Libraries ${CMAKE_INSTALL_PREFIX}/Frameworks/mediastreamer2.framework/Libraries\")")
	endif()
else()
	set(MS2_PLUGINS_DIR "${CMAKE_INSTALL_LIBDIR}/mediastreamer/plugins")
endif()
define_property(TARGET PROPERTY "MS2_PLUGINS_DIR" BRIEF_DOCS "Stores the location of mediastreamer2 plugins" FULL_DOCS "Stores the location of mediastreamer2 plugins")
install(DIRECTORY DESTINATION "${MS2_PLUGINS_DIR}")

if(ENABLE_RELATIVE_PREFIX)
	if(CMAKE_SYSTEM_NAME STREQUAL "WindowsPhone" OR CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
		set(PACKAGE_PLUGINS_DIR ".")
		set(PACKAGE_DATA_DIR ".")
	else()
		set(PACKAGE_PLUGINS_DIR "./${MS2_PLUGINS_DIR}")
		set(PACKAGE_DATA_DIR "./${CMAKE_INSTALL_DATADIR}")
	endif()
else()
	set(PACKAGE_PLUGINS_DIR "${CMAKE_INSTALL_PREFIX}/${MS2_PLUGINS_DIR}")
	set(PACKAGE_DATA_DIR "${CMAKE_INSTALL_DATADIR}")
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/mediastreamer-config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/mediastreamer-config.h)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/mediastreamer-config.h PROPERTIES GENERATED ON)
add_definitions("-DHAVE_CONFIG_H")

if(ENABLE_DOC)
	add_subdirectory(help)
endif()
add_subdirectory(include)
add_subdirectory(src)
if(ENABLE_UNIT_TESTS)
	add_subdirectory(tester)
endif()
if(ENABLE_TOOLS AND NOT ANDROID)
	add_subdirectory(tools)
endif()

include(CMakePackageConfigHelpers)
set(CMAKE_MODULES_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
configure_package_config_file("cmake/${PROJECT_NAME}Config.cmake.in" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
	INSTALL_DESTINATION "${CMAKE_MODULES_INSTALL_DIR}"
	PATH_VARS MS2_PLUGINS_DIR
)
write_basic_package_version_file("${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
	VERSION ${PROJECT_VERSION}
	COMPATIBILITY AnyNewerVersion
)
install(FILES
	"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
	"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
	DESTINATION ${CMAKE_MODULES_INSTALL_DIR}
)
if(PCAP_FOUND)
	install(FILES "cmake/FindPCAP.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(SRTP_FOUND)
	install(FILES "cmake/FindSRTP.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(SpanDSP_FOUND)
	install(FILES "cmake/FindSpanDSP.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(GSM_FOUND)
	install(FILES "cmake/FindGSM.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(BV16_FOUND)
	install(FILES "cmake/FindBV16.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(Opus_FOUND)
	install(FILES "cmake/FindOpus.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(Speex_FOUND)
	install(FILES "cmake/FindSpeex.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(SpeexDSP_FOUND)
	install(FILES "cmake/FindSpeexDSP.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(Arts_FOUND)
	install(FILES "cmake/FindArts.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(PortAudio_FOUND)
	install(FILES "cmake/FindPortAudio.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(PulseAudio_FOUND)
	install(FILES "cmake/FindPulseAudio.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(QSA_FOUND)
	install(FILES "cmake/FindQSA.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(QnxAudioManager_FOUND)
	install(FILES "cmake/FindQnxAudioManager.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(FFMpeg_FOUND)
	install(FILES "cmake/FindFFMpeg.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(V4L_FOUND)
	install(FILES "cmake/FindV4L.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(Theora_FOUND)
	install(FILES "cmake/FindTheora.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(VPX_FOUND)
	install(FILES "cmake/FindVPX.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(Screen_FOUND)
	install(FILES "cmake/FindScreen.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(CamApi_FOUND)
	install(FILES "cmake/FindCamApi.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(TurboJpeg_FOUND)
	install(FILES "cmake/FindTurboJpeg.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(LibYUV_FOUND)
	install(FILES "cmake/FindLibYUV.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()
if(ZXing_FOUND)
	install(FILES "cmake/FindZXing.cmake" DESTINATION ${CMAKE_MODULES_INSTALL_DIR})
endif()

install(EXPORT ${PROJECT_NAME}Targets
	FILE "${PROJECT_NAME}Targets.cmake"
	DESTINATION ${CMAKE_MODULES_INSTALL_DIR}
)

if(ENABLE_PACKAGE_SOURCE)
	add_subdirectory(build)
endif()
