Blob Blame History Raw
From 828d03c76b3b108019b1cf66062b0e9679012207 Mon Sep 17 00:00:00 2001
From: Jan Grulich <grulja@gmail.com>
Date: Mon, 23 Jan 2023 11:04:00 +0100
Subject: [Blink] mediarecorder: allow to dlopen openh264


diff --git a/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/BUILD.gn b/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/BUILD.gn
index cf6de49bef6b2..61e8879d6b97f 100644
--- a/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/BUILD.gn
+++ b/src/3rdparty/chromium/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/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc b/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc
index c9aee6f3a..0f4ef593d 100644
--- a/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc
+++ b/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.cc
@@ -24,10 +24,22 @@
 #include "third_party/openh264/src/codec/api/svc/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>
@@ -98,6 +110,13 @@ H264Encoder::H264Encoder(
       codec_profile_(codec_profile) {
   DCHECK(encoding_thread_);
   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
 }

 H264Encoder::~H264Encoder() {
@@ -180,6 +199,14 @@ void H264Encoder::EncodeOnEncodingTaskRunner(
 bool H264Encoder::ConfigureEncoderOnEncodingTaskRunner(const gfx::Size& size) {
   TRACE_EVENT0("media", "H264Encoder::ConfigureEncoderOnEncodingTaskRunner");
   DCHECK(encoding_task_runner_->RunsTasksInCurrentSequence());
+
+#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/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h b/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h
index e7aa2c338..79e902f67 100644
--- a/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h
+++ b/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/h264_encoder.h
@@ -62,6 +62,8 @@ class MODULES_EXPORT H264Encoder final : public VideoTrackRecorder::Encoder {
   // The |VideoFrame::timestamp()| of the first received frame. Only used on
   // VideoTrackRecorder::Encoder::encoding_thread_.
   base::TimeTicks first_frame_timestamp_;
+
+  bool openh264_dlopened_ = false;
 };

 }  // namespace blink

diff --git a/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/openh264.sigs b/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/openh264.sigs
new file mode 100644
index 0000000000000..4924f8e9a17de
--- /dev/null
+++ b/src/3rdparty/chromium/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/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/openh264_stub_header.fragment b/src/3rdparty/chromium/third_party/blink/renderer/modules/mediarecorder/openh264_stub_header.fragment
new file mode 100644
index 0000000000000..9bc0a7cbee038
--- /dev/null
+++ b/src/3rdparty/chromium/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/svc/codec_api.h"
+#include "third_party/openh264/src/codec/api/svc/codec_app_def.h"
+#include "third_party/openh264/src/codec/api/svc/codec_def.h"
+#include "third_party/openh264/src/codec/api/svc/codec_ver.h"
+
+}