Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1
5#pragma once
6
7#ifndef __kernel_entry
8 #define __kernel_entry
9#endif
10
11#ifndef BOOST_PROCESS_VERSION
12 #define BOOST_PROCESS_VERSION 1
13#endif
14
15// standard includes
16#include <optional>
17#include <string>
18#include <string_view>
19#include <unordered_map>
20#include <vector>
21
22// lib includes
23#include <boost/process/v1/child.hpp>
24#include <boost/process/v1/group.hpp>
25#include <boost/process/v1/environment.hpp>
26#include <boost/process/v1/search_path.hpp>
27#include <boost/property_tree/ptree.hpp>
28#include <nlohmann/json.hpp>
29
30// local includes
31#include "config.h"
32#include "platform/common.h"
33#include "rtsp.h"
34#include "utility.h"
35
37
38#define VIRTUAL_DISPLAY_UUID "8902CB19-674A-403D-A587-41B092E900BA"
39#define FALLBACK_DESKTOP_UUID "EAAC6159-089A-46A9-9E24-6436885F6610"
40#define REMOTE_INPUT_UUID "8CB5C136-DA67-4F99-B4A1-F9CD35005CF4"
41#define TERMINATE_APP_UUID "E16CBE1B-295D-4632-9A76-EC4180C857D3"
42#define DETACHED_DESKTOP_UUID "5B1C7E0A-3D52-4F8E-9A61-2E7D0C4B9F13"
43#define DETACHED_STEAM_UUID "A4E2F6C8-1B37-4D95-8C0E-7F3A9D2B6E51"
44
45namespace proc {
46 using file_t = util::safe_ptr_v2<FILE, int, fclose>;
47
48 extern VDISPLAY::DRIVER_STATUS vDisplayDriverStatus;
49
50 typedef config::prep_cmd_t cmd_t;
51
58 enum class session_route_e {
59 shared,
60 isolated_application,
61 isolated_desktop,
62 };
63
76 struct ctx_t {
77 std::vector<cmd_t> prep_cmds;
78 std::vector<cmd_t> state_cmds;
79
92 std::vector<std::string> detached;
93
94 std::string idx;
95 std::string uuid;
96 std::string name;
97 std::string cmd;
98 std::string working_dir;
99 std::string output;
100 std::string image_path;
101 std::string id;
102 std::string gamepad;
103 bool elevated;
104 bool auto_detach;
105 bool wait_all;
106 bool virtual_display;
112 bool force_scale_factor {false};
113 bool virtual_display_primary;
114 bool use_app_identity;
115 bool per_client_app_identity;
116 bool allow_client_commands;
117 bool terminate_on_pause;
118 bool unlock_host_session;
120 bool stop_detached {true};
122 bool limit_fps {false};
123 // default: follow the global isolation_method when a virtual display is requested.
124 // gamescope/kwin_virtual: always run in that isolated session.
125 std::string isolation_method;
126 // Never isolate, whatever is requested.
127 bool host_session {false};
128 // Isolated sessions only: how long the compositor has to put its first
129 // frame on the session's card. Zero picks the profile's default.
130 std::chrono::seconds first_frame_timeout {0};
131 // auto: extend the virtual display beside the physical outputs and follow
132 // the global exclusive option. extend/detached/mirror/exclusive: force that
133 // layout for this app's session, overriding the global option.
134 std::string virtual_display_layout;
135 // Isolated application sessions only. auto: on when the command's program
136 // is steam. on: gamescope's Steam mode (-e) with games on their own
137 // Xwayland. off: plain gamescope with one Xwayland.
138 std::string gamescope_steam_mode;
139 // Isolated application sessions only. Replaces the default optional
140 // gamescope flags when not empty; Hermes still sets backend, card and mode.
141 std::string gamescope_args;
142 // Isolated sessions only, auto/on/off. A private /dev without the host's
143 // input, USB, camera and sound devices; and no /dev/snd. auto: when Hermes
144 // can (capabilities), on: refuse to start without it.
145 std::string isolate_devices;
146 std::string isolate_sound;
147 // Isolated sessions only, auto/on/off. Shut down a Steam client running
148 // outside Hermes before the session starts; auto: when cmd runs steam.
149 std::string stop_desktop_steam;
150 int scale_factor;
151 std::chrono::seconds exit_timeout;
152 };
153
162 const ctx_t &app,
163 const rtsp_stream::launch_session_t &launch_session,
164 bool headless_mode
165 );
166
174
176 bool use;
178 };
179
189 const ctx_t &app,
190 const rtsp_stream::launch_session_t &launch_session,
191 bool headless_mode
192 );
193
195 std::string_view describe(virtual_display_source_e source);
196
204
206 int percent;
208 };
209
216 const ctx_t &app,
217 const rtsp_stream::launch_session_t &launch_session,
218 int host_default
219 );
220
221 std::string_view describe(scale_factor_source_e source);
222
231 const ctx_t &app,
232 bool isolated_sessions_enabled,
233 bool virtual_display_requested,
234 std::string_view global_method = "gamescope"
235 );
236
238 void migration_v7(nlohmann::json &fileTree);
239
240 class proc_t {
241 public:
242 KITTY_DEFAULT_CONSTR_MOVE_THROW(proc_t)
243
244 std::string display_name;
245 std::string initial_display;
246 std::string mode_changed_display;
247 bool initial_hdr = false;
248 bool virtual_display = false;
249 bool allow_client_commands = false;
250
251 proc_t(
252 boost::process::v1::environment &&env,
253 std::vector<ctx_t> &&apps
254 ):
255 _env(std::move(env)),
256 _apps(std::move(apps)) {
257 }
258
259 void launch_input_only();
260
261 int execute(const ctx_t& _app, std::shared_ptr<rtsp_stream::launch_session_t> launch_session);
262
274 std::shared_ptr<rtsp_stream::launch_session_t> launch_session,
275 bool apply_scale = true,
276 const ctx_t *session_app = nullptr,
277 std::optional<uid_t> session_owner_uid = std::nullopt,
278 bool isolated_session = false
279 );
280
286 const ctx_t &app,
287 std::shared_ptr<rtsp_stream::launch_session_t> launch_session
288 );
289
291 void terminate_isolated(uint32_t launch_session_id);
292
294 void disconnect_isolated(uint32_t launch_session_id);
295
297 void terminate_isolated_client(const std::string &client_uuid);
298
300 bool isolated_client_present(const std::string &client_uuid);
301
304
316 uint32_t launch_session_id {};
317 std::string client_uuid;
318 std::string client_name;
319 std::string app_id;
320 std::string app_name;
321 std::string profile;
322 std::string compositor;
323 std::string seat;
324 std::string display;
325 std::string account_user;
326 uint32_t account_uid {};
327 std::string unit;
328 std::string audio_sink;
329 bool running {};
330 int64_t uptime_seconds {};
331 };
332
334 std::vector<isolated_session_t> list_isolated();
335
337 int running_for_client(const std::string &client_uuid);
338 std::string running_app_uuid_for_client(const std::string &client_uuid);
339
341 bool any_running();
342
344 boost::process::v1::environment get_session_env(uint32_t launch_session_id);
345
350 std::string isolated_audio_sink(uint32_t launch_session_id);
351
353 std::string isolated_audio_server(uint32_t launch_session_id);
354
356 std::string isolated_mic_source(uint32_t launch_session_id);
357
362 bool isolated_session_has_account(uint32_t launch_session_id);
363
366 std::shared_ptr<rtsp_stream::launch_session_t> launch_session
367 );
368
372 int running();
373
374 ~proc_t();
375
376 const std::vector<ctx_t> &get_apps() const;
377 std::vector<ctx_t> &get_apps();
378 std::string get_app_image(int app_id);
379 std::string get_last_run_app_name();
380 std::string get_running_app_uuid();
381 boost::process::v1::environment get_env();
382 void resume();
383 void pause();
384 void terminate(bool immediate = false, bool needs_refresh = true);
385
386 private:
387 int _app_id = 0;
388 std::string _app_name;
389
390 boost::process::v1::environment _env;
391
392 std::shared_ptr<rtsp_stream::launch_session_t> _launch_session;
393 std::shared_ptr<config::input_t> _saved_input_config;
394
395 std::vector<ctx_t> _apps;
396 ctx_t _app;
397 std::chrono::steady_clock::time_point _app_launch_time;
398
399 // If no command associated with _app_id, yet it's still running
400 bool placebo {};
402 std::vector<std::pair<std::string, std::optional<std::string>>> _frame_limit_restore;
404 bool _reopen_desktop_steam {};
405 bool _desktop_steam_flatpak {};
406
407 boost::process::v1::child _process;
408 boost::process::v1::group _process_group;
410 std::vector<std::string> _detached_units;
411
412 file_t _pipe;
413 std::vector<cmd_t>::const_iterator _app_prep_it;
414 std::vector<cmd_t>::const_iterator _app_prep_begin;
415 };
416
417 boost::filesystem::path
418 find_working_directory(const std::string &cmd, const boost::process::v1::environment &env);
419
424 std::tuple<std::string, std::string> calculate_app_id(const std::string &app_name, std::string app_image_path, int index);
425
426 std::string validate_app_image_path(std::string app_image_path);
427 void refresh(const std::string &file_name, bool needs_terminate = true);
428 void migrate_apps(nlohmann::json* fileTree_p, nlohmann::json* inputTree_p);
438 std::optional<proc::proc_t> parse(const std::string &file_name, bool fallback_on_failure = true);
439
444 std::unique_ptr<platf::deinit_t> init();
445
450 void initVDisplayDriver();
451
458 void terminate_process_group(boost::process::v1::child &proc, boost::process::v1::group &group, std::chrono::seconds exit_timeout);
459
460 extern proc_t proc;
461
462 extern int input_only_app_id;
463 extern std::string input_only_app_id_str;
464 extern int terminate_app_id;
465 extern std::string terminate_app_id_str;
466} // namespace proc
467
468#ifdef BOOST_PROCESS_VERSION
469 #undef BOOST_PROCESS_VERSION
470#endif
int execute_isolated(const ctx_t &app, std::shared_ptr< rtsp_stream::launch_session_t > launch_session)
Definition process.cpp:1865
int running_for_client(const std::string &client_uuid)
Definition process.cpp:3093
void terminate_isolated_client(const std::string &client_uuid)
Definition process.cpp:3051
std::string isolated_audio_sink(uint32_t launch_session_id)
The sink an isolated session plays into, or empty for one that shares the host's.
Definition process.cpp:3261
bool isolated_client_present(const std::string &client_uuid)
Definition process.cpp:3068
std::string isolated_audio_server(uint32_t launch_session_id)
Definition process.cpp:3273
std::string isolated_mic_source(uint32_t launch_session_id)
Definition process.cpp:3282
bool isolated_session_has_account(uint32_t launch_session_id)
Whether an isolated session runs under a helper account of its own, whose audio server the host's can...
Definition process.cpp:3291
void terminate_isolated(uint32_t launch_session_id)
Definition process.cpp:3019
void disconnect_isolated(uint32_t launch_session_id)
Definition process.cpp:3033
int prepare_session_virtual_display(std::shared_ptr< rtsp_stream::launch_session_t > launch_session, bool apply_scale=true, const ctx_t *session_app=nullptr, std::optional< uid_t > session_owner_uid=std::nullopt, bool isolated_session=false)
Definition process.cpp:1729
bool any_running()
Definition process.cpp:3134
boost::process::v1::environment get_session_env(uint32_t launch_session_id)
Definition process.cpp:3180
bool prepare_isolated_resume(std::shared_ptr< rtsp_stream::launch_session_t > launch_session)
Definition process.cpp:3297
std::vector< isolated_session_t > list_isolated()
Definition process.cpp:3189
void terminate_all_isolated()
Definition process.cpp:3074
Declarations for common platform specific utilities.
Declarations for the configuration of Sunshine.
session_route_e resolve_session_route(const ctx_t &app, bool isolated_sessions_enabled, bool virtual_display_requested, std::string_view global_method)
Definition process.cpp:179
void migrate_apps(nlohmann::json *fileTree_p, nlohmann::json *inputTree_p)
Migrate the applications stored in the file tree by merging in a new app.
Definition process.cpp:4521
std::string_view describe(virtual_display_source_e source)
Definition process.cpp:126
bool launch_requests_virtual_display(const ctx_t &app, const rtsp_stream::launch_session_t &launch_session, bool headless_mode)
Definition process.cpp:140
void migration_v7(nlohmann::json &fileTree)
Definition process.cpp:4796
void terminate_process_group(boost::process::v1::child &proc, boost::process::v1::group &group, std::chrono::seconds exit_timeout)
Terminates all child processes in a process group.
Definition process.cpp:1611
std::unique_ptr< platf::deinit_t > init()
Initialize proc functions.
Definition process.cpp:1601
virtual_display_decision_t decide_virtual_display(const ctx_t &app, const rtsp_stream::launch_session_t &launch_session, bool headless_mode)
Definition process.cpp:109
std::tuple< std::string, std::string > calculate_app_id(const std::string &app_name, std::string app_image_path, int index)
Calculate a stable id based on name and image data.
Definition process.cpp:4470
std::optional< proc::proc_t > parse(const std::string &file_name, bool fallback_on_failure)
Load the applications from apps.json.
Definition process.cpp:4864
void initVDisplayDriver()
Initialize the virtual display driver. This should be called early in startup to enable virtual displ...
Definition process.cpp:1580
scale_factor_decision_t decide_scale_factor(const ctx_t &app, const rtsp_stream::launch_session_t &launch_session, int host_default)
Definition process.cpp:148
scale_factor_source_e
Definition process.h:198
@ host_default
Neither did; the host's default_scale_factor.
Definition process.h:202
@ app
The client sent none; the app's own scale.
Definition process.h:201
virtual_display_source_e
Definition process.h:168
@ app_forced
The app forces its setting on every client.
Definition process.h:170
@ default_rule
No explicit choice: client request, per-client or app setting.
Definition process.h:172
session_route_e
Definition process.h:58
Declarations for RTSP streaming.
bool limit_fps
Cap the application's frame rate at the stream's (DXVK, VKD3D-Proton, MangoHud).
Definition process.h:122
bool force_virtual_display
Definition process.h:109
bool stop_detached
Stop the app's detached commands, and all they started, when the app ends.
Definition process.h:120
std::vector< std::string > detached
Definition process.h:92
bool force_scale_factor
Definition process.h:112
One live isolated session, as the host needs to see it.
Definition process.h:315
std::string unit
The systemd unit, where the session has one.
Definition process.h:327
std::string profile
"desktop" or "application".
Definition process.h:321
bool running
False for one whose compositor has died but was not reaped.
Definition process.h:329
std::string account_user
Empty where the session runs as the Hermes user.
Definition process.h:325
std::string audio_sink
Empty where the session shares the host's audio.
Definition process.h:328
Declarations for utility functions.
Virtual display declarations for Linux.
DRIVER_STATUS
Status of the virtual display driver.