Skip to content

Commit

Permalink
Merge pull request #3370 from metalefty/x264-threads
Browse files Browse the repository at this point in the history
make x264 threads configurable
  • Loading branch information
metalefty authored Dec 31, 2024
2 parents 4e378c9 + 65a02d0 commit bd56127
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 3 deletions.
12 changes: 10 additions & 2 deletions docs/man/gfx.toml.5.in
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ for use with xrdp. Here are available options:

.TP
\fBprofile\fR = \fI<string>\fR
Select a profile. Here are available options:
Select a profile. Here are available options:

.B main, baseline, high, high10, high422, high444

Expand All @@ -83,12 +83,20 @@ Set the size of the VBV buffer size in kilobits.

.TP
\fBfps_num\fR = \fI<integer>\fR
Set the fps numerator.
Set the fps numerator.

.TP
\fBfps_den\fR = \fI<integer>\fR
Set the fps denominator.

.TP
\fBthreads = \fI<integer>\fR
Specify how many CPU threads to use for H.264 encoding per screen (0 for
auto). Carefully evaluate this value when configuring it to avoid exhausting
the available threads. For example, if 3 users are connected simultaneously
with dual screens, xrdp may use up to <threads> * 3 * 2 threads for H.264
encoding. Please also note that too many threads can hurt quality.

.SH "OPENH264"

\fB[OpenH264]\fR section defines encoding parameters that will be passed to
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_codec_h264_only.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 24
fps_den = 1
threads = 1

[x264.lan]
[x264.wan]
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_codec_h264_preferred.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 24
fps_den = 1
threads = 1

[x264.lan]
[x264.wan]
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_codec_order_undefined.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 24
fps_den = 1
threads = 1

[x264.lan]
[x264.wan]
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_codec_rfx_only.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 24
fps_den = 1
threads = 1

[x264.lan]
[x264.wan]
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_codec_rfx_preferred.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 24
fps_den = 1
threads = 1

[x264.lan]
[x264.wan]
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_codec_rfx_preferred_odd.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 24
fps_den = 1
threads = 1

[x264.lan]
[x264.wan]
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_h264_encoder_invalid.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 60
fps_den = 1
threads = 1

[x264.lan]
# inherits default
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_h264_encoder_openh264.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 60
fps_den = 1
threads = 1

[x264.lan]
# inherits default
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_h264_encoder_undefined.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 60
fps_den = 1
threads = 1

[x264.lan]
# inherits default
Expand Down
1 change: 1 addition & 0 deletions tests/xrdp/gfx/gfx_h264_encoder_x264.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 60
fps_den = 1
threads = 1

[x264.lan]
# inherits default
Expand Down
1 change: 1 addition & 0 deletions xrdp/gfx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vbv_max_bitrate = 0
vbv_buffer_size = 0
fps_num = 60
fps_den = 1
threads = 1 # recommended: 1 or 2, see `man gfx.toml` for details

[x264.lan]
# inherits default
Expand Down
2 changes: 1 addition & 1 deletion xrdp/xrdp_encoder_x264.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ xrdp_encoder_x264_encode(void *handle, int session, int left, int top,
x264_param_default_preset(&(xe->x264_params),
xg->x264_param[ct].preset,
xg->x264_param[ct].tune);
xe->x264_params.i_threads = 1;
xe->x264_params.i_threads = xg->x264_param[ct].threads;
xe->x264_params.i_width = (width + 15) & ~15;
xe->x264_params.i_height = (height + 15) & ~15;
xe->x264_params.i_fps_num = xg->x264_param[ct].fps_num;
Expand Down
28 changes: 28 additions & 0 deletions xrdp/xrdp_tconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#define X264_DEFAULT_PROFILE "main"
#define X264_DEFAULT_FPS_NUM 24
#define X264_DEFAULT_FPS_DEN 1
#define X264_DEFAULT_THREADS 1 /* not to exhaust CPU threads for 1 user */

const char *
tconfig_codec_order_to_str(
Expand Down Expand Up @@ -334,6 +335,33 @@ tconfig_load_gfx_x264_ct(toml_table_t *tfile, const int connection_type,
param[connection_type].fps_den = X264_DEFAULT_FPS_DEN;
}

/* threads */
datum = toml_int_in(x264_ct, "threads");
if (datum.ok)
{
if (datum.u.i >= 0)
{
param[connection_type].threads = datum.u.i;
}
else
{
TCLOG(LOG_LEVEL_WARNING,
"[x264.%s] an invalid value (< 0) is specified for threads, "
"adopting the default value [%d]",
rdpbcgr_connection_type_names[connection_type],
X264_DEFAULT_THREADS);
param[connection_type].threads = X264_DEFAULT_THREADS;
}
}
else if (connection_type == 0)
{
TCLOG(LOG_LEVEL_WARNING,
"[x264.%s] threads is not set, adopting the default value [%d]",
rdpbcgr_connection_type_names[connection_type],
X264_DEFAULT_THREADS);
param[connection_type].threads = X264_DEFAULT_THREADS;
}

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions xrdp/xrdp_tconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct xrdp_tconfig_gfx_x264_param
int vbv_buffer_size;
int fps_num;
int fps_den;
int threads;
};

struct xrdp_tconfig_gfx_openh264_param
Expand Down

0 comments on commit bd56127

Please sign in to comment.