# - Try to find GStreamer
# Once done this will define
#
#  GSTREAMER_FOUND - system has GStreamer
#  GSTREAMER_INCLUDE_DIR - the GStreamer include directory
#  GSTREAMER_WEBRTC_INCLUDE_DIR - the GStreamer include directory
#  GSTREAMER_LIBRARIES - the libraries needed to use GStreamer
#  GSTREAMER_DEFINITIONS - Compiler switches required for using GStreamer
#  GSTREAMER_VERSION - the version of GStreamer

# Copyright (c) 2008 Helio Chissini de Castro, <helio@kde.org>
#  (c)2006, Tim Beaulen <tbscope@gmail.com>

# TODO: Other versions --> GSTREAMER_X_Y_FOUND (Example: GSTREAMER_0_8_FOUND and GSTREAMER_1.0_FOUND etc)


IF(GSTREAMER_INCLUDE_DIR AND GSTREAMER_LIBRARIES AND GSTREAMER_BASE_LIBRARY AND GSTREAMER_APP_LIBRARY AND GSTREAMER_VIDEO_LIBRARY AND GSTREAMER_AUDIO_LIBRARY)
  # in cache already
  SET(GStreamer_FIND_QUIETLY TRUE)
ELSE(GSTREAMER_INCLUDE_DIR AND GSTREAMER_LIBRARIES AND GSTREAMER_BASE_LIBRARY AND GSTREAMER_APP_LIBRARY AND GSTREAMER_VIDEO_LIBRARY AND GSTREAMER_AUDIO_LIBRARY)
  SET(GStreamer_FIND_QUIETLY FALSE)
ENDIF(GSTREAMER_INCLUDE_DIR AND GSTREAMER_LIBRARIES AND GSTREAMER_BASE_LIBRARY AND GSTREAMER_APP_LIBRARY AND GSTREAMER_VIDEO_LIBRARY AND GSTREAMER_AUDIO_LIBRARY)

IF(NOT WIN32)
  FIND_PACKAGE(PkgConfig REQUIRED)
  # use pkg-config to get the directories and then use these values
  # in the FIND_PATH() and FIND_LIBRARY() calls
  # don't make this check required - otherwise you can't use macro_optional_find_package on this one
  PKG_CHECK_MODULES(PKG_GSTREAMER gstreamer-1.0)
  SET(GSTREAMER_VERSION ${PKG_GSTREAMER_VERSION})
  SET(GSTREAMER_DEFINITIONS ${PKG_GSTREAMER_CFLAGS})
ENDIF(NOT WIN32)

FIND_PATH(GSTREAMER_INCLUDE_DIR gst/gst.h
  PATHS
  ${PKG_GSTREAMER_INCLUDE_DIRS}
  PATH_SUFFIXES gstreamer-1.0
)

IF(NOT GSTREAMER_INCLUDE_DIR)
  MESSAGE(STATUS "GStreamer: WARNING: include dir not found")
ENDIF(NOT GSTREAMER_INCLUDE_DIR)

FIND_PATH(GSTREAMER_WEBRTC_INCLUDE_DIR gst/webrtc/webrtc_fwd.h
  PATHS
  ${PKG_GSTREAMER_INCLUDE_DIRS}
  PATH_SUFFIXES gstreamer-1.0
)

# Newer versions of GStreamer put gst-config.h under lib/gstreamer-1.0/include
FIND_PATH(GSTREAMER_CONF_INCLUDE_DIR gst/gstconfig.h
  PATHS
  ${PKG_GSTREAMER_INCLUDE_DIRS}
  PATH_SUFFIXES gstreamer-1.0 gstreamer-1.0/include
)

IF(NOT GSTREAMER_CONF_INCLUDE_DIR)
  MESSAGE(STATUS "GStreamer: WARNING: gstconfig.h not found")
ENDIF(NOT GSTREAMER_CONF_INCLUDE_DIR)

IF(GSTREAMER_INCLUDE_DIR AND GSTREAMER_CONF_INCLUDE_DIR)
  IF(NOT GSTREAMER_CONF_INCLUDE_DIR STREQUAL GSTREAMER_INCLUDE_DIR)
    LIST(APPEND GSTREAMER_INCLUDE_DIR "${GSTREAMER_CONF_INCLUDE_DIR}")
  ENDIF()
ENDIF(GSTREAMER_INCLUDE_DIR AND GSTREAMER_CONF_INCLUDE_DIR)

FIND_LIBRARY(GSTREAMER_LIBRARIES NAMES gstreamer-1.0
  PATHS ${PKG_GSTREAMER_LIBRARY_DIRS}
)

FIND_LIBRARY(GSTREAMER_BASE_LIBRARY NAMES gstbase-1.0
  PATHS ${PKG_GSTREAMER_LIBRARY_DIRS}
)

FIND_LIBRARY(GSTREAMER_APP_LIBRARY NAMES gstapp-1.0
  PATHS ${PKG_GSTREAMER_LIBRARY_DIRS}
)

FIND_LIBRARY(GSTREAMER_VIDEO_LIBRARY NAMES gstvideo-1.0
  PATHS ${PKG_GSTREAMER_LIBRARY_DIRS}
)

FIND_LIBRARY(GSTREAMER_AUDIO_LIBRARY NAMES gstaudio-1.0
  PATHS ${PKG_GSTREAMER_LIBRARY_DIRS}
)

FIND_LIBRARY(GSTREAMER_WEBRTC_LIBRARY NAMES gstwebrtc-1.0
  PATHS ${PKG_GSTREAMER_LIBRARY_DIRS}
)

FIND_LIBRARY(GSTREAMER_SDP_LIBRARY NAMES gstsdp-1.0
  PATHS ${PKG_GSTREAMER_LIBRARY_DIRS}
)

IF(GSTREAMER_LIBRARIES)
ELSE(GSTREAMER_LIBRARIES)
  MESSAGE(STATUS "GStreamer: WARNING: library not found")
ENDIF(GSTREAMER_LIBRARIES)

IF(GSTREAMER_APP_LIBRARY)
ELSE(GSTREAMER_APP_LIBRARY)
  MESSAGE(STATUS "GStreamer: WARNING: app library not found")
ENDIF(GSTREAMER_APP_LIBRARY)

IF(GSTREAMER_INCLUDE_DIR AND GSTREAMER_LIBRARIES AND GSTREAMER_BASE_LIBRARY AND GSTREAMER_APP_LIBRARY AND GSTREAMER_VIDEO_LIBRARY AND GSTREAMER_AUDIO_LIBRARY)
  SET(GSTREAMER_FOUND TRUE)
ELSE(GSTREAMER_INCLUDE_DIR AND GSTREAMER_LIBRARIES AND GSTREAMER_BASE_LIBRARY AND GSTREAMER_APP_LIBRARY AND GSTREAMER_VIDEO_LIBRARY AND GSTREAMER_AUDIO_LIBRARY)
  SET(GSTREAMER_FOUND FALSE)
ENDIF(GSTREAMER_INCLUDE_DIR AND GSTREAMER_LIBRARIES AND GSTREAMER_BASE_LIBRARY AND GSTREAMER_APP_LIBRARY AND GSTREAMER_VIDEO_LIBRARY AND GSTREAMER_AUDIO_LIBRARY)

IF(GSTREAMER_WEBRTC_INCLUDE_DIR AND GSTREAMER_WEBRTC_LIBRARY AND GSTREAMER_SDP_LIBRARY)
  SET(GSTREAMER_WEBRTC_FOUND TRUE)
ELSE(GSTREAMER_WEBRTC_INCLUDE_DIR AND GSTREAMER_WEBRTC_LIBRARY AND GSTREAMER_SDP_LIBRARY)
  SET(GSTREAMER_WEBRTC_FOUND FALSE)
ENDIF(GSTREAMER_WEBRTC_INCLUDE_DIR AND GSTREAMER_WEBRTC_LIBRARY AND GSTREAMER_SDP_LIBRARY)

IF(GSTREAMER_FOUND)
  IF(NOT GStreamer_FIND_QUIETLY)
    MESSAGE(STATUS "Found GStreamer: ${GSTREAMER_LIBRARIES}")
  ENDIF (NOT GStreamer_FIND_QUIETLY)
ELSE(GSTREAMER_FOUND)
  IF(GStreamer_FIND_REQUIRED)
    MESSAGE(SEND_ERROR "Could NOT find GStreamer")
  ENDIF (GStreamer_FIND_REQUIRED)
ENDIF (GSTREAMER_FOUND)

INCLUDE(FindPackageHandleStandardArgs)

IF(GSTREAMER_VERSION)
  find_package_handle_standard_args(Gstreamer
    REQUIRED_VARS
      GSTREAMER_LIBRARIES
      GSTREAMER_INCLUDE_DIR
    VERSION_VAR
      GSTREAMER_VERSION
    FAIL_MESSAGE
      "Could NOT find Gstreamer, try to set the path to Gstreamer root folder in the system variable"
  )
ELSE()
  find_package_handle_standard_args(Gstreamer "Could NOT find Gstreamer, try to set the path to Gstreamer root folder in the system variable"
    GSTREAMER_LIBRARIES
    GSTREAMER_INCLUDE_DIR
  )
ENDIF()

MARK_AS_ADVANCED(GSTREAMER_INCLUDE_DIR GSTREAMER_WEBRTC_INCLUDE_DIR GSTREAMER_LIBRARIES GSTREAMER_BASE_LIBRARY GSTREAMER_INTERFACE_LIBRARY GSTREAMER_APP_LIBRARY GSTREAMER_VIDEO_LIBRARY GSTREAMER_AUDIO_LIBRARY GSTREAMER_WEBRTC_LIBRARY GSTREAMER_SDP_LIBRARY)
