Sunshine latest
Self-hosted game stream host for Moonlight.
common.h File Reference

Declarations for common platform specific utilities. More...

#include <bitset>
#include <filesystem>
#include <functional>
#include <mutex>
#include <string>
#include <boost/core/noncopyable.hpp>
#include "src/config.h"
#include "src/logging.h"
#include "src/thread_safe.h"
#include "src/utility.h"
#include "src/video_colorspace.h"
#include <moonlight-common-c/src/Limelight.h>
Include dependency graph for common.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  platf::audio_control_t
 
struct  platf::avcodec_encode_device_t
 
struct  platf::batched_send_info_t
 
struct  platf::buffer_descriptor_t
 
class  platf::deinit_t
 
class  platf::display_t
 
struct  platf::encode_device_t
 
struct  platf::gamepad_arrival_t
 
struct  platf::gamepad_battery_t
 
struct  platf::gamepad_feedback_msg_t
 
struct  platf::gamepad_id_t
 
struct  platf::gamepad_motion_t
 
struct  platf::gamepad_state_t
 
struct  platf::gamepad_touch_t
 
struct  platf::high_precision_timer
 
struct  platf::img_t
 
class  platf::mic_t
 
struct  platf::sink_t::null_t
 
struct  platf::nvenc_encode_device_t
 
struct  platf::pen_input_t
 
struct  platf::send_info_t
 
struct  platf::sink_t
 
struct  platf::supported_gamepad_t
 
struct  platf::touch_input_t
 
struct  platf::touch_port_t
 

Namespaces

namespace  nvenc
 Standalone NVENC encoder.
 

Macros

#define _CONVERT(x)
 
#define SERVICE_NAME   "Sunshine"
 
#define SERVICE_TYPE   "_nvstream._tcp"
 

Typedefs

typedef uint32_t platf::platform_caps::caps_t
 
typedef deinit_t platf::client_input_t
 
typedef basic_environment< char > boost::v1::environment
 
using platf::feedback_queue_t = safe::mail_raw_t::queue_t<gamepad_feedback_msg_t>
 
using platf::input_t = util::safe_ptr<void, freeInput>
 

Enumerations

enum class  platf::capture_e : int {
  ok , reinit , timeout , interrupted ,
  error
}
 
enum class  platf::gamepad_feedback_e { rumble , rumble_triggers , set_motion_event_state , set_rgb_led }
 
enum class  platf::mem_type_e {
  system , vaapi , dxgi , cuda ,
  videotoolbox , unknown
}
 
enum class  platf::pix_fmt_e {
  yuv420p , yuv420p10 , nv12 , p010 ,
  ayuv , yuv444p16 , y410 , unknown
}
 
enum class  platf::qos_data_type_e : int { audio , video }
 
enum  platf::speaker::speaker_e {
  platf::speaker::FRONT_LEFT , platf::speaker::FRONT_RIGHT , platf::speaker::FRONT_CENTER , platf::speaker::LOW_FREQUENCY ,
  platf::speaker::BACK_LEFT , platf::speaker::BACK_RIGHT , platf::speaker::SIDE_LEFT , platf::speaker::SIDE_RIGHT ,
  platf::speaker::MAX_SPEAKERS
}
 
enum class  platf::thread_priority_e : int { low , normal , high , critical }
 

Functions

void platf::abs_mouse (input_t &input, const touch_port_t &touch_port, float x, float y)
 
void platf::adjust_thread_priority (thread_priority_e priority)
 
int platf::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.
 
std::unique_ptr< client_input_tplatf::allocate_client_input_context (input_t &input)
 Allocate a context to store per-client input data.
 
std::filesystem::path platf::appdata ()
 Performs migration if necessary, then returns the appdata directory.
 
std::unique_ptr< audio_control_tplatf::audio_control ()
 
void platf::button_mouse (input_t &input, int button, bool release)
 
std::unique_ptr< high_precision_timerplatf::create_high_precision_timer ()
 Create platform-specific timer capable of high-precision sleep.
 
std::shared_ptr< display_tplatf::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 monitor that's compatible you can find If you require to use this parameter in a separate thread, make a copy of it.
 
std::vector< std::string > platf::display_names (mem_type_e hwdevice_type)
 
std::unique_ptr< deinit_tplatf::enable_socket_qos (uintptr_t native_socket, boost::asio::ip::address &address, uint16_t port, qos_data_type_e data_type, bool dscp_tagging)
 Enable QoS on the given socket for traffic to the specified destination.
 
void platf::free_gamepad (input_t &input, int nr)
 
void platf::freeInput (void *)
 
std::string_view platf::from_pix_fmt (pix_fmt_e pix_fmt)
 
std::string platf::from_sockaddr (const sockaddr *const)
 
std::pair< std::uint16_t, std::string > platf::from_sockaddr_ex (const sockaddr *const)
 
void platf::gamepad_battery (input_t &input, const gamepad_battery_t &battery)
 Send a gamepad battery event to the OS.
 
void platf::gamepad_motion (input_t &input, const gamepad_motion_t &motion)
 Send a gamepad motion event to the OS.
 
void platf::gamepad_touch (input_t &input, const gamepad_touch_t &touch)
 Send a gamepad touch event to the OS.
 
void platf::gamepad_update (input_t &input, int nr, const gamepad_state_t &gamepad_state)
 Updates virtual gamepad with the provided gamepad state.
 
platform_caps::caps_t platf::get_capabilities ()
 Get the supported platform capabilities to advertise to the client.
 
std::string platf::get_host_name ()
 Returns the current computer name in UTF-8.
 
std::string platf::get_mac_address (const std::string_view &address)
 
util::point_t platf::get_mouse_loc (input_t &input)
 Get the current mouse position on screen.
 
void platf::hscroll (input_t &input, int distance)
 
std::unique_ptr< deinit_tplatf::init ()
 
input_t platf::input ()
 
void platf::keyboard_update (input_t &input, uint16_t modcode, bool release, uint8_t flags)
 
void platf::move_mouse (input_t &input, int deltaX, int deltaY)
 
bool platf::needs_encoder_reenumeration ()
 Check if GPUs/drivers have changed since the last call to this function.
 
void platf::open_url (const std::string &url)
 Open a url in the default web browser.
 
void platf::pen_update (client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen)
 Send a pen event to the OS.
 
bool platf::process_group_running (std::uintptr_t native_handle)
 Check if a process group still has running children.
 
bool platf::request_process_group_exit (std::uintptr_t native_handle)
 Attempt to gracefully terminate a process group.
 
void platf::restart ()
 
boost::process::v1::child platf::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)
 
void platf::scroll (input_t &input, int distance)
 
bool platf::send (send_info_t &send_info)
 
bool platf::send_batch (batched_send_info_t &send_info)
 
int platf::set_env (const std::string &name, const std::string &value)
 Set an environment variable.
 
std::unique_ptr< deinit_tplatf::publish::start ()
 Main entry point for publication of our service on macOS.
 
void platf::streaming_will_start ()
 
void platf::streaming_will_stop ()
 
std::vector< supported_gamepad_t > & platf::supported_gamepads (input_t *input)
 Gets the supported gamepads for this platform backend.
 
void platf::touch_update (client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch)
 Send a touch event to the OS.
 
void platf::unicode (input_t &input, char *utf8, int size)
 
int platf::unset_env (const std::string &name)
 Unset an environment variable.
 

Variables

constexpr std::uint32_t platf::A = 0x1000
 
constexpr std::uint32_t platf::B = 0x2000
 
constexpr std::uint32_t platf::BACK = 0x0020
 
constexpr caps_t platf::platform_caps::controller_touch = 0x02
 
constexpr std::uint32_t platf::DPAD_DOWN = 0x0002
 
constexpr std::uint32_t platf::DPAD_LEFT = 0x0004
 
constexpr std::uint32_t platf::DPAD_RIGHT = 0x0008
 
constexpr std::uint32_t platf::DPAD_UP = 0x0001
 
constexpr std::uint32_t platf::HOME = 0x0400
 
constexpr std::uint32_t platf::LEFT_BUTTON = 0x0100
 
constexpr std::uint32_t platf::LEFT_STICK = 0x0040
 
constexpr std::uint8_t platf::speaker::map_stereo []
 
constexpr std::uint8_t platf::speaker::map_surround51 []
 
constexpr std::uint8_t platf::speaker::map_surround71 []
 
constexpr auto platf::MAX_GAMEPADS = 16
 
constexpr std::uint32_t platf::MISC_BUTTON = 0x200000
 
constexpr std::uint32_t platf::PADDLE1 = 0x010000
 
constexpr std::uint32_t platf::PADDLE2 = 0x020000
 
constexpr std::uint32_t platf::PADDLE3 = 0x040000
 
constexpr std::uint32_t platf::PADDLE4 = 0x080000
 
constexpr caps_t platf::platform_caps::pen_touch = 0x01
 
constexpr std::uint32_t platf::RIGHT_BUTTON = 0x0200
 
constexpr std::uint32_t platf::RIGHT_STICK = 0x0080
 
constexpr std::uint32_t platf::START = 0x0010
 
constexpr std::uint32_t platf::TOUCHPAD_BUTTON = 0x100000
 
constexpr std::uint32_t platf::X = 0x4000
 
constexpr std::uint32_t platf::Y = 0x8000
 

Detailed Description

Declarations for common platform specific utilities.

Macro Definition Documentation

◆ _CONVERT

#define _CONVERT ( x)
Value:
case pix_fmt_e::x: \
return #x##sv

Enumeration Type Documentation

◆ capture_e

enum class platf::capture_e : int
strong
Enumerator
ok 

Success.

reinit 

Need to reinitialize.

timeout 

Timeout.

interrupted 

Capture was interrupted.

error 

Error.

◆ gamepad_feedback_e

enum class platf::gamepad_feedback_e
strong
Enumerator
rumble 

Rumble.

rumble_triggers 

Rumble triggers.

set_motion_event_state 

Set motion event state.

set_rgb_led 

Set RGB LED.

◆ mem_type_e

enum class platf::mem_type_e
strong
Enumerator
system 

System memory.

vaapi 

VAAPI.

dxgi 

DXGI.

cuda 

CUDA.

videotoolbox 

VideoToolbox.

unknown 

Unknown.

◆ pix_fmt_e

enum class platf::pix_fmt_e
strong
Enumerator
yuv420p 

YUV 4:2:0.

yuv420p10 

YUV 4:2:0 10-bit.

nv12 

NV12.

p010 

P010.

ayuv 

AYUV.

yuv444p16 

Planar 10-bit (shifted to 16-bit) YUV 4:4:4.

y410 

Y410.

unknown 

Unknown.

◆ qos_data_type_e

enum class platf::qos_data_type_e : int
strong
Enumerator
audio 

Audio.

video 

Video.

◆ speaker_e

Enumerator
FRONT_LEFT 

Front left.

FRONT_RIGHT 

Front right.

FRONT_CENTER 

Front center.

LOW_FREQUENCY 

Low frequency.

BACK_LEFT 

Back left.

BACK_RIGHT 

Back right.

SIDE_LEFT 

Side left.

SIDE_RIGHT 

Side right.

MAX_SPEAKERS 

Maximum number of speakers.

◆ thread_priority_e

enum class platf::thread_priority_e : int
strong
Enumerator
low 

Low priority.

normal 

Normal priority.

high 

High priority.

critical 

Critical priority.

Function Documentation

◆ alloc_gamepad()

int platf::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.

Parameters
inputThe global input context.
idThe gamepad ID.
metadataController metadata from client (empty if none provided).
feedback_queueThe queue for posting messages back to the client.
Returns
0 on success.

◆ allocate_client_input_context()

std::unique_ptr< client_input_t > platf::allocate_client_input_context ( input_t & input)

Allocate a context to store per-client input data.

Allocates a context to store per-client input data.

Parameters
inputThe global input context.
Returns
A unique pointer to a per-client input data context.

◆ appdata()

std::filesystem::path platf::appdata ( )

Performs migration if necessary, then returns the appdata directory.

This is used for the log directory, so it cannot invoke Boost logging!

Returns
The path of the appdata directory that should be used.

◆ create_high_precision_timer()

std::unique_ptr< high_precision_timer > platf::create_high_precision_timer ( )

Create platform-specific timer capable of high-precision sleep.

Returns
A unique pointer to timer

◆ display()

std::shared_ptr< display_t > platf::display ( platf::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 monitor that's compatible you can find If you require to use this parameter in a separate thread, make a copy of it.

Parameters
display_nameThe name of the monitor that SHOULD be displayed
configStream configuration
Returns
The display_t instance based on hwdevice_type.

Pick a display adapter and capture method.

Parameters
hwdevice_typeenables possible use of hardware encoder

◆ enable_socket_qos()

std::unique_ptr< deinit_t > platf::enable_socket_qos ( uintptr_t native_socket,
boost::asio::ip::address & address,
uint16_t port,
qos_data_type_e data_type,
bool dscp_tagging )

Enable QoS on the given socket for traffic to the specified destination.

Enables QoS on the given socket for traffic to the specified destination.

Parameters
native_socketThe native socket handle.
addressThe destination address for traffic sent on this socket.
portThe destination port for traffic sent on this socket.
data_typeThe type of traffic sent on this socket.
dscp_taggingSpecifies whether to enable DSCP tagging on outgoing traffic.

◆ gamepad_battery()

void platf::gamepad_battery ( input_t & input,
const gamepad_battery_t & battery )

Send a gamepad battery event to the OS.

Sends a gamepad battery event to the OS.

Parameters
inputThe global input context.
batteryThe battery event.

◆ gamepad_motion()

void platf::gamepad_motion ( input_t & input,
const gamepad_motion_t & motion )

Send a gamepad motion event to the OS.

Sends a gamepad motion event to the OS.

Parameters
inputThe global input context.
motionThe motion event.

◆ gamepad_touch()

void platf::gamepad_touch ( input_t & input,
const gamepad_touch_t & touch )

Send a gamepad touch event to the OS.

Sends a gamepad touch event to the OS.

Parameters
inputThe global input context.
touchThe touch event.

◆ gamepad_update()

void platf::gamepad_update ( input_t & input,
int nr,
const gamepad_state_t & gamepad_state )

Updates virtual gamepad with the provided gamepad state.

Parameters
inputThe input context.
nrThe gamepad index to update.
gamepad_stateThe gamepad button/axis state sent from the client.

◆ get_capabilities()

platform_caps::caps_t platf::get_capabilities ( )

Get the supported platform capabilities to advertise to the client.

Returns the supported platform capabilities to advertise to the client.

Returns
Capability flags.

◆ get_host_name()

std::string platf::get_host_name ( )

Returns the current computer name in UTF-8.

Returns
Computer name or a placeholder upon failure.

◆ get_mouse_loc()

util::point_t platf::get_mouse_loc ( input_t & input)

Get the current mouse position on screen.

Parameters
inputThe input_t instance to use.
Returns
Screen coordinates of the mouse.

Examples

auto [x, y] = get_mouse_loc(input);

◆ needs_encoder_reenumeration()

bool platf::needs_encoder_reenumeration ( )

Check if GPUs/drivers have changed since the last call to this function.

Returns if GPUs/drivers have changed since the last call to this function.

Returns
true if a change has occurred or if it is unknown whether a change occurred.

◆ open_url()

void platf::open_url ( const std::string & url)

Open a url in the default web browser.

Parameters
urlThe url to open.

◆ pen_update()

void platf::pen_update ( client_input_t * input,
const touch_port_t & touch_port,
const pen_input_t & pen )

Send a pen event to the OS.

Sends a pen event to the OS.

Parameters
inputThe client-specific input context.
touch_portThe current viewport for translating to screen coordinates.
penThe pen event.

◆ process_group_running()

bool platf::process_group_running ( std::uintptr_t native_handle)

Check if a process group still has running children.

Parameters
native_handleThe native handle of the process group.
Returns
true if processes are still running.

◆ request_process_group_exit()

bool platf::request_process_group_exit ( std::uintptr_t native_handle)

Attempt to gracefully terminate a process group.

Parameters
native_handleThe native handle of the process group.
Returns
true if termination was successfully requested.

◆ set_env()

int platf::set_env ( const std::string & name,
const std::string & value )

Set an environment variable.

Parameters
nameThe name of the environment variable.
valueThe value to set the environment variable to.
Returns
0 on success, non-zero on failure.

◆ start()

std::unique_ptr<::platf::deinit_t > platf::publish::start ( )

Main entry point for publication of our service on macOS.

This function initiates a connection to the macOS mDNS service and requests to register our Sunshine service. Registration will occur asynchronously (unless it fails immediately, which is probably only possible if the host machine is misconfigured).

Returns
Either nullptr (if the registration fails immediately) or a uniqur_ptr<deinit_t>, which will manage polling for a response from the mDNS service, and then, when deconstructed, will deregister the service.

◆ supported_gamepads()

std::vector< supported_gamepad_t > & platf::supported_gamepads ( input_t * input)

Gets the supported gamepads for this platform backend.

This may be called prior to platf::input()!

Parameters
inputPointer to the platform's input_t or nullptr.
Returns
Vector of gamepad options and status.

◆ touch_update()

void platf::touch_update ( client_input_t * input,
const touch_port_t & touch_port,
const touch_input_t & touch )

Send a touch event to the OS.

Sends a touch event to the OS.

Parameters
inputThe client-specific input context.
touch_portThe current viewport for translating to screen coordinates.
touchThe touch event.

◆ unset_env()

int platf::unset_env ( const std::string & name)

Unset an environment variable.

Parameters
nameThe name of the environment variable.
Returns
0 on success, non-zero on failure.

Variable Documentation

◆ map_stereo

constexpr std::uint8_t platf::speaker::map_stereo[]
constexpr
Initial value:
{
}
@ FRONT_LEFT
Front left.
Definition common.h:176
@ FRONT_RIGHT
Front right.
Definition common.h:177

◆ map_surround51

constexpr std::uint8_t platf::speaker::map_surround51[]
constexpr
Initial value:
{
}
@ BACK_LEFT
Back left.
Definition common.h:180
@ LOW_FREQUENCY
Low frequency.
Definition common.h:179
@ FRONT_CENTER
Front center.
Definition common.h:178
@ BACK_RIGHT
Back right.
Definition common.h:181

◆ map_surround71

constexpr std::uint8_t platf::speaker::map_surround71[]
constexpr
Initial value:
{
}
@ SIDE_RIGHT
Side right.
Definition common.h:183
@ SIDE_LEFT
Side left.
Definition common.h:182