15#include <boost/core/noncopyable.hpp>
17 #include <boost/asio.hpp>
18 #include <boost/process.hpp>
29#include <moonlight-common-c/src/Limelight.h>
32using namespace std::literals;
37struct AVHWFramesContext;
51 namespace filesystem {
55 namespace process::inline 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[] {
249 inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
250 using namespace std::literals;
271 int offset_x, offset_y;
276 namespace platform_caps {
277 typedef uint32_t caps_t;
279 constexpr caps_t pen_touch = 0x01;
280 constexpr caps_t controller_touch = 0x02;
284 std::uint32_t buttonFlags;
301 std::uint8_t clientRelativeIndex;
306 std::uint16_t capabilities;
307 std::uint32_t supportedButtons;
312 std::uint8_t eventType;
313 std::uint32_t pointerId;
321 std::uint8_t motionType;
333 std::uint8_t percentage;
337 std::uint8_t eventType;
338 std::uint16_t rotation;
339 std::uint32_t pointerId;
342 float pressureOrDistance;
343 float contactAreaMajor;
344 float contactAreaMinor;
348 std::uint8_t eventType;
349 std::uint8_t toolType;
350 std::uint8_t penButtons;
352 std::uint16_t rotation;
355 float pressureOrDistance;
356 float contactAreaMajor;
357 float contactAreaMinor;
365 struct img_t: std::enable_shared_from_this<img_t> {
374 std::uint8_t *data {};
375 std::int32_t width {};
376 std::int32_t height {};
377 std::int32_t pixel_pitch {};
378 std::int32_t row_pitch {};
380 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
382 virtual ~img_t() =
default;
393 std::string surround51;
394 std::string surround71;
397 std::optional<null_t> null;
416 virtual void apply_colorspace() {
423 virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx) {
424 BOOST_LOG(error) <<
"Illegal call to hwdevice_t::set_frame(). Did you forget to override it?";
502 virtual std::shared_ptr<img_t> alloc_img() = 0;
504 virtual int dummy_img(
img_t *img) = 0;
506 virtual std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e pix_fmt) {
510 virtual std::unique_ptr<nvenc_encode_device_t> make_nvenc_encode_device(
pix_fmt_e pix_fmt) {
514 virtual bool is_hdr() {
518 virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata) {
519 std::memset(&metadata, 0,
sizeof(metadata));
536 int offset_x, offset_y;
537 int env_width, env_height;
548 virtual capture_e sample(std::vector<float> &frame_buffer) = 0;
550 virtual ~mic_t() =
default;
555 virtual int set_sink(
const std::string &sink) = 0;
557 virtual std::unique_ptr<mic_t> microphone(
const std::uint8_t *mapping,
int channels, std::uint32_t sample_rate, std::uint32_t frame_size) = 0;
566 virtual std::optional<sink_t> sink_info() = 0;
571 void freeInput(
void *);
575 std::filesystem::path
appdata();
577 std::string get_mac_address(
const std::string_view &address);
579 std::string from_sockaddr(
const sockaddr *
const);
580 std::pair<std::uint16_t, std::string> from_sockaddr_ex(
const sockaddr *
const);
582 std::unique_ptr<audio_control_t> audio_control();
595 std::vector<std::string> display_names(
mem_type_e hwdevice_type);
603 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);
611 void adjust_thread_priority(thread_priority_e priority);
614 void streaming_will_start();
615 void streaming_will_stop();
625 int set_env(
const std::string &name,
const std::string &value);
632 int unset_env(
const std::string &name);
658 std::vector<buffer_descriptor_t> &payload_buffers;
668 std::uintptr_t native_socket;
669 boost::asio::ip::address &target_address;
670 uint16_t target_port;
671 boost::asio::ip::address &source_address;
679 for (
const auto &desc : payload_buffers) {
680 if (offset < desc.size) {
682 desc.buffer + offset,
693 bool send_batch(batched_send_info_t &send_info);
701 std::uintptr_t native_socket;
702 boost::asio::ip::address &target_address;
703 uint16_t target_port;
704 boost::asio::ip::address &source_address;
722 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);
728 void open_url(
const std::string &url);
735 bool request_process_group_exit(std::uintptr_t native_handle);
742 bool process_group_running(std::uintptr_t native_handle);
754 void move_mouse(input_t &input,
int deltaX,
int deltaY);
755 void abs_mouse(input_t &input,
const touch_port_t &touch_port,
float x,
float y);
756 void button_mouse(input_t &input,
int button,
bool release);
757 void scroll(input_t &input,
int distance);
758 void hscroll(input_t &input,
int distance);
759 void keyboard_update(input_t &input, uint16_t modcode,
bool release, uint8_t flags);
760 void gamepad_update(input_t &input,
int nr,
const gamepad_state_t &gamepad_state);
761 void unicode(input_t &input,
char *utf8,
int size);
763 typedef deinit_t client_input_t;
770 std::unique_ptr<client_input_t> allocate_client_input_context(input_t &input);
778 void touch_update(client_input_t *input,
const touch_port_t &touch_port,
const touch_input_t &touch);
786 void pen_update(client_input_t *input,
const touch_port_t &touch_port,
const pen_input_t &pen);
793 void gamepad_touch(input_t &input,
const gamepad_touch_t &touch);
800 void gamepad_motion(input_t &input,
const gamepad_motion_t &motion);
807 void gamepad_battery(input_t &input,
const gamepad_battery_t &battery);
817 int alloc_gamepad(input_t &input,
const gamepad_id_t &
id,
const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
818 void free_gamepad(input_t &input,
int nr);
824 platform_caps::caps_t get_capabilities();
826#define SERVICE_NAME "Sunshine"
827#define SERVICE_TYPE "_nvstream._tcp"
830 [[nodiscard]] std::unique_ptr<deinit_t> start();
833 [[nodiscard]] std::unique_ptr<deinit_t>
init();
856 virtual void sleep_for(
const std::chrono::nanoseconds &duration) = 0;
862 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:529
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:472
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:481
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:864
pix_fmt_e
Definition common.h:238
@ yuv420p10
YUV 4:2:0 10-bit.
@ yuv444p16
Planar 10-bit (shifted to 16-bit) YUV 4:4:4.
std::filesystem::path appdata()
Performs migration if necessary, then returns the appdata directory.
Definition misc.cpp:108
qos_data_type_e
Definition common.h:709
std::unique_ptr< high_precision_timer > create_high_precision_timer()
Create platform-specific timer capable of high-precision sleep.
Definition misc.cpp:971
bool needs_encoder_reenumeration()
Check if GPUs/drivers have changed since the last call to this function.
Definition misc.cpp:859
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:605
@ critical
Critical priority.
std::string get_host_name()
Returns the current computer name in UTF-8.
Definition misc.cpp:765
capture_e
Definition common.h:455
@ 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:87
virtual int prepare_to_derive_context(int hw_device_type)
Prepare to derive a context.
Definition common.h:444
virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options)
Provides a hook for allow platform-specific code to adjust codec options.
Definition common.h:438
virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx)
Set the frame to be encoded.
Definition common.h:423
virtual void init_hwframes(AVHWFramesContext *frames)
Initialize the hwframes context.
Definition common.h:432
buffer_descriptor_t buffer_for_payload_offset(ptrdiff_t offset)
Returns a payload buffer descriptor for the given payload offset.
Definition common.h:678
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.