1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
|
/******************************************************************************
Copyright (C) 2014 by John R. Bradley <jrb@turrettech.com>
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <obs-frontend-api.h>
#include <util/threading.h>
#include <util/platform.h>
#include <util/util.hpp>
#include <util/dstr.hpp>
#include <obs-module.h>
#include <obs.hpp>
#include <functional>
#include <sstream>
#include <thread>
#include <mutex>
#include <nlohmann/json.hpp>
#include "obs-browser-source.hpp"
#include "browser-scheme.hpp"
#include "browser-app.hpp"
#include "browser-version.h"
#include "obs-websocket-api/obs-websocket-api.h"
#include "cef-headers.hpp"
#ifdef _WIN32
#include <util/windows/ComPtr.hpp>
#include <dxgi.h>
#include <dxgi1_2.h>
#include <d3d11.h>
#else
#include "signal-restore.hpp"
#endif
#ifdef ENABLE_BROWSER_QT_LOOP
#include <QApplication>
#include <QThread>
#endif
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("obs-browser", "en-US")
MODULE_EXPORT const char *obs_module_description(void)
{
return "CEF-based web browser source & panels";
}
using namespace std;
static thread manager_thread;
static bool manager_initialized = false;
os_event_t *cef_started_event = nullptr;
#if defined(_WIN32)
static int adapterCount = 0;
#endif
static std::wstring deviceId;
bool hwaccel = false;
/* ========================================================================= */
#ifdef ENABLE_BROWSER_QT_LOOP
extern MessageObject messageObject;
#endif
class BrowserTask : public CefTask {
public:
std::function<void()> task;
inline BrowserTask(std::function<void()> task_) : task(task_) {}
virtual void Execute() override
{
#ifdef ENABLE_BROWSER_QT_LOOP
/* you have to put the tasks on the Qt event queue after this
* call otherwise the CEF message pump may stop functioning
* correctly, it's only supposed to take 10ms max */
QMetaObject::invokeMethod(&messageObject, "ExecuteTask",
Qt::QueuedConnection,
Q_ARG(MessageTask, task));
#else
task();
#endif
}
IMPLEMENT_REFCOUNTING(BrowserTask);
};
bool QueueCEFTask(std::function<void()> task)
{
return CefPostTask(TID_UI,
CefRefPtr<BrowserTask>(new BrowserTask(task)));
}
/* ========================================================================= */
static const char *default_css = "\
body { \
background-color: rgba(0, 0, 0, 0); \
margin: 0px auto; \
overflow: hidden; \
}";
static void browser_source_get_defaults(obs_data_t *settings)
{
obs_data_set_default_string(settings, "url",
"https://obsproject.com/browser-source");
obs_data_set_default_int(settings, "width", 800);
obs_data_set_default_int(settings, "height", 600);
obs_data_set_default_int(settings, "fps", 30);
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
obs_data_set_default_bool(settings, "fps_custom", false);
#else
obs_data_set_default_bool(settings, "fps_custom", true);
#endif
obs_data_set_default_bool(settings, "shutdown", false);
obs_data_set_default_bool(settings, "restart_when_active", false);
obs_data_set_default_int(settings, "webpage_control_level",
(int)DEFAULT_CONTROL_LEVEL);
obs_data_set_default_string(settings, "css", default_css);
obs_data_set_default_bool(settings, "reroute_audio", false);
}
static bool is_local_file_modified(obs_properties_t *props, obs_property_t *,
obs_data_t *settings)
{
bool enabled = obs_data_get_bool(settings, "is_local_file");
obs_property_t *url = obs_properties_get(props, "url");
obs_property_t *local_file = obs_properties_get(props, "local_file");
obs_property_set_visible(url, !enabled);
obs_property_set_visible(local_file, enabled);
return true;
}
static bool is_fps_custom(obs_properties_t *props, obs_property_t *,
obs_data_t *settings)
{
bool enabled = obs_data_get_bool(settings, "fps_custom");
obs_property_t *fps = obs_properties_get(props, "fps");
obs_property_set_visible(fps, enabled);
return true;
}
static obs_properties_t *browser_source_get_properties(void *data)
{
obs_properties_t *props = obs_properties_create();
BrowserSource *bs = static_cast<BrowserSource *>(data);
DStr path;
obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
obs_property_t *prop = obs_properties_add_bool(
props, "is_local_file", obs_module_text("LocalFile"));
if (bs && !bs->url.empty()) {
const char *slash;
dstr_copy(path, bs->url.c_str());
dstr_replace(path, "\\", "/");
slash = strrchr(path->array, '/');
if (slash)
dstr_resize(path, slash - path->array + 1);
}
obs_property_set_modified_callback(prop, is_local_file_modified);
obs_properties_add_path(props, "local_file",
obs_module_text("LocalFile"), OBS_PATH_FILE,
"*.*", path->array);
obs_properties_add_text(props, "url", obs_module_text("URL"),
OBS_TEXT_DEFAULT);
obs_properties_add_int(props, "width", obs_module_text("Width"), 1,
8192, 1);
obs_properties_add_int(props, "height", obs_module_text("Height"), 1,
8192, 1);
obs_properties_add_bool(props, "reroute_audio",
obs_module_text("RerouteAudio"));
obs_property_t *fps_set = obs_properties_add_bool(
props, "fps_custom", obs_module_text("CustomFrameRate"));
obs_property_set_modified_callback(fps_set, is_fps_custom);
#ifndef ENABLE_BROWSER_SHARED_TEXTURE
obs_property_set_enabled(fps_set, false);
#endif
obs_properties_add_int(props, "fps", obs_module_text("FPS"), 1, 60, 1);
obs_property_t *p = obs_properties_add_text(
props, "css", obs_module_text("CSS"), OBS_TEXT_MULTILINE);
obs_property_text_set_monospace(p, true);
obs_properties_add_bool(props, "shutdown",
obs_module_text("ShutdownSourceNotVisible"));
obs_properties_add_bool(props, "restart_when_active",
obs_module_text("RefreshBrowserActive"));
obs_property_t *controlLevel = obs_properties_add_list(
props, "webpage_control_level",
obs_module_text("WebpageControlLevel"), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_INT);
obs_property_list_add_int(
controlLevel, obs_module_text("WebpageControlLevel.Level.None"),
(int)ControlLevel::None);
obs_property_list_add_int(
controlLevel,
obs_module_text("WebpageControlLevel.Level.ReadObs"),
(int)ControlLevel::ReadObs);
obs_property_list_add_int(
controlLevel,
obs_module_text("WebpageControlLevel.Level.ReadUser"),
(int)ControlLevel::ReadUser);
obs_property_list_add_int(
controlLevel,
obs_module_text("WebpageControlLevel.Level.Basic"),
(int)ControlLevel::Basic);
obs_property_list_add_int(
controlLevel,
obs_module_text("WebpageControlLevel.Level.Advanced"),
(int)ControlLevel::Advanced);
obs_property_list_add_int(
controlLevel, obs_module_text("WebpageControlLevel.Level.All"),
(int)ControlLevel::All);
obs_properties_add_button(
props, "refreshnocache", obs_module_text("RefreshNoCache"),
[](obs_properties_t *, obs_property_t *, void *data) {
static_cast<BrowserSource *>(data)->Refresh();
return false;
});
return props;
}
static void missing_file_callback(void *src, const char *new_path, void *data)
{
BrowserSource *bs = static_cast<BrowserSource *>(src);
if (bs) {
obs_source_t *source = bs->source;
OBSDataAutoRelease settings = obs_source_get_settings(source);
obs_data_set_string(settings, "local_file", new_path);
obs_source_update(source, settings);
}
UNUSED_PARAMETER(data);
}
static obs_missing_files_t *browser_source_missingfiles(void *data)
{
BrowserSource *bs = static_cast<BrowserSource *>(data);
obs_missing_files_t *files = obs_missing_files_create();
if (bs) {
obs_source_t *source = bs->source;
OBSDataAutoRelease settings = obs_source_get_settings(source);
bool enabled = obs_data_get_bool(settings, "is_local_file");
const char *path = obs_data_get_string(settings, "local_file");
if (enabled && strcmp(path, "") != 0) {
if (!os_file_exists(path)) {
obs_missing_file_t *file =
obs_missing_file_create(
path, missing_file_callback,
OBS_MISSING_FILE_SOURCE,
bs->source, NULL);
obs_missing_files_add_file(files, file);
}
}
}
return files;
}
static CefRefPtr<BrowserApp> app;
static void BrowserInit(void)
{
string path = obs_get_module_binary_path(obs_current_module());
path = path.substr(0, path.find_last_of('/') + 1);
path += "//obs-browser-page";
#ifdef _WIN32
path += ".exe";
CefMainArgs args;
#else
/* On non-windows platforms, ie macOS, we'll want to pass thru flags to
* CEF */
struct obs_cmdline_args cmdline_args = obs_get_cmdline_args();
CefMainArgs args(cmdline_args.argc, cmdline_args.argv);
#endif
BPtr<char> conf_path = obs_module_config_path("");
os_mkdir(conf_path);
CefSettings settings;
settings.log_severity = LOGSEVERITY_DISABLE;
BPtr<char> log_path = obs_module_config_path("debug.log");
BPtr<char> log_path_abs = os_get_abs_path_ptr(log_path);
CefString(&settings.log_file) = log_path_abs;
settings.windowless_rendering_enabled = true;
settings.no_sandbox = true;
uint32_t obs_ver = obs_get_version();
uint32_t obs_maj = obs_ver >> 24;
uint32_t obs_min = (obs_ver >> 16) & 0xFF;
uint32_t obs_pat = obs_ver & 0xFFFF;
/* This allows servers the ability to determine that browser panels and
* browser sources are coming from OBS. */
std::stringstream prod_ver;
prod_ver << "Chrome/";
prod_ver << std::to_string(cef_version_info(4)) << "."
<< std::to_string(cef_version_info(5)) << "."
<< std::to_string(cef_version_info(6)) << "."
<< std::to_string(cef_version_info(7));
prod_ver << " OBS/";
prod_ver << std::to_string(obs_maj) << "." << std::to_string(obs_min)
<< "." << std::to_string(obs_pat);
#if CHROME_VERSION_BUILD >= 4472
CefString(&settings.user_agent_product) = prod_ver.str();
#else
CefString(&settings.product_version) = prod_ver.str();
#endif
#ifdef ENABLE_BROWSER_QT_LOOP
settings.external_message_pump = true;
settings.multi_threaded_message_loop = false;
#endif
#if !defined(_WIN32) && !defined(__APPLE__)
// Override locale path from OBS binary path to plugin binary path
string locales = obs_get_module_binary_path(obs_current_module());
locales = locales.substr(0, locales.find_last_of('/') + 1);
locales += "locales";
BPtr<char> abs_locales = os_get_abs_path_ptr(locales.c_str());
CefString(&settings.locales_dir_path) = abs_locales;
#endif
std::string obs_locale = obs_get_locale();
std::string accepted_languages;
if (obs_locale != "en-US") {
accepted_languages = obs_locale;
accepted_languages += ",";
accepted_languages += "en-US,en";
} else {
accepted_languages = "en-US,en";
}
BPtr<char> conf_path_abs = os_get_abs_path_ptr(conf_path);
CefString(&settings.locale) = obs_get_locale();
CefString(&settings.accept_language_list) = accepted_languages;
settings.persist_user_preferences = 1;
CefString(&settings.cache_path) = conf_path_abs;
#if !defined(__APPLE__) || defined(ENABLE_BROWSER_LEGACY)
char *abs_path = os_get_abs_path_ptr(path.c_str());
CefString(&settings.browser_subprocess_path) = abs_path;
bfree(abs_path);
#endif
bool tex_sharing_avail = false;
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
if (hwaccel) {
obs_enter_graphics();
hwaccel = tex_sharing_avail = gs_shared_texture_available();
obs_leave_graphics();
}
#endif
app = new BrowserApp(tex_sharing_avail);
#ifdef _WIN32
CefExecuteProcess(args, app, nullptr);
#endif
#if !defined(_WIN32)
BackupSignalHandlers();
CefInitialize(args, settings, app, nullptr);
RestoreSignalHandlers();
#elif (CHROME_VERSION_BUILD > 3770)
CefInitialize(args, settings, app, nullptr);
#else
/* Massive (but amazing) hack to prevent chromium from modifying our
* process tokens and permissions, which caused us problems with winrt,
* used with window capture. Note, the structure internally is just
* two pointers normally. If it causes problems with future versions
* we'll just switch back to the static library but I doubt we'll need
* to. */
uintptr_t zeroed_memory_lol[32] = {};
CefInitialize(args, settings, app, zeroed_memory_lol);
#endif
#if !ENABLE_LOCAL_FILE_URL_SCHEME
/* Register http://absolute/ scheme handler for older
* CEF builds which do not support file:// URLs */
CefRegisterSchemeHandlerFactory("http", "absolute",
new BrowserSchemeHandlerFactory());
#endif
os_event_signal(cef_started_event);
}
static void BrowserShutdown(void)
{
#if !ENABLE_LOCAL_FILE_URL_SCHEME
CefClearSchemeHandlerFactories();
#endif
#ifdef ENABLE_BROWSER_QT_LOOP
while (messageObject.ExecuteNextBrowserTask())
;
CefDoMessageLoopWork();
#endif
CefShutdown();
app = nullptr;
}
#ifndef ENABLE_BROWSER_QT_LOOP
static void BrowserManagerThread(void)
{
BrowserInit();
CefRunMessageLoop();
BrowserShutdown();
}
#endif
extern "C" EXPORT void obs_browser_initialize(void)
{
if (!os_atomic_set_bool(&manager_initialized, true)) {
#ifdef ENABLE_BROWSER_QT_LOOP
BrowserInit();
#else
manager_thread = thread(BrowserManagerThread);
#endif
}
}
void RegisterBrowserSource()
{
struct obs_source_info info = {};
info.id = "browser_source";
info.type = OBS_SOURCE_TYPE_INPUT;
info.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_AUDIO |
OBS_SOURCE_CUSTOM_DRAW | OBS_SOURCE_INTERACTION |
OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB;
info.get_properties = browser_source_get_properties;
info.get_defaults = browser_source_get_defaults;
info.icon_type = OBS_ICON_TYPE_BROWSER;
info.get_name = [](void *) { return obs_module_text("BrowserSource"); };
info.create = [](obs_data_t *settings, obs_source_t *source) -> void * {
obs_browser_initialize();
return new BrowserSource(settings, source);
};
info.destroy = [](void *data) {
static_cast<BrowserSource *>(data)->Destroy();
};
info.missing_files = browser_source_missingfiles;
info.update = [](void *data, obs_data_t *settings) {
static_cast<BrowserSource *>(data)->Update(settings);
};
info.get_width = [](void *data) {
return (uint32_t) static_cast<BrowserSource *>(data)->width;
};
info.get_height = [](void *data) {
return (uint32_t) static_cast<BrowserSource *>(data)->height;
};
info.video_tick = [](void *data, float) {
static_cast<BrowserSource *>(data)->Tick();
};
info.video_render = [](void *data, gs_effect_t *) {
static_cast<BrowserSource *>(data)->Render();
};
#if CHROME_VERSION_BUILD < 4103
info.audio_mix = [](void *data, uint64_t *ts_out,
struct audio_output_data *audio_output,
size_t channels, size_t sample_rate) {
return static_cast<BrowserSource *>(data)->AudioMix(
ts_out, audio_output, channels, sample_rate);
};
info.enum_active_sources = [](void *data, obs_source_enum_proc_t cb,
void *param) {
static_cast<BrowserSource *>(data)->EnumAudioStreams(cb, param);
};
#endif
info.mouse_click = [](void *data, const struct obs_mouse_event *event,
int32_t type, bool mouse_up,
uint32_t click_count) {
static_cast<BrowserSource *>(data)->SendMouseClick(
event, type, mouse_up, click_count);
};
info.mouse_move = [](void *data, const struct obs_mouse_event *event,
bool mouse_leave) {
static_cast<BrowserSource *>(data)->SendMouseMove(event,
mouse_leave);
};
info.mouse_wheel = [](void *data, const struct obs_mouse_event *event,
int x_delta, int y_delta) {
static_cast<BrowserSource *>(data)->SendMouseWheel(
event, x_delta, y_delta);
};
info.focus = [](void *data, bool focus) {
static_cast<BrowserSource *>(data)->SendFocus(focus);
};
info.key_click = [](void *data, const struct obs_key_event *event,
bool key_up) {
static_cast<BrowserSource *>(data)->SendKeyClick(event, key_up);
};
info.show = [](void *data) {
static_cast<BrowserSource *>(data)->SetShowing(true);
};
info.hide = [](void *data) {
static_cast<BrowserSource *>(data)->SetShowing(false);
};
info.activate = [](void *data) {
BrowserSource *bs = static_cast<BrowserSource *>(data);
if (bs->restart)
bs->Refresh();
bs->SetActive(true);
};
info.deactivate = [](void *data) {
static_cast<BrowserSource *>(data)->SetActive(false);
};
obs_register_source(&info);
}
/* ========================================================================= */
extern void DispatchJSEvent(std::string eventName, std::string jsonString,
BrowserSource *browser = nullptr);
static void handle_obs_frontend_event(enum obs_frontend_event event, void *)
{
switch (event) {
case OBS_FRONTEND_EVENT_STREAMING_STARTING:
DispatchJSEvent("obsStreamingStarting", "null");
break;
case OBS_FRONTEND_EVENT_STREAMING_STARTED:
DispatchJSEvent("obsStreamingStarted", "null");
break;
case OBS_FRONTEND_EVENT_STREAMING_STOPPING:
DispatchJSEvent("obsStreamingStopping", "null");
break;
case OBS_FRONTEND_EVENT_STREAMING_STOPPED:
DispatchJSEvent("obsStreamingStopped", "null");
break;
case OBS_FRONTEND_EVENT_RECORDING_STARTING:
DispatchJSEvent("obsRecordingStarting", "null");
break;
case OBS_FRONTEND_EVENT_RECORDING_STARTED:
DispatchJSEvent("obsRecordingStarted", "null");
break;
case OBS_FRONTEND_EVENT_RECORDING_PAUSED:
DispatchJSEvent("obsRecordingPaused", "null");
break;
case OBS_FRONTEND_EVENT_RECORDING_UNPAUSED:
DispatchJSEvent("obsRecordingUnpaused", "null");
break;
case OBS_FRONTEND_EVENT_RECORDING_STOPPING:
DispatchJSEvent("obsRecordingStopping", "null");
break;
case OBS_FRONTEND_EVENT_RECORDING_STOPPED:
DispatchJSEvent("obsRecordingStopped", "null");
break;
case OBS_FRONTEND_EVENT_REPLAY_BUFFER_STARTING:
DispatchJSEvent("obsReplaybufferStarting", "null");
break;
case OBS_FRONTEND_EVENT_REPLAY_BUFFER_STARTED:
DispatchJSEvent("obsReplaybufferStarted", "null");
break;
case OBS_FRONTEND_EVENT_REPLAY_BUFFER_SAVED:
DispatchJSEvent("obsReplaybufferSaved", "null");
break;
case OBS_FRONTEND_EVENT_REPLAY_BUFFER_STOPPING:
DispatchJSEvent("obsReplaybufferStopping", "null");
break;
case OBS_FRONTEND_EVENT_REPLAY_BUFFER_STOPPED:
DispatchJSEvent("obsReplaybufferStopped", "null");
break;
case OBS_FRONTEND_EVENT_VIRTUALCAM_STARTED:
DispatchJSEvent("obsVirtualcamStarted", "null");
break;
case OBS_FRONTEND_EVENT_VIRTUALCAM_STOPPED:
DispatchJSEvent("obsVirtualcamStopped", "null");
break;
case OBS_FRONTEND_EVENT_SCENE_CHANGED: {
OBSSourceAutoRelease source = obs_frontend_get_current_scene();
if (!source)
break;
const char *name = obs_source_get_name(source);
if (!name)
break;
nlohmann::json json = {{"name", name},
{"width", obs_source_get_width(source)},
{"height",
obs_source_get_height(source)}};
DispatchJSEvent("obsSceneChanged", json.dump());
break;
}
case OBS_FRONTEND_EVENT_SCENE_LIST_CHANGED: {
struct obs_frontend_source_list list = {};
obs_frontend_get_scenes(&list);
std::vector<const char *> scenes_vector;
for (size_t i = 0; i < list.sources.num; i++) {
obs_source_t *source = list.sources.array[i];
scenes_vector.push_back(obs_source_get_name(source));
}
nlohmann::json json = scenes_vector;
obs_frontend_source_list_free(&list);
DispatchJSEvent("obsSceneListChanged", json.dump());
break;
}
case OBS_FRONTEND_EVENT_TRANSITION_CHANGED: {
OBSSourceAutoRelease source =
obs_frontend_get_current_transition();
if (!source)
break;
const char *name = obs_source_get_name(source);
if (!name)
break;
nlohmann::json json = {{"name", name}};
DispatchJSEvent("obsTransitionChanged", json.dump());
break;
}
case OBS_FRONTEND_EVENT_TRANSITION_LIST_CHANGED: {
struct obs_frontend_source_list list = {};
obs_frontend_get_transitions(&list);
std::vector<const char *> transitions_vector;
for (size_t i = 0; i < list.sources.num; i++) {
obs_source_t *source = list.sources.array[i];
transitions_vector.push_back(
obs_source_get_name(source));
}
nlohmann::json json = transitions_vector;
obs_frontend_source_list_free(&list);
DispatchJSEvent("obsTransitionListChanged", json.dump());
break;
}
case OBS_FRONTEND_EVENT_EXIT:
DispatchJSEvent("obsExit", "null");
break;
default:;
}
}
#ifdef _WIN32
static inline void EnumAdapterCount()
{
ComPtr<IDXGIFactory1> factory;
ComPtr<IDXGIAdapter1> adapter;
HRESULT hr;
UINT i = 0;
hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void **)&factory);
if (FAILED(hr))
return;
while (factory->EnumAdapters1(i++, &adapter) == S_OK) {
DXGI_ADAPTER_DESC desc;
hr = adapter->GetDesc(&desc);
if (FAILED(hr))
continue;
if (i == 1)
deviceId = desc.Description;
/* ignore Microsoft's 'basic' renderer' */
if (desc.VendorId == 0x1414 && desc.DeviceId == 0x8c)
continue;
adapterCount++;
}
}
#endif
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
#ifdef _WIN32
static const wchar_t *blacklisted_devices[] = {
L"Intel", L"Microsoft", L"Radeon HD 8850M", L"Radeon HD 7660", nullptr};
static inline bool is_intel(const std::wstring &str)
{
return wstrstri(str.c_str(), L"Intel") != 0;
}
static void check_hwaccel_support(void)
{
/* do not use hardware acceleration if a blacklisted device is the
* default and on 2 or more adapters */
const wchar_t **device = blacklisted_devices;
if (adapterCount >= 2 || !is_intel(deviceId)) {
while (*device) {
if (!!wstrstri(deviceId.c_str(), *device)) {
hwaccel = false;
blog(LOG_INFO, "[obs-browser]: "
"Blacklisted device "
"detected, "
"disabling browser "
"source hardware "
"acceleration.");
break;
}
device++;
}
}
}
#else
static void check_hwaccel_support(void)
{
return;
}
#endif
#endif
bool obs_module_load(void)
{
#ifdef ENABLE_BROWSER_QT_LOOP
qRegisterMetaType<MessageTask>("MessageTask");
#endif
os_event_init(&cef_started_event, OS_EVENT_TYPE_MANUAL);
#ifdef _WIN32
/* CefEnableHighDPISupport doesn't do anything on OS other than Windows. Would also crash macOS at this point as CEF is not directly linked */
CefEnableHighDPISupport();
EnumAdapterCount();
#else
#if defined(__APPLE__) && !defined(ENABLE_BROWSER_LEGACY)
/* Load CEF at runtime as required on macOS */
CefScopedLibraryLoader library_loader;
if (!library_loader.LoadInMain())
return false;
#endif
#endif
blog(LOG_INFO, "[obs-browser]: Version %s", OBS_BROWSER_VERSION_STRING);
blog(LOG_INFO,
"[obs-browser]: CEF Version %i.%i.%i.%i (runtime), %s (compiled)",
cef_version_info(4), cef_version_info(5), cef_version_info(6),
cef_version_info(7), CEF_VERSION);
RegisterBrowserSource();
obs_frontend_add_event_callback(handle_obs_frontend_event, nullptr);
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
OBSDataAutoRelease private_data = obs_get_private_data();
hwaccel = obs_data_get_bool(private_data, "BrowserHWAccel");
if (hwaccel) {
check_hwaccel_support();
}
#endif
#if defined(__APPLE__) && CHROME_VERSION_BUILD < 4183
// Make sure CEF malloc hijacking happens early in the process
obs_browser_initialize();
#endif
return true;
}
void obs_module_post_load(void)
{
auto vendor = obs_websocket_register_vendor("obs-browser");
if (!vendor)
return;
auto emit_event_request_cb = [](obs_data_t *request_data, obs_data_t *,
void *) {
const char *event_name =
obs_data_get_string(request_data, "event_name");
if (!event_name)
return;
OBSDataAutoRelease event_data =
obs_data_get_obj(request_data, "event_data");
const char *event_data_string =
event_data ? obs_data_get_json(event_data) : "{}";
DispatchJSEvent(event_name, event_data_string, nullptr);
};
if (!obs_websocket_vendor_register_request(
vendor, "emit_event", emit_event_request_cb, nullptr))
blog(LOG_WARNING,
"[obs-browser]: Failed to register obs-websocket request emit_event");
}
void obs_module_unload(void)
{
#ifdef ENABLE_BROWSER_QT_LOOP
BrowserShutdown();
#else
if (manager_thread.joinable()) {
while (!QueueCEFTask([]() { CefQuitMessageLoop(); }))
os_sleep_ms(5);
manager_thread.join();
}
#endif
os_event_destroy(cef_started_event);
}
|