15#include <boost/core/noncopyable.hpp>
17 #include <boost/asio.hpp>
18 #include <boost/process/v1.hpp>
29#include <moonlight-common-c/src/Limelight.h>
32using namespace std::literals;
37struct AVHWFramesContext;
51 namespace filesystem {
55 namespace process::v1 {
58 template<
typename Char>
74 constexpr auto MAX_GAMEPADS = 16;
76 constexpr std::uint32_t DPAD_UP = 0x0001;
77 constexpr std::uint32_t DPAD_DOWN = 0x0002;
78 constexpr std::uint32_t DPAD_LEFT = 0x0004;
79 constexpr std::uint32_t DPAD_RIGHT = 0x0008;
80 constexpr std::uint32_t START = 0x0010;
81 constexpr std::uint32_t BACK = 0x0020;
82 constexpr std::uint32_t LEFT_STICK = 0x0040;
83 constexpr std::uint32_t RIGHT_STICK = 0x0080;
84 constexpr std::uint32_t LEFT_BUTTON = 0x0100;
85 constexpr std::uint32_t RIGHT_BUTTON = 0x0200;
86 constexpr std::uint32_t HOME = 0x0400;
87 constexpr std::uint32_t A = 0x1000;
88 constexpr std::uint32_t B = 0x2000;
89 constexpr std::uint32_t X = 0x4000;
90 constexpr std::uint32_t Y = 0x8000;
91 constexpr std::uint32_t PADDLE1 = 0x010000;
92 constexpr std::uint32_t PADDLE2 = 0x020000;
93 constexpr std::uint32_t PADDLE3 = 0x040000;
94 constexpr std::uint32_t PADDLE4 = 0x080000;
95 constexpr std::uint32_t TOUCHPAD_BUTTON = 0x100000;
96 constexpr std::uint32_t MISC_BUTTON = 0x200000;
101 std::string reason_disabled;
113 static gamepad_feedback_msg_t make_rumble(std::uint16_t
id, std::uint16_t lowfreq, std::uint16_t highfreq) {
115 msg.type = gamepad_feedback_e::rumble;
117 msg.data.rumble = {lowfreq, highfreq};
121 static gamepad_feedback_msg_t make_rumble_triggers(std::uint16_t
id, std::uint16_t left, std::uint16_t right) {
123 msg.type = gamepad_feedback_e::rumble_triggers;
125 msg.data.rumble_triggers = {left, right};
129 static gamepad_feedback_msg_t make_motion_event_state(std::uint16_t
id, std::uint8_t motion_type, std::uint16_t report_rate) {
131 msg.type = gamepad_feedback_e::set_motion_event_state;
133 msg.data.motion_event_state.motion_type = motion_type;
134 msg.data.motion_event_state.report_rate = report_rate;
138 static gamepad_feedback_msg_t make_rgb_led(std::uint16_t
id, std::uint8_t r, std::uint8_t g, std::uint8_t b) {
140 msg.type = gamepad_feedback_e::set_rgb_led;
142 msg.data.rgb_led = {r, g, b};
146 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) {
148 msg.type = gamepad_feedback_e::set_adaptive_triggers;
150 msg.data.adaptive_triggers = {.event_flags = event_flags, .type_left = type_left, .type_right = type_right, .left = left, .right = right};
159 std::uint16_t lowfreq;
160 std::uint16_t highfreq;
164 std::uint16_t left_trigger;
165 std::uint16_t right_trigger;
169 std::uint16_t report_rate;
170 std::uint8_t motion_type;
171 } motion_event_state;
180 uint16_t controllerNumber;
184 std::array<uint8_t, 10> left;
185 std::array<uint8_t, 10> right;
190 using feedback_queue_t = safe::mail_raw_t::queue_t<gamepad_feedback_msg_t>;
205 constexpr std::uint8_t map_stereo[] {
209 constexpr std::uint8_t map_surround51[] {
217 constexpr std::uint8_t map_surround71[] {
250 inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
251 using namespace std::literals;
281 namespace platform_caps {
282 typedef uint32_t caps_t;
284 constexpr caps_t pen_touch = 0x01;
285 constexpr caps_t controller_touch = 0x02;
289 std::uint32_t buttonFlags;
306 std::uint8_t clientRelativeIndex;
311 std::uint16_t capabilities;
312 std::uint32_t supportedButtons;
317 std::uint8_t eventType;
318 std::uint32_t pointerId;
326 std::uint8_t motionType;
338 std::uint8_t percentage;
342 std::uint8_t eventType;
343 std::uint16_t rotation;
344 std::uint32_t pointerId;
347 float pressureOrDistance;
348 float contactAreaMajor;
349 float contactAreaMinor;
353 std::uint8_t eventType;
354 std::uint8_t toolType;
355 std::uint8_t penButtons;
357 std::uint16_t rotation;
360 float pressureOrDistance;
361 float contactAreaMajor;
362 float contactAreaMinor;
370 struct img_t: std::enable_shared_from_this<img_t> {
379 std::uint8_t *data {};
380 std::int32_t width {};
381 std::int32_t height {};
382 std::int32_t pixel_pitch {};
383 std::int32_t row_pitch {};
385 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
387 virtual ~img_t() =
default;
398 std::string surround51;
399 std::string surround71;
402 std::optional<null_t> null;
421 virtual void apply_colorspace() {
428 virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx) {
429 BOOST_LOG(error) <<
"Illegal call to hwdevice_t::set_frame(). Did you forget to override it?";
504 virtual std::shared_ptr<
img_t> alloc_img() = 0;
506 virtual
int dummy_img(
img_t *img) = 0;
512 virtual std::unique_ptr<nvenc_encode_device_t> make_nvenc_encode_device(
pix_fmt_e pix_fmt) {
516 virtual bool is_hdr() {
520 virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata) {
521 std::memset(&metadata, 0,
sizeof(metadata));
542 int env_logical_width {0};
543 int env_logical_height {0};
546 int logical_width {0};
547 int logical_height {0};
556 virtual capture_e sample(std::vector<float> &frame_buffer) = 0;
558 virtual ~mic_t() =
default;
563 virtual int set_sink(
const std::string &sink) = 0;
565 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;
574 virtual std::optional<sink_t> sink_info() = 0;
579 void freeInput(
void *);
583 std::filesystem::path
appdata();
585 std::string get_mac_address(
const std::string_view &address);
587 std::string from_sockaddr(
const sockaddr *
const);
588 std::pair<std::uint16_t, std::string> from_sockaddr_ex(
const sockaddr *
const);
590 std::unique_ptr<audio_control_t> audio_control();
603 std::vector<std::string> display_names(
mem_type_e hwdevice_type);
611 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);
619 void adjust_thread_priority(thread_priority_e priority);
625 void set_thread_name(
const std::string &name);
627 void enable_mouse_keys();
630 void streaming_will_start();
631 void streaming_will_stop();
641 int set_env(
const std::string &name,
const std::string &value);
648 int unset_env(
const std::string &name);
674 std::vector<buffer_descriptor_t> &payload_buffers;
684 std::uintptr_t native_socket;
685 boost::asio::ip::address &target_address;
686 uint16_t target_port;
687 boost::asio::ip::address &source_address;
695 for (
const auto &desc : payload_buffers) {
696 if (offset < desc.size) {
698 desc.buffer + offset,
709 bool send_batch(batched_send_info_t &send_info);
717 std::uintptr_t native_socket;
718 boost::asio::ip::address &target_address;
719 uint16_t target_port;
720 boost::asio::ip::address &source_address;
738 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);
744 void open_url(
const std::string &url);
751 bool request_process_group_exit(std::uintptr_t native_handle);
758 bool process_group_running(std::uintptr_t native_handle);
770 void move_mouse(input_t &input,
int deltaX,
int deltaY);
771 void abs_mouse(input_t &input,
const touch_port_t &touch_port,
float x,
float y);
772 void button_mouse(input_t &input,
int button,
bool release);
773 void scroll(input_t &input,
int distance);
774 void hscroll(input_t &input,
int distance);
775 void keyboard_update(input_t &input, uint16_t modcode,
bool release, uint8_t flags);
776 void gamepad_update(input_t &input,
int nr,
const gamepad_state_t &gamepad_state);
777 void unicode(input_t &input,
char *utf8,
int size);
779 typedef deinit_t client_input_t;
786 std::unique_ptr<client_input_t> allocate_client_input_context(input_t &input);
794 void touch_update(client_input_t *input,
const touch_port_t &touch_port,
const touch_input_t &touch);
802 void pen_update(client_input_t *input,
const touch_port_t &touch_port,
const pen_input_t &pen);
809 void gamepad_touch(input_t &input,
const gamepad_touch_t &touch);
816 void gamepad_motion(input_t &input,
const gamepad_motion_t &motion);
823 void gamepad_battery(input_t &input,
const gamepad_battery_t &battery);
833 int alloc_gamepad(input_t &input,
const gamepad_id_t &
id,
const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
834 void free_gamepad(input_t &input,
int nr);
840 platform_caps::caps_t get_capabilities();
842 constexpr auto SERVICE_NAME =
"Sunshine";
843 constexpr auto SERVICE_TYPE =
"_nvstream._tcp";
846 [[nodiscard]] std::unique_ptr<deinit_t> start();
849 [[nodiscard]] std::unique_ptr<deinit_t>
init();
881 virtual void sleep_for(
const std::chrono::nanoseconds &duration) = 0;
887 virtual operator bool() = 0;
A helper class for tracking and logging short time intervals across a period of time.
Definition logging.h:166
Abstract platform-agnostic base of standalone NVENC encoder. Derived classes perform platform-specifi...
Definition nvenc_base.h:26
virtual bool is_sink_available(const std::string &sink)=0
Check if the audio sink is available in the system.
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:531
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:477
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:486
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.
mem_type_e
Definition common.h:229
speaker_e
Definition common.h:193
@ BACK_LEFT
Back left.
Definition common.h:198
@ LOW_FREQUENCY
Low frequency.
Definition common.h:197
@ SIDE_RIGHT
Side right.
Definition common.h:201
@ MAX_SPEAKERS
Maximum number of speakers.
Definition common.h:202
@ FRONT_LEFT
Front left.
Definition common.h:194
@ SIDE_LEFT
Side left.
Definition common.h:200
@ FRONT_CENTER
Front center.
Definition common.h:196
@ BACK_RIGHT
Back right.
Definition common.h:199
@ FRONT_RIGHT
Front right.
Definition common.h:195
std::vector< supported_gamepad_t > & supported_gamepads(input_t *input)
Gets the supported gamepads for this platform backend.
Definition inputtino.cpp:130
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:1092
pix_fmt_e
Definition common.h:239
@ yuv420p10
YUV 4:2:0 10-bit.
@ yuv444p16
Planar 10-bit (shifted to 16-bit) YUV 4:4:4.
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:1294
std::filesystem::path appdata()
Performs migration if necessary, then returns the appdata directory.
Definition misc.cpp:140
qos_data_type_e
Definition common.h:725
std::unique_ptr< high_precision_timer > create_high_precision_timer()
Create platform-specific timer capable of high-precision sleep.
Definition misc.cpp:1226
bool needs_encoder_reenumeration()
Check if GPUs/drivers have changed since the last call to this function.
Definition misc.cpp:1087
gamepad_feedback_e
Definition common.h:104
@ rumble_triggers
Rumble triggers.
@ set_adaptive_triggers
Set adaptive triggers.
@ set_motion_event_state
Set motion event state.
@ set_rgb_led
Set RGB LED.
thread_priority_e
Definition common.h:613
@ critical
Critical priority.
void drop_elevated_privileges(bool all_caps)
Drop elevated privileges (e.g. system admin/nice etc.)
Definition misc.cpp:1323
std::string get_host_name()
Returns the current computer name in UTF-8.
Definition misc.cpp:957
std::string resolve_render_device()
Resolves the render device path to use for hardware encoding.
Definition misc.cpp:1278
capture_e
Definition common.h:460
@ reinit
Need to reinitialize.
@ interrupted
Capture was interrupted.
Declarations for the configuration of Sunshine.
std::unique_ptr< platf::deinit_t > init(const std::filesystem::path &persistence_filepath, const config::video_t &video_config)
Initialize the implementation and perform the initial state recovery (if needed).
Definition display_device.cpp:704
Declarations for logging related functions.
Standalone NVENC encoder.
Definition nvenc_base.cpp:90
virtual int prepare_to_derive_context(int hw_device_type)
Prepare to derive a context.
Definition common.h:449
virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options)
Provides a hook for allow platform-specific code to adjust codec options.
Definition common.h:443
virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx)
Set the frame to be encoded.
Definition common.h:428
virtual void init_hwframes(AVHWFramesContext *frames)
Initialize the hwframes context.
Definition common.h:437
buffer_descriptor_t buffer_for_payload_offset(ptrdiff_t offset)
Returns a payload buffer descriptor for the given payload offset.
Definition common.h:694
virtual void sleep_for(const std::chrono::nanoseconds &duration)=0
Sleep for the duration.
Definition video_colorspace.h:20
Declarations for thread-safe data structures.
Declarations for utility functions.
Declarations for colorspace functions.