Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions browser-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
{
CefRefPtr<CefV8Value> globalObj = context->GetGlobal();

CefRefPtr<CefV8Value> obsStudioObj = CefV8Value::CreateObject(0, 0);
CefRefPtr<CefV8Value> obsStudioObj =
CefV8Value::CreateObject(nullptr, nullptr);
globalObj->SetValue("obsstudio", obsStudioObj,
V8_PROPERTY_ATTRIBUTE_NONE);

Expand Down Expand Up @@ -154,7 +155,7 @@ void BrowserApp::ExecuteJSFunction(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefV8Value> jsFunction = obsStudioObj->GetValue(functionName);

if (jsFunction && jsFunction->IsFunction())
jsFunction->ExecuteFunction(NULL, arguments);
jsFunction->ExecuteFunction(nullptr, arguments);

context->Exit();
}
Expand Down Expand Up @@ -310,7 +311,7 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,

CefRefPtr<CefV8Value> dispatchEvent =
globalObj->GetValue("dispatchEvent");
dispatchEvent->ExecuteFunction(NULL, arguments);
dispatchEvent->ExecuteFunction(nullptr, arguments);

context->Exit();

Expand Down Expand Up @@ -340,7 +341,7 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
args.push_back(retval);

if (callback)
callback->ExecuteFunction(NULL, args);
callback->ExecuteFunction(nullptr, args);

context->Exit();

Expand Down
102 changes: 76 additions & 26 deletions browser-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <QApplication>
#include <QThread>
#include <QToolTip>
#if defined(__APPLE__) && CHROME_VERSION_BUILD > 4430
#include <IOSurface/IOSurface.h>
#endif

using namespace json11;

Expand Down Expand Up @@ -61,9 +64,34 @@ CefRefPtr<CefAudioHandler> BrowserClient::GetAudioHandler()
}
#endif

#if CHROME_VERSION_BUILD >= 4638
CefRefPtr<CefRequestHandler> BrowserClient::GetRequestHandler()
{
return this;
}

CefRefPtr<CefResourceRequestHandler> BrowserClient::GetResourceRequestHandler(
CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
CefRefPtr<CefRequest> request, bool, bool, const CefString &, bool &)
{
if (request->GetHeaderByName("origin") == "null") {
return this;
}

return nullptr;
}

CefResourceRequestHandler::ReturnValue BrowserClient::OnBeforeResourceLoad(
CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefRequest>,
CefRefPtr<CefCallback>)
{
return RV_CONTINUE;
}
#endif

bool BrowserClient::OnBeforePopup(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
const CefString &, const CefString &,
WindowOpenDisposition, bool,
cef_window_open_disposition_t, bool,
const CefPopupFeatures &, CefWindowInfo &,
CefRefPtr<CefClient> &, CefBrowserSettings &,
#if CHROME_VERSION_BUILD >= 3770
Expand Down Expand Up @@ -269,37 +297,59 @@ void BrowserClient::OnAcceleratedPaint(CefRefPtr<CefBrowser>,
return;
}

if (shared_handle != last_handle) {
obs_enter_graphics();
#ifndef _WIN32
if (shared_handle == last_handle)
return;
#endif

if (bs->texture) {
if (bs->extra_texture) {
gs_texture_destroy(bs->extra_texture);
bs->extra_texture = nullptr;
}
gs_texture_destroy(bs->texture);
bs->texture = nullptr;
}
obs_enter_graphics();

bs->texture = gs_texture_open_shared(
(uint32_t)(uintptr_t)shared_handle);
if (bs->texture) {
const uint32_t cx = gs_texture_get_width(bs->texture);
const uint32_t cy = gs_texture_get_height(bs->texture);
const gs_color_format format =
gs_texture_get_color_format(bs->texture);
const gs_color_format linear_format =
gs_generalize_format(format);
if (linear_format != format) {
bs->extra_texture = gs_texture_create(
cx, cy, linear_format, 1, nullptr, 0);
}
if (bs->texture) {
if (bs->extra_texture) {
gs_texture_destroy(bs->extra_texture);
bs->extra_texture = nullptr;
}
#ifdef _WIN32
gs_texture_release_sync(bs->texture, 0);
#endif
gs_texture_destroy(bs->texture);
#ifdef _WIN32
CloseHandle(extra_handle);
#endif
bs->texture = nullptr;
}

obs_leave_graphics();
#if defined(__APPLE__) && CHROME_VERSION_BUILD > 4183
bs->texture = gs_texture_create_from_iosurface(
(IOSurfaceRef)(uintptr_t)shared_handle);
#elif defined(_WIN32) && CHROME_VERSION_BUILD > 4183
DuplicateHandle(GetCurrentProcess(), (HANDLE)(uintptr_t)shared_handle,
GetCurrentProcess(), &extra_handle, 0, false,
DUPLICATE_SAME_ACCESS);

bs->texture =
gs_texture_open_nt_shared((uint32_t)(uintptr_t)shared_handle);
gs_texture_acquire_sync(bs->texture, 1, INFINITE);
#else
bs->texture =
gs_texture_open_shared((uint32_t)(uintptr_t)shared_handle);
#endif

last_handle = shared_handle;
if (bs->texture) {
const uint32_t cx = gs_texture_get_width(bs->texture);
const uint32_t cy = gs_texture_get_height(bs->texture);
const gs_color_format format =
gs_texture_get_color_format(bs->texture);
const gs_color_format linear_format =
gs_generalize_format(format);
if (linear_format != format) {
bs->extra_texture = gs_texture_create(
cx, cy, linear_format, 1, nullptr, 0);
}
}
obs_leave_graphics();

last_handle = shared_handle;
}
#endif

Expand Down
26 changes: 25 additions & 1 deletion browser-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include <graphics/graphics.h>
#include <util/threading.h>
#include "cef-headers.hpp"
#include "browser-config.h"
#include "obs-browser-source.hpp"
Expand All @@ -28,6 +29,10 @@ struct BrowserSource;
class BrowserClient : public CefClient,
public CefDisplayHandler,
public CefLifeSpanHandler,
public CefRequestHandler,
#if CHROME_VERSION_BUILD >= 4638
public CefResourceRequestHandler,
#endif
public CefContextMenuHandler,
public CefRenderHandler,
#if CHROME_VERSION_BUILD >= 3683
Expand All @@ -38,6 +43,7 @@ class BrowserClient : public CefClient,
#ifdef SHARED_TEXTURE_SUPPORT_ENABLED
#ifdef _WIN32
void *last_handle = INVALID_HANDLE_VALUE;
void *extra_handle = INVALID_HANDLE_VALUE;
#elif defined(__APPLE__)
void *last_handle = nullptr;
#endif
Expand Down Expand Up @@ -72,6 +78,9 @@ class BrowserClient : public CefClient,
virtual CefRefPtr<CefRenderHandler> GetRenderHandler() override;
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() override;
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override;
#if CHROME_VERSION_BUILD >= 4638
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() override;
#endif
virtual CefRefPtr<CefContextMenuHandler>
GetContextMenuHandler() override;
#if CHROME_VERSION_BUILD >= 3683
Expand Down Expand Up @@ -102,14 +111,29 @@ class BrowserClient : public CefClient,
OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
const CefString &target_url,
const CefString &target_frame_name,
WindowOpenDisposition target_disposition,
cef_window_open_disposition_t target_disposition,
bool user_gesture, const CefPopupFeatures &popupFeatures,
CefWindowInfo &windowInfo, CefRefPtr<CefClient> &client,
CefBrowserSettings &settings,
#if CHROME_VERSION_BUILD >= 3770
CefRefPtr<CefDictionaryValue> &extra_info,
#endif
bool *no_javascript_access) override;
#if CHROME_VERSION_BUILD >= 4638
/* CefRequestHandler */
virtual CefRefPtr<CefResourceRequestHandler> GetResourceRequestHandler(
CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, bool is_navigation,
bool is_download, const CefString &request_initiator,
bool &disable_default_handling) override;

/* CefResourceRequestHandler */
virtual CefResourceRequestHandler::ReturnValue
OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
CefRefPtr<CefCallback> callback) override;
#endif

/* CefContextMenuHandler */
virtual void
Expand Down
8 changes: 6 additions & 2 deletions browser-scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ BrowserSchemeHandlerFactory::Create(CefRefPtr<CefBrowser> browser,
CefStreamReader::CreateForFile(path);
#endif

return new CefStreamResourceHandler(CefGetMimeType(fileExtension),
stream);
if (stream) {
return new CefStreamResourceHandler(
CefGetMimeType(fileExtension), stream);
} else {
return nullptr;
}
}
#endif
2 changes: 1 addition & 1 deletion browser-scheme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <string>
#include <fstream>

#if CHROME_VERSION_BUILD >= 3440
#if CHROME_VERSION_BUILD >= 3440 && CHROME_VERSION_BUILD < 4638
#define ENABLE_LOCAL_FILE_URL_SCHEME 1
#else
#define ENABLE_LOCAL_FILE_URL_SCHEME 0
Expand Down
7 changes: 5 additions & 2 deletions cef-headers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@
#endif

#if CHROME_VERSION_BUILD >= 3770
#define SendBrowserProcessMessage(browser, pid, msg) \
browser->GetMainFrame()->SendProcessMessage(pid, msg);
#define SendBrowserProcessMessage(browser, pid, msg) \
CefRefPtr<CefFrame> mainFrame = browser->GetMainFrame(); \
if (mainFrame) { \
mainFrame->SendProcessMessage(pid, msg); \
}
#else
#define SendBrowserProcessMessage(browser, pid, msg) \
browser->SendProcessMessage(pid, msg);
Expand Down
42 changes: 33 additions & 9 deletions obs-browser-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ bool BrowserSource::CreateBrowser()
#if CHROME_VERSION_BUILD < 3071
windowInfo.transparent_painting_enabled = true;
#endif
#if CHROME_VERSION_BUILD < 4430
windowInfo.width = width;
windowInfo.height = height;
#else
windowInfo.bounds.width = width;
windowInfo.bounds.height = height;
#endif
windowInfo.windowless_rendering_enabled = true;

#ifdef SHARED_TEXTURE_SUPPORT_ENABLED
Expand All @@ -159,17 +164,19 @@ bool BrowserSource::CreateBrowser()
CefBrowserSettings cefBrowserSettings;

#ifdef SHARED_TEXTURE_SUPPORT_ENABLED
#ifdef _WIN32
#ifdef BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED
if (!fps_custom) {
windowInfo.external_begin_frame_enabled = true;
cefBrowserSettings.windowless_frame_rate = 0;
} else {
cefBrowserSettings.windowless_frame_rate = fps;
}
#else
double video_fps = obs_get_active_fps();
struct obs_video_info ovi;
obs_get_video_info(&ovi);
canvas_fps = (double)ovi.fps_num / (double)ovi.fps_den;
cefBrowserSettings.windowless_frame_rate =
(fps_custom) ? fps : video_fps;
(fps_custom) ? fps : canvas_fps;
#endif
#else
cefBrowserSettings.windowless_frame_rate = fps;
Expand All @@ -178,14 +185,13 @@ bool BrowserSource::CreateBrowser()
cefBrowserSettings.default_font_size = 16;
cefBrowserSettings.default_fixed_font_size = 16;

#if ENABLE_LOCAL_FILE_URL_SCHEME
#if ENABLE_LOCAL_FILE_URL_SCHEME && CHROME_VERSION_BUILD < 4430
if (is_local) {
/* Disable web security for file:// URLs to allow
* local content access to remote APIs */
cefBrowserSettings.web_security = STATE_DISABLED;
}
#endif

cefBrowser = CefBrowserHost::CreateBrowserSync(
windowInfo, browserClient, url, cefBrowserSettings,
#if CHROME_VERSION_BUILD >= 3770
Expand Down Expand Up @@ -299,7 +305,11 @@ void BrowserSource::SendFocus(bool focus)
{
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
#if CHROME_VERSION_BUILD < 4430
cefBrowser->GetHost()->SendFocusEvent(focus);
#else
cefBrowser->GetHost()->SetFocus(focus);
#endif
},
true);
}
Expand Down Expand Up @@ -379,7 +389,8 @@ void BrowserSource::SetShowing(bool showing)
true);
Json json = Json::object{{"visible", showing}};
DispatchJSEvent("obsSourceVisibleChanged", json.dump(), this);
#if defined(_WIN32) && defined(SHARED_TEXTURE_SUPPORT_ENABLED)
#if defined(BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED) && \
defined(SHARED_TEXTURE_SUPPORT_ENABLED)
if (showing && !fps_custom) {
reset_frame = false;
}
Expand Down Expand Up @@ -414,7 +425,7 @@ void BrowserSource::Refresh()
true);
}
#ifdef SHARED_TEXTURE_SUPPORT_ENABLED
#ifdef _WIN32
#ifdef BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED
inline void BrowserSource::SignalBeginFrame()
{
if (reset_frame) {
Expand Down Expand Up @@ -539,9 +550,21 @@ void BrowserSource::Tick()
{
if (create_browser && CreateBrowser())
create_browser = false;
#if defined(_WIN32) && defined(SHARED_TEXTURE_SUPPORT_ENABLED)
#if defined(SHARED_TEXTURE_SUPPORT_ENABLED)
#if defined(BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED)
if (!fps_custom)
reset_frame = true;
#else
struct obs_video_info ovi;
obs_get_video_info(&ovi);
double video_fps = (double)ovi.fps_num / (double)ovi.fps_den;

if (!fps_custom) {
if (!!cefBrowser && canvas_fps != video_fps) {
Update();
}
}
#endif
#endif
}

Expand Down Expand Up @@ -600,7 +623,8 @@ void BrowserSource::Render()
gs_enable_framebuffer_srgb(previous);
}

#if defined(_WIN32) && defined(SHARED_TEXTURE_SUPPORT_ENABLED)
#if defined(BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED) && \
defined(SHARED_TEXTURE_SUPPORT_ENABLED)
SignalBeginFrame();
#elif USE_QT_LOOP
ProcessCef();
Expand Down
Loading