File tg_owt-h264-dlopen.patch of Package telegram-desktop
diff --git a/tg_owt-git20250913/CMakeLists.txt b/tg_owt-git20250913-patched/CMakeLists.txt
index e585335..1eb3989 100644
--- a/tg_owt-git20250913/CMakeLists.txt
+++ b/tg_owt-git20250913-patched/CMakeLists.txt
@@ -23,6 +23,7 @@ cmake_dependent_option(TG_OWT_USE_X11 "Use X11 for desktop capture." ON "UNIX; N
cmake_dependent_option(TG_OWT_USE_PIPEWIRE "Use pipewire for desktop capture." ON "UNIX; NOT APPLE" OFF)
cmake_dependent_option(TG_OWT_DLOPEN_PIPEWIRE "dlopen pipewire for desktop capture." ${not_packaged_build} TG_OWT_USE_PIPEWIRE OFF)
option(TG_OWT_BUILD_AUDIO_BACKENDS "Build webrtc audio backends." OFF)
+option(TG_OWT_DLOPEN_H264 "dlopen H264 for video coding." OFF)
if (BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@@ -149,7 +150,7 @@ link_openssl(tg_owt)
link_ffmpeg(tg_owt)
link_opus(tg_owt)
link_libabsl(tg_owt)
-link_libopenh264(tg_owt)
+link_libopenh264(tg_owt ${TG_OWT_DLOPEN_H264})
link_libsrtp(tg_owt)
link_libvpx(tg_owt)
link_crc32c(tg_owt)
diff --git a/tg_owt-git20250913/cmake/external.cmake b/tg_owt-git20250913-patched/cmake/external.cmake
index 0bcdf60..2b4c244 100644
--- a/tg_owt-git20250913/cmake/external.cmake
+++ b/tg_owt-git20250913-patched/cmake/external.cmake
@@ -116,8 +116,8 @@ endfunction()
# libopenh264
set(TG_OWT_OPENH264_INCLUDE_PATH "" CACHE STRING "Include path for openh264.")
-function(link_libopenh264 target_name)
- if (TG_OWT_PACKAGED_BUILD)
+function(link_libopenh264 target_name with_dlopen)
+ if (NOT TG_OWT_PACKAGED_BUILD)
find_package(PkgConfig REQUIRED)
pkg_check_modules(OPENH264 REQUIRED openh264)
target_link_libraries(${target_name} PRIVATE ${OPENH264_LINK_LIBRARIES})
@@ -132,6 +132,10 @@ function(link_libopenh264 target_name)
${TG_OWT_OPENH264_INCLUDE_PATH}
)
endif()
+ if (with_dlopen)
+ target_compile_definitions(${target_name} PRIVATE -DWEBRTC_USE_H264_DLOPEN)
+ target_sources(${target_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/modules/video_coding/codecs/h264/h264_dlopen.cc)
+ endif()
endfunction()
# libSRTP
diff --git a/tg_owt-git20250913-patched/cmake/external.cmake.orig b/tg_owt-git20250913-patched/cmake/external.cmake.orig
new file mode 100644
index 0000000..0bcdf60
--- /dev/null
+++ b/tg_owt-git20250913-patched/cmake/external.cmake.orig
@@ -0,0 +1,239 @@
+# OpenSSL
+set(TG_OWT_OPENSSL_INCLUDE_PATH "" CACHE STRING "Include path for openssl.")
+function(link_openssl target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(OpenSSL REQUIRED)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${OPENSSL_INCLUDE_DIR})
+ target_link_libraries(${target_name} PRIVATE ${OPENSSL_LIBRARIES})
+ else()
+ if (TG_OWT_OPENSSL_INCLUDE_PATH STREQUAL "")
+ message(FATAL_ERROR "You should specify 'TG_OWT_OPENSSL_INCLUDE_PATH'.")
+ endif()
+
+ target_include_directories(${target_name} SYSTEM
+ PRIVATE
+ ${TG_OWT_OPENSSL_INCLUDE_PATH}
+ )
+ endif()
+endfunction()
+
+# Opus
+set(TG_OWT_OPUS_INCLUDE_PATH "" CACHE STRING "Include path for opus.")
+function(link_opus target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(OPUS REQUIRED opus)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${OPUS_INCLUDE_DIRS})
+ target_link_libraries(${target_name} PRIVATE ${OPUS_LINK_LIBRARIES})
+ else()
+ if (TG_OWT_OPUS_INCLUDE_PATH STREQUAL "")
+ message(FATAL_ERROR "You should specify 'TG_OWT_OPUS_INCLUDE_PATH'.")
+ endif()
+
+ target_include_directories(${target_name} SYSTEM
+ PRIVATE
+ ${TG_OWT_OPUS_INCLUDE_PATH}
+ )
+ endif()
+endfunction()
+
+# FFmpeg
+set(TG_OWT_FFMPEG_INCLUDE_PATH "" CACHE STRING "Include path for ffmpeg.")
+function(link_ffmpeg target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(FFMPEG REQUIRED libavcodec libavformat libavutil libswresample libswscale)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${FFMPEG_INCLUDE_DIRS})
+ target_link_libraries(${target_name} PRIVATE ${FFMPEG_LINK_LIBRARIES})
+ else()
+ if (TG_OWT_FFMPEG_INCLUDE_PATH STREQUAL "")
+ message(FATAL_ERROR "You should specify 'TG_OWT_FFMPEG_INCLUDE_PATH'.")
+ endif()
+
+ target_include_directories(${target_name} SYSTEM
+ PRIVATE
+ ${TG_OWT_FFMPEG_INCLUDE_PATH}
+ )
+ endif()
+endfunction()
+
+# libjpeg
+set(TG_OWT_LIBJPEG_INCLUDE_PATH "" CACHE STRING "Include path for libjpeg.")
+function(link_libjpeg target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(JPEG REQUIRED)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${JPEG_INCLUDE_DIRS})
+ target_link_libraries(${target_name} PRIVATE ${JPEG_LIBRARIES})
+ else()
+ if (TG_OWT_LIBJPEG_INCLUDE_PATH STREQUAL "")
+ message(FATAL_ERROR "You should specify 'TG_OWT_LIBJPEG_INCLUDE_PATH'.")
+ endif()
+
+ target_include_directories(${target_name} SYSTEM
+ PRIVATE
+ ${TG_OWT_LIBJPEG_INCLUDE_PATH}
+ ${TG_OWT_LIBJPEG_INCLUDE_PATH}/src
+ )
+ endif()
+endfunction()
+
+# libabsl
+# HINT: System abseil should be built with -DCMAKE_CXX_STANDARD=20
+function(link_libabsl target_name)
+ set(scope PRIVATE)
+ get_target_property(target_type ${target_name} TYPE)
+ if (${target_type} STREQUAL "INTERFACE_LIBRARY")
+ set(scope INTERFACE)
+ endif()
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(absl)
+ set(absl_FOUND ${absl_FOUND} PARENT_SCOPE)
+ if (absl_FOUND)
+ target_link_libraries(${target_name}
+ ${scope}
+ absl::algorithm_container
+ absl::bind_front
+ absl::config
+ absl::core_headers
+ absl::flat_hash_map
+ absl::inlined_vector
+ absl::flags
+ absl::flags_parse
+ absl::flags_usage
+ absl::memory
+ absl::optional
+ absl::strings
+ absl::synchronization
+ absl::type_traits
+ absl::variant
+ )
+ endif()
+ endif()
+ if (NOT absl_FOUND)
+ target_link_libraries(${target_name} ${scope} tg_owt::libabsl)
+ endif()
+endfunction()
+
+# libopenh264
+set(TG_OWT_OPENH264_INCLUDE_PATH "" CACHE STRING "Include path for openh264.")
+function(link_libopenh264 target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(OPENH264 REQUIRED openh264)
+ target_link_libraries(${target_name} PRIVATE ${OPENH264_LINK_LIBRARIES})
+ target_include_directories(${target_name} SYSTEM PRIVATE ${OPENH264_INCLUDE_DIRS})
+ else()
+ if (TG_OWT_OPENH264_INCLUDE_PATH STREQUAL "")
+ message(FATAL_ERROR "You should specify 'TG_OWT_OPENH264_INCLUDE_PATH'.")
+ endif()
+
+ target_include_directories(${target_name} SYSTEM
+ PRIVATE
+ ${TG_OWT_OPENH264_INCLUDE_PATH}
+ )
+ endif()
+endfunction()
+
+# libSRTP
+function(link_libsrtp target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(PkgConfig)
+ if (PkgConfig_FOUND)
+ pkg_check_modules(SRTP libsrtp2)
+ if (SRTP_FOUND)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${SRTP_INCLUDE_DIRS})
+ target_link_libraries(${target_name} PRIVATE ${SRTP_LINK_LIBRARIES})
+ endif()
+ endif()
+ endif()
+ if (NOT SRTP_FOUND)
+ target_link_libraries(${target_name} PRIVATE tg_owt::libsrtp)
+ endif()
+endfunction()
+
+# libvpx
+set(TG_OWT_LIBVPX_INCLUDE_PATH "" CACHE STRING "Include path for libvpx.")
+function(link_libvpx target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(VPX REQUIRED vpx>=1.10.0)
+ target_link_libraries(${target_name} PRIVATE ${VPX_LINK_LIBRARIES})
+ target_include_directories(${target_name} SYSTEM PRIVATE ${VPX_INCLUDE_DIRS})
+ else()
+ if (TG_OWT_LIBVPX_INCLUDE_PATH STREQUAL "")
+ message(FATAL_ERROR "You should specify 'TG_OWT_LIBVPX_INCLUDE_PATH'.")
+ endif()
+
+ target_include_directories(${target_name} SYSTEM
+ PRIVATE
+ ${TG_OWT_LIBVPX_INCLUDE_PATH}
+ )
+ endif()
+endfunction()
+
+# crc32c
+function(link_crc32c target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(Crc32c)
+ set(Crc32c_FOUND ${Crc32c_FOUND} PARENT_SCOPE)
+ if (Crc32c_FOUND)
+ target_link_libraries(${target_name} PRIVATE Crc32c::crc32c)
+ endif()
+ endif()
+ if (NOT Crc32c_FOUND)
+ target_link_libraries(${target_name} PRIVATE tg_owt::libcrc32c)
+ endif()
+endfunction()
+
+function(link_glib target_name)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(GLIB2 REQUIRED glib-2.0 gobject-2.0 gio-2.0 gio-unix-2.0)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${GLIB2_INCLUDE_DIRS})
+ if (TG_OWT_PACKAGED_BUILD)
+ target_link_libraries(${target_name} PRIVATE ${GLIB2_LINK_LIBRARIES})
+ endif()
+endfunction()
+
+# x11
+function(link_x11 target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(X11 REQUIRED x11 xcomposite xdamage xext xfixes xrandr xrender xtst)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${X11_INCLUDE_DIRS})
+ target_link_libraries(${target_name} PRIVATE ${X11_LINK_LIBRARIES})
+ endif()
+endfunction()
+
+# PipeWire
+function(link_pipewire target_name only_include_dirs)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(PIPEWIRE REQUIRED libpipewire-0.3)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${PIPEWIRE_INCLUDE_DIRS})
+ if (NOT only_include_dirs)
+ target_link_libraries(${target_name} PRIVATE ${PIPEWIRE_LINK_LIBRARIES})
+ endif()
+endfunction()
+
+# Alsa
+function(link_libalsa target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(ALSA REQUIRED)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${ALSA_INCLUDE_DIRS})
+ endif()
+endfunction()
+
+# PulseAudio
+function(link_libpulse target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ find_package(PkgConfig REQUIRED)
+ pkg_check_modules(PULSE REQUIRED libpulse)
+ target_include_directories(${target_name} SYSTEM PRIVATE ${PULSE_INCLUDE_DIRS})
+ endif()
+endfunction()
+
+# dl
+function(link_dl target_name)
+ if (TG_OWT_PACKAGED_BUILD)
+ target_link_libraries(${target_name} PRIVATE ${CMAKE_DL_LIBS})
+ endif()
+endfunction()
diff --git a/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_dlopen.cc b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_dlopen.cc
new file mode 100644
index 0000000..3762e1d
--- /dev/null
+++ b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_dlopen.cc
@@ -0,0 +1,128 @@
+/*
+ * OpenH264 dlopen code
+ *
+ * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include <dlfcn.h>
+#include <cstddef>
+
+#include "h264_dlopen.h"
+
+/*
+ * The symbol binding makes sure we do not run into strict aliasing issues which
+ * can lead into segfaults.
+ */
+typedef int (*__oh264_WelsCreateSVCEncoder)(ISVCEncoder **);
+typedef void (*__oh264_WelsDestroySVCEncoder)(ISVCEncoder *);
+typedef int (*__oh264_WelsGetDecoderCapability)(SDecoderCapability *);
+typedef long (*__oh264_WelsCreateDecoder)(ISVCDecoder **);
+typedef void (*__oh264_WelsDestroyDecoder)(ISVCDecoder *);
+typedef OpenH264Version (*__oh264_WelsGetCodecVersion)(void);
+typedef void (*__oh264_WelsGetCodecVersionEx)(OpenH264Version *);
+
+#define OH264_SYMBOL_ENTRY(i) \
+ union { \
+ __oh264_##i f; \
+ void *obj; \
+ } _oh264_##i
+
+struct oh264_symbols {
+ OH264_SYMBOL_ENTRY(WelsCreateSVCEncoder);
+ OH264_SYMBOL_ENTRY(WelsDestroySVCEncoder);
+ OH264_SYMBOL_ENTRY(WelsGetDecoderCapability);
+ OH264_SYMBOL_ENTRY(WelsCreateDecoder);
+ OH264_SYMBOL_ENTRY(WelsDestroyDecoder);
+ OH264_SYMBOL_ENTRY(WelsGetCodecVersion);
+ OH264_SYMBOL_ENTRY(WelsGetCodecVersionEx);
+};
+
+/* Symbols are bound by loadLibOpenH264() */
+static struct oh264_symbols openh264_symbols;
+
+int oh264_WelsCreateSVCEncoder(ISVCEncoder **ppEncoder) {
+ return openh264_symbols._oh264_WelsCreateSVCEncoder.f(ppEncoder);
+}
+
+void oh264_WelsDestroySVCEncoder(ISVCEncoder *pEncoder) {
+ return openh264_symbols._oh264_WelsDestroySVCEncoder.f(pEncoder);
+}
+
+int oh264_WelsGetDecoderCapability(SDecoderCapability *pDecCapability) {
+ return openh264_symbols._oh264_WelsGetDecoderCapability.f(pDecCapability);
+}
+
+long oh264_WelsCreateDecoder(ISVCDecoder **ppDecoder) {
+ return openh264_symbols._oh264_WelsCreateDecoder.f(ppDecoder);
+}
+
+void oh264_WelsDestroyDecoder(ISVCDecoder *pDecoder) {
+ return openh264_symbols._oh264_WelsDestroyDecoder.f(pDecoder);
+}
+
+OpenH264Version oh264_WelsGetCodecVersion(void) {
+ return openh264_symbols._oh264_WelsGetCodecVersion.f();
+}
+
+void oh264_WelsGetCodecVersionEx(OpenH264Version *pVersion) {
+ openh264_symbols._oh264_WelsGetCodecVersionEx.f(pVersion);
+}
+
+static void *_oh264_bind_symbol(void *handle,
+ const char *sym_name) {
+ void *sym = NULL;
+
+ sym = dlsym(handle, sym_name);
+ if (sym == NULL) {
+ const char *err = dlerror();
+ return NULL;
+ }
+
+ return sym;
+}
+
+#define oh264_bind_symbol(handle, sym_name) \
+ if (openh264_symbols._oh264_##sym_name.obj == NULL) { \
+ openh264_symbols._oh264_##sym_name.obj = _oh264_bind_symbol(handle, #sym_name); \
+ if (openh264_symbols._oh264_##sym_name.obj == NULL) { \
+ return 1; \
+ } \
+ }
+
+int loadLibOpenH264(void) {
+ static bool initialized = false;
+ void *libopenh264 = NULL;
+ const char *err = NULL;
+
+ if (initialized) {
+ return 0;
+ }
+
+#define OPENH264_LIB "libopenh264.so.7"
+ libopenh264 = dlopen(OPENH264_LIB, RTLD_LAZY);
+ err = dlerror();
+ if (err != NULL) {
+ if (libopenh264 != NULL) {
+ dlclose(libopenh264);
+ }
+ return 1;
+ }
+
+ oh264_bind_symbol(libopenh264, WelsCreateSVCEncoder);
+ oh264_bind_symbol(libopenh264, WelsDestroySVCEncoder);
+ oh264_bind_symbol(libopenh264, WelsGetDecoderCapability);
+ oh264_bind_symbol(libopenh264, WelsCreateDecoder);
+ oh264_bind_symbol(libopenh264, WelsDestroyDecoder);
+ oh264_bind_symbol(libopenh264, WelsGetCodecVersion);
+ oh264_bind_symbol(libopenh264, WelsGetCodecVersionEx);
+
+ initialized = true;
+
+ return 0;
+}
diff --git a/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_dlopen.h b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_dlopen.h
new file mode 100644
index 0000000..25fe94e
--- /dev/null
+++ b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_dlopen.h
@@ -0,0 +1,46 @@
+/*
+ * OpenH264 dlopen code
+ *
+ * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef HAVE_LIBOPENH264_DLOPEN_H
+#define HAVE_LIBOPENH264_DLOPEN_H
+
+#ifdef WEBRTC_USE_H264_DLOPEN
+
+#include <wels/codec_api.h>
+#include <wels/codec_ver.h>
+
+int oh264_WelsCreateSVCEncoder(ISVCEncoder **ppEncoder);
+#define WelsCreateSVCEncoder oh264_WelsCreateSVCEncoder
+
+void oh264_WelsDestroySVCEncoder(ISVCEncoder *pEncoder);
+#define WelsDestroySVCEncoder oh264_WelsDestroySVCEncoder
+
+int oh264_WelsGetDecoderCapability(SDecoderCapability *pDecCapability);
+#define WelsGetDecoderCapability oh264_WelsGetDecoderCapability
+
+long oh264_WelsCreateDecoder(ISVCDecoder **ppDecoder);
+#define WelsCreateDecoder oh264_WelsCreateDecoder
+
+void oh264_WelsDestroyDecoder(ISVCDecoder *pDecoder);
+#define WelsDestroyDecoder oh264_WelsDestroyDecoder
+
+OpenH264Version oh264_WelsGetCodecVersion(void);
+#define WelsGetCodecVersion oh264_WelsGetCodecVersion
+
+void oh264_WelsGetCodecVersionEx(OpenH264Version *pVersion);
+#define WelsGetCodecVersionEx oh264_WelsGetCodecVersionEx
+
+int loadLibOpenH264(void);
+
+#endif /* WEBRTC_USE_H264_DLOPEN */
+
+#endif /* HAVE_LIBOPENH264_DLOPEN_H */
diff --git a/tg_owt-git20250913/src/modules/video_coding/codecs/h264/h264_encoder_impl.cc b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_encoder_impl.cc
index c232133..29914aa 100644
--- a/tg_owt-git20250913/src/modules/video_coding/codecs/h264/h264_encoder_impl.cc
+++ b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_encoder_impl.cc
@@ -217,6 +217,12 @@ int32_t H264EncoderImpl::InitEncode(const VideoCodec* inst,
ReportError();
return release_ret;
}
+
+ #ifdef WEBRTC_USE_H264_DLOPEN
+ if (loadLibOpenH264()) {
+ return WEBRTC_VIDEO_CODEC_ERROR;
+ }
+ #endif
int number_of_streams = SimulcastUtility::NumberOfSimulcastStreams(*inst);
bool doing_simulcast = (number_of_streams > 1);
diff --git a/tg_owt-git20250913/src/modules/video_coding/codecs/h264/h264_encoder_impl.h b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_encoder_impl.h
index 9e246b8..2deb5a2 100644
--- a/tg_owt-git20250913/src/modules/video_coding/codecs/h264/h264_encoder_impl.h
+++ b/tg_owt-git20250913-patched/src/modules/video_coding/codecs/h264/h264_encoder_impl.h
@@ -30,7 +30,12 @@
#include "modules/video_coding/codecs/h264/include/h264.h"
#include "modules/video_coding/svc/scalable_video_controller.h"
#include "modules/video_coding/utility/quality_scaler.h"
+
+#ifdef WEBRTC_USE_H264_DLOPEN
+#include "h264_dlopen.h"
+#else
#include <wels/codec_app_def.h>
+#endif
class ISVCEncoder;