Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <bitset>
9#include <chrono>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <unordered_map>
14#include <vector>
15
16// local includes
17#include "nvenc/nvenc_config.h"
18#include "string_hash.h"
19
20namespace config {
24 using config_map_t = std::unordered_map<std::string, std::string, util::transparent_string_hash, std::equal_to<>>;
25
26 namespace nv {
33 std::string ffmpeg_preset_from_quality(int quality_preset);
34 } // namespace nv
35
36 // Valid range for the packetsize limit
37 constexpr int PACKETSIZE_MIN = 200;
38 constexpr int PACKETSIZE_MAX = 65535;
39 constexpr int PACKETSIZE_SMALL = 500;
40 constexpr int PACKETSIZE_LARGE = 1456;
41
42 inline constexpr std::string_view GAMEPAD_DRIVER_ALL = "all";
43 inline constexpr std::string_view GAMEPAD_DRIVER_VIRTUALHID = "virtualhid";
44 inline constexpr std::string_view GAMEPAD_DRIVER_VIGEMBUS = "vigembus";
45 inline constexpr std::string_view GAMEPAD_DRIVER_NONE = "none";
46
47 // track modified config options
49
50 // sensitive values that should be redacted from logging
54 inline constexpr std::array redacted_config = {
55 "csrf_allowed_origins"
56 };
57
64 void log_config_settings(const config_map_t &vars, bool save);
65
69 struct video_t {
70 // ffmpeg params
74 int qp; // higher == more compression and less quality
75
78
80
81 struct {
82 std::string sw_preset;
83 std::string sw_tune;
84 std::optional<int> svtav1_preset;
85 } sw;
86
91
92 struct {
93 std::string preset;
94 int multipass;
95 int h264_coder;
96 int spatial_aq;
99
100 struct {
101 std::optional<int> qsv_preset;
102 std::optional<int> qsv_cavlc;
103 bool qsv_slow_hevc;
104 } qsv;
105
106 struct {
107 std::optional<int> amd_usage_h264;
108 std::optional<int> amd_usage_hevc;
109 std::optional<int> amd_usage_av1;
110 std::optional<int> amd_rc_h264;
111 std::optional<int> amd_rc_hevc;
112 std::optional<int> amd_rc_av1;
113 std::optional<int> amd_enforce_hrd;
114 std::optional<int> amd_quality_h264;
115 std::optional<int> amd_quality_hevc;
116 std::optional<int> amd_quality_av1;
117 std::optional<int> amd_preanalysis;
118 std::optional<int> amd_vbaq;
119 std::optional<int> amd_max_au_size;
120 int amd_coder;
121 } amd;
122
123 struct {
124 int vt_allow_sw;
125 int vt_require_sw;
126 int vt_realtime;
127 int vt_coder;
128 } vt;
129
130 struct {
131 std::optional<int> blbrc;
132 std::optional<int> vaapi_quality;
133 std::optional<int> vaapi_rc;
134 std::string vaapi_rc_str;
135 bool strict_rc_buffer;
136 } vaapi;
137
138 struct {
139 int tune;
140 int rc_mode;
141 int quality;
142 } vk;
143
144 std::string capture;
146 std::string encoder;
147 std::string adapter_name;
148 std::string output_name;
149
153 struct dd_t {
158 std::chrono::milliseconds hdr_toggle_delay;
159 };
160
164 enum class config_option_e {
165 disabled,
166 verify_only,
167 ensure_active,
168 ensure_primary,
169 ensure_only_display
170 };
171
176 disabled,
177 automatic,
178 manual
179 };
180
185 disabled,
186 automatic,
187 manual
188 };
189
193 enum class hdr_option_e {
194 disabled,
195 automatic
196 };
197
203 std::string requested_fps;
204 std::string final_resolution;
205 std::string final_refresh_rate;
206 };
207
212 std::vector<mode_remapping_entry_t> mixed;
213 std::vector<mode_remapping_entry_t> resolution_only;
214 std::vector<mode_remapping_entry_t> refresh_rate_only;
215 };
216
219 std::string manual_resolution;
223 std::chrono::milliseconds config_revert_delay;
227 };
228
230
233 };
234
238 struct audio_t {
239 std::string sink;
240 std::string virtual_sink;
241 bool stream;
244 std::string format {"opus"};
245 bool high_quality_audio {false};
246 };
247
251 constexpr int ENCRYPTION_MODE_NEVER = 0; // Never use video encryption, even if the client supports it
255 constexpr int ENCRYPTION_MODE_OPPORTUNISTIC = 1; // Use video encryption if available, but stream without it if not supported
259 constexpr int ENCRYPTION_MODE_MANDATORY = 2; // Always use video encryption and refuse clients that can't encrypt
260
264 struct stream_t {
265 std::chrono::milliseconds ping_timeout;
266
267 std::string file_apps;
268
270
271 // Video encryption settings for LAN and WAN streams
274
275 // Limit the packetsize to avoid fragmentation on a low MTU link
277 };
278
282 struct nvhttp_t {
283 // Could be any of the following values:
284 // pc|lan|wan
286
287 std::string pkey;
288 std::string cert;
289
290 std::string sunshine_name;
291
292 std::string file_state;
293
294 std::string external_ip;
295 };
296
300 struct input_t {
301 std::unordered_map<int, int> keybindings;
302
303 std::chrono::milliseconds back_button_timeout;
304 std::chrono::milliseconds key_repeat_delay;
305 std::chrono::duration<double> key_repeat_period;
306
307 std::string gamepad;
308 std::string gamepad_driver;
313
314 bool keyboard;
316 bool mouse;
318
320
323 };
324
325 namespace flag {
337 } // namespace flag
338
342 struct prep_cmd_t {
350 prep_cmd_t(std::string &&do_cmd, std::string &&undo_cmd, bool &&elevated):
351 do_cmd(std::move(do_cmd)),
352 undo_cmd(std::move(undo_cmd)),
353 elevated(std::move(elevated)) {
354 }
355
362 explicit prep_cmd_t(std::string &&do_cmd, bool &&elevated):
363 do_cmd(std::move(do_cmd)),
364 elevated(std::move(elevated)) {
365 }
366
367 std::string do_cmd;
368 std::string undo_cmd;
369 bool elevated;
370 };
371
375 struct sunshine_t {
376 std::string locale;
378 std::bitset<flag::FLAG_SIZE> flags;
379 std::string credentials_file;
380
381 std::string username;
382 std::string password;
383 std::string salt;
384
385 std::string config_file;
386
390 struct cmd_t {
391 std::string name;
392 int argc;
393 char **argv;
394 };
395
397
398 std::uint16_t port;
399 std::string address_family;
400 std::string bind_address;
401
402 std::string log_file;
405 std::vector<prep_cmd_t> prep_cmds;
406
407 // List of allowed origins for CSRF protection (e.g., "https://example.com,https://app.example.com")
408 // Comma-separated list of additional origins. Default includes localhost variants and web UI port.
409 std::vector<std::string> csrf_allowed_origins;
410 };
411
412 extern video_t video;
413 extern audio_t audio;
414 extern stream_t stream;
415 extern nvhttp_t nvhttp;
416 extern input_t input;
417 extern sunshine_t sunshine;
418
419#ifdef SUNSHINE_TESTS
425 void apply_config_for_test(std::string_view file_content);
426#endif
427
435 int parse(int argc, char *argv[]);
442 config_map_t parse_config(const std::string_view &file_content);
443
454 bool persist_config_option_if_missing(std::string_view name, std::string_view value);
455
462 bool select_all_gamepad_drivers_if_licensed(bool virtualhid_licensed);
463} // namespace config
bool select_all_gamepad_drivers_if_licensed(const bool virtualhid_licensed)
Select all available gamepad drivers when a licensed user has not made a choice.
Definition config.cpp:1068
stream_t stream
Default stream configuration values used before file and CLI overrides.
Definition config.cpp:819
config_map_t parse_config(const std::string_view &file_content)
Parse Sunshine configuration text into key-value entries.
Definition config.cpp:1024
input_t input
Default input configuration values used before file and CLI overrides.
Definition config.cpp:848
int parse(int argc, char *argv[])
Parse serialized text into the corresponding runtime representation.
Definition config.cpp:1977
video_t video
Default video configuration values used before file and CLI overrides.
Definition config.cpp:719
sunshine_t sunshine
Default top-level Sunshine configuration values used before file and CLI overrides.
Definition config.cpp:877
audio_t audio
Default audio configuration values used before file and CLI overrides.
Definition config.cpp:808
bool persist_config_option_if_missing(const std::string_view name, const std::string_view value)
Persist a configuration option when the active file does not already define it.
Definition config.cpp:1049
constexpr int PACKETSIZE_MAX
Highest accepted configured packet size in bytes.
Definition config.h:38
std::unordered_map< std::string, std::string, util::transparent_string_hash, std::equal_to<> > config_map_t
Owning configuration entries supporting lookup by string view or C string.
Definition config.h:24
config_map_t modified_config_settings
Configuration keys changed during the current parse or UI update.
Definition config.h:48
constexpr int ENCRYPTION_MODE_OPPORTUNISTIC
Encryption policy that uses encrypted video only when the client supports it.
Definition config.h:255
flag_e
Enumerates supported flag options.
Definition config.h:329
@ UPNP
Try Universal Plug 'n Play.
Definition config.h:333
@ FRESH_STATE
Do not load or save state.
Definition config.h:331
@ FLAG_SIZE
Number of flags.
Definition config.h:335
@ PIN_STDIN
Read PIN from stdin instead of http.
Definition config.h:330
@ FORCE_VIDEO_HEADER_REPLACE
force replacing headers inside video data
Definition config.h:332
@ CONST_PIN
Use "universal" pin.
Definition config.h:334
constexpr std::array redacted_config
Configuration keys whose values must be hidden in logs.
Definition config.h:54
constexpr int PACKETSIZE_LARGE
Default large packet size that avoids common MTU fragmentation.
Definition config.h:40
constexpr std::string_view GAMEPAD_DRIVER_VIRTUALHID
Use Virtual HID Broker for gamepads on Windows or macOS.
Definition config.h:43
constexpr int PACKETSIZE_MIN
Lowest accepted configured packet size in bytes.
Definition config.h:37
constexpr int ENCRYPTION_MODE_MANDATORY
Encryption policy that rejects clients without video encryption support.
Definition config.h:259
constexpr int ENCRYPTION_MODE_NEVER
Encryption policy that always sends unencrypted video.
Definition config.h:251
constexpr std::string_view GAMEPAD_DRIVER_VIGEMBUS
Allow only ViGEmBus for Windows gamepads.
Definition config.h:44
constexpr std::string_view GAMEPAD_DRIVER_NONE
Disable virtual gamepads on Windows and macOS.
Definition config.h:45
constexpr int PACKETSIZE_SMALL
Conservative packet size used for low-MTU links.
Definition config.h:39
constexpr std::string_view GAMEPAD_DRIVER_ALL
Allow every available Windows virtual gamepad driver.
Definition config.h:42
Contains all the functions and variables related to the nvhttp (GameStream) server.
Definition nvhttp.cpp:46
Handles the system tray icon and notification system.
Definition system_tray.cpp:90
Declarations for NVENC encoder configuration.
Transparent hashing for containers with owning string keys.
Audio capture and encoder settings loaded from configuration.
Definition config.h:238
bool external_audio
Windows capture-only mode; an external router owns endpoint defaults and formats.
Definition config.h:243
bool stream
Enable audio streaming to clients.
Definition config.h:241
bool install_steam_drivers
Install Steam audio drivers for enhanced compatibility.
Definition config.h:242
std::string sink
Audio output device/sink to use for audio capture.
Definition config.h:239
std::string virtual_sink
Virtual audio sink for audio routing.
Definition config.h:240
std::string format
Encoded audio format: Opus, AC-3, or E-AC-3.
Definition config.h:244
bool high_quality_audio
Use higher bitrates for negotiated Dolby streams; ignored for Opus.
Definition config.h:245
Input emulation settings loaded from configuration.
Definition config.h:300
bool mouse
Enable mouse input from clients.
Definition config.h:316
bool virtualhid_randomize_mac
Randomize the libvirtualhid virtual controller MAC address.
Definition config.h:312
bool touchpad_as_ds4
Prefer PlayStation-style emulation for client gamepads with touchpad input.
Definition config.h:311
bool keyboard
Enable keyboard input from clients.
Definition config.h:314
std::unordered_map< int, int > keybindings
Client keycode to platform keycode bindings.
Definition config.h:301
bool high_resolution_scrolling
Enable high-resolution mouse-wheel events.
Definition config.h:321
std::string gamepad_driver
Virtual gamepad backend policy on Windows and macOS.
Definition config.h:308
std::chrono::milliseconds key_repeat_delay
Delay before repeating a held keyboard key.
Definition config.h:304
bool key_rightalt_to_key_win
Map the client Right Alt key to the Windows key.
Definition config.h:315
bool controller
Enable controller input from clients.
Definition config.h:317
bool always_send_scancodes
Always send keyboard scancodes when available.
Definition config.h:319
std::string gamepad
Virtual controller profile selected by configuration.
Definition config.h:307
std::chrono::duration< double > key_repeat_period
Interval between repeated keyboard key events.
Definition config.h:305
bool native_pen_touch
Enable native pen and touch injection.
Definition config.h:322
bool motion_as_ds4
Prefer PlayStation-style emulation for client gamepads with motion controls.
Definition config.h:310
bool ds4_back_as_touchpad_click
Map Back/Select to touchpad click for PlayStation-style gamepads.
Definition config.h:309
std::chrono::milliseconds back_button_timeout
Hold duration that turns a controller Back button into a special action.
Definition config.h:303
HTTP and HTTPS settings used by the GameStream pairing server.
Definition config.h:282
std::string file_state
Path to the persisted Sunshine state file.
Definition config.h:292
std::string external_ip
External address advertised to clients when configured.
Definition config.h:294
std::string cert
Certificate PEM string or path.
Definition config.h:288
std::string pkey
Private key PEM string or path.
Definition config.h:287
std::string sunshine_name
Host name advertised to Moonlight clients.
Definition config.h:290
std::string origin_web_ui_allowed
Origin policy used for Web UI access checks.
Definition config.h:285
prep_cmd_t(std::string &&do_cmd, bool &&elevated)
Build a preparation command entry from parsed configuration data.
Definition config.h:362
prep_cmd_t(std::string &&do_cmd, std::string &&undo_cmd, bool &&elevated)
Build a preparation command entry from parsed configuration data.
Definition config.h:350
std::string undo_cmd
Command to run after the application exits.
Definition config.h:368
std::string do_cmd
Command to run before the application starts.
Definition config.h:367
bool elevated
Whether the process should be launched elevated.
Definition config.h:369
Network stream settings shared by audio, video, and control channels.
Definition config.h:264
int lan_encryption_mode
Video encryption policy for LAN clients.
Definition config.h:272
std::string file_apps
Path to the configured applications file.
Definition config.h:267
int wan_encryption_mode
Video encryption policy for WAN clients.
Definition config.h:273
int fec_percentage
Percentage of forward-error-correction packets to add to the stream (0-100); 0 disables FEC.
Definition config.h:269
int packetsize
Maximum payload size for network packets.
Definition config.h:276
std::chrono::milliseconds ping_timeout
Timeout used when waiting for client ping responses.
Definition config.h:265
Command-line options parsed before configuration loading.
Definition config.h:390
char ** argv
Command-line argument vector.
Definition config.h:393
int argc
Number of command-line arguments.
Definition config.h:392
std::string name
Executable name from the command line.
Definition config.h:391
Top-level Sunshine configuration and credential state.
Definition config.h:375
std::string salt
Salt used when hashing the Web UI password.
Definition config.h:383
std::string address_family
Address family requested for listening sockets.
Definition config.h:399
std::string password
Password hash or secret for the local Web UI account.
Definition config.h:382
std::string config_file
Path to the active Sunshine configuration file.
Definition config.h:385
std::uint16_t port
TCP port used by Sunshine services.
Definition config.h:398
std::string credentials_file
Path to the stored pairing credentials file.
Definition config.h:379
std::string locale
Locale selected for Sunshine UI and log messages.
Definition config.h:376
std::vector< prep_cmd_t > prep_cmds
Preparation commands executed around application launch.
Definition config.h:405
std::string log_file
Path to the configured log file.
Definition config.h:402
std::string username
Username for the local Web UI account.
Definition config.h:381
int min_log_level
Minimum severity level written to the configured log sink.
Definition config.h:377
std::bitset< flag::FLAG_SIZE > flags
Runtime flags parsed from command-line options.
Definition config.h:378
bool notify_pre_releases
Notify users about pre-release updates.
Definition config.h:403
cmd_t cmd
Command line used to launch the application.
Definition config.h:396
std::string bind_address
Local address Sunshine should bind to.
Definition config.h:400
std::vector< std::string > csrf_allowed_origins
Additional origins allowed by CSRF validation.
Definition config.h:409
std::optional< int > amd_quality_h264
AMF H.264 quality preset; unset uses the encoder default.
std::optional< int > amd_quality_av1
AMF AV1 quality preset; unset uses the encoder default.
std::optional< int > amd_quality_hevc
AMF HEVC quality preset; unset uses the encoder default.
std::optional< int > amd_rc_av1
AMF AV1 rate-control mode; unset uses the encoder default.
std::optional< int > amd_max_au_size
Maximum AMF H.264/HEVC access unit size in bits; unset uses the encoder default.
int amd_coder
AMF entropy-coding mode.
std::optional< int > amd_rc_hevc
AMF HEVC rate-control mode; unset uses the encoder default.
std::optional< int > amd_usage_hevc
AMF HEVC usage profile; unset uses the encoder default.
std::optional< int > amd_vbaq
AMF variance-based adaptive-quantization setting; unset uses the encoder default.
std::optional< int > amd_usage_h264
AMF H.264 usage profile; unset uses the encoder default.
std::optional< int > amd_enforce_hrd
AMF HRD enforcement setting; unset uses the encoder default.
std::optional< int > amd_rc_h264
AMF H.264 rate-control mode; unset uses the encoder default.
std::optional< int > amd_usage_av1
AMF AV1 usage profile; unset uses the encoder default.
std::optional< int > amd_preanalysis
AMF pre-analysis setting; unset uses the encoder default.
int h264_coder
Legacy NVENC H.264 entropy-coding mode.
int vbv_percentage_increase
Legacy NVENC VBV buffer-size percentage increase.
int spatial_aq
FFmpeg NVENC spatial adaptive-quantization mode.
std::string preset
FFmpeg NVENC preset name shared by supported FFmpeg versions.
int multipass
Legacy NVENC multipass mode.
std::optional< int > qsv_preset
Intel Quick Sync preset; unset uses the encoder default.
bool qsv_slow_hevc
Whether to enable the slower Intel Quick Sync HEVC path.
std::optional< int > qsv_cavlc
Intel Quick Sync CAVLC selection; unset uses the encoder default.
std::optional< int > svtav1_preset
SVT-AV1 preset; unset uses the encoder default.
std::string sw_tune
FFmpeg software-encoder tuning profile.
std::string sw_preset
FFmpeg software-encoder preset.
std::optional< int > blbrc
VA-API block-level bitrate-control setting; unset uses the encoder default.
std::optional< int > vaapi_quality
VA-API quality setting; unset uses the encoder default.
std::optional< int > vaapi_rc
VA-API rate-control mode; unset uses the encoder default.
bool strict_rc_buffer
Whether VA-API must strictly enforce the rate-control buffer.
std::string vaapi_rc_str
Text representation of the selected VA-API rate-control mode.
int tune
Vulkan encoder tuning mode: default, HQ, LL, ULL, or lossless.
int quality
Vulkan encoder quality preset: 1 = speed, 2 = balanced (default), 3 = quality.
int rc_mode
Vulkan encoder rate-control mode: driver, CQP, CBR, or VBR.
int vt_coder
VideoToolbox entropy-coding mode.
int vt_allow_sw
Whether VideoToolbox may use software encoding.
int vt_realtime
Whether VideoToolbox uses realtime encoding mode.
int vt_require_sw
Whether VideoToolbox must use software encoding.
Single display mode remapping rule from configuration.
Definition config.h:201
std::string final_refresh_rate
Refresh-rate string to apply after remapping.
Definition config.h:205
std::string requested_fps
Refresh-rate string requested by the client.
Definition config.h:203
std::string final_resolution
Resolution string to apply after remapping.
Definition config.h:204
std::string requested_resolution
Resolution string requested by the client.
Definition config.h:202
Collection of display mode remapping rules.
Definition config.h:211
std::vector< mode_remapping_entry_t > refresh_rate_only
To be use when only refresh_rate_option is set to automatic.
Definition config.h:214
std::vector< mode_remapping_entry_t > mixed
To be used when resolution_option and refresh_rate_option is set to automatic.
Definition config.h:212
std::vector< mode_remapping_entry_t > resolution_only
To be use when only resolution_option is set to automatic.
Definition config.h:213
Compatibility workarounds for display-device control.
Definition config.h:157
std::chrono::milliseconds hdr_toggle_delay
Specify whether to apply HDR high-contrast color workaround and what delay to use.
Definition config.h:158
Display-device integration settings.
Definition config.h:153
refresh_rate_option_e refresh_rate_option
Refresh-rate selection mode selected by configuration.
Definition config.h:220
std::string manual_resolution
Manual resolution in case resolution_option == resolution_option_e::manual.
Definition config.h:219
refresh_rate_option_e
Selects how Sunshine chooses the stream display refresh rate.
Definition config.h:184
hdr_option_e
Selects how Sunshine handles HDR state for the stream display.
Definition config.h:193
bool config_revert_on_disconnect
Specify whether to revert display configuration on client disconnect.
Definition config.h:224
std::chrono::milliseconds config_revert_delay
Time to wait until settings are reverted (after stream ends/app exists).
Definition config.h:223
std::string manual_refresh_rate
Manual refresh rate in case refresh_rate_option == refresh_rate_option_e::manual.
Definition config.h:221
config_option_e
Selects how Sunshine prepares the active display before streaming.
Definition config.h:164
hdr_option_e hdr_option
HDR-selection mode selected by configuration.
Definition config.h:222
resolution_option_e
Selects how Sunshine chooses the stream display resolution.
Definition config.h:175
mode_remapping_t mode_remapping
Display mode remapping rules grouped by automatic selection mode.
Definition config.h:225
workarounds_t wa
Display-device compatibility workarounds.
Definition config.h:226
config_option_e configuration_option
Display-preparation mode selected by configuration.
Definition config.h:217
resolution_option_e resolution_option
Resolution-selection mode selected by configuration.
Definition config.h:218
Video encoder, capture, and color settings loaded from configuration.
Definition config.h:69
std::string encoder
Encoder backend name selected by configuration.
Definition config.h:146
bool nv_realtime_hags
Enable the NVIDIA realtime HAGS workaround.
Definition config.h:88
std::string capture
Capture backend name selected by configuration.
Definition config.h:144
int qp
Quantization parameter used by encoders where higher values trade quality for compression.
Definition config.h:74
int max_bitrate
Maximum bitrate ceiling in kbps for bitrate requested from the client.
Definition config.h:231
int min_threads
Minimum number of threads or slices for CPU encoding.
Definition config.h:79
std::string adapter_name
Display adapter name selected in configuration.
Definition config.h:147
std::string output_name
Display output name selected in configuration.
Definition config.h:148
struct config::video_t::@274203255166134055042140142125316060346267075326 nv_legacy
Legacy NVIDIA encoder options kept for config compatibility.
double minimum_fps_target
Lowest framerate that will be used when streaming. Range 0-1000, 0 = half of client's requested frame...
Definition config.h:232
bool nv_sunshine_high_power_mode
Request NVIDIA high-power mode for Sunshine.
Definition config.h:90
int av1_mode
AV1 support mode advertised to clients.
Definition config.h:77
nvenc::nvenc_config nv
NVIDIA NVENC encoder settings.
Definition config.h:87
int hevc_mode
HEVC support mode advertised to clients.
Definition config.h:76
dd_t dd
Display-device integration settings.
Definition config.h:229
bool nv_opengl_vulkan_on_dxgi
Prefer NVIDIA OpenGL/Vulkan-on-DXGI interop.
Definition config.h:89
bool fp16_sdr_gamma_encoded
Treat FP16 captures of SDR displays as already sRGB gamma-encoded (Windows).
Definition config.h:145
NVENC encoder configuration.
Definition nvenc_config.h:30