forked from obsproject/obs-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs-browser-plugin.cpp
511 lines (442 loc) · 13.8 KB
/
obs-browser-plugin.cpp
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
/******************************************************************************
Copyright (C) 2014 by John R. Bradley <[email protected]>
Copyright (C) 2018 by Hugh Bailey ("Jim") <[email protected]>
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 <thread>
#include <mutex>
#include "obs-browser-source.hpp"
#include "browser-scheme.hpp"
#include "browser-app.hpp"
#include "browser-version.h"
#include "browser-config.h"
#include "json11/json11.hpp"
#include "cef-headers.hpp"
#ifdef _WIN32
#include <util/windows/ComPtr.hpp>
#include <dxgi.h>
#include <dxgi1_2.h>
#include <d3d11.h>
#endif
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("obs-browser", "en-US")
using namespace std;
using namespace json11;
static thread manager_thread;
os_event_t *cef_started_event = nullptr;
static int adapterCount = 0;
static std::wstring deviceId;
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
bool hwaccel = false;
#endif
/* ========================================================================= */
class BrowserTask : public CefTask {
public:
std::function<void()> task;
inline BrowserTask(std::function<void()> task_) : task(task_) {}
virtual void Execute() override {task();}
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);
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
obs_data_set_default_bool(settings, "fps_custom", false);
#else
obs_data_set_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_string(settings, "css", default_css);
}
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("Local file"), 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, 4096, 1);
obs_properties_add_int(props, "height",
obs_module_text("Height"), 1, 4096, 1);
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);
#if !EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
obs_property_set_enabled(fps_set, false);
#endif
obs_properties_add_int(props, "fps",
obs_module_text("FPS"), 1, 60, 1);
obs_properties_add_text(props, "css",
obs_module_text("CSS"), OBS_TEXT_MULTILINE);
obs_properties_add_bool(props, "shutdown",
obs_module_text("ShutdownSourceNotVisible"));
obs_properties_add_bool(props, "restart_when_active",
obs_module_text("RefreshBrowserActive"));
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 BrowserManagerThread(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
CefSettings settings;
settings.log_severity = LOGSEVERITY_DISABLE;
settings.windowless_rendering_enabled = true;
settings.no_sandbox = true;
#if defined(__APPLE__) && !defined(BROWSER_DEPLOY)
CefString(&settings.framework_dir_path) = CEF_LIBRARY;
#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 = obs_module_config_path("");
os_mkdir(conf_path);
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;
CefString(&settings.cache_path) = conf_path_abs;
CefString(&settings.browser_subprocess_path) = path;
bool tex_sharing_avail = false;
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
if (hwaccel) {
obs_enter_graphics();
hwaccel = tex_sharing_avail = gs_shared_texture_available();
obs_leave_graphics();
}
#endif
CefRefPtr<BrowserApp> app(new BrowserApp(tex_sharing_avail));
CefExecuteProcess(args, app, nullptr);
CefInitialize(args, settings, app, nullptr);
CefRegisterSchemeHandlerFactory("http", "absolute",
new BrowserSchemeHandlerFactory());
os_event_signal(cef_started_event);
CefRunMessageLoop();
CefShutdown();
}
extern "C" EXPORT void obs_browser_initialize(void)
{
static bool manager_initialized = false;
if (!os_atomic_set_bool(&manager_initialized, true)) {
manager_thread = thread(BrowserManagerThread);
}
}
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_CUSTOM_DRAW |
OBS_SOURCE_INTERACTION |
OBS_SOURCE_DO_NOT_DUPLICATE;
info.get_properties = browser_source_get_properties;
info.get_defaults = browser_source_get_defaults;
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)
{
delete static_cast<BrowserSource *>(data);
};
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();
};
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(const char *eventName, const char *jsonString);
static void handle_obs_frontend_event(enum obs_frontend_event event, void *)
{
switch (event) {
case OBS_FRONTEND_EVENT_STREAMING_STARTING:
DispatchJSEvent("obsStreamingStarting", nullptr);
break;
case OBS_FRONTEND_EVENT_STREAMING_STARTED:
DispatchJSEvent("obsStreamingStarted", nullptr);
break;
case OBS_FRONTEND_EVENT_STREAMING_STOPPING:
DispatchJSEvent("obsStreamingStopping", nullptr);
break;
case OBS_FRONTEND_EVENT_STREAMING_STOPPED:
DispatchJSEvent("obsStreamingStopped", nullptr);
break;
case OBS_FRONTEND_EVENT_RECORDING_STARTING:
DispatchJSEvent("obsRecordingStarting", nullptr);
break;
case OBS_FRONTEND_EVENT_RECORDING_STARTED:
DispatchJSEvent("obsRecordingStarted", nullptr);
break;
case OBS_FRONTEND_EVENT_RECORDING_STOPPING:
DispatchJSEvent("obsRecordingStopping", nullptr);
break;
case OBS_FRONTEND_EVENT_RECORDING_STOPPED:
DispatchJSEvent("obsRecordingStopped", nullptr);
break;
case OBS_FRONTEND_EVENT_SCENE_CHANGED:
{
OBSSource source = obs_frontend_get_current_scene();
obs_source_release(source);
if (!source)
break;
const char *name = obs_source_get_name(source);
if (!name)
break;
Json json = Json::object {
{"name", name},
{"width", (int)obs_source_get_width(source)},
{"height", (int)obs_source_get_height(source)}
};
DispatchJSEvent("obsSceneChanged", json.dump().c_str());
break;
}
case OBS_FRONTEND_EVENT_EXIT:
DispatchJSEvent("obsExit", nullptr);
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
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
static const wchar_t *blacklisted_devices[] = {
L"Intel",
L"Microsoft",
L"Radeon HD 8850M",
L"Radeon HD 7660",
nullptr
};
#endif
bool obs_module_load(void)
{
blog(LOG_INFO, "[obs-browser]: Version %s",
OBS_BROWSER_VERSION_STRING);
os_event_init(&cef_started_event, OS_EVENT_TYPE_MANUAL);
CefEnableHighDPISupport();
#ifdef _WIN32
EnumAdapterCount();
#endif
RegisterBrowserSource();
obs_frontend_add_event_callback(handle_obs_frontend_event, nullptr);
#if EXPERIMENTAL_SHARED_TEXTURE_SUPPORT_ENABLED
obs_data_t *private_data = obs_get_private_data();
hwaccel = obs_data_get_bool(private_data, "BrowserHWAccel");
if (hwaccel) {
/* do not use hardware acceleration if a blacklisted device is
* the default and on 2 or more adapters */
const wchar_t **device = blacklisted_devices;
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++;
}
}
obs_data_release(private_data);
#endif
return true;
}
void obs_module_unload(void)
{
if (manager_thread.joinable()) {
while (!QueueCEFTask([] () {CefQuitMessageLoop();}))
os_sleep_ms(5);
manager_thread.join();
}
os_event_destroy(cef_started_event);
}