ui/obs-browser-widget: Integrate OBS Browser Source as a Qt Widget

Provides us with an easy and clean way to use Browser Sources as a Qt enabled widget.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2023-03-09 15:16:50 +01:00
parent 54859e9f14
commit dadd56b31d
4 changed files with 153 additions and 0 deletions

View file

@ -1494,6 +1494,8 @@ if(T_CHECK)
"source/ui/ui-about.cpp" "source/ui/ui-about.cpp"
"source/ui/ui-about-entry.hpp" "source/ui/ui-about-entry.hpp"
"source/ui/ui-about-entry.cpp" "source/ui/ui-about-entry.cpp"
"source/ui/ui-obs-browser-widget.hpp"
"source/ui/ui-obs-browser-widget.cpp"
) )
list(APPEND PROJECT_INCLUDE_DIRS list(APPEND PROJECT_INCLUDE_DIRS
"source/ui" "source/ui"

View file

@ -0,0 +1,93 @@
// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END
#include "ui-obs-browser-widget.hpp"
#include "plugin.hpp"
#include "warning-disable.hpp"
#include <mutex>
#include <../plugins/obs-browser/panel/browser-panel.hpp>
#include "warning-enable.hpp"
streamfx::ui::obs_browser_cef::obs_browser_cef()
{
// Load the "obs-browser" module.
_module = util::library::load(obs_get_module("obs-browser"));
auto fn = reinterpret_cast<QCef* (*)(void)>(_module->load_symbol("obs_browser_create_qcef"));
if (!fn) {
throw std::runtime_error("Unable to create Browser Panel.");
}
// Create a QCef instance and initialize it.
_cef = fn();
if (!_cef) {
throw std::runtime_error("Unable to initialize for CEF-based Browser Panel.");
}
reinterpret_cast<QCef*>(_cef)->init_browser();
reinterpret_cast<QCef*>(_cef)->wait_for_browser_init();
// Create a generic Cookie manager for widgets.
_cookie =
reinterpret_cast<QCef*>(_cef)->create_cookie_manager(streamfx::config_file_path("cookies").u8string(), false);
}
streamfx::ui::obs_browser_cef::~obs_browser_cef()
{
delete reinterpret_cast<QCefCookieManager*>(_cookie);
delete reinterpret_cast<QCef*>(_cef);
}
void* streamfx::ui::obs_browser_cef::cef()
{
return _cef;
}
void* streamfx::ui::obs_browser_cef::cookie_manager()
{
return _cookie;
}
std::shared_ptr<streamfx::ui::obs_browser_cef> streamfx::ui::obs_browser_cef::instance()
{
static std::weak_ptr<obs_browser_cef> ptr;
static std::mutex lock;
std::lock_guard<decltype(lock)> lg(lock);
if (!ptr.expired()) {
return ptr.lock();
}
std::shared_ptr<obs_browser_cef> sintance{new obs_browser_cef()};
ptr = sintance;
return sintance;
}
streamfx::ui::obs_browser_widget::obs_browser_widget(QUrl url, QWidget* parent) : QWidget(parent)
{
_cef = obs_browser_cef::instance();
_widget = reinterpret_cast<QCef*>(_cef->cef())
->create_widget(this, url.toString().toStdString(),
reinterpret_cast<QCefCookieManager*>(_cef->cookie_manager()));
if (!_widget) {
throw std::runtime_error("Failed to create QCefWidget.");
}
// Add a proper layout.
_layout = new QHBoxLayout();
_layout->setContentsMargins(0, 0, 0, 0);
_layout->setSpacing(0);
this->setLayout(_layout);
_layout->addWidget(_widget);
// Disable all popups.
dynamic_cast<QCefWidget*>(_widget)->allowAllPopups(false);
}
streamfx::ui::obs_browser_widget::~obs_browser_widget() {}
void streamfx::ui::obs_browser_widget::set_url(QUrl url)
{
dynamic_cast<QCefWidget*>(_widget)->setURL(url.toString().toStdString());
}

View file

@ -0,0 +1,49 @@
// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END
#pragma once
#include "util/util-library.hpp"
#include "warning-disable.hpp"
#include <QHBoxLayout>
#include <QSharedPointer>
#include <QUrl>
#include <QWidget>
#include "warning-enable.hpp"
namespace streamfx::ui {
class obs_browser_cef {
std::shared_ptr<::streamfx::util::library> _module;
void* _cef;
void* _cookie;
private:
obs_browser_cef();
public:
~obs_browser_cef();
void* cef();
void* cookie_manager();
public: // Singleton
static std::shared_ptr<obs_browser_cef> instance();
};
class obs_browser_widget : public QWidget {
Q_OBJECT
std::shared_ptr<obs_browser_cef> _cef;
QWidget* _widget;
QHBoxLayout* _layout;
public:
obs_browser_widget(QUrl url, QWidget* parent = nullptr);
virtual ~obs_browser_widget();
void set_url(QUrl url);
};
} // namespace streamfx::ui

View file

@ -9,6 +9,7 @@
#include "configuration.hpp" #include "configuration.hpp"
#include "obs/obs-tools.hpp" #include "obs/obs-tools.hpp"
#include "plugin.hpp" #include "plugin.hpp"
#include "ui/ui-obs-browser-widget.hpp"
#include "warning-disable.hpp" #include "warning-disable.hpp"
#include <string_view> #include <string_view>
@ -37,6 +38,8 @@ constexpr std::string_view _url_discord = "https://s.xaymar.com/streamfx-dc";
constexpr std::string_view _url_twitter = "https://s.xaymar.com/streamfx-tw"; constexpr std::string_view _url_twitter = "https://s.xaymar.com/streamfx-tw";
constexpr std::string_view _url_youtube = "https://s.xaymar.com/streamfx-yt"; constexpr std::string_view _url_youtube = "https://s.xaymar.com/streamfx-yt";
static std::shared_ptr<streamfx::ui::obs_browser_cef> _obs_browser_cef;
inline void qt_init_resource() inline void qt_init_resource()
{ {
Q_INIT_RESOURCE(streamfx); Q_INIT_RESOURCE(streamfx);
@ -107,6 +110,9 @@ void streamfx::ui::handler::on_obs_loaded()
_translator = new streamfx::ui::translator(this); _translator = new streamfx::ui::translator(this);
QCoreApplication::installTranslator(_translator); QCoreApplication::installTranslator(_translator);
// Pre-load CEF.
_obs_browser_cef = streamfx::ui::obs_browser_cef::instance();
// Create the 'About StreamFX' dialog. // Create the 'About StreamFX' dialog.
_about_dialog = new streamfx::ui::about(); _about_dialog = new streamfx::ui::about();
@ -201,6 +207,9 @@ void streamfx::ui::handler::on_obs_loaded()
void streamfx::ui::handler::on_obs_exit() void streamfx::ui::handler::on_obs_exit()
{ {
// Release CEF
_obs_browser_cef.reset();
// Remove translator. // Remove translator.
QCoreApplication::removeTranslator(_translator); QCoreApplication::removeTranslator(_translator);