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;
112 static gamepad_feedback_msg_t make_rumble(std::uint16_t
id, std::uint16_t lowfreq, std::uint16_t highfreq) {
114 msg.type = gamepad_feedback_e::rumble;
116 msg.data.rumble = {lowfreq, highfreq};
120 static gamepad_feedback_msg_t make_rumble_triggers(std::uint16_t
id, std::uint16_t left, std::uint16_t right) {
122 msg.type = gamepad_feedback_e::rumble_triggers;
124 msg.data.rumble_triggers = {left, right};
128 static gamepad_feedback_msg_t make_motion_event_state(std::uint16_t
id, std::uint8_t motion_type, std::uint16_t report_rate) {
130 msg.type = gamepad_feedback_e::set_motion_event_state;
132 msg.data.motion_event_state.motion_type = motion_type;
133 msg.data.motion_event_state.report_rate = report_rate;
137 static gamepad_feedback_msg_t make_rgb_led(std::uint16_t
id, std::uint8_t r, std::uint8_t g, std::uint8_t b) {
139 msg.type = gamepad_feedback_e::set_rgb_led;
141 msg.data.rgb_led = {r, g, b};
150 std::uint16_t lowfreq;
151 std::uint16_t highfreq;
155 std::uint16_t left_trigger;
156 std::uint16_t right_trigger;
160 std::uint16_t report_rate;
161 std::uint8_t motion_type;
162 } motion_event_state;
172 using feedback_queue_t = safe::mail_raw_t::queue_t<gamepad_feedback_msg_t>;
187 constexpr std::uint8_t map_stereo[] {
191 constexpr std::uint8_t map_surround51[] {
199 constexpr std::uint8_t map_surround71[] {
231 inline std::string_view from_pix_fmt(pix_fmt_e pix_fmt) {
232 using namespace std::literals;
253 int offset_x, offset_y;
258 namespace platform_caps {
259 typedef uint32_t caps_t;
261 constexpr caps_t pen_touch = 0x01;
262 constexpr caps_t controller_touch = 0x02;
266 std::uint32_t buttonFlags;
283 std::uint8_t clientRelativeIndex;
288 std::uint16_t capabilities;
289 std::uint32_t supportedButtons;
294 std::uint8_t eventType;
295 std::uint32_t pointerId;
303 std::uint8_t motionType;
315 std::uint8_t percentage;
319 std::uint8_t eventType;
320 std::uint16_t rotation;
321 std::uint32_t pointerId;
324 float pressureOrDistance;
325 float contactAreaMajor;
326 float contactAreaMinor;
330 std::uint8_t eventType;
331 std::uint8_t toolType;
332 std::uint8_t penButtons;
334 std::uint16_t rotation;
337 float pressureOrDistance;
338 float contactAreaMajor;
339 float contactAreaMinor;
347 struct img_t: std::enable_shared_from_this<img_t> {
356 std::uint8_t *data {};
357 std::int32_t width {};
358 std::int32_t height {};
359 std::int32_t pixel_pitch {};
360 std::int32_t row_pitch {};
362 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
364 virtual ~img_t() =
default;
375 std::string surround51;
376 std::string surround71;
379 std::optional<null_t> null;
398 virtual void apply_colorspace() {
405 virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx) {
406 BOOST_LOG(error) <<
"Illegal call to hwdevice_t::set_frame(). Did you forget to override it?";
484 virtual std::shared_ptr<img_t> alloc_img() = 0;
486 virtual int dummy_img(
img_t *img) = 0;
488 virtual std::unique_ptr<avcodec_encode_device_t> make_avcodec_encode_device(
pix_fmt_e pix_fmt) {
492 virtual std::unique_ptr<nvenc_encode_device_t> make_nvenc_encode_device(
pix_fmt_e pix_fmt) {
496 virtual bool is_hdr() {
500 virtual bool get_hdr_metadata(SS_HDR_METADATA &metadata) {
501 std::memset(&metadata, 0,
sizeof(metadata));
518 int offset_x, offset_y;
519 int env_width, env_height;
530 virtual capture_e sample(std::vector<float> &frame_buffer) = 0;
532 virtual ~mic_t() =
default;
537 virtual int set_sink(
const std::string &sink) = 0;
539 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;
548 virtual std::optional<sink_t> sink_info() = 0;
553 void freeInput(
void *);
557 std::filesystem::path
appdata();
559 std::string get_mac_address(
const std::string_view &address);
561 std::string from_sockaddr(
const sockaddr *
const);
562 std::pair<std::uint16_t, std::string> from_sockaddr_ex(
const sockaddr *
const);
564 std::unique_ptr<audio_control_t> audio_control();
577 std::vector<std::string> display_names(
mem_type_e hwdevice_type);
585 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);
593 void adjust_thread_priority(thread_priority_e priority);
596 void streaming_will_start();
597 void streaming_will_stop();
607 int set_env(
const std::string &name,
const std::string &value);
614 int unset_env(
const std::string &name);
640 std::vector<buffer_descriptor_t> &payload_buffers;
650 std::uintptr_t native_socket;
651 boost::asio::ip::address &target_address;
652 uint16_t target_port;
653 boost::asio::ip::address &source_address;
661 for (
const auto &desc : payload_buffers) {
662 if (offset < desc.size) {
664 desc.buffer + offset,
675 bool send_batch(batched_send_info_t &send_info);
683 std::uintptr_t native_socket;
684 boost::asio::ip::address &target_address;
685 uint16_t target_port;
686 boost::asio::ip::address &source_address;
704 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);
710 void open_url(
const std::string &url);
717 bool request_process_group_exit(std::uintptr_t native_handle);
724 bool process_group_running(std::uintptr_t native_handle);
736 void move_mouse(input_t &input,
int deltaX,
int deltaY);
737 void abs_mouse(input_t &input,
const touch_port_t &touch_port,
float x,
float y);
738 void button_mouse(input_t &input,
int button,
bool release);
739 void scroll(input_t &input,
int distance);
740 void hscroll(input_t &input,
int distance);
741 void keyboard_update(input_t &input, uint16_t modcode,
bool release, uint8_t flags);
742 void gamepad_update(input_t &input,
int nr,
const gamepad_state_t &gamepad_state);
743 void unicode(input_t &input,
char *utf8,
int size);
745 typedef deinit_t client_input_t;
752 std::unique_ptr<client_input_t> allocate_client_input_context(input_t &input);
760 void touch_update(client_input_t *input,
const touch_port_t &touch_port,
const touch_input_t &touch);
768 void pen_update(client_input_t *input,
const touch_port_t &touch_port,
const pen_input_t &pen);
775 void gamepad_touch(input_t &input,
const gamepad_touch_t &touch);
782 void gamepad_motion(input_t &input,
const gamepad_motion_t &motion);
789 void gamepad_battery(input_t &input,
const gamepad_battery_t &battery);
799 int alloc_gamepad(input_t &input,
const gamepad_id_t &
id,
const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue);
800 void free_gamepad(input_t &input,
int nr);
806 platform_caps::caps_t get_capabilities();
808#define SERVICE_NAME "Sunshine"
809#define SERVICE_TYPE "_nvstream._tcp"
812 [[nodiscard]] std::unique_ptr<deinit_t> start();
815 [[nodiscard]] std::unique_ptr<deinit_t>
init();
838 virtual void sleep_for(
const std::chrono::nanoseconds &duration) = 0;
844 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:511
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:454
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:463
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:211
speaker_e
Definition common.h:175
@ BACK_LEFT
Back left.
Definition common.h:180
@ LOW_FREQUENCY
Low frequency.
Definition common.h:179
@ SIDE_RIGHT
Side right.
Definition common.h:183
@ MAX_SPEAKERS
Maximum number of speakers.
Definition common.h:184
@ FRONT_LEFT
Front left.
Definition common.h:176
@ SIDE_LEFT
Side left.
Definition common.h:182
@ FRONT_CENTER
Front center.
Definition common.h:178
@ BACK_RIGHT
Back right.
Definition common.h:181
@ FRONT_RIGHT
Front right.
Definition common.h:177
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:220
@ 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:691
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_motion_event_state
Set motion event state.
@ set_rgb_led
Set RGB LED.
thread_priority_e
Definition common.h:587
@ 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:437
@ 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:426
virtual void init_codec_options(AVCodecContext *ctx, AVDictionary **options)
Provides a hook for allow platform-specific code to adjust codec options.
Definition common.h:420
virtual int set_frame(AVFrame *frame, AVBufferRef *hw_frames_ctx)
Set the frame to be encoded.
Definition common.h:405
virtual void init_hwframes(AVHWFramesContext *frames)
Initialize the hwframes context.
Definition common.h:414
buffer_descriptor_t buffer_for_payload_offset(ptrdiff_t offset)
Returns a payload buffer descriptor for the given payload offset.
Definition common.h:660
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.