From b0d0a0739e3f03dcfc1839ba18fea8c90d712d5f Mon Sep 17 00:00:00 2001
From: Benau <Benau@users.noreply.github.com>
Date: Tue, 4 Apr 2017 14:23:04 +0800
Subject: [PATCH] Add CMake flag to enable / disable recorder

---
 CMakeLists.txt                       | 70 +++++++++++++++++++---------
 cmake/FindOggVorbis.cmake            |  5 ++
 src/graphics/irr_driver.cpp          |  8 ++--
 src/recorder/pulseaudio_recorder.cpp | 19 ++++++--
 src/recorder/pulseaudio_recorder.hpp |  6 ++-
 src/recorder/recorder_common.cpp     |  2 +-
 src/recorder/recorder_common.hpp     |  2 +-
 src/recorder/vorbis_encoder.cpp      |  2 +-
 src/recorder/vorbis_encoder.hpp      |  2 +-
 src/recorder/vpx_encoder.cpp         |  2 +-
 src/recorder/vpx_encoder.hpp         |  2 +-
 src/recorder/wasapi_recorder.cpp     |  2 +-
 src/recorder/wasapi_recorder.hpp     |  6 ++-
 src/recorder/webm_writer.cpp         |  2 +-
 src/recorder/webm_writer.hpp         |  2 +-
 15 files changed, 92 insertions(+), 40 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f40683622..9a9250b53 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,6 +8,7 @@ if(NOT (CMAKE_MAJOR_VERSION VERSION_LESS 3))
 endif()
 
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
+include(CMakeDependentOption)
 
 include(BuildTypeSTKRelease)
 if (NOT CMAKE_BUILD_TYPE)
@@ -21,6 +22,13 @@ option(CHECK_ASSETS "Check if assets are installed in ../stk-assets" ON)
 option(USE_SYSTEM_ANGELSCRIPT "Use system angelscript instead of built-in angelscript. If you enable this option, make sure to use a compatible version." OFF)
 option(ENABLE_NETWORK_MULTIPLAYER "Enable network multiplayer. This will replace the online profile GUI in the main menu with the network multiplayer GUI" OFF)
 
+CMAKE_DEPENDENT_OPTION(BUILD_RECORDER "Build opengl recorder" ON
+    "NOT SERVER_ONLY;NOT USE_GLES2;NOT APPLE" OFF)
+CMAKE_DEPENDENT_OPTION(BUILD_RECORDER_WITH_SOUND "Build opengl recorder with sound" ON
+    BUILD_RECORDER OFF)
+CMAKE_DEPENDENT_OPTION(BUILD_PULSE_WO_DL "If pulseaudio in your distro / system is optional, turn this off to load pulse with libdl"
+    ON "BUILD_RECORDER_WITH_SOUND;UNIX" OFF)
+
 if (UNIX AND NOT APPLE)
     option(USE_GLES2 "Use OpenGL ES2 renderer" OFF)
 endif()
@@ -74,6 +82,16 @@ if(SERVER_ONLY)
     add_definitions(-DNO_IRR_COMPILE_WITH_X11_)
 endif()
 
+if(BUILD_RECORDER)
+    add_definitions(-DENABLE_RECORDER)
+    if(BUILD_RECORDER_WITH_SOUND)
+        add_definitions(-DENABLE_REC_SOUND)
+        if(BUILD_PULSE_WO_DL)
+            add_definitions(-DENABLE_PULSE_WO_DL)
+        endif()
+    endif()
+endif()
+
 # Build the Bullet physics library
 add_subdirectory("${PROJECT_SOURCE_DIR}/lib/bullet")
 include_directories("${PROJECT_SOURCE_DIR}/lib/bullet/src")
@@ -111,7 +129,7 @@ if((WIN32 AND NOT MINGW) OR APPLE)
     set(PNG_LIBRARY png15_static)
 endif()
 
-# Add jpeg-turbo library
+# Add jpeg library
 if (APPLE)
     add_subdirectory("${PROJECT_SOURCE_DIR}/lib/jpeglib")
     include_directories("${PROJECT_SOURCE_DIR}/lib/jpeglib")
@@ -119,36 +137,33 @@ if (APPLE)
     set(JPEG_LIBRARY jpeglib)
 else()
     find_package(JPEG REQUIRED)
-    find_library(TURBOJPEG_LIBRARY NAMES turbojpeg libturbojpeg PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
-    mark_as_advanced(TURBOJPEG_LIBRARY)
     include_directories(${JPEG_INCLUDE_DIR})
 endif()
 
-if (UNIX AND NOT APPLE)
+if(BUILD_RECORDER)
     include(FindPkgConfig)
-    pkg_check_modules(PULSEAUDIO libpulse)
-    include_directories(${PULSEAUDIO_INCLUDEDIR})
+    find_library(TURBOJPEG_LIBRARY NAMES turbojpeg libturbojpeg PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
+    mark_as_advanced(TURBOJPEG_LIBRARY)
+    if(UNIX)
+        pkg_check_modules(VPX vpx)
+    else()
+        find_path(VPX_INCLUDEDIR NAMES vpx/vpx_codec.h PATHS "${PROJECT_SOURCE_DIR}/dependencies/include")
+        find_library(VPX_LIBRARIES NAMES libvpx PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
+    endif()
+    include_directories(${VPX_INCLUDEDIR})
+    if(BUILD_RECORDER_WITH_SOUND AND UNIX)
+        pkg_check_modules(PULSEAUDIO libpulse)
+        include_directories(${PULSEAUDIO_INCLUDEDIR})
+    endif()
+    add_subdirectory("${PROJECT_SOURCE_DIR}/lib/libwebm")
+    include_directories("${PROJECT_SOURCE_DIR}/lib/libwebm")
 endif()
 
-if (UNIX AND NOT APPLE)
-    pkg_check_modules(VPX vpx)
-else()
-    find_path(VPX_INCLUDEDIR NAMES vpx/vpx_codec.h PATHS "${PROJECT_SOURCE_DIR}/dependencies/include")
-    find_library(VPX_LIBRARIES NAMES libvpx PATHS "${PROJECT_SOURCE_DIR}/dependencies/lib")
-endif()
-
-include_directories(${VPX_INCLUDEDIR})
-
 if(NOT SERVER_ONLY AND NOT USE_GLES2)
     add_subdirectory("${PROJECT_SOURCE_DIR}/lib/graphics_utils")
     include_directories("${PROJECT_SOURCE_DIR}/lib/graphics_utils")
 endif()
 
-if(NOT SERVER_ONLY AND NOT USE_GLES2)
-    add_subdirectory("${PROJECT_SOURCE_DIR}/lib/libwebm")
-    include_directories("${PROJECT_SOURCE_DIR}/lib/libwebm")
-endif()
-
 # Build the irrlicht library
 add_subdirectory("${PROJECT_SOURCE_DIR}/lib/irrlicht")
 include_directories("${PROJECT_SOURCE_DIR}/lib/irrlicht/include")
@@ -408,14 +423,14 @@ target_link_libraries(supertuxkart
 
 if(NOT SERVER_ONLY)
     if(NOT USE_GLES2)
-        target_link_libraries(supertuxkart ${OPENGL_LIBRARIES} glew graphics_utils webm)
+        target_link_libraries(supertuxkart ${OPENGL_LIBRARIES} glew graphics_utils)
     else()
         target_link_libraries(supertuxkart EGL GLESv2)
     endif()
 endif()
 
 if(UNIX AND NOT APPLE)
-    target_link_libraries(supertuxkart dl ${X11_LIBRARIES} ${XRANDR_LIBRARIES})
+    target_link_libraries(supertuxkart ${X11_LIBRARIES} ${XRANDR_LIBRARIES})
     if(USE_LIBBFD)
         target_link_libraries(supertuxkart ${LIBBFD_LIBRARIES})
     endif()
@@ -425,6 +440,17 @@ if(UNIX AND NOT APPLE)
     endif()
 endif()
 
+if(BUILD_RECORDER)
+    target_link_libraries(supertuxkart webm ${TURBOJPEG_LIBRARY} ${VPX_LIBRARIES})
+    if(BUILD_RECORDER_WITH_SOUND)
+        if(BUILD_PULSE_WO_DL)
+            target_link_libraries(supertuxkart ${PULSEAUDIO_LIBRARIES})
+        else()
+            target_link_libraries(supertuxkart dl)
+        endif()
+    endif()
+endif()
+
 # FreeBSD does not search in /usr/local/lib, but at least Freetype is installed there :(
 if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
     SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")
diff --git a/cmake/FindOggVorbis.cmake b/cmake/FindOggVorbis.cmake
index e30056cf9..387861c42 100644
--- a/cmake/FindOggVorbis.cmake
+++ b/cmake/FindOggVorbis.cmake
@@ -27,6 +27,11 @@ if(APPLE AND NOT OGGVORBIS_VORBISFILE_LIBRARY)
     set(OGGVORBIS_VORBISFILE_LIBRARY ${OGGVORBIS_VORBIS_LIBRARY})
 endif()
 
+if(APPLE AND NOT OGGVORBIS_VORBISENC_LIBRARY)
+    # Seems to be the same on Apple systems
+    set(OGGVORBIS_VORBISENC_LIBRARY ${OGGVORBIS_VORBIS_LIBRARY})
+endif()
+
 include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(OggVorbis DEFAULT_MSG
     OGGVORBIS_OGG_INCLUDE_DIR OGGVORBIS_VORBIS_INCLUDE_DIR
diff --git a/src/graphics/irr_driver.cpp b/src/graphics/irr_driver.cpp
index b67b35fea..c88f2f034 100644
--- a/src/graphics/irr_driver.cpp
+++ b/src/graphics/irr_driver.cpp
@@ -169,7 +169,7 @@ IrrDriver::~IrrDriver()
 #endif
     delete m_wind;
     delete m_renderer;
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
     Recorder::destroyRecorder();
 #endif
 }   // ~IrrDriver
@@ -926,7 +926,7 @@ void IrrDriver::applyResolutionSettings()
     // (we're sure to update main.cpp at some point and forget this one...)
     VAOManager::getInstance()->kill();
     STKTexManager::getInstance()->kill();
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
     Recorder::destroyRecorder();
     m_recording = false;
 #endif
@@ -1894,7 +1894,7 @@ void IrrDriver::update(float dt)
     // menu.
     //if(World::getWorld() && World::getWorld()->isRacePhase())
     //    printRenderStats();
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
     if (m_recording)
         Recorder::captureFrameBufferImage();
 #endif
@@ -1903,7 +1903,7 @@ void IrrDriver::update(float dt)
 // ----------------------------------------------------------------------------
 void IrrDriver::setRecording(bool val)
 {
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
     if (!CVS->isARBPixelBufferObjectUsable())
     {
         Log::warn("irr_driver", "PBO extension missing, can't record video.");
diff --git a/src/recorder/pulseaudio_recorder.cpp b/src/recorder/pulseaudio_recorder.cpp
index b96c225b2..6abe955c7 100644
--- a/src/recorder/pulseaudio_recorder.cpp
+++ b/src/recorder/pulseaudio_recorder.cpp
@@ -15,7 +15,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) && !defined(WIN32)
+#if defined(ENABLE_REC_SOUND) && !defined(WIN32)
 
 #include "recorder/vorbis_encoder.hpp"
 #include "utils/synchronised.hpp"
@@ -23,11 +23,14 @@
 #include "utils/vs.hpp"
 
 #include <cstring>
-#include <dlfcn.h>
 #include <list>
 #include <pulse/pulseaudio.h>
 #include <string>
 
+#ifndef ENABLE_PULSE_WO_DL
+#include <dlfcn.h>
+#endif
+
 namespace Recorder
 {
     // ========================================================================
@@ -42,9 +45,10 @@ namespace Recorder
         pa_mainloop* m_loop;
         pa_context* m_context;
         pa_stream* m_stream;
-        void* m_dl_handle;
         pa_sample_spec m_sample_spec;
         std::string m_default_sink;
+#ifndef ENABLE_PULSE_WO_DL
+        void* m_dl_handle;
 
         typedef pa_stream* (*pa_stream_new_t)(pa_context*, const char*,
             const pa_sample_spec*, const pa_channel_map*);
@@ -110,6 +114,7 @@ namespace Recorder
 
         typedef void (*pa_mainloop_free_t)(pa_mainloop*);
         pa_mainloop_free_t pa_mainloop_free;
+#endif
         // --------------------------------------------------------------------
         PulseAudioData()
         {
@@ -117,6 +122,7 @@ namespace Recorder
             m_loop = NULL;
             m_context = NULL;
             m_stream = NULL;
+#ifndef ENABLE_PULSE_WO_DL
             m_dl_handle = NULL;
             pa_stream_new = NULL;
             pa_stream_connect_record = NULL;
@@ -138,8 +144,10 @@ namespace Recorder
             pa_context_disconnect = NULL;
             pa_context_unref = NULL;
             pa_mainloop_free = NULL;
+#endif
         }   // PulseAudioData
         // --------------------------------------------------------------------
+#ifndef ENABLE_PULSE_WO_DL
         bool loadPulseAudioLibrary()
         {
             m_dl_handle = dlopen("libpulse.so", RTLD_LAZY);
@@ -311,9 +319,11 @@ namespace Recorder
             }
             return true;
         }   // loadPulseAudioLibrary
+#endif
         // --------------------------------------------------------------------
         bool load()
         {
+#ifndef ENABLE_PULSE_WO_DL
             if (!loadPulseAudioLibrary())
             {
                 if (m_dl_handle != NULL)
@@ -323,6 +333,7 @@ namespace Recorder
                 }
                 return false;
             }
+#endif
             m_loop = pa_mainloop_new();
             if (m_loop == NULL)
             {
@@ -455,10 +466,12 @@ namespace Recorder
                 {
                     pa_mainloop_free(m_loop);
                 }
+#ifndef ENABLE_PULSE_WO_DL
                 if (m_dl_handle != NULL)
                 {
                     dlclose(m_dl_handle);
                 }
+#endif
             }
         }   // ~PulseAudioData
     };
diff --git a/src/recorder/pulseaudio_recorder.hpp b/src/recorder/pulseaudio_recorder.hpp
index 33b85fc51..86abbc557 100644
--- a/src/recorder/pulseaudio_recorder.hpp
+++ b/src/recorder/pulseaudio_recorder.hpp
@@ -15,14 +15,18 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) && !defined(WIN32)
+#ifndef WIN32
 
 #ifndef HEADER_PULSEAUDIO_RECORD_HPP
 #define HEADER_PULSEAUDIO_RECORD_HPP
 
 namespace Recorder
 {
+#ifdef ENABLE_REC_SOUND
     void* audioRecorder(void *obj);
+#else
+    inline void* audioRecorder(void *obj) { return NULL; }
+#endif
 };
 
 #endif
diff --git a/src/recorder/recorder_common.cpp b/src/recorder/recorder_common.cpp
index 6f48718d8..7cd80b00f 100644
--- a/src/recorder/recorder_common.cpp
+++ b/src/recorder/recorder_common.cpp
@@ -15,7 +15,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) && !defined(WIN32)
+#ifdef ENABLE_RECORDER
 
 #include "recorder/recorder_common.hpp"
 #include "config/user_config.hpp"
diff --git a/src/recorder/recorder_common.hpp b/src/recorder/recorder_common.hpp
index 2e6f5ea2b..799e35e7c 100644
--- a/src/recorder/recorder_common.hpp
+++ b/src/recorder/recorder_common.hpp
@@ -16,7 +16,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
 
 #ifndef HEADER_RECORDER_COMMON_HPP
 #define HEADER_RECORDER_COMMON_HPP
diff --git a/src/recorder/vorbis_encoder.cpp b/src/recorder/vorbis_encoder.cpp
index a8e9022ec..a238afa31 100644
--- a/src/recorder/vorbis_encoder.cpp
+++ b/src/recorder/vorbis_encoder.cpp
@@ -15,7 +15,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
 
 #include "recorder/vorbis_encoder.hpp"
 #include "recorder/recorder_common.hpp"
diff --git a/src/recorder/vorbis_encoder.hpp b/src/recorder/vorbis_encoder.hpp
index 134d37e37..6ac305536 100644
--- a/src/recorder/vorbis_encoder.hpp
+++ b/src/recorder/vorbis_encoder.hpp
@@ -15,7 +15,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
 
 #ifndef HEADER_VORBIS_ENCODE_HPP
 #define HEADER_VORBIS_ENCODE_HPP
diff --git a/src/recorder/vpx_encoder.cpp b/src/recorder/vpx_encoder.cpp
index 65edc333a..3d8cc390e 100644
--- a/src/recorder/vpx_encoder.cpp
+++ b/src/recorder/vpx_encoder.cpp
@@ -15,7 +15,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
 
 #include "recorder/vpx_encoder.hpp"
 #include "config/user_config.hpp"
diff --git a/src/recorder/vpx_encoder.hpp b/src/recorder/vpx_encoder.hpp
index 57732d6cc..74235c961 100644
--- a/src/recorder/vpx_encoder.hpp
+++ b/src/recorder/vpx_encoder.hpp
@@ -16,7 +16,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
 
 #ifndef HEADER_VPX_ENCODER_HPP
 #define HEADER_VPX_ENCODER_HPP
diff --git a/src/recorder/wasapi_recorder.cpp b/src/recorder/wasapi_recorder.cpp
index 0e75beee6..c3f652932 100644
--- a/src/recorder/wasapi_recorder.cpp
+++ b/src/recorder/wasapi_recorder.cpp
@@ -15,7 +15,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) && defined(WIN32)
+#if defined(ENABLE_REC_SOUND) && defined(WIN32)
 
 #include "recorder/vorbis_encoder.hpp"
 #include "utils/synchronised.hpp"
diff --git a/src/recorder/wasapi_recorder.hpp b/src/recorder/wasapi_recorder.hpp
index c09ddaf94..ac5bc0599 100644
--- a/src/recorder/wasapi_recorder.hpp
+++ b/src/recorder/wasapi_recorder.hpp
@@ -15,14 +15,18 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2)) && defined(WIN32)
+#ifdef WIN32
 
 #ifndef HEADER_WASAPI_RECORD_HPP
 #define HEADER_WASAPI_RECORD_HPP
 
 namespace Recorder
 {
+#ifdef ENABLE_REC_SOUND
     void* audioRecorder(void *obj);
+#else
+    inline void* audioRecorder(void *obj) { return NULL; }
+#endif
 };
 
 #endif
diff --git a/src/recorder/webm_writer.cpp b/src/recorder/webm_writer.cpp
index 0347d58de..474b262bf 100644
--- a/src/recorder/webm_writer.cpp
+++ b/src/recorder/webm_writer.cpp
@@ -15,7 +15,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
 
 #include "recorder/webm_writer.hpp"
 #include "config/user_config.hpp"
diff --git a/src/recorder/webm_writer.hpp b/src/recorder/webm_writer.hpp
index 126f38ac2..7108e3b2b 100644
--- a/src/recorder/webm_writer.hpp
+++ b/src/recorder/webm_writer.hpp
@@ -16,7 +16,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-#if !(defined(SERVER_ONLY) || defined(USE_GLES2))
+#ifdef ENABLE_RECORDER
 
 #ifndef HEADER_WEBM_WRITER_HPP
 #define HEADER_WEBM_WRITER_HPP