From a19fe8c2f6b9cdda194ca8d51c025389067a068a Mon Sep 17 00:00:00 2001
From: Jan Grulich <grulja@gmail.com>
Date: Tue, 7 Feb 2023 14:05:29 +0100
Subject: Allow to dlopen openh264
diff --git a/media/BUILD.gn b/media/BUILD.gn
index 3da738354446b..e75b92f2d3254 100644
--- a/media/BUILD.gn
+++ b/media/BUILD.gn
@@ -91,6 +91,9 @@ config("media_config") {
if (use_cras) {
defines += [ "USE_CRAS" ]
}
+ if (media_use_openh264 && media_dlopen_openh264) {
+ defines += [ "DLOPEN_OPENH264" ]
+ }
}
# Internal grouping of the configs necessary to support sub-folders having their
diff --git a/media/media_options.gni b/media/media_options.gni
index 6418ab92fd648..0d7b009076a06 100644
--- a/media/media_options.gni
+++ b/media/media_options.gni
@@ -56,6 +56,10 @@ declare_args() {
media_use_openh264 = false
}
+ # Allow to use OpenH264 on systems where OpenH264 cannot be installed by
+ # default due to licensing, but can be installed later from other sources.
+ media_dlopen_openh264 = false
+
# Override to dynamically link the cras (ChromeOS audio) library.
use_cras = is_chromeos_device
diff --git a/media/video/BUILD.gn b/media/video/BUILD.gn
index fc45b5c1c4ee8..ec0c13eba3e08 100644
--- a/media/video/BUILD.gn
+++ b/media/video/BUILD.gn
@@ -3,6 +3,18 @@
# found in the LICENSE file.
import("//media/media_options.gni")
+import("//tools/generate_stubs/rules.gni")
+
+if (media_use_openh264 && media_dlopen_openh264) {
+ # When OpenH264 is not directly linked, use stubs to allow for dlopening of
+ # the binary.
+ generate_stubs("openh264_stubs") {
+ extra_header = "openh264_stub_header.fragment"
+ output_name = "openh264_stubs"
+ sigs = [ "openh264.sigs" ]
+ deps = [ "//base" ]
+ }
+}
source_set("video") {
# Do not expand the visibility here without double-checking with OWNERS, this
@@ -102,7 +114,12 @@ source_set("video") {
"openh264_video_encoder.cc",
"openh264_video_encoder.h",
]
- deps += [ "//third_party/openh264:encoder" ]
+
+ if (media_dlopen_openh264) {
+ deps += [ ":openh264_stubs" ]
+ } else {
+ deps += [ "//third_party/openh264:encoder" ]
+ }
}
}
diff --git a/media/video/openh264.sigs b/media/video/openh264.sigs
new file mode 100644
index 0000000000000..82f6b43619644
--- /dev/null
+++ b/media/video/openh264.sigs
@@ -0,0 +1,9 @@
+// Copyright 2023 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+//------------------------------------------------
+// Functions from OpenH264.
+//------------------------------------------------
+int WelsCreateSVCEncoder(ISVCEncoder **ppEncoder);
+void WelsDestroySVCEncoder(ISVCEncoder *pEncoder);
diff --git a/media/video/openh264_stub_header.fragment b/media/video/openh264_stub_header.fragment
new file mode 100644
index 0000000000000..937dffc847b01
--- /dev/null
+++ b/media/video/openh264_stub_header.fragment
@@ -0,0 +1,8 @@
+// The extra include header needed in the generated stub file for defining
+// various OpenH264 types.
+
+extern "C" {
+
+#include "third_party/openh264/src/codec/api/wels/codec_api.h"
+
+}
diff --git a/media/video/openh264_video_encoder.cc b/media/video/openh264_video_encoder.cc
index 27d6ca96524b1..c2ff441563fa6 100644
--- a/media/video/openh264_video_encoder.cc
+++ b/media/video/openh264_video_encoder.cc
@@ -7,6 +7,7 @@
#include <algorithm>
#include <limits>
+#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/system/sys_info.h"
@@ -17,10 +18,23 @@
#include "media/base/video_util.h"
#include "media/video/video_encoder_info.h"
+#if defined(DLOPEN_OPENH264)
+#include "media/video/openh264_stubs.h"
+#endif // defined(DLOPEN_OPENH264)
+
namespace media {
namespace {
+#if defined(DLOPEN_OPENH264)
+using media_video::InitializeStubs;
+using media_video::kModuleOpenh264;
+using media_video::StubPathMap;
+
+static const base::FilePath::CharType kOpenH264Lib[] = FILE_PATH_LITERAL("libopenh264.so.7");
+#endif // defined(DLOPEN_OPENH264)
+
+
void SetUpOpenH264Params(const VideoEncoder::Options& options,
const VideoColorSpace& itu_cs,
SEncParamExt* params) {
@@ -147,6 +161,18 @@ void OpenH264VideoEncoder::Initialize(VideoCodecProfile profile,
return;
}
+#if defined(DLOPEN_OPENH264)
+ StubPathMap paths;
+ paths[kModuleOpenh264].push_back(kOpenH264Lib);
+
+ if (!InitializeStubs(paths)) {
+ std::move(done_cb).Run(
+ EncoderStatus(EncoderStatus::Codes::kEncoderInitializationError,
+ "Failed on loading the openh264 library and symbols,"));
+ return;
+ }
+#endif // defined(DLOPEN_OPENH264)
+
ISVCEncoder* raw_codec = nullptr;
if (WelsCreateSVCEncoder(&raw_codec) != 0) {
std::move(done_cb).Run(
diff --git a/sandbox/policy/linux/bpf_gpu_policy_linux.cc b/sandbox/policy/linux/bpf_gpu_policy_linux.cc
index 35ccbb7a7f82b..dd6b990d2e10c 100644
--- a/sandbox/policy/linux/bpf_gpu_policy_linux.cc
+++ b/sandbox/policy/linux/bpf_gpu_policy_linux.cc
@@ -41,6 +41,7 @@ GpuProcessPolicy::~GpuProcessPolicy() {}
// Main policy for x86_64/i386. Extended by CrosArmGpuProcessPolicy.
ResultExpr GpuProcessPolicy::EvaluateSyscall(int sysno) const {
+ VLOG(1) << "GpuProcessPolicy::EvaluateSyscall";
// Many GPU drivers need to dlopen additional libraries (see the file
// permissions in gpu_sandbox_hook_linux.cc that end in .so).
if (SyscallSets::IsDlopen(sysno)) {
diff --git a/third_party/blink/renderer/modules/mediarecorder/BUILD.gn b/third_party/blink/renderer/modules/mediarecorder/BUILD.gn
index cf6de49bef6b2..61e8879d6b97f 100644
--- a/third_party/blink/renderer/modules/mediarecorder/BUILD.gn
+++ b/third_party/blink/renderer/modules/mediarecorder/BUILD.gn
@@ -5,12 +5,23 @@
import("//build/buildflag_header.gni")
import("//third_party/blink/renderer/modules/modules.gni")
import("//third_party/webrtc/webrtc.gni")
+import("//tools/generate_stubs/rules.gni")
buildflag_header("buildflags") {
header = "buildflags.h"
flags = [ "RTC_USE_H264=$rtc_use_h264" ]
}
+if (rtc_use_h264 && rtc_dlopen_openh264) {
+ # When OpenH264 is not directly linked, use stubs to allow for dlopening of
+ # the binary.
+ generate_stubs("openh264_stubs") {
+ extra_header = "openh264_stub_header.fragment"
+ output_name = "openh264_stubs"
+ sigs = [ "openh264.sigs" ]
+ }
+}
+
blink_modules_sources("mediarecorder") {
sources = [
"audio_track_encoder.cc",
@@ -56,6 +67,11 @@ blink_modules_sources("mediarecorder") {
"h264_encoder.h",
]
- deps += [ "//third_party/openh264:encoder" ]
+ if (rtc_dlopen_openh264) {
+ defines = [ "BLINK_DLOPEN_OPENH264" ]
+ deps += [ ":openh264_stubs" ]
+ } else {
+ deps += [ "//third_party/openh264:encoder" ]
+ }
}
}
diff --git a/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc b/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc
index 0aac5f9eba327..3ec1b4c8c4e49 100644
--- a/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc
+++ b/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc
@@ -23,9 +23,21 @@
#include "third_party/openh264/src/codec/api/wels/codec_def.h"
#include "ui/gfx/geometry/size.h"
+#if defined(BLINK_DLOPEN_OPENH264)
+#include "third_party/blink/renderer/modules/mediarecorder/openh264_stubs.h"
+#endif // defined(BLINK_DLOPEN_OPENH264)
+
namespace blink {
namespace {
+#if defined(BLINK_DLOPEN_OPENH264)
+using third_party_blink_renderer_modules_mediarecorder::InitializeStubs;
+using third_party_blink_renderer_modules_mediarecorder::kModuleOpenh264;
+using third_party_blink_renderer_modules_mediarecorder::StubPathMap;
+
+static constexpr char kOpenH264Lib[] = "libopenh264.so.7";
+#endif
+
absl::optional<EProfileIdc> ToOpenH264Profile(
media::VideoCodecProfile profile) {
static const HashMap<media::VideoCodecProfile, EProfileIdc>
@@ -86,6 +98,13 @@ H264Encoder::H264Encoder(
: Encoder(on_encoded_video_cb, bits_per_second),
codec_profile_(codec_profile) {
DCHECK_EQ(codec_profile_.codec_id, VideoTrackRecorder::CodecId::kH264);
+
+#if defined(BLINK_DLOPEN_OPENH264)
+ StubPathMap paths;
+ paths[kModuleOpenh264].push_back(kOpenH264Lib);
+
+ openh264_dlopened_ = InitializeStubs(paths);
+#endif
}
// Needs to be defined here to combat a Windows linking issue.
@@ -167,6 +186,14 @@ void H264Encoder::EncodeFrame(scoped_refptr<media::VideoFrame> frame,
bool H264Encoder::ConfigureEncoder(const gfx::Size& size) {
TRACE_EVENT0("media", "H264Encoder::ConfigureEncoder");
+
+#if defined(BLINK_DLOPEN_OPENH264)
+ if (!openh264_dlopened_) {
+ NOTREACHED() << "Failed to dlopen openh264";
+ return false;
+ }
+#endif
+
ISVCEncoder* temp_encoder = nullptr;
if (WelsCreateSVCEncoder(&temp_encoder) != 0) {
NOTREACHED() << "Failed to create OpenH264 encoder";
diff --git a/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h b/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h
index 1d9d7bc5a3a3d..77494d2044ef4 100644
--- a/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h
+++ b/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h
@@ -55,6 +55,8 @@ class MODULES_EXPORT H264Encoder final : public VideoTrackRecorder::Encoder {
gfx::Size configured_size_;
ScopedISVCEncoderPtr openh264_encoder_;
+ bool openh264_dlopened_ = false;
+
// The |VideoFrame::timestamp()| of the first received frame.
base::TimeTicks first_frame_timestamp_;
base::WeakPtrFactory<H264Encoder> weak_factory_{this};
diff --git a/third_party/blink/renderer/modules/mediarecorder/openh264.sigs b/third_party/blink/renderer/modules/mediarecorder/openh264.sigs
new file mode 100644
index 0000000000000..4924f8e9a17de
--- /dev/null
+++ b/third_party/blink/renderer/modules/mediarecorder/openh264.sigs
@@ -0,0 +1,14 @@
+// Copyright 2022 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.
+
+//------------------------------------------------
+// Functions from OpenH264.
+//------------------------------------------------
+int WelsCreateSVCEncoder(ISVCEncoder **ppEncoder);
+void WelsDestroySVCEncoder(ISVCEncoder *pEncoder);
+int WelsGetDecoderCapability(SDecoderCapability *pDecCapability);
+long WelsCreateDecoder(ISVCDecoder **ppDecoder);
+void WelsDestroyDecoder(ISVCDecoder *pDecoder);
+OpenH264Version WelsGetCodecVersion(void);
+void WelsGetCodecVersionEx(OpenH264Version *pVersion);
diff --git a/third_party/blink/renderer/modules/mediarecorder/openh264_stub_header.fragment b/third_party/blink/renderer/modules/mediarecorder/openh264_stub_header.fragment
new file mode 100644
index 0000000000000..9bc0a7cbee038
--- /dev/null
+++ b/third_party/blink/renderer/modules/mediarecorder/openh264_stub_header.fragment
@@ -0,0 +1,11 @@
+// The extra include header needed in the generated stub file for defining
+// various OpenH264 types.
+
+extern "C" {
+
+#include "third_party/openh264/src/codec/api/wels/codec_api.h"
+#include "third_party/openh264/src/codec/api/wels/codec_app_def.h"
+#include "third_party/openh264/src/codec/api/wels/codec_def.h"
+#include "third_party/openh264/src/codec/api/wels/codec_ver.h"
+
+}
diff --git a/third_party/blink/renderer/modules/webcodecs/BUILD.gn b/third_party/blink/renderer/modules/webcodecs/BUILD.gn
index 70f30624ddcc5..659161b295747 100644
--- a/third_party/blink/renderer/modules/webcodecs/BUILD.gn
+++ b/third_party/blink/renderer/modules/webcodecs/BUILD.gn
@@ -109,9 +109,6 @@ blink_modules_sources("webcodecs") {
if (media_use_libvpx) {
deps += [ "//third_party/libvpx" ]
}
- if (media_use_openh264) {
- deps += [ "//third_party/openh264:encoder" ]
- }
if (is_fuchsia) {
deps += [ "//media/fuchsia/video" ]
}