Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properties: Move gpu and chrome_extra_flags to env vars #91

Merged
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
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ GST_PLUGIN_PATH=Release:$GST_PLUGIN_PATH gst-launch-1.0 \
gst-launch-1.0 playbin uri=web+https://www.soundcloud.com/platform/sama
```

### Note on Global CEF Parameters

This note is only relevant if you want to run multiple cefsrc instances in the same process.

Prior releases of this plugin exposed some CEF properties that are in fact global to a single process.
We have now deprecated use of these as cefsrc element properties and instead exposed them as environment
variables. These properties are listed here for reference along with the env var that replaces them:

- `cef-cache-location` - set GST_CEF_CACHE_LOCATION instead
- `chrome-extra-flags` - set GST_CEF_CHROME_EXTRA_FLAGS
- `chromium-debug-port` - set GST_CEF_CHROME_EXTRA_FLAGS
- `gpu` - set GST_CEF_GPU_ENABLED
- `log-severity` - set GST_CEF_LOG_SEVERITY
- `sandbox` - set GST_CEF_SANDBOX

Note that if you do require pipelines / applications that run multiple `cefsrc` instances with differing
values for the above properties, we recommend running these source sections in a separate process and
bring the resulting frames into your main process using something like the
[`ipc`](https://gstreamer.freedesktop.org/documentation/ipcpipeline/index.html?gi-language=c) plugins.

## Docker GPU Acceleration

This is simply a hint/note for those who want to use this plugin in a docker container with GPU acceleration. Your particular setup may vary. The following was tested on Ubuntu 22.04 with a Nvidia GPU. This assumes you have installed the Nvidia drivers, docker, and the Nvidia Container Toolkit. You may also need to configure your xorg.conf within the container to use the Nvidia GPU.
Expand All @@ -138,11 +158,11 @@ Xorg -noreset +extension GLX +extension RANDR \+extension RENDER -logfile ./xser
Test that the GPU is accessible by running:

``` shell
gst-launch-1.0 -e cefsrc url="chrome://gpu" gpu=true \
chrome-extra-flags="use-gl=egl, enable-gpu-rasterization,ignore-gpu-blocklist" \
! video/x-raw, width=1920, height=8080, framerate=1/1 \
! cefdemux name=demux ! queue ! videoconvert \
! pngenc ! multifilesink location="frame%d.png"
GST_CEF_CHROME_EXTRA_FLAGS="use-gl=egl, enable-gpu-rasterization,ignore-gpu-blocklist" \
GST_CEF_GPU_ENABLED="set" gst-launch-1.0 -e cefsrc url="chrome://gpu" \
! video/x-raw, width=1920, height=8080, framerate=1/1 \
! cefdemux name=demux ! queue ! videoconvert \
! pngenc ! multifilesink location="frame%d.png"
```

It is also helpful to run nvidia-smi or nvtop to verify the GPU is being used. Note it is possible to use the GPU within a kube pod as well. This has been tested and runs in a production environment on GKE using Container Optimized OS.
Expand Down
157 changes: 122 additions & 35 deletions gstcefsrc.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstdio>
#include <glib.h>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -90,26 +91,39 @@ static GThread *thread = nullptr;
(gst_cef_log_severity_mode_get_type ())


static const GEnumValue log_severity_values[] = {
{LOGSEVERITY_DEBUG, "debug / verbose cef log severity", "debug"},
{LOGSEVERITY_INFO, "info cef log severity", "info"},
{LOGSEVERITY_WARNING, "warning cef log severity", "warning"},
{LOGSEVERITY_ERROR, "error cef log severity", "error"},
{LOGSEVERITY_FATAL, "fatal cef log severity", "fatal"},
{LOGSEVERITY_DISABLE, "disable cef log severity", "disable"},
{0, NULL, NULL},
};

static GType
gst_cef_log_severity_mode_get_type (void)
{
static GType type = 0;
static const GEnumValue values[] = {
{LOGSEVERITY_DEBUG, "debug / verbose cef log severity", "debug"},
{LOGSEVERITY_INFO, "info cef log severity", "info"},
{LOGSEVERITY_WARNING, "warning cef log severity", "warning"},
{LOGSEVERITY_ERROR, "error cef log severity", "error"},
{LOGSEVERITY_FATAL, "fatal cef log severity", "fatal"},
{LOGSEVERITY_DISABLE, "disable cef log severity", "disable"},
{0, NULL, NULL},
};

if (!type) {
type = g_enum_register_static ("GstCefLogSeverityMode", values);
type = g_enum_register_static ("GstCefLogSeverityMode", log_severity_values);
}
return type;
}

static gint gst_cef_log_severity_from_str (const gchar *str)
{
for (guint i = 0; i < sizeof(log_severity_values) / sizeof(GEnumValue); i++) {
const gchar *nick = log_severity_values[i].value_nick;
if (!nick) break;
if (g_str_equal(str, nick)) {
return log_severity_values[i].value;
}
}

return -1;
}

enum
{
PROP_0,
Expand Down Expand Up @@ -600,15 +614,17 @@ void BrowserApp::OnBeforeCommandLineProcessing(const CefString &process_type,
command_line->AppendSwitch("disable-dev-shm-usage"); /* https://github.com/GoogleChrome/puppeteer/issues/1834 */
command_line->AppendSwitch("enable-begin-frame-scheduling"); /* https://bitbucket.org/chromiumembedded/cef/issues/1368 */

bool gpu = src->gpu || (!!g_getenv ("GST_CEF_GPU_ENABLED"));

#ifdef __APPLE__
command_line->AppendSwitch("off-screen-rendering-enabled");
if (src->gpu) {
if (gpu) {
GST_WARNING_OBJECT(src, "GPU rendering is known not to work on macOS. Disabling it now. See https://github.com/chromiumembedded/cef/issues/3322 and https://magpcss.org/ceforum/viewtopic.php?f=6&t=19397");
src->gpu = FALSE;
gpu = FALSE;
}
#endif

if (!src->gpu) {
if (!gpu) {
// Optimize for no gpu usage
command_line->AppendSwitch("disable-gpu");
command_line->AppendSwitch("disable-gpu-compositing");
Expand All @@ -618,8 +634,13 @@ void BrowserApp::OnBeforeCommandLineProcessing(const CefString &process_type,
command_line->AppendSwitchWithValue("remote-debugging-port", g_strdup_printf ("%i", src->chromium_debug_port));
}

if (src->chrome_extra_flags) {
gchar **flags_list = g_strsplit ((const gchar *) src->chrome_extra_flags, ",", -1);
const gchar *extra_flags = src->chrome_extra_flags;
if (!extra_flags) {
extra_flags = g_getenv ("GST_CEF_CHROME_EXTRA_FLAGS");
}

if (extra_flags) {
gchar **flags_list = g_strsplit (extra_flags, ",", -1);
guint i;

for (i = 0; i < g_strv_length (flags_list); i++) {
Expand Down Expand Up @@ -698,7 +719,7 @@ gst_cef_shutdown(void *)
* concurrent cefsrc instances.
*/
static gpointer
run_cef (GstCefSrc *src)
init_cef (GstCefSrc *src)
{
#ifdef G_OS_WIN32
HINSTANCE hInstance = GetModuleHandle(NULL);
Expand All @@ -712,9 +733,31 @@ run_cef (GstCefSrc *src)
CefWindowInfo window_info;
CefBrowserSettings browserSettings;

settings.no_sandbox = !src->sandbox;
// pull in parameters from gst properties (deprecated) or environment
cef_log_severity_t log_severity = src->log_severity;
const gchar* log_severity_env = g_getenv ("GST_CEF_LOG_SEVERITY");
if (log_severity_env) {
gint severity = gst_cef_log_severity_from_str(log_severity_env);
if (severity >= 0) {
log_severity = (cef_log_severity_t) severity;
}
}

const gchar *js_flags = src->js_flags;
if (!js_flags) {
js_flags = g_getenv ("GST_CEF_JS_FLAGS");
}

const gchar *cef_cache_location = src->cef_cache_location;
if (!cef_cache_location) {
cef_cache_location = g_getenv ("GST_CEF_CACHE_LOCATION");
}

bool sandbox = src->gpu || (!!g_getenv ("GST_CEF_SANDBOX"));

settings.no_sandbox = !sandbox;
settings.windowless_rendering_enabled = true;
settings.log_severity = src->log_severity;
settings.log_severity = log_severity;
settings.multi_threaded_message_loop = false;
#ifdef __APPLE__
settings.external_message_pump = true;
Expand Down Expand Up @@ -777,12 +820,12 @@ run_cef (GstCefSrc *src)
gchar *locales_dir_path = g_build_filename(base_path, "locales", nullptr);
CefString(&settings.locales_dir_path).FromASCII(locales_dir_path);

if (src->js_flags != NULL) {
CefString(&settings.javascript_flags).FromASCII(src->js_flags);
if (js_flags != NULL) {
CefString(&settings.javascript_flags).FromASCII(js_flags);
}

if (src->cef_cache_location != NULL) {
CefString(&settings.cache_path).FromASCII(src->cef_cache_location);
if (cef_cache_location != NULL) {
CefString(&settings.cache_path).FromASCII(cef_cache_location);
}

g_free(base_path);
Expand Down Expand Up @@ -847,7 +890,7 @@ gst_cef_src_change_state(GstElement *src, GstStateChange transition)
}
#else
/* in a separate UI thread */
thread = g_thread_new("cef-ui-thread", (GThreadFunc) run_cef, (GstCefSrc*)src);
thread = g_thread_new("cef-ui-thread", (GThreadFunc) init_cef, (GstCefSrc*)src);
while (cef_status == CEF_STATUS_INITIALIZING)
g_cond_wait (&init_cond, &init_lock);
#endif
Expand Down Expand Up @@ -1120,22 +1163,42 @@ gst_cef_src_set_property (GObject * object, guint prop_id, const GValue * value,
break;
}
case PROP_CHROME_EXTRA_FLAGS: {
GST_WARNING_OBJECT(
src,
"cefsrc chrome-extra-flags property is deprecated and is global across all cefsrc instances - "
"set GST_CEF_CHROME_EXTRA_FLAGS instead"
);
g_free (src->chrome_extra_flags);
src->chrome_extra_flags = g_value_dup_string (value);
break;
}
case PROP_GPU:
{
GST_WARNING_OBJECT(
src,
"cefsrc gpu property is deprecated and is global across all cefsrc instances - "
"set GST_CEF_GPU_ENABLED instead"
);
src->gpu = g_value_get_boolean (value);
break;
}
case PROP_CHROMIUM_DEBUG_PORT:
{
GST_WARNING_OBJECT(
src,
"cefsrc chromium-debug-port property is deprecated and is global across all cefsrc instances - "
"set GST_CEF_CHROME_EXTRA_FLAGS instead"
);
src->chromium_debug_port = g_value_get_int (value);
break;
}
case PROP_SANDBOX:
{
GST_WARNING_OBJECT(
src,
"cefsrc sandbox property is deprecated and is global across all cefsrc instances - "
"set GST_CEF_SANDBOX instead"
);
src->sandbox = g_value_get_boolean (value);
break;
}
Expand All @@ -1144,16 +1207,34 @@ gst_cef_src_set_property (GObject * object, guint prop_id, const GValue * value,
src->listen_for_js_signals = g_value_get_boolean (value);
break;
}
case PROP_JS_FLAGS: {
case PROP_JS_FLAGS:
{
GST_WARNING_OBJECT(
src,
"cefsrc js-flags property is deprecated and is global across all cefsrc instances - "
"set GST_CEF_JS_FLAGS instead"
);
g_free (src->js_flags);
src->js_flags = g_value_dup_string (value);
break;
}
case PROP_LOG_SEVERITY: {
case PROP_LOG_SEVERITY:
{
GST_WARNING_OBJECT(
src,
"cefsrc log-severity property is deprecated and is global across all cefsrc instances - "
"set GST_CEF_LOG_SEVERITY instead"
);
src->log_severity = (cef_log_severity_t) g_value_get_enum (value);
break;
}
case PROP_CEF_CACHE_LOCATION: {
case PROP_CEF_CACHE_LOCATION:
{
GST_WARNING_OBJECT(
src,
"cefsrc cef-cache-location property is deprecated and is global across all cefsrc instances - "
"set GST_CEF_CACHE_LOCATION instead"
);
g_free (src->cef_cache_location);
src->cef_cache_location = g_value_dup_string (value);
break;
Expand Down Expand Up @@ -1267,24 +1348,27 @@ gst_cef_src_class_init (GstCefSrcClass * klass)

g_object_class_install_property (gobject_class, PROP_GPU,
g_param_spec_boolean ("gpu", "gpu",
"Enable GPU usage in chromium (Improves performance if you have GPU)",
"Enable GPU usage in chromium (Improves performance if you have GPU) - "
"deprecated: set GST_CEF_GPU_ENABLED in the environment instead",
DEFAULT_GPU, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY)));

g_object_class_install_property (gobject_class, PROP_CHROMIUM_DEBUG_PORT,
g_param_spec_int ("chromium-debug-port", "chromium-debug-port",
"Set chromium debug port (-1 = disabled) "
"deprecated: use chrome-extra-flags instead", -1, G_MAXUINT16,
"Set chromium debug port (-1 = disabled) - "
"deprecated: set GST_CEF_CHROME_EXTRA_FLAGS in the environment instead", -1, G_MAXUINT16,
DEFAULT_CHROMIUM_DEBUG_PORT, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY)));

g_object_class_install_property (gobject_class, PROP_CHROME_EXTRA_FLAGS,
g_param_spec_string ("chrome-extra-flags", "chrome-extra-flags",
"Comma delimiter flags to be passed into chrome "
"(Example: show-fps-counter,remote-debugging-port=9222)",
"(Example: show-fps-counter,remote-debugging-port=9222) - "
"deprecated: set GST_CEF_CHROME_EXTRA_FLAGS in the environment instead",
NULL, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY)));

g_object_class_install_property (gobject_class, PROP_SANDBOX,
g_param_spec_boolean ("sandbox", "sandbox",
"Toggle chromium sandboxing capabilities",
"Toggle chromium sandboxing capabilities - "
"deprecated: set GST_CEF_SANDBOX in the environment instead",
DEFAULT_SANDBOX, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY)));
g_object_class_install_property (gobject_class, PROP_LISTEN_FOR_JS_SIGNAL,
g_param_spec_boolean ("listen-for-js-signals", "listen-for-js-signals",
Expand All @@ -1297,19 +1381,22 @@ gst_cef_src_class_init (GstCefSrcClass * klass)
g_object_class_install_property (gobject_class, PROP_JS_FLAGS,
g_param_spec_string ("js-flags", "js-flags",
"Space delimited JavaScript flags to be passed to Chromium "
"(Example: --noexpose_wasm --expose-gc)",
"(Example: --noexpose_wasm --expose-gc) - "
"deprecated: set GST_CEF_JS_FLAGS in the environment instead",
NULL, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY)));

g_object_class_install_property (gobject_class, PROP_LOG_SEVERITY,
g_param_spec_enum ("log-severity", "log-severity",
"CEF log severity level",
"CEF log severity level - "
"deprecated: set GST_CEF_LOG_SEVERITY in the environment instead",
GST_TYPE_CEF_LOG_SEVERITY_MODE, DEFAULT_LOG_SEVERITY,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY)));

g_object_class_install_property (gobject_class, PROP_CEF_CACHE_LOCATION,
g_param_spec_string ("cef-cache-location", "cef-cache-location",
"Cache location for CEF. Defaults to in memory cache. "
"(Example: /tmp/cef-cache/)",
"(Example: /tmp/cef-cache/) - "
"deprecated: set GST_CEF_CACHE_LOCATION in the environment instead",
NULL, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY)));

gst_element_class_set_static_metadata (gstelement_class,
Expand Down
Loading