15#include <boost/core/noncopyable.hpp>
16#include <boost/asio/ip/address.hpp>
17#include <boost/process/v1/child.hpp>
18#include <boost/process/v1/group.hpp>
19#include <boost/process/v1/environment.hpp>
29#include <moonlight-common-c/src/Limelight.h>
32using namespace std::literals;
37struct AVHWFramesContext;
51 constexpr auto MAX_GAMEPADS = 16;
55 return "source" + std::string {sink.substr(sink.find(
'-'))};
58 constexpr std::uint32_t DPAD_UP = 0x0001;
59 constexpr std::uint32_t DPAD_DOWN = 0x0002;
60 constexpr std::uint32_t DPAD_LEFT = 0x0004;
61 constexpr std::uint32_t DPAD_RIGHT = 0x0008;
62 constexpr std::uint32_t START = 0x0010;
63 constexpr std::uint32_t BACK = 0x0020;
64 constexpr std::uint32_t LEFT_STICK = 0x0040;
65 constexpr std::uint32_t RIGHT_STICK = 0x0080;
66 constexpr std::uint32_t LEFT_BUTTON = 0x0100;
67 constexpr std::uint32_t RIGHT_BUTTON = 0x0200;
68 constexpr std::uint32_t HOME = 0x0400;
69 constexpr std::uint32_t A = 0x1000;
70 constexpr std::uint32_t B = 0x2000;
71 constexpr std::uint32_t X = 0x4000;
72 constexpr std::uint32_t Y = 0x8000;
73 constexpr std::uint32_t PADDLE1 = 0x010000;
74 constexpr std::uint32_t PADDLE2 = 0x020000;
75 constexpr std::uint32_t PADDLE3 = 0x040000;
76 constexpr std::uint32_t PADDLE4 = 0x080000;
77 constexpr std::uint32_t TOUCHPAD_BUTTON = 0x100000;
78 constexpr std::uint32_t MISC_BUTTON = 0x200000;
83 std::string reason_disabled;
95 static gamepad_feedback_msg_t make_rumble(std::uint16_t
id, std::uint16_t lowfreq, std::uint16_t highfreq) {
99 msg.data.rumble = {lowfreq, highfreq};
103 static gamepad_feedback_msg_t make_rumble_triggers(std::uint16_t
id, std::uint16_t left, std::uint16_t right) {
107 msg.data.rumble_triggers = {left, right};
111 static gamepad_feedback_msg_t make_motion_event_state(std::uint16_t
id, std::uint8_t motion_type, std::uint16_t report_rate) {
115 msg.data.motion_event_state.motion_type = motion_type;
116 msg.data.motion_event_state.report_rate = report_rate;
120 static gamepad_feedback_msg_t make_rgb_led(std::uint16_t
id, std::uint8_t r, std::uint8_t g, std::uint8_t b) {
124 msg.data.rgb_led = {r, g, b};
128 static gamepad_feedback_msg_t make_adaptive_triggers(std::uint16_t
id, uint8_t event_flags, uint8_t type_left, uint8_t type_right,
const std::array<uint8_t, 10> &left,
const std::array<uint8_t, 10> &right) {
132 msg.data.adaptive_triggers = {.event_flags = event_flags, .type_left = type_left, .type_right = type_right, .left = left, .right = right};
141 std::uint16_t lowfreq;
142 std::uint16_t highfreq;
146 std::uint16_t left_trigger;
147 std::uint16_t right_trigger;
151 std::uint16_t report_rate;
152 std::uint8_t motion_type;
153 } motion_event_state;
162 uint16_t controllerNumber;
166 std::array<uint8_t, 10> left;
167 std::array<uint8_t, 10> right;
172 using feedback_queue_t = safe::mail_raw_t::queue_t<gamepad_feedback_msg_t>;
187 constexpr std::uint8_t map_stereo[] {
191 constexpr std::uint8_t map_surround51[] {
199 constexpr std::uint8_t map_surround71[] {
229 inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
230 using namespace std::literals;
251 int offset_x, offset_y;
256 namespace platform_caps {
257 typedef uint32_t caps_t;
259 constexpr caps_t pen_touch = 0x01;
260 constexpr caps_t controller_touch = 0x02;
264 std::uint32_t buttonFlags;
281 std::uint8_t clientRelativeIndex;
286 std::uint16_t capabilities;
287 std::uint32_t supportedButtons;
293 std::uint8_t eventType;
294 std::uint32_t pointerId;
302 std::uint8_t motionType;
314 std::uint8_t percentage;
318 std::uint8_t eventType;
319 std::uint16_t rotation;
320 std::uint32_t pointerId;
323 float pressureOrDistance;
324 float contactAreaMajor;
325 float contactAreaMinor;
329 std::uint8_t eventType;
330 std::uint8_t toolType;
331 std::uint8_t penButtons;
333 std::uint16_t rotation;
336 float pressureOrDistance;
337 float contactAreaMajor;
338 float contactAreaMinor;
346 struct img_t: std::enable_shared_from_this<img_t> {
350 img_t(img_t &&) =
delete;
351 img_t(
const img_t &) =
delete;
352 img_t &operator=(img_t &&) =
delete;
353 img_t &operator=(
const img_t &) =
delete;
355 std::uint8_t *data {};
356 std::int32_t width {};
357 std::int32_t height {};
358 std::int32_t pixel_pitch {};
359 std::int32_t row_pitch {};
361 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
363 virtual ~img_t() =
default;
373 std::string surround51;
374 std::string surround71;
377 std::optional<null_t> null;
396 virtual void apply_colorspace() {
403 virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx) {
404 BOOST_LOG(error) <<
"Illegal call to hwdevice_t::set_frame(). Did you forget to override it?";
463 display_t() noexcept:
482 virtual std::shared_ptr<img_t> alloc_img() = 0;
484 virtual int dummy_img(
img_t *img) = 0;
486 virtual std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e pix_fmt) {
490 virtual std::unique_ptr<nvenc_encode_device_t> make_nvenc_encode_device(
pix_fmt_e pix_fmt) {
504 virtual bool is_hdr() {
519 virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata) {
520 std::memset(&metadata, 0,
sizeof(metadata));
539 int offset_x, offset_y;
540 int env_width, env_height;
551 virtual capture_e sample(std::vector<float> &frame_buffer) = 0;
553 virtual ~mic_t() =
default;
560 virtual void write(
const std::int16_t *samples,
int count) = 0;
567 virtual int set_sink(
const std::string &sink) = 0;
569 virtual std::unique_ptr<mic_t> microphone(
const std::uint8_t *mapping,
int channels, std::uint32_t sample_rate, std::uint32_t frame_size) = 0;
584 virtual std::unique_ptr<mic_t>
microphone_from([[maybe_unused]]
const std::string &sink,
const std::uint8_t *mapping,
int channels, std::uint32_t sample_rate, std::uint32_t frame_size) {
585 return microphone(mapping, channels, sample_rate, frame_size);
609 virtual std::optional<sink_t> sink_info() = 0;
629 [[maybe_unused]]
const std::uint8_t *mapping,
630 [[maybe_unused]]
int channels) {
643 void freeInput(
void *);
645 using input_t = util::safe_ptr<void, freeInput>;
647 std::filesystem::path appdata();
656 std::string get_mac_address(
const std::string_view &address);
658 std::string get_local_ip_for_gateway();
660 std::string from_sockaddr(
const sockaddr *
const);
661 std::pair<std::uint16_t, std::string> from_sockaddr_ex(
const sockaddr *
const);
663 std::unique_ptr<audio_control_t> audio_control();
673 std::shared_ptr<display_t> display(mem_type_e hwdevice_type,
const std::string &display_name,
const video::config_t &config);
680 std::shared_ptr<display_t> encoder_probe_display(mem_type_e hwdevice_type,
const video::config_t &config);
683 std::vector<std::string> display_names(mem_type_e hwdevice_type);
689 bool needs_encoder_reenumeration();
691 boost::process::v1::child run_command(
bool elevated,
bool interactive,
const std::string &cmd, boost::filesystem::path &working_dir,
const boost::process::v1::environment &env, FILE *file, std::error_code &ec, boost::process::v1::group *group);
699 void adjust_thread_priority(thread_priority_e priority);
702 void streaming_will_start();
703 void streaming_will_stop();
713 int set_env(
const std::string &name,
const std::string &value);
720 int unset_env(
const std::string &name);
722 struct buffer_descriptor_t {
727 buffer_descriptor_t(
const char *buffer,
size_t size):
732 buffer_descriptor_t():
746 std::vector<buffer_descriptor_t> &payload_buffers;
756 std::uintptr_t native_socket;
757 boost::asio::ip::address &target_address;
758 uint16_t target_port;
759 boost::asio::ip::address &source_address;
767 for (
const auto &desc : payload_buffers) {
768 if (offset < desc.size) {
770 desc.buffer + offset,
781 bool send_batch(batched_send_info_t &send_info);
789 std::uintptr_t native_socket;
790 boost::asio::ip::address &target_address;
791 uint16_t target_port;
792 boost::asio::ip::address &source_address;
810 std::unique_ptr<deinit_t> enable_socket_qos(uintptr_t native_socket, boost::asio::ip::address &address, uint16_t port, qos_data_type_e data_type,
bool dscp_tagging);
816 void open_url(
const std::string &url);
823 bool request_process_group_exit(std::uintptr_t native_handle);
830 bool process_group_running(std::uintptr_t native_handle);
836 input_t input(
const std::string &session_tag = {});
837#ifdef HERMES_BUILD_KWIN_TRANSPORT
843 input_t input_private_kwin(
const std::string &wayland_socket,
const std::string &session_tag = {});
851 void forget_private_kwin_input(
const std::string &wayland_socket);
862 void move_mouse(input_t &
input,
int deltaX,
int deltaY);
863 void abs_mouse(input_t &
input,
const touch_port_t &touch_port,
float x,
float y);
864 void button_mouse(input_t &
input,
int button,
bool release);
865 void scroll(input_t &
input,
int distance);
866 void hscroll(input_t &
input,
int distance);
867 void keyboard_update(input_t &
input, uint16_t modcode,
bool release, uint8_t flags);
869 void unicode(input_t &
input,
char *utf8,
int size);
938 void free_gamepad(input_t &
input,
int nr);
957#define SERVICE_TYPE "_nvstream._tcp"
960 [[nodiscard]] std::unique_ptr<deinit_t> start();
963 [[nodiscard]] std::unique_ptr<deinit_t> init();
986 virtual void sleep_for(
const std::chrono::nanoseconds &duration) = 0;
992 virtual operator bool() = 0;
1005 set_clipboard(
const std::string& content);
1008 clipboard_available();
1040 return "desktop_x11";
1042 return "desktop_wayland";
1044 return "gamescope_session";
1057 session_environment_t
1058 detect_session_environment();
A helper class for tracking and logging short time intervals across a period of time.
Abstract platform-agnostic base of standalone NVENC encoder. Derived classes perform platform-specifi...
virtual bool create_session_sink(const std::string &name, const std::uint8_t *mapping, int channels)
Create a sink that belongs to one isolated session alone.
virtual std::vector< std::string > streams_playing_elsewhere(const std::string &sink)
Applications playing to a sink other than sink.
virtual void remove_session_sink(const std::string &name)
Remove a sink created by create_session_sink(). Removing one that is already gone is not an error.
virtual bool is_sink_available(const std::string &sink)=0
Check if the audio sink is available in the system.
virtual std::unique_ptr< mic_t > microphone_from(const std::string &sink, const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size)
Record a named sink's monitor, whatever the current sink is.
virtual bool is_codec_supported(std::string_view name, const ::video::config_t &config)
Check that a given codec is supported by the display device.
std::function< bool(std::shared_ptr< img_t > &&img, bool frame_captured)> push_captured_image_cb_t
Callback for when a new image is ready. When display has a new image ready or a timeout occurs,...
std::function< bool(std::shared_ptr< img_t > &img_out)> pull_free_image_cb_t
Get free image from pool. Calls must be synchronized. Blocks until there is free image in the pool or...
virtual capture_e capture(const push_captured_image_cb_t &push_captured_image_cb, const pull_free_image_cb_t &pull_free_image_cb, bool *cursor)=0
Capture a frame.
virtual bool requires_hdr_for_hdr_output() const
Whether this capture backend requires an HDR encoder when the display is in HDR mode.
virtual std::unique_ptr< avcodec_encode_device_t > make_pyrowave_encode_device()
Make a device that converts captured frames into PyroWave's GPU planes.
Feeds a client's microphone into a session mic.
virtual void write(const std::int16_t *samples, int count)=0
Never blocks: samples that do not fit are dropped.
@ LOW_FREQUENCY
Low frequency.
@ MAX_SPEAKERS
Maximum number of speakers.
@ FRONT_CENTER
Front center.
@ FRONT_RIGHT
Front right.
std::vector< supported_gamepad_t > & supported_gamepads(input_t *input)
Gets the supported gamepads for this platform backend.
std::string session_mic_source(std::string_view sink)
The microphone source of the session that plays into sink.
@ yuv420p10
YUV 4:2:0 10-bit.
@ yuv444p16
Planar 10-bit (shifted to 16-bit) YUV 4:4:4.
int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue)
Create a new virtual gamepad.
void forget_kept_gamepads(const std::string &session_tag)
Remove the gamepads a session tag kept between streams.
session_environment_e
The kind of session/compositor environment Hermes is running in.
@ gamescope_session
Standalone Gamescope compositor (SteamOS Game Mode).
@ unknown
Could not be determined.
@ desktop_wayland
Wayland desktop session (KDE/GNOME/wlroots).
@ desktop_x11
X11 desktop session.
@ headless
No window system / capture source available.
std::unique_ptr< high_precision_timer > create_high_precision_timer()
Create platform-specific timer capable of high-precision sleep.
util::point_t get_mouse_loc(input_t &input)
Get the current mouse position on screen.
std::unique_ptr< client_input_t > allocate_client_input_context(input_t &input)
Allocate a context to store per-client input data.
void pen_update(client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen)
Send a pen event to the OS.
void gamepad_motion(input_t &input, const gamepad_motion_t &motion)
Send a gamepad motion event to the OS.
@ rumble_triggers
Rumble triggers.
@ set_adaptive_triggers
Set adaptive triggers.
@ set_motion_event_state
Set motion event state.
@ set_rgb_led
Set RGB LED.
@ critical
Critical priority.
std::string get_host_name()
Returns the current computer name in UTF-8.
void gamepad_battery(input_t &input, const gamepad_battery_t &battery)
Send a gamepad battery event to the OS.
void release_gamepad(input_t &input, int nr)
Release a gamepad because its stream ended, not because the client removed it. A detached desktop kee...
platform_caps::caps_t get_capabilities()
Get the supported platform capabilities to advertise to the client.
bool touch_binds_to_output()
Whether the session binds a touchscreen or a pen to one output.
void gamepad_touch(input_t &input, const gamepad_touch_t &touch)
Send a gamepad touch event to the OS.
input_t input(const std::string &session_tag={})
void touch_update(client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch)
Send a touch event to the OS.
@ reinit
Need to reinitialize.
@ interrupted
Capture was interrupted.
appliance_readiness_t appliance_readiness()
Assess appliance-mode readiness without changing any state.
Declarations for the configuration of Sunshine.
Declarations for logging related functions.
Standalone NVENC encoder.
Read-only assessment of whether the host could run as a dedicated streaming appliance (boot straight ...
bool autologin_configured
A display-manager autologin drop-in was detected.
bool gamescope_available
A Gamescope compositor/session is present.
bool headless_capable
Can present without a physical monitor (virtual display present).
bool enabled
config appliance_mode flag state.
std::string session_environment
session_environment_t::describe() value.
bool virtual_display_available
An EVDI or Hermes-KMS virtual display is usable.
virtual int prepare_to_derive_context(int hw_device_type)
Prepare to derive a context.
virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options)
Provides a hook for allow platform-specific code to adjust codec options.
virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx)
Set the frame to be encoded.
virtual void init_hwframes(AVHWFramesContext *frames)
Initialize the hwframes context.
buffer_descriptor_t buffer_for_payload_offset(ptrdiff_t offset)
Returns a payload buffer descriptor for the given payload offset.
std::string client_id
The paired client's id; with the controller number, a stable identity.
virtual void sleep_for(const std::chrono::nanoseconds &duration)=0
Sleep for the duration.
bool gamescope_running
A Gamescope compositor is present.
std::string describe() const
Stable string id for diagnostics/APIs, e.g. "gamescope_session".
bool can_host_virtual_display
A desktop compositor can adopt a virtual display.
std::string desktop
Raw XDG_CURRENT_DESKTOP, for diagnostics.
Declarations for thread-safe data structures.
Declarations for utility functions.
Declarations for colorspace functions.