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 <filesystem>
10#include <functional>
11#include <mutex>
12#include <optional>
13#include <string>
14
15// lib includes
16#include <boost/core/noncopyable.hpp>
17#ifndef _WIN32
18 #include <boost/asio.hpp>
19#endif
20
21// local includes
22#ifndef _WIN32
24#endif
25#include "src/config.h"
26#include "src/logging.h"
27#include "src/thread_safe.h"
28#include "src/utility.h"
30
31extern "C" {
32#include <moonlight-common-c/src/Limelight.h>
33}
34
35using namespace std::literals;
36
37struct sockaddr;
38struct AVFrame;
39struct AVBufferRef;
40struct AVHWFramesContext;
41struct AVCodecContext;
42struct AVDictionary;
43
44#ifdef _WIN32
45struct ID3D11Device;
46
47// Forward declarations of boost classes to avoid having to include boost headers
48// here, which results in issues with Windows.h and WinSock2.h include order.
49namespace boost {
50 namespace asio {
51 namespace ip {
52 class address;
53 } // namespace ip
54 } // namespace asio
55
56 namespace filesystem {
57 class path;
58 }
59
60 namespace process::v1 {
61 class child;
62 class group;
63 template<typename Char>
69 } // namespace process::v1
70} // namespace boost
71#endif
72namespace video {
73 struct config_t;
74} // namespace video
75
76namespace nvenc {
77 class nvenc_encoder;
78}
79
80namespace platf {
81 // Limited by bits in activeGamepadMask
82 constexpr auto MAX_GAMEPADS = 16;
83
84 constexpr std::uint32_t DPAD_UP = 0x0001;
85 constexpr std::uint32_t DPAD_DOWN = 0x0002;
86 constexpr std::uint32_t DPAD_LEFT = 0x0004;
87 constexpr std::uint32_t DPAD_RIGHT = 0x0008;
88 constexpr std::uint32_t START = 0x0010;
89 constexpr std::uint32_t BACK = 0x0020;
90 constexpr std::uint32_t LEFT_STICK = 0x0040;
91 constexpr std::uint32_t RIGHT_STICK = 0x0080;
92 constexpr std::uint32_t LEFT_BUTTON = 0x0100;
93 constexpr std::uint32_t RIGHT_BUTTON = 0x0200;
94 constexpr std::uint32_t HOME = 0x0400;
95 constexpr std::uint32_t A = 0x1000;
96 constexpr std::uint32_t B = 0x2000;
97 constexpr std::uint32_t X = 0x4000;
98 constexpr std::uint32_t Y = 0x8000;
99 constexpr std::uint32_t PADDLE1 = 0x010000;
100 constexpr std::uint32_t PADDLE2 = 0x020000;
101 constexpr std::uint32_t PADDLE3 = 0x040000;
102 constexpr std::uint32_t PADDLE4 = 0x080000;
103 constexpr std::uint32_t TOUCHPAD_BUTTON = 0x100000;
104 constexpr std::uint32_t MISC_BUTTON = 0x200000;
105
110 std::string name;
112 std::string reason_disabled;
113 };
114
126
139 static gamepad_feedback_msg_t make_rumble(std::uint16_t id, std::uint16_t lowfreq, std::uint16_t highfreq) {
142 msg.id = id;
143 msg.data.rumble = {lowfreq, highfreq};
144 return msg;
145 }
146
155 static gamepad_feedback_msg_t make_rumble_triggers(std::uint16_t id, std::uint16_t left, std::uint16_t right) {
158 msg.id = id;
159 msg.data.rumble_triggers = {left, right};
160 return msg;
161 }
162
171 static gamepad_feedback_msg_t make_motion_event_state(std::uint16_t id, std::uint8_t motion_type, std::uint16_t report_rate) {
174 msg.id = id;
175 msg.data.motion_event_state.motion_type = motion_type;
176 msg.data.motion_event_state.report_rate = report_rate;
177 return msg;
178 }
179
189 static gamepad_feedback_msg_t make_rgb_led(std::uint16_t id, std::uint8_t r, std::uint8_t g, std::uint8_t b) {
192 msg.id = id;
193 msg.data.rgb_led = {r, g, b};
194 return msg;
195 }
196
205 static gamepad_feedback_msg_t make_player_leds(std::uint16_t id, std::uint8_t solid, std::uint8_t flashing) {
208 msg.id = id;
209 msg.data.player_leds = {solid, flashing};
210 return msg;
211 }
212
224 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) {
227 msg.id = id;
228 msg.data.adaptive_triggers = {.event_flags = event_flags, .type_left = type_left, .type_right = type_right, .left = left, .right = right};
229 return msg;
230 }
231
233 std::uint16_t id;
234
235 union {
236 struct {
237 std::uint16_t lowfreq;
238 std::uint16_t highfreq;
239 } rumble;
240
241 struct {
242 std::uint16_t left_trigger;
243 std::uint16_t right_trigger;
245
246 struct {
247 std::uint16_t report_rate;
248 std::uint8_t motion_type;
250
251 struct {
252 std::uint8_t r;
253 std::uint8_t g;
254 std::uint8_t b;
255 } rgb_led;
256
257 struct {
258 std::uint8_t solid;
259 std::uint8_t flashing;
260 } player_leds;
261
262 struct {
263 uint8_t event_flags;
264 uint8_t type_left;
265 uint8_t type_right;
266 std::array<uint8_t, 10> left;
267 std::array<uint8_t, 10> right;
270 };
271
276
277 namespace speaker {
292
296 constexpr std::array<std::uint8_t, 2> map_stereo {
299 };
300
303 constexpr std::array<std::uint8_t, 6> map_surround51 {
308 BACK_LEFT,
310 };
311
314 constexpr std::array<std::uint8_t, 8> map_surround71 {
319 BACK_LEFT,
321 SIDE_LEFT,
323 };
324 } // namespace speaker
325
329 enum class mem_type_e {
331 vaapi,
332 dxgi,
333 cuda,
334 videotoolbox,
337 };
338
353
360 inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
361 using namespace std::literals;
362#ifndef DOXYGEN
363 #define _CONVERT(x) \
364 case pix_fmt_e::x: \
365 return #x##sv
366#endif
367 switch (pix_fmt) {
368 _CONVERT(yuv420p);
369 _CONVERT(yuv420p10);
370 _CONVERT(nv12);
371 _CONVERT(p010);
372 _CONVERT(ayuv);
373 _CONVERT(yuv444p16);
374 _CONVERT(yuv444p);
375 _CONVERT(y410);
376 _CONVERT(unknown);
377 }
378#undef _CONVERT
379
380 return "unknown"sv;
381 }
382
383 // Dimensions for touchscreen input
397
398 // These values must match Limelight-internal.h's SS_FF_* constants!
399 namespace platform_caps {
403 using caps_t = uint32_t;
404
408 constexpr caps_t pen_touch = 0x01; // Pen and touch events
412 constexpr caps_t controller_touch = 0x02; // Controller touch and motion events
413 }; // namespace platform_caps
414
419 std::uint32_t buttonFlags;
420 std::uint8_t lt;
421 std::uint8_t rt;
422 std::int16_t lsX;
423 std::int16_t lsY;
424 std::int16_t rsX;
425 std::int16_t rsY;
426 };
427
432 // The global index is used when looking up gamepads in the platform's
433 // gamepad array. It identifies gamepads uniquely among all clients.
435
436 // The client-relative index is the controller number as reported by the
437 // client. It must be used when communicating back to the client via
438 // the input feedback queue.
439 std::uint8_t clientRelativeIndex;
440 };
441
446 std::uint8_t type;
447 std::uint16_t capabilities;
448 std::uint32_t supportedButtons;
449 };
450
456 std::uint8_t eventType;
457 std::uint32_t pointerId;
458 float x;
459 float y;
460 float pressure;
461 };
462
468 std::uint8_t motionType;
469
470 // Accel: m/s^2
471 // Gyro: deg/s
472 float x;
473 float y;
474 float z;
475 };
476
482 std::uint8_t state;
483 std::uint8_t percentage;
484 };
485
490 std::uint8_t eventType;
491 std::uint16_t rotation;
492 std::uint32_t pointerId;
493 float x;
494 float y;
498 };
499
503 struct pen_input_t {
504 std::uint8_t eventType;
505 std::uint8_t toolType;
506 std::uint8_t penButtons;
507 std::uint8_t tilt;
508 std::uint16_t rotation;
509 float x;
510 float y;
514 };
515
519 class deinit_t {
520 public:
524 virtual ~deinit_t() = default;
525 };
526
530 struct img_t: std::enable_shared_from_this<img_t> {
531 public:
532 img_t() = default;
533
534 img_t(img_t &&) = delete;
535 img_t(const img_t &) = delete;
536 img_t &operator=(img_t &&) = delete;
537 img_t &operator=(const img_t &) = delete;
538
539 std::uint8_t *data {};
540 std::int32_t width {};
541 std::int32_t height {};
542 std::int32_t pixel_pitch {};
543 std::int32_t row_pitch {};
544
545 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
546
550 virtual ~img_t() = default;
551 };
552
556 struct sink_t {
557 // Play on host PC
558 std::string host;
559
560 // On macOS and Windows, it is not possible to create a virtual sink
561 // Therefore, it is optional
565 struct null_t {
566 std::string stereo;
567 std::string surround51;
568 std::string surround71;
569 };
570
571 std::optional<null_t> null;
572 };
573
578 virtual ~encode_device_t() = default;
579
586 virtual int convert(platf::img_t &img) = 0;
587
589 };
590
595 bool hardware = false;
599 using init_hw_device_fn_t = int (*)(avcodec_encode_device_t *encode_device, AVBufferRef **hw_device_buf);
601#ifdef _WIN32
602 ID3D11Device *d3d11_device = nullptr;
603#endif
604 AVFrame *frame {};
605
612 int convert(platf::img_t &img) override {
613 return -1;
614 }
615
619 virtual void apply_colorspace() {
620 }
621
630 virtual int set_frame(AVFrame *new_frame, AVBufferRef *hw_frames_ctx) {
631 BOOST_LOG(error) << "Illegal call to hwdevice_t::set_frame(). Did you forget to override it?";
632 return -1;
633 };
634
641 virtual void init_hwframes(AVHWFramesContext *frames) {};
642
650 virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options) {};
651
659 virtual int prepare_to_derive_context(int hw_device_type) {
660 return 0;
661 };
662 };
663
679
690
694 class display_t {
695 public:
703 using push_captured_image_cb_t = std::function<bool(std::shared_ptr<img_t> &&img, bool frame_captured)>;
704
712 using pull_free_image_cb_t = std::function<bool(std::shared_ptr<img_t> &img_out)>;
713
714 display_t() noexcept = default;
715
728 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;
729
735 virtual std::shared_ptr<img_t> alloc_img() = 0;
736
743 virtual int dummy_img(img_t *img) = 0;
744
752 return nullptr;
753 }
754
761 virtual std::unique_ptr<nvenc_encode_device_t> make_nvenc_encode_device(pix_fmt_e pix_fmt) {
762 return nullptr;
763 }
764
770 virtual bool is_hdr() {
771 return false;
772 }
773
780 virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata) {
781 std::memset(&metadata, 0, sizeof(metadata));
782 return false;
783 }
784
791 virtual bool is_codec_supported(std::string_view name, const ::video::config_t &config) {
792 return true;
793 }
794
795 virtual ~display_t() = default;
796
797 // Offsets for when streaming a specific monitor. By default, they are 0.
798 int offset_x {0};
799 int offset_y {0};
800 int env_offset_x {0};
801 int env_offset_y {0};
802 int env_width {0};
803 int env_height {0};
806 int width {0};
807 int height {0};
810
811 protected:
812 // collect capture timing data (at loglevel debug)
814 };
815
819 class mic_t {
820 public:
827 virtual capture_e sample(std::vector<float> &frame_buffer) = 0;
828
829 virtual ~mic_t() = default;
830 };
831
836 public:
843 virtual int set_sink(const std::string &sink) = 0;
844
856 virtual std::unique_ptr<mic_t> microphone(const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size, bool continuous, [[maybe_unused]] bool host_audio_enabled) = 0;
857
863 virtual bool is_sink_available(const std::string &sink) = 0;
864
870 virtual std::optional<sink_t> sink_info() = 0;
871
875 virtual ~audio_control_t() = default;
876 };
877
881 struct input_raw_t;
882
889
894
895 std::filesystem::path appdata();
896
897 std::filesystem::path get_xdg_restore_token_path();
898
905 std::string get_mac_address(const std::string_view &address);
906
913 std::string from_sockaddr(const sockaddr *const);
920 std::pair<std::uint16_t, std::string> from_sockaddr_ex(const sockaddr *const);
921
927 std::unique_ptr<audio_control_t> audio_control();
928
937 std::shared_ptr<display_t> display(mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config);
938
939 // A list of names of displays accepted as display_name with the mem_type_e
946 std::vector<std::string> display_names(mem_type_e hwdevice_type);
947
953
967 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);
968
978
983 void adjust_thread_priority(thread_priority_e priority);
984
991 void set_thread_name(std::string_view name);
992
993 void enable_mouse_keys();
994
995 // Allow OS-specific actions to be taken to prepare for streaming
996 void streaming_will_start();
997 void streaming_will_stop();
998
999 void restart();
1000
1005 const char *buffer;
1006 size_t size;
1007
1008 // Constructors required for emplace_back() prior to C++20
1015 buffer_descriptor_t(const char *buffer, size_t size):
1016 buffer(buffer),
1017 size(size) {
1018 }
1019
1021 buffer(nullptr),
1022 size(0) {
1023 }
1024 };
1025
1030 // Optional headers to be prepended to each packet
1031 const char *headers;
1033
1034 // One or more data buffers to use for the payloads
1035 //
1036 // NB: Data buffers must be aligned to payload size!
1037 std::vector<buffer_descriptor_t> &payload_buffers;
1039
1040 // The offset (in header+payload message blocks) in the header and payload
1041 // buffers to begin sending messages from
1043
1044 // The number of header+payload message blocks to send
1046
1047 std::uintptr_t native_socket;
1048 boost::asio::ip::address &target_address;
1049 uint16_t target_port;
1050 boost::asio::ip::address &source_address;
1051
1058 for (const auto &desc : payload_buffers) {
1059 if (offset < desc.size) {
1060 return {
1061 desc.buffer + offset,
1062 desc.size - offset,
1063 };
1064 } else {
1065 offset -= desc.size;
1066 }
1067 }
1068 return {};
1069 }
1070 };
1071
1078 bool send_batch(const batched_send_info_t &send_info);
1079
1084 const char *header;
1086 const char *payload;
1088
1089 std::uintptr_t native_socket;
1090 boost::asio::ip::address &target_address;
1091 uint16_t target_port;
1092 boost::asio::ip::address &source_address;
1093 };
1094
1101 bool send(send_info_t &send_info);
1102
1106 enum class qos_data_type_e : int {
1107 audio,
1108 video
1109 };
1110
1121 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);
1122
1127 void open_url(const std::string &url);
1128
1134 bool request_process_group_exit(std::uintptr_t native_handle);
1135
1141 bool process_group_running(std::uintptr_t native_handle);
1142
1148 input_t input();
1164 std::optional<util::point_t> get_mouse_loc(input_t &input);
1172 void move_mouse(input_t &input, int deltaX, int deltaY);
1181 void move_mouse(input_t &input, const touch_port_t &touch_port, int deltaX, int deltaY);
1190 void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y);
1198 void button_mouse(input_t &input, int button, bool release);
1205 void scroll(input_t &input, int distance);
1212 void hscroll(input_t &input, int distance);
1222 void keyboard_update(input_t &input, uint16_t modcode, bool release, uint8_t flags, bool extended = false);
1223 void gamepad_update(input_t &input, int nr, const gamepad_state_t &gamepad_state);
1231 void unicode(input_t &input, const char *utf8, int size);
1232
1237
1243 std::unique_ptr<client_input_t> allocate_client_input_context(input_t &input);
1244
1251 void touch_update(client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch);
1252
1259 void pen_update(client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen);
1260
1266 void gamepad_touch(input_t &input, const gamepad_touch_t &touch);
1267
1273 void gamepad_motion(input_t &input, const gamepad_motion_t &motion);
1274
1280 void gamepad_battery(input_t &input, const gamepad_battery_t &battery);
1281
1290 int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
1299 int rebind_gamepad(input_t &input, const gamepad_id_t &id, feedback_queue_t feedback_queue);
1306 void free_gamepad(input_t &input, int nr);
1307
1313
1314 constexpr auto SERVICE_NAME = "Sunshine";
1315 constexpr auto SERVICE_TYPE = "_nvstream._tcp";
1316
1317 namespace publish {
1318 [[nodiscard]] std::unique_ptr<deinit_t> start();
1319 }
1320
1326 [[nodiscard]] std::unique_ptr<deinit_t> init();
1327
1332 std::string get_host_name();
1333
1341 std::string resolve_render_device();
1342
1349 std::vector<supported_gamepad_t> &supported_gamepads(input_t *input);
1350
1354 struct high_precision_timer: private boost::noncopyable {
1355 virtual ~high_precision_timer() = default;
1356
1361 virtual void sleep_for(const std::chrono::nanoseconds &duration) = 0;
1362
1367 virtual explicit operator bool() = 0;
1368 };
1369
1374 std::unique_ptr<high_precision_timer> create_high_precision_timer();
1375
1381 bool has_elevated_privileges(bool all_caps);
1382
1387 void drop_elevated_privileges(bool all_caps);
1388
1389} // namespace platf
Includes Boost.Process while isolating third-party deprecation diagnostics.
A helper class for tracking and logging short time intervals across a period of time.
Definition logging.h:253
SDK-neutral standalone NVENC encoder interface.
Definition nvenc_encoder.h:28
Platform audio controller that manages sinks and microphone capture.
Definition common.h:835
virtual std::unique_ptr< mic_t > microphone(const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size, bool continuous, bool host_audio_enabled)=0
Create a microphone capture stream for the requested layout.
virtual std::optional< sink_t > sink_info()=0
Query host and virtual sink names available to Sunshine.
virtual int set_sink(const std::string &sink)=0
Update the sink value on the backend.
virtual bool is_sink_available(const std::string &sink)=0
Check if the audio sink is available in the system.
virtual ~audio_control_t()=default
Destroy the audio control.
RAII helper that runs shutdown cleanup when destroyed.
Definition common.h:519
virtual ~deinit_t()=default
Destroy the deinitializer.
Abstract display capture backend used by the streaming pipeline.
Definition common.h:694
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.
Definition common.h:791
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,...
Definition common.h:703
virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata)
Read HDR metadata for the active display mode.
Definition common.h:780
int env_height
Height of the full capture environment in physical pixels.
Definition common.h:803
int env_offset_x
Horizontal origin of the full capture environment in native desktop coordinates.
Definition common.h:800
int env_offset_y
Vertical origin of the full capture environment in native desktop coordinates.
Definition common.h:801
int env_logical_height
Height of the full capture environment after display scaling.
Definition common.h:805
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...
Definition common.h:712
logging::time_delta_periodic_logger sleep_overshoot_logger
Periodic logger for capture sleep overshoot measurements.
Definition common.h:813
int offset_y
Vertical capture offset in physical pixels.
Definition common.h:799
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 std::unique_ptr< nvenc_encode_device_t > make_nvenc_encode_device(pix_fmt_e pix_fmt)
Create NVENC encode device.
Definition common.h:761
int width
Width of the captured display in physical pixels.
Definition common.h:806
int logical_height
Height of the captured display after display scaling.
Definition common.h:809
int env_width
Width of the full capture environment in physical pixels.
Definition common.h:802
int height
Height of the captured display in physical pixels.
Definition common.h:807
virtual std::unique_ptr< avcodec_encode_device_t > make_avcodec_encode_device(pix_fmt_e pix_fmt)
Create AVCodec encode device.
Definition common.h:751
int offset_x
Horizontal capture offset in physical pixels.
Definition common.h:798
virtual bool is_hdr()
Report whether the active display mode is HDR.
Definition common.h:770
int env_logical_width
Width of the full capture environment after display scaling.
Definition common.h:804
virtual std::shared_ptr< img_t > alloc_img()=0
Allocate an image buffer compatible with this display backend.
int logical_width
Width of the captured display after display scaling.
Definition common.h:808
virtual int dummy_img(img_t *img)=0
Populate a fallback image when real capture data is unavailable.
Audio capture source used by the streaming pipeline.
Definition common.h:819
virtual capture_e sample(std::vector< float > &frame_buffer)=0
Deliver a captured audio sample to Sunshine's audio pipeline.
std::shared_ptr< post_t< queue_t< T > > > queue_t
Mailbox-backed queue wrapper for typed messages.
Definition thread_safe.h:813
mem_type_e
Enumerates supported mem type options.
Definition common.h:329
@ system
System memory.
Definition common.h:330
@ vulkan
Vulkan.
Definition common.h:335
@ unknown
Unknown.
Definition common.h:336
void free_gamepad(input_t &input, int nr)
Release gamepad resources.
Definition input.cpp:743
constexpr std::array< std::uint8_t, 2 > map_stereo
Moonlight speaker order for stereo audio.
Definition common.h:296
basic_environment< char > environment
Map of environment variable names to values.
Definition common.h:68
constexpr std::uint32_t B
Moonlight gamepad button mask bit for B.
Definition common.h:96
speaker_e
Enumerates supported speaker options.
Definition common.h:281
@ BACK_LEFT
Back left.
Definition common.h:286
@ LOW_FREQUENCY
Low frequency.
Definition common.h:285
@ SIDE_RIGHT
Side right.
Definition common.h:289
@ MAX_SPEAKERS
Maximum number of speakers.
Definition common.h:290
@ FRONT_LEFT
Front left.
Definition common.h:282
@ SIDE_LEFT
Side left.
Definition common.h:288
@ FRONT_CENTER
Front center.
Definition common.h:284
@ BACK_RIGHT
Back right.
Definition common.h:287
@ FRONT_RIGHT
Front right.
Definition common.h:283
constexpr std::uint32_t LEFT_BUTTON
Moonlight gamepad button mask bit for left shoulder.
Definition common.h:92
deinit_t client_input_t
Per-client input context allocated by a platform backend.
Definition common.h:1236
int rebind_gamepad(input_t &input, const gamepad_id_t &id, feedback_queue_t feedback_queue)
Rebind an existing virtual gamepad to a resumed client session.
Definition input.cpp:729
input_t input()
Create the platform input backend for a stream.
Definition input.cpp:561
constexpr std::uint32_t DPAD_LEFT
Moonlight gamepad button mask bit for D-pad left.
Definition common.h:86
std::string get_mac_address(const std::string_view &address)
Return the hardware MAC address associated with a network address.
Definition misc.cpp:424
constexpr std::uint32_t DPAD_UP
Moonlight gamepad button mask bit for D-pad up.
Definition common.h:84
std::shared_ptr< display_t > display(mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config)
Get the display_t instance for the given hwdevice_type. If display_name is empty, use the first monit...
Definition misc.cpp:1365
constexpr std::uint32_t PADDLE3
Moonlight gamepad button mask bit for paddle 3.
Definition common.h:101
constexpr auto SERVICE_TYPE
mDNS service type advertised for GameStream discovery.
Definition common.h:1315
std::pair< std::uint16_t, std::string > from_sockaddr_ex(const sockaddr *const)
Convert a socket address to a port and printable IP address.
Definition misc.cpp:405
pix_fmt_e
Enumerates supported pix fmt options.
Definition common.h:342
@ ayuv
AYUV.
Definition common.h:347
@ yuv420p10
YUV 4:2:0 10-bit.
Definition common.h:344
@ yuv444p
Planar 8-bit YUV 4:4:4.
Definition common.h:349
@ y410
Y410.
Definition common.h:350
@ nv12
NV12.
Definition common.h:345
@ yuv444p16
Planar 10-bit (shifted to 16-bit) YUV 4:4:4.
Definition common.h:348
@ yuv420p
YUV 4:2:0.
Definition common.h:343
@ p010
P010.
Definition common.h:346
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.
Definition input.cpp:666
constexpr std::uint32_t HOME
Moonlight gamepad button mask bit for Home.
Definition common.h:94
bool has_elevated_privileges(bool all_caps)
Check is the current process is running with elevated privileges (e.g. system admin/etc....
Definition misc.cpp:1623
uint32_t caps_t
Bitset containing platform capability flags.
Definition common.h:403
std::filesystem::path appdata()
Performs migration if necessary, then returns the appdata directory.
Definition misc.cpp:298
constexpr std::uint32_t TOUCHPAD_BUTTON
Moonlight gamepad button mask bit for touchpad click.
Definition common.h:103
constexpr std::uint32_t LEFT_STICK
Moonlight gamepad button mask bit for left stick press.
Definition common.h:90
constexpr std::uint32_t DPAD_RIGHT
Moonlight gamepad button mask bit for D-pad right.
Definition common.h:87
constexpr std::uint32_t PADDLE2
Moonlight gamepad button mask bit for paddle 2.
Definition common.h:100
constexpr std::uint32_t RIGHT_STICK
Moonlight gamepad button mask bit for right stick press.
Definition common.h:91
std::filesystem::path get_xdg_restore_token_path()
XDG restore token path.
Definition misc.cpp:378
qos_data_type_e
Identifies traffic classes used for socket QoS tagging.
Definition common.h:1106
std::unique_ptr< high_precision_timer > create_high_precision_timer()
Create platform-specific timer capable of high-precision sleep.
Definition misc.cpp:1550
std::string_view from_pix_fmt(pix_fmt_e pix_fmt)
Convert a Sunshine pixel format enum to its string name.
Definition common.h:360
std::unique_ptr< audio_control_t > audio_control()
Create the platform audio controller.
Definition audio.cpp:718
constexpr std::uint32_t PADDLE1
Moonlight gamepad button mask bit for paddle 1.
Definition common.h:99
std::unique_ptr< client_input_t > allocate_client_input_context(input_t &input)
Allocate a context to store per-client input data.
Definition input.cpp:658
constexpr std::uint32_t PADDLE4
Moonlight gamepad button mask bit for paddle 4.
Definition common.h:102
constexpr caps_t pen_touch
Capability bit indicating native pen and touch support.
Definition common.h:408
constexpr std::uint32_t A
Moonlight gamepad button mask bit for A.
Definition common.h:95
constexpr auto SERVICE_NAME
mDNS service instance name advertised for GameStream discovery.
Definition common.h:1314
bool send(send_info_t &send_info)
Send the serialized response over the active socket.
Definition misc.cpp:945
constexpr std::uint32_t DPAD_DOWN
Moonlight gamepad button mask bit for D-pad down.
Definition common.h:85
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.
Definition virtualhid_input.cpp:1190
void gamepad_motion(input_t &input, const gamepad_motion_t &motion)
Send a gamepad motion event to the OS.
Definition input.cpp:1219
bool needs_encoder_reenumeration()
Check if GPUs/drivers have changed since the last call to this function.
Definition misc.cpp:1352
gamepad_feedback_e
Enumerates supported gamepad feedback options.
Definition common.h:118
@ set_player_leds
Set player indicator LEDs.
Definition common.h:123
@ rumble
Rumble.
Definition common.h:119
@ rumble_triggers
Rumble triggers.
Definition common.h:120
@ set_adaptive_triggers
Set adaptive triggers.
Definition common.h:124
@ set_motion_event_state
Set motion event state.
Definition common.h:121
@ set_rgb_led
Set RGB LED.
Definition common.h:122
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)
Launch a configured preparation or application command.
constexpr std::uint32_t BACK
Moonlight gamepad button mask bit for Back.
Definition common.h:89
thread_priority_e
Enumerates supported thread priority options.
Definition common.h:972
@ low
Low priority.
Definition common.h:973
@ critical
Critical priority.
Definition common.h:976
@ high
High priority.
Definition common.h:975
@ normal
Normal priority.
Definition common.h:974
util::safe_ptr< input_raw_t, &freeInput > input_t
Owning pointer for a platform input backend.
Definition common.h:893
constexpr std::array< std::uint8_t, 8 > map_surround71
Moonlight speaker order for 7.1 surround audio.
Definition common.h:314
void drop_elevated_privileges(bool all_caps)
Drop elevated privileges (e.g. system admin/nice etc.).
Definition misc.cpp:1652
constexpr std::array< std::uint8_t, 6 > map_surround51
Moonlight speaker order for 5.1 surround audio.
Definition common.h:303
constexpr auto MAX_GAMEPADS
Maximum number of simultaneously tracked gamepads.
Definition common.h:82
std::string from_sockaddr(const sockaddr *const)
Convert a socket address to a printable IP address.
Definition misc.cpp:389
constexpr std::uint32_t RIGHT_BUTTON
Moonlight gamepad button mask bit for right shoulder.
Definition common.h:93
safe::mail_raw_t::queue_t< gamepad_feedback_msg_t > feedback_queue_t
Queue used to deliver controller feedback commands to the platform backend.
Definition common.h:275
constexpr std::uint32_t START
Moonlight gamepad button mask bit for Start.
Definition common.h:88
constexpr std::uint32_t MISC_BUTTON
Moonlight gamepad button mask bit for the miscellaneous button.
Definition common.h:104
void gamepad_battery(input_t &input, const gamepad_battery_t &battery)
Send a gamepad battery event to the OS.
Definition input.cpp:1252
constexpr std::uint32_t X
Moonlight gamepad button mask bit for X.
Definition common.h:97
platform_caps::caps_t get_capabilities()
Get the supported platform capabilities to advertise to the client.
Definition virtualhid.cpp:21
std::vector< std::string > display_names(mem_type_e hwdevice_type)
List display names accepted by the selected capture backend.
Definition misc.cpp:1312
void gamepad_touch(input_t &input, const gamepad_touch_t &touch)
Send a gamepad touch event to the OS.
Definition input.cpp:1185
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.
Definition virtualhid_input.cpp:1186
constexpr caps_t controller_touch
Capability bit indicating controller touchpad and motion support.
Definition common.h:412
capture_e
Enumerates supported capture options.
Definition common.h:683
@ capture_error
Capture failed.
Definition common.h:688
@ ok
Success.
Definition common.h:684
@ timeout
Timeout.
Definition common.h:686
@ reinit
Need to reinitialize.
Definition common.h:685
@ interrupted
Capture was interrupted.
Definition common.h:687
constexpr std::uint32_t Y
Moonlight gamepad button mask bit for Y.
Definition common.h:98
void freeInput(input_raw_t *input)
Release a platform input backend created by input().
Definition input.cpp:1280
Declarations for the configuration of Sunshine.
avahi::EntryGroup * group
Active Avahi entry group that owns the published service.
Definition publish.cpp:396
bl::sources::severity_logger< int > debug
Follow what is happening.
Definition logging.cpp:43
bl::sources::severity_logger< int > error
Recoverable errors.
Definition logging.cpp:46
boost::shared_ptr< boost::log::sinks::asynchronous_sink< boost::log::sinks::text_ostream_backend > > sink
Sink.
Definition logging.cpp:40
Declarations for logging related functions.
Standalone NVENC encoder.
Definition nvenc_base.cpp:185
AVCodec-backed encode device and frame state.
Definition common.h:594
int convert(platf::img_t &img) override
Convert a captured image into the encoder input representation.
Definition common.h:612
virtual int set_frame(AVFrame *new_frame, AVBufferRef *hw_frames_ctx)
Set the frame to be encoded.
Definition common.h:630
virtual int prepare_to_derive_context(int hw_device_type)
Prepare to derive a context.
Definition common.h:659
init_hw_device_fn_t init_hw_device
Optional backend-specific hardware device initializer.
Definition common.h:600
bool hardware
Whether this device supplies hardware frames instead of software conversion.
Definition common.h:595
virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options)
Provides a hook for allow platform-specific code to adjust codec options.
Definition common.h:650
virtual void apply_colorspace()
Apply the configured colorspace metadata to the active frame.
Definition common.h:619
ID3D11Device * d3d11_device
Native D3D11 device borrowed while the encode device is alive.
Definition common.h:602
AVFrame * frame
FFmpeg frame currently owned by the encode device.
Definition common.h:604
int(*)(avcodec_encode_device_t *encode_device, AVBufferRef **hw_device_buf) init_hw_device_fn_t
Callback signature for backend-specific FFmpeg hardware device initialization.
Definition common.h:599
virtual void init_hwframes(AVHWFramesContext *frames)
Initialize the hwframes context.
Definition common.h:641
Buffers and native metadata for one batched send operation.
Definition common.h:1029
boost::asio::ip::address & source_address
Local source IP address for outgoing packets.
Definition common.h:1050
const char * headers
Optional header bytes prepended to each payload.
Definition common.h:1031
std::vector< buffer_descriptor_t > & payload_buffers
Payload buffers containing one or more packet blocks.
Definition common.h:1037
std::uintptr_t native_socket
Platform socket handle used for the send operation.
Definition common.h:1047
size_t block_count
Number of packet blocks to send.
Definition common.h:1045
buffer_descriptor_t buffer_for_payload_offset(ptrdiff_t offset) const
Locate a payload region without changing the batch descriptor or its buffers.
Definition common.h:1057
uint16_t target_port
Destination UDP port for outgoing packets.
Definition common.h:1049
size_t block_offset
First packet block index to send.
Definition common.h:1042
size_t header_size
Header size in bytes.
Definition common.h:1032
size_t payload_size
Payload size in bytes for each packet block.
Definition common.h:1038
boost::asio::ip::address & target_address
Destination IP address for outgoing packets.
Definition common.h:1048
Platform buffer pointer and size for batched socket sends.
Definition common.h:1004
size_t size
Size of the buffer in bytes.
Definition common.h:1006
buffer_descriptor_t(const char *buffer, size_t size)
Describe a contiguous byte buffer for batched socket sending.
Definition common.h:1015
const char * buffer
Pointer to the payload buffer.
Definition common.h:1005
Base interface for hardware or software frame conversion.
Definition common.h:577
video::sunshine_colorspace_t colorspace
Colorspace metadata expected by the encoder.
Definition common.h:588
virtual int convert(platf::img_t &img)=0
Convert a captured image into the encoder input representation.
Capabilities reported when a controller is connected.
Definition common.h:445
std::uint8_t type
Protocol or controller type discriminator.
Definition common.h:446
std::uint32_t supportedButtons
Button mask supported by the connected controller.
Definition common.h:448
std::uint16_t capabilities
Capability flags advertised by the controller.
Definition common.h:447
Battery state reported by a virtual gamepad.
Definition common.h:480
std::uint8_t state
Battery state reported by the client.
Definition common.h:482
std::uint8_t percentage
Battery charge percentage.
Definition common.h:483
gamepad_id_t id
Gamepad identifier for the event.
Definition common.h:481
std::array< uint8_t, 10 > left
Left adaptive-trigger effect parameters.
std::array< uint8_t, 10 > right
Right adaptive-trigger effect parameters.
uint8_t event_flags
Flags describing which adaptive-trigger data is present.
std::uint16_t report_rate
Requested motion-sensor report rate.
std::uint8_t solid
Bit mask of solid player indicators.
std::uint8_t flashing
Bit mask of flashing player indicators.
std::uint16_t right_trigger
Right-trigger rumble motor intensity.
std::uint16_t left_trigger
Left-trigger rumble motor intensity.
std::uint16_t lowfreq
Low-frequency rumble motor intensity.
std::uint16_t highfreq
High-frequency rumble motor intensity.
Feedback command sent from Sunshine to a virtual gamepad.
Definition common.h:130
static gamepad_feedback_msg_t make_motion_event_state(std::uint16_t id, std::uint8_t motion_type, std::uint16_t report_rate)
Motion-event feedback command payload for a controller.
Definition common.h:171
std::uint16_t id
Controller identifier associated with this message.
Definition common.h:233
static gamepad_feedback_msg_t make_rumble(std::uint16_t id, std::uint16_t lowfreq, std::uint16_t highfreq)
Create a rumble object or message.
Definition common.h:139
static gamepad_feedback_msg_t make_player_leds(std::uint16_t id, std::uint8_t solid, std::uint8_t flashing)
Create player indicator LED state.
Definition common.h:205
static gamepad_feedback_msg_t make_rgb_led(std::uint16_t id, std::uint8_t r, std::uint8_t g, std::uint8_t b)
Create RGB led.
Definition common.h:189
static gamepad_feedback_msg_t make_rumble_triggers(std::uint16_t id, std::uint16_t left, std::uint16_t right)
Create rumble triggers.
Definition common.h:155
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)
Create adaptive triggers.
Definition common.h:224
union platf::gamepad_feedback_msg_t::@234001133101023130022260273167130254046220012376 data
Controller feedback payload for the selected feedback type.
gamepad_feedback_e type
Feedback command type stored in the union payload.
Definition common.h:232
Global and client-relative identifiers for a virtual gamepad.
Definition common.h:431
int globalIndex
Index unique across all connected clients.
Definition common.h:434
std::uint8_t clientRelativeIndex
Client relative index.
Definition common.h:439
Accelerometer or gyroscope sample from a controller.
Definition common.h:466
float z
Depth or Z-axis vector component.
Definition common.h:474
float x
Horizontal coordinate or vector component.
Definition common.h:472
gamepad_id_t id
Gamepad identifier for the event.
Definition common.h:467
float y
Vertical coordinate or vector component.
Definition common.h:473
std::uint8_t motionType
Motion type.
Definition common.h:468
Button and axis state received for a virtual gamepad.
Definition common.h:418
std::int16_t rsX
Right stick X-axis value.
Definition common.h:424
std::uint32_t buttonFlags
Moonlight button mask for the current gamepad state.
Definition common.h:419
std::uint8_t lt
Left trigger value.
Definition common.h:420
std::int16_t lsX
Left stick X-axis value.
Definition common.h:422
std::int16_t rsY
Right stick Y-axis value.
Definition common.h:425
std::uint8_t rt
Right trigger value.
Definition common.h:421
std::int16_t lsY
Left stick Y-axis value.
Definition common.h:423
Touchpad contact data reported by a controller.
Definition common.h:454
float pressure
Contact pressure reported by the client.
Definition common.h:460
std::uint32_t pointerId
Client-provided pointer identifier for a touch contact.
Definition common.h:457
float y
Vertical coordinate or vector component.
Definition common.h:459
std::uint8_t eventType
Moonlight event type for the input packet.
Definition common.h:456
float x
Horizontal coordinate or vector component.
Definition common.h:458
gamepad_id_t id
Gamepad identifier for the event.
Definition common.h:455
Platform timer object used for precise frame pacing.
Definition common.h:1354
virtual void sleep_for(const std::chrono::nanoseconds &duration)=0
Sleep for the duration.
Captured frame buffer shared between capture and encode stages.
Definition common.h:530
std::int32_t row_pitch
Bytes between consecutive image rows.
Definition common.h:543
std::uint8_t * data
Pointer to the captured image buffer.
Definition common.h:539
std::int32_t pixel_pitch
Bytes per pixel in the image buffer.
Definition common.h:542
virtual ~img_t()=default
Destroy the image.
std::int32_t height
Image height in pixels.
Definition common.h:541
std::int32_t width
Image width in pixels.
Definition common.h:540
std::optional< std::chrono::steady_clock::time_point > frame_timestamp
Capture timestamp associated with the frame.
Definition common.h:545
Global virtual input device handles shared by clients.
Definition input.cpp:522
NVENC-backed encode device state.
Definition common.h:667
virtual bool init_encoder(const video::config_t &client_config, const video::sunshine_colorspace_t &colorspace)=0
Initialize the platform encoder for the client stream configuration.
Pen tablet event data from the client.
Definition common.h:503
std::uint8_t tilt
Degrees (0..90) or LI_TILT_UNKNOWN.
Definition common.h:507
float contactAreaMinor
Minor axis of the reported contact area.
Definition common.h:513
float x
Horizontal coordinate or vector component.
Definition common.h:509
std::uint8_t eventType
Moonlight event type for the input packet.
Definition common.h:504
std::uint8_t toolType
Pen tool type reported by the client.
Definition common.h:505
float y
Vertical coordinate or vector component.
Definition common.h:510
float pressureOrDistance
Distance for hover and pressure for contact.
Definition common.h:511
float contactAreaMajor
Major axis of the reported contact area.
Definition common.h:512
std::uint8_t penButtons
Button mask for the active pen tool.
Definition common.h:506
std::uint16_t rotation
Degrees (0..360) or LI_ROT_UNKNOWN.
Definition common.h:508
Destination address and payload data for one UDP send.
Definition common.h:1083
const char * payload
Payload bytes to send after the header.
Definition common.h:1086
std::uintptr_t native_socket
Platform socket handle used for the send operation.
Definition common.h:1089
boost::asio::ip::address & target_address
Destination IP address for outgoing packets.
Definition common.h:1090
size_t payload_size
Payload size in bytes for each packet block.
Definition common.h:1087
uint16_t target_port
Destination UDP port for outgoing packets.
Definition common.h:1091
size_t header_size
Header size in bytes.
Definition common.h:1085
const char * header
Optional header bytes prepended to the payload.
Definition common.h:1084
boost::asio::ip::address & source_address
Local source IP address for outgoing packets.
Definition common.h:1092
Optional virtual sink names for each supported channel layout.
Definition common.h:565
std::string surround51
Virtual sink name for 5.1 surround audio.
Definition common.h:567
std::string stereo
Virtual sink name for stereo audio.
Definition common.h:566
std::string surround71
Virtual sink name for 7.1 surround audio.
Definition common.h:568
Host and virtual audio sink names for audio routing.
Definition common.h:556
std::string host
Host playback sink name.
Definition common.h:558
std::optional< null_t > null
Optional virtual sink names for monitor capture.
Definition common.h:571
Gamepad type exposed to clients and why it may be disabled.
Definition common.h:109
std::string reason_disabled
Human-readable reason the gamepad type is disabled.
Definition common.h:112
std::string name
Human-readable name for this item.
Definition common.h:110
bool is_enabled
Whether this gamepad type is currently available.
Definition common.h:111
Absolute touchscreen event data from the client.
Definition common.h:489
float x
Horizontal coordinate or vector component.
Definition common.h:493
std::uint32_t pointerId
Client-provided pointer identifier for a touch contact.
Definition common.h:492
float contactAreaMajor
Major axis of the reported contact area.
Definition common.h:496
float contactAreaMinor
Minor axis of the reported contact area.
Definition common.h:497
std::uint16_t rotation
Degrees (0..360) or LI_ROT_UNKNOWN.
Definition common.h:491
std::uint8_t eventType
Moonlight event type for the input packet.
Definition common.h:490
float y
Vertical coordinate or vector component.
Definition common.h:494
float pressureOrDistance
Distance for hover and pressure for contact.
Definition common.h:495
Touchscreen coordinate bounds used to scale absolute input.
Definition common.h:387
int logical_width
Logical width after display scaling.
Definition common.h:392
int env_offset_x
Horizontal origin of the full input environment in native desktop coordinates.
Definition common.h:394
int height
Frame or display height in pixels.
Definition common.h:391
int offset_y
Vertical offset in physical pixels.
Definition common.h:389
int offset_x
Horizontal offset in physical pixels.
Definition common.h:388
int logical_height
Logical height after display scaling.
Definition common.h:393
int env_offset_y
Vertical origin of the full input environment in native desktop coordinates.
Definition common.h:395
int width
Frame or display width in pixels.
Definition common.h:390
Encoding configuration requested by a remote client.
Definition video.h:33
Sunshine colorimetry values derived from stream configuration.
Definition video_colorspace.h:36
Declarations for thread-safe data structures.
struct platf::gamepad_feedback_msg_t::@234001133101023130022260273167130254046220012376::@037021105207321207034272072146135103300350235316 rumble_triggers
Trigger-rumble payload.
struct platf::gamepad_feedback_msg_t::@234001133101023130022260273167130254046220012376::@044345256342224141102302237155264067070201106066 player_leds
Player-indicator LED payload.
struct platf::gamepad_feedback_msg_t::@234001133101023130022260273167130254046220012376::@106035267303314031173166200007272265340336012106 adaptive_triggers
Adaptive-trigger effect payload.
struct platf::gamepad_feedback_msg_t::@234001133101023130022260273167130254046220012376::@140357377176242234057270015324307025371227217003 motion_event_state
Motion-event configuration payload.
struct platf::gamepad_feedback_msg_t::@234001133101023130022260273167130254046220012376::@213310322173366006247037042350147155371343226356 rumble
Main rumble-motor payload.
struct platf::gamepad_feedback_msg_t::@234001133101023130022260273167130254046220012376::@313276062140061056216371202126064256137165137065 rgb_led
RGB LED payload.
Declarations for utility functions.
uniq_ptr< T, Destroy< T *, void, function > > safe_ptr
Unique pointer using a compile-time C-style destroy function.
Definition utility.h:1590
Declarations for colorspace functions.