Sunshine master
Self-hosted game stream host for Moonlight.
common.h
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 <string>
13
14// lib includes
15#include <boost/core/noncopyable.hpp>
16#ifndef _WIN32
17 #include <boost/asio.hpp>
18 #include <boost/process/v1.hpp>
19#endif
20
21// local includes
22#include "src/config.h"
23#include "src/logging.h"
24#include "src/thread_safe.h"
25#include "src/utility.h"
27
28extern "C" {
29#include <moonlight-common-c/src/Limelight.h>
30}
31
32using namespace std::literals;
33
34struct sockaddr;
35struct AVFrame;
36struct AVBufferRef;
37struct AVHWFramesContext;
38struct AVCodecContext;
39struct AVDictionary;
40
41#ifdef _WIN32
42// Forward declarations of boost classes to avoid having to include boost headers
43// here, which results in issues with Windows.h and WinSock2.h include order.
44namespace boost {
45 namespace asio {
46 namespace ip {
47 class address;
48 } // namespace ip
49 } // namespace asio
50
51 namespace filesystem {
52 class path;
53 }
54
55 namespace process::v1 {
56 class child;
57 class group;
58 template<typename Char>
64 } // namespace process::v1
65} // namespace boost
66#endif
67namespace video {
68 struct config_t;
69} // namespace video
70
71namespace nvenc {
72 class nvenc_base;
73}
74
75namespace platf {
76 // Limited by bits in activeGamepadMask
77 constexpr auto MAX_GAMEPADS = 16;
78
79 constexpr std::uint32_t DPAD_UP = 0x0001;
80 constexpr std::uint32_t DPAD_DOWN = 0x0002;
81 constexpr std::uint32_t DPAD_LEFT = 0x0004;
82 constexpr std::uint32_t DPAD_RIGHT = 0x0008;
83 constexpr std::uint32_t START = 0x0010;
84 constexpr std::uint32_t BACK = 0x0020;
85 constexpr std::uint32_t LEFT_STICK = 0x0040;
86 constexpr std::uint32_t RIGHT_STICK = 0x0080;
87 constexpr std::uint32_t LEFT_BUTTON = 0x0100;
88 constexpr std::uint32_t RIGHT_BUTTON = 0x0200;
89 constexpr std::uint32_t HOME = 0x0400;
90 constexpr std::uint32_t A = 0x1000;
91 constexpr std::uint32_t B = 0x2000;
92 constexpr std::uint32_t X = 0x4000;
93 constexpr std::uint32_t Y = 0x8000;
94 constexpr std::uint32_t PADDLE1 = 0x010000;
95 constexpr std::uint32_t PADDLE2 = 0x020000;
96 constexpr std::uint32_t PADDLE3 = 0x040000;
97 constexpr std::uint32_t PADDLE4 = 0x080000;
98 constexpr std::uint32_t TOUCHPAD_BUTTON = 0x100000;
99 constexpr std::uint32_t MISC_BUTTON = 0x200000;
100
105 std::string name;
107 std::string reason_disabled;
108 };
109
120
133 static gamepad_feedback_msg_t make_rumble(std::uint16_t id, std::uint16_t lowfreq, std::uint16_t highfreq) {
135 msg.type = gamepad_feedback_e::rumble;
136 msg.id = id;
137 msg.data.rumble = {lowfreq, highfreq};
138 return msg;
139 }
140
149 static gamepad_feedback_msg_t make_rumble_triggers(std::uint16_t id, std::uint16_t left, std::uint16_t right) {
151 msg.type = gamepad_feedback_e::rumble_triggers;
152 msg.id = id;
153 msg.data.rumble_triggers = {left, right};
154 return msg;
155 }
156
165 static gamepad_feedback_msg_t make_motion_event_state(std::uint16_t id, std::uint8_t motion_type, std::uint16_t report_rate) {
167 msg.type = gamepad_feedback_e::set_motion_event_state;
168 msg.id = id;
169 msg.data.motion_event_state.motion_type = motion_type;
170 msg.data.motion_event_state.report_rate = report_rate;
171 return msg;
172 }
173
183 static gamepad_feedback_msg_t make_rgb_led(std::uint16_t id, std::uint8_t r, std::uint8_t g, std::uint8_t b) {
185 msg.type = gamepad_feedback_e::set_rgb_led;
186 msg.id = id;
187 msg.data.rgb_led = {r, g, b};
188 return msg;
189 }
190
202 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) {
204 msg.type = gamepad_feedback_e::set_adaptive_triggers;
205 msg.id = id;
206 msg.data.adaptive_triggers = {.event_flags = event_flags, .type_left = type_left, .type_right = type_right, .left = left, .right = right};
207 return msg;
208 }
209
211 std::uint16_t id;
212
213 union {
214 struct {
215 std::uint16_t lowfreq;
216 std::uint16_t highfreq;
217 } rumble;
218
219 struct {
220 std::uint16_t left_trigger;
221 std::uint16_t right_trigger;
222 } rumble_triggers;
223
224 struct {
225 std::uint16_t report_rate;
226 std::uint8_t motion_type;
227 } motion_event_state;
228
229 struct {
230 std::uint8_t r;
231 std::uint8_t g;
232 std::uint8_t b;
233 } rgb_led;
234
235 struct {
236 uint16_t controllerNumber;
237 uint8_t event_flags;
238 uint8_t type_left;
239 uint8_t type_right;
240 std::array<uint8_t, 10> left;
241 std::array<uint8_t, 10> right;
242 } adaptive_triggers;
244 };
245
250
251 namespace speaker {
266
270 constexpr std::uint8_t map_stereo[] {
273 };
277 constexpr std::uint8_t map_surround51[] {
282 BACK_LEFT,
284 };
288 constexpr std::uint8_t map_surround71[] {
293 BACK_LEFT,
295 SIDE_LEFT,
297 };
298 } // namespace speaker
299
303 enum class mem_type_e {
304 system,
305 vaapi,
306 dxgi,
307 cuda,
308 videotoolbox,
309 vulkan,
310 unknown
311 };
312
316 enum class pix_fmt_e {
317 yuv420p,
318 yuv420p10,
319 nv12,
320 p010,
321 ayuv,
322 yuv444p16,
323 yuv444p,
324 y410,
325 unknown
326 };
327
334 inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
335 using namespace std::literals;
336#ifndef DOXYGEN
337 #define _CONVERT(x) \
338 case pix_fmt_e::x: \
339 return #x##sv
340#endif
341 switch (pix_fmt) {
342 _CONVERT(yuv420p);
343 _CONVERT(yuv420p10);
344 _CONVERT(nv12);
345 _CONVERT(p010);
346 _CONVERT(ayuv);
347 _CONVERT(yuv444p16);
348 _CONVERT(yuv444p);
349 _CONVERT(y410);
350 _CONVERT(unknown);
351 }
352#undef _CONVERT
353
354 return "unknown"sv;
355 }
356
357 // Dimensions for touchscreen input
369
370 // These values must match Limelight-internal.h's SS_FF_* constants!
371 namespace platform_caps {
375 typedef uint32_t caps_t;
376
380 constexpr caps_t pen_touch = 0x01; // Pen and touch events
384 constexpr caps_t controller_touch = 0x02; // Controller touch events
385 }; // namespace platform_caps
386
391 std::uint32_t buttonFlags;
392 std::uint8_t lt;
393 std::uint8_t rt;
394 std::int16_t lsX;
395 std::int16_t lsY;
396 std::int16_t rsX;
397 std::int16_t rsY;
398 };
399
404 // The global index is used when looking up gamepads in the platform's
405 // gamepad array. It identifies gamepads uniquely among all clients.
407
408 // The client-relative index is the controller number as reported by the
409 // client. It must be used when communicating back to the client via
410 // the input feedback queue.
411 std::uint8_t clientRelativeIndex;
412 };
413
418 std::uint8_t type;
419 std::uint16_t capabilities;
420 std::uint32_t supportedButtons;
421 };
422
428 std::uint8_t eventType;
429 std::uint32_t pointerId;
430 float x;
431 float y;
432 float pressure;
433 };
434
440 std::uint8_t motionType;
441
442 // Accel: m/s^2
443 // Gyro: deg/s
444 float x;
445 float y;
446 float z;
447 };
448
454 std::uint8_t state;
455 std::uint8_t percentage;
456 };
457
462 std::uint8_t eventType;
463 std::uint16_t rotation;
464 std::uint32_t pointerId;
465 float x;
466 float y;
470 };
471
475 struct pen_input_t {
476 std::uint8_t eventType;
477 std::uint8_t toolType;
478 std::uint8_t penButtons;
479 std::uint8_t tilt;
480 std::uint16_t rotation;
481 float x;
482 float y;
486 };
487
491 class deinit_t {
492 public:
496 virtual ~deinit_t() = default;
497 };
498
502 struct img_t: std::enable_shared_from_this<img_t> {
503 public:
504 img_t() = default;
505
506 img_t(img_t &&) = delete;
507 img_t(const img_t &) = delete;
508 img_t &operator=(img_t &&) = delete;
509 img_t &operator=(const img_t &) = delete;
510
511 std::uint8_t *data {};
512 std::int32_t width {};
513 std::int32_t height {};
514 std::int32_t pixel_pitch {};
515 std::int32_t row_pitch {};
516
517 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
518
522 virtual ~img_t() = default;
523 };
524
528 struct sink_t {
529 // Play on host PC
530 std::string host;
531
532 // On macOS and Windows, it is not possible to create a virtual sink
533 // Therefore, it is optional
537 struct null_t {
538 std::string stereo;
539 std::string surround51;
540 std::string surround71;
541 };
542
543 std::optional<null_t> null;
544 };
545
550 virtual ~encode_device_t() = default;
551
558 virtual int convert(platf::img_t &img) = 0;
559
561 };
562
567 void *data {};
568 AVFrame *frame {};
569
576 int convert(platf::img_t &img) override {
577 return -1;
578 }
579
583 virtual void apply_colorspace() {
584 }
585
594 virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx) {
595 BOOST_LOG(error) << "Illegal call to hwdevice_t::set_frame(). Did you forget to override it?";
596 return -1;
597 };
598
605 virtual void init_hwframes(AVHWFramesContext *frames) {};
606
614 virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options) {};
615
623 virtual int prepare_to_derive_context(int hw_device_type) {
624 return 0;
625 };
626 };
627
639 virtual bool init_encoder(const video::config_t &client_config, const video::sunshine_colorspace_t &colorspace) = 0;
640
642 };
643
647 enum class capture_e : int {
648 ok,
649 reinit,
650 timeout,
652 error
653 };
654
658 class display_t {
659 public:
667 using push_captured_image_cb_t = std::function<bool(std::shared_ptr<img_t> &&img, bool frame_captured)>;
668
676 using pull_free_image_cb_t = std::function<bool(std::shared_ptr<img_t> &img_out)>;
677
678 display_t() noexcept = default;
679
692 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;
693
699 virtual std::shared_ptr<img_t> alloc_img() = 0;
700
707 virtual int dummy_img(img_t *img) = 0;
708
716 return nullptr;
717 }
718
725 virtual std::unique_ptr<nvenc_encode_device_t> make_nvenc_encode_device(pix_fmt_e pix_fmt) {
726 return nullptr;
727 }
728
734 virtual bool is_hdr() {
735 return false;
736 }
737
744 virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata) {
745 std::memset(&metadata, 0, sizeof(metadata));
746 return false;
747 }
748
755 virtual bool is_codec_supported(std::string_view name, const ::video::config_t &config) {
756 return true;
757 }
758
759 virtual ~display_t() = default;
760
761 // Offsets for when streaming a specific monitor. By default, they are 0.
762 int offset_x {0};
763 int offset_y {0};
764 int env_width {0};
765 int env_height {0};
768 int width {0};
769 int height {0};
772
773 protected:
774 // collect capture timing data (at loglevel debug)
776 };
777
781 class mic_t {
782 public:
789 virtual capture_e sample(std::vector<float> &frame_buffer) = 0;
790
791 virtual ~mic_t() = default;
792 };
793
798 public:
805 virtual int set_sink(const std::string &sink) = 0;
806
818 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;
819
825 virtual bool is_sink_available(const std::string &sink) = 0;
826
832 virtual std::optional<sink_t> sink_info() = 0;
833
837 virtual ~audio_control_t() = default;
838 };
839
845 void freeInput(void *);
846
851
852 std::filesystem::path appdata();
853
860 std::string get_mac_address(const std::string_view &address);
861
868 std::string from_sockaddr(const sockaddr *const);
875 std::pair<std::uint16_t, std::string> from_sockaddr_ex(const sockaddr *const);
876
882 std::unique_ptr<audio_control_t> audio_control();
883
892 std::shared_ptr<display_t> display(mem_type_e hwdevice_type, const std::string &display_name, const video::config_t &config);
893
894 // A list of names of displays accepted as display_name with the mem_type_e
901 std::vector<std::string> display_names(mem_type_e hwdevice_type);
902
908
922 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);
923
927 enum class thread_priority_e : int {
928 low,
929 normal,
930 high,
931 critical
932 };
938 void adjust_thread_priority(thread_priority_e priority);
939
946 void set_thread_name(const std::string &name);
947
948 void enable_mouse_keys();
949
950 // Allow OS-specific actions to be taken to prepare for streaming
951 void streaming_will_start();
952 void streaming_will_stop();
953
954 void restart();
955
960 const char *buffer;
961 size_t size;
962
963 // Constructors required for emplace_back() prior to C++20
970 buffer_descriptor_t(const char *buffer, size_t size):
971 buffer(buffer),
972 size(size) {
973 }
974
976 buffer(nullptr),
977 size(0) {
978 }
979 };
980
985 // Optional headers to be prepended to each packet
986 const char *headers;
987 size_t header_size;
988
989 // One or more data buffers to use for the payloads
990 //
991 // NB: Data buffers must be aligned to payload size!
992 std::vector<buffer_descriptor_t> &payload_buffers;
994
995 // The offset (in header+payload message blocks) in the header and payload
996 // buffers to begin sending messages from
998
999 // The number of header+payload message blocks to send
1001
1002 std::uintptr_t native_socket;
1003 boost::asio::ip::address &target_address;
1004 uint16_t target_port;
1005 boost::asio::ip::address &source_address;
1006
1013 for (const auto &desc : payload_buffers) {
1014 if (offset < desc.size) {
1015 return {
1016 desc.buffer + offset,
1017 desc.size - offset,
1018 };
1019 } else {
1020 offset -= desc.size;
1021 }
1022 }
1023 return {};
1024 }
1025 };
1026
1033 bool send_batch(batched_send_info_t &send_info);
1034
1039 const char *header;
1041 const char *payload;
1043
1044 std::uintptr_t native_socket;
1045 boost::asio::ip::address &target_address;
1046 uint16_t target_port;
1047 boost::asio::ip::address &source_address;
1048 };
1049
1056 bool send(send_info_t &send_info);
1057
1061 enum class qos_data_type_e : int {
1062 audio,
1063 video
1064 };
1065
1076 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);
1077
1082 void open_url(const std::string &url);
1083
1089 bool request_process_group_exit(std::uintptr_t native_handle);
1090
1096 bool process_group_running(std::uintptr_t native_handle);
1097
1103 input_t input();
1112 util::point_t get_mouse_loc(input_t &input);
1120 void move_mouse(input_t &input, int deltaX, int deltaY);
1129 void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y);
1137 void button_mouse(input_t &input, int button, bool release);
1144 void scroll(input_t &input, int distance);
1151 void hscroll(input_t &input, int distance);
1160 void keyboard_update(input_t &input, uint16_t modcode, bool release, uint8_t flags);
1161 void gamepad_update(input_t &input, int nr, const gamepad_state_t &gamepad_state);
1169 void unicode(input_t &input, char *utf8, int size);
1170
1175
1181 std::unique_ptr<client_input_t> allocate_client_input_context(input_t &input);
1182
1189 void touch_update(client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch);
1190
1197 void pen_update(client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen);
1198
1204 void gamepad_touch(input_t &input, const gamepad_touch_t &touch);
1205
1211 void gamepad_motion(input_t &input, const gamepad_motion_t &motion);
1212
1218 void gamepad_battery(input_t &input, const gamepad_battery_t &battery);
1219
1228 int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
1235 void free_gamepad(input_t &input, int nr);
1236
1242
1243 constexpr auto SERVICE_NAME = "Sunshine";
1244 constexpr auto SERVICE_TYPE = "_nvstream._tcp";
1245
1246 namespace publish {
1247 [[nodiscard]] std::unique_ptr<deinit_t> start();
1248 }
1249
1255 [[nodiscard]] std::unique_ptr<deinit_t> init();
1256
1261 std::string get_host_name();
1262
1270 std::string resolve_render_device();
1271
1278 std::vector<supported_gamepad_t> &supported_gamepads(input_t *input);
1279
1283 struct high_precision_timer: private boost::noncopyable {
1284 virtual ~high_precision_timer() = default;
1285
1290 virtual void sleep_for(const std::chrono::nanoseconds &duration) = 0;
1291
1296 virtual operator bool() = 0;
1297 };
1298
1303 std::unique_ptr<high_precision_timer> create_high_precision_timer();
1304
1310 bool has_elevated_privileges(bool all_caps);
1311
1316 void drop_elevated_privileges(bool all_caps);
1317
1318} // namespace platf
A helper class for tracking and logging short time intervals across a period of time.
Definition logging.h:204
Abstract platform-agnostic base of standalone NVENC encoder. Derived classes perform platform-specifi...
Definition nvenc_base.h:26
Platform audio controller that manages sinks and microphone capture.
Definition common.h:797
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:491
virtual ~deinit_t()=default
Destroy the deinitializer.
Abstract display capture backend used by the streaming pipeline.
Definition common.h:658
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:755
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:667
virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata)
Read HDR metadata for the active display mode.
Definition common.h:744
int env_height
Height of the full capture environment in physical pixels.
Definition common.h:765
int env_logical_height
Height of the full capture environment after display scaling.
Definition common.h:767
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:676
logging::time_delta_periodic_logger sleep_overshoot_logger
Periodic logger for capture sleep overshoot measurements.
Definition common.h:775
int offset_y
Vertical capture offset in physical pixels.
Definition common.h:763
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:725
int width
Width of the captured display in physical pixels.
Definition common.h:768
int logical_height
Height of the captured display after display scaling.
Definition common.h:771
int env_width
Width of the full capture environment in physical pixels.
Definition common.h:764
int height
Height of the captured display in physical pixels.
Definition common.h:769
virtual std::unique_ptr< avcodec_encode_device_t > make_avcodec_encode_device(pix_fmt_e pix_fmt)
Create AVCodec encode device.
Definition common.h:715
int offset_x
Horizontal capture offset in physical pixels.
Definition common.h:762
virtual bool is_hdr()
Report whether the active display mode is HDR.
Definition common.h:734
int env_logical_width
Width of the full capture environment after display scaling.
Definition common.h:766
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:770
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:781
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:797
Unique pointer wrapper with customizable pointer and deleter types.
Definition utility.h:820
mem_type_e
Enumerates supported mem type options.
Definition common.h:303
@ system
System memory.
constexpr std::uint32_t B
Moonlight gamepad button mask bit for B.
Definition common.h:91
speaker_e
Enumerates supported speaker options.
Definition common.h:255
@ BACK_LEFT
Back left.
Definition common.h:260
@ LOW_FREQUENCY
Low frequency.
Definition common.h:259
@ SIDE_RIGHT
Side right.
Definition common.h:263
@ MAX_SPEAKERS
Maximum number of speakers.
Definition common.h:264
@ FRONT_LEFT
Front left.
Definition common.h:256
@ SIDE_LEFT
Side left.
Definition common.h:262
@ FRONT_CENTER
Front center.
Definition common.h:258
@ BACK_RIGHT
Back right.
Definition common.h:261
@ FRONT_RIGHT
Front right.
Definition common.h:257
constexpr std::uint32_t LEFT_BUTTON
Moonlight gamepad button mask bit for left shoulder.
Definition common.h:87
constexpr std::uint32_t DPAD_LEFT
Moonlight gamepad button mask bit for D-pad left.
Definition common.h:81
std::string get_mac_address(const std::string_view &address)
Return the hardware MAC address associated with a network address.
Definition misc.cpp:364
constexpr std::uint32_t DPAD_UP
Moonlight gamepad button mask bit for D-pad up.
Definition common.h:79
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:1254
constexpr std::uint32_t PADDLE3
Moonlight gamepad button mask bit for paddle 3.
Definition common.h:96
constexpr auto SERVICE_TYPE
mDNS service type advertised for GameStream discovery.
Definition common.h:1244
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:345
pix_fmt_e
Enumerates supported pix fmt options.
Definition common.h:316
@ yuv420p10
YUV 4:2:0 10-bit.
@ yuv444p
Planar 8-bit YUV 4:4:4.
@ yuv444p16
Planar 10-bit (shifted to 16-bit) YUV 4:4:4.
@ yuv420p
YUV 4:2:0.
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 inputtino.cpp:109
constexpr std::uint32_t HOME
Moonlight gamepad button mask bit for Home.
Definition common.h:89
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:1467
std::filesystem::path appdata()
Performs migration if necessary, then returns the appdata directory.
Definition misc.cpp:251
constexpr std::uint32_t TOUCHPAD_BUTTON
Moonlight gamepad button mask bit for touchpad click.
Definition common.h:98
constexpr std::uint32_t LEFT_STICK
Moonlight gamepad button mask bit for left stick press.
Definition common.h:85
constexpr std::uint32_t DPAD_RIGHT
Moonlight gamepad button mask bit for D-pad right.
Definition common.h:82
constexpr std::uint32_t PADDLE2
Moonlight gamepad button mask bit for paddle 2.
Definition common.h:95
constexpr std::uint32_t RIGHT_STICK
Moonlight gamepad button mask bit for right stick press.
Definition common.h:86
qos_data_type_e
Identifies traffic classes used for socket QoS tagging.
Definition common.h:1061
std::unique_ptr< high_precision_timer > create_high_precision_timer()
Create platform-specific timer capable of high-precision sleep.
Definition misc.cpp:1394
std::string_view from_pix_fmt(pix_fmt_e pix_fmt)
Convert a Sunshine pixel format enum to its string name.
Definition common.h:334
constexpr std::uint8_t map_surround51[]
Moonlight speaker order for 5.1 surround audio.
Definition common.h:277
uint32_t caps_t
Bitset containing platform capability flags.
Definition common.h:375
std::unique_ptr< audio_control_t > audio_control()
Create the platform audio controller.
Definition audio.cpp:678
constexpr std::uint32_t PADDLE1
Moonlight gamepad button mask bit for paddle 1.
Definition common.h:94
std::unique_ptr< client_input_t > allocate_client_input_context(input_t &input)
Allocate a context to store per-client input data.
Definition inputtino.cpp:31
constexpr std::uint32_t PADDLE4
Moonlight gamepad button mask bit for paddle 4.
Definition common.h:97
constexpr std::uint8_t map_stereo[]
Moonlight speaker order for stereo audio.
Definition common.h:270
constexpr caps_t pen_touch
Capability bit indicating native pen and touch support.
Definition common.h:380
constexpr std::uint32_t A
Moonlight gamepad button mask bit for A.
Definition common.h:90
constexpr auto SERVICE_NAME
mDNS service instance name advertised for GameStream discovery.
Definition common.h:1243
constexpr std::uint8_t map_surround71[]
Moonlight speaker order for 7.1 surround audio.
Definition common.h:288
bool send(send_info_t &send_info)
Send the serialized response over the active socket.
Definition misc.cpp:867
constexpr std::uint32_t DPAD_DOWN
Moonlight gamepad button mask bit for D-pad down.
Definition common.h:80
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 inputtino.cpp:104
void gamepad_motion(input_t &input, const gamepad_motion_t &motion)
Send a gamepad motion event to the OS.
Definition inputtino.cpp:132
bool needs_encoder_reenumeration()
Check if GPUs/drivers have changed since the last call to this function.
Definition misc.cpp:1249
gamepad_feedback_e
Enumerates supported gamepad feedback options.
Definition common.h:113
@ rumble_triggers
Rumble triggers.
@ set_adaptive_triggers
Set adaptive triggers.
@ set_motion_event_state
Set motion event state.
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:84
thread_priority_e
Enumerates supported thread priority options.
Definition common.h:927
@ critical
Critical priority.
@ high
High priority.
@ normal
Normal priority.
void drop_elevated_privileges(bool all_caps)
Drop elevated privileges (e.g. system admin/nice etc.)
Definition misc.cpp:1496
std::string from_sockaddr(const sockaddr *const)
Convert a socket address to a printable IP address.
Definition misc.cpp:329
constexpr std::uint32_t RIGHT_BUTTON
Moonlight gamepad button mask bit for right shoulder.
Definition common.h:88
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:249
constexpr std::uint32_t START
Moonlight gamepad button mask bit for Start.
Definition common.h:83
constexpr std::uint32_t MISC_BUTTON
Moonlight gamepad button mask bit for the miscellaneous button.
Definition common.h:99
void gamepad_battery(input_t &input, const gamepad_battery_t &battery)
Send a gamepad battery event to the OS.
Definition inputtino.cpp:137
constexpr std::uint32_t X
Moonlight gamepad button mask bit for X.
Definition common.h:92
platform_caps::caps_t get_capabilities()
Get the supported platform capabilities to advertise to the client.
Definition inputtino.cpp:142
std::vector< std::string > display_names(mem_type_e hwdevice_type)
List display names accepted by the selected capture backend.
Definition misc.cpp:1209
void gamepad_touch(input_t &input, const gamepad_touch_t &touch)
Send a gamepad touch event to the OS.
Definition inputtino.cpp:127
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 inputtino.cpp:99
constexpr caps_t controller_touch
Capability bit indicating controller touchpad support.
Definition common.h:384
capture_e
Enumerates supported capture options.
Definition common.h:647
@ timeout
Timeout.
@ reinit
Need to reinitialize.
@ interrupted
Capture was interrupted.
constexpr std::uint32_t Y
Moonlight gamepad button mask bit for Y.
Definition common.h:93
void freeInput(void *)
Release a platform input backend created by input().
Definition inputtino.cpp:38
Declarations for the configuration of Sunshine.
avahi::EntryGroup * group
Active Avahi entry group that owns the published service.
Definition publish.cpp:398
bl::sources::severity_logger< int > debug
Follow what is happening.
Definition logging.cpp:41
bl::sources::severity_logger< int > error
Recoverable errors.
Definition logging.cpp:44
boost::shared_ptr< boost::log::sinks::asynchronous_sink< boost::log::sinks::text_ostream_backend > > sink
Sink.
Definition logging.cpp:38
Declarations for logging related functions.
Standalone NVENC encoder.
Definition nvenc_base.cpp:94
AVCodec-backed encode device and frame state.
Definition common.h:566
int convert(platf::img_t &img) override
Convert a captured image into the encoder input representation.
Definition common.h:576
virtual int prepare_to_derive_context(int hw_device_type)
Prepare to derive a context.
Definition common.h:623
virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options)
Provides a hook for allow platform-specific code to adjust codec options.
Definition common.h:614
virtual void apply_colorspace()
Apply the configured colorspace metadata to the active frame.
Definition common.h:583
AVFrame * frame
FFmpeg frame currently owned by the encode device.
Definition common.h:568
virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx)
Set the frame to be encoded.
Definition common.h:594
void * data
Backend-specific conversion state.
Definition common.h:567
virtual void init_hwframes(AVHWFramesContext *frames)
Initialize the hwframes context.
Definition common.h:605
Buffers and native metadata for one batched send operation.
Definition common.h:984
boost::asio::ip::address & source_address
Local source IP address for outgoing packets.
Definition common.h:1005
const char * headers
Optional header bytes prepended to each payload.
Definition common.h:986
std::vector< buffer_descriptor_t > & payload_buffers
Payload buffers containing one or more packet blocks.
Definition common.h:992
buffer_descriptor_t buffer_for_payload_offset(ptrdiff_t offset)
Returns a payload buffer descriptor for the given payload offset.
Definition common.h:1012
std::uintptr_t native_socket
Platform socket handle used for the send operation.
Definition common.h:1002
size_t block_count
Number of packet blocks to send.
Definition common.h:1000
uint16_t target_port
Destination UDP port for outgoing packets.
Definition common.h:1004
size_t block_offset
First packet block index to send.
Definition common.h:997
size_t header_size
Header size in bytes.
Definition common.h:987
size_t payload_size
Payload size in bytes for each packet block.
Definition common.h:993
boost::asio::ip::address & target_address
Destination IP address for outgoing packets.
Definition common.h:1003
Platform buffer pointer and size for batched socket sends.
Definition common.h:959
size_t size
Size of the buffer in bytes.
Definition common.h:961
buffer_descriptor_t(const char *buffer, size_t size)
Describe a contiguous byte buffer for batched socket sending.
Definition common.h:970
const char * buffer
Pointer to the payload buffer.
Definition common.h:960
Base interface for hardware or software frame conversion.
Definition common.h:549
video::sunshine_colorspace_t colorspace
Colorspace metadata expected by the encoder.
Definition common.h:560
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:417
std::uint8_t type
Protocol or controller type discriminator.
Definition common.h:418
std::uint32_t supportedButtons
Button mask supported by the connected controller.
Definition common.h:420
std::uint16_t capabilities
Capability flags advertised by the controller.
Definition common.h:419
Battery state reported by a virtual gamepad.
Definition common.h:452
std::uint8_t state
Battery state reported by the client.
Definition common.h:454
std::uint8_t percentage
Battery charge percentage.
Definition common.h:455
gamepad_id_t id
Gamepad identifier for the event.
Definition common.h:453
Feedback command sent from Sunshine to a virtual gamepad.
Definition common.h:124
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:165
std::uint16_t id
Controller identifier associated with this message.
Definition common.h:211
union platf::gamepad_feedback_msg_t::@13 data
Controller feedback payload for the selected feedback type.
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:133
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:183
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:149
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:202
gamepad_feedback_e type
Feedback command type stored in the union payload.
Definition common.h:210
Global and client-relative identifiers for a virtual gamepad.
Definition common.h:403
int globalIndex
Index unique across all connected clients.
Definition common.h:406
std::uint8_t clientRelativeIndex
Client relative index.
Definition common.h:411
Accelerometer or gyroscope sample from a controller.
Definition common.h:438
float z
Depth or Z-axis vector component.
Definition common.h:446
float x
Horizontal coordinate or vector component.
Definition common.h:444
gamepad_id_t id
Gamepad identifier for the event.
Definition common.h:439
float y
Vertical coordinate or vector component.
Definition common.h:445
std::uint8_t motionType
Motion type.
Definition common.h:440
Button and axis state received for a virtual gamepad.
Definition common.h:390
std::int16_t rsX
Right stick X-axis value.
Definition common.h:396
std::uint32_t buttonFlags
Moonlight button mask for the current gamepad state.
Definition common.h:391
std::uint8_t lt
Left trigger value.
Definition common.h:392
std::int16_t lsX
Left stick X-axis value.
Definition common.h:394
std::int16_t rsY
Right stick Y-axis value.
Definition common.h:397
std::uint8_t rt
Right trigger value.
Definition common.h:393
std::int16_t lsY
Left stick Y-axis value.
Definition common.h:395
Touchpad contact data reported by a controller.
Definition common.h:426
float pressure
Contact pressure reported by the client.
Definition common.h:432
std::uint32_t pointerId
Client-provided pointer identifier for a touch contact.
Definition common.h:429
float y
Vertical coordinate or vector component.
Definition common.h:431
std::uint8_t eventType
Moonlight event type for the input packet.
Definition common.h:428
float x
Horizontal coordinate or vector component.
Definition common.h:430
gamepad_id_t id
Gamepad identifier for the event.
Definition common.h:427
Platform timer object used for precise frame pacing.
Definition common.h:1283
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:502
std::int32_t row_pitch
Bytes between consecutive image rows.
Definition common.h:515
std::uint8_t * data
Pointer to the captured image buffer.
Definition common.h:511
std::int32_t pixel_pitch
Bytes per pixel in the image buffer.
Definition common.h:514
virtual ~img_t()=default
Destroy the image.
std::int32_t height
Image height in pixels.
Definition common.h:513
std::int32_t width
Image width in pixels.
Definition common.h:512
std::optional< std::chrono::steady_clock::time_point > frame_timestamp
Capture timestamp associated with the frame.
Definition common.h:517
NVENC-backed encode device state.
Definition common.h:631
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:475
std::uint8_t tilt
Degrees (0..90) or LI_TILT_UNKNOWN.
Definition common.h:479
float contactAreaMinor
Minor axis of the reported contact area.
Definition common.h:485
float x
Horizontal coordinate or vector component.
Definition common.h:481
std::uint8_t eventType
Moonlight event type for the input packet.
Definition common.h:476
std::uint8_t toolType
Pen tool type reported by the client.
Definition common.h:477
float y
Vertical coordinate or vector component.
Definition common.h:482
float pressureOrDistance
Distance for hover and pressure for contact.
Definition common.h:483
float contactAreaMajor
Major axis of the reported contact area.
Definition common.h:484
std::uint8_t penButtons
Button mask for the active pen tool.
Definition common.h:478
std::uint16_t rotation
Degrees (0..360) or LI_ROT_UNKNOWN.
Definition common.h:480
Destination address and payload data for one UDP send.
Definition common.h:1038
const char * payload
Payload bytes to send after the header.
Definition common.h:1041
std::uintptr_t native_socket
Platform socket handle used for the send operation.
Definition common.h:1044
boost::asio::ip::address & target_address
Destination IP address for outgoing packets.
Definition common.h:1045
size_t payload_size
Payload size in bytes for each packet block.
Definition common.h:1042
uint16_t target_port
Destination UDP port for outgoing packets.
Definition common.h:1046
size_t header_size
Header size in bytes.
Definition common.h:1040
const char * header
Optional header bytes prepended to the payload.
Definition common.h:1039
boost::asio::ip::address & source_address
Local source IP address for outgoing packets.
Definition common.h:1047
Optional virtual sink names for each supported channel layout.
Definition common.h:537
std::string surround51
Virtual sink name for 5.1 surround audio.
Definition common.h:539
std::string stereo
Virtual sink name for stereo audio.
Definition common.h:538
std::string surround71
Virtual sink name for 7.1 surround audio.
Definition common.h:540
Host and virtual audio sink names for audio routing.
Definition common.h:528
std::string host
Host playback sink name.
Definition common.h:530
std::optional< null_t > null
Optional virtual sink names for monitor capture.
Definition common.h:543
Gamepad type exposed to clients and why it may be disabled.
Definition common.h:104
std::string reason_disabled
Human-readable reason the gamepad type is disabled.
Definition common.h:107
std::string name
Human-readable name for this item.
Definition common.h:105
bool is_enabled
Whether this gamepad type is currently available.
Definition common.h:106
Absolute touchscreen event data from the client.
Definition common.h:461
float x
Horizontal coordinate or vector component.
Definition common.h:465
std::uint32_t pointerId
Client-provided pointer identifier for a touch contact.
Definition common.h:464
float contactAreaMajor
Major axis of the reported contact area.
Definition common.h:468
float contactAreaMinor
Minor axis of the reported contact area.
Definition common.h:469
std::uint16_t rotation
Degrees (0..360) or LI_ROT_UNKNOWN.
Definition common.h:463
std::uint8_t eventType
Moonlight event type for the input packet.
Definition common.h:462
float y
Vertical coordinate or vector component.
Definition common.h:466
float pressureOrDistance
Distance for hover and pressure for contact.
Definition common.h:467
Touchscreen coordinate bounds used to scale absolute input.
Definition common.h:361
int logical_width
Logical width after display scaling.
Definition common.h:366
int height
Frame or display height in pixels.
Definition common.h:365
int offset_y
Vertical offset in physical pixels.
Definition common.h:363
int offset_x
Horizontal offset in physical pixels.
Definition common.h:362
int logical_height
Logical height after display scaling.
Definition common.h:367
int width
Frame or display width in pixels.
Definition common.h:364
Two-dimensional integer point.
Definition utility.h:1659
Encoding configuration requested by a remote client.
Definition video.h:28
Sunshine colorimetry values derived from stream configuration.
Definition video_colorspace.h:26
Declarations for thread-safe data structures.
Declarations for utility functions.
Declarations for colorspace functions.